Skip to content
This repository was archived by the owner on Aug 2, 2022. It is now read-only.

Commit 3fa954c

Browse files
committed
[1] Trying to fix commit
1 parent b918bad commit 3fa954c

File tree

214 files changed

+8167
-2727
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

214 files changed

+8167
-2727
lines changed

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ The following projects have been merged into this repository as separate folders
1616
* [SQL CLI](https://github.com/opendistro-for-elasticsearch/sql/tree/master/sql-cli)
1717
* [SQL JDBC](https://github.com/opendistro-for-elasticsearch/sql/tree/master/sql-jdbc)
1818
* [SQL ODBC](https://github.com/opendistro-for-elasticsearch/sql/tree/master/sql-odbc)
19-
* [SQL Workbench](https://github.com/opendistro-for-elasticsearch/sql/tree/master/sql-workbench)
19+
* [SQL Workbench](https://github.com/opendistro-for-elasticsearch/sql/tree/master/workbench)
2020

2121

2222
## Documentation

core/src/main/java/com/amazon/opendistroforelasticsearch/sql/analysis/Analyzer.java

+34-8
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,13 @@
2525
import com.amazon.opendistroforelasticsearch.sql.ast.expression.Let;
2626
import com.amazon.opendistroforelasticsearch.sql.ast.expression.Literal;
2727
import com.amazon.opendistroforelasticsearch.sql.ast.expression.Map;
28+
import com.amazon.opendistroforelasticsearch.sql.ast.expression.UnresolvedArgument;
2829
import com.amazon.opendistroforelasticsearch.sql.ast.expression.UnresolvedExpression;
2930
import com.amazon.opendistroforelasticsearch.sql.ast.tree.Aggregation;
3031
import com.amazon.opendistroforelasticsearch.sql.ast.tree.Dedupe;
3132
import com.amazon.opendistroforelasticsearch.sql.ast.tree.Eval;
3233
import com.amazon.opendistroforelasticsearch.sql.ast.tree.Filter;
34+
import com.amazon.opendistroforelasticsearch.sql.ast.tree.Head;
3335
import com.amazon.opendistroforelasticsearch.sql.ast.tree.Project;
3436
import com.amazon.opendistroforelasticsearch.sql.ast.tree.RareTopN;
3537
import com.amazon.opendistroforelasticsearch.sql.ast.tree.Relation;
@@ -46,10 +48,12 @@
4648
import com.amazon.opendistroforelasticsearch.sql.expression.NamedExpression;
4749
import com.amazon.opendistroforelasticsearch.sql.expression.ReferenceExpression;
4850
import com.amazon.opendistroforelasticsearch.sql.expression.aggregation.Aggregator;
51+
import com.amazon.opendistroforelasticsearch.sql.expression.aggregation.NamedAggregator;
4952
import com.amazon.opendistroforelasticsearch.sql.planner.logical.LogicalAggregation;
5053
import com.amazon.opendistroforelasticsearch.sql.planner.logical.LogicalDedupe;
5154
import com.amazon.opendistroforelasticsearch.sql.planner.logical.LogicalEval;
5255
import com.amazon.opendistroforelasticsearch.sql.planner.logical.LogicalFilter;
56+
import com.amazon.opendistroforelasticsearch.sql.planner.logical.LogicalHead;
5357
import com.amazon.opendistroforelasticsearch.sql.planner.logical.LogicalPlan;
5458
import com.amazon.opendistroforelasticsearch.sql.planner.logical.LogicalProject;
5559
import com.amazon.opendistroforelasticsearch.sql.planner.logical.LogicalRareTopN;
@@ -80,6 +84,8 @@ public class Analyzer extends AbstractNodeVisitor<LogicalPlan, AnalysisContext>
8084

8185
private final SelectExpressionAnalyzer selectExpressionAnalyzer;
8286

87+
private final NamedExpressionAnalyzer namedExpressionAnalyzer;
88+
8389
private final StorageEngine storageEngine;
8490

8591
/**
@@ -91,6 +97,7 @@ public Analyzer(
9197
this.expressionAnalyzer = expressionAnalyzer;
9298
this.storageEngine = storageEngine;
9399
this.selectExpressionAnalyzer = new SelectExpressionAnalyzer(expressionAnalyzer);
100+
this.namedExpressionAnalyzer = new NamedExpressionAnalyzer(expressionAnalyzer);
94101
}
95102

96103
public LogicalPlan analyze(UnresolvedPlan unresolved, AnalysisContext context) {
@@ -153,25 +160,27 @@ public LogicalPlan visitRename(Rename node, AnalysisContext context) {
153160
@Override
154161
public LogicalPlan visitAggregation(Aggregation node, AnalysisContext context) {
155162
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<>();
157164
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()));
159168
}
160-
ImmutableList<Aggregator> aggregators = aggregatorBuilder.build();
169+
ImmutableList<NamedAggregator> aggregators = aggregatorBuilder.build();
161170

162-
ImmutableList.Builder<Expression> groupbyBuilder = new ImmutableList.Builder<>();
171+
ImmutableList.Builder<NamedExpression> groupbyBuilder = new ImmutableList.Builder<>();
163172
for (UnresolvedExpression expr : node.getGroupExprList()) {
164-
groupbyBuilder.add(expressionAnalyzer.analyze(expr, context));
173+
groupbyBuilder.add(namedExpressionAnalyzer.analyze(expr, context));
165174
}
166-
ImmutableList<Expression> groupBys = groupbyBuilder.build();
175+
ImmutableList<NamedExpression> groupBys = groupbyBuilder.build();
167176

168177
// new context
169178
context.push();
170179
TypeEnvironment newEnv = context.peek();
171180
aggregators.forEach(aggregator -> newEnv.define(new Symbol(Namespace.FIELD_NAME,
172-
aggregator.toString()), aggregator.type()));
181+
aggregator.getName()), aggregator.type()));
173182
groupBys.forEach(group -> newEnv.define(new Symbol(Namespace.FIELD_NAME,
174-
group.toString()), group.type()));
183+
group.getName()), group.type()));
175184
return new LogicalAggregation(child, aggregators, groupBys);
176185
}
177186

@@ -311,6 +320,23 @@ public LogicalPlan visitDedupe(Dedupe node, AnalysisContext context) {
311320
consecutive);
312321
}
313322

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+
314340
@Override
315341
public LogicalPlan visitValues(Values node, AnalysisContext context) {
316342
List<List<Literal>> values = node.getValues();

core/src/main/java/com/amazon/opendistroforelasticsearch/sql/analysis/ExpressionAnalyzer.java

-1
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@
2525
import com.amazon.opendistroforelasticsearch.sql.ast.expression.Field;
2626
import com.amazon.opendistroforelasticsearch.sql.ast.expression.Function;
2727
import com.amazon.opendistroforelasticsearch.sql.ast.expression.Interval;
28-
import com.amazon.opendistroforelasticsearch.sql.ast.expression.IntervalUnit;
2928
import com.amazon.opendistroforelasticsearch.sql.ast.expression.Literal;
3029
import com.amazon.opendistroforelasticsearch.sql.ast.expression.Not;
3130
import com.amazon.opendistroforelasticsearch.sql.ast.expression.Or;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
/*
2+
*
3+
* Copyright 2020 Amazon.com, Inc. or its affiliates. All Rights Reserved.
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License").
6+
* You may not use this file except in compliance with the License.
7+
* A copy of the License is located at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* or in the "license" file accompanying this file. This file is distributed
12+
* on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
13+
* express or implied. See the License for the specific language governing
14+
* permissions and limitations under the License.
15+
*
16+
*/
17+
18+
package com.amazon.opendistroforelasticsearch.sql.analysis;
19+
20+
import com.amazon.opendistroforelasticsearch.sql.ast.AbstractNodeVisitor;
21+
import com.amazon.opendistroforelasticsearch.sql.ast.expression.Alias;
22+
import com.amazon.opendistroforelasticsearch.sql.ast.expression.QualifiedName;
23+
import com.amazon.opendistroforelasticsearch.sql.ast.expression.UnresolvedExpression;
24+
import com.amazon.opendistroforelasticsearch.sql.expression.DSL;
25+
import com.amazon.opendistroforelasticsearch.sql.expression.NamedExpression;
26+
import lombok.RequiredArgsConstructor;
27+
28+
/**
29+
* Analyze the Alias node in the {@link AnalysisContext} to construct the list of
30+
* {@link NamedExpression}.
31+
*/
32+
@RequiredArgsConstructor
33+
public class NamedExpressionAnalyzer extends
34+
AbstractNodeVisitor<NamedExpression, AnalysisContext> {
35+
private final ExpressionAnalyzer expressionAnalyzer;
36+
37+
/**
38+
* Analyze Select fields.
39+
*/
40+
public NamedExpression analyze(UnresolvedExpression expression,
41+
AnalysisContext analysisContext) {
42+
return expression.accept(this, analysisContext);
43+
}
44+
45+
@Override
46+
public NamedExpression visitAlias(Alias node, AnalysisContext context) {
47+
return DSL.named(
48+
unqualifiedNameIfFieldOnly(node, context),
49+
node.getDelegated().accept(expressionAnalyzer, context),
50+
node.getAlias());
51+
}
52+
53+
private String unqualifiedNameIfFieldOnly(Alias node, AnalysisContext context) {
54+
UnresolvedExpression selectItem = node.getDelegated();
55+
if (selectItem instanceof QualifiedName) {
56+
QualifierAnalyzer qualifierAnalyzer = new QualifierAnalyzer(context);
57+
return qualifierAnalyzer.unqualified((QualifiedName) selectItem);
58+
}
59+
return node.getName();
60+
}
61+
}

core/src/main/java/com/amazon/opendistroforelasticsearch/sql/analysis/SelectExpressionAnalyzer.java

+29-8
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,11 @@
2020
import com.amazon.opendistroforelasticsearch.sql.analysis.symbol.Namespace;
2121
import com.amazon.opendistroforelasticsearch.sql.analysis.symbol.Symbol;
2222
import com.amazon.opendistroforelasticsearch.sql.ast.AbstractNodeVisitor;
23+
import com.amazon.opendistroforelasticsearch.sql.ast.expression.AggregateFunction;
2324
import com.amazon.opendistroforelasticsearch.sql.ast.expression.Alias;
2425
import com.amazon.opendistroforelasticsearch.sql.ast.expression.AllFields;
2526
import com.amazon.opendistroforelasticsearch.sql.ast.expression.Field;
27+
import com.amazon.opendistroforelasticsearch.sql.ast.expression.Function;
2628
import com.amazon.opendistroforelasticsearch.sql.ast.expression.QualifiedName;
2729
import com.amazon.opendistroforelasticsearch.sql.ast.expression.UnresolvedExpression;
2830
import com.amazon.opendistroforelasticsearch.sql.data.type.ExprType;
@@ -67,23 +69,42 @@ public List<NamedExpression> visitField(Field node, AnalysisContext context) {
6769

6870
@Override
6971
public List<NamedExpression> visitAlias(Alias node, AnalysisContext context) {
70-
Expression expr = referenceIfSymbolDefined(node.getDelegated(), context);
72+
Expression expr = referenceIfSymbolDefined(node, context);
7173
return Collections.singletonList(DSL.named(
7274
unqualifiedNameIfFieldOnly(node, context),
7375
expr,
7476
node.getAlias()));
7577
}
7678

77-
private Expression referenceIfSymbolDefined(UnresolvedExpression expr,
79+
/**
80+
* The Alias could be
81+
* 1. SELECT name, AVG(age) FROM s BY name ->
82+
* Project(Alias("name", expr), Alias("AVG(age)", aggExpr))
83+
* Agg(Alias("AVG(age)", aggExpr))
84+
* 2. SELECT length(name), AVG(age) FROM s BY length(name)
85+
* Project(Alias("name", expr), Alias("AVG(age)", aggExpr))
86+
* Agg(Alias("AVG(age)", aggExpr))
87+
* 3. SELECT length(name) as l, AVG(age) FROM s BY l
88+
* Project(Alias("name", expr, l), Alias("AVG(age)", aggExpr))
89+
* Agg(Alias("AVG(age)", aggExpr), Alias("length(name)", groupExpr))
90+
*/
91+
private Expression referenceIfSymbolDefined(Alias expr,
7892
AnalysisContext context) {
93+
UnresolvedExpression delegatedExpr = expr.getDelegated();
7994
try {
80-
// Since resolved aggregator.toString() is used as symbol name, unresolved expression
81-
// needs to be analyzed too to get toString() name for consistency
82-
String symbolName = expressionAnalyzer.analyze(expr, context).toString();
83-
ExprType type = context.peek().resolve(new Symbol(Namespace.FIELD_NAME, symbolName));
84-
return DSL.ref(symbolName, type);
95+
if ((delegatedExpr instanceof AggregateFunction)
96+
|| (delegatedExpr instanceof Function)) {
97+
ExprType type = context.peek().resolve(new Symbol(Namespace.FIELD_NAME, expr.getName()));
98+
return DSL.ref(expr.getName(), type);
99+
} else {
100+
// Since resolved aggregator.toString() is used as symbol name, unresolved expression
101+
// needs to be analyzed too to get toString() name for consistency
102+
String symbolName = expressionAnalyzer.analyze(delegatedExpr, context).toString();
103+
ExprType type = context.peek().resolve(new Symbol(Namespace.FIELD_NAME, symbolName));
104+
return DSL.ref(symbolName, type);
105+
}
85106
} catch (SemanticCheckException e) {
86-
return expr.accept(expressionAnalyzer, context);
107+
return delegatedExpr.accept(expressionAnalyzer, context);
87108
}
88109
}
89110

core/src/main/java/com/amazon/opendistroforelasticsearch/sql/ast/AbstractNodeVisitor.java

+10
Original file line numberDiff line numberDiff line change
@@ -33,12 +33,14 @@
3333
import com.amazon.opendistroforelasticsearch.sql.ast.expression.Not;
3434
import com.amazon.opendistroforelasticsearch.sql.ast.expression.Or;
3535
import com.amazon.opendistroforelasticsearch.sql.ast.expression.QualifiedName;
36+
import com.amazon.opendistroforelasticsearch.sql.ast.expression.UnresolvedArgument;
3637
import com.amazon.opendistroforelasticsearch.sql.ast.expression.UnresolvedAttribute;
3738
import com.amazon.opendistroforelasticsearch.sql.ast.expression.Xor;
3839
import com.amazon.opendistroforelasticsearch.sql.ast.tree.Aggregation;
3940
import com.amazon.opendistroforelasticsearch.sql.ast.tree.Dedupe;
4041
import com.amazon.opendistroforelasticsearch.sql.ast.tree.Eval;
4142
import com.amazon.opendistroforelasticsearch.sql.ast.tree.Filter;
43+
import com.amazon.opendistroforelasticsearch.sql.ast.tree.Head;
4244
import com.amazon.opendistroforelasticsearch.sql.ast.tree.Project;
4345
import com.amazon.opendistroforelasticsearch.sql.ast.tree.RareTopN;
4446
import com.amazon.opendistroforelasticsearch.sql.ast.tree.Relation;
@@ -179,6 +181,10 @@ public T visitDedupe(Dedupe node, C context) {
179181
return visitChildren(node, context);
180182
}
181183

184+
public T visitHead(Head node, C context) {
185+
return visitChildren(node, context);
186+
}
187+
182188
public T visitRareTopN(RareTopN node, C context) {
183189
return visitChildren(node, context);
184190
}
@@ -198,4 +204,8 @@ public T visitAllFields(AllFields node, C context) {
198204
public T visitInterval(Interval node, C context) {
199205
return visitChildren(node, context);
200206
}
207+
208+
public T visitUnresolvedArgument(UnresolvedArgument node, C context) {
209+
return visitChildren(node, context);
210+
}
201211
}

core/src/main/java/com/amazon/opendistroforelasticsearch/sql/ast/dsl/AstDSL.java

+24
Original file line numberDiff line numberDiff line change
@@ -33,13 +33,15 @@
3333
import com.amazon.opendistroforelasticsearch.sql.ast.expression.Not;
3434
import com.amazon.opendistroforelasticsearch.sql.ast.expression.Or;
3535
import com.amazon.opendistroforelasticsearch.sql.ast.expression.QualifiedName;
36+
import com.amazon.opendistroforelasticsearch.sql.ast.expression.UnresolvedArgument;
3637
import com.amazon.opendistroforelasticsearch.sql.ast.expression.UnresolvedAttribute;
3738
import com.amazon.opendistroforelasticsearch.sql.ast.expression.UnresolvedExpression;
3839
import com.amazon.opendistroforelasticsearch.sql.ast.expression.Xor;
3940
import com.amazon.opendistroforelasticsearch.sql.ast.tree.Aggregation;
4041
import com.amazon.opendistroforelasticsearch.sql.ast.tree.Dedupe;
4142
import com.amazon.opendistroforelasticsearch.sql.ast.tree.Eval;
4243
import com.amazon.opendistroforelasticsearch.sql.ast.tree.Filter;
44+
import com.amazon.opendistroforelasticsearch.sql.ast.tree.Head;
4345
import com.amazon.opendistroforelasticsearch.sql.ast.tree.Project;
4446
import com.amazon.opendistroforelasticsearch.sql.ast.tree.RareTopN;
4547
import com.amazon.opendistroforelasticsearch.sql.ast.tree.RareTopN.CommandType;
@@ -214,6 +216,10 @@ public static Argument argument(String argName, Literal argValue) {
214216
return new Argument(argName, argValue);
215217
}
216218

219+
public static UnresolvedArgument unresolvedArg(String argName, UnresolvedExpression argValue) {
220+
return new UnresolvedArgument(argName, argValue);
221+
}
222+
217223
public static UnresolvedExpression field(UnresolvedExpression field) {
218224
return new Field((QualifiedName) field);
219225
}
@@ -254,6 +260,10 @@ public static List<Argument> exprList(Argument... exprList) {
254260
return Arrays.asList(exprList);
255261
}
256262

263+
public static List<UnresolvedArgument> unresolvedArgList(UnresolvedArgument... exprList) {
264+
return Arrays.asList(exprList);
265+
}
266+
257267
public static List<Argument> defaultFieldsArgs() {
258268
return exprList(argument("exclude", booleanLiteral(false)));
259269
}
@@ -299,6 +309,20 @@ public static Dedupe dedupe(UnresolvedPlan input, List<Argument> options, Field.
299309
return new Dedupe(input, options, Arrays.asList(fields));
300310
}
301311

312+
public static Head head(UnresolvedPlan input, List<UnresolvedArgument> options) {
313+
return new Head(input, options);
314+
}
315+
316+
/**
317+
* Default Head Command Args.
318+
*/
319+
public static List<UnresolvedArgument> defaultHeadArgs() {
320+
return unresolvedArgList(
321+
unresolvedArg("keeplast", booleanLiteral(true)),
322+
unresolvedArg("whileExpr", booleanLiteral(true)),
323+
unresolvedArg("number", intLiteral(10)));
324+
}
325+
302326
public static List<Argument> defaultTopArgs() {
303327
return exprList(argument("noOfResults", intLiteral(10)));
304328
}

core/src/main/java/com/amazon/opendistroforelasticsearch/sql/ast/expression/Function.java

+10-2
Original file line numberDiff line numberDiff line change
@@ -18,17 +18,17 @@
1818
import com.amazon.opendistroforelasticsearch.sql.ast.AbstractNodeVisitor;
1919
import java.util.Collections;
2020
import java.util.List;
21+
import java.util.stream.Collectors;
2122
import lombok.EqualsAndHashCode;
2223
import lombok.Getter;
2324
import lombok.RequiredArgsConstructor;
24-
import lombok.ToString;
25+
2526

2627
/**
2728
* Expression node of scalar function.
2829
* Params include function name (@funcName) and function arguments (@funcArgs)
2930
*/
3031
@Getter
31-
@ToString
3232
@EqualsAndHashCode(callSuper = false)
3333
@RequiredArgsConstructor
3434
public class Function extends UnresolvedExpression {
@@ -44,4 +44,12 @@ public List<UnresolvedExpression> getChild() {
4444
public <R, C> R accept(AbstractNodeVisitor<R, C> nodeVisitor, C context) {
4545
return nodeVisitor.visitFunction(this, context);
4646
}
47+
48+
@Override
49+
public String toString() {
50+
return String.format("%s(%s)", funcName,
51+
funcArgs.stream()
52+
.map(Object::toString)
53+
.collect(Collectors.joining(", ")));
54+
}
4755
}

core/src/main/java/com/amazon/opendistroforelasticsearch/sql/ast/expression/Literal.java

+5-1
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@
2929
* literal data type (@type) which can be selected from {@link DataType}.
3030
*/
3131
@Getter
32-
@ToString
3332
@EqualsAndHashCode(callSuper = false)
3433
@RequiredArgsConstructor
3534
public class Literal extends UnresolvedExpression {
@@ -46,4 +45,9 @@ public List<UnresolvedExpression> getChild() {
4645
public <R, C> R accept(AbstractNodeVisitor<R, C> nodeVisitor, C context) {
4746
return nodeVisitor.visitLiteral(this, context);
4847
}
48+
49+
@Override
50+
public String toString() {
51+
return value.toString();
52+
}
4953
}

0 commit comments

Comments
 (0)