25
25
import com .amazon .opendistroforelasticsearch .sql .ast .expression .Let ;
26
26
import com .amazon .opendistroforelasticsearch .sql .ast .expression .Literal ;
27
27
import com .amazon .opendistroforelasticsearch .sql .ast .expression .Map ;
28
+ import com .amazon .opendistroforelasticsearch .sql .ast .expression .UnresolvedArgument ;
28
29
import com .amazon .opendistroforelasticsearch .sql .ast .expression .UnresolvedExpression ;
29
30
import com .amazon .opendistroforelasticsearch .sql .ast .tree .Aggregation ;
30
31
import com .amazon .opendistroforelasticsearch .sql .ast .tree .Dedupe ;
31
32
import com .amazon .opendistroforelasticsearch .sql .ast .tree .Eval ;
32
33
import com .amazon .opendistroforelasticsearch .sql .ast .tree .Filter ;
34
+ import com .amazon .opendistroforelasticsearch .sql .ast .tree .Head ;
33
35
import com .amazon .opendistroforelasticsearch .sql .ast .tree .Project ;
34
36
import com .amazon .opendistroforelasticsearch .sql .ast .tree .RareTopN ;
35
37
import com .amazon .opendistroforelasticsearch .sql .ast .tree .Relation ;
46
48
import com .amazon .opendistroforelasticsearch .sql .expression .NamedExpression ;
47
49
import com .amazon .opendistroforelasticsearch .sql .expression .ReferenceExpression ;
48
50
import com .amazon .opendistroforelasticsearch .sql .expression .aggregation .Aggregator ;
51
+ import com .amazon .opendistroforelasticsearch .sql .expression .aggregation .NamedAggregator ;
49
52
import com .amazon .opendistroforelasticsearch .sql .planner .logical .LogicalAggregation ;
50
53
import com .amazon .opendistroforelasticsearch .sql .planner .logical .LogicalDedupe ;
51
54
import com .amazon .opendistroforelasticsearch .sql .planner .logical .LogicalEval ;
52
55
import com .amazon .opendistroforelasticsearch .sql .planner .logical .LogicalFilter ;
56
+ import com .amazon .opendistroforelasticsearch .sql .planner .logical .LogicalHead ;
53
57
import com .amazon .opendistroforelasticsearch .sql .planner .logical .LogicalPlan ;
54
58
import com .amazon .opendistroforelasticsearch .sql .planner .logical .LogicalProject ;
55
59
import com .amazon .opendistroforelasticsearch .sql .planner .logical .LogicalRareTopN ;
@@ -80,6 +84,8 @@ public class Analyzer extends AbstractNodeVisitor<LogicalPlan, AnalysisContext>
80
84
81
85
private final SelectExpressionAnalyzer selectExpressionAnalyzer ;
82
86
87
+ private final NamedExpressionAnalyzer namedExpressionAnalyzer ;
88
+
83
89
private final StorageEngine storageEngine ;
84
90
85
91
/**
@@ -91,6 +97,7 @@ public Analyzer(
91
97
this .expressionAnalyzer = expressionAnalyzer ;
92
98
this .storageEngine = storageEngine ;
93
99
this .selectExpressionAnalyzer = new SelectExpressionAnalyzer (expressionAnalyzer );
100
+ this .namedExpressionAnalyzer = new NamedExpressionAnalyzer (expressionAnalyzer );
94
101
}
95
102
96
103
public LogicalPlan analyze (UnresolvedPlan unresolved , AnalysisContext context ) {
@@ -153,25 +160,27 @@ public LogicalPlan visitRename(Rename node, AnalysisContext context) {
153
160
@ Override
154
161
public LogicalPlan visitAggregation (Aggregation node , AnalysisContext context ) {
155
162
final LogicalPlan child = node .getChild ().get (0 ).accept (this , context );
156
- ImmutableList .Builder <Aggregator > aggregatorBuilder = new ImmutableList .Builder <>();
163
+ ImmutableList .Builder <NamedAggregator > aggregatorBuilder = new ImmutableList .Builder <>();
157
164
for (UnresolvedExpression expr : node .getAggExprList ()) {
158
- aggregatorBuilder .add ((Aggregator ) expressionAnalyzer .analyze (expr , context ));
165
+ NamedExpression aggExpr = namedExpressionAnalyzer .analyze (expr , context );
166
+ aggregatorBuilder
167
+ .add (new NamedAggregator (aggExpr .getName (), (Aggregator ) aggExpr .getDelegated ()));
159
168
}
160
- ImmutableList <Aggregator > aggregators = aggregatorBuilder .build ();
169
+ ImmutableList <NamedAggregator > aggregators = aggregatorBuilder .build ();
161
170
162
- ImmutableList .Builder <Expression > groupbyBuilder = new ImmutableList .Builder <>();
171
+ ImmutableList .Builder <NamedExpression > groupbyBuilder = new ImmutableList .Builder <>();
163
172
for (UnresolvedExpression expr : node .getGroupExprList ()) {
164
- groupbyBuilder .add (expressionAnalyzer .analyze (expr , context ));
173
+ groupbyBuilder .add (namedExpressionAnalyzer .analyze (expr , context ));
165
174
}
166
- ImmutableList <Expression > groupBys = groupbyBuilder .build ();
175
+ ImmutableList <NamedExpression > groupBys = groupbyBuilder .build ();
167
176
168
177
// new context
169
178
context .push ();
170
179
TypeEnvironment newEnv = context .peek ();
171
180
aggregators .forEach (aggregator -> newEnv .define (new Symbol (Namespace .FIELD_NAME ,
172
- aggregator .toString ()), aggregator .type ()));
181
+ aggregator .getName ()), aggregator .type ()));
173
182
groupBys .forEach (group -> newEnv .define (new Symbol (Namespace .FIELD_NAME ,
174
- group .toString ()), group .type ()));
183
+ group .getName ()), group .type ()));
175
184
return new LogicalAggregation (child , aggregators , groupBys );
176
185
}
177
186
@@ -311,6 +320,23 @@ public LogicalPlan visitDedupe(Dedupe node, AnalysisContext context) {
311
320
consecutive );
312
321
}
313
322
323
+ /**
324
+ * Build {@link LogicalHead}.
325
+ */
326
+ public LogicalPlan visitHead (Head node , AnalysisContext context ) {
327
+ LogicalPlan child = node .getChild ().get (0 ).accept (this , context );
328
+ List <UnresolvedArgument > options = node .getOptions ();
329
+ Boolean keeplast = (Boolean ) getOptionAsLiteral (options , 0 ).getValue ();
330
+ Expression whileExpr = expressionAnalyzer .analyze (options .get (1 ).getValue (), context );
331
+ Integer number = (Integer ) getOptionAsLiteral (options , 2 ).getValue ();
332
+
333
+ return new LogicalHead (child , keeplast , whileExpr , number );
334
+ }
335
+
336
+ private static Literal getOptionAsLiteral (List <UnresolvedArgument > options , int optionIdx ) {
337
+ return (Literal ) options .get (optionIdx ).getValue ();
338
+ }
339
+
314
340
@ Override
315
341
public LogicalPlan visitValues (Values node , AnalysisContext context ) {
316
342
List <List <Literal >> values = node .getValues ();
0 commit comments