-
Notifications
You must be signed in to change notification settings - Fork 13.8k
[FLINK-24809][table-common][table-planner] Fix precision for aggs on DECIMAL types #18135
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
Conversation
Thanks a lot for your contribution to the Apache Flink project. I'm the @flinkbot. I help the community Automated ChecksLast check on commit 6dcc619 (Thu Dec 16 15:35:15 UTC 2021) Warnings:
Mention the bot in a comment to re-run the automated checks. Review Progress
Please see the Pull Request Review Guide for a full explanation of the review process. The Bot is tracking the review progress through labels. Labels are applied according to the order of the review items. For consensus, approval by a Flink committer of PMC member is required Bot commandsThe @flinkbot bot supports the following commands:
|
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.
Thanks for the PR @matriv. I had only minor comments. Should be good in the next iteration.
* on a Decimal type. Uses the {@link LogicalTypeMerging#findSumAggType(LogicalType)} to avoid | ||
* the normal {@link #PLUS} override the special calculation for precision and scale needed by | ||
* SUM. | ||
* Special "+" operator used internally by {@code SumAggFunction}, {@code Sum0AggFunction}, |
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.
remove IncrSumAggFunction
. how about we keep this section a bit more generic to not update it whenever we update the implementation? maybe just used internally by for implementing SUM/AVG aggregations (with and without retractions)
return ifThenElse(equalTo(count, literal(0L)), ifTrue, ifFalse); | ||
} | ||
|
||
protected UnresolvedCallExpression doPlus( |
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.
doPlus
sounds a bit generic, how about specializedPlus
or adjustedPlus
?
tEnv.sqlQuery(sql).toRetractStream[Row].addSink(sink) | ||
env.execute() | ||
|
||
val expected = List("6.41671935,65947.23071935707000000000,609.02867403703699700000") |
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.
can you add a comment to explain the result? for me as a reviewer that is not super familiar with the topic anymore, it is difficult to see something in those numbers
Btw the issue number is incorrect for both the PR and commit |
Thx, fixing. |
@cshuo @JingsongLi Could you please also take a look at this one? |
@flinkbot run azure |
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.
Thanks! Looks good to me overall. Left minor comments.
changelogRow("+I", Byte.box(3), Short.box(3), Int.box(3), Long.box(3), | ||
Float.box(3.0F), Double.box(3.0), "a")) | ||
|
||
val upsertSourceDataId = registerData(upsertSourceCurrencyData); |
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.
minor: remove ";"
return literal(0); | ||
} | ||
|
||
protected UnresolvedCallExpression adjustedPlus( |
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.
override?
return aggDecimalPlus(arg1, arg2); | ||
} | ||
|
||
protected UnresolvedCallExpression adjustedMinus( |
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.
ditto
@flinkbot run azure |
7f37df4
to
dee9315
Compare
Apply some small code optimisations for the build-in aggregate functions.
Use `TestValuesTableFactory` to setup the source changelog stream any make sure that the SQL query will generate `SumWithRetractAggFunction` to implement sum aggregations.
…DECIMAL types Since `Sum0AggFunction`,`SumWithRetractAggFunction` and `AvgAggFunction` are using internally `plus()` and `minus()` operators to implement the sum and avg aggregation (`minus()` is used also for the `WithRetract`), the decimal return type calculated by `LogicalTypeMerging#findSumAggType()` (also for the `AvgAggFunction`s internal `SumType`) gets overriden by the calculation for the `plus()` (and/or `minus()`) operator done by `LogicalTypeMerging#findAdditionDecimalType()`. To prevent this add a special `aggDecimalMinus()` operator and use it together with the previously added `aggDecimalPlus()` in those aggregate functions to avoid overriding the calculated precision of their decimal return type. Follows: apache#17634
…DECIMAL types Since `Sum0AggFunction`,`SumWithRetractAggFunction` and `AvgAggFunction` are using internally `plus()` and `minus()` operators to implement the sum and avg aggregation (`minus()` is used also for the `WithRetract`), the decimal return type calculated by `LogicalTypeMerging#findSumAggType()` (also for the `AvgAggFunction`s internal `SumType`) gets overriden by the calculation for the `plus()` (and/or `minus()`) operator done by `LogicalTypeMerging#findAdditionDecimalType()`. To prevent this add a special `aggDecimalMinus()` operator and use it together with the previously added `aggDecimalPlus()` in those aggregate functions to avoid overriding the calculated precision of their decimal return type. See also FLINK-24691. This closes apache#18135.
What is the purpose of the change
Fix precision for agg functions on decimal types.
Brief change log
SumWithRetractAggFunction
which didn't actually use it but instead used the normalSumAggFunction
.Sum0AggFunction
,SumWithRetractAggFunction
andAvgAggFunction
are using internallyplus()
andminus()
operators to implement the sum and avg aggregation (minus()
is used also for theWithRetract
), the decimal return type calculated byLogicalTypeMerging#findSumAggType()
(also for theAvgAggFunction
s internalSumType
) gets overriden by the calculation for theplus()
(and/orminus()
) operator done byLogicalTypeMerging#findAdditionDecimalType()
. To prevent this add a specialaggDecimalMinus()
operator and use it together with the previously addedaggDecimalPlus()
in those aggregate functions to avoid overriding the calculated precision of their decimal return type.Verifying this change
This change added tests and can be verified as follows:
AggregateITCase.scala
both for SQL and TableApi.Does this pull request potentially affect one of the following parts:
@Public(Evolving)
: noDocumentation