Repository navigation
feat: native RANGE window frames with explicit offset on " DECIMAL " ORDER BY #4987
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
42b64bf
de51bd4
2c19ce6
aca4d3c
7468551
76f8ba2
cc5ae3a
45b179f
83a6bf3
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -393,30 +393,30 @@ object CometWindowExec extends CometOperatorSerde[WindowExec] { | |
| } | ||
| } | ||
|
|
||
| // Comet's native window planner ships RANGE frame offsets as | ||
| // ScalarValue::Int64, which the DECIMAL comparator can't handle: Spark | ||
| // decimal arithmetic widens precision on +/-, so the computed boundary | ||
| // (e.g. Decimal(11,0)) doesn't match the current value's precision | ||
| // (e.g. Decimal(10,0)) and the comparator fails with "Uncomparable | ||
| // values". Fall back for DECIMAL until the native side normalizes | ||
| // precision. DATE is now handled natively by wrapping the offset as | ||
| // IntervalDayTime in the planner. UNBOUNDED / CURRENT ROW bounds don't | ||
| // evaluate the offending arithmetic and stay native regardless. | ||
| // DataFusion computes a RANGE frame bound as `current +/- offset` and treats | ||
| // a bound that overflows as the edge of the partition, which also takes in | ||
| // NULL keys sorted at that edge. Spark's bound never overflows that way, so | ||
| // fall back for the offsets that can overflow. UNBOUNDED / CURRENT ROW | ||
| // bounds skip the arithmetic and stay native. | ||
| // | ||
| // For DATE, Spark computes the bound with DateAdd, an Int addition that | ||
| // wraps silently on overflow, while DataFusion treats an overflowing bound | ||
| // as the partition edge. Over dates in Spark's supported range the two only | ||
| // disagree when the offset is large enough to wrap, so fall back for those | ||
| // offsets. https://github.com/apache/datafusion-comet/issues/5022 | ||
| // wraps silently on overflow. Over dates in Spark's supported range the two | ||
| // only disagree when the offset is large enough to wrap, so fall back for | ||
| // those offsets. | ||
| // | ||
| // For DECIMAL, Spark's DecimalAddNoOverflowCheck keeps the exact bound, | ||
| // while DataFusion adds in Decimal128 and overflows past i128. | ||
| // https://github.com/apache/datafusion-comet/issues/5022 | ||
| f match { | ||
| case SpecifiedWindowFrame(RangeFrame, lb, ub) | ||
| if !lb.isInstanceOf[SpecialFrameBoundary] || | ||
| !ub.isInstanceOf[SpecialFrameBoundary] => | ||
| windowExpr.windowSpec.orderSpec.headOption.map(_.dataType) match { | ||
| case Some(_: DecimalType) => | ||
| case Some(dt: DecimalType) if !Seq(lb, ub).forall(isDecimalRangeOffsetSafe(_, dt)) => | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Could an ASC lower bound like |
||
| withFallbackReason( | ||
| windowExpr, | ||
| "RANGE frame with explicit offset on DECIMAL ORDER BY is not supported") | ||
| s"RANGE frame on ${dt.catalogString} ORDER BY with an offset whose bound can " + | ||
| "overflow Decimal128 is not supported") | ||
| return None | ||
| case Some(DateType) if !Seq(lb, ub).forall(isDateRangeOffsetSafe) => | ||
| withFallbackReason( | ||
|
|
@@ -700,6 +700,35 @@ object CometWindowExec extends CometOperatorSerde[WindowExec] { | |
| } | ||
| } | ||
|
|
||
| private val MaxDecimal128Unscaled: BigInt = BigInt(2).pow(127) - 1 | ||
|
|
||
| // DataFusion rescales both operands to the larger scale and adds them as | ||
| // i128, so the bound cannot overflow when the largest value the ORDER BY | ||
| // type holds plus the offset fits in an i128. | ||
| private def isDecimalRangeOffsetSafe(bound: Expression, orderType: DecimalType): Boolean = | ||
| bound match { | ||
| case _: SpecialFrameBoundary => true | ||
| case e => | ||
| val offset = | ||
| try { | ||
| e.eval() | ||
| } catch { | ||
| // Rejected when the bound is serialized. | ||
| case _: Exception => return true | ||
| } | ||
| offset match { | ||
| case d: Decimal if d.scale >= 0 && orderType.scale >= 0 => | ||
| val scale = math.max(orderType.scale, d.scale) | ||
| val maxValue = (BigInt(10).pow(orderType.precision) - 1) * | ||
| BigInt(10).pow(scale - orderType.scale) | ||
| val offsetValue = | ||
| BigInt(d.toJavaBigDecimal.unscaledValue).abs * BigInt(10).pow(scale - d.scale) | ||
| maxValue + offsetValue <= MaxDecimal128Unscaled | ||
| case _: Decimal => false | ||
| case _ => true | ||
| } | ||
| } | ||
|
|
||
| // Folds a RANGE frame bound expression to a constant and serializes its | ||
| // magnitude as a typed Literal proto. Spark encodes PRECEDING/FOLLOWING via | ||
| // the sign of the literal (negative => PRECEDING, positive => FOLLOWING), | ||
|
|
||
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -248,16 +248,14 @@ ORDER BY cate, val_date | |||||||||||||||||||||
| -- Ported from Spark's typeCoercion/native/windowFrameCoercion.sql: | ||||||||||||||||||||||
| -- SELECT COUNT(*) OVER (PARTITION BY 1 ORDER BY cast(1 as decimal(10, 0)) DESC | ||||||||||||||||||||||
| -- RANGE BETWEEN CURRENT ROW AND 1 FOLLOWING) FROM t | ||||||||||||||||||||||
| -- Comet falls back because Spark decimal arithmetic widens precision on | ||||||||||||||||||||||
| -- +/-, so the native frame-boundary arithmetic produces e.g. Decimal(11,0) | ||||||||||||||||||||||
| -- while the current row stays Decimal(10,0), and DataFusion's comparator | ||||||||||||||||||||||
| -- fails with "Uncomparable values". Once the native planner preserves the | ||||||||||||||||||||||
| -- ORDER BY column's precision when computing RANGE boundaries, the guard | ||||||||||||||||||||||
| -- in CometWindowExec can be removed and this test will start failing | ||||||||||||||||||||||
| -- because Comet stops falling back — that's the signal to re-enable it. | ||||||||||||||||||||||
| -- Runs natively: Spark's WindowFrameTypeCoercion casts the frame offset to | ||||||||||||||||||||||
| -- the same Decimal type as the ORDER BY column, so the boundary arithmetic | ||||||||||||||||||||||
| -- keeps the current row's precision and DataFusion's comparator has matching | ||||||||||||||||||||||
| -- operands. The DATE case in 1.11 keeps an IntegerType offset instead, which | ||||||||||||||||||||||
| -- the native planner coerces to IntervalDayTime. | ||||||||||||||||||||||
|
Comment on lines
+251
to
+255
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit: Could we explain why this now passes? The bound still widens to DECIMAL(11,0); I think it works because DataFusion compares decimals with matching scales regardless of precision.
Suggested change
|
||||||||||||||||||||||
| -- ============================================================ | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| query expect_fallback(RANGE frame with explicit offset on DECIMAL ORDER BY is not supported) | ||||||||||||||||||||||
| query | ||||||||||||||||||||||
| SELECT COUNT(*) OVER (PARTITION BY 1 | ||||||||||||||||||||||
| ORDER BY CAST(1 AS DECIMAL(10, 0)) DESC | ||||||||||||||||||||||
| RANGE BETWEEN CURRENT ROW AND 1 FOLLOWING) AS c | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
[P2] [P2] Preserve Spark's descending decimal-offset negation
Could this fallback also account for offsets whose magnitude changes when Spark negates them for descending order? With
d DECIMAL(35,0)containing[N,0], whereN = 10000000000000000000000000000000001,COUNT(*) OVER (ORDER BY d DESC RANGE BETWEEN CURRENT ROW AND N FOLLOWING)returns[1,1]on Spark 4.1.3 under both ANSI settings. Spark'sUnaryMinus(offset)rounds to-10000000000000000000000000000000000, leaving the first row's boundary at1. This guard returns true, andrangeBoundLiteralpreserves the positive offset exactly, so the native frame subtractsN, reaches0, and returns[2,1]. The previous blanket fallback preserved Spark's result. Please retain fallback for affected descending offsets or serialize Spark's effective offset, with a regression containing rows at the differing boundary.Evidence: Reproduction: create
twithSELECT CAST(str AS DECIMAL(35,0)) AS d FROM VALUES ('10000000000000000000000000000000001'), ('0') AS v(str), then runSELECT d, COUNT(*) OVER (ORDER BY d DESC RANGE BETWEEN CURRENT ROW AND 10000000000000000000000000000000001 FOLLOWING) AS c FROM t ORDER BY d DESC. Spark 4.1.3 produced counts[1,1]with ANSI true and false. The compiled exact-headisDecimalRangeOffsetSafereturned true and the serializedd.absretained all 35 digits. The lockfile-matching DataFusion 55.2.0/Arrow 59.3.0WindowFrameContext, usingCurrentRowand the exact decimalFollowingscalar, produced[2,1]. Also reproduced withDECIMAL(35,34)and offset1.0000000000000000000000000000000001. Spark 3.5.9 returned[2,1], confirming the version-dependent behavior. Harnesses and logs are under/tmp/comet4987-83a6-review/.