Skip to content

Commit

Permalink
close RelOptPlanner API
Browse files Browse the repository at this point in the history
  • Loading branch information
xy2953396112 committed Sep 23, 2021
1 parent e8e0796 commit 22fd97c
Show file tree
Hide file tree
Showing 7 changed files with 193 additions and 158 deletions.
Expand Up @@ -16,8 +16,6 @@
*/
package org.apache.calcite.plan;

import org.apache.calcite.linq4j.function.Experimental;
import org.apache.calcite.plan.SubstitutionVisitor.UnifyRule;
import org.apache.calcite.plan.volcano.RelSubset;
import org.apache.calcite.rel.RelNode;
import org.apache.calcite.rel.metadata.RelMetadataProvider;
Expand Down Expand Up @@ -204,11 +202,6 @@ public boolean isRuleExcluded(RelOptRule rule) {
return this;
}

@Experimental
@Override public void addMaterializationRules(List<UnifyRule> rules) {
// ignore - this planner does not support materializations
}

@Override public void addMaterialization(RelOptMaterialization materialization) {
// ignore - this planner does not support materializations
}
Expand Down
Expand Up @@ -50,18 +50,28 @@
*/
public abstract class RelOptMaterializations {

@Deprecated // to be removed before 2.0
/**
* Returns a list of RelNode transformed from all possible combination of
* materialized view uses. Big queries will likely have more than one
* transformed RelNode, e.g., (t1 group by c1) join (t2 group by c2).
* @param rel the original RelNode
* @param materializations the materialized view list
* @return the list of transformed RelNode together with their corresponding
* materialized views used in the transformation.
*/
public static List<Pair<RelNode, List<RelOptMaterialization>>> useMaterializedViews(
final RelNode rel, List<RelOptMaterialization> materializations) {
return useMaterializedViews(rel, materializations, ImmutableList.of());
return useMaterializedViews(rel, materializations, SubstitutionVisitor.DEFAULT_RULES);
}

/**
* Returns a list of RelNode transformed from all possible combination of
* materialized view uses. Big queries will likely have more than one
* transformed RelNode, e.g., (t1 group by c1) join (t2 group by c2).
* In addition, you can add custom materialized view recognition rules.
* @param rel the original RelNode
* @param materializations the materialized view list
* @param materializationRules the materialized view recognition rules
* @return the list of transformed RelNode together with their corresponding
* materialized views used in the transformation.
*/
Expand Down
11 changes: 0 additions & 11 deletions core/src/main/java/org/apache/calcite/plan/RelOptPlanner.java
Expand Up @@ -16,7 +16,6 @@
*/
package org.apache.calcite.plan;

import org.apache.calcite.plan.SubstitutionVisitor.UnifyRule;
import org.apache.calcite.rel.RelNode;
import org.apache.calcite.rel.metadata.CachingRelMetadataProvider;
import org.apache.calcite.rel.metadata.RelMetadataProvider;
Expand Down Expand Up @@ -159,16 +158,6 @@ public interface RelOptPlanner {
*/
RelOptPlanner chooseDelegate();

/**
* In addition to the internal defined materialization matching rules in
* {@link org.apache.calcite.plan.MaterializedViewSubstitutionVisitor}
* and {@link org.apache.calcite.plan.SubstitutionVisitor}, this method
* allows user to SET self defined {@link SubstitutionVisitor.UnifyRule}s,
* thus to extend the ability of substitution based materialization
* matching in different scenarios.
*/
void addMaterializationRules(List<UnifyRule> rules);

/**
* Defines a pair of relational expressions that are equivalent.
*
Expand Down
Expand Up @@ -38,8 +38,6 @@
import org.apache.calcite.plan.RelTrait;
import org.apache.calcite.plan.RelTraitDef;
import org.apache.calcite.plan.RelTraitSet;
import org.apache.calcite.plan.SubstitutionVisitor;
import org.apache.calcite.plan.SubstitutionVisitor.UnifyRule;
import org.apache.calcite.rel.PhysicalNode;
import org.apache.calcite.rel.RelNode;
import org.apache.calcite.rel.convert.Converter;
Expand Down Expand Up @@ -175,8 +173,6 @@ public class VolcanoPlanner extends AbstractRelOptPlanner {
*/
private boolean noneConventionHasInfiniteCost = true;

private ImmutableList<UnifyRule> materializationRules = ImmutableList.of();

private final List<RelOptMaterialization> materializations =
new ArrayList<>();

Expand Down Expand Up @@ -298,10 +294,6 @@ public void setTopDownOpt(boolean value) {
return ImmutableList.copyOf(materializations);
}

@Override public void addMaterializationRules(List<UnifyRule> rules) {
materializationRules = ImmutableList.copyOf(rules);
}

@Override public void addMaterialization(
RelOptMaterialization materialization) {
materializations.add(materialization);
Expand All @@ -328,9 +320,7 @@ protected void registerMaterializations() {

// Register rels using materialized views.
final List<Pair<RelNode, List<RelOptMaterialization>>> materializationUses =
RelOptMaterializations.useMaterializedViews(originalRoot, materializations,
materializationRules.size() != 0 ? materializationRules
: SubstitutionVisitor.DEFAULT_RULES);
RelOptMaterializations.useMaterializedViews(originalRoot, materializations);
for (Pair<RelNode, List<RelOptMaterialization>> use : materializationUses) {
RelNode rel = use.left;
Hook.SUB.run(rel);
Expand Down
@@ -0,0 +1,179 @@
/*
* 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.calcite.materialize;

import org.apache.calcite.plan.RelOptMaterialization;
import org.apache.calcite.plan.RelOptMaterializations;
import org.apache.calcite.plan.RelOptUtil;
import org.apache.calcite.plan.RelTraitDef;
import org.apache.calcite.plan.SubstitutionVisitor;
import org.apache.calcite.plan.SubstitutionVisitor.UnifyRule;
import org.apache.calcite.rel.RelNode;
import org.apache.calcite.rel.mutable.MutableCalc;
import org.apache.calcite.rel.type.RelDataType;
import org.apache.calcite.rel.type.RelDataTypeFactory;
import org.apache.calcite.rex.RexCall;
import org.apache.calcite.rex.RexInputRef;
import org.apache.calcite.rex.RexLiteral;
import org.apache.calcite.rex.RexNode;
import org.apache.calcite.rex.RexUtil;
import org.apache.calcite.schema.SchemaPlus;
import org.apache.calcite.schema.impl.AbstractTable;
import org.apache.calcite.sql.SqlKind;
import org.apache.calcite.sql.fun.SqlStdOperatorTable;
import org.apache.calcite.sql.parser.SqlParser;
import org.apache.calcite.sql.type.SqlTypeName;
import org.apache.calcite.test.CalciteAssert;
import org.apache.calcite.test.SqlToRelTestBase;
import org.apache.calcite.tools.Frameworks;
import org.apache.calcite.tools.RelBuilder;
import org.apache.calcite.util.NlsString;
import org.apache.calcite.util.Pair;

import com.google.common.collect.ImmutableList;
import com.google.common.collect.Lists;

import org.junit.jupiter.api.Test;

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

import static org.apache.calcite.test.Matchers.isLinux;

import static org.hamcrest.MatcherAssert.assertThat;

/**
* Unit tests for {@link RelOptMaterializations#useMaterializedViews}.
*/
public class CustomMaterializedViewRecognitionRuleTest extends SqlToRelTestBase {

public static Frameworks.ConfigBuilder config() {
final SchemaPlus rootSchema = Frameworks.createRootSchema(true);
rootSchema.add("mv0", new AbstractTable() {
@Override public RelDataType getRowType(RelDataTypeFactory typeFactory) {
return typeFactory.builder()
.add("empno", SqlTypeName.INTEGER)
.add("ename", SqlTypeName.VARCHAR)
.add("job", SqlTypeName.VARCHAR)
.add("mgr", SqlTypeName.SMALLINT)
.add("hiredate", SqlTypeName.DATE)
.add("sal", SqlTypeName.DECIMAL)
.add("comm", SqlTypeName.DECIMAL)
.add("deptno", SqlTypeName.TINYINT)
.build();
}
});
return Frameworks.newConfigBuilder()
.parserConfig(SqlParser.Config.DEFAULT)
.defaultSchema(
CalciteAssert.addSchema(rootSchema, CalciteAssert.SchemaSpec.SCOTT_WITH_TEMPORAL))
.traitDefs((List<RelTraitDef>) null);
}

@Test void testCushionLikeOperatorRecognitionRule() {
final RelBuilder relBuilder = RelBuilder.create(config().build());
final RelNode query = relBuilder.scan("EMP")
.filter(
relBuilder.call(SqlStdOperatorTable.LIKE,
relBuilder.field(1), relBuilder.literal("ABCD%")))
.build();
final RelNode target = relBuilder.scan("EMP")
.filter(
relBuilder.call(SqlStdOperatorTable.LIKE,
relBuilder.field(1), relBuilder.literal("ABC%")))
.build();
final RelNode replacement = relBuilder.scan("mv0").build();
final RelOptMaterialization relOptMaterialization =
new RelOptMaterialization(replacement,
target, null, Lists.newArrayList("mv0"));
final List<UnifyRule> rules = new ArrayList<>();
rules.addAll(SubstitutionVisitor.DEFAULT_RULES);
rules.add(CustomizedMaterializationRule.INSTANCE);
final List<Pair<RelNode, List<RelOptMaterialization>>> relOptimized =
RelOptMaterializations.useMaterializedViews(query,
ImmutableList.of(relOptMaterialization), rules);
final String optimized = ""
+ "LogicalCalc(expr#0..7=[{inputs}], expr#8=['ABCD%'], expr#9=[LIKE($t1, $t8)], proj#0."
+ ".7=[{exprs}], $condition=[$t9])\n"
+ " LogicalProject(empno=[CAST($0):SMALLINT NOT NULL], ename=[CAST($1):VARCHAR(10)], "
+ "job=[CAST($2):VARCHAR(9)], mgr=[CAST($3):SMALLINT], hiredate=[CAST($4):DATE], "
+ "sal=[CAST($5):DECIMAL(7, 2)], comm=[CAST($6):DECIMAL(7, 2)], deptno=[CAST($7)"
+ ":TINYINT])\n"
+ " LogicalTableScan(table=[[mv0]])\n";
final String relOptimizedStr = RelOptUtil.toString(relOptimized.get(0).getKey());
assertThat(relOptimizedStr, isLinux(optimized));
}

/**
* A customized materialization rule, which match expression of 'LIKE'
* and match by compensation.
*/
private static class CustomizedMaterializationRule
extends SubstitutionVisitor.AbstractUnifyRule {

public static final CustomizedMaterializationRule INSTANCE =
new CustomizedMaterializationRule();

private CustomizedMaterializationRule() {
super(operand(MutableCalc.class, query(0)),
operand(MutableCalc.class, target(0)), 1);
}

@Override protected SubstitutionVisitor.UnifyResult apply(
SubstitutionVisitor.UnifyRuleCall call) {
final MutableCalc query = (MutableCalc) call.query;
final Pair<RexNode, List<RexNode>> queryExplained = SubstitutionVisitor.explainCalc(query);
final RexNode queryCond = queryExplained.left;
final List<RexNode> queryProjs = queryExplained.right;

final MutableCalc target = (MutableCalc) call.target;
final Pair<RexNode, List<RexNode>> targetExplained = SubstitutionVisitor.explainCalc(target);
final RexNode targetCond = targetExplained.left;
final List<RexNode> targetProjs = targetExplained.right;
final List parsedQ = parseLikeCondition(queryCond);
final List parsedT = parseLikeCondition(targetCond);
if (RexUtil.isIdentity(queryProjs, query.getInput().rowType)
&& RexUtil.isIdentity(targetProjs, target.getInput().rowType)
&& parsedQ != null && parsedT != null) {
if (parsedQ.get(0).equals(parsedT.get(0))) {
String literalQ = ((NlsString) parsedQ.get(1)).getValue();
String literalT = ((NlsString) parsedT.get(1)).getValue();
if (literalQ.endsWith("%") && literalT.endsWith("%")
&& !literalQ.equals(literalT)
&& literalQ.startsWith(literalT.substring(0, literalT.length() - 1))) {
return call.result(MutableCalc.of(target, query.program));
}
}
}
return null;
}

private List parseLikeCondition(RexNode rexNode) {
if (rexNode instanceof RexCall) {
RexCall rexCall = (RexCall) rexNode;
if (rexCall.getKind() == SqlKind.LIKE
&& rexCall.operands.get(0) instanceof RexInputRef
&& rexCall.operands.get(1) instanceof RexLiteral) {
return ImmutableList.of(rexCall.operands.get(0),
((RexLiteral) (rexCall.operands.get(1))).getValue());
}
}
return null;
}
}

}
Expand Up @@ -20,7 +20,6 @@
import org.apache.calcite.plan.RelOptMaterializations;
import org.apache.calcite.plan.RelOptUtil;
import org.apache.calcite.plan.RelTraitDef;
import org.apache.calcite.plan.SubstitutionVisitor;
import org.apache.calcite.rel.RelNode;
import org.apache.calcite.rel.core.AggregateCall;
import org.apache.calcite.rel.logical.LogicalAggregate;
Expand Down Expand Up @@ -98,7 +97,7 @@ public static Frameworks.ConfigBuilder config() {
target, null, Lists.newArrayList("mv0"));
final List<Pair<RelNode, List<RelOptMaterialization>>> relOptimized =
RelOptMaterializations.useMaterializedViews(query,
ImmutableList.of(relOptMaterialization), SubstitutionVisitor.DEFAULT_RULES);
ImmutableList.of(relOptMaterialization));

final String optimized = ""
+ "LogicalProject(deptno=[CAST($0):TINYINT], count_sal=[$1])\n"
Expand Down

0 comments on commit 22fd97c

Please sign in to comment.