-
Notifications
You must be signed in to change notification settings - Fork 141
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
Add position() function to V2 engine #1121
Merged
MaxKsyunz
merged 7 commits into
opensearch-project:2.x
from
Bit-Quill:integ-add-position-to-v2
Dec 6, 2022
+241
−23
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
57ae38b
Add position() to V2 engine (#177)
margarit-h b6d9a39
Address doctest failure
margarit-h 52714ae
Address PR review feedback
margarit-h ee8cb7d
Fixes after rebase
margarit-h 3786f04
Addressed PR review comments
margarit-h f4ee6d0
minor fix
margarit-h 17bd91d
Add more PR review feedback
margarit-h File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
129 changes: 129 additions & 0 deletions
129
integ-test/src/test/java/org/opensearch/sql/sql/PositionFunctionIT.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Returns 0 if substr is not in str? not -1?
what if POSITION('world' IN 'world')?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This function is the synonym of the
LOCATE
function. And works the same way in mySQL and legacy engine.It returns 0, if substr in not in string like
LOCATE
andPOSITION
do in mySql and legacy engine.POSITION('world' IN 'world')
would return 1 as the first argument matches the given string (2nd arg) starting from the 1st position.