Skip to content

Commit 804db79

Browse files
committed
hex max limit doc and unit tests
Signed-off-by: Asif Bashar <[email protected]>
1 parent 0f0125a commit 804db79

File tree

2 files changed

+82
-1
lines changed

2 files changed

+82
-1
lines changed

docs/user/ppl/functions/conversion.rst

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,8 @@ Return type: Number
133133

134134

135135
You can use this function with the eval commands and as part of eval expressions.
136-
Base values can be between 2 and 36.
136+
Base values can be between 2 and 36. The maximum value supported for base 10 is +(2-2^-52)·2^1023 and minimum is -(2-2^-52)·2^1023.
137+
The maximum for other supported bases is 2^63-1 (or 7FFFFFFFFFFFFFFF) and minimum is -2^63 (or -7FFFFFFFFFFFFFFF).
137138

138139
You can use this function to convert a string representation of a binary number to return the corresponding number in base 10.
139140

ppl/src/test/java/org/opensearch/sql/ppl/calcite/CalcitePPLToNumberFunctionTest.java

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,86 @@ public void testNumberHex() {
4949
verifyPPLToSparkSQL(root, expectedSparkSql);
5050
}
5151

52+
53+
@Test
54+
public void testNumberHexMinLimit() {
55+
String ppl =
56+
"source=EMP | eval long_value = tonumber('-7FFFFFFFFFFFFFFF',16) | fields long_value|head"
57+
+ " 1";
58+
RelNode root = getRelNode(ppl);
59+
String expectedLogical =
60+
"LogicalSort(fetch=[1])\n"
61+
+ " LogicalProject(long_value=[TONUMBER('-7FFFFFFFFFFFFFFF':VARCHAR, 16)])\n"
62+
+ " LogicalTableScan(table=[[scott, EMP]])\n";
63+
verifyLogical(root, expectedLogical);
64+
String expectedResult = "long_value=-9.223372036854776E18\n";
65+
verifyResult(root, expectedResult);
66+
67+
String expectedSparkSql =
68+
"SELECT `TONUMBER`('-7FFFFFFFFFFFFFFF', 16) `long_value`\nFROM `scott`.`EMP`\nLIMIT 1";
69+
70+
verifyPPLToSparkSQL(root, expectedSparkSql);
71+
}
72+
73+
@Test
74+
public void testNumberHexMaxLimit() {
75+
String ppl =
76+
"source=EMP | eval long_value = tonumber('7FFFFFFFFFFFFFFF',16) | fields long_value|head"
77+
+ " 1";
78+
RelNode root = getRelNode(ppl);
79+
String expectedLogical =
80+
"LogicalSort(fetch=[1])\n"
81+
+ " LogicalProject(long_value=[TONUMBER('7FFFFFFFFFFFFFFF':VARCHAR, 16)])\n"
82+
+ " LogicalTableScan(table=[[scott, EMP]])\n";
83+
verifyLogical(root, expectedLogical);
84+
String expectedResult = "long_value=9.223372036854776E18\n";
85+
verifyResult(root, expectedResult);
86+
87+
String expectedSparkSql =
88+
"SELECT `TONUMBER`('7FFFFFFFFFFFFFFF', 16) `long_value`\nFROM `scott`.`EMP`\nLIMIT 1";
89+
90+
verifyPPLToSparkSQL(root, expectedSparkSql);
91+
}
92+
93+
@Test
94+
public void testNumberHexOverNegativeMaxLimit() {
95+
String ppl =
96+
"source=EMP | eval long_value = tonumber('-FFFFFFFFFFFFFFFF',16) | fields long_value|head"
97+
+ " 1";
98+
RelNode root = getRelNode(ppl);
99+
String expectedLogical =
100+
"LogicalSort(fetch=[1])\n"
101+
+ " LogicalProject(long_value=[TONUMBER('-FFFFFFFFFFFFFFFF':VARCHAR, 16)])\n"
102+
+ " LogicalTableScan(table=[[scott, EMP]])\n";
103+
verifyLogical(root, expectedLogical);
104+
String expectedResult = "long_value=1.0\n";
105+
verifyResult(root, expectedResult);
106+
107+
String expectedSparkSql =
108+
"SELECT `TONUMBER`('-FFFFFFFFFFFFFFFF', 16) `long_value`\nFROM `scott`.`EMP`\nLIMIT 1";
109+
110+
verifyPPLToSparkSQL(root, expectedSparkSql);
111+
}
112+
113+
@Test
114+
public void testNumberHexOverPositiveMaxLimit() {
115+
String ppl =
116+
"source=EMP | eval long_value = tonumber('FFFFFFFFFFFFFFFF',16) | fields long_value|head 1";
117+
RelNode root = getRelNode(ppl);
118+
String expectedLogical =
119+
"LogicalSort(fetch=[1])\n"
120+
+ " LogicalProject(long_value=[TONUMBER('FFFFFFFFFFFFFFFF':VARCHAR, 16)])\n"
121+
+ " LogicalTableScan(table=[[scott, EMP]])\n";
122+
verifyLogical(root, expectedLogical);
123+
String expectedResult = "long_value=-1.0\n";
124+
verifyResult(root, expectedResult);
125+
126+
String expectedSparkSql =
127+
"SELECT `TONUMBER`('FFFFFFFFFFFFFFFF', 16) `long_value`\nFROM `scott`.`EMP`\nLIMIT 1";
128+
129+
verifyPPLToSparkSQL(root, expectedSparkSql);
130+
}
131+
52132
@Test
53133
public void testNumber() {
54134
String ppl = "source=EMP | eval int_value = tonumber('4598') | fields int_value|head 1";

0 commit comments

Comments
 (0)