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

Commit 8df9a88

Browse files
authored
Redefine the Expression interface and Type (#550)
* Redefine the Expression interface and Type * address comments
1 parent bd379cf commit 8df9a88

File tree

86 files changed

+1373
-1083
lines changed

Some content is hidden

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

86 files changed

+1373
-1083
lines changed

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

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515

1616
package com.amazon.opendistroforelasticsearch.sql.analysis;
1717

18+
import com.amazon.opendistroforelasticsearch.sql.analysis.symbol.Namespace;
19+
import com.amazon.opendistroforelasticsearch.sql.analysis.symbol.Symbol;
1820
import com.amazon.opendistroforelasticsearch.sql.ast.AbstractNodeVisitor;
1921
import com.amazon.opendistroforelasticsearch.sql.ast.expression.Argument;
2022
import com.amazon.opendistroforelasticsearch.sql.ast.expression.Field;
@@ -82,7 +84,7 @@ public LogicalPlan visitRelation(Relation node, AnalysisContext context) {
8284
context.push();
8385
TypeEnvironment curEnv = context.peek();
8486
Table table = storageEngine.getTable(node.getTableName());
85-
table.getFieldTypes().forEach((k, v) -> curEnv.define(DSL.ref(k), v));
87+
table.getFieldTypes().forEach((k, v) -> curEnv.define(new Symbol(Namespace.FIELD_NAME, k), v));
8688
return new LogicalRelation(node.getTableName());
8789
}
8890

@@ -106,9 +108,10 @@ public LogicalPlan visitRename(Rename node, AnalysisContext context) {
106108
// We should define the new target field in the context instead of analyze it.
107109
if (renameMap.getTarget() instanceof Field) {
108110
ReferenceExpression target =
109-
new ReferenceExpression(((Field) renameMap.getTarget()).getField().toString());
110-
context.peek().define(target, origin.type(context.peek()));
111-
renameMapBuilder.put(DSL.ref(origin.toString()), target);
111+
new ReferenceExpression(((Field) renameMap.getTarget()).getField().toString(),
112+
origin.type());
113+
context.peek().define(target);
114+
renameMapBuilder.put(DSL.ref(origin.toString(), origin.type()), target);
112115
} else {
113116
throw new SemanticCheckException(
114117
String.format("the target expected to be field, but is %s", renameMap.getTarget()));
@@ -157,15 +160,15 @@ public LogicalPlan visitProject(Project node, AnalysisContext context) {
157160
if (exclude) {
158161
List<ReferenceExpression> referenceExpressions =
159162
node.getProjectList().stream()
160-
.map(expr -> (ReferenceExpression) expressionAnalyzer.analyze(expr, context))
163+
.map(expr -> (ReferenceExpression) expressionAnalyzer.analyze(expr, context))
161164
.collect(Collectors.toList());
162165
return new LogicalRemove(child, ImmutableSet.copyOf(referenceExpressions));
163166
}
164167
}
165168

166169
List<Expression> expressions = node.getProjectList().stream()
167-
.map(expr -> expressionAnalyzer.analyze(expr, context))
168-
.collect(Collectors.toList());
170+
.map(expr -> expressionAnalyzer.analyze(expr, context))
171+
.collect(Collectors.toList());
169172
return new LogicalProject(child, expressions);
170173
}
171174

@@ -178,12 +181,12 @@ public LogicalPlan visitEval(Eval node, AnalysisContext context) {
178181
ImmutableList.Builder<Pair<ReferenceExpression, Expression>> expressionsBuilder =
179182
new Builder<>();
180183
for (Let let : node.getExpressionList()) {
181-
ReferenceExpression ref = DSL.ref(let.getVar().getField().toString());
182184
Expression expression = expressionAnalyzer.analyze(let.getExpression(), context);
185+
ReferenceExpression ref = DSL.ref(let.getVar().getField().toString(), expression.type());
183186
expressionsBuilder.add(ImmutablePair.of(ref, expression));
184187
TypeEnvironment typeEnvironment = context.peek();
185188
// define the new reference in type env.
186-
typeEnvironment.define(ref, expression.type(typeEnvironment));
189+
typeEnvironment.define(ref);
187190
}
188191
return new LogicalEval(child, expressionsBuilder.build());
189192
}
@@ -239,8 +242,8 @@ public LogicalPlan visitValues(Values node, AnalysisContext context) {
239242
List<List<LiteralExpression>> valueExprs = new ArrayList<>();
240243
for (List<Literal> value : values) {
241244
valueExprs.add(value.stream()
242-
.map(val -> (LiteralExpression) expressionAnalyzer.analyze(val, context))
243-
.collect(Collectors.toList()));
245+
.map(val -> (LiteralExpression) expressionAnalyzer.analyze(val, context))
246+
.collect(Collectors.toList()));
244247
}
245248
return new LogicalValues(valueExprs);
246249
}

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

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515

1616
package com.amazon.opendistroforelasticsearch.sql.analysis;
1717

18+
import com.amazon.opendistroforelasticsearch.sql.analysis.symbol.Namespace;
19+
import com.amazon.opendistroforelasticsearch.sql.analysis.symbol.Symbol;
1820
import com.amazon.opendistroforelasticsearch.sql.ast.AbstractNodeVisitor;
1921
import com.amazon.opendistroforelasticsearch.sql.ast.expression.AggregateFunction;
2022
import com.amazon.opendistroforelasticsearch.sql.ast.expression.And;
@@ -63,18 +65,15 @@ public Expression analyze(UnresolvedExpression unresolved, AnalysisContext conte
6365

6466
@Override
6567
public Expression visitUnresolvedAttribute(UnresolvedAttribute node, AnalysisContext context) {
66-
TypeEnvironment typeEnv = context.peek();
67-
ReferenceExpression ref = DSL.ref(node.getAttr());
68-
typeEnv.resolve(ref);
69-
return ref;
68+
return visitIdentifier(node.getAttr(), context);
7069
}
7170

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

77-
return dsl.equal(context.peek(), left, right);
76+
return dsl.equal(left, right);
7877
}
7978

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

90-
return dsl.and(context.peek(), left, right);
89+
return dsl.and(left, right);
9190
}
9291

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

