Skip to content

Commit 41d7bf5

Browse files
committed
Base array functionality working with IT tests.
Signed-off-by: forestmvey <[email protected]>
1 parent 579d44a commit 41d7bf5

File tree

8 files changed

+118
-21
lines changed

8 files changed

+118
-21
lines changed

common/src/main/java/org/opensearch/sql/common/utils/StringUtils.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,6 @@ private static boolean isQuoted(String text, String mark) {
107107
}
108108

109109
public static String removeParenthesis(String qualifier) {
110-
return qualifier.replaceAll("\\[.+\\]", "");
110+
return qualifier.replaceAll("\\[\\d+\\]", "");
111111
}
112112
}

core/src/main/java/org/opensearch/sql/expression/ArrayReferenceExpression.java

Lines changed: 18 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717
import lombok.Getter;
1818
import org.apache.commons.lang3.tuple.Pair;
1919
import org.opensearch.sql.common.utils.StringUtils;
20+
import org.opensearch.sql.data.model.ExprCollectionValue;
21+
import org.opensearch.sql.data.model.ExprMissingValue;
2022
import org.opensearch.sql.data.model.ExprTupleValue;
2123
import org.opensearch.sql.data.model.ExprValue;
2224
import org.opensearch.sql.data.model.ExprValueUtils;
@@ -31,13 +33,13 @@ public class ArrayReferenceExpression extends ReferenceExpression {
3133
@Getter
3234
private final ExprType type;
3335
public ArrayReferenceExpression(String ref, ExprType type, List<Pair<String, OptionalInt>> partsAndIndexes) {
34-
super(ref, type);
36+
super(StringUtils.removeParenthesis(ref), type);
3537
this.partsAndIndexes = partsAndIndexes;
3638
this.type = type;
3739
}
3840

3941
public ArrayReferenceExpression(ReferenceExpression ref) {
40-
super(ref.toString(), ref.type());
42+
super(StringUtils.removeParenthesis(ref.toString()), ref.type());
4143
this.partsAndIndexes = Arrays.stream(ref.toString().split("\\.")).map(e -> Pair.of(e, OptionalInt.empty())).collect(
4244
Collectors.toList());
4345
this.type = ref.type();
@@ -68,28 +70,25 @@ private ExprValue resolve(ExprValue value, List<Pair<String, OptionalInt>> paths
6870
ExprValue wholePathValue = value.keyValue(String.join(PATH_SEP, pathsWithoutParenthesis));
6971

7072
if (!paths.get(0).getRight().isEmpty()) {
71-
wholePathValue = getIndexValueOfCollection(wholePathValue);
72-
} else if (paths.size() == 1 && !wholePathValue.type().equals(ExprCoreType.ARRAY)) {
73-
wholePathValue = ExprValueUtils.missingValue();
73+
if (value.keyValue(pathsWithoutParenthesis.get(0)) instanceof ExprCollectionValue) { // TODO check array size
74+
wholePathValue = value
75+
.keyValue(pathsWithoutParenthesis.get(0))
76+
.collectionValue()
77+
.get(paths.get(0).getRight().getAsInt());
78+
if (paths.size() != 1) {
79+
return resolve(wholePathValue, paths.subList(1, paths.size()));
80+
}
81+
} else {
82+
return ExprValueUtils.missingValue();
83+
}
84+
} else if (wholePathValue.isMissing()) {
85+
return resolve(value.keyValue(pathsWithoutParenthesis.get(0)), paths.subList(1, paths.size()));
7486
}
7587

7688
if (!wholePathValue.isMissing() || paths.size() == 1) {
7789
return wholePathValue;
7890
} else {
79-
return resolve(value.keyValue(pathsWithoutParenthesis.get(0)
80-
), paths.subList(1, paths.size()));
91+
return resolve(value.keyValue(pathsWithoutParenthesis.get(0)), paths.subList(1, paths.size()));
8192
}
8293
}
83-
84-
private ExprValue getIndexValueOfCollection(ExprValue value) {
85-
ExprValue collectionVal = value;
86-
// for (OptionalInt currentIndex : List.of(index)) {
87-
if (collectionVal.type().equals(ExprCoreType.ARRAY)) {
88-
// collectionVal = collectionVal.collectionValue().get(currentIndex.getAsInt());
89-
} else {
90-
return ExprValueUtils.missingValue();
91-
}
92-
// }
93-
return collectionVal;
94-
}
9594
}

integ-test/src/test/java/org/opensearch/sql/legacy/SQLIntegTestCase.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
import static com.google.common.base.Strings.isNullOrEmpty;
3939
import static org.opensearch.sql.legacy.TestUtils.createIndexByRestClient;
4040
import static org.opensearch.sql.legacy.TestUtils.getAccountIndexMapping;
41+
import static org.opensearch.sql.legacy.TestUtils.getArraysIndexMapping;
4142
import static org.opensearch.sql.legacy.TestUtils.getBankIndexMapping;
4243
import static org.opensearch.sql.legacy.TestUtils.getBankWithNullValuesIndexMapping;
4344
import static org.opensearch.sql.legacy.TestUtils.getDataTypeNonnumericIndexMapping;
@@ -681,7 +682,11 @@ public enum Index {
681682
NESTED_WITH_NULLS(TestsConstants.TEST_INDEX_NESTED_WITH_NULLS,
682683
"multi_nested",
683684
getNestedTypeIndexMapping(),
684-
"src/test/resources/nested_with_nulls.json");
685+
"src/test/resources/nested_with_nulls.json"),
686+
ARRAYS(TestsConstants.TEST_INDEX_ARRAYS,
687+
"arrays",
688+
getArraysIndexMapping(),
689+
"src/test/resources/arrays.json");
685690

686691
private final String name;
687692
private final String type;

integ-test/src/test/java/org/opensearch/sql/legacy/TestUtils.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -223,6 +223,11 @@ public static String getDateIndexMapping() {
223223
return getMappingFile(mappingFile);
224224
}
225225

226+
public static String getArraysIndexMapping() {
227+
String mappingFile = "arrays_mapping.json";
228+
return getMappingFile(mappingFile);
229+
}
230+
226231
public static String getDateTimeIndexMapping() {
227232
String mappingFile = "date_time_index_mapping.json";
228233
return getMappingFile(mappingFile);

integ-test/src/test/java/org/opensearch/sql/legacy/TestsConstants.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ public class TestsConstants {
4848
public final static String TEST_INDEX_ORDER = TEST_INDEX + "_order";
4949
public final static String TEST_INDEX_WEBLOG = TEST_INDEX + "_weblog";
5050
public final static String TEST_INDEX_DATE = TEST_INDEX + "_date";
51+
public final static String TEST_INDEX_ARRAYS = TEST_INDEX + "_arrays";
5152
public final static String TEST_INDEX_DATE_TIME = TEST_INDEX + "_datetime";
5253
public final static String TEST_INDEX_DEEP_NESTED = TEST_INDEX + "_deep_nested";
5354
public final static String TEST_INDEX_STRINGS = TEST_INDEX + "_strings";
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
/*
2+
* Copyright OpenSearch Contributors
3+
* SPDX-License-Identifier: Apache-2.0
4+
*/
5+
6+
package org.opensearch.sql.sql;
7+
8+
import static org.opensearch.sql.legacy.TestsConstants.TEST_INDEX_ARRAYS;
9+
import static org.opensearch.sql.util.MatcherUtils.rows;
10+
import static org.opensearch.sql.util.MatcherUtils.verifyDataRows;
11+
12+
import java.io.IOException;
13+
import java.util.List;
14+
15+
import com.google.common.collect.ImmutableMap;
16+
import org.json.JSONArray;
17+
import org.json.JSONObject;
18+
import org.junit.Test;
19+
import org.opensearch.sql.legacy.SQLIntegTestCase;
20+
21+
public class ArraysIT extends SQLIntegTestCase {
22+
@Override
23+
public void init() throws IOException {
24+
loadIndex(Index.ARRAYS);
25+
}
26+
27+
@Test
28+
public void object_array_index_1_test() {
29+
String query = "SELECT objectArray[0] FROM " + TEST_INDEX_ARRAYS;
30+
JSONObject result = executeJdbcRequest(query);
31+
32+
verifyDataRows(result,
33+
rows(new JSONObject(ImmutableMap.of("innerObject", List.of(1, 2)))));
34+
}
35+
36+
@Test
37+
public void object_array_index_1_inner_object_test() {
38+
String query = "SELECT objectArray[0].innerObject FROM " + TEST_INDEX_ARRAYS;
39+
JSONObject result = executeJdbcRequest(query);
40+
41+
verifyDataRows(result,
42+
rows(new JSONArray(List.of(1, 2))));
43+
}
44+
45+
@Test
46+
public void object_array_index_1_inner_object_index_1_test() {
47+
String query = "SELECT objectArray[0].innerObject[0] FROM " + TEST_INDEX_ARRAYS;
48+
JSONObject result = executeJdbcRequest(query);
49+
50+
verifyDataRows(result,
51+
rows(1));
52+
}
53+
54+
@Test
55+
public void multi_object_array_index_1_test() {
56+
String query = "SELECT multiObjectArray[0] FROM " + TEST_INDEX_ARRAYS;
57+
JSONObject result = executeJdbcRequest(query);
58+
59+
verifyDataRows(result,
60+
rows(new JSONObject(ImmutableMap.of("id", 1, "name", "blah"))));
61+
}
62+
}
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
{"index":{"_id":"1"}}
2+
{"objectArray": [{"innerObject": [1, 2]}, {"innerObject": 3}], "multiObjectArray":[{"id": 1, "name": "blah"}, {"id": 2,"name": "hoo"}]}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
{
2+
"mappings": {
3+
"properties": {
4+
"objectArray": {
5+
"properties": {
6+
"innerObject": {
7+
"type": "keyword"
8+
}
9+
}
10+
},
11+
"multiObjectArray": {
12+
"properties": {
13+
"id": {
14+
"type": "long"
15+
},
16+
"name": {
17+
"type": "keyword"
18+
}
19+
}
20+
}
21+
}
22+
}
23+
}

0 commit comments

Comments
 (0)