Skip to content

Commit 63f8cb9

Browse files
committed
simplified code
Signed-off-by: xinyual <xinyual@amazon.com>
1 parent 1d06008 commit 63f8cb9

3 files changed

Lines changed: 42 additions & 39 deletions

File tree

integ-test/src/test/java/org/opensearch/sql/calcite/standalone/CalcitePPLBasicIT.java

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -566,23 +566,19 @@ public void testMetaFieldAlias() {
566566
}
567567

568568
@Test
569-
public void testMergeTwoObject() {
570-
JSONObject result =
571-
executeQuery(
572-
String.format("source=%s | fields machine.os1 ", TEST_INDEX_MERGE_TEST_WILDCARD));
573-
verifySchema(result, schema("original_col", "integer"), schema("alias_col", "integer"));
574-
verifyDataRows(result, rows(2, 2), rows(3, 3));
575-
}
576-
577-
@Test
578-
public void testMergeTwoObjectReturn() {
569+
public void testFieldsMergedObject() {
579570
JSONObject result =
580571
executeQuery(
581572
String.format(
582573
"source=%s | fields machine.os1, machine.os2, machine_array.os1, "
583574
+ " machine_array.os2",
584575
TEST_INDEX_MERGE_TEST_WILDCARD));
585-
verifySchema(result, schema("original_col", "integer"), schema("alias_col", "integer"));
586-
verifyDataRows(result, rows(2, 2), rows(3, 3));
576+
verifySchema(
577+
result,
578+
schema("machine.os1", "string"),
579+
schema("machine.os2", "string"),
580+
schema("machine_array.os1", "string"),
581+
schema("machine_array.os2", "string"));
582+
verifyDataRows(result, rows("linux", null, "linux", null), rows(null, "linux", null, "linux"));
587583
}
588584
}

integ-test/src/test/java/org/opensearch/sql/ppl/FieldsCommandIT.java

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,7 @@
55

66
package org.opensearch.sql.ppl;
77

8-
import static org.opensearch.sql.legacy.TestsConstants.TEST_INDEX_ACCOUNT;
9-
import static org.opensearch.sql.legacy.TestsConstants.TEST_INDEX_BANK;
8+
import static org.opensearch.sql.legacy.TestsConstants.*;
109
import static org.opensearch.sql.util.MatcherUtils.columnName;
1110
import static org.opensearch.sql.util.MatcherUtils.columnPattern;
1211
import static org.opensearch.sql.util.MatcherUtils.rows;
@@ -28,6 +27,8 @@ public void init() throws Exception {
2827
super.init();
2928
loadIndex(Index.ACCOUNT);
3029
loadIndex(Index.BANK);
30+
loadIndex(Index.MERGE_TEST_1);
31+
loadIndex(Index.MERGE_TEST_2);
3132
}
3233

3334
@Test
@@ -105,4 +106,21 @@ public void testMetadataFieldsWithEvalMetaField() {
105106
"source=%s | eval _id = 1 | fields firstname, _id", TEST_INDEX_ACCOUNT)));
106107
verifyErrorMessageContains(e, "Cannot use metadata field [_id] as the eval field.");
107108
}
109+
110+
@Test
111+
public void testFieldsTwoMergedObject() throws IOException {
112+
JSONObject result =
113+
executeQuery(
114+
String.format(
115+
"source=%s | fields machine.os1, machine.os2, machine_array.os1, "
116+
+ " machine_array.os2",
117+
TEST_INDEX_MERGE_TEST_WILDCARD));
118+
verifySchema(
119+
result,
120+
schema("machine.os1", "string"),
121+
schema("machine.os2", "string"),
122+
schema("machine_array.os1", "string"),
123+
schema("machine_array.os2", "string"));
124+
verifyDataRows(result, rows("linux", null, "linux", null), rows(null, "linux", null, "linux"));
125+
}
108126
}

opensearch/src/main/java/org/opensearch/sql/opensearch/request/system/OpenSearchDescribeIndexRequest.java

Lines changed: 14 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
import java.util.List;
1616
import java.util.Locale;
1717
import java.util.Map;
18+
import lombok.extern.log4j.Log4j2;
1819
import org.opensearch.sql.data.model.ExprTupleValue;
1920
import org.opensearch.sql.data.model.ExprValue;
2021
import org.opensearch.sql.data.type.ExprCoreType;
@@ -25,6 +26,7 @@
2526
import org.opensearch.sql.opensearch.mapping.IndexMapping;
2627
import org.opensearch.sql.opensearch.request.OpenSearchRequest;
2728

29+
@Log4j2
2830
/** Describe index meta data request. */
2931
public class OpenSearchDescribeIndexRequest implements OpenSearchSystemRequest {
3032

@@ -100,40 +102,27 @@ public Map<String, OpenSearchDataType> getFieldTypes() {
100102
Map<String, OpenSearchDataType> fieldTypes = new HashMap<>();
101103
Map<String, IndexMapping> indexMappings =
102104
client.getIndexMappings(getLocalIndexNames(indexName.getIndexNames()));
105+
log.error(indexMappings.keySet());
103106
for (IndexMapping indexMapping : indexMappings.values()) {
104-
addIndexMapping(fieldTypes, indexMapping.getFieldMappings());
107+
mergeObjectAndArrayInsideMap(fieldTypes, indexMapping.getFieldMappings());
105108
}
106109
return fieldTypes;
107110
}
108111

109-
private void addIndexMapping(
110-
Map<String, OpenSearchDataType> resultMap, Map<String, OpenSearchDataType> candidateMap) {
111-
for (Map.Entry<String, OpenSearchDataType> entry : candidateMap.entrySet()) {
112-
if (resultMap.containsKey(entry.getKey())
113-
&& checkWhetherToMerge(entry.getValue(), resultMap.get(entry.getKey()))) {
114-
resultMap.put(
115-
entry.getKey(), mergetTwoObject(resultMap.get(entry.getKey()), entry.getValue()));
116-
} else {
117-
resultMap.put(entry.getKey(), entry.getValue());
118-
}
119-
}
120-
}
112+
private void mergeObjectAndArrayInsideMap(
113+
Map<String, OpenSearchDataType> target, Map<String, OpenSearchDataType> source) {
114+
for (Map.Entry<String, OpenSearchDataType> entry : source.entrySet()) {
115+
String key = entry.getKey();
116+
OpenSearchDataType value = entry.getValue();
121117

122-
private OpenSearchDataType mergetTwoObject(OpenSearchDataType first, OpenSearchDataType second) {
123-
Map<String, OpenSearchDataType> candidateMap = second.getProperties();
124-
for (Map.Entry<String, OpenSearchDataType> entry : candidateMap.entrySet()) {
125-
if (first.getProperties().containsKey(entry.getKey())
126-
&& checkWhetherToMerge(entry.getValue(), first.getProperties().get(entry.getKey()))) {
127-
first
128-
.getProperties()
129-
.put(
130-
entry.getKey(),
131-
mergetTwoObject(first.getProperties().get(entry.getKey()), entry.getValue()));
118+
if (target.containsKey(key) && checkWhetherToMerge(value, target.get(key))) {
119+
OpenSearchDataType merged = target.get(key);
120+
mergeObjectAndArrayInsideMap(merged.getProperties(), value.getProperties());
121+
target.put(key, merged);
132122
} else {
133-
first.getProperties().put(entry.getKey(), entry.getValue());
123+
target.put(key, value);
134124
}
135125
}
136-
return first;
137126
}
138127

139128
private Boolean checkWhetherToMerge(OpenSearchDataType first, OpenSearchDataType second) {

0 commit comments

Comments
 (0)