Joe McDonnell has posted comments on this change. ( 
http://gerrit.cloudera.org:8080/25024 )

Change subject: IMPALA-15486: Fix DECIMAL scale types in Calcite
......................................................................


Patch Set 4:

(2 comments)

http://gerrit.cloudera.org:8080/#/c/25024/4/java/calcite-planner/src/main/java/org/apache/impala/calcite/operators/ImpalaAdjustScaleFunction.java
File 
java/calcite-planner/src/main/java/org/apache/impala/calcite/operators/ImpalaAdjustScaleFunction.java:

http://gerrit.cloudera.org:8080/#/c/25024/4/java/calcite-planner/src/main/java/org/apache/impala/calcite/operators/ImpalaAdjustScaleFunction.java@104
PS4, Line 104:       int resultScale = Math.max(0, 
Math.min(decimalType.decimalScale(), scale));
             :       int integerDigits = decimalType.decimalPrecision() - 
decimalType.decimalScale();
             :       // Rounding can carry only when reducing the fractional 
scale.
             :       if ((getName().equals("ROUND") || 
getName().equals("DROUND"))
             :           && resultScale < decimalType.decimalScale()) {
             :         ++integerDigits;
             :       }
There is an issue with negative scales for round. round(cast(9995 as 
decimal(4,0)),-1) should be able to produce a decimal(5,0) with 10000.

We're clamping the result scale to zero, but negative scales can result in an 
extra digit.

This would require handling cases where the result can exceed the max precision 
(i.e. round(decimal(38,0), -1) should stay at decimal(38,0)).

The original planner has the same issue in FunctionCallExpr's 
resolveDecimalReturnType().


http://gerrit.cloudera.org:8080/#/c/25024/4/testdata/workloads/functional-query/queries/QueryTest/decimal-scale.test
File testdata/workloads/functional-query/queries/QueryTest/decimal-scale.test:

http://gerrit.cloudera.org:8080/#/c/25024/4/testdata/workloads/functional-query/queries/QueryTest/decimal-scale.test@18
PS4, Line 18:   trunc(d,-1) as negative_scale,
I was looking at what happens for trunc with a larger numbers and a larger 
negative scale. I found a bug in other code that applies to the original 
planner and Calcite.
trunc(1234, -11) is not zero. It's 1234, which is wrong.

I see that as separate to this change, so I filed IMPALA-15719 for that



--
To view, visit http://gerrit.cloudera.org:8080/25024
To unsubscribe, visit http://gerrit.cloudera.org:8080/settings

Gerrit-Project: Impala-ASF
Gerrit-Branch: master
Gerrit-MessageType: comment
Gerrit-Change-Id: I8f6e6e2eaa7fe6eeab88b4143ed6b73e65220333
Gerrit-Change-Number: 25024
Gerrit-PatchSet: 4
Gerrit-Owner: Aleksandr Efimov <[email protected]>
Gerrit-Reviewer: Aleksandr Efimov <[email protected]>
Gerrit-Reviewer: Aman Sinha <[email protected]>
Gerrit-Reviewer: Impala Public Jenkins <[email protected]>
Gerrit-Reviewer: Joe McDonnell <[email protected]>
Gerrit-Reviewer: Michael Smith <[email protected]>
Gerrit-Reviewer: Steve Carlin <[email protected]>
Gerrit-Comment-Date: Fri, 30 Oct 2026 22:24:31 +0000
Gerrit-HasComments: Yes

Reply via email to