Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Feature](Nereids) Data structure of comparison predicate #9506

Merged
merged 5 commits into from
May 16, 2022
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 @@ -371,8 +371,7 @@ ZONE: 'ZONE';

EQ : '=' | '==';
NSEQ: '<=>';
NEQ : '<>';
NEQJ: '!=';
NEQ : '<>' | '!=';
LT : '<';
LTE : '<=' | '!>';
GT : '>';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ constant
;

comparisonOperator
: EQ | NEQ | NEQJ | LT | LTE | GT | GTE | NSEQ
: EQ | NEQ | LT | LTE | GT | GTE | NSEQ
;

booleanValue
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,8 @@
/**
* Expression for unbound alias.
*/
public class UnboundAlias<CHILD_TYPE extends Expression>
extends UnaryExpression<UnboundAlias<CHILD_TYPE>, CHILD_TYPE>
implements NamedExpression<UnboundAlias<CHILD_TYPE>> {
public class UnboundAlias<CHILD_TYPE extends Expression> extends NamedExpression<UnboundAlias<CHILD_TYPE>>
implements UnaryExpression<UnboundAlias<CHILD_TYPE>, CHILD_TYPE> {

public UnboundAlias(CHILD_TYPE child) {
super(NodeType.UNBOUND_ALIAS, child);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
/**
* Star expression.
*/
public class UnboundStar extends LeafExpression<UnboundStar> implements NamedExpression<UnboundStar> {
public class UnboundStar extends NamedExpression<UnboundStar> implements LeafExpression<UnboundStar> {
private final List<String> target;

public UnboundStar(List<String> target) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,17 +50,24 @@
import org.apache.doris.nereids.analyzer.UnboundSlot;
import org.apache.doris.nereids.analyzer.UnboundStar;
import org.apache.doris.nereids.trees.expressions.Alias;
import org.apache.doris.nereids.trees.expressions.BinaryPredicate;
import org.apache.doris.nereids.trees.expressions.EqualTo;
import org.apache.doris.nereids.trees.expressions.Expression;
import org.apache.doris.nereids.trees.expressions.GreaterThan;
import org.apache.doris.nereids.trees.expressions.GreaterThanEqual;
import org.apache.doris.nereids.trees.expressions.LessThan;
import org.apache.doris.nereids.trees.expressions.LessThanEqual;
import org.apache.doris.nereids.trees.expressions.Literal;
import org.apache.doris.nereids.trees.expressions.NamedExpression;
import org.apache.doris.nereids.trees.expressions.Not;
import org.apache.doris.nereids.trees.expressions.NullSafeEqual;
import org.apache.doris.nereids.trees.plans.JoinType;
import org.apache.doris.nereids.trees.plans.logical.LogicalFilter;
import org.apache.doris.nereids.trees.plans.logical.LogicalJoin;
import org.apache.doris.nereids.trees.plans.logical.LogicalPlan;
import org.apache.doris.nereids.trees.plans.logical.LogicalProject;

import com.clearspring.analytics.util.Lists;
import com.google.common.collect.Lists;

import org.antlr.v4.runtime.ParserRuleContext;
import org.antlr.v4.runtime.RuleContext;
import org.antlr.v4.runtime.tree.ParseTree;
Expand All @@ -74,9 +81,9 @@
import java.util.stream.Collectors;

/**
* Build an AST that consisting of logical plans.
* Build an logical plan tree with unbounded nodes.
*/
public class AstBuilder extends DorisParserBaseVisitor<Object> {
public class LogicalPlanBuilder extends DorisParserBaseVisitor<Object> {

/**
* Create a logical plan using a where clause.
Expand Down Expand Up @@ -356,20 +363,19 @@ public Expression visitComparison(ComparisonContext ctx) {
TerminalNode operator = (TerminalNode) ctx.comparisonOperator().getChild(0);
switch (operator.getSymbol().getType()) {
case DorisParser.EQ:
return new BinaryPredicate(left, right, BinaryPredicate.Operator.EQ);
case DorisParser.NSEQ:
return new BinaryPredicate(left, right, BinaryPredicate.Operator.NSEQ);
return new EqualTo(left, right);
case DorisParser.NEQ:
return new Not(new EqualTo(left, right));
case DorisParser.LT:
return new BinaryPredicate(left, right, BinaryPredicate.Operator.LT);
return new LessThan(left, right);
case DorisParser.GT:
return new BinaryPredicate(left, right, BinaryPredicate.Operator.GT);
return new GreaterThan(left, right);
case DorisParser.LTE:
return new BinaryPredicate(left, right, BinaryPredicate.Operator.LE);
return new LessThanEqual(left, right);
case DorisParser.GTE:
return new BinaryPredicate(left, right, BinaryPredicate.Operator.GE);
case DorisParser.NEQ:
case DorisParser.NEQJ:
return new BinaryPredicate(left, right, BinaryPredicate.Operator.EQ);
return new GreaterThanEqual(left, right);
case DorisParser.NSEQ:
return new NullSafeEqual(left, right);
default:
return null;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ public LogicalPlan parse(String sql) throws Exception {
tree = parser.singleStatement();
}

AstBuilder astBuilder = new AstBuilder();
return (LogicalPlan) astBuilder.visit(tree);
LogicalPlanBuilder logicalPlanBuilder = new LogicalPlanBuilder();
return (LogicalPlan) logicalPlanBuilder.visit(tree);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,7 @@
import org.apache.doris.nereids.trees.NodeType;
import org.apache.doris.nereids.trees.TreeNode;
import org.apache.doris.nereids.trees.expressions.Alias;
import org.apache.doris.nereids.trees.expressions.BinaryPredicate;
import org.apache.doris.nereids.trees.expressions.BinaryPredicate.Operator;
import org.apache.doris.nereids.trees.expressions.EqualTo;
import org.apache.doris.nereids.trees.expressions.Expression;
import org.apache.doris.nereids.trees.expressions.Literal;
import org.apache.doris.nereids.trees.expressions.SlotReference;
Expand Down Expand Up @@ -329,38 +328,28 @@ default PatternDescriptor<SlotReference, Expression> slotReference() {
}

/**
* create a binaryPredicate pattern.
* TODO create a ComparisonPredicate pattern.
*/
default PatternDescriptor<BinaryPredicate<Expression, Expression>, Expression> binaryPredicate() {
return new PatternDescriptor<>(
new Pattern<>(NodeType.BINARY_PREDICATE),
defaultPromise()
);
}

/**
* create a binaryPredicate pattern with operator type.
* TODO create a ComparisonPredicate pattern with children patterns.
*/
default PatternDescriptor<BinaryPredicate<Expression, Expression>, Expression> binaryPredicate(Operator operator) {
return new PatternDescriptor<BinaryPredicate<Expression, Expression>, Expression>(
new Pattern<>(NodeType.BINARY_PREDICATE),
defaultPromise()
).when(p -> p.getOperator() == operator);

/**
* create a equal to predicate pattern.
*/
default PatternDescriptor<EqualTo<Expression, Expression>, Expression> equalTo() {
return new PatternDescriptor<>(new Pattern<>(NodeType.EQUAL_TO), defaultPromise());
}

/**
* create a binaryPredicate pattern with children patterns.
* create a equal to predicate pattern with children patterns.
*/
default <C1 extends Expression, C2 extends Expression> PatternDescriptor<BinaryPredicate<C1, C2>, Expression>
binaryPredicate(PatternDescriptor<C1, Expression> leftChildPattern,
PatternDescriptor<C2, Expression> rightChildPattern) {
default <C1 extends Expression, C2 extends Expression> PatternDescriptor<EqualTo<C1, C2>, Expression> equalTo(
PatternDescriptor<C1, Expression> leftChildPattern, PatternDescriptor<C2, Expression> rightChildPattern) {
return new PatternDescriptor<>(
new Pattern<>(NodeType.BINARY_PREDICATE,
leftChildPattern.pattern,
rightChildPattern.pattern
),
defaultPromise()
);
new Pattern<>(NodeType.EQUAL_TO, leftChildPattern.pattern, rightChildPattern.pattern),
defaultPromise());
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,14 @@ public enum NodeType {
UNBOUND_STAR,
LITERAL,
SLOT_REFERENCE,
BINARY_PREDICATE,
COMPARISON_PREDICATE,
EmmyMiao87 marked this conversation as resolved.
Show resolved Hide resolved
EQUAL_TO,
LESS_THAN,
GREATER_THAN,
LESS_THAN_EQUAL,
GREATER_THAN_EQUAL,
NULL_SAFE_EQUAL,
NOT,
ALIAS,

// pattern
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,16 +20,15 @@
import org.apache.doris.nereids.exceptions.UnboundException;
import org.apache.doris.nereids.trees.NodeType;

import com.clearspring.analytics.util.Lists;
import com.google.common.collect.Lists;

import java.util.List;

/**
* Expression for alias, such as col1 as c1.
*/
public class Alias<CHILD_TYPE extends Expression>
extends UnaryExpression<Alias<CHILD_TYPE>, CHILD_TYPE>
implements NamedExpression<Alias<CHILD_TYPE>> {
public class Alias<CHILD_TYPE extends Expression> extends NamedExpression<Alias<CHILD_TYPE>>
implements UnaryExpression<Alias<CHILD_TYPE>, CHILD_TYPE> {

private final ExprId exprId;
private final String name;
Expand All @@ -43,7 +42,7 @@ public class Alias<CHILD_TYPE extends Expression>
*/
public Alias(CHILD_TYPE child, String name) {
super(NodeType.ALIAS, child);
exprId = NamedExpressionUtils.newExprId();
exprId = NamedExpressionUtil.newExprId();
this.name = name;
qualifier = Lists.newArrayList();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,26 +21,12 @@
import org.apache.doris.nereids.trees.NodeType;

/**
* Abstract class for all expression that have two children.
* Interface for all expression that have two children.
*/
public abstract class BinaryExpression<
public interface BinaryExpression<
EXPR_TYPE extends BinaryExpression<EXPR_TYPE, LEFT_CHILD_TYPE, RIGHT_CHILD_TYPE>,
LEFT_CHILD_TYPE extends Expression,
RIGHT_CHILD_TYPE extends Expression>
extends AbstractExpression<EXPR_TYPE>
implements BinaryNode<EXPR_TYPE, LEFT_CHILD_TYPE, RIGHT_CHILD_TYPE> {
extends BinaryNode<EXPR_TYPE, LEFT_CHILD_TYPE, RIGHT_CHILD_TYPE> {

public BinaryExpression(NodeType type, LEFT_CHILD_TYPE left, RIGHT_CHILD_TYPE right) {
super(type, left, right);
}

@Override
public LEFT_CHILD_TYPE left() {
return BinaryNode.super.left();
}

@Override
public RIGHT_CHILD_TYPE right() {
return BinaryNode.super.right();
}
}

This file was deleted.

Loading