Skip to content

Commit

Permalink
Add new tests for IP data type to SortCommandIT, and update `weblog…
Browse files Browse the repository at this point in the history
…s` test data.

Signed-off-by: currantw <[email protected]>
  • Loading branch information
currantw committed Dec 11, 2024
1 parent 89ae898 commit 28ed843
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 19 deletions.
15 changes: 8 additions & 7 deletions docs/user/ppl/functions/ip.rst
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,14 @@ Return type: BOOLEAN

Example:

os> source=weblogs | where cidrmatch(host, '199.120.110.0/24') | fields host
fetched rows / total rows = 1/1
+----------------+
| host |
|----------------|
| 199.120.110.21 |
+----------------+
os> source=weblogs | where cidrmatch(host, '1.0.0.0/24') | fields host, url
fetched rows / total rows = 2/2
+----------------+--------------------|
| host | url |
+----------------+--------------------+
| 1.2.3.4 | /history/voyager1/ |
| 1.2.3.5 | /history/voyager2/ |
+----------------+--------------------|

Note:
- `ip` can be an IPv4 or an IPv6 address
Expand Down
12 changes: 9 additions & 3 deletions doctest/test_data/weblogs.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
{"index":{}}
{"host": "199.72.81.55", "method": "GET", "url": "/history/apollo/", "response": "200", "bytes": "6245"}
{"host": "::1", "method": "GET", "url": "/history/apollo/", "response": "200", "bytes": "6245"}
{"index":{}}
{"host": "199.120.110.21", "method": "GET", "url": "/shuttle/missions/sts-73/mission-sts-73.html", "response": "200", "bytes": "4085"}
{"host": "0.0.0.2", "method": "GET", "url": "/shuttle/missions/sts-73/mission-sts-73.html", "response": "200", "bytes": "4085"}
{"index":{}}
{"host": "205.212.115.106", "method": "GET", "url": "/shuttle/countdown/countdown.html", "response": "200", "bytes": "3985"}
{"host": "::3", "method": "GET", "url": "/shuttle/countdown/countdown.html", "response": "200", "bytes": "3985"}
{"index":{}}
{"host": "::FFFF:1.2.3.4", "method": "GET", "url": "/history/voyager1/", "response": "200", "bytes": "1234"}
{"index":{}}
{"host": "1.2.3.4", "method": "GET", "url": "/history/voyager2/", "response": "200", "bytes": "4321"}
{"index":{}}
{"host": "::FFFF:1234", "method": "GET", "url": "/history/artemis/", "response": "200", "bytes": "9876"}
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public void test_cidrmatch() throws IOException {
result =
executeQuery(
String.format(
"source=%s | where cidrmatch(host, '199.120.111.0/24') | fields host",
"source=%s | where cidrmatch(host, '250.0.0.0/24') | fields host",
TEST_INDEX_WEBLOG));
verifySchema(result, schema("host", null, "ip"));
verifyDataRows(result);
Expand All @@ -40,18 +40,17 @@ public void test_cidrmatch() throws IOException {
result =
executeQuery(
String.format(
"source=%s | where cidrmatch(host, '199.120.110.0/24') | fields host",
"source=%s | where cidrmatch(host, '0.0.0.0/24') | fields host",
TEST_INDEX_WEBLOG));
verifySchema(result, schema("host", null, "ip"));
verifyDataRows(result, rows("199.120.110.21"));
verifyDataRows(result, rows("0.0.0.2"));

// Multiple matches
result =
executeQuery(
String.format(
"source=%s | where cidrmatch(host, '199.0.0.0/8') | fields host",
TEST_INDEX_WEBLOG));
"source=%s | where cidrmatch(host, '1.2.3.0/8') | fields host", TEST_INDEX_WEBLOG));
verifySchema(result, schema("host", null, "ip"));
verifyDataRows(result, rows("199.72.81.55"), rows("199.120.110.21"));
verifyDataRows(result, rows("1.2.3.4"), rows("1.2.3.5"));
}
}
16 changes: 16 additions & 0 deletions integ-test/src/test/java/org/opensearch/sql/ppl/SortCommandIT.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import static org.opensearch.sql.legacy.TestsConstants.TEST_INDEX_BANK;
import static org.opensearch.sql.legacy.TestsConstants.TEST_INDEX_BANK_WITH_NULL_VALUES;
import static org.opensearch.sql.legacy.TestsConstants.TEST_INDEX_DOG;
import static org.opensearch.sql.legacy.TestsConstants.TEST_INDEX_WEBLOG;
import static org.opensearch.sql.util.MatcherUtils.rows;
import static org.opensearch.sql.util.MatcherUtils.verifyOrder;

Expand All @@ -28,6 +29,7 @@ public void init() throws IOException {
loadIndex(Index.BANK);
loadIndex(Index.BANK_WITH_NULL_VALUES);
loadIndex(Index.DOG);
loadIndex(Index.WEBLOG);
}

@Test
Expand Down Expand Up @@ -130,6 +132,20 @@ public void testSortStringField() throws IOException {
rows("Ratliff"));
}

@Test
public void testSortIpField() throws IOException {
final JSONObject result =
executeQuery(String.format("source=%s | sort host | fields host", TEST_INDEX_WEBLOG));
verifyOrder(
result,
rows("::1"),
rows("::3"),
rows("::ffff:1234"),
rows("0.0.0.2"),
rows("1.2.3.4"),
rows("1.2.3.5"));
}

@Test
public void testSortMultipleFields() throws IOException {
JSONObject result =
Expand Down
12 changes: 9 additions & 3 deletions integ-test/src/test/resources/weblogs.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
{"index":{}}
{"host": "199.72.81.55", "method": "GET", "url": "/history/apollo/", "response": "200", "bytes": "6245"}
{"host": "::1", "method": "GET", "url": "/history/apollo/", "response": "200", "bytes": "6245"}
{"index":{}}
{"host": "199.120.110.21", "method": "GET", "url": "/shuttle/missions/sts-73/mission-sts-73.html", "response": "200", "bytes": "4085"}
{"host": "0.0.0.2", "method": "GET", "url": "/shuttle/missions/sts-73/mission-sts-73.html", "response": "200", "bytes": "4085"}
{"index":{}}
{"host": "205.212.115.106", "method": "GET", "url": "/shuttle/countdown/countdown.html", "response": "200", "bytes": "3985"}
{"host": "::3", "method": "GET", "url": "/shuttle/countdown/countdown.html", "response": "200", "bytes": "3985"}
{"index":{}}
{"host": "::FFFF:1.2.3.4", "method": "GET", "url": "/history/voyager1/", "response": "200", "bytes": "1234"}
{"index":{}}
{"host": "1.2.3.5", "method": "GET", "url": "/history/voyager2/", "response": "200", "bytes": "4321"}
{"index":{}}
{"host": "::FFFF:1234", "method": "GET", "url": "/history/artemis/", "response": "200", "bytes": "9876"}

0 comments on commit 28ed843

Please sign in to comment.