Implement Spark-compatible `floor(expr, scale)` (two-argument form)
Maintainer thường phản hồi trong vòng 1 ngày
Chưa có ai nhận issue này.
Đánh giá
- Độ khó
- 4/5
- Thời gian dự kiến
- 3-5 ngày
- Mức phù hợp với người mới
- 18/100
Hướng nghiên cứu
Start by reading crates/spark/src/function/math/floor.rs and round.rs to see how the existing one-argument SparkFloor and the HALF_UP decimal rounding on unscaled integers are wired up, then compare against Spark's FloorExpressionBuilder/RoundBase in mathExpressions.scala. The work is to accept an optional literal scale, derive the result DECIMAL in return_field_from_args, and share a RoundBase-style helper with ceil. Done means the new two-argument cases in crates/sqllogictest/test_files/math/floor.slt pass and SparkFloor's #[user_doc] is regenerated via dev/update_function_docs.sh.
Do mô hình lập chỉ mục viết ra từ nội dung của issue.
Mô tả
Part of #174 and apache/datafusion-comet#7300.
What the function does
floor(expr, scale) rounds expr down (toward negative infinity) to scale decimal places; a negative scale rounds to the left of the decimal point. Spark builds it through FloorExpressionBuilder: one argument gives Floor (already implemented as crates/spark/src/function/math/floor.rs), two arguments give RoundFloor, which uses RoundBase with RoundingMode.FLOOR. Available since 3.3.0. The matching ceil(expr, scale) form is already tracked in apache/datafusion-spark#15 and apache/datafusion-spark#22 and should share the implementation.
scale must be a foldable, non-NULL expression whose type is exactly INT (checked before type coercion, so 2L or CAST(2 AS SMALLINT) is rejected). expr is implicitly cast to DECIMAL (inputTypes = Seq(DecimalType, IntegerType)): TINYINT to DECIMAL(3,0), SMALLINT to DECIMAL(5,0), INT to DECIMAL(10,0), BIGINT to DECIMAL(20,0), FLOAT to DECIMAL(14,7), DOUBLE to DECIMAL(30,15). For an input DECIMAL(p, s) the result is DECIMAL(min(p - s + 1 + min(s, scale), 38), min(s, scale)) when scale >= 0, and DECIMAL(min(max(p - s + 1, -scale + 1), 38), 0) when scale < 0. The result is always nullable.
Examples
SELECT floor(3.1411, 3);
-- 3.141
SELECT floor(3.1411, -3);
-- 0
SELECT floor(-0.35, 1);
-- -0.4 decimal(2,1)
SELECT floor(-35, -1);
-- -40 decimal(11,0)
SELECT floor(5, 0);
-- 5 decimal(11,0)
SELECT floor(-9.9, 0);
-- -10 decimal(2,0)
SELECT floor(CAST(-99 AS DECIMAL(2, 0)), -1);
-- -100 decimal(3,0)
SELECT floor(3.14115, -3);
-- 0 decimal(4,0)
SELECT floor(2.5, null);
-- error NON_FOLDABLE_ARGUMENT
Edge cases to test
- NULL
exprreturns NULL. NULL or non-literalscalefails analysis:NON_FOLDABLE_ARGUMENT(SQLSTATE 42K08)The function `floor` requires the parameter `scale` to be a foldable expression of the type "INT", but the actual argument is a non-foldable.(also forfloor(2.5, 'a')). Three arguments:WRONG_NUM_ARGS.WITHOUT_SUGGESTION. - Result types for every input type, including the
p - s + 1growth and the cap at 38 (floor(CAST(-99 AS DECIMAL(2,0)), -1)isDECIMAL(3,0)). scalelarger than the input scale leaves the value unchanged (RoundFloor(bdPi, 8..10)returnsbdPiinMathExpressionsSuite).- Negative numbers round away from zero:
floor(-0.1, 0)is -1,floor(-2.5, 0)is -3,floor(135.135, -2)is 100. - Negative scale beyond the integral digits:
floor(3.1411, -3)is 0,floor(-3.1411, -3)is -1000. - Overflow on the negative-scale path when the precision is already 38:
floor(CAST(-99999999999999999999999999999999999999 AS DECIMAL(38,0)), -1)needs 39 digits and throwsNUMERIC_VALUE_OUT_OF_RANGE.WITHOUT_SUGGESTIONfromDecimal.set(derived from code, regardless of ANSI mode). - FLOAT / DOUBLE inputs go through the implicit cast first:
- Values that do not fit (
|x| >= 10^7for FLOAT,|x| >= 10^15for DOUBLE) are NULL with ANSI off and fail withNUMERIC_VALUE_OUT_OF_RANGE.WITH_SUGGESTIONwithspark.sql.ansi.enabled=true(the 4.0+ default). - NaN and +/-Infinity become NULL in both modes (the cast catches
NumberFormatException). - The cast uses
BigDecimal.valueOf(double)(shortest decimal string), then HALF_UP to the cast scale, so FLOAT0.1fbecomes0.1000000.
- Values that do not fit (
- BIGINT inputs near
Long.MaxValuefitDECIMAL(20,0). - Decimal32/64/256 Arrow inputs if accepted (Spark only has 128-bit-range decimals up to precision 38).
Differences across Spark versions
- 3.4 and 3.5: a NULL or non-foldable
scalefails with_LEGACY_ERROR_TEMP_1100(The 'scale' parameter of function 'floor' needs to be a int literal.); 4.0+ usesNON_FOLDABLE_ARGUMENT. - 3.4: the positive-scale path called
decimal.toPrecision(decimal.precision, s, mode)(the value's precision); 3.5+ uses the result type precisionp. - 4.0: ANSI on by default, which changes the FLOAT/DOUBLE overflow case from NULL to an error.
Implementation notes
- Spark:
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/expressions/mathExpressions.scala,FloorExpressionBuilder,CeilFloorExpressionBuilderBase,RoundFloorandRoundBase(dataType,nullSafeEval). - DataFusion core
floortakes one argument and keeps the input type. DataFusion'strunc/roundwith a precision argument use different rounding modes and keep floating-point types. - In
datafusion-spark, extendcrates/spark/src/function/math/floor.rsto accept the optionalscale(decided inreturn_field_from_argsfrom the literal), and share aRoundBase-style decimal rounding helper withceilandround(round.rsalready has HALF_UP decimal rounding on unscaled integers; add FLOOR/CEILING modes). Implement the implicit casts in the signature (or require the caller to cast), with DOUBLE/FLOAT conversion matching Spark's cast. - Tests: extend
crates/sqllogictest/test_files/math/floor.sltwith the two-argument cases, and update the#[user_doc]onSparkFloor(then rundev/update_function_docs.sh).
This issue was generated by an LLM (Claude) from the Apache Spark source code and tests. Examples come from Spark's ExpressionDescription examples, test suites and .sql.out golden files, but please verify them against Spark before relying on them.
- Ngôn ngữ chính
- Rust
- Star
- 7
- Fork
- 15
- Merge trung bình
- 16 giờ 43 phút
- Pull request đã merge (30 ngày)
- 26
Chuẩn bị môi trường
- Không có Dockerfile hay tệp Docker Compose
- Không có mẫu pull request
- Đọc hướng dẫn đóng góp
Bắt đầu từ đâu
- Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
- Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
- Fork repository và làm thay đổi trên một nhánh.
- Mở pull request có tham chiếu số hiệu của issue.
Issue khác của apache/datafusion-spark
-
enhancement
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 70/100
apache/datafusion-spark#287 ·
Maintainer thường phản hồi trong vòng 1 ngày
-
enhancement good first issue
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 80/100
apache/datafusion-spark#266 ·
Maintainer thường phản hồi trong vòng 1 ngày
-
[Bug] make_dt_interval returns NULL for more than about 1.49 million days, Spark returns the intervalCó thể đã có người làm @Subodh-17 đã nhận hôm nay. Đang mở
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 78/100
apache/datafusion-spark#109 ·
Maintainer thường phản hồi trong vòng 1 ngày
-
enhancement
Độ khó 5/5 Hơn một tuần Mức phù hợp với người mới 18/100
apache/datafusion-spark#299 ·
Maintainer thường phản hồi trong vòng 1 ngày
-
enhancement
Độ khó 4/5 3-5 ngày Mức phù hợp với người mới 35/100
apache/datafusion-spark#298 ·
Maintainer thường phản hồi trong vòng 1 ngày
Tất cả issue của apache/datafusion-spark
Issue tương tự
-
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 70/100
storytold/photocraft#15232 · 1 bình luận ·
Maintainer thường phản hồi trong vòng 1 ngày
-
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 68/100
suhina-daw/suhina-daw#1477 ·
Maintainer thường phản hồi trong vòng 1 ngày
-
parity priority: low ui
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 62/100
storytold/pdfcraft#7483 · 1 bình luận ·
Maintainer thường phản hồi trong vòng 2 ngày
-
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 72/100
IngvarConsulting/unica#3097 ·
Maintainer thường phản hồi trong vòng 1 ngày
-
enhancement in-progress
Độ khó 1/5 Dưới một giờ Mức phù hợp với người mới 64/100