branch-2.1: [fix](Nereids) fix log and power #46077 (#46635)

Cherry-picked from #46077

Co-authored-by: LiBinfeng <libinfeng@selectdb.com>
This commit is contained in:
github-actions[bot]
2025-01-08 22:31:22 +08:00
committed by GitHub
parent f2419c49a7
commit b99ef07a3b
2 changed files with 16 additions and 0 deletions

View File

@ -827,6 +827,9 @@ public class NumericArithmetic {
@ExecFunction(name = "log")
public static Expression log(DoubleLiteral first, DoubleLiteral second) {
checkInputBoundary(first, 0.0d, Double.MAX_VALUE, false, true);
if (first.getValue().equals(1.0d)) {
throw new NotSupportedException("the first input of function log can not be 1.0");
}
return checkOutputBoundary(new DoubleLiteral(Math.log(first.getValue()) / Math.log(second.getValue())));
}
@ -863,6 +866,9 @@ public class NumericArithmetic {
@ExecFunction(name = "power")
public static Expression power(DoubleLiteral first, DoubleLiteral second) {
checkInputBoundary(second, Double.NEGATIVE_INFINITY, Double.POSITIVE_INFINITY, false, false);
if (first.getValue() < 0 && second.getValue() % 1 != 0) {
throw new NotSupportedException("input pair of function power can not be negative number and non-integer");
}
return checkOutputBoundary(new DoubleLiteral(Math.pow(first.getValue(), second.getValue())));
}

View File

@ -49,6 +49,7 @@ import org.apache.doris.nereids.trees.expressions.functions.scalar.Floor;
import org.apache.doris.nereids.trees.expressions.functions.scalar.FromUnixtime;
import org.apache.doris.nereids.trees.expressions.functions.scalar.HoursAdd;
import org.apache.doris.nereids.trees.expressions.functions.scalar.Ln;
import org.apache.doris.nereids.trees.expressions.functions.scalar.Log;
import org.apache.doris.nereids.trees.expressions.functions.scalar.MinutesAdd;
import org.apache.doris.nereids.trees.expressions.functions.scalar.Power;
import org.apache.doris.nereids.trees.expressions.functions.scalar.Round;
@ -393,6 +394,11 @@ class FoldConstantTest extends ExpressionRewriteTestHelper {
executor.rewrite(exExp, context);
}, "input -1 is out of boundary");
Assertions.assertThrows(NotSupportedException.class, () -> {
Log exExp = new Log(new DoubleLiteral(1.0d), new DoubleLiteral(1.0d));
executor.rewrite(exExp, context);
}, "the first input of function log can not be 1.0");
Sqrt sqrt = new Sqrt(new DoubleLiteral(16d));
rewritten = executor.rewrite(sqrt, context);
Assertions.assertEquals(new DoubleLiteral(4d), rewritten);
@ -411,6 +417,10 @@ class FoldConstantTest extends ExpressionRewriteTestHelper {
Power exExp = new Power(new DoubleLiteral(2d), new DoubleLiteral(10000d));
executor.rewrite(exExp, context);
}, "infinite result is invalid");
Assertions.assertThrows(NotSupportedException.class, () -> {
Power exExp = new Power(new DoubleLiteral(-1d), new DoubleLiteral(1.1d));
executor.rewrite(exExp, context);
}, "input pair of function power can not be negative number and non-integer");
Sin sin = new Sin(new DoubleLiteral(Math.PI / 2));
rewritten = executor.rewrite(sin, context);