|
| 1 | +/* |
| 2 | + * Copyright OpenSearch Contributors |
| 3 | + * SPDX-License-Identifier: Apache-2.0 |
| 4 | + */ |
| 5 | + |
| 6 | +package org.opensearch.sql.ppl; |
| 7 | + |
| 8 | +import org.junit.Test; |
| 9 | + |
| 10 | +import java.io.IOException; |
| 11 | + |
| 12 | +import static org.opensearch.sql.legacy.TestsConstants.TEST_INDEX_BEER; |
| 13 | +import static org.opensearch.sql.legacy.TestsConstants.TEST_INDEX_CALCS; |
| 14 | +import static org.opensearch.sql.util.MatcherUtils.rows; |
| 15 | +import static org.opensearch.sql.util.MatcherUtils.schema; |
| 16 | +import static org.opensearch.sql.util.MatcherUtils.verifyDataRows; |
| 17 | +import static org.opensearch.sql.util.MatcherUtils.verifySchema; |
| 18 | + |
| 19 | +public class PositionFunctionIT extends PPLIntegTestCase { |
| 20 | + @Override |
| 21 | + public void init() throws IOException { |
| 22 | + loadIndex(Index.CALCS); |
| 23 | + } |
| 24 | + |
| 25 | + @Test |
| 26 | + public void test_position_function() throws IOException { |
| 27 | + String query = "source=" + TEST_INDEX_CALCS |
| 28 | + + " | eval f=position('ON', str1) | fields f"; |
| 29 | + |
| 30 | + var result = executeQuery(query); |
| 31 | + |
| 32 | + assertEquals(17, result.getInt("total")); |
| 33 | + verifyDataRows(result, |
| 34 | + rows(7), rows(7), |
| 35 | + rows(2), rows(0), |
| 36 | + rows(0), rows(0), |
| 37 | + rows(0), rows(0), |
| 38 | + rows(0), rows(0), |
| 39 | + rows(0), rows(0), |
| 40 | + rows(0), rows(0), |
| 41 | + rows(0), rows(0), |
| 42 | + rows(0)); |
| 43 | + } |
| 44 | + |
| 45 | + @Test |
| 46 | + public void test_position_function_with_fields_only() throws IOException { |
| 47 | + String query = "source=" + TEST_INDEX_CALCS |
| 48 | + + " | eval f=position(str3 IN str2) | where str2 IN ('one', 'two', 'three')| fields f"; |
| 49 | + |
| 50 | + var result = executeQuery(query); |
| 51 | + |
| 52 | + assertEquals(3, result.getInt("total")); |
| 53 | + verifyDataRows(result, rows(3), rows(0), rows(4)); |
| 54 | + } |
| 55 | + |
| 56 | + @Test |
| 57 | + public void test_position_function_with_string_literals() throws IOException { |
| 58 | + String query = "source=" + TEST_INDEX_CALCS |
| 59 | + + " | eval f=position('world' IN 'hello world') | where str2='one' | fields f"; |
| 60 | + |
| 61 | + var result = executeQuery(query); |
| 62 | + |
| 63 | + assertEquals(1, result.getInt("total")); |
| 64 | + verifyDataRows(result, rows(7)); |
| 65 | + } |
| 66 | + |
| 67 | + @Test |
| 68 | + public void test_position_function_with_nulls() throws IOException { |
| 69 | + String query = "source=" + TEST_INDEX_CALCS |
| 70 | + + " | eval f=position('ee' IN str2) | where isnull(str2) | fields str2,f"; |
| 71 | + |
| 72 | + var result = executeQuery(query); |
| 73 | + |
| 74 | + assertEquals(4, result.getInt("total")); |
| 75 | + verifyDataRows(result, |
| 76 | + rows(null, null), |
| 77 | + rows(null, null), |
| 78 | + rows(null, null), |
| 79 | + rows(null, null)); |
| 80 | + } |
| 81 | + |
| 82 | + @Test |
| 83 | + public void test_position_function_with_function_as_arg() throws IOException { |
| 84 | + String query = "source=" + TEST_INDEX_CALCS |
| 85 | + + " | eval f=position(upper(str3) IN str1) | where like(str1, 'BINDING SUPPLIES') | fields f"; |
| 86 | + |
| 87 | + var result = executeQuery(query); |
| 88 | + |
| 89 | + assertEquals(1, result.getInt("total")); |
| 90 | + verifyDataRows(result, rows(15)); |
| 91 | + } |
| 92 | + |
| 93 | + @Test |
| 94 | + public void test_position_function_with_function_in_where_clause() throws IOException { |
| 95 | + String query = "source=" + TEST_INDEX_CALCS |
| 96 | + + " | where position(str3 IN str2)=1 | fields str2"; |
| 97 | + |
| 98 | + var result = executeQuery(query); |
| 99 | + |
| 100 | + assertEquals(2, result.getInt("total")); |
| 101 | + verifyDataRows(result, rows("eight"), rows("eleven")); |
| 102 | + } |
| 103 | +} |
0 commit comments