Skip to content

Commit

Permalink
[feature](nereids) Materialized view rewrite basic util implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
seawinde committed Nov 24, 2023
1 parent 11912b9 commit c443db7
Show file tree
Hide file tree
Showing 18 changed files with 1,047 additions and 174 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

package org.apache.doris.nereids.rules.exploration.mv;

import org.apache.doris.nereids.rules.exploration.mv.mapping.RelationMapping;
import org.apache.doris.nereids.trees.expressions.NamedExpression;
import org.apache.doris.nereids.trees.plans.Plan;
import org.apache.doris.nereids.trees.plans.logical.LogicalProject;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,10 @@
import org.apache.doris.catalog.TableIf;
import org.apache.doris.nereids.CascadesContext;
import org.apache.doris.nereids.memo.Group;
import org.apache.doris.nereids.rules.exploration.mv.Mapping.ExpressionIndexMapping;
import org.apache.doris.nereids.rules.exploration.mv.Predicates.SplitPredicate;
import org.apache.doris.nereids.rules.exploration.mv.mapping.ExpressionIndexMapping;
import org.apache.doris.nereids.rules.exploration.mv.mapping.RelationMapping;
import org.apache.doris.nereids.rules.exploration.mv.mapping.SlotMapping;
import org.apache.doris.nereids.trees.expressions.Expression;
import org.apache.doris.nereids.trees.expressions.NamedExpression;
import org.apache.doris.nereids.trees.plans.Plan;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,11 +56,11 @@ public void addEquivalenceClass(SlotReference slot0, SlotReference slot1) {
equivalenceSlotMap.put(newRef, slot0Sets);
}
} else if (slot0Sets != null) {
// p1 present, we need to merge into it
// slot0Sets present, we need to merge into it
slot0Sets.add(slot1);
equivalenceSlotMap.put(slot1, slot0Sets);
} else if (slot1Sets != null) {
// p2 present, we need to merge into it
// slot1Sets present, we need to merge into it
slot1Sets.add(slot0);
equivalenceSlotMap.put(slot0, slot1Sets);
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@

import org.apache.doris.nereids.trees.expressions.Expression;
import org.apache.doris.nereids.trees.expressions.literal.BooleanLiteral;
import org.apache.doris.nereids.trees.expressions.visitor.ExpressionVisitors.PredicatesSpliter;
import org.apache.doris.nereids.util.ExpressionUtils;

import com.google.common.collect.ImmutableList;
Expand Down Expand Up @@ -55,8 +54,7 @@ public Expression composedExpression() {
* Split the expression to equal, range and residual predicate.
* */
public static SplitPredicate splitPredicates(Expression expression) {
PredicatesSpliter predicatesSplit = new PredicatesSpliter(expression);
expression.accept(predicatesSplit, null);
PredicatesSplitter predicatesSplit = new PredicatesSplitter(expression);
return predicatesSplit.getSplitPredicate();
}

Expand All @@ -74,15 +72,15 @@ public SplitPredicate(Expression equalPredicates, Expression rangePredicates, Ex
this.residualPredicates = residualPredicates;
}

public Expression getEqualPredicates() {
public Expression getEqualPredicate() {
return equalPredicates;
}

public Expression getRangePredicates() {
public Expression getRangePredicate() {
return rangePredicates;
}

public Expression getResidualPredicates() {
public Expression getResidualPredicate() {
return residualPredicates;
}

Expand All @@ -108,10 +106,6 @@ public boolean isEmpty() {
&& residualPredicates == null;
}

public Expression composedExpression() {
return ExpressionUtils.and(equalPredicates, rangePredicates, residualPredicates);
}

public List<Expression> toList() {
return ImmutableList.of(equalPredicates, rangePredicates, residualPredicates);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
// 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.rules.exploration.mv;

import org.apache.doris.nereids.trees.expressions.Alias;
import org.apache.doris.nereids.trees.expressions.Cast;
import org.apache.doris.nereids.trees.expressions.ComparisonPredicate;
import org.apache.doris.nereids.trees.expressions.CompoundPredicate;
import org.apache.doris.nereids.trees.expressions.EqualTo;
import org.apache.doris.nereids.trees.expressions.Expression;
import org.apache.doris.nereids.trees.expressions.NullSafeEqual;
import org.apache.doris.nereids.trees.expressions.Or;
import org.apache.doris.nereids.trees.expressions.SlotReference;
import org.apache.doris.nereids.trees.expressions.literal.Literal;
import org.apache.doris.nereids.trees.expressions.visitor.DefaultExpressionVisitor;
import org.apache.doris.nereids.util.ExpressionUtils;

import java.util.ArrayList;
import java.util.List;

/**
* Split the expression to equal, range and residual predicate.
* Should instance when used.
*/
public class PredicatesSplitter {

private List<Expression> equalPredicates = new ArrayList<>();
private List<Expression> rangePredicates = new ArrayList<>();
private List<Expression> residualPredicates = new ArrayList<>();
private List<Expression> conjunctExpressions;

private final PredicateExtract instance = new PredicateExtract();

public PredicatesSplitter(Expression target) {
this.conjunctExpressions = ExpressionUtils.extractConjunction(target);
for (Expression expression : conjunctExpressions) {
expression.accept(instance, expression);
}
}

/**PredicateExtract*/
public class PredicateExtract extends DefaultExpressionVisitor<Void, Expression> {

@Override
public Void visitComparisonPredicate(ComparisonPredicate comparisonPredicate, Expression sourceExpression) {
Expression leftArg = comparisonPredicate.getArgument(0);
Expression rightArg = comparisonPredicate.getArgument(1);
boolean leftArgOnlyContainsColumnRef = containOnlyColumnRef(leftArg, true);
boolean rightArgOnlyContainsColumnRef = containOnlyColumnRef(rightArg, true);
if (comparisonPredicate instanceof EqualTo || comparisonPredicate instanceof NullSafeEqual) {
if (leftArgOnlyContainsColumnRef && rightArgOnlyContainsColumnRef) {
equalPredicates.add(comparisonPredicate);
return null;
}
} else if ((leftArgOnlyContainsColumnRef && rightArg instanceof Literal)
|| (rightArgOnlyContainsColumnRef && leftArg instanceof Literal)) {
rangePredicates.add(comparisonPredicate);
} else {
residualPredicates.add(comparisonPredicate);
}
return null;
}

@Override
public Void visitCompoundPredicate(CompoundPredicate compoundPredicate, Expression context) {
if (compoundPredicate instanceof Or) {
residualPredicates.add(compoundPredicate);
return null;
}
return super.visitCompoundPredicate(compoundPredicate, context);
}
}

public Predicates.SplitPredicate getSplitPredicate() {
return Predicates.SplitPredicate.of(
equalPredicates.isEmpty() ? null : ExpressionUtils.and(equalPredicates),
rangePredicates.isEmpty() ? null : ExpressionUtils.and(rangePredicates),
residualPredicates.isEmpty() ? null : ExpressionUtils.and(residualPredicates));
}

private static boolean containOnlyColumnRef(Expression expression, boolean allowCast) {
if (expression instanceof SlotReference && ((SlotReference) expression).isColumnFromTable()) {
return true;
}
if (allowCast && expression instanceof Cast) {
return containOnlyColumnRef(((Cast) expression).child(), true);
}
if (expression instanceof Alias) {
return containOnlyColumnRef(((Alias) expression).child(), true);
}
return false;
}
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ private StructInfo(List<CatalogRelation> relations,
// construct equivalenceClass according to equals predicates
this.equivalenceClass = new EquivalenceClass();
SplitPredicate splitPredicate = Predicates.splitPredicates(predicates.composedExpression());
for (Expression expression : ExpressionUtils.extractConjunction(splitPredicate.getEqualPredicates())) {
for (Expression expression : ExpressionUtils.extractConjunction(splitPredicate.getEqualPredicate())) {
EqualTo equalTo = (EqualTo) expression;
equivalenceClass.addEquivalenceClass(
(SlotReference) equalTo.getArguments().get(0),
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
// 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.rules.exploration.mv.mapping;

import org.apache.doris.nereids.trees.expressions.Expression;

import com.google.common.collect.ArrayListMultimap;
import com.google.common.collect.Multimap;

import java.util.List;

/**
* Expression and it's index mapping
*/
public class ExpressionIndexMapping extends Mapping {
private final Multimap<Expression, Integer> expressionIndexMapping;

public ExpressionIndexMapping(Multimap<Expression, Integer> expressionIndexMapping) {
this.expressionIndexMapping = expressionIndexMapping;
}

public Multimap<Expression, Integer> getExpressionIndexMapping() {
return expressionIndexMapping;
}

public static ExpressionIndexMapping generate(List<? extends Expression> expressions) {
Multimap<Expression, Integer> expressionIndexMapping = ArrayListMultimap.create();
for (int i = 0; i < expressions.size(); i++) {
expressionIndexMapping.put(expressions.get(i), i);
}
return new ExpressionIndexMapping(expressionIndexMapping);
}
}
Loading

0 comments on commit c443db7

Please sign in to comment.