Skip to content

Commit

Permalink
branch-2.1: [fix](Nereids) fold const return type does not matched wi…
Browse files Browse the repository at this point in the history
…th type coercion #44022 (#44141)

Cherry-picked from #44022

Co-authored-by: LiBinfeng <[email protected]>
  • Loading branch information
github-actions[bot] and LiBinfeng-01 authored Nov 18, 2024
1 parent 05e1620 commit b01b202
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -704,15 +704,16 @@ private static Expression castDecimalV3Literal(DecimalV3Literal literal, int pre
*/
@ExecFunction(name = "round")
public static Expression round(DecimalV3Literal first) {
return castDecimalV3Literal(first.round(0), first.getValue().precision());
return castDecimalV3Literal(first.round(0), ((DecimalV3Type) first.getDataType()).getPrecision());
}

/**
* round
*/
@ExecFunction(name = "round")
public static Expression round(DecimalV3Literal first, IntegerLiteral second) {
return castDecimalV3Literal(first.round(second.getValue()), first.getValue().precision());
return castDecimalV3Literal(first.round(second.getValue()),
((DecimalV3Type) first.getDataType()).getPrecision());
}

/**
Expand All @@ -738,15 +739,16 @@ public static Expression round(DoubleLiteral first, IntegerLiteral second) {
*/
@ExecFunction(name = "ceil")
public static Expression ceil(DecimalV3Literal first) {
return castDecimalV3Literal(first.roundCeiling(0), first.getValue().precision());
return castDecimalV3Literal(first.roundCeiling(0), ((DecimalV3Type) first.getDataType()).getPrecision());
}

/**
* ceil
*/
@ExecFunction(name = "ceil")
public static Expression ceil(DecimalV3Literal first, IntegerLiteral second) {
return castDecimalV3Literal(first.roundCeiling(second.getValue()), first.getValue().precision());
return castDecimalV3Literal(first.roundCeiling(second.getValue()),
((DecimalV3Type) first.getDataType()).getPrecision());
}

/**
Expand All @@ -772,15 +774,16 @@ public static Expression ceil(DoubleLiteral first, IntegerLiteral second) {
*/
@ExecFunction(name = "floor")
public static Expression floor(DecimalV3Literal first) {
return castDecimalV3Literal(first.roundFloor(0), first.getValue().precision());
return castDecimalV3Literal(first.roundFloor(0), ((DecimalV3Type) first.getDataType()).getPrecision());
}

/**
* floor
*/
@ExecFunction(name = "floor")
public static Expression floor(DecimalV3Literal first, IntegerLiteral second) {
return castDecimalV3Literal(first.roundFloor(second.getValue()), first.getValue().precision());
return castDecimalV3Literal(first.roundFloor(second.getValue()),
((DecimalV3Type) first.getDataType()).getPrecision());
}

/**
Expand Down Expand Up @@ -1142,9 +1145,11 @@ public static Expression truncate(DecimalV3Literal first, IntegerLiteral second)
if (first.getValue().compareTo(BigDecimal.ZERO) == 0) {
return first;
} else if (first.getValue().compareTo(BigDecimal.ZERO) < 0) {
return castDecimalV3Literal(first.roundCeiling(second.getValue()), first.getValue().precision());
return castDecimalV3Literal(first.roundCeiling(second.getValue()),
((DecimalV3Type) first.getDataType()).getPrecision());
} else {
return castDecimalV3Literal(first.roundFloor(second.getValue()), first.getValue().precision());
return castDecimalV3Literal(first.roundFloor(second.getValue()),
((DecimalV3Type) first.getDataType()).getPrecision());
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -434,4 +434,13 @@ test {
testFoldConst("with cte as (select round(300.343, -4) order by 1 limit 1) select * from cte")
testFoldConst("with cte as (select ceil(300.343, -4) order by 1 limit 1) select * from cte")
testFoldConst("with cte as (select truncate(300.343, -4) order by 1 limit 1) select * from cte")

testFoldConst("with cte as (select floor(3) order by 1 limit 1) select * from cte")
testFoldConst("with cte as (select round(3) order by 1 limit 1) select * from cte")
testFoldConst("with cte as (select ceil(3) order by 1 limit 1) select * from cte")

testFoldConst("with cte as (select floor(3, 2) order by 1 limit 1) select * from cte")
testFoldConst("with cte as (select round(3, 2) order by 1 limit 1) select * from cte")
testFoldConst("with cte as (select ceil(3, 2) order by 1 limit 1) select * from cte")
testFoldConst("with cte as (select truncate(3, 2) order by 1 limit 1) select * from cte")
}

0 comments on commit b01b202

Please sign in to comment.