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

Commit

Permalink
[1] Trying to fix commit
Browse files Browse the repository at this point in the history
  • Loading branch information
lyndonbauto committed Sep 23, 2020
1 parent b918bad commit 3fa954c
Show file tree
Hide file tree
Showing 214 changed files with 8,167 additions and 2,727 deletions.
2 changes: 1 addition & 1 deletion README.md
Expand Up @@ -16,7 +16,7 @@ The following projects have been merged into this repository as separate folders
* [SQL CLI](https://github.com/opendistro-for-elasticsearch/sql/tree/master/sql-cli)
* [SQL JDBC](https://github.com/opendistro-for-elasticsearch/sql/tree/master/sql-jdbc)
* [SQL ODBC](https://github.com/opendistro-for-elasticsearch/sql/tree/master/sql-odbc)
* [SQL Workbench](https://github.com/opendistro-for-elasticsearch/sql/tree/master/sql-workbench)
* [SQL Workbench](https://github.com/opendistro-for-elasticsearch/sql/tree/master/workbench)


## Documentation
Expand Down
Expand Up @@ -25,11 +25,13 @@
import com.amazon.opendistroforelasticsearch.sql.ast.expression.Let;
import com.amazon.opendistroforelasticsearch.sql.ast.expression.Literal;
import com.amazon.opendistroforelasticsearch.sql.ast.expression.Map;
import com.amazon.opendistroforelasticsearch.sql.ast.expression.UnresolvedArgument;
import com.amazon.opendistroforelasticsearch.sql.ast.expression.UnresolvedExpression;
import com.amazon.opendistroforelasticsearch.sql.ast.tree.Aggregation;
import com.amazon.opendistroforelasticsearch.sql.ast.tree.Dedupe;
import com.amazon.opendistroforelasticsearch.sql.ast.tree.Eval;
import com.amazon.opendistroforelasticsearch.sql.ast.tree.Filter;
import com.amazon.opendistroforelasticsearch.sql.ast.tree.Head;
import com.amazon.opendistroforelasticsearch.sql.ast.tree.Project;
import com.amazon.opendistroforelasticsearch.sql.ast.tree.RareTopN;
import com.amazon.opendistroforelasticsearch.sql.ast.tree.Relation;
Expand All @@ -46,10 +48,12 @@
import com.amazon.opendistroforelasticsearch.sql.expression.NamedExpression;
import com.amazon.opendistroforelasticsearch.sql.expression.ReferenceExpression;
import com.amazon.opendistroforelasticsearch.sql.expression.aggregation.Aggregator;
import com.amazon.opendistroforelasticsearch.sql.expression.aggregation.NamedAggregator;
import com.amazon.opendistroforelasticsearch.sql.planner.logical.LogicalAggregation;
import com.amazon.opendistroforelasticsearch.sql.planner.logical.LogicalDedupe;
import com.amazon.opendistroforelasticsearch.sql.planner.logical.LogicalEval;
import com.amazon.opendistroforelasticsearch.sql.planner.logical.LogicalFilter;
import com.amazon.opendistroforelasticsearch.sql.planner.logical.LogicalHead;
import com.amazon.opendistroforelasticsearch.sql.planner.logical.LogicalPlan;
import com.amazon.opendistroforelasticsearch.sql.planner.logical.LogicalProject;
import com.amazon.opendistroforelasticsearch.sql.planner.logical.LogicalRareTopN;
Expand Down Expand Up @@ -80,6 +84,8 @@ public class Analyzer extends AbstractNodeVisitor<LogicalPlan, AnalysisContext>

private final SelectExpressionAnalyzer selectExpressionAnalyzer;

private final NamedExpressionAnalyzer namedExpressionAnalyzer;

private final StorageEngine storageEngine;

/**
Expand All @@ -91,6 +97,7 @@ public Analyzer(
this.expressionAnalyzer = expressionAnalyzer;
this.storageEngine = storageEngine;
this.selectExpressionAnalyzer = new SelectExpressionAnalyzer(expressionAnalyzer);
this.namedExpressionAnalyzer = new NamedExpressionAnalyzer(expressionAnalyzer);
}

public LogicalPlan analyze(UnresolvedPlan unresolved, AnalysisContext context) {
Expand Down Expand Up @@ -153,25 +160,27 @@ public LogicalPlan visitRename(Rename node, AnalysisContext context) {
@Override
public LogicalPlan visitAggregation(Aggregation node, AnalysisContext context) {
final LogicalPlan child = node.getChild().get(0).accept(this, context);
ImmutableList.Builder<Aggregator> aggregatorBuilder = new ImmutableList.Builder<>();
ImmutableList.Builder<NamedAggregator> aggregatorBuilder = new ImmutableList.Builder<>();
for (UnresolvedExpression expr : node.getAggExprList()) {
aggregatorBuilder.add((Aggregator) expressionAnalyzer.analyze(expr, context));
NamedExpression aggExpr = namedExpressionAnalyzer.analyze(expr, context);
aggregatorBuilder
.add(new NamedAggregator(aggExpr.getName(), (Aggregator) aggExpr.getDelegated()));
}
ImmutableList<Aggregator> aggregators = aggregatorBuilder.build();
ImmutableList<NamedAggregator> aggregators = aggregatorBuilder.build();

ImmutableList.Builder<Expression> groupbyBuilder = new ImmutableList.Builder<>();
ImmutableList.Builder<NamedExpression> groupbyBuilder = new ImmutableList.Builder<>();
for (UnresolvedExpression expr : node.getGroupExprList()) {
groupbyBuilder.add(expressionAnalyzer.analyze(expr, context));
groupbyBuilder.add(namedExpressionAnalyzer.analyze(expr, context));
}
ImmutableList<Expression> groupBys = groupbyBuilder.build();
ImmutableList<NamedExpression> groupBys = groupbyBuilder.build();

// new context
context.push();
TypeEnvironment newEnv = context.peek();
aggregators.forEach(aggregator -> newEnv.define(new Symbol(Namespace.FIELD_NAME,
aggregator.toString()), aggregator.type()));
aggregator.getName()), aggregator.type()));
groupBys.forEach(group -> newEnv.define(new Symbol(Namespace.FIELD_NAME,
group.toString()), group.type()));
group.getName()), group.type()));
return new LogicalAggregation(child, aggregators, groupBys);
}

Expand Down Expand Up @@ -311,6 +320,23 @@ public LogicalPlan visitDedupe(Dedupe node, AnalysisContext context) {
consecutive);
}

/**
* Build {@link LogicalHead}.
*/
public LogicalPlan visitHead(Head node, AnalysisContext context) {
LogicalPlan child = node.getChild().get(0).accept(this, context);
List<UnresolvedArgument> options = node.getOptions();
Boolean keeplast = (Boolean) getOptionAsLiteral(options, 0).getValue();
Expression whileExpr = expressionAnalyzer.analyze(options.get(1).getValue(), context);
Integer number = (Integer) getOptionAsLiteral(options, 2).getValue();

return new LogicalHead(child, keeplast, whileExpr, number);
}

private static Literal getOptionAsLiteral(List<UnresolvedArgument> options, int optionIdx) {
return (Literal) options.get(optionIdx).getValue();
}

@Override
public LogicalPlan visitValues(Values node, AnalysisContext context) {
List<List<Literal>> values = node.getValues();
Expand Down
Expand Up @@ -25,7 +25,6 @@
import com.amazon.opendistroforelasticsearch.sql.ast.expression.Field;
import com.amazon.opendistroforelasticsearch.sql.ast.expression.Function;
import com.amazon.opendistroforelasticsearch.sql.ast.expression.Interval;
import com.amazon.opendistroforelasticsearch.sql.ast.expression.IntervalUnit;
import com.amazon.opendistroforelasticsearch.sql.ast.expression.Literal;
import com.amazon.opendistroforelasticsearch.sql.ast.expression.Not;
import com.amazon.opendistroforelasticsearch.sql.ast.expression.Or;
Expand Down
@@ -0,0 +1,61 @@
/*
*
* Copyright 2020 Amazon.com, Inc. or its affiliates. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License").
* You may not use this file except in compliance with the License.
* A copy of the License is located at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* or in the "license" file accompanying this file. This file is distributed
* on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
* express or implied. See the License for the specific language governing
* permissions and limitations under the License.
*
*/

package com.amazon.opendistroforelasticsearch.sql.analysis;

import com.amazon.opendistroforelasticsearch.sql.ast.AbstractNodeVisitor;
import com.amazon.opendistroforelasticsearch.sql.ast.expression.Alias;
import com.amazon.opendistroforelasticsearch.sql.ast.expression.QualifiedName;
import com.amazon.opendistroforelasticsearch.sql.ast.expression.UnresolvedExpression;
import com.amazon.opendistroforelasticsearch.sql.expression.DSL;
import com.amazon.opendistroforelasticsearch.sql.expression.NamedExpression;
import lombok.RequiredArgsConstructor;

/**
* Analyze the Alias node in the {@link AnalysisContext} to construct the list of
* {@link NamedExpression}.
*/
@RequiredArgsConstructor
public class NamedExpressionAnalyzer extends
AbstractNodeVisitor<NamedExpression, AnalysisContext> {
private final ExpressionAnalyzer expressionAnalyzer;

/**
* Analyze Select fields.
*/
public NamedExpression analyze(UnresolvedExpression expression,
AnalysisContext analysisContext) {
return expression.accept(this, analysisContext);
}

@Override
public NamedExpression visitAlias(Alias node, AnalysisContext context) {
return DSL.named(
unqualifiedNameIfFieldOnly(node, context),
node.getDelegated().accept(expressionAnalyzer, context),
node.getAlias());
}

private String unqualifiedNameIfFieldOnly(Alias node, AnalysisContext context) {
UnresolvedExpression selectItem = node.getDelegated();
if (selectItem instanceof QualifiedName) {
QualifierAnalyzer qualifierAnalyzer = new QualifierAnalyzer(context);
return qualifierAnalyzer.unqualified((QualifiedName) selectItem);
}
return node.getName();
}
}
Expand Up @@ -20,9 +20,11 @@
import com.amazon.opendistroforelasticsearch.sql.analysis.symbol.Namespace;
import com.amazon.opendistroforelasticsearch.sql.analysis.symbol.Symbol;
import com.amazon.opendistroforelasticsearch.sql.ast.AbstractNodeVisitor;
import com.amazon.opendistroforelasticsearch.sql.ast.expression.AggregateFunction;
import com.amazon.opendistroforelasticsearch.sql.ast.expression.Alias;
import com.amazon.opendistroforelasticsearch.sql.ast.expression.AllFields;
import com.amazon.opendistroforelasticsearch.sql.ast.expression.Field;
import com.amazon.opendistroforelasticsearch.sql.ast.expression.Function;
import com.amazon.opendistroforelasticsearch.sql.ast.expression.QualifiedName;
import com.amazon.opendistroforelasticsearch.sql.ast.expression.UnresolvedExpression;
import com.amazon.opendistroforelasticsearch.sql.data.type.ExprType;
Expand Down Expand Up @@ -67,23 +69,42 @@ public List<NamedExpression> visitField(Field node, AnalysisContext context) {

@Override
public List<NamedExpression> visitAlias(Alias node, AnalysisContext context) {
Expression expr = referenceIfSymbolDefined(node.getDelegated(), context);
Expression expr = referenceIfSymbolDefined(node, context);
return Collections.singletonList(DSL.named(
unqualifiedNameIfFieldOnly(node, context),
expr,
node.getAlias()));
}

private Expression referenceIfSymbolDefined(UnresolvedExpression expr,
/**
* The Alias could be
* 1. SELECT name, AVG(age) FROM s BY name ->
* Project(Alias("name", expr), Alias("AVG(age)", aggExpr))
* Agg(Alias("AVG(age)", aggExpr))
* 2. SELECT length(name), AVG(age) FROM s BY length(name)
* Project(Alias("name", expr), Alias("AVG(age)", aggExpr))
* Agg(Alias("AVG(age)", aggExpr))
* 3. SELECT length(name) as l, AVG(age) FROM s BY l
* Project(Alias("name", expr, l), Alias("AVG(age)", aggExpr))
* Agg(Alias("AVG(age)", aggExpr), Alias("length(name)", groupExpr))
*/
private Expression referenceIfSymbolDefined(Alias expr,
AnalysisContext context) {
UnresolvedExpression delegatedExpr = expr.getDelegated();
try {
// Since resolved aggregator.toString() is used as symbol name, unresolved expression
// needs to be analyzed too to get toString() name for consistency
String symbolName = expressionAnalyzer.analyze(expr, context).toString();
ExprType type = context.peek().resolve(new Symbol(Namespace.FIELD_NAME, symbolName));
return DSL.ref(symbolName, type);
if ((delegatedExpr instanceof AggregateFunction)
|| (delegatedExpr instanceof Function)) {
ExprType type = context.peek().resolve(new Symbol(Namespace.FIELD_NAME, expr.getName()));
return DSL.ref(expr.getName(), type);
} else {
// Since resolved aggregator.toString() is used as symbol name, unresolved expression
// needs to be analyzed too to get toString() name for consistency
String symbolName = expressionAnalyzer.analyze(delegatedExpr, context).toString();
ExprType type = context.peek().resolve(new Symbol(Namespace.FIELD_NAME, symbolName));
return DSL.ref(symbolName, type);
}
} catch (SemanticCheckException e) {
return expr.accept(expressionAnalyzer, context);
return delegatedExpr.accept(expressionAnalyzer, context);
}
}

Expand Down
Expand Up @@ -33,12 +33,14 @@
import com.amazon.opendistroforelasticsearch.sql.ast.expression.Not;
import com.amazon.opendistroforelasticsearch.sql.ast.expression.Or;
import com.amazon.opendistroforelasticsearch.sql.ast.expression.QualifiedName;
import com.amazon.opendistroforelasticsearch.sql.ast.expression.UnresolvedArgument;
import com.amazon.opendistroforelasticsearch.sql.ast.expression.UnresolvedAttribute;
import com.amazon.opendistroforelasticsearch.sql.ast.expression.Xor;
import com.amazon.opendistroforelasticsearch.sql.ast.tree.Aggregation;
import com.amazon.opendistroforelasticsearch.sql.ast.tree.Dedupe;
import com.amazon.opendistroforelasticsearch.sql.ast.tree.Eval;
import com.amazon.opendistroforelasticsearch.sql.ast.tree.Filter;
import com.amazon.opendistroforelasticsearch.sql.ast.tree.Head;
import com.amazon.opendistroforelasticsearch.sql.ast.tree.Project;
import com.amazon.opendistroforelasticsearch.sql.ast.tree.RareTopN;
import com.amazon.opendistroforelasticsearch.sql.ast.tree.Relation;
Expand Down Expand Up @@ -179,6 +181,10 @@ public T visitDedupe(Dedupe node, C context) {
return visitChildren(node, context);
}

public T visitHead(Head node, C context) {
return visitChildren(node, context);
}

public T visitRareTopN(RareTopN node, C context) {
return visitChildren(node, context);
}
Expand All @@ -198,4 +204,8 @@ public T visitAllFields(AllFields node, C context) {
public T visitInterval(Interval node, C context) {
return visitChildren(node, context);
}

public T visitUnresolvedArgument(UnresolvedArgument node, C context) {
return visitChildren(node, context);
}
}
Expand Up @@ -33,13 +33,15 @@
import com.amazon.opendistroforelasticsearch.sql.ast.expression.Not;
import com.amazon.opendistroforelasticsearch.sql.ast.expression.Or;
import com.amazon.opendistroforelasticsearch.sql.ast.expression.QualifiedName;
import com.amazon.opendistroforelasticsearch.sql.ast.expression.UnresolvedArgument;
import com.amazon.opendistroforelasticsearch.sql.ast.expression.UnresolvedAttribute;
import com.amazon.opendistroforelasticsearch.sql.ast.expression.UnresolvedExpression;
import com.amazon.opendistroforelasticsearch.sql.ast.expression.Xor;
import com.amazon.opendistroforelasticsearch.sql.ast.tree.Aggregation;
import com.amazon.opendistroforelasticsearch.sql.ast.tree.Dedupe;
import com.amazon.opendistroforelasticsearch.sql.ast.tree.Eval;
import com.amazon.opendistroforelasticsearch.sql.ast.tree.Filter;
import com.amazon.opendistroforelasticsearch.sql.ast.tree.Head;
import com.amazon.opendistroforelasticsearch.sql.ast.tree.Project;
import com.amazon.opendistroforelasticsearch.sql.ast.tree.RareTopN;
import com.amazon.opendistroforelasticsearch.sql.ast.tree.RareTopN.CommandType;
Expand Down Expand Up @@ -214,6 +216,10 @@ public static Argument argument(String argName, Literal argValue) {
return new Argument(argName, argValue);
}

public static UnresolvedArgument unresolvedArg(String argName, UnresolvedExpression argValue) {
return new UnresolvedArgument(argName, argValue);
}

public static UnresolvedExpression field(UnresolvedExpression field) {
return new Field((QualifiedName) field);
}
Expand Down Expand Up @@ -254,6 +260,10 @@ public static List<Argument> exprList(Argument... exprList) {
return Arrays.asList(exprList);
}

public static List<UnresolvedArgument> unresolvedArgList(UnresolvedArgument... exprList) {
return Arrays.asList(exprList);
}

public static List<Argument> defaultFieldsArgs() {
return exprList(argument("exclude", booleanLiteral(false)));
}
Expand Down Expand Up @@ -299,6 +309,20 @@ public static Dedupe dedupe(UnresolvedPlan input, List<Argument> options, Field.
return new Dedupe(input, options, Arrays.asList(fields));
}

public static Head head(UnresolvedPlan input, List<UnresolvedArgument> options) {
return new Head(input, options);
}

/**
* Default Head Command Args.
*/
public static List<UnresolvedArgument> defaultHeadArgs() {
return unresolvedArgList(
unresolvedArg("keeplast", booleanLiteral(true)),
unresolvedArg("whileExpr", booleanLiteral(true)),
unresolvedArg("number", intLiteral(10)));
}

public static List<Argument> defaultTopArgs() {
return exprList(argument("noOfResults", intLiteral(10)));
}
Expand Down
Expand Up @@ -18,17 +18,17 @@
import com.amazon.opendistroforelasticsearch.sql.ast.AbstractNodeVisitor;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;
import lombok.EqualsAndHashCode;
import lombok.Getter;
import lombok.RequiredArgsConstructor;
import lombok.ToString;


/**
* Expression node of scalar function.
* Params include function name (@funcName) and function arguments (@funcArgs)
*/
@Getter
@ToString
@EqualsAndHashCode(callSuper = false)
@RequiredArgsConstructor
public class Function extends UnresolvedExpression {
Expand All @@ -44,4 +44,12 @@ public List<UnresolvedExpression> getChild() {
public <R, C> R accept(AbstractNodeVisitor<R, C> nodeVisitor, C context) {
return nodeVisitor.visitFunction(this, context);
}

@Override
public String toString() {
return String.format("%s(%s)", funcName,
funcArgs.stream()
.map(Object::toString)
.collect(Collectors.joining(", ")));
}
}
Expand Up @@ -29,7 +29,6 @@
* literal data type (@type) which can be selected from {@link DataType}.
*/
@Getter
@ToString
@EqualsAndHashCode(callSuper = false)
@RequiredArgsConstructor
public class Literal extends UnresolvedExpression {
Expand All @@ -46,4 +45,9 @@ public List<UnresolvedExpression> getChild() {
public <R, C> R accept(AbstractNodeVisitor<R, C> nodeVisitor, C context) {
return nodeVisitor.visitLiteral(this, context);
}

@Override
public String toString() {
return value.toString();
}
}

0 comments on commit 3fa954c

Please sign in to comment.