Skip to content

Commit 2aa1fa2

Browse files
github-actions[bot]LiBinfeng
andauthored
branch-3.0: [fix](Nereids) fold const return type does not matched with type coercion #44022 (#44139)
Cherry-picked from #44022 Co-authored-by: LiBinfeng <libinfeng@selectdb.com>
1 parent d685167 commit 2aa1fa2

File tree

2 files changed

+22
-8
lines changed

2 files changed

+22
-8
lines changed

fe/fe-core/src/main/java/org/apache/doris/nereids/trees/expressions/functions/executable/NumericArithmetic.java

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -704,15 +704,16 @@ private static Expression castDecimalV3Literal(DecimalV3Literal literal, int pre
704704
*/
705705
@ExecFunction(name = "round")
706706
public static Expression round(DecimalV3Literal first) {
707-
return castDecimalV3Literal(first.round(0), first.getValue().precision());
707+
return castDecimalV3Literal(first.round(0), ((DecimalV3Type) first.getDataType()).getPrecision());
708708
}
709709

710710
/**
711711
* round
712712
*/
713713
@ExecFunction(name = "round")
714714
public static Expression round(DecimalV3Literal first, IntegerLiteral second) {
715-
return castDecimalV3Literal(first.round(second.getValue()), first.getValue().precision());
715+
return castDecimalV3Literal(first.round(second.getValue()),
716+
((DecimalV3Type) first.getDataType()).getPrecision());
716717
}
717718

718719
/**
@@ -738,15 +739,16 @@ public static Expression round(DoubleLiteral first, IntegerLiteral second) {
738739
*/
739740
@ExecFunction(name = "ceil")
740741
public static Expression ceil(DecimalV3Literal first) {
741-
return castDecimalV3Literal(first.roundCeiling(0), first.getValue().precision());
742+
return castDecimalV3Literal(first.roundCeiling(0), ((DecimalV3Type) first.getDataType()).getPrecision());
742743
}
743744

744745
/**
745746
* ceil
746747
*/
747748
@ExecFunction(name = "ceil")
748749
public static Expression ceil(DecimalV3Literal first, IntegerLiteral second) {
749-
return castDecimalV3Literal(first.roundCeiling(second.getValue()), first.getValue().precision());
750+
return castDecimalV3Literal(first.roundCeiling(second.getValue()),
751+
((DecimalV3Type) first.getDataType()).getPrecision());
750752
}
751753

752754
/**
@@ -772,15 +774,16 @@ public static Expression ceil(DoubleLiteral first, IntegerLiteral second) {
772774
*/
773775
@ExecFunction(name = "floor")
774776
public static Expression floor(DecimalV3Literal first) {
775-
return castDecimalV3Literal(first.roundFloor(0), first.getValue().precision());
777+
return castDecimalV3Literal(first.roundFloor(0), ((DecimalV3Type) first.getDataType()).getPrecision());
776778
}
777779

778780
/**
779781
* floor
780782
*/
781783
@ExecFunction(name = "floor")
782784
public static Expression floor(DecimalV3Literal first, IntegerLiteral second) {
783-
return castDecimalV3Literal(first.roundFloor(second.getValue()), first.getValue().precision());
785+
return castDecimalV3Literal(first.roundFloor(second.getValue()),
786+
((DecimalV3Type) first.getDataType()).getPrecision());
784787
}
785788

786789
/**
@@ -1142,9 +1145,11 @@ public static Expression truncate(DecimalV3Literal first, IntegerLiteral second)
11421145
if (first.getValue().compareTo(BigDecimal.ZERO) == 0) {
11431146
return first;
11441147
} else if (first.getValue().compareTo(BigDecimal.ZERO) < 0) {
1145-
return castDecimalV3Literal(first.roundCeiling(second.getValue()), first.getValue().precision());
1148+
return castDecimalV3Literal(first.roundCeiling(second.getValue()),
1149+
((DecimalV3Type) first.getDataType()).getPrecision());
11461150
} else {
1147-
return castDecimalV3Literal(first.roundFloor(second.getValue()), first.getValue().precision());
1151+
return castDecimalV3Literal(first.roundFloor(second.getValue()),
1152+
((DecimalV3Type) first.getDataType()).getPrecision());
11481153
}
11491154
}
11501155

regression-test/suites/nereids_p0/expression/fold_constant/fold_constant_numeric_arithmatic.groovy

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -434,4 +434,13 @@ test {
434434
testFoldConst("with cte as (select round(300.343, -4) order by 1 limit 1) select * from cte")
435435
testFoldConst("with cte as (select ceil(300.343, -4) order by 1 limit 1) select * from cte")
436436
testFoldConst("with cte as (select truncate(300.343, -4) order by 1 limit 1) select * from cte")
437+
438+
testFoldConst("with cte as (select floor(3) order by 1 limit 1) select * from cte")
439+
testFoldConst("with cte as (select round(3) order by 1 limit 1) select * from cte")
440+
testFoldConst("with cte as (select ceil(3) order by 1 limit 1) select * from cte")
441+
442+
testFoldConst("with cte as (select floor(3, 2) order by 1 limit 1) select * from cte")
443+
testFoldConst("with cte as (select round(3, 2) order by 1 limit 1) select * from cte")
444+
testFoldConst("with cte as (select ceil(3, 2) order by 1 limit 1) select * from cte")
445+
testFoldConst("with cte as (select truncate(3, 2) order by 1 limit 1) select * from cte")
437446
}

0 commit comments

Comments
 (0)