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

Redefine the Expression interface and Type #550

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@

package com.amazon.opendistroforelasticsearch.sql.analysis;

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.Argument;
import com.amazon.opendistroforelasticsearch.sql.ast.expression.Field;
Expand Down Expand Up @@ -82,7 +84,7 @@ public LogicalPlan visitRelation(Relation node, AnalysisContext context) {
context.push();
TypeEnvironment curEnv = context.peek();
Table table = storageEngine.getTable(node.getTableName());
table.getFieldTypes().forEach((k, v) -> curEnv.define(DSL.ref(k), v));
table.getFieldTypes().forEach((k, v) -> curEnv.define(new Symbol(Namespace.FIELD_NAME, k), v));
return new LogicalRelation(node.getTableName());
}

Expand All @@ -106,9 +108,10 @@ public LogicalPlan visitRename(Rename node, AnalysisContext context) {
// We should define the new target field in the context instead of analyze it.
if (renameMap.getTarget() instanceof Field) {
ReferenceExpression target =
new ReferenceExpression(((Field) renameMap.getTarget()).getField().toString());
context.peek().define(target, origin.type(context.peek()));
renameMapBuilder.put(DSL.ref(origin.toString()), target);
new ReferenceExpression(((Field) renameMap.getTarget()).getField().toString(),
origin.type());
context.peek().define(target);
renameMapBuilder.put(DSL.ref(origin.toString(), origin.type()), target);
} else {
throw new SemanticCheckException(
String.format("the target expected to be field, but is %s", renameMap.getTarget()));
Expand Down Expand Up @@ -157,15 +160,15 @@ public LogicalPlan visitProject(Project node, AnalysisContext context) {
if (exclude) {
List<ReferenceExpression> referenceExpressions =
node.getProjectList().stream()
.map(expr -> (ReferenceExpression) expressionAnalyzer.analyze(expr, context))
.map(expr -> (ReferenceExpression) expressionAnalyzer.analyze(expr, context))
.collect(Collectors.toList());
return new LogicalRemove(child, ImmutableSet.copyOf(referenceExpressions));
}
}

List<Expression> expressions = node.getProjectList().stream()
.map(expr -> expressionAnalyzer.analyze(expr, context))
.collect(Collectors.toList());
.map(expr -> expressionAnalyzer.analyze(expr, context))
.collect(Collectors.toList());
return new LogicalProject(child, expressions);
}

Expand All @@ -178,12 +181,12 @@ public LogicalPlan visitEval(Eval node, AnalysisContext context) {
ImmutableList.Builder<Pair<ReferenceExpression, Expression>> expressionsBuilder =
new Builder<>();
for (Let let : node.getExpressionList()) {
ReferenceExpression ref = DSL.ref(let.getVar().getField().toString());
Expression expression = expressionAnalyzer.analyze(let.getExpression(), context);
ReferenceExpression ref = DSL.ref(let.getVar().getField().toString(), expression.type());
expressionsBuilder.add(ImmutablePair.of(ref, expression));
TypeEnvironment typeEnvironment = context.peek();
// define the new reference in type env.
typeEnvironment.define(ref, expression.type(typeEnvironment));
typeEnvironment.define(ref);
}
return new LogicalEval(child, expressionsBuilder.build());
}
Expand Down Expand Up @@ -239,8 +242,8 @@ public LogicalPlan visitValues(Values node, AnalysisContext context) {
List<List<LiteralExpression>> valueExprs = new ArrayList<>();
for (List<Literal> value : values) {
valueExprs.add(value.stream()
.map(val -> (LiteralExpression) expressionAnalyzer.analyze(val, context))
.collect(Collectors.toList()));
.map(val -> (LiteralExpression) expressionAnalyzer.analyze(val, context))
.collect(Collectors.toList()));
}
return new LogicalValues(valueExprs);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@

package com.amazon.opendistroforelasticsearch.sql.analysis;

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.And;
Expand Down Expand Up @@ -63,18 +65,15 @@ public Expression analyze(UnresolvedExpression unresolved, AnalysisContext conte

@Override
public Expression visitUnresolvedAttribute(UnresolvedAttribute node, AnalysisContext context) {
TypeEnvironment typeEnv = context.peek();
ReferenceExpression ref = DSL.ref(node.getAttr());
typeEnv.resolve(ref);
return ref;
return visitIdentifier(node.getAttr(), context);
}

@Override
public Expression visitEqualTo(EqualTo node, AnalysisContext context) {
Expression left = node.getLeft().accept(this, context);
Expression right = node.getRight().accept(this, context);

return dsl.equal(context.peek(), left, right);
return dsl.equal(left, right);
}

@Override
Expand All @@ -87,28 +86,28 @@ public Expression visitAnd(And node, AnalysisContext context) {
Expression left = node.getLeft().accept(this, context);
Expression right = node.getRight().accept(this, context);

return dsl.and(context.peek(), left, right);
return dsl.and(left, right);
}

@Override
public Expression visitOr(Or node, AnalysisContext context) {
Expression left = node.getLeft().accept(this, context);
Expression right = node.getRight().accept(this, context);

return dsl.or(context.peek(), left, right);
return dsl.or(left, right);
}

@Override
public Expression visitXor(Xor node, AnalysisContext context) {
Expression left = node.getLeft().accept(this, context);
Expression right = node.getRight().accept(this, context);

return dsl.xor(context.peek(), left, right);
return dsl.xor(left, right);
}

@Override
public Expression visitNot(Not node, AnalysisContext context) {
return dsl.not(context.peek(), node.getExpression().accept(this, context));
return dsl.not(node.getExpression().accept(this, context));
}

@Override
Expand All @@ -118,7 +117,7 @@ public Expression visitAggregateFunction(AggregateFunction node, AnalysisContext
Expression arg = node.getField().accept(this, context);
return (Aggregator)
repository.compile(
builtinFunctionName.get().getName(), Collections.singletonList(arg), context.peek());
builtinFunctionName.get().getName(), Collections.singletonList(arg));
} else {
throw new SemanticCheckException("Unsupported aggregation function " + node.getFuncName());
}
Expand All @@ -131,7 +130,7 @@ public Expression visitFunction(Function node, AnalysisContext context) {
node.getFuncArgs().stream()
.map(unresolvedExpression -> analyze(unresolvedExpression, context))
.collect(Collectors.toList());
return (Expression) repository.compile(functionName, arguments, context.peek());
return (Expression) repository.compile(functionName, arguments);
}

@Override
Expand All @@ -140,15 +139,19 @@ public Expression visitCompare(Compare node, AnalysisContext context) {
Expression left = analyze(node.getLeft(), context);
Expression right = analyze(node.getRight(), context);
return (Expression)
repository.compile(functionName, Arrays.asList(left, right), context.peek());
repository.compile(functionName, Arrays.asList(left, right));
}

@Override
public Expression visitField(Field node, AnalysisContext context) {
String attr = node.getField().toString();
return visitIdentifier(attr, context);
}

private Expression visitIdentifier(String ident, AnalysisContext context) {
TypeEnvironment typeEnv = context.peek();
ReferenceExpression ref = DSL.ref(attr);
typeEnv.resolve(ref);
ReferenceExpression ref = DSL.ref(ident,
typeEnv.resolve(new Symbol(Namespace.FIELD_NAME, ident)));
return ref;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
import com.amazon.opendistroforelasticsearch.sql.analysis.symbol.Namespace;
import com.amazon.opendistroforelasticsearch.sql.analysis.symbol.Symbol;
import com.amazon.opendistroforelasticsearch.sql.analysis.symbol.SymbolTable;
import com.amazon.opendistroforelasticsearch.sql.data.model.ExprType;
import com.amazon.opendistroforelasticsearch.sql.data.type.ExprType;
import com.amazon.opendistroforelasticsearch.sql.exception.SemanticCheckException;
import com.amazon.opendistroforelasticsearch.sql.expression.Expression;
import com.amazon.opendistroforelasticsearch.sql.expression.ReferenceExpression;
Expand All @@ -29,7 +29,7 @@
/**
* The definition of Type Environment.
*/
public class TypeEnvironment implements Environment<Expression, ExprType> {
public class TypeEnvironment implements Environment<Symbol, ExprType> {
@Getter
private final TypeEnvironment parent;
private final SymbolTable symbolTable;
Expand All @@ -47,38 +47,38 @@ public TypeEnvironment(TypeEnvironment parent, SymbolTable symbolTable) {
/**
* Resolve the {@link Expression} from environment.
*
* @param var expression
* @param symbol Symbol
* @return resolved {@link ExprType}
*/
@Override
public ExprType resolve(Expression var) {
if (var instanceof ReferenceExpression) {
ReferenceExpression ref = (ReferenceExpression) var;
for (TypeEnvironment cur = this; cur != null; cur = cur.parent) {
Optional<ExprType> typeOptional = cur.symbolTable.lookup(new Symbol(Namespace.FIELD_NAME,
ref.getAttr()));
if (typeOptional.isPresent()) {
return typeOptional.get();
}
public ExprType resolve(Symbol symbol) {
for (TypeEnvironment cur = this; cur != null; cur = cur.parent) {
Optional<ExprType> typeOptional = cur.symbolTable.lookup(symbol);
if (typeOptional.isPresent()) {
return typeOptional.get();
}
}
throw new SemanticCheckException(String.format("can't resolve expression %s in type env", var));
throw new SemanticCheckException(
String.format("can't resolve %s in type env", symbol));
}

/**
* Define symbol with the type.
*
* @param var symbol to define
* @param type type
* @param symbol symbol to define
* @param type type
*/
public void define(Expression var, ExprType type) {
if (var instanceof ReferenceExpression) {
ReferenceExpression ref = (ReferenceExpression) var;
symbolTable.store(new Symbol(Namespace.FIELD_NAME, ref.getAttr()), type);
} else {
throw new IllegalArgumentException(
String.format("only support define reference, unexpected expression %s", var));
}
public void define(Symbol symbol, ExprType type) {
symbolTable.store(symbol, type);
}

/**
* Define expression with the type.
*
* @param ref {@link ReferenceExpression}
*/
public void define(ReferenceExpression ref) {
define(new Symbol(Namespace.FIELD_NAME, ref.getAttr()), ref.type());
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,12 @@

import lombok.Getter;
import lombok.RequiredArgsConstructor;
import lombok.ToString;

/**
* Symbol in the scope.
*/
@ToString
@Getter
@RequiredArgsConstructor
public class Symbol {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
import static java.util.Collections.emptyMap;
import static java.util.Collections.emptyNavigableMap;

import com.amazon.opendistroforelasticsearch.sql.data.model.ExprType;
import com.amazon.opendistroforelasticsearch.sql.data.type.ExprType;
import java.util.EnumMap;
import java.util.Map;
import java.util.NavigableMap;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@

package com.amazon.opendistroforelasticsearch.sql.data.model;

import com.amazon.opendistroforelasticsearch.sql.data.type.ExprCoreType;
import lombok.EqualsAndHashCode;

@EqualsAndHashCode
Expand Down Expand Up @@ -42,8 +43,8 @@ public Object value() {
}

@Override
public ExprType type() {
return ExprType.BOOLEAN;
public ExprCoreType type() {
return ExprCoreType.BOOLEAN;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@

package com.amazon.opendistroforelasticsearch.sql.data.model;

import com.amazon.opendistroforelasticsearch.sql.data.type.ExprCoreType;
import java.util.List;
import java.util.stream.Collectors;
import lombok.EqualsAndHashCode;
Expand All @@ -31,8 +32,8 @@ public Object value() {
}

@Override
public ExprType type() {
return ExprType.ARRAY;
public ExprCoreType type() {
return ExprCoreType.ARRAY;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@

package com.amazon.opendistroforelasticsearch.sql.data.model;

import com.amazon.opendistroforelasticsearch.sql.data.type.ExprCoreType;
import lombok.EqualsAndHashCode;
import lombok.RequiredArgsConstructor;

Expand All @@ -29,8 +30,8 @@ public Object value() {
}

@Override
public ExprType type() {
return ExprType.DOUBLE;
public ExprCoreType type() {
return ExprCoreType.DOUBLE;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@

package com.amazon.opendistroforelasticsearch.sql.data.model;

import com.amazon.opendistroforelasticsearch.sql.data.type.ExprCoreType;
import lombok.EqualsAndHashCode;
import lombok.RequiredArgsConstructor;

Expand All @@ -29,8 +30,8 @@ public Object value() {
}

@Override
public ExprType type() {
return ExprType.FLOAT;
public ExprCoreType type() {
return ExprCoreType.FLOAT;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@

package com.amazon.opendistroforelasticsearch.sql.data.model;

import com.amazon.opendistroforelasticsearch.sql.data.type.ExprCoreType;
import lombok.EqualsAndHashCode;
import lombok.RequiredArgsConstructor;

Expand All @@ -29,8 +30,8 @@ public Object value() {
}

@Override
public ExprType type() {
return ExprType.INTEGER;
public ExprCoreType type() {
return ExprCoreType.INTEGER;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@

package com.amazon.opendistroforelasticsearch.sql.data.model;

import com.amazon.opendistroforelasticsearch.sql.data.type.ExprCoreType;
import lombok.EqualsAndHashCode;
import lombok.RequiredArgsConstructor;

Expand All @@ -29,8 +30,8 @@ public Object value() {
}

@Override
public ExprType type() {
return ExprType.LONG;
public ExprCoreType type() {
return ExprCoreType.LONG;
}

@Override
Expand Down
Loading