Skip to content

Commit 136535e

Browse files
committed
Update implementation of NumberToStringFunction
Signed-off-by: Yuanchun Shen <yuanchu@amazon.com>
1 parent 88cb647 commit 136535e

2 files changed

Lines changed: 6 additions & 2 deletions

File tree

core/src/main/java/org/opensearch/sql/calcite/ExtendedRexBuilder.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,7 @@ public RexNode makeCast(
153153
// Use a custom operator when casting an approximate numeric (e.g. double) to a character type.
154154
// This patch is necessary because Calcite's built-in CAST converts 0.0 to 0E0 as string.
155155
else if (SqlTypeUtil.isApproximateNumeric(exp.getType()) && SqlTypeUtil.isCharacter(type)) {
156-
// NUMBER_TO_STRING first box the number, then invoke its toString method
156+
// NUMBER_TO_STRING uses java's built-in method to get the string representation of a number
157157
return makeCall(type, PPLBuiltinOperators.NUMBER_TO_STRING, List.of(exp));
158158
}
159159
return super.makeCast(pos, type, exp, matchNullability, safe, format);

core/src/main/java/org/opensearch/sql/expression/function/udf/math/NumberToStringFunction.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,13 @@
66
package org.opensearch.sql.expression.function.udf.math;
77

88
import java.util.List;
9+
import java.util.Objects;
910
import org.apache.calcite.adapter.enumerable.NotNullImplementor;
1011
import org.apache.calcite.adapter.enumerable.NullPolicy;
1112
import org.apache.calcite.adapter.enumerable.RexToLixTranslator;
1213
import org.apache.calcite.linq4j.tree.Expression;
1314
import org.apache.calcite.linq4j.tree.Expressions;
15+
import org.apache.calcite.linq4j.tree.Primitive;
1416
import org.apache.calcite.rex.RexCall;
1517
import org.apache.calcite.sql.type.SqlReturnTypeInference;
1618
import org.opensearch.sql.calcite.utils.PPLOperandTypes;
@@ -45,7 +47,9 @@ public static class NumberToStringImplementor implements NotNullImplementor {
4547
public Expression implement(
4648
RexToLixTranslator translator, RexCall call, List<Expression> translatedOperands) {
4749
Expression operand = translatedOperands.get(0);
48-
return Expressions.call(Expressions.box(operand), "toString");
50+
Primitive primitive = Primitive.of(operand.getType());
51+
Objects.requireNonNull(primitive);
52+
return Expressions.call(primitive.getBoxClass(), "toString", operand);
4953
}
5054
}
5155
}

0 commit comments

Comments
 (0)