Skip to content

Commit 0f0125a

Browse files
committed
made recommended changes to simply parse to number, null when malformed
Signed-off-by: Asif Bashar <[email protected]>
1 parent b7afa17 commit 0f0125a

File tree

2 files changed

+19
-62
lines changed

2 files changed

+19
-62
lines changed

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

Lines changed: 13 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55

66
package org.opensearch.sql.expression.function.udf;
77

8+
import java.math.BigInteger;
89
import java.util.List;
910
import org.apache.calcite.adapter.enumerable.NotNullImplementor;
1011
import org.apache.calcite.adapter.enumerable.NullPolicy;
@@ -69,50 +70,23 @@ public static Number toNumber(String numStr) {
6970
@Strict
7071
public static Number toNumber(String numStr, int base) {
7172
if (base < 2 || base > 36) {
72-
throw new IllegalArgumentException("Base must be between 2 and 36");
73+
throw new IllegalArgumentException("Base has to be between 2 and 36.");
7374
}
74-
75-
if (numStr.contains(".")) {
76-
77-
boolean isNegative = numStr.startsWith("-");
78-
if (isNegative) {
79-
numStr = numStr.substring(1);
80-
}
81-
82-
// Split integer and fractional parts
83-
String[] parts = numStr.split("\\.");
84-
String intPart = parts[0];
85-
String fracPart = parts.length > 1 ? parts[1] : "";
86-
87-
// Convert integer part
88-
double intValue = 0;
89-
for (char c : intPart.toCharArray()) {
90-
int digit = Character.digit(c, base);
91-
if (digit < 0) throw new IllegalArgumentException("Invalid digit: " + c);
92-
intValue = intValue * base + digit;
93-
}
94-
95-
// Convert fractional part
96-
double fracValue = 0;
75+
Number result = null;
76+
try {
9777
if (base == 10) {
98-
double divisor = base;
99-
for (char c : fracPart.toCharArray()) {
100-
int digit = Character.digit(c, base);
101-
if (digit < 0) throw new IllegalArgumentException("Invalid digit: " + c);
102-
fracValue += (double) digit / divisor;
103-
divisor *= base;
78+
if (numStr.contains(".")) {
79+
result = Double.parseDouble(numStr);
80+
} else {
81+
result = Long.parseLong(numStr);
10482
}
105-
}
106-
107-
double result = intValue + fracValue;
108-
result = isNegative ? -result : result;
109-
if (base == 10) {
110-
return result;
11183
} else {
112-
return (long) result;
84+
BigInteger bigInteger = new BigInteger(numStr, base);
85+
result = bigInteger.longValue();
11386
}
114-
} else {
115-
return Long.parseLong(numStr, base);
87+
} catch (Exception e) {
88+
11689
}
90+
return result;
11791
}
11892
}

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

Lines changed: 6 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -141,29 +141,12 @@ void testToNumberInvalidBase() {
141141

142142
@Test
143143
void testToNumberInvalidDigits() {
144-
assertThrows(
145-
IllegalArgumentException.class,
146-
() -> {
147-
ToNumberFunction.toNumber("12A", 10);
148-
});
149-
150-
assertThrows(
151-
IllegalArgumentException.class,
152-
() -> {
153-
ToNumberFunction.toNumber("102", 2);
154-
});
155-
156-
assertThrows(
157-
IllegalArgumentException.class,
158-
() -> {
159-
ToNumberFunction.toNumber("189", 8);
160-
});
161-
162-
assertThrows(
163-
IllegalArgumentException.class,
164-
() -> {
165-
ToNumberFunction.toNumber("GHI", 16);
166-
});
144+
assertEquals(null, ToNumberFunction.toNumber("12A", 10));
145+
assertEquals(null, ToNumberFunction.toNumber("102", 2));
146+
assertEquals(null, ToNumberFunction.toNumber("101.101", 2));
147+
assertEquals(null, ToNumberFunction.toNumber("189", 8));
148+
assertEquals(null, ToNumberFunction.toNumber("GHI", 16));
149+
assertEquals(null, ToNumberFunction.toNumber("FF.8", 16));
167150
}
168151

169152
@Test

0 commit comments

Comments
 (0)