Skip to content

Commit 9cb2ccb

Browse files
committed
Support expand with alias
Signed-off-by: Yuanchun Shen <yuanchu@amazon.com>
1 parent 7003abb commit 9cb2ccb

6 files changed

Lines changed: 22 additions & 13 deletions

File tree

core/src/main/java/org/opensearch/sql/ast/dsl/AstDSL.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -97,8 +97,8 @@ public static Eval eval(UnresolvedPlan input, Let... projectList) {
9797
return new Eval(Arrays.asList(projectList)).attach(input);
9898
}
9999

100-
public Expand expand(UnresolvedPlan input, Field field) {
101-
return new Expand(field).attach(input);
100+
public Expand expand(UnresolvedPlan input, Field field, String alias) {
101+
return new Expand(field, alias).attach(input);
102102
}
103103

104104
public static UnresolvedPlan projectWithArg(

core/src/main/java/org/opensearch/sql/ast/tree/Expand.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
import com.google.common.collect.ImmutableList;
99
import java.util.List;
10+
import javax.annotation.Nullable;
1011
import lombok.Getter;
1112
import lombok.RequiredArgsConstructor;
1213
import lombok.ToString;
@@ -21,6 +22,7 @@ public class Expand extends UnresolvedPlan {
2122

2223
private UnresolvedPlan child;
2324
@Getter private final Field field;
25+
@Getter @Nullable private final String alias;
2426

2527
@Override
2628
public Expand attach(UnresolvedPlan child) {

core/src/main/java/org/opensearch/sql/calcite/CalciteRelNodeVisitor.java

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -842,9 +842,10 @@ public RelNode visitExpand(Expand expand, CalcitePlanContext context) {
842842

843843
RelBuilder relBuilder = context.relBuilder;
844844

845-
// 2. Get the field to expand
845+
// 2. Get the field to expand and an optional alias.
846846
Field arrayField = expand.getField();
847847
RexInputRef arrayFieldRex = (RexInputRef) rexVisitor.analyze(arrayField, context);
848+
String alias = expand.getAlias();
848849

849850
// 3. Capture the outer row in a CorrelationId
850851
Holder<RexCorrelVariable> correlVariable = Holder.empty();
@@ -860,7 +861,7 @@ public RelNode visitExpand(Expand expand, CalcitePlanContext context) {
860861

861862
// 5. Filter rows where the array field is the same as the left side
862863
// TODO: This is not a standard way to use correlate and uncollect together.
863-
// A filter should not be necessary. Correct it in the future.
864+
// A filter should not be necessary. Correct it in the future.
864865
RexNode filterCondition = relBuilder.equals(correlArrayField, arrayFieldRex);
865866
relBuilder.filter(filterCondition);
866867

@@ -876,9 +877,17 @@ public RelNode visitExpand(Expand expand, CalcitePlanContext context) {
876877
// be used by the right side to correlate with the left side.
877878
relBuilder.correlate(JoinRelType.INNER, correlVariable.get().id, List.of(arrayFieldRex));
878879

879-
// 8. Remove the original array field from the output. No alias is currently supported in the
880-
// expand command, so it can be safely deleted. Its name is re-used for the expanded element.
880+
// 8. Remove the original array field from the output.
881+
// TODO: RFC: should we keep the original array field when alias is present?
881882
relBuilder.projectExcept(arrayFieldRex);
883+
if (alias != null) {
884+
// Sub-nested fields cannot be removed after renaming the nested field.
885+
tryToRemoveNestedFields(context);
886+
RexInputRef expandedField = relBuilder.field(arrayField.getField().toString());
887+
List<String> names = new ArrayList<>(relBuilder.peek().getRowType().getFieldNames());
888+
names.set(expandedField.getIndex(), alias);
889+
relBuilder.rename(names);
890+
}
882891
return relBuilder.peek();
883892
}
884893
}

integ-test/src/test/java/org/opensearch/sql/calcite/remote/CalciteExpandCommandIT.java

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -53,18 +53,15 @@ public void testExpandOnArray() throws Exception {
5353
verifyNumOfRows(response, 5);
5454
}
5555

56-
// TODO: confirm if expand with alias will be supported
57-
@Ignore
5856
@Test
5957
public void testExpandWithAlias() throws Exception {
6058
JSONObject response =
6159
executeQuery(String.format("source=%s | expand address as addr", TEST_INDEX_NESTED_SIMPLE));
6260
verifySchema(
6361
response,
6462
schema("name", "string"),
65-
schema("age", "integer"),
66-
schema("id", "integer"),
67-
schema("address", "array"),
63+
schema("age", "bigint"),
64+
schema("id", "bigint"),
6865
schema("addr", "struct"));
6966
verifyNumOfRows(response, 11);
7067
}

ppl/src/main/antlr/OpenSearchPPLParser.g4

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -240,7 +240,7 @@ trendlineType
240240
;
241241

242242
expandCommand
243-
: EXPAND fieldExpression
243+
: EXPAND fieldExpression (AS alias = qualifiedName)?
244244
;
245245

246246
kmeansCommand

ppl/src/main/java/org/opensearch/sql/ppl/parser/AstBuilder.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -419,7 +419,8 @@ public UnresolvedPlan visitTopCommand(OpenSearchPPLParser.TopCommandContext ctx)
419419
@Override
420420
public UnresolvedPlan visitExpandCommand(OpenSearchPPLParser.ExpandCommandContext ctx) {
421421
Field fieldExpression = (Field) internalVisitExpression(ctx.fieldExpression());
422-
return new Expand(fieldExpression);
422+
String alias = ctx.alias != null ? internalVisitExpression(ctx.alias).toString() : null;
423+
return new Expand(fieldExpression, alias);
423424
}
424425

425426
@Override

0 commit comments

Comments
 (0)