|
| 1 | +/* |
| 2 | + * Copyright OpenSearch Contributors |
| 3 | + * SPDX-License-Identifier: Apache-2.0 |
| 4 | + */ |
| 5 | + |
| 6 | +package org.opensearch.sql.ppl; |
| 7 | + |
| 8 | +import static org.opensearch.sql.legacy.TestsConstants.*; |
| 9 | + |
| 10 | +import java.io.IOException; |
| 11 | +import org.json.JSONObject; |
| 12 | +import org.junit.jupiter.api.Test; |
| 13 | + |
| 14 | +public class RegexCommandIT extends PPLIntegTestCase { |
| 15 | + |
| 16 | + @Override |
| 17 | + public void init() throws Exception { |
| 18 | + super.init(); |
| 19 | + enableCalcite(); |
| 20 | + loadIndex(Index.ACCOUNT); |
| 21 | + } |
| 22 | + |
| 23 | + @Test |
| 24 | + public void testRegexBasicStringMatch() throws IOException { |
| 25 | + JSONObject result = |
| 26 | + executeQuery( |
| 27 | + String.format( |
| 28 | + "source=%s | regex firstname='Amber' | fields account_number, firstname", |
| 29 | + TEST_INDEX_ACCOUNT)); |
| 30 | + |
| 31 | + assertEquals(1, result.getJSONArray("datarows").length()); |
| 32 | + assertEquals("Amber", result.getJSONArray("datarows").getJSONArray(0).get(1)); |
| 33 | + } |
| 34 | + |
| 35 | + @Test |
| 36 | + public void testRegexPartialStringMatch() throws IOException { |
| 37 | + JSONObject result = |
| 38 | + executeQuery( |
| 39 | + String.format( |
| 40 | + "source=%s | regex firstname='nan' | fields account_number, firstname", |
| 41 | + TEST_INDEX_ACCOUNT)); |
| 42 | + |
| 43 | + // Should match names containing "nan": Fernandez, Buchanan |
| 44 | + assertEquals(2, result.getJSONArray("datarows").length()); |
| 45 | + // Verify one of the results contains "nan" |
| 46 | + String firstName = result.getJSONArray("datarows").getJSONArray(0).get(1).toString(); |
| 47 | + assertTrue(firstName.contains("nan")); |
| 48 | + } |
| 49 | + |
| 50 | + @Test |
| 51 | + public void testRegexPatternMatch() throws IOException { |
| 52 | + JSONObject result = |
| 53 | + executeQuery( |
| 54 | + String.format( |
| 55 | + "source=%s | regex firstname='A.*' | fields account_number, firstname", |
| 56 | + TEST_INDEX_ACCOUNT)); |
| 57 | + |
| 58 | + // Should match names starting with A - there are 66 such names in accounts.json |
| 59 | + assertEquals(66, result.getJSONArray("datarows").length()); |
| 60 | + // Verify first result is a name starting with A |
| 61 | + assertTrue(result.getJSONArray("datarows").getJSONArray(0).get(1).toString().startsWith("A")); |
| 62 | + } |
| 63 | + |
| 64 | + @Test |
| 65 | + public void testRegexNegatedMatch() throws IOException { |
| 66 | + JSONObject result = |
| 67 | + executeQuery( |
| 68 | + String.format( |
| 69 | + "source=%s | regex firstname!='Amber' | fields account_number, firstname | head 3", |
| 70 | + TEST_INDEX_ACCOUNT)); |
| 71 | + |
| 72 | + assertEquals(3, result.getJSONArray("datarows").length()); |
| 73 | + // Verify Amber is not in results |
| 74 | + for (int i = 0; i < result.getJSONArray("datarows").length(); i++) { |
| 75 | + assertNotEquals("Amber", result.getJSONArray("datarows").getJSONArray(i).get(1)); |
| 76 | + } |
| 77 | + } |
| 78 | + |
| 79 | + @Test |
| 80 | + public void testRegexWithStateField() throws IOException { |
| 81 | + JSONObject result = |
| 82 | + executeQuery( |
| 83 | + String.format( |
| 84 | + "source=%s | regex state='CA' | fields account_number, firstname, state", |
| 85 | + TEST_INDEX_ACCOUNT)); |
| 86 | + |
| 87 | + // There are 17 CA records in accounts.json |
| 88 | + assertEquals(17, result.getJSONArray("datarows").length()); |
| 89 | + assertEquals("CA", result.getJSONArray("datarows").getJSONArray(0).get(2)); |
| 90 | + } |
| 91 | +} |
0 commit comments