Skip to content

Commit

Permalink
[Refactor](nereids)Refactor Literal to an inheritance hierarchy (apac…
Browse files Browse the repository at this point in the history
…he#10771)

Use inheritance hierarchy instead of combination to make the framework more clear
  • Loading branch information
Kikyou1997 authored Jul 18, 2022
1 parent d0dc936 commit bf95440
Show file tree
Hide file tree
Showing 14 changed files with 405 additions and 81 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,31 +27,26 @@
import org.apache.doris.analysis.IntLiteral;
import org.apache.doris.analysis.NullLiteral;
import org.apache.doris.analysis.StringLiteral;
import org.apache.doris.catalog.Type;
import org.apache.doris.nereids.exceptions.AnalysisException;
import org.apache.doris.nereids.trees.expressions.Alias;
import org.apache.doris.nereids.trees.expressions.Arithmetic;
import org.apache.doris.nereids.trees.expressions.Between;
import org.apache.doris.nereids.trees.expressions.BooleanLiteral;
import org.apache.doris.nereids.trees.expressions.CompoundPredicate;
import org.apache.doris.nereids.trees.expressions.DoubleLiteral;
import org.apache.doris.nereids.trees.expressions.EqualTo;
import org.apache.doris.nereids.trees.expressions.Expression;
import org.apache.doris.nereids.trees.expressions.ExpressionType;
import org.apache.doris.nereids.trees.expressions.GreaterThan;
import org.apache.doris.nereids.trees.expressions.GreaterThanEqual;
import org.apache.doris.nereids.trees.expressions.IntegerLiteral;
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.Not;
import org.apache.doris.nereids.trees.expressions.NullSafeEqual;
import org.apache.doris.nereids.trees.expressions.SlotReference;
import org.apache.doris.nereids.trees.expressions.functions.BoundFunction;
import org.apache.doris.nereids.trees.expressions.visitor.DefaultExpressionVisitor;
import org.apache.doris.nereids.types.BooleanType;
import org.apache.doris.nereids.types.DataType;
import org.apache.doris.nereids.types.DoubleType;
import org.apache.doris.nereids.types.IntegerType;
import org.apache.doris.nereids.types.NullType;
import org.apache.doris.nereids.types.StringType;

import java.util.ArrayList;
import java.util.List;
Expand Down Expand Up @@ -143,24 +138,31 @@ public Expr visitSlotReference(SlotReference slotReference, PlanTranslatorContex
return context.findSlotRef(slotReference.getExprId());
}

/**
* translate to stale literal.
*/
@Override
public Expr visitLiteral(Literal literal, PlanTranslatorContext context) {
DataType dataType = literal.getDataType();
if (dataType instanceof BooleanType) {
return new BoolLiteral((Boolean) literal.getValue());
} else if (dataType instanceof DoubleType) {
return new FloatLiteral((Double) literal.getValue(), Type.DOUBLE);
} else if (dataType instanceof IntegerType) {
return new IntLiteral((Integer) literal.getValue());
} else if (dataType instanceof NullType) {
return new NullLiteral();
} else if (dataType instanceof StringType) {
return new StringLiteral((String) literal.getValue());
}
throw new RuntimeException(String.format("Unsupported data type: %s", dataType.toString()));
public Expr visitBooleanLiteral(BooleanLiteral booleanLiteral, PlanTranslatorContext context) {
return new BoolLiteral(booleanLiteral.getValue());
}

@Override
public Expr visitStringLiteral(org.apache.doris.nereids.trees.expressions.StringLiteral stringLiteral,
PlanTranslatorContext context) {
return new StringLiteral(stringLiteral.getValue());
}

@Override
public Expr visitIntegerLiteral(IntegerLiteral integerLiteral, PlanTranslatorContext context) {
return new IntLiteral(integerLiteral.getValue());
}

@Override
public Expr visitNullLiteral(org.apache.doris.nereids.trees.expressions.NullLiteral nullLiteral,
PlanTranslatorContext context) {
return new NullLiteral();
}

@Override
public Expr visitDoubleLiteral(DoubleLiteral doubleLiteral, PlanTranslatorContext context) {
return new FloatLiteral(doubleLiteral.getValue());
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,11 +65,13 @@
import org.apache.doris.nereids.trees.expressions.Alias;
import org.apache.doris.nereids.trees.expressions.And;
import org.apache.doris.nereids.trees.expressions.Between;
import org.apache.doris.nereids.trees.expressions.BooleanLiteral;
import org.apache.doris.nereids.trees.expressions.Divide;
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.IntegerLiteral;
import org.apache.doris.nereids.trees.expressions.LessThan;
import org.apache.doris.nereids.trees.expressions.LessThanEqual;
import org.apache.doris.nereids.trees.expressions.Like;
Expand All @@ -78,9 +80,11 @@
import org.apache.doris.nereids.trees.expressions.Multiply;
import org.apache.doris.nereids.trees.expressions.NamedExpression;
import org.apache.doris.nereids.trees.expressions.Not;
import org.apache.doris.nereids.trees.expressions.NullLiteral;
import org.apache.doris.nereids.trees.expressions.NullSafeEqual;
import org.apache.doris.nereids.trees.expressions.Or;
import org.apache.doris.nereids.trees.expressions.Regexp;
import org.apache.doris.nereids.trees.expressions.StringLiteral;
import org.apache.doris.nereids.trees.expressions.Subtract;
import org.apache.doris.nereids.trees.plans.JoinType;
import org.apache.doris.nereids.trees.plans.logical.LogicalAggregate;
Expand Down Expand Up @@ -376,20 +380,20 @@ public UnboundSlot visitColumnReference(ColumnReferenceContext ctx) {
*/
@Override
public Expression visitNullLiteral(NullLiteralContext ctx) {
return new Literal(null);
return new NullLiteral();
}

@Override
public Literal visitBooleanLiteral(BooleanLiteralContext ctx) {
Boolean b = Boolean.valueOf(ctx.getText());
return new Literal(b);
return new BooleanLiteral(b);
}

@Override
public Literal visitIntegerLiteral(IntegerLiteralContext ctx) {
// TODO: throw NumberFormatException
Integer l = Integer.valueOf(ctx.getText());
return new Literal(l);
return new IntegerLiteral(l);
}

@Override
Expand All @@ -400,7 +404,7 @@ public Literal visitStringLiteral(StringLiteralContext ctx) {
.map(str -> str.substring(1, str.length() - 1))
.reduce((s1, s2) -> s1 + s2)
.orElse("");
return new Literal(s);
return new StringLiteral(s);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,9 @@
import org.apache.doris.nereids.rules.RuleType;
import org.apache.doris.nereids.rules.expression.rewrite.ExpressionRuleExecutor;
import org.apache.doris.nereids.rules.rewrite.OneRewriteRuleFactory;
import org.apache.doris.nereids.trees.expressions.BooleanLiteral;
import org.apache.doris.nereids.trees.expressions.ComparisonPredicate;
import org.apache.doris.nereids.trees.expressions.Expression;
import org.apache.doris.nereids.trees.expressions.Literal;
import org.apache.doris.nereids.trees.expressions.Slot;
import org.apache.doris.nereids.trees.expressions.visitor.SlotExtractor;
import org.apache.doris.nereids.trees.plans.GroupPlan;
Expand Down Expand Up @@ -71,7 +71,7 @@ public Rule<Plan> build() {
LogicalJoin<GroupPlan, GroupPlan> join = filter.child();

Expression wherePredicates = filter.getPredicates();
Expression onPredicates = Literal.TRUE_LITERAL;
Expression onPredicates = BooleanLiteral.TRUE;

List<Expression> otherConditions = Lists.newArrayList();
List<Expression> eqConditions = Lists.newArrayList();
Expand Down Expand Up @@ -126,11 +126,11 @@ private Plan pushDownPredicate(LogicalJoin<GroupPlan, GroupPlan> joinPlan,
ExpressionRuleExecutor exprRewriter = new ExpressionRuleExecutor();
Plan leftPlan = joinPlan.left();
Plan rightPlan = joinPlan.right();
if (!left.equals(Literal.TRUE_LITERAL)) {
if (!left.equals(BooleanLiteral.TRUE)) {
leftPlan = new LogicalFilter(exprRewriter.rewrite(left), leftPlan);
}

if (!right.equals(Literal.TRUE_LITERAL)) {
if (!right.equals(BooleanLiteral.TRUE)) {
rightPlan = new LogicalFilter(exprRewriter.rewrite(right), rightPlan);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
// Licensed to the Apache Software Foundation (ASF) under one
// or more contributor license agreements. See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership. The ASF licenses this file
// to you under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance
// with the License. You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License 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 org.apache.doris.nereids.trees.expressions;

import org.apache.doris.nereids.trees.expressions.visitor.ExpressionVisitor;
import org.apache.doris.nereids.types.BooleanType;


/**
* Represents Boolean literal
*/
public class BooleanLiteral extends Literal {

public static final BooleanLiteral TRUE = new BooleanLiteral(true);
public static final BooleanLiteral FALSE = new BooleanLiteral(false);

private final boolean value;

public BooleanLiteral(boolean value) {
super(BooleanType.INSTANCE);
this.value = value;
}

@Override
public Boolean getValue() {
return value;
}

@Override
public <R, C> R accept(ExpressionVisitor<R, C> visitor, C context) {
return visitor.visitBooleanLiteral(this, context);
}

@Override
public String toString() {
return Boolean.valueOf(value).toString().toUpperCase();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
// Licensed to the Apache Software Foundation (ASF) under one
// or more contributor license agreements. See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership. The ASF licenses this file
// to you under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance
// with the License. You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License 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 org.apache.doris.nereids.trees.expressions;

import org.apache.doris.nereids.trees.expressions.visitor.ExpressionVisitor;
import org.apache.doris.nereids.types.DoubleType;

/**
* Double literal
*/
public class DoubleLiteral extends Literal {

private final double value;

public DoubleLiteral(double value) {
super(DoubleType.INSTANCE);
this.value = value;
}

@Override
public Double getValue() {
return value;
}

@Override
public <R, C> R accept(ExpressionVisitor<R, C> visitor, C context) {
return visitor.visitDoubleLiteral(this, context);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
// Licensed to the Apache Software Foundation (ASF) under one
// or more contributor license agreements. See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership. The ASF licenses this file
// to you under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance
// with the License. You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License 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 org.apache.doris.nereids.trees.expressions;

import org.apache.doris.nereids.trees.expressions.visitor.ExpressionVisitor;
import org.apache.doris.nereids.types.IntegerType;

/**
* Represents Integer literal
*/
public class IntegerLiteral extends Literal {

private final int value;

public IntegerLiteral(int value) {
super(IntegerType.INSTANCE);
this.value = value;
}

@Override
public Integer getValue() {
return value;
}

@Override
public <R, C> R accept(ExpressionVisitor<R, C> visitor, C context) {
return visitor.visitIntegerLiteral(this, context);
}
}
Loading

0 comments on commit bf95440

Please sign in to comment.