Skip to content

[SPARK-41793][SQL] Incorrect result for window frames defined by a range clause on large decimals #40138

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

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,7 @@ case class CheckOverflowInSum(
}

/**
* An add expression for decimal values which is only used internally by Sum/Avg.
* An add expression for decimal values which is only used internally by Sum/Avg/Window.
*
* Nota that, this expression does not check overflow which is different with `Add`. When
* aggregating values, Spark writes the aggregation buffer values to `UnsafeRow` via
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,7 @@ trait WindowExecBase extends UnaryExecNode {
TimestampAddYMInterval(expr, boundOffset, Some(timeZone))
case (TimestampType | TimestampNTZType, _: DayTimeIntervalType) =>
TimeAdd(expr, boundOffset, Some(timeZone))
case (d: DecimalType, _: DecimalType) => DecimalAddNoOverflowCheck(expr, boundOffset, d)
case (a, b) if a == b => Add(expr, boundOffset)
}
val bound = MutableProjection.create(boundExpr :: Nil, child.output)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@

package org.apache.spark.sql

import org.apache.spark.sql.catalyst.expressions.{Ascending, Literal, NonFoldableLiteral, RangeFrame, SortOrder, SpecifiedWindowFrame, UnspecifiedFrame}
import org.apache.spark.sql.catalyst.expressions.{Ascending, Literal, NonFoldableLiteral, RangeFrame, SortOrder, SpecifiedWindowFrame, UnaryMinus, UnspecifiedFrame}
import org.apache.spark.sql.catalyst.plans.logical.{Window => WindowNode}
import org.apache.spark.sql.expressions.{Window, WindowSpec}
import org.apache.spark.sql.functions._
Expand Down Expand Up @@ -474,4 +474,22 @@ class DataFrameWindowFramesSuite extends QueryTest with SharedSparkSession {
checkAnswer(df,
Row(3, 1.5) :: Row(3, 1.5) :: Row(6, 2.0) :: Row(6, 2.0) :: Row(6, 2.0) :: Nil)
}

test("SPARK-41793: Incorrect result for window frames defined by a range clause on large " +
"decimals") {
val window = new WindowSpec(Seq($"a".expr), Seq(SortOrder($"b".expr, Ascending)),
SpecifiedWindowFrame(RangeFrame,
UnaryMinus(Literal(BigDecimal(10.2345))), Literal(BigDecimal(6.7890))))

val df = Seq(
1 -> "11342371013783243717493546650944543.47",
1 -> "999999999999999999999999999999999999.99"
).toDF("a", "b")
.select($"a", $"b".cast("decimal(38, 2)"))
.select(count("*").over(window))

checkAnswer(
df,
Row(1) :: Row(1) :: Nil)
}
}