sangkyoonnam commented on code in PR #29395:
URL: https://github.com/apache/flink/pull/29395#discussion_r4339000704
##########
flink-table/flink-table-planner/src/test/scala/org/apache/flink/table/planner/runtime/batch/sql/agg/AggregateITCaseBase.scala:
##########
@@ -124,6 +124,24 @@ abstract class AggregateITCaseBase(testName: String)
extends BatchTestBase {
)
}
+ @Test
+ def testAvgOnIntDoesNotOverflowWhenReducedToSumAndCount(): Unit = {
+ val maxInt = Int.MaxValue
+ registerCollection(
+ "MaxIntTable",
+ Seq(row(maxInt), row(maxInt), row(1)),
+ new RowTypeInfo(Types.INT),
+ "x")
+ registerCollection("TwoRowTable", Seq(row(1), row(2)), new
RowTypeInfo(Types.INT), "y")
+
+ // SUM and COUNT of the same column make the planner reduce AVG to SUM /
COUNT
+ checkResult(
+ "SELECT AVG(x), SUM(CAST(x AS BIGINT)), COUNT(x) FROM MaxIntTable",
+ Seq(row(1431655765, 4294967295L, 3L)))
Review Comment:
This assertion passes with the rule-set changes reverted. `SUM(x)` on the
same column exercises the reduction:
```suggestion
// SUM and COUNT of the same column make the planner reduce AVG to SUM /
COUNT;
// SUM(x) itself stays INT and wraps to -1
checkResult("SELECT AVG(x), SUM(x), COUNT(x) FROM MaxIntTable",
Seq(row(1431655765, -1, 3L)))
```
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]