-
Notifications
You must be signed in to change notification settings - Fork 181
Add position() function to V2 engine #1121
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 6 commits
57ae38b
b6d9a39
52714ae
ee8cb7d
3786f04
f4ee6d0
17bd91d
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -2330,6 +2330,31 @@ Example:: | |
| +---------------------+---------------------+ | ||
|
|
||
|
|
||
| POSITION | ||
| ------ | ||
|
|
||
| Description | ||
| >>>>>>>>>>> | ||
|
|
||
| Usage: The syntax POSITION(substr IN str) returns the position of the first occurrence of substring substr in string str. Returns 0 if substr is not in str. Returns NULL if any argument is NULL. | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Returns 0 if substr is not in str? not -1?
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This function is the synonym of the |
||
|
|
||
| Argument type: STRING, STRING | ||
|
|
||
| Return type integer: | ||
|
|
||
| (STRING IN STRING) -> INTEGER | ||
|
|
||
| Example:: | ||
|
|
||
| os> SELECT POSITION('world' IN 'helloworld'), POSITION('invalid' IN 'helloworld'); | ||
| fetched rows / total rows = 1/1 | ||
| +-------------------------------------+---------------------------------------+ | ||
| | POSITION('world' IN 'helloworld') | POSITION('invalid' IN 'helloworld') | | ||
| |-------------------------------------+---------------------------------------| | ||
| | 6 | 0 | | ||
| +-------------------------------------+---------------------------------------+ | ||
|
|
||
|
|
||
| REPLACE | ||
| ------- | ||
|
|
||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,129 @@ | ||
| /* | ||
| * Copyright OpenSearch Contributors | ||
| * SPDX-License-Identifier: Apache-2.0 | ||
| */ | ||
|
|
||
| package org.opensearch.sql.sql; | ||
|
|
||
| import org.json.JSONObject; | ||
| import org.junit.Test; | ||
| import org.opensearch.sql.legacy.SQLIntegTestCase; | ||
| import org.opensearch.sql.legacy.TestsConstants; | ||
|
|
||
| import static org.opensearch.sql.util.MatcherUtils.rows; | ||
| import static org.opensearch.sql.util.MatcherUtils.schema; | ||
| import static org.opensearch.sql.util.MatcherUtils.verifyDataRows; | ||
| import static org.opensearch.sql.util.MatcherUtils.verifySchema; | ||
|
|
||
| public class PositionFunctionIT extends SQLIntegTestCase { | ||
|
|
||
| @Override | ||
| protected void init() throws Exception { | ||
| loadIndex(Index.PEOPLE2); | ||
| loadIndex(Index.CALCS); | ||
| } | ||
|
|
||
| @Test | ||
| public void position_function_test() { | ||
| String query = "SELECT firstname, position('a' IN firstname) FROM %s"; | ||
| JSONObject response = executeJdbcRequest(String.format(query, TestsConstants.TEST_INDEX_PEOPLE2)); | ||
|
|
||
| verifySchema(response, schema("firstname", null, "keyword"), | ||
| schema("position('a' IN firstname)", null, "integer")); | ||
| assertEquals(12, response.getInt("total")); | ||
|
|
||
| verifyDataRows(response, | ||
| rows("Daenerys", 2), rows("Hattie", 2), | ||
| rows("Nanette", 2), rows("Dale", 2), | ||
| rows("Elinor", 0), rows("Virginia", 8), | ||
| rows("Dillard", 5), rows("Mcgee", 0), | ||
| rows("Aurelia", 7), rows("Fulton", 0), | ||
| rows("Burton", 0), rows("Josie", 0)); | ||
| } | ||
|
|
||
| @Test | ||
| public void position_function_with_nulls_test() { | ||
| String query = "SELECT str2, position('ee' IN str2) FROM %s"; | ||
| JSONObject response = executeJdbcRequest(String.format(query, TestsConstants.TEST_INDEX_CALCS)); | ||
|
|
||
| verifySchema(response, schema("str2", null, "keyword"), | ||
| schema("position('ee' IN str2)", null, "integer")); | ||
| assertEquals(17, response.getInt("total")); | ||
|
|
||
| verifyDataRows(response, | ||
| rows("one", 0), rows("two", 0), | ||
| rows("three", 4), rows(null, null), | ||
| rows("five", 0), rows("six", 0), | ||
| rows(null, null), rows("eight", 0), | ||
| rows("nine", 0), rows("ten", 0), | ||
| rows("eleven", 0), rows("twelve", 0), | ||
| rows(null, null), rows("fourteen", 6), | ||
| rows("fifteen", 5), rows("sixteen", 5), | ||
| rows(null, null)); | ||
| } | ||
|
|
||
| @Test | ||
| public void position_function_with_string_literals_test() { | ||
| String query = "SELECT position('world' IN 'hello world')"; | ||
| JSONObject response = executeJdbcRequest(query); | ||
|
|
||
| verifySchema(response, schema("position('world' IN 'hello world')", null, "integer")); | ||
| assertEquals(1, response.getInt("total")); | ||
|
|
||
| verifyDataRows(response, rows(7)); | ||
| } | ||
|
|
||
| @Test | ||
| public void position_function_with_only_fields_as_args_test() { | ||
| String query = "SELECT position(str3 IN str2) FROM %s WHERE str2 IN ('one', 'two', 'three')"; | ||
| JSONObject response = executeJdbcRequest(String.format(query, TestsConstants.TEST_INDEX_CALCS)); | ||
|
|
||
| verifySchema(response, schema("position(str3 IN str2)", null, "integer")); | ||
| assertEquals(3, response.getInt("total")); | ||
|
|
||
| verifyDataRows(response, rows(3), rows(0), rows(4)); | ||
| } | ||
|
|
||
| @Test | ||
| public void position_function_with_function_as_arg_test() { | ||
| String query = "SELECT position(upper(str3) IN str1) FROM %s WHERE str1 LIKE 'BINDING SUPPLIES'"; | ||
| JSONObject response = executeJdbcRequest(String.format(query, TestsConstants.TEST_INDEX_CALCS)); | ||
|
|
||
| verifySchema(response, schema("position(upper(str3) IN str1)", null, "integer")); | ||
| assertEquals(1, response.getInt("total")); | ||
|
|
||
| verifyDataRows(response, rows(15)); | ||
| } | ||
|
|
||
| @Test | ||
| public void position_function_in_where_clause_test() { | ||
| String query = "SELECT str2 FROM %s WHERE position(str3 IN str2)=1"; | ||
| JSONObject response = executeJdbcRequest(String.format(query, TestsConstants.TEST_INDEX_CALCS)); | ||
|
|
||
| verifySchema(response, schema("str2", null, "keyword")); | ||
| assertEquals(2, response.getInt("total")); | ||
|
|
||
| verifyDataRows(response, rows("eight"), rows("eleven")); | ||
| } | ||
|
|
||
| @Test | ||
| public void position_function_with_null_args_test() { | ||
| String query1 = "SELECT str2, position(null IN str2) FROM %s WHERE str2 IN ('one')"; | ||
| String query2 = "SELECT str2, position(str2 IN null) FROM %s WHERE str2 IN ('one')"; | ||
| JSONObject response1 = executeJdbcRequest(String.format(query1, TestsConstants.TEST_INDEX_CALCS)); | ||
| JSONObject response2 = executeJdbcRequest(String.format(query2, TestsConstants.TEST_INDEX_CALCS)); | ||
|
|
||
| verifySchema(response1, | ||
| schema("str2", null, "keyword"), | ||
| schema("position(null IN str2)", null, "integer")); | ||
| assertEquals(1, response1.getInt("total")); | ||
|
|
||
| verifySchema(response2, | ||
| schema("str2", null, "keyword"), | ||
| schema("position(str2 IN null)", null, "integer")); | ||
| assertEquals(1, response2.getInt("total")); | ||
|
|
||
| verifyDataRows(response1, rows("one", null)); | ||
| verifyDataRows(response2, rows("one", null)); | ||
| } | ||
| } |

Uh oh!
There was an error while loading. Please reload this page.