-
Notifications
You must be signed in to change notification settings - Fork 29k
[SPARK-28067][SQL] Fix incorrect results for decimal aggregate sum by returning null on decimal overflow #27627
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
Closed
Changes from 1 commit
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
edb32b9
Fix the incorrect results for sum for decimal overflow, support repor…
skambha 8fe9aa4
Add the jira testcase
skambha 1adc512
Change formatting
skambha d90c790
hasoverflow changes
skambha 136e6dc
Fix formatting
skambha 6979e8d
Add a new implementation for sum for dec type to handle overflow, Add…
skambha 4119e02
Put back the decimal handling changes into Sum like how we started with
skambha cc2fec0
Use a new flag isEmptyOrNulls to handle the scenario with all nulls a…
skambha 23739c9
Rebase
skambha fa45378
rebase
skambha fbd80a6
Add new test and also make the agg buffer changes only for decimal type
skambha de2d68f
simplify
cloud-fan 8339e28
Fix the test failure
skambha 59a00c4
Cleanup comments
skambha 7795888
cleanup code comments
skambha File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Add a new implementation for sum for dec type to handle overflow, Add…
… a new DecimalSum and add a analyzer rule and changes to optimizer rule. Add tests For now the DecimalSum has the code to handle other types, incase we want to put this logic back into Sum after discussion
- Loading branch information
commit 6979e8dceed4ad632ee8114ccf4561ed63f35d19
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
110 changes: 110 additions & 0 deletions
110
...alyst/src/main/scala/org/apache/spark/sql/catalyst/expressions/aggregate/DecimalSum.scala
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,110 @@ | ||
| /* | ||
| * Licensed to the Apache Software Foundation (ASF) under one or more | ||
| * contributor license agreements. See the NOTICE file distributed with | ||
| * this work for additional information regarding copyright ownership. | ||
| * The ASF licenses this file to You under the Apache License, Version 2.0 | ||
| * (the "License"); you may not use this file except in compliance with | ||
| * the License. You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
|
|
||
| package org.apache.spark.sql.catalyst.expressions.aggregate | ||
|
|
||
| import org.apache.spark.sql.catalyst.dsl.expressions._ | ||
| import org.apache.spark.sql.catalyst.expressions._ | ||
| import org.apache.spark.sql.internal.SQLConf | ||
| import org.apache.spark.sql.types._ | ||
|
|
||
| case class DecimalSum(child: Expression) extends DeclarativeAggregate with ImplicitCastInputTypes { | ||
skambha marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| override def children: Seq[Expression] = child :: Nil | ||
|
|
||
| override def nullable: Boolean = true | ||
|
|
||
| // Return data type. | ||
| override def dataType: DataType = resultType | ||
|
|
||
| override def inputTypes: Seq[AbstractDataType] = Seq(NumericType) | ||
|
|
||
| private lazy val resultType = child.dataType match { | ||
| case DecimalType.Fixed(precision, scale) => | ||
| DecimalType.bounded(precision + 10, scale) | ||
| case _: IntegralType => LongType | ||
| case _ => DoubleType | ||
| } | ||
|
|
||
| private lazy val sumDataType = resultType | ||
|
|
||
| private lazy val sum = AttributeReference("sum", sumDataType)() | ||
| private lazy val overflow = AttributeReference("overflow", BooleanType, false)() | ||
|
|
||
| private lazy val zero = Literal.default(resultType) | ||
|
|
||
| override lazy val aggBufferAttributes = sum :: overflow :: Nil | ||
|
|
||
| override lazy val initialValues: Seq[Expression] = Seq( | ||
| /* sum = */ Literal.create(null, sumDataType), | ||
| /* overflow = */ Literal.create(false, BooleanType) | ||
| ) | ||
|
|
||
| override lazy val updateExpressions: Seq[Expression] = { | ||
skambha marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| if (child.nullable) { | ||
| resultType match { | ||
| case d: DecimalType => | ||
| Seq( | ||
| If(overflow, Literal.create(null, sumDataType), coalesce( | ||
| CheckOverflow(coalesce(sum, zero) + child.cast(sumDataType), d, true), sum)), | ||
|
||
| overflow || | ||
| coalesce(HasOverflow(coalesce(sum, zero) + child.cast(sumDataType), d), false) | ||
| ) | ||
| case _ => Seq(coalesce(coalesce(sum, zero) + child.cast(sumDataType), sum), false) | ||
| } | ||
| } else { | ||
| resultType match { | ||
| case d: DecimalType => | ||
| Seq( | ||
| If(overflow, Literal.create(null, sumDataType), | ||
| CheckOverflow(coalesce(sum, zero) + child.cast(sumDataType), d, true)), | ||
| overflow || | ||
| coalesce(HasOverflow(coalesce(sum, zero) + child.cast(sumDataType), d), false) | ||
| ) | ||
| case _ => Seq(coalesce(sum, zero) + child.cast(sumDataType), false) | ||
| } | ||
| } | ||
| } | ||
|
|
||
| override lazy val mergeExpressions: Seq[Expression] = { | ||
| resultType match { | ||
| case d: DecimalType => | ||
| Seq( | ||
| If(coalesce(overflow.left, false) || coalesce(overflow.right, false), | ||
| Literal.create(null, d), | ||
| coalesce(CheckOverflow(coalesce(sum.left, zero) + sum.right, d, true), sum.left)), | ||
| If(coalesce(overflow.left, false) || coalesce(overflow.right, false), | ||
| true, HasOverflow(coalesce(sum.left, zero) + sum.right, d)) | ||
| ) | ||
| case _ => | ||
| Seq( | ||
| coalesce(coalesce(sum.left, zero) + sum.right, sum.left), | ||
| If(coalesce(overflow.left, false) || coalesce(overflow.right, false), true, false) | ||
| ) | ||
| } | ||
| } | ||
|
|
||
| override lazy val evaluateExpression: Expression = resultType match { | ||
| case d: DecimalType => | ||
| If(EqualTo(overflow, true), | ||
| If(!SQLConf.get.ansiEnabled, | ||
| Literal.create(null, sumDataType), | ||
| OverflowException(resultType, "Arithmetic Operation overflow")), | ||
| sum) | ||
| case _ => sum | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.