Skip to content

Commit 9509c1a

Browse files
authored
[Calcite Engine] Support alias type field (#3538)
* [Calcite Engine] Support alias type field Signed-off-by: Heng Qian <qianheng@amazon.com> * Add standalone IT to ensure w/wo push down test cases Signed-off-by: Heng Qian <qianheng@amazon.com> --------- Signed-off-by: Heng Qian <qianheng@amazon.com>
1 parent d0a6b47 commit 9509c1a

File tree

5 files changed

+46
-3
lines changed

5 files changed

+46
-3
lines changed

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

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55

66
package org.opensearch.sql.calcite.standalone;
77

8+
import static org.opensearch.sql.legacy.TestsConstants.TEST_INDEX_ALIAS;
89
import static org.opensearch.sql.legacy.TestsConstants.TEST_INDEX_BANK;
910
import static org.opensearch.sql.util.MatcherUtils.rows;
1011
import static org.opensearch.sql.util.MatcherUtils.schema;
@@ -35,6 +36,7 @@ public void init() throws IOException {
3536
client().performRequest(request3);
3637

3738
loadIndex(Index.BANK);
39+
loadIndex(Index.DATA_TYPE_ALIAS);
3840
}
3941

4042
@Test
@@ -517,4 +519,15 @@ public void testXor() {
517519
TEST_INDEX_BANK));
518520
verifyDataRows(result, rows("Elinor", 36));
519521
}
522+
523+
@Test
524+
public void testAliasDataType() {
525+
JSONObject result =
526+
executeQuery(
527+
String.format(
528+
"source=%s | where alias_col > 1 | fields original_col, alias_col ",
529+
TEST_INDEX_ALIAS));
530+
verifySchema(result, schema("original_col", "integer"), schema("alias_col", "integer"));
531+
verifyDataRows(result, rows(2, 2), rows(3, 3));
532+
}
520533
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -837,7 +837,7 @@ public enum Index {
837837
TestsConstants.TEST_INDEX_ALIAS,
838838
"alias",
839839
getAliasIndexMapping(),
840-
"src/test/resources/work_information.json"),
840+
"src/test/resources/alias.json"),
841841
DUPLICATION_NULLABLE(
842842
TestsConstants.TEST_INDEX_DUPLICATION_NULLABLE,
843843
"duplication_nullable",

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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,9 @@
1111
import static org.opensearch.sql.legacy.TestsConstants.TEST_INDEX_ALIAS;
1212
import static org.opensearch.sql.legacy.TestsConstants.TEST_INDEX_DATATYPE_NONNUMERIC;
1313
import static org.opensearch.sql.legacy.TestsConstants.TEST_INDEX_DATATYPE_NUMERIC;
14+
import static org.opensearch.sql.util.MatcherUtils.rows;
1415
import static org.opensearch.sql.util.MatcherUtils.schema;
16+
import static org.opensearch.sql.util.MatcherUtils.verifyDataRows;
1517
import static org.opensearch.sql.util.MatcherUtils.verifySchema;
1618

1719
import java.io.IOException;
@@ -85,8 +87,9 @@ public void test_alias_data_type() throws IOException {
8587
JSONObject result =
8688
executeQuery(
8789
String.format(
88-
"source=%s | where alias_col > 1 " + "| fields original_col, alias_col ",
90+
"source=%s | where alias_col > 1 | fields original_col, alias_col ",
8991
TEST_INDEX_ALIAS));
9092
verifySchema(result, schema("original_col", "int"), schema("alias_col", "int"));
93+
verifyDataRows(result, rows(2, 2), rows(3, 3));
9194
}
9295
}

opensearch/src/main/java/org/opensearch/sql/opensearch/storage/OpenSearchIndex.java

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,9 @@
1010
import java.util.LinkedHashMap;
1111
import java.util.List;
1212
import java.util.Map;
13+
import java.util.Map.Entry;
1314
import java.util.function.Function;
15+
import java.util.stream.Collectors;
1416
import lombok.Getter;
1517
import lombok.RequiredArgsConstructor;
1618
import org.apache.calcite.linq4j.AbstractEnumerable;
@@ -81,6 +83,9 @@ public class OpenSearchIndex extends OpenSearchTable {
8183
/** The cached ExprType of fields. */
8284
private Map<String, ExprType> cachedFieldTypes = null;
8385

86+
/** The cached mapping of alias type field to its original path. */
87+
private Map<String, String> aliasMapping = null;
88+
8489
/** The cached max result window setting of index. */
8590
private Integer cachedMaxResultWindow = null;
8691

@@ -148,6 +153,22 @@ public Map<String, ExprType> getReservedFieldTypes() {
148153
return METADATAFIELD_TYPE_MAP;
149154
}
150155

156+
public Map<String, String> getAliasMapping() {
157+
if (cachedFieldOpenSearchTypes == null) {
158+
cachedFieldOpenSearchTypes =
159+
new OpenSearchDescribeIndexRequest(client, indexName).getFieldTypes();
160+
}
161+
if (aliasMapping == null) {
162+
aliasMapping =
163+
cachedFieldOpenSearchTypes.entrySet().stream()
164+
.filter(entry -> entry.getValue().getOriginalPath().isPresent())
165+
.collect(
166+
Collectors.toUnmodifiableMap(
167+
Entry::getKey, entry -> entry.getValue().getOriginalPath().get()));
168+
}
169+
return aliasMapping;
170+
}
171+
151172
/**
152173
* Get parsed mapping info.
153174
*

opensearch/src/main/java/org/opensearch/sql/opensearch/storage/scan/CalciteEnumerableIndexScan.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,10 +94,16 @@ public Result implement(EnumerableRelImplementor implementor, Prefer pref) {
9494
public Enumerator<Object> enumerator() {
9595
return new OpenSearchIndexEnumerator(
9696
osIndex.getClient(),
97-
List.copyOf(getRowType().getFieldNames()),
97+
getFieldPath(),
9898
requestBuilder.getMaxResponseSize(),
9999
osIndex.buildRequest(requestBuilder));
100100
}
101101
};
102102
}
103+
104+
private List<String> getFieldPath() {
105+
return getRowType().getFieldNames().stream()
106+
.map(f -> osIndex.getAliasMapping().getOrDefault(f, f))
107+
.toList();
108+
}
103109
}

0 commit comments

Comments
 (0)