Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -694,20 +694,25 @@ private static Expression checkOutputBoundary(Literal input) {
return input;
}

private static Expression castDecimalV3Literal(DecimalV3Literal literal, int precision) {
return new DecimalV3Literal(DecimalV3Type.createDecimalV3Type(precision, literal.getValue().scale()),
literal.getValue());
}

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

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

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

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

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

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

/**
Expand Down Expand Up @@ -1136,21 +1141,10 @@ public static Expression mathE() {
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());
} else {
if (first.getValue().scale() < second.getValue()) {
return first;
}
if (second.getValue() < 0) {
double factor = Math.pow(10, Math.abs(second.getValue()));
return new DecimalV3Literal(
DecimalV3Type.createDecimalV3Type(first.getValue().precision(), 0),
BigDecimal.valueOf(Math.floor(first.getDouble() / factor) * factor));
}
if (first.getValue().compareTo(BigDecimal.ZERO) == -1) {
return first.roundCeiling(second.getValue());
} else {
return first.roundFloor(second.getValue());
}
return castDecimalV3Literal(first.roundFloor(second.getValue()), first.getValue().precision());
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -409,4 +409,29 @@ test {

//Additional cases for Xor, Conv, and other mathematical functions
testFoldConst("SELECT CONV(-10, 10, 2) AS conv_invalid_base") //Conv with negative input (may be undefined)

// fix floor/ceil/round function return type with DecimalV3 input
testFoldConst("with cte as (select floor(300.343) order by 1 limit 1) select * from cte")
testFoldConst("with cte as (select round(300.343) order by 1 limit 1) select * from cte")
testFoldConst("with cte as (select ceil(300.343) order by 1 limit 1) select * from cte")

testFoldConst("with cte as (select floor(300.343, 2) order by 1 limit 1) select * from cte")
testFoldConst("with cte as (select round(300.343, 2) order by 1 limit 1) select * from cte")
testFoldConst("with cte as (select ceil(300.343, 2) order by 1 limit 1) select * from cte")
Comment on lines +418 to +420
Copy link
Contributor

@superdiaodiao superdiaodiao Nov 7, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I tested the query before this PR:
with cte as (select truncate(300.343, 2) order by 1 limit 1) select * from cte;,
and the exception is:
ERROR 1105 (HY000): errCode = 2, detailMessage = (9.134.238.135)[INTERNAL_ERROR]output type not match expr type , col name , expected type Decimal(6, 2) , real type Decimal(5, 2).
So whether the truncate() should be tested together?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it make sense, added

testFoldConst("with cte as (select truncate(300.343, 2) order by 1 limit 1) select * from cte")

testFoldConst("with cte as (select floor(300.343, 0) order by 1 limit 1) select * from cte")
testFoldConst("with cte as (select round(300.343, 0) order by 1 limit 1) select * from cte")
testFoldConst("with cte as (select ceil(300.343, 0) order by 1 limit 1) select * from cte")
testFoldConst("with cte as (select truncate(300.343, 0) order by 1 limit 1) select * from cte")

testFoldConst("with cte as (select floor(300.343, -1) order by 1 limit 1) select * from cte")
testFoldConst("with cte as (select round(300.343, -1) order by 1 limit 1) select * from cte")
testFoldConst("with cte as (select ceil(300.343, -1) order by 1 limit 1) select * from cte")
testFoldConst("with cte as (select truncate(300.343, -1) order by 1 limit 1) select * from cte")

testFoldConst("with cte as (select floor(300.343, -4) order by 1 limit 1) select * from cte")
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")
}