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

Commit

Permalink
Merge branch 'develop' into support-null-first-last-for-window-function
Browse files Browse the repository at this point in the history
  • Loading branch information
dai-chen committed Jan 5, 2021
2 parents f7f99a2 + 0855c53 commit 5b2b9a7
Show file tree
Hide file tree
Showing 82 changed files with 4,403 additions and 270 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,7 @@ public LogicalPlan visitAggregation(Aggregation node, AnalysisContext context) {
for (UnresolvedExpression expr : node.getAggExprList()) {
NamedExpression aggExpr = namedExpressionAnalyzer.analyze(expr, context);
aggregatorBuilder
.add(new NamedAggregator(aggExpr.getName(), (Aggregator) aggExpr.getDelegated()));
.add(new NamedAggregator(aggExpr.getNameOrAlias(), (Aggregator) aggExpr.getDelegated()));
}
ImmutableList<NamedAggregator> aggregators = aggregatorBuilder.build();

Expand All @@ -210,7 +210,7 @@ public LogicalPlan visitAggregation(Aggregation node, AnalysisContext context) {
aggregators.forEach(aggregator -> newEnv.define(new Symbol(Namespace.FIELD_NAME,
aggregator.getName()), aggregator.type()));
groupBys.forEach(group -> newEnv.define(new Symbol(Namespace.FIELD_NAME,
group.getName()), group.type()));
group.getNameOrAlias()), group.type()));
return new LogicalAggregation(child, aggregators, groupBys);
}

Expand Down Expand Up @@ -291,7 +291,7 @@ public LogicalPlan visitProject(Project node, AnalysisContext context) {
context.push();
TypeEnvironment newEnv = context.peek();
namedExpressions.forEach(expr -> newEnv.define(new Symbol(Namespace.FIELD_NAME,
expr.getName()), expr.type()));
expr.getNameOrAlias()), expr.type()));
return new LogicalProject(child, namedExpressions);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import com.amazon.opendistroforelasticsearch.sql.ast.expression.AllFields;
import com.amazon.opendistroforelasticsearch.sql.ast.expression.And;
import com.amazon.opendistroforelasticsearch.sql.ast.expression.Case;
import com.amazon.opendistroforelasticsearch.sql.ast.expression.Cast;
import com.amazon.opendistroforelasticsearch.sql.ast.expression.Compare;
import com.amazon.opendistroforelasticsearch.sql.ast.expression.EqualTo;
import com.amazon.opendistroforelasticsearch.sql.ast.expression.Field;
Expand Down Expand Up @@ -67,6 +68,13 @@ public class ExpressionAnalyzer extends AbstractNodeVisitor<Expression, Analysis
private final BuiltinFunctionRepository repository;
private final DSL dsl;

@Override
public Expression visitCast(Cast node, AnalysisContext context) {
final Expression expression = node.getExpression().accept(this, context);
return (Expression) repository
.compile(node.convertFunctionName(), Collections.singletonList(expression));
}

public ExpressionAnalyzer(
BuiltinFunctionRepository repository) {
this.repository = repository;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,8 @@ public Void visitAggregation(LogicalAggregation plan, Void context) {
new ReferenceExpression(namedAggregator.getName(), namedAggregator.type())));
// Create the mapping for all the group by.
plan.getGroupByList().forEach(groupBy -> expressionMap
.put(groupBy.getDelegated(), new ReferenceExpression(groupBy.getName(), groupBy.type())));
.put(groupBy.getDelegated(),
new ReferenceExpression(groupBy.getNameOrAlias(), groupBy.type())));
return null;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import com.amazon.opendistroforelasticsearch.sql.ast.expression.Argument;
import com.amazon.opendistroforelasticsearch.sql.ast.expression.AttributeList;
import com.amazon.opendistroforelasticsearch.sql.ast.expression.Case;
import com.amazon.opendistroforelasticsearch.sql.ast.expression.Cast;
import com.amazon.opendistroforelasticsearch.sql.ast.expression.Compare;
import com.amazon.opendistroforelasticsearch.sql.ast.expression.EqualTo;
import com.amazon.opendistroforelasticsearch.sql.ast.expression.Field;
Expand Down Expand Up @@ -226,6 +227,10 @@ public T visitWhen(When node, C context) {
return visitChildren(node, context);
}

public T visitCast(Cast node, C context) {
return visitChildren(node, context);
}

public T visitUnresolvedArgument(UnresolvedArgument node, C context) {
return visitChildren(node, context);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import com.amazon.opendistroforelasticsearch.sql.ast.expression.And;
import com.amazon.opendistroforelasticsearch.sql.ast.expression.Argument;
import com.amazon.opendistroforelasticsearch.sql.ast.expression.Case;
import com.amazon.opendistroforelasticsearch.sql.ast.expression.Cast;
import com.amazon.opendistroforelasticsearch.sql.ast.expression.Compare;
import com.amazon.opendistroforelasticsearch.sql.ast.expression.DataType;
import com.amazon.opendistroforelasticsearch.sql.ast.expression.EqualTo;
Expand Down Expand Up @@ -222,6 +223,10 @@ public UnresolvedExpression caseWhen(UnresolvedExpression caseValueExpr,
return new Case(caseValueExpr, Arrays.asList(whenClauses), elseClause);
}

public UnresolvedExpression cast(UnresolvedExpression expr, Literal type) {
return new Cast(expr, type);
}

public When when(UnresolvedExpression condition, UnresolvedExpression result) {
return new When(condition, result);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
/*
*
* 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.ast.expression;

import static com.amazon.opendistroforelasticsearch.sql.expression.function.BuiltinFunctionName.CAST_TO_BOOLEAN;
import static com.amazon.opendistroforelasticsearch.sql.expression.function.BuiltinFunctionName.CAST_TO_DATE;
import static com.amazon.opendistroforelasticsearch.sql.expression.function.BuiltinFunctionName.CAST_TO_DOUBLE;
import static com.amazon.opendistroforelasticsearch.sql.expression.function.BuiltinFunctionName.CAST_TO_FLOAT;
import static com.amazon.opendistroforelasticsearch.sql.expression.function.BuiltinFunctionName.CAST_TO_INT;
import static com.amazon.opendistroforelasticsearch.sql.expression.function.BuiltinFunctionName.CAST_TO_LONG;
import static com.amazon.opendistroforelasticsearch.sql.expression.function.BuiltinFunctionName.CAST_TO_STRING;
import static com.amazon.opendistroforelasticsearch.sql.expression.function.BuiltinFunctionName.CAST_TO_TIME;
import static com.amazon.opendistroforelasticsearch.sql.expression.function.BuiltinFunctionName.CAST_TO_TIMESTAMP;

import com.amazon.opendistroforelasticsearch.sql.ast.AbstractNodeVisitor;
import com.amazon.opendistroforelasticsearch.sql.ast.Node;
import com.amazon.opendistroforelasticsearch.sql.expression.function.FunctionName;
import com.google.common.collect.ImmutableMap;
import java.util.Collections;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import lombok.AllArgsConstructor;
import lombok.EqualsAndHashCode;
import lombok.Getter;
import lombok.ToString;

/**
* AST node that represents Cast clause.
*/
@AllArgsConstructor
@EqualsAndHashCode(callSuper = false)
@Getter
@ToString
public class Cast extends UnresolvedExpression {

private static Map<String, FunctionName> CONVERTED_TYPE_FUNCTION_NAME_MAP =
new ImmutableMap.Builder<String, FunctionName>()
.put("string", CAST_TO_STRING.getName())
.put("int", CAST_TO_INT.getName())
.put("long", CAST_TO_LONG.getName())
.put("float", CAST_TO_FLOAT.getName())
.put("double", CAST_TO_DOUBLE.getName())
.put("boolean", CAST_TO_BOOLEAN.getName())
.put("date", CAST_TO_DATE.getName())
.put("time", CAST_TO_TIME.getName())
.put("timestamp", CAST_TO_TIMESTAMP.getName())
.build();

/**
* The source expression cast from.
*/
private final UnresolvedExpression expression;

/**
* Expression that represents ELSE statement result.
*/
private final UnresolvedExpression convertedType;

/**
* Get the converted type.
*
* @return converted type
*/
public FunctionName convertFunctionName() {
String type = convertedType.toString().toLowerCase(Locale.ROOT);
if (CONVERTED_TYPE_FUNCTION_NAME_MAP.containsKey(type)) {
return CONVERTED_TYPE_FUNCTION_NAME_MAP.get(type);
} else {
throw new IllegalStateException("unsupported cast type: " + type);
}
}

@Override
public List<? extends Node> getChild() {
return Collections.singletonList(expression);
}

@Override
public <T, C> T accept(AbstractNodeVisitor<T, C> nodeVisitor, C context) {
return nodeVisitor.visitCast(this, context);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -409,6 +409,10 @@ public FunctionExpression strcmp(Expression... expressions) {
return function(BuiltinFunctionName.STRCMP, expressions);
}

public FunctionExpression right(Expression... expressions) {
return function(BuiltinFunctionName.RIGHT, expressions);
}

public FunctionExpression and(Expression... expressions) {
return function(BuiltinFunctionName.AND, expressions);
}
Expand Down Expand Up @@ -523,4 +527,49 @@ public FunctionExpression interval(Expression value, Expression unit) {
return (FunctionExpression) repository.compile(
BuiltinFunctionName.INTERVAL.getName(), Arrays.asList(value, unit));
}

public FunctionExpression castString(Expression value) {
return (FunctionExpression) repository
.compile(BuiltinFunctionName.CAST_TO_STRING.getName(), Arrays.asList(value));
}

public FunctionExpression castInt(Expression value) {
return (FunctionExpression) repository
.compile(BuiltinFunctionName.CAST_TO_INT.getName(), Arrays.asList(value));
}

public FunctionExpression castLong(Expression value) {
return (FunctionExpression) repository
.compile(BuiltinFunctionName.CAST_TO_LONG.getName(), Arrays.asList(value));
}

public FunctionExpression castFloat(Expression value) {
return (FunctionExpression) repository
.compile(BuiltinFunctionName.CAST_TO_FLOAT.getName(), Arrays.asList(value));
}

public FunctionExpression castDouble(Expression value) {
return (FunctionExpression) repository
.compile(BuiltinFunctionName.CAST_TO_DOUBLE.getName(), Arrays.asList(value));
}

public FunctionExpression castBoolean(Expression value) {
return (FunctionExpression) repository
.compile(BuiltinFunctionName.CAST_TO_BOOLEAN.getName(), Arrays.asList(value));
}

public FunctionExpression castDate(Expression value) {
return (FunctionExpression) repository
.compile(BuiltinFunctionName.CAST_TO_DATE.getName(), Arrays.asList(value));
}

public FunctionExpression castTime(Expression value) {
return (FunctionExpression) repository
.compile(BuiltinFunctionName.CAST_TO_TIME.getName(), Arrays.asList(value));
}

public FunctionExpression castTimestamp(Expression value) {
return (FunctionExpression) repository
.compile(BuiltinFunctionName.CAST_TO_TIMESTAMP.getName(), Arrays.asList(value));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ public T visitLiteral(LiteralExpression node, C context) {
}

public T visitNamed(NamedExpression node, C context) {
return visitNode(node, context);
return node.getDelegated().accept(this, context);
}

public T visitReference(ReferenceExpression node, C context) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@
import lombok.EqualsAndHashCode;
import lombok.Getter;
import lombok.RequiredArgsConstructor;
import lombok.ToString;

/**
* Named expression that represents expression with name.
Expand All @@ -33,6 +32,7 @@
*/
@AllArgsConstructor
@EqualsAndHashCode
@Getter
@RequiredArgsConstructor
public class NamedExpression implements Expression {

Expand All @@ -44,13 +44,11 @@ public class NamedExpression implements Expression {
/**
* Expression that being named.
*/
@Getter
private final Expression delegated;

/**
* Optional alias.
*/
@Getter
private String alias;

@Override
Expand All @@ -67,7 +65,7 @@ public ExprType type() {
* Get expression name using name or its alias (if it's present).
* @return expression name
*/
public String getName() {
public String getNameOrAlias() {
return Strings.isNullOrEmpty(alias) ? name : alias;
}

Expand All @@ -78,7 +76,7 @@ public <T, C> T accept(ExpressionNodeVisitor<T, C> visitor, C context) {

@Override
public String toString() {
return getName();
return getNameOrAlias();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,22 @@
import static com.amazon.opendistroforelasticsearch.sql.data.type.ExprCoreType.TIME;
import static com.amazon.opendistroforelasticsearch.sql.data.type.ExprCoreType.TIMESTAMP;

import com.amazon.opendistroforelasticsearch.sql.data.type.ExprCoreType;
import com.amazon.opendistroforelasticsearch.sql.data.type.ExprType;
import com.amazon.opendistroforelasticsearch.sql.expression.function.BuiltinFunctionName;
import com.amazon.opendistroforelasticsearch.sql.expression.function.BuiltinFunctionRepository;
import com.amazon.opendistroforelasticsearch.sql.expression.function.FunctionBuilder;
import com.amazon.opendistroforelasticsearch.sql.expression.function.FunctionName;
import com.amazon.opendistroforelasticsearch.sql.expression.function.FunctionResolver;
import com.amazon.opendistroforelasticsearch.sql.expression.function.FunctionSignature;
import com.google.common.collect.ImmutableMap;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

import lombok.experimental.UtilityClass;

/**
Expand Down Expand Up @@ -73,27 +81,11 @@ private static FunctionResolver avg() {

private static FunctionResolver count() {
FunctionName functionName = BuiltinFunctionName.COUNT.getName();
return new FunctionResolver(
functionName,
new ImmutableMap.Builder<FunctionSignature, FunctionBuilder>()
.put(new FunctionSignature(functionName, Collections.singletonList(INTEGER)),
arguments -> new CountAggregator(arguments, INTEGER))
.put(new FunctionSignature(functionName, Collections.singletonList(LONG)),
arguments -> new CountAggregator(arguments, INTEGER))
.put(new FunctionSignature(functionName, Collections.singletonList(FLOAT)),
arguments -> new CountAggregator(arguments, INTEGER))
.put(new FunctionSignature(functionName, Collections.singletonList(DOUBLE)),
arguments -> new CountAggregator(arguments, INTEGER))
.put(new FunctionSignature(functionName, Collections.singletonList(STRING)),
arguments -> new CountAggregator(arguments, INTEGER))
.put(new FunctionSignature(functionName, Collections.singletonList(STRUCT)),
arguments -> new CountAggregator(arguments, INTEGER))
.put(new FunctionSignature(functionName, Collections.singletonList(ARRAY)),
arguments -> new CountAggregator(arguments, INTEGER))
.put(new FunctionSignature(functionName, Collections.singletonList(BOOLEAN)),
arguments -> new CountAggregator(arguments, INTEGER))
.build()
);
FunctionResolver functionResolver = new FunctionResolver(functionName,
ExprCoreType.coreTypes().stream().collect(Collectors.toMap(
type -> new FunctionSignature(functionName, Collections.singletonList(type)),
type -> arguments -> new CountAggregator(arguments, INTEGER))));
return functionResolver;
}

private static FunctionResolver sum() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import com.amazon.opendistroforelasticsearch.sql.expression.function.BuiltinFunctionRepository;
import com.amazon.opendistroforelasticsearch.sql.expression.operator.arthmetic.ArithmeticFunction;
import com.amazon.opendistroforelasticsearch.sql.expression.operator.arthmetic.MathematicalFunction;
import com.amazon.opendistroforelasticsearch.sql.expression.operator.convert.TypeCastOperator;
import com.amazon.opendistroforelasticsearch.sql.expression.operator.predicate.BinaryPredicateOperator;
import com.amazon.opendistroforelasticsearch.sql.expression.operator.predicate.UnaryPredicateOperator;
import com.amazon.opendistroforelasticsearch.sql.expression.text.TextFunction;
Expand Down Expand Up @@ -51,6 +52,7 @@ public BuiltinFunctionRepository functionRepository() {
IntervalClause.register(builtinFunctionRepository);
WindowFunctions.register(builtinFunctionRepository);
TextFunction.register(builtinFunctionRepository);
TypeCastOperator.register(builtinFunctionRepository);
return builtinFunctionRepository;
}

Expand Down
Loading

0 comments on commit 5b2b9a7

Please sign in to comment.