Skip to content

Commit 3ba75c3

Browse files
committed
Correct casting ip to string & update conversion doc
Signed-off-by: Yuanchun Shen <yuanchu@amazon.com>
1 parent 88cdb02 commit 3ba75c3

5 files changed

Lines changed: 26 additions & 26 deletions

File tree

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

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,6 @@
55

66
package org.opensearch.sql.calcite;
77

8-
import static org.opensearch.sql.calcite.utils.OpenSearchTypeFactory.ExprUDT.EXPR_DATE;
9-
import static org.opensearch.sql.calcite.utils.OpenSearchTypeFactory.ExprUDT.EXPR_IP;
10-
import static org.opensearch.sql.calcite.utils.OpenSearchTypeFactory.ExprUDT.EXPR_TIME;
11-
import static org.opensearch.sql.calcite.utils.OpenSearchTypeFactory.ExprUDT.EXPR_TIMESTAMP;
12-
138
import com.google.common.collect.ImmutableList;
149
import java.util.Arrays;
1510
import java.util.List;
@@ -140,7 +135,7 @@ public RexNode makeCast(
140135
if (argExprType == ExprCoreType.IP) {
141136
yield exp;
142137
} else if (argExprType == ExprCoreType.STRING) {
143-
yield makeCall(type, PPLBuiltinOperators.CAST_IP, List.of(exp));
138+
yield makeCall(type, PPLBuiltinOperators.IP, List.of(exp));
144139
}
145140
// Throwing error inside implementation will be suppressed by Calcite, thus
146141
// throwing 500 error. Therefore, we throw error here to ensure the error

core/src/main/java/org/opensearch/sql/data/model/ExprIpValue.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ public boolean equal(ExprValue other) {
4545

4646
@Override
4747
public String toString() {
48-
return String.format("IP %s", value());
48+
return value();
4949
}
5050

5151
@Override

core/src/main/java/org/opensearch/sql/expression/function/PPLBuiltinOperators.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@
7575
import org.opensearch.sql.expression.function.udf.datetime.YearweekFunction;
7676
import org.opensearch.sql.expression.function.udf.ip.CidrMatchFunction;
7777
import org.opensearch.sql.expression.function.udf.ip.CompareIpFunction;
78-
import org.opensearch.sql.expression.function.udf.ip.IPCastFunction;
78+
import org.opensearch.sql.expression.function.udf.ip.IPFunction;
7979
import org.opensearch.sql.expression.function.udf.math.CRC32Function;
8080
import org.opensearch.sql.expression.function.udf.math.ConvFunction;
8181
import org.opensearch.sql.expression.function.udf.math.DivideFunction;
@@ -284,7 +284,7 @@ public class PPLBuiltinOperators extends ReflectiveSqlOperatorTable {
284284
.toUDF("TIME");
285285

286286
// IP cast function
287-
public static final SqlOperator CAST_IP = new IPCastFunction().toUDF("CAST_IP");
287+
public static final SqlOperator IP = new IPFunction().toUDF("IP");
288288
public static final SqlOperator TIME_TO_SEC =
289289
adaptExprMethodToUDF(
290290
DateTimeFunctions.class,

core/src/main/java/org/opensearch/sql/expression/function/udf/ip/IPCastFunction.java renamed to core/src/main/java/org/opensearch/sql/expression/function/udf/ip/IPFunction.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,9 @@
3232
* <li>(IP) -> IP
3333
* </ul>
3434
*/
35-
public class IPCastFunction extends ImplementorUDF {
35+
public class IPFunction extends ImplementorUDF {
3636

37-
public IPCastFunction() {
37+
public IPFunction() {
3838
super(new CastImplementor(), NullPolicy.ANY);
3939
}
4040

docs/user/ppl/functions/conversion.rst

Lines changed: 20 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -16,24 +16,29 @@ Description
1616

1717
Usage: cast(expr as dateType) cast the expr to dataType. return the value of dataType. The following conversion rules are used:
1818

19-
+------------+--------+--------+---------+-------------+--------+--------+
20-
| Src/Target | STRING | NUMBER | BOOLEAN | TIMESTAMP | DATE | TIME |
21-
+------------+--------+--------+---------+-------------+--------+--------+
22-
| STRING | | Note1 | Note1 | TIMESTAMP() | DATE() | TIME() |
23-
+------------+--------+--------+---------+-------------+--------+--------+
24-
| NUMBER | Note1 | | v!=0 | N/A | N/A | N/A |
25-
+------------+--------+--------+---------+-------------+--------+--------+
26-
| BOOLEAN | Note1 | v?1:0 | | N/A | N/A | N/A |
27-
+------------+--------+--------+---------+-------------+--------+--------+
28-
| TIMESTAMP | Note1 | N/A | N/A | | DATE() | TIME() |
29-
+------------+--------+--------+---------+-------------+--------+--------+
30-
| DATE | Note1 | N/A | N/A | N/A | | N/A |
31-
+------------+--------+--------+---------+-------------+--------+--------+
32-
| TIME | Note1 | N/A | N/A | N/A | N/A | |
33-
+------------+--------+--------+---------+-------------+--------+--------+
19+
+------------+--------+--------+---------+-------------+--------+--------+--------+
20+
| Src/Target | STRING | NUMBER | BOOLEAN | TIMESTAMP | DATE | TIME | IP |
21+
+------------+--------+--------+---------+-------------+--------+--------+--------+
22+
| STRING | | Note1 | Note1 | TIMESTAMP() | DATE() | TIME() | IP() |
23+
+------------+--------+--------+---------+-------------+--------+--------+--------+
24+
| NUMBER | Note1 | | v!=0 | N/A | N/A | N/A | N/A |
25+
+------------+--------+--------+---------+-------------+--------+--------+--------+
26+
| BOOLEAN | Note1 | v?1:0 | | N/A | N/A | N/A | N/A |
27+
+------------+--------+--------+---------+-------------+--------+--------+--------+
28+
| TIMESTAMP | Note1 | N/A | N/A | | DATE() | TIME() | N/A |
29+
+------------+--------+--------+---------+-------------+--------+--------+--------+
30+
| DATE | Note1 | N/A | N/A | N/A | | N/A | N/A |
31+
+------------+--------+--------+---------+-------------+--------+--------+--------+
32+
| TIME | Note1 | N/A | N/A | N/A | N/A | | N/A |
33+
+------------+--------+--------+---------+-------------+--------+--------+--------+
34+
| IP | Note2 | N/A | N/A | N/A | N/A | N/A | |
35+
+------------+--------+--------+---------+-------------+--------+--------+--------+
3436

3537
Note1: the conversion follow the JDK specification.
3638

39+
Note2: IP will be converted to its canonical representation. Canonical representation
40+
for IPv6 is described in `RFC 5952 <https://datatracker.ietf.org/doc/html/rfc5952>`_.
41+
3742
Cast to string example::
3843

3944
os> source=people | eval `cbool` = CAST(true as string), `cint` = CAST(1 as string), `cdate` = CAST(CAST('2012-08-07' as date) as string) | fields `cbool`, `cint`, `cdate`

0 commit comments

Comments
 (0)