98-
return dsl.or(context.peek(), left, right);
97+
return dsl.or(left, right);
9998
}
10099

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

106-
return dsl.xor(context.peek(), left, right);
105+
return dsl.xor(left, right);
107106
}
108107

109108
@Override
110109
public Expression visitNot(Not node, AnalysisContext context) {
111-
return dsl.not(context.peek(), node.getExpression().accept(this, context));
110+
return dsl.not(node.getExpression().accept(this, context));
112111
}
113112

114113
@Override
@@ -118,7 +117,7 @@ public Expression visitAggregateFunction(AggregateFunction node, AnalysisContext
118117
Expression arg = node.getField().accept(this, context);
119118
return (Aggregator)
120119
repository.compile(
121-
builtinFunctionName.get().getName(), Collections.singletonList(arg), context.peek());
120+
builtinFunctionName.get().getName(), Collections.singletonList(arg));
122121
} else {
123122
throw new SemanticCheckException("Unsupported aggregation function " + node.getFuncName());
124123
}
@@ -131,7 +130,7 @@ public Expression visitFunction(Function node, AnalysisContext context) {
131130
node.getFuncArgs().stream()
132131
.map(unresolvedExpression -> analyze(unresolvedExpression, context))
133132
.collect(Collectors.toList());
134-
return (Expression) repository.compile(functionName, arguments, context.peek());
133+
return (Expression) repository.compile(functionName, arguments);
135134
}
136135

137136
@Override
@@ -140,15 +139,19 @@ public Expression visitCompare(Compare node, AnalysisContext context) {
140139
Expression left = analyze(node.getLeft(), context);
141140
Expression right = analyze(node.getRight(), context);
142141
return (Expression)
143-
repository.compile(functionName, Arrays.asList(left, right), context.peek());
142+
repository.compile(functionName, Arrays.asList(left, right));
144143
}
145144

146145
@Override
147146
public Expression visitField(Field node, AnalysisContext context) {
148147
String attr = node.getField().toString();
148+
return visitIdentifier(attr, context);
149+
}
150+
151+
private Expression visitIdentifier(String ident, AnalysisContext context) {
149152
TypeEnvironment typeEnv = context.peek();
150-
ReferenceExpression ref = DSL.ref(attr);
151-
typeEnv.resolve(ref);
153+
ReferenceExpression ref = DSL.ref(ident,
154+
typeEnv.resolve(new Symbol(Namespace.FIELD_NAME, ident)));
152155
return ref;
153156
}
154157
}

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

Lines changed: 23 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
import com.amazon.opendistroforelasticsearch.sql.analysis.symbol.Namespace;
1919
import com.amazon.opendistroforelasticsearch.sql.analysis.symbol.Symbol;
2020
import com.amazon.opendistroforelasticsearch.sql.analysis.symbol.SymbolTable;
21-
import com.amazon.opendistroforelasticsearch.sql.data.model.ExprType;
21+
import com.amazon.opendistroforelasticsearch.sql.data.type.ExprType;
2222
import com.amazon.opendistroforelasticsearch.sql.exception.SemanticCheckException;
2323
import com.amazon.opendistroforelasticsearch.sql.expression.Expression;
2424
import com.amazon.opendistroforelasticsearch.sql.expression.ReferenceExpression;
@@ -29,7 +29,7 @@
2929
/**
3030
* The definition of Type Environment.
3131
*/
32-
public class TypeEnvironment implements Environment<Expression, ExprType> {
32+
public class TypeEnvironment implements Environment<Symbol, ExprType> {
3333
@Getter
3434
private final TypeEnvironment parent;
3535
private final SymbolTable symbolTable;
@@ -47,38 +47,38 @@ public TypeEnvironment(TypeEnvironment parent, SymbolTable symbolTable) {
4747
/**
4848
* Resolve the {@link Expression} from environment.
4949
*
50-
* @param var expression
50+
* @param symbol Symbol
5151
* @return resolved {@link ExprType}
5252
*/
5353
@Override
54-
public ExprType resolve(Expression var) {
55-
if (var instanceof ReferenceExpression) {
56-
ReferenceExpression ref = (ReferenceExpression) var;
57-
for (TypeEnvironment cur = this; cur != null; cur = cur.parent) {
58-
Optional<ExprType> typeOptional = cur.symbolTable.lookup(new Symbol(Namespace.FIELD_NAME,
59-
ref.getAttr()));
60-
if (typeOptional.isPresent()) {
61-
return typeOptional.get();
62-
}
54+
public ExprType resolve(Symbol symbol) {
55+
for (TypeEnvironment cur = this; cur != null; cur = cur.parent) {
56+
Optional<ExprType> typeOptional = cur.symbolTable.lookup(symbol);
57+
if (typeOptional.isPresent()) {
58+
return typeOptional.get();
6359
}
6460
}
65-
throw new SemanticCheckException(String.format("can't resolve expression %s in type env", var));
61+
throw new SemanticCheckException(
62+
String.format("can't resolve %s in type env", symbol));
6663
}
6764

6865
/**
6966
* Define symbol with the type.
7067
*
71-
* @param var symbol to define
72-
* @param type type
68+
* @param symbol symbol to define
69+
* @param type type
7370
*/
74-
public void define(Expression var, ExprType type) {
75-
if (var instanceof ReferenceExpression) {
76-
ReferenceExpression ref = (ReferenceExpression) var;
77-
symbolTable.store(new Symbol(Namespace.FIELD_NAME, ref.getAttr()), type);
78-
} else {
79-
throw new IllegalArgumentException(
80-
String.format("only support define reference, unexpected expression %s", var));
81-
}
71+
public void define(Symbol symbol, ExprType type) {
72+
symbolTable.store(symbol, type);
73+
}
74+
75+
/**
76+
* Define expression with the type.
77+
*
78+
* @param ref {@link ReferenceExpression}
79+
*/
80+
public void define(ReferenceExpression ref) {
81+
define(new Symbol(Namespace.FIELD_NAME, ref.getAttr()), ref.type());
8282
}
8383

8484
}

core/src/main/java/com/amazon/opendistroforelasticsearch/sql/analysis/symbol/Symbol.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,12 @@
1717

1818
import lombok.Getter;
1919
import lombok.RequiredArgsConstructor;
20+
import lombok.ToString;
2021

2122
/**
2223
* Symbol in the scope.
2324
*/
25+
@ToString
2426
@Getter
2527
@RequiredArgsConstructor
2628
public class Symbol {

core/src/main/java/com/amazon/opendistroforelasticsearch/sql/analysis/symbol/SymbolTable.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
import static java.util.Collections.emptyMap;
1919
import static java.util.Collections.emptyNavigableMap;
2020

21-
import com.amazon.opendistroforelasticsearch.sql.data.model.ExprType;
21+
import com.amazon.opendistroforelasticsearch.sql.data.type.ExprType;
2222
import java.util.EnumMap;
2323
import java.util.Map;
2424
import java.util.NavigableMap;

core/src/main/java/com/amazon/opendistroforelasticsearch/sql/data/model/ExprBooleanValue.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515

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

18+
import com.amazon.opendistroforelasticsearch.sql.data.type.ExprCoreType;
1819
import lombok.EqualsAndHashCode;
1920

2021
@EqualsAndHashCode
@@ -42,8 +43,8 @@ public Object value() {
4243
}
4344

4445
@Override
45-
public ExprType type() {
46-
return ExprType.BOOLEAN;
46+
public ExprCoreType type() {
47+
return ExprCoreType.BOOLEAN;
4748
}
4849

4950
@Override

core/src/main/java/com/amazon/opendistroforelasticsearch/sql/data/model/ExprCollectionValue.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515

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

18+
import com.amazon.opendistroforelasticsearch.sql.data.type.ExprCoreType;
1819
import java.util.List;
1920
import java.util.stream.Collectors;
2021
import lombok.EqualsAndHashCode;
@@ -31,8 +32,8 @@ public Object value() {
3132
}
3233

3334
@Override
34-
public ExprType type() {
35-
return ExprType.ARRAY;
35+
public ExprCoreType type() {
36+
return ExprCoreType.ARRAY;
3637
}
3738

3839
@Override

core/src/main/java/com/amazon/opendistroforelasticsearch/sql/data/model/ExprDoubleValue.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515

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

18+
import com.amazon.opendistroforelasticsearch.sql.data.type.ExprCoreType;
1819
import lombok.EqualsAndHashCode;
1920
import lombok.RequiredArgsConstructor;
2021

@@ -29,8 +30,8 @@ public Object value() {
2930
}
3031

3132
@Override
32-
public ExprType type() {
33-
return ExprType.DOUBLE;
33+
public ExprCoreType type() {
34+
return ExprCoreType.DOUBLE;
3435
}
3536

3637
@Override

core/src/main/java/com/amazon/opendistroforelasticsearch/sql/data/model/ExprFloatValue.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515

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

18+
import com.amazon.opendistroforelasticsearch.sql.data.type.ExprCoreType;
1819
import lombok.EqualsAndHashCode;
1920
import lombok.RequiredArgsConstructor;
2021

@@ -29,8 +30,8 @@ public Object value() {
2930
}
3031

3132
@Override
32-
public ExprType type() {
33-
return ExprType.FLOAT;
33+
public ExprCoreType type() {
34+
return ExprCoreType.FLOAT;
3435
}
3536

3637
@Override

core/src/main/java/com/amazon/opendistroforelasticsearch/sql/data/model/ExprIntegerValue.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515

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

18+
import com.amazon.opendistroforelasticsearch.sql.data.type.ExprCoreType;
1819
import lombok.EqualsAndHashCode;
1920
import lombok.RequiredArgsConstructor;
2021

@@ -29,8 +30,8 @@ public Object value() {
2930
}
3031

3132
@Override
32-
public ExprType type() {
33-
return ExprType.INTEGER;
33+
public ExprCoreType type() {
34+
return ExprCoreType.INTEGER;
3435
}
3536

3637
@Override

0 commit comments

Comments
 (0)