Implement Spark-compatible `bround`
メンテナーはふだん 1 日以内に返信
まだ誰も着手していません。
評価
調査の方向性
Read crates/spark/src/function/math/round.rs first, then compare with Spark's BRound/RoundBase in mathExpressions.scala. Generalise the existing integer and decimal helpers over the rounding mode, add the float shortest-decimal path, and wire ANSI overflow via datafusion.execution.enable_ansi_mode. Enable and extend the commented queries in crates/sqllogictest/test_files/math/bround.slt; done means that suite passes for the listed edge cases and the function carries #[user_doc].
索引モデルが issue の本文から書いたものです。
説明
Part of #174 and apache/datafusion-comet#7300.
What the function does
bround(expr[, d]) rounds expr to d decimal places (default 0) using HALF_EVEN ("banker's") rounding. A negative d rounds to the left of the decimal point. It is BRound, a RoundBase with RoundingMode.HALF_EVEN, the same code as round except for the rounding mode. Available since 2.0.0.
expr accepts any numeric type and d is implicitly cast to INT and must be foldable (DATATYPE_MISMATCH.NON_FOLDABLE_INPUT otherwise). The result type equals the input type for TINYINT, SMALLINT, INT, BIGINT, FLOAT and DOUBLE. For DECIMAL(p, s) it is DECIMAL(min(p - s + 1 + min(s, d), 38), min(s, d)) when d >= 0 and DECIMAL(min(max(p - s + 1, -d + 1), 38), 0) when d < 0. The result is always nullable; a NULL d returns NULL without evaluating expr.
Examples
SELECT bround(2.5, 0);
-- 2 decimal(2,0)
SELECT bround(25, -1);
-- 20
SELECT bround(525s, -1);
-- 520 smallint
SELECT bround(525L, -3);
-- 1000 bigint
SELECT bround(25y, -2);
-- 0 tinyint
SELECT bround(2147483647, -1);
-- error ARITHMETIC_OVERFLOW (ANSI on)
-- -2147483646 (ANSI off)
SELECT bround(-128y, -1);
-- error ARITHMETIC_OVERFLOW (ANSI on)
-- 126 (ANSI off)
MathExpressionsSuite asserts bround(2.5D, 0) = 2.0, bround(3.5D, 0) = 4.0, bround(-2.5D, 0) = -2.0, bround(-0.35D, 1) = -0.4, bround(-35, -1) = -40 and bround(45.00BD, -1) = 40 (where round gives 50).
Edge cases to test
- NULL
expr, NULLd(literal), non-foldabled(analysis errorDATATYPE_MISMATCH.NON_FOLDABLE_INPUT). - Ties in both directions and at several scales: 0.5, 1.5, 2.5, -2.5, 0.125 with
d = 2, 25/35/45 withd = -1. - Integer types with negative
d: result stays the same integer type; overflow with ANSI on fails withARITHMETIC_OVERFLOW(SQLSTATE 22003, messageOverflow, configspark.sql.ansi.enabled), with ANSI off it wraps (goldennonansi/math.sql.out:bround(32767s, -1)is -32766,bround(-9223372036854775808L, -1)is 9223372036854775806). Very negativedreturns 0 (bround(25y, -3)is 0). - Integer types with
d >= 0return the input unchanged. - FLOAT / DOUBLE: Spark converts with
BigDecimal.valueOf(double)(the decimal string fromDouble.toString), rounds HALF_EVEN, then converts back. Sobround(2.675D, 2)rounds the decimal 2.675 and gives 2.68, while(x * 100).round_ties_even() / 100gives 2.67 because 2.675 * 100 is 267.49999999999997 in binary. FLOAT is widened to DOUBLE first (bround(3.1415f, 4)is3.1415f). - NaN and +/-Infinity are returned unchanged. -0.0 and negative values that round to zero (
bround(-0.4D, 0)) return0.0, not-0.0, becauseBigDecimalhas no negative zero. - Very large
d(e.g. 400) on DOUBLE leaves the value unchanged; very negativedgives 0.0. - DECIMAL: result precision/scale per the formula above, including the cap at 38.
dlarger than the scale returns the value unchanged with the input scale. On the negative-dpath, a result that needs 39 digits throwsNUMERIC_VALUE_OUT_OF_RANGE.WITHOUT_SUGGESTIONfromDecimal.set(derived from code). - Column
exprwith scalard, and a scalarexpr.
Differences across Spark versions
- 4.0: ANSI on by default, so integer overflow now errors by default (
ARITHMETIC_OVERFLOW); it wraps in 3.x defaults. - 3.4: the decimal positive-scale path used the value's precision in
toPrecision(decimal.precision, s, mode); 3.5+ uses the result type precision. NON_FOLDABLE_INPUTinputNameisscalein 3.x and`scale`in 4.0+.Double.toStringchanged in JDK 19 to always produce the shortest repr; on older JDKs a few doubles have a longer digit string, which can change a HALF_EVEN tie. This applies whenever Spark runs on JDK 17 or older.
Implementation notes
- Spark:
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/expressions/mathExpressions.scala,BRoundandRoundBase(dataType,nullSafeEval,doGenCode). - DataFusion core has no
bround; itsroundrounds half away from zero and keeps floating-point arithmetic. datafusion-sparkhascrates/spark/src/function/math/round.rs(HALF_UP). Generalise its integer and decimal helpers over the rounding mode and reuse them, but note two known gaps there thatbroundshould not inherit: decimal results keep the input scale instead of Spark's reduced scale (apache/datafusion-spark#37), and floating-point rounding multiplies by10^dinstead of rounding the shortest decimal representation (Rust's{}formatting off64gives the shortest round-trip digits, which can be fed to a decimal rounding routine). Usedatafusion.execution.enable_ansi_modefor the overflow behaviour and raiseARITHMETIC_OVERFLOW.- Tests: the placeholder
crates/sqllogictest/test_files/math/bround.sltexists with commented-out queries; enable and extend it. Document with#[user_doc].
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.
- 主要言語
- Rust
- スター
- 7
- フォーク
- 15
- 平均マージ
- 16時間 43分
- マージ済み PR(30日)
- 26
環境構築
- Dockerfile・Docker Compose ファイルなし
- プルリクエストのテンプレートなし
- コントリビューションガイドを読む
はじめの一歩
- issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
- 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
- リポジトリをフォークし、ブランチを切って変更します。
- issue 番号を参照したプルリクエストを送ります。
apache/datafusion-spark のほかの issue
-
enhancement
難易度 2/5 1〜3時間 初心者へのやさしさ 70/100
apache/datafusion-spark#287 ·
メンテナーはふだん 1 日以内に返信
-
enhancement good first issue
難易度 2/5 1〜3時間 初心者へのやさしさ 80/100
apache/datafusion-spark#266 ·
メンテナーはふだん 1 日以内に返信
-
[Bug] make_dt_interval returns NULL for more than about 1.49 million days, Spark returns the interval対応中かも @Subodh-17 が今日担当しました。 オープン
難易度 2/5 1〜3時間 初心者へのやさしさ 78/100
apache/datafusion-spark#109 ·
メンテナーはふだん 1 日以内に返信
-
enhancement
難易度 5/5 1週間以上 初心者へのやさしさ 18/100
apache/datafusion-spark#299 ·
メンテナーはふだん 1 日以内に返信
-
enhancement
難易度 4/5 3〜5日 初心者へのやさしさ 35/100
apache/datafusion-spark#298 ·
メンテナーはふだん 1 日以内に返信
apache/datafusion-spark の issue をすべて見る
似ている issue
-
難易度 2/5 1〜3時間 初心者へのやさしさ 70/100
storytold/photocraft#15232 · コメント 1 件 ·
メンテナーはふだん 1 日以内に返信
-
難易度 2/5 1〜3時間 初心者へのやさしさ 68/100
suhina-daw/suhina-daw#1477 ·
メンテナーはふだん 1 日以内に返信
-
parity priority: low ui
難易度 2/5 1〜3時間 初心者へのやさしさ 62/100
storytold/pdfcraft#7483 · コメント 1 件 ·
メンテナーはふだん 2 日以内に返信
-
難易度 2/5 1〜3時間 初心者へのやさしさ 72/100
IngvarConsulting/unica#3097 ·
メンテナーはふだん 1 日以内に返信
-
enhancement in-progress
難易度 1/5 1時間未満 初心者へのやさしさ 64/100