Skip to content

Commit 15817ae

Browse files
committed
Revert "added IT , add null cases, fixed format for integer/big integer"
Signed-off-by: Asif Bashar <asif.bashar@gmail.com>
1 parent 77d53a7 commit 15817ae

2 files changed

Lines changed: 43 additions & 11 deletions

File tree

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

Lines changed: 37 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -50,24 +50,50 @@ public SqlReturnTypeInference getReturnTypeInference() {
5050
return (opBinding) -> {
5151
// Try to determine if the result will be Long or Double based on the input
5252
int base = 10;
53-
try {
54-
base =
55-
opBinding.getOperandCount() > 1
56-
? opBinding.getOperandLiteralValue(1, Integer.class)
57-
: 10;
58-
} catch (NumberFormatException e) {
59-
// If parsing fails, default to base 10
53+
if (opBinding.getOperandCount() > 1) {
54+
55+
base = opBinding.getOperandLiteralValue(1, Integer.class);
6056
}
57+
6158
if (opBinding.getOperandCount() > 0 && opBinding.isOperandLiteral(0, false)) {
6259
String literal = opBinding.getOperandLiteralValue(0, String.class);
6360
if (literal != null) {
64-
65-
// Check if it's a decimal number
66-
if (base != 10 || !(literal.contains("."))) {
61+
try {
62+
// Check if it's a decimal number
63+
if (base != 10) {
64+
return opBinding
65+
.getTypeFactory()
66+
.createTypeWithNullability(
67+
opBinding.getTypeFactory().createSqlType(SqlTypeName.BIGINT), true);
68+
}
69+
if (literal.contains(".")) {
70+
return opBinding
71+
.getTypeFactory()
72+
.createTypeWithNullability(
73+
opBinding
74+
.getTypeFactory()
75+
.createSqlType(org.apache.calcite.sql.type.SqlTypeName.DOUBLE),
76+
true);
77+
} else {
78+
// Check if it's an integer that fits in Long
79+
Long.parseLong(literal);
80+
return opBinding
81+
.getTypeFactory()
82+
.createTypeWithNullability(
83+
opBinding
84+
.getTypeFactory()
85+
.createSqlType(org.apache.calcite.sql.type.SqlTypeName.BIGINT),
86+
true);
87+
}
88+
} catch (NumberFormatException e) {
89+
// If parsing fails, default to Double (matches the runtime behavior)
6790
return opBinding
6891
.getTypeFactory()
6992
.createTypeWithNullability(
70-
opBinding.getTypeFactory().createSqlType(SqlTypeName.BIGINT), true);
93+
opBinding
94+
.getTypeFactory()
95+
.createSqlType(org.apache.calcite.sql.type.SqlTypeName.DOUBLE),
96+
true);
7197
}
7298
}
7399
}

core/src/test/java/org/opensearch/sql/expression/function/udf/ToNumberFunctionTest.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,19 @@
77

88
import static org.junit.jupiter.api.Assertions.*;
99

10+
import org.apache.calcite.sql.type.ReturnTypes;
1011
import org.junit.jupiter.api.Test;
1112
import org.opensearch.sql.calcite.utils.PPLOperandTypes;
1213

1314
public class ToNumberFunctionTest {
1415

1516
private final ToNumberFunction function = new ToNumberFunction();
1617

18+
@Test
19+
void testGetReturnTypeInference() {
20+
assertEquals(ReturnTypes.DOUBLE_FORCE_NULLABLE, function.getReturnTypeInference());
21+
}
22+
1723
@Test
1824
void testGetOperandMetadata() {
1925
assertEquals(PPLOperandTypes.STRING_OR_STRING_INTEGER, function.getOperandMetadata());

0 commit comments

Comments
 (0)