Skip to content

Commit 3fe396b

Browse files
committed
Change string representation form IP [ip] to [ip] to accomadate casting ip to string
Signed-off-by: Yuanchun Shen <yuanchu@amazon.com>
1 parent 3ba75c3 commit 3fe396b

3 files changed

Lines changed: 23 additions & 6 deletions

File tree

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

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

4646
@Override
4747
public String toString() {
48+
// used for casting to string
4849
return value();
4950
}
5051

core/src/test/java/org/opensearch/sql/data/model/ExprIpValueTest.java

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -122,12 +122,8 @@ public void testEquals() {
122122

123123
@Test
124124
public void testToString() {
125-
ipv4EqualStrings.forEach(
126-
(s) ->
127-
assertEquals(String.format("IP %s", ipv4String), ExprValueUtils.ipValue(s).toString()));
128-
ipv6EqualStrings.forEach(
129-
(s) ->
130-
assertEquals(String.format("IP %s", ipv6String), ExprValueUtils.ipValue(s).toString()));
125+
ipv4EqualStrings.forEach((s) -> assertEquals(ipv4String, ExprValueUtils.ipValue(s).toString()));
126+
ipv6EqualStrings.forEach((s) -> assertEquals(ipv6String, ExprValueUtils.ipValue(s).toString()));
131127
}
132128

133129
@Test

integ-test/src/test/java/org/opensearch/sql/calcite/remote/CalcitePPLCastFunctionIT.java

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import static org.opensearch.sql.legacy.TestsConstants.TEST_INDEX_DATATYPE_NONNUMERIC;
99
import static org.opensearch.sql.legacy.TestsConstants.TEST_INDEX_DATATYPE_NUMERIC;
1010
import static org.opensearch.sql.legacy.TestsConstants.TEST_INDEX_STATE_COUNTRY;
11+
import static org.opensearch.sql.legacy.TestsConstants.TEST_INDEX_WEBLOGS;
1112
import static org.opensearch.sql.util.MatcherUtils.rows;
1213
import static org.opensearch.sql.util.MatcherUtils.schema;
1314
import static org.opensearch.sql.util.MatcherUtils.verifyDataRows;
@@ -177,4 +178,23 @@ public void testCastIntegerToIp() {
177178
verifyErrorMessageContains(
178179
t, "Cannot convert INTEGER to IP, only STRING and IP types are supported");
179180
}
181+
182+
// Not available in v2
183+
@Test
184+
public void testCastIpToString() throws IOException {
185+
// Test casting ip to string
186+
var actual =
187+
executeQuery(
188+
String.format(
189+
"source=%s | eval s = cast(host as STRING) | fields s", TEST_INDEX_WEBLOGS));
190+
verifySchema(actual, schema("s", "string"));
191+
verifyDataRows(
192+
actual,
193+
rows("::1"),
194+
rows("0.0.0.2"),
195+
rows("::3"),
196+
rows("1.2.3.4"),
197+
rows("1.2.3.5"),
198+
rows("::ffff:1234"));
199+
}
180200
}

0 commit comments

Comments
 (0)