Skip to content

Commit fcb3558

Browse files
committed
Adding explain and test for Nested.
Signed-off-by: forestmvey <forestv@bitquilltech.com>
1 parent 77fbd0e commit fcb3558

2 files changed

Lines changed: 51 additions & 27 deletions

File tree

core/src/main/java/org/opensearch/sql/executor/Explain.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
import org.opensearch.sql.planner.physical.EvalOperator;
2424
import org.opensearch.sql.planner.physical.FilterOperator;
2525
import org.opensearch.sql.planner.physical.LimitOperator;
26+
import org.opensearch.sql.planner.physical.NestedOperator;
2627
import org.opensearch.sql.planner.physical.PhysicalPlan;
2728
import org.opensearch.sql.planner.physical.PhysicalPlanNodeVisitor;
2829
import org.opensearch.sql.planner.physical.ProjectOperator;
@@ -142,6 +143,12 @@ public ExplainResponseNode visitLimit(LimitOperator node, Object context) {
142143
"limit", node.getLimit(), "offset", node.getOffset())));
143144
}
144145

146+
@Override
147+
public ExplainResponseNode visitNested(NestedOperator node, Object context) {
148+
return explain(node, context, explanNode -> explanNode.setDescription(ImmutableMap.of(
149+
"nested", node.getFields())));
150+
}
151+
145152
protected ExplainResponseNode explain(PhysicalPlan node, Object context,
146153
Consumer<ExplainResponseNode> doExplain) {
147154
ExplainResponseNode explainNode = new ExplainResponseNode(getOperatorName(node));

core/src/test/java/org/opensearch/sql/executor/ExplainTest.java

Lines changed: 44 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import static org.opensearch.sql.planner.physical.PhysicalPlanDSL.eval;
2323
import static org.opensearch.sql.planner.physical.PhysicalPlanDSL.filter;
2424
import static org.opensearch.sql.planner.physical.PhysicalPlanDSL.limit;
25+
import static org.opensearch.sql.planner.physical.PhysicalPlanDSL.nested;
2526
import static org.opensearch.sql.planner.physical.PhysicalPlanDSL.project;
2627
import static org.opensearch.sql.planner.physical.PhysicalPlanDSL.rareTopN;
2728
import static org.opensearch.sql.planner.physical.PhysicalPlanDSL.remove;
@@ -30,10 +31,9 @@
3031
import static org.opensearch.sql.planner.physical.PhysicalPlanDSL.values;
3132
import static org.opensearch.sql.planner.physical.PhysicalPlanDSL.window;
3233

33-
import com.google.common.collect.ImmutableList;
34-
import com.google.common.collect.ImmutableMap;
3534
import java.util.List;
3635
import java.util.Map;
36+
import java.util.Set;
3737
import org.apache.commons.lang3.tuple.ImmutablePair;
3838
import org.apache.commons.lang3.tuple.Pair;
3939
import org.junit.jupiter.api.DisplayNameGeneration;
@@ -83,28 +83,28 @@ void can_explain_project_filter_table_scan() {
8383
new ExplainResponse(
8484
new ExplainResponseNode(
8585
"ProjectOperator",
86-
ImmutableMap.of("fields", "[name, age]"),
86+
Map.of("fields", "[name, age]"),
8787
singletonList(new ExplainResponseNode(
8888
"FilterOperator",
89-
ImmutableMap.of("conditions", "and(=(balance, 10000), >(age, 30))"),
89+
Map.of("conditions", "and(=(balance, 10000), >(age, 30))"),
9090
singletonList(tableScan.explainNode()))))),
9191
explain.apply(plan));
9292
}
9393

9494
@Test
9595
void can_explain_aggregations() {
96-
List<Expression> aggExprs = ImmutableList.of(ref("balance", DOUBLE));
97-
List<NamedAggregator> aggList = ImmutableList.of(
96+
List<Expression> aggExprs = List.of(ref("balance", DOUBLE));
97+
List<NamedAggregator> aggList = List.of(
9898
named("avg(balance)", DSL.avg(aggExprs.toArray(new Expression[0]))));
99-
List<NamedExpression> groupByList = ImmutableList.of(
99+
List<NamedExpression> groupByList = List.of(
100100
named("state", ref("state", STRING)));
101101

102102
PhysicalPlan plan = agg(new FakeTableScan(), aggList, groupByList);
103103
assertEquals(
104104
new ExplainResponse(
105105
new ExplainResponseNode(
106106
"AggregationOperator",
107-
ImmutableMap.of(
107+
Map.of(
108108
"aggregators", "[avg(balance)]",
109109
"groupBy", "[state]"),
110110
singletonList(tableScan.explainNode()))),
@@ -120,7 +120,7 @@ void can_explain_rare_top_n() {
120120
new ExplainResponse(
121121
new ExplainResponseNode(
122122
"RareTopNOperator",
123-
ImmutableMap.of(
123+
Map.of(
124124
"commandType", TOP,
125125
"noOfResults", 10,
126126
"fields", "[state]",
@@ -131,8 +131,8 @@ void can_explain_rare_top_n() {
131131

132132
@Test
133133
void can_explain_window() {
134-
List<Expression> partitionByList = ImmutableList.of(DSL.ref("state", STRING));
135-
List<Pair<Sort.SortOption, Expression>> sortList = ImmutableList.of(
134+
List<Expression> partitionByList = List.of(DSL.ref("state", STRING));
135+
List<Pair<Sort.SortOption, Expression>> sortList = List.of(
136136
ImmutablePair.of(DEFAULT_ASC, ref("age", INTEGER)));
137137

138138
PhysicalPlan plan = window(tableScan, named(DSL.rank()),
@@ -142,12 +142,12 @@ void can_explain_window() {
142142
new ExplainResponse(
143143
new ExplainResponseNode(
144144
"WindowOperator",
145-
ImmutableMap.of(
145+
Map.of(
146146
"function", "rank()",
147-
"definition", ImmutableMap.of(
147+
"definition", Map.of(
148148
"partitionBy", "[state]",
149-
"sortList", ImmutableMap.of(
150-
"age", ImmutableMap.of(
149+
"sortList", Map.of(
150+
"age", Map.of(
151151
"sortOrder", "ASC",
152152
"nullOrder", "NULL_FIRST")))),
153153
singletonList(tableScan.explainNode()))),
@@ -157,14 +157,14 @@ void can_explain_window() {
157157
@Test
158158
void can_explain_other_operators() {
159159
ReferenceExpression[] removeList = {ref("state", STRING)};
160-
Map<ReferenceExpression, ReferenceExpression> renameMapping = ImmutableMap.of(
160+
Map<ReferenceExpression, ReferenceExpression> renameMapping = Map.of(
161161
ref("state", STRING), ref("s", STRING));
162162
Pair<ReferenceExpression, Expression> evalExprs = ImmutablePair.of(
163163
ref("age", INTEGER), DSL.add(ref("age", INTEGER), literal(2)));
164164
Expression[] dedupeList = {ref("age", INTEGER)};
165165
Pair<Sort.SortOption, Expression> sortList = ImmutablePair.of(
166166
DEFAULT_ASC, ref("age", INTEGER));
167-
List<LiteralExpression> values = ImmutableList.of(literal("WA"), literal(30));
167+
List<LiteralExpression> values = List.of(literal("WA"), literal(30));
168168

169169
PhysicalPlan plan =
170170
remove(
@@ -183,30 +183,30 @@ void can_explain_other_operators() {
183183
new ExplainResponse(
184184
new ExplainResponseNode(
185185
"RemoveOperator",
186-
ImmutableMap.of("removeList", "[state]"),
186+
Map.of("removeList", "[state]"),
187187
singletonList(new ExplainResponseNode(
188188
"RenameOperator",
189-
ImmutableMap.of("mapping", ImmutableMap.of("state", "s")),
189+
Map.of("mapping", Map.of("state", "s")),
190190
singletonList(new ExplainResponseNode(
191191
"EvalOperator",
192-
ImmutableMap.of("expressions", ImmutableMap.of("age", "+(age, 2)")),
192+
Map.of("expressions", Map.of("age", "+(age, 2)")),
193193
singletonList(new ExplainResponseNode(
194194
"DedupeOperator",
195-
ImmutableMap.of(
195+
Map.of(
196196
"dedupeList", "[age]",
197197
"allowedDuplication", 1,
198198
"keepEmpty", false,
199199
"consecutive", false),
200200
singletonList(new ExplainResponseNode(
201201
"SortOperator",
202-
ImmutableMap.of(
203-
"sortList", ImmutableMap.of(
204-
"age", ImmutableMap.of(
202+
Map.of(
203+
"sortList", Map.of(
204+
"age", Map.of(
205205
"sortOrder", "ASC",
206206
"nullOrder", "NULL_FIRST"))),
207207
singletonList(new ExplainResponseNode(
208208
"ValuesOperator",
209-
ImmutableMap.of("values", ImmutableList.of(values)),
209+
Map.of("values", List.of(values)),
210210
emptyList())))))))))))
211211
),
212212
explain.apply(plan)
@@ -220,7 +220,24 @@ void can_explain_limit() {
220220
new ExplainResponse(
221221
new ExplainResponseNode(
222222
"LimitOperator",
223-
ImmutableMap.of("limit", 10, "offset", 5),
223+
Map.of("limit", 10, "offset", 5),
224+
singletonList(tableScan.explainNode()))),
225+
explain.apply(plan)
226+
);
227+
}
228+
229+
@Test
230+
void can_explain_nested() {
231+
Set<String> nestedOperatorArgs = Set.of("message.info", "message");
232+
Map<String, List<String>> groupedFieldsByPath =
233+
Map.of("message", List.of("message.info"));
234+
PhysicalPlan plan = nested(tableScan, nestedOperatorArgs, groupedFieldsByPath);
235+
236+
assertEquals(
237+
new ExplainResponse(
238+
new ExplainResponseNode(
239+
"NestedOperator",
240+
Map.of("nested", Set.of("message.info", "message")),
224241
singletonList(tableScan.explainNode()))),
225242
explain.apply(plan)
226243
);
@@ -246,7 +263,7 @@ public String toString() {
246263
public ExplainResponseNode explainNode() {
247264
return new ExplainResponseNode(
248265
"FakeTableScan",
249-
ImmutableMap.of("request", "Fake DSL request"),
266+
Map.of("request", "Fake DSL request"),
250267
emptyList());
251268
}
252269

0 commit comments

Comments
 (0)