-
Notifications
You must be signed in to change notification settings - Fork 0
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
Fix for arrays being in separated rows or returned as null #213
Changes from all commits
872d853
0293c10
cea553e
b8c5a54
4f5a7ab
ff901da
a48587d
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 |
---|---|---|
|
@@ -10,11 +10,15 @@ | |
|
||
import java.util.Arrays; | ||
import java.util.List; | ||
import java.util.stream.Collectors; | ||
|
||
import lombok.EqualsAndHashCode; | ||
import lombok.Getter; | ||
import lombok.RequiredArgsConstructor; | ||
import org.opensearch.sql.data.model.ExprCollectionValue; | ||
import org.opensearch.sql.data.model.ExprTupleValue; | ||
import org.opensearch.sql.data.model.ExprValue; | ||
import org.opensearch.sql.data.type.ExprCoreType; | ||
import org.opensearch.sql.data.type.ExprType; | ||
import org.opensearch.sql.expression.env.Environment; | ||
|
||
|
@@ -100,6 +104,12 @@ public ExprValue resolve(ExprTupleValue value) { | |
} | ||
|
||
private ExprValue resolve(ExprValue value, List<String> paths) { | ||
// This case is to allow returning all values in an array to be in one row | ||
if (value.type().equals(ExprCoreType.ARRAY)){ | ||
return new ExprCollectionValue(value.collectionValue().stream() | ||
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. Looks like code coverage is complaining about no test for when the |
||
.map(val -> resolve(val, paths)).collect(Collectors.toList())); | ||
} | ||
|
||
final ExprValue wholePathValue = value.keyValue(String.join(PATH_SEP, paths)); | ||
if (!wholePathValue.isMissing() || paths.size() == 1) { | ||
return wholePathValue; | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -85,22 +85,6 @@ public void testSelectNestedFieldItself() { | |
); | ||
} | ||
|
||
@Test | ||
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. We can fix these rather than deleting them.
|
||
public void testSelectObjectFieldOfArrayValuesItself() { | ||
JSONObject response = new JSONObject(query("SELECT accounts FROM %s")); | ||
|
||
// Only the first element of the list of is returned. | ||
verifyDataRows(response, rows(new JSONObject("{\"id\": 1}"))); | ||
} | ||
|
||
@Test | ||
public void testSelectObjectFieldOfArrayValuesInnerFields() { | ||
JSONObject response = new JSONObject(query("SELECT accounts.id FROM %s")); | ||
|
||
// Only the first element of the list of is returned. | ||
verifyDataRows(response, rows(1)); | ||
} | ||
|
||
private String query(String sql) { | ||
return executeQuery( | ||
StringUtils.format(sql, TEST_INDEX_DEEP_NESTED), | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,24 +5,52 @@ | |
|
||
package org.opensearch.sql.sql; | ||
|
||
import static org.opensearch.sql.legacy.TestsConstants.TEST_INDEX_MULTI_NESTED; | ||
import static org.opensearch.sql.util.MatcherUtils.rows; | ||
import static org.opensearch.sql.util.MatcherUtils.verifyDataRows; | ||
|
||
import org.json.JSONArray; | ||
import org.json.JSONObject; | ||
import org.junit.Test; | ||
import org.opensearch.sql.legacy.SQLIntegTestCase; | ||
|
||
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. Does this space between the imports pass checkstyle? |
||
import java.io.IOException; | ||
import java.util.List; | ||
|
||
import static org.opensearch.sql.legacy.TestsConstants.TEST_INDEX_NESTED_TYPE; | ||
|
||
public class NestedIT extends SQLIntegTestCase { | ||
@Override | ||
public void init() throws IOException { | ||
loadIndex(Index.NESTED); | ||
loadIndex(Index.MULTI_NESTED); | ||
} | ||
|
||
@Test | ||
public void nested_function_with_array_of_nested_field_test() { | ||
String query = "SELECT nested(message.info), nested(comment.data) FROM " + TEST_INDEX_NESTED_TYPE; | ||
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. Plugin response is {
"schema": [
{
"name": "nested(message.info)",
"type": "keyword"
},
{
"name": "nested(comment.data)",
"type": "keyword"
}
],
"total": 5,
"datarows": [
[
"a",
"ab"
],
[
"b",
"aa"
],
[
"c",
"aa"
],
[
[
"c",
"a"
],
"ab"
],
[
[
"zz"
],
[
"aa",
"bb"
]
]
],
"size": 5,
"status": 200
} It could be confusing when datatype is I think we should return all arrays and value type should be 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. I believe our solution is to split the To ensure that typings aren't broken, we could introduce partiql like syntax for nested that would make sure that only strict typing is followed on output. For example:
For now, this can be marked as 'out of scope' as the user should be responsible for proper data setup and typing. 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. I agree. It is a new feature reported in #1300. |
||
JSONObject result = executeJdbcRequest(query); | ||
|
||
assertEquals(5, result.getInt("total")); | ||
verifyDataRows(result, | ||
rows("a", "ab"), | ||
rows("b", "aa"), | ||
rows("c", "aa"), | ||
rows(new JSONArray(List.of("c","a")), "ab"), | ||
rows(new JSONArray(List.of("zz")), new JSONArray(List.of("aa", "bb")))); | ||
} | ||
|
||
@Test | ||
public void nested_string_subfield_test() { | ||
String query = "SELECT nested(message.dayOfWeek) FROM " + TEST_INDEX_NESTED_TYPE; | ||
public void nested_function_with_array_of_multi_nested_field_test() { | ||
String query = "SELECT nested(message.author.name) FROM " + TEST_INDEX_MULTI_NESTED; | ||
JSONObject result = executeJdbcRequest(query); | ||
|
||
assertEquals(5, result.getInt("total")); | ||
verifyDataRows(result, | ||
rows("e"), | ||
rows("f"), | ||
rows("g"), | ||
rows(new JSONArray(List.of("h", "p"))), | ||
rows(new JSONArray(List.of("yy")))); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
package org.opensearch.sql.sql; | ||
|
||
import org.json.JSONArray; | ||
import org.json.JSONObject; | ||
import org.junit.Test; | ||
import org.opensearch.sql.legacy.SQLIntegTestCase; | ||
|
||
import java.io.IOException; | ||
import java.util.List; | ||
Comment on lines
+3
to
+9
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. Do these imports pass checkstyle with the extra space and them out of alphabetical order? |
||
|
||
import static org.opensearch.sql.legacy.TestsConstants.TEST_INDEX_MULTI_NESTED; | ||
import static org.opensearch.sql.legacy.TestsConstants.TEST_INDEX_NESTED_TYPE; | ||
import static org.opensearch.sql.util.MatcherUtils.rows; | ||
import static org.opensearch.sql.util.MatcherUtils.verifyDataRows; | ||
|
||
public class NestedPartiQLIT extends SQLIntegTestCase { | ||
@Override | ||
public void init() throws IOException { | ||
loadIndex(Index.NESTED); | ||
loadIndex(Index.MULTI_NESTED); | ||
} | ||
|
||
@Test | ||
public void partiQL_with_array_of_nested_field_test() { | ||
String query = "SELECT message.info, comment.data FROM " + TEST_INDEX_NESTED_TYPE; | ||
JSONObject result = executeJdbcRequest(query); | ||
|
||
assertEquals(5, result.getInt("total")); | ||
verifyDataRows(result, | ||
rows("a", "ab"), | ||
rows("b", "aa"), | ||
rows("c", "aa"), | ||
rows(new JSONArray(List.of("c","a")), "ab"), | ||
rows(new JSONArray(List.of("zz")), new JSONArray(List.of("aa", "bb")))); | ||
} | ||
|
||
@Test | ||
public void partiQL_with_array_of_multi_nested_field_test() { | ||
String query = "SELECT message.author.name, message.info FROM " + TEST_INDEX_MULTI_NESTED; | ||
JSONObject result = executeJdbcRequest(query); | ||
|
||
assertEquals(5, result.getInt("total")); | ||
verifyDataRows(result, | ||
rows("e", "a"), | ||
rows("f", "b"), | ||
rows("g", "c"), | ||
rows(new JSONArray(List.of("h", "p")), new JSONArray(List.of("d","i"))), | ||
rows(new JSONArray(List.of("yy")), new JSONArray(List.of("zz")))); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
package org.opensearch.sql.sql; | ||
|
||
import org.json.JSONArray; | ||
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. checkstyle passes? |
||
import org.json.JSONObject; | ||
import org.junit.Test; | ||
import org.opensearch.sql.legacy.SQLIntegTestCase; | ||
|
||
import java.io.IOException; | ||
import java.util.List; | ||
|
||
import static org.opensearch.sql.legacy.TestsConstants.TEST_INDEX_MULTI_NESTED_OBJECT; | ||
import static org.opensearch.sql.legacy.TestsConstants.TEST_INDEX_NESTED_OBJECT; | ||
import static org.opensearch.sql.util.MatcherUtils.rows; | ||
import static org.opensearch.sql.util.MatcherUtils.verifyDataRows; | ||
|
||
public class ObjectIT extends SQLIntegTestCase { | ||
@Override | ||
public void init() throws IOException { | ||
loadIndex(Index.NESTED_OBJECT); | ||
loadIndex(Index.MULTI_NESTED_OBJECT); | ||
} | ||
|
||
@Test | ||
public void object_array_of_objects_test() { | ||
String query = "SELECT message.info, comment.data FROM " + TEST_INDEX_NESTED_OBJECT; | ||
JSONObject result = executeJdbcRequest(query); | ||
|
||
assertEquals(5, result.getInt("total")); | ||
verifyDataRows(result, | ||
rows("a", "ab"), | ||
rows("b", "aa"), | ||
rows("c", "aa"), | ||
rows(new JSONArray(List.of("c","a")), "ab"), | ||
rows(new JSONArray(List.of("zz")), new JSONArray(List.of("aa", "bb")))); | ||
} | ||
|
||
@Test | ||
public void object_with_array_of_multi_nested_objects_test() { | ||
String query = "SELECT message.author.name, message.info FROM " + TEST_INDEX_MULTI_NESTED_OBJECT; | ||
JSONObject result = executeJdbcRequest(query); | ||
|
||
assertEquals(5, result.getInt("total")); | ||
verifyDataRows(result, | ||
rows("e", "a"), | ||
rows("f", "b"), | ||
rows("g", "c"), | ||
rows(new JSONArray(List.of("h", "p")), new JSONArray(List.of("d","i"))), | ||
rows(new JSONArray(List.of("yy")), new JSONArray(List.of("zz")))); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,9 @@ | ||
{"index":{"_id":"1"}} | ||
{"message":[{"info":"a","author":{"name": "e", "address": {"street": "bc", "number": 1}},"dayOfWeek":1}]} | ||
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. If we are updating the test data, maybe we could update the values to be better then one or two single letters. |
||
{"message":{"info":"a","author":{"name": "e", "address": {"street": "bc", "number": 1}},"dayOfWeek":1}} | ||
{"index":{"_id":"2"}} | ||
{"message":[{"info":"b","author":{"name": "f", "address": {"street": "ab", "number": 2}},"dayOfWeek":2}]} | ||
{"message":{"info":"b","author":{"name": "f", "address": {"street": "ab", "number": 2}},"dayOfWeek":2}} | ||
{"index":{"_id":"3"}} | ||
{"message":[{"info":"c","author":{"name": "g", "address": {"street": "sk", "number": 3}},"dayOfWeek":1}]} | ||
{"message":{"info":"c","author":{"name": "g", "address": {"street": "sk", "number": 3}},"dayOfWeek":1}} | ||
{"index":{"_id":"4"}} | ||
{"message":[{"info":"d","author":{"name": "h", "address": {"street": "mb", "number": 4}},"dayOfWeek":4},{"info":"i","author":{"name": "p", "address": {"street": "on", "number": 5}},"dayOfWeek":5}]} | ||
{"index":{"_id":"5"}} | ||
|
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.
Does this pass checkstyle?