Skip to content
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 @@ -23,6 +23,7 @@
import org.apache.calcite.plan.hep.HepPlanner;
import org.apache.calcite.plan.hep.HepProgram;
import org.apache.calcite.plan.hep.HepProgramBuilder;
import org.apache.calcite.plan.hep.HepRelVertex;
import org.apache.calcite.rel.RelNode;
import org.apache.calcite.rel.core.Aggregate;
import org.apache.calcite.rel.core.AggregateCall;
Expand Down Expand Up @@ -259,6 +260,10 @@ public abstract class MaterializedViewAggregateRule<C extends MaterializedViewAg
HepProgram unionRewritingPullProgram = config.unionRewritingPullProgram();
if (unionRewritingPullProgram != null) {
final HepPlanner tmpPlanner = new HepPlanner(unionRewritingPullProgram);
if (newAggregateInput instanceof HepRelVertex) {
// We cannot apply a new HepPlanner on a previous HepRelVertex tree, we must strip first
newAggregateInput = RelOptUtil.stripAll(newAggregateInput);
}
tmpPlanner.setRoot(newAggregateInput);
newAggregateInput = tmpPlanner.findBestExp();
target = newAggregateInput.getInput(0);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,10 @@
*/
package org.apache.calcite.rel.rules.materialize;

import org.apache.calcite.plan.RelOptUtil;
import org.apache.calcite.plan.hep.HepPlanner;
import org.apache.calcite.plan.hep.HepProgram;
import org.apache.calcite.plan.hep.HepRelVertex;
import org.apache.calcite.rel.RelNode;
import org.apache.calcite.rel.core.JoinRelType;
import org.apache.calcite.rel.core.Project;
Expand Down Expand Up @@ -168,8 +170,11 @@ public abstract class MaterializedViewJoinRule<C extends MaterializedViewRule.Co
RelNode target = node;
HepProgram unionRewritingPullProgram = config.unionRewritingPullProgram();
if (unionRewritingPullProgram != null) {
final HepPlanner tmpPlanner =
new HepPlanner(unionRewritingPullProgram);
final HepPlanner tmpPlanner = new HepPlanner(unionRewritingPullProgram);
if (newNode.getInput(0) instanceof HepRelVertex) {
// We cannot apply a new HepPlanner on a previous HepRelVertex tree, we must strip first
newNode = RelOptUtil.stripAll(newNode);
}
tmpPlanner.setRoot(newNode);
newNode = tmpPlanner.findBestExp();
target = newNode.getInput(0);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,14 @@
import org.apache.calcite.plan.RelOptPlanner;
import org.apache.calcite.plan.RelOptUtil;
import org.apache.calcite.plan.RelTraitSet;
import org.apache.calcite.plan.hep.HepPlanner;
import org.apache.calcite.plan.hep.HepProgram;
import org.apache.calcite.plan.hep.HepProgramBuilder;
import org.apache.calcite.rel.RelNode;
import org.apache.calcite.rel.rules.materialize.MaterializedViewOnlyAggregateRule;
import org.apache.calcite.rel.rules.materialize.MaterializedViewOnlyFilterRule;
import org.apache.calcite.rel.rules.materialize.MaterializedViewRules;
import org.apache.calcite.tools.Program;
import org.apache.calcite.tools.Programs;
import org.apache.calcite.util.Pair;

Expand Down Expand Up @@ -540,6 +547,59 @@ protected final MaterializedViewFixture sql(String materialize,
.ok();
}

/** Test case for
* <a href="https://issues.apache.org/jira/browse/CALCITE-7641">[CALCITE-7641]
* Materialize view rules with UnionRewritingPullProgram on a HepPlanner
* throws IllegalArgumentException</a>. */
@Test void testJoinAggregateMaterializationNoAggregateFuncs9Hep() {
// Tester using a HepPlanner instead of Volcano
final MaterializedViewTester hepTester =
new MaterializedViewTester() {
@Override protected List<RelNode> optimize(RelNode queryRel,
List<RelOptMaterialization> materializationList) {
// Dummy UnionRewritingPullProgram
final HepProgram unionRewritingPullProgram = new HepProgramBuilder().build();
// MaterializedViewRule with UnionRewritingPullProgram
final HepProgram mainProgram = new HepProgramBuilder()
.addRuleInstance(MaterializedViewOnlyAggregateRule.Config.DEFAULT
.withUnionRewritingPullProgram(unionRewritingPullProgram).toRule())
.build();
final HepPlanner hepPlanner = new HepPlanner(mainProgram);
final Program program =
(planner, rel, requiredOutputTraits, materializations, lattices) -> {
for (RelOptMaterialization materialization : materializations) {
planner.addMaterialization(materialization);
}
planner.setRoot(rel);
return planner.findBestExp();
};
return ImmutableList.of(
program.run(hepPlanner, queryRel, queryRel.getCluster().traitSet(),
materializationList, ImmutableList.of()));
}
};

String materialize = "select \"depts\".\"deptno\", \"dependents\".\"empid\"\n"
+ "from \"depts\"\n"
+ "join \"dependents\" on (\"depts\".\"name\" = \"dependents\".\"name\")\n"
+ "join \"locations\" on (\"locations\".\"name\" = \"dependents\".\"name\")\n"
+ "join \"emps\" on (\"emps\".\"deptno\" = \"depts\".\"deptno\")\n"
+ "where \"depts\".\"deptno\" > 11 and \"depts\".\"deptno\" < 19\n"
+ "group by \"depts\".\"deptno\", \"dependents\".\"empid\"";
String query = "select \"dependents\".\"empid\"\n"
+ "from \"depts\"\n"
+ "join \"dependents\" on (\"depts\".\"name\" = \"dependents\".\"name\")\n"
+ "join \"locations\" on (\"locations\".\"name\" = \"dependents\".\"name\")\n"
+ "join \"emps\" on (\"emps\".\"deptno\" = \"depts\".\"deptno\")\n"
+ "where \"depts\".\"deptno\" > 10 and \"depts\".\"deptno\" < 20\n"
+ "group by \"dependents\".\"empid\"";

MaterializedViewFixture.create(query, hepTester)
.withMaterializations(ImmutableList.of(Pair.of(materialize, "MV0")))
.checkingThatResultContains("EnumerableTableScan(table=[[hr, MV0]])")
.ok();
}

@Test void testJoinAggregateMaterializationNoAggregateFuncs10() {
sql("select \"depts\".\"name\", \"dependents\".\"name\" as \"name2\", "
+ "\"emps\".\"deptno\", \"depts\".\"deptno\" as \"deptno2\", "
Expand Down Expand Up @@ -950,6 +1010,58 @@ protected final MaterializedViewFixture sql(String materialize,
.ok();
}

/** Test case for
* <a href="https://issues.apache.org/jira/browse/CALCITE-7641">[CALCITE-7641]
* Materialize view rules with UnionRewritingPullProgram on a HepPlanner
* throws IllegalArgumentException</a>. */
@Test void testJoinMaterialization10Hep() {
// Tester using a HepPlanner instead of Volcano
final MaterializedViewTester hepTester =
new MaterializedViewTester() {
@Override protected List<RelNode> optimize(RelNode queryRel,
List<RelOptMaterialization> materializationList) {
// Dummy UnionRewritingPullProgram
final HepProgram unionRewritingPullProgram = new HepProgramBuilder().build();
final HepProgram mainProgram = new HepProgramBuilder()
.addRuleInstance(MaterializedViewRules.JOIN)
.addRuleInstance(MaterializedViewRules.PROJECT_JOIN)
.addRuleInstance(MaterializedViewRules.PROJECT_FILTER)
// MaterializedViewOnlyFilterRule with UnionRewritingPullProgram
.addRuleInstance(MaterializedViewOnlyFilterRule.Config.DEFAULT
.withUnionRewritingPullProgram(unionRewritingPullProgram).toRule())
.build();
final HepPlanner hepPlanner = new HepPlanner(mainProgram);
final Program program =
(planner, rel, requiredOutputTraits, materializations, lattices) -> {
for (RelOptMaterialization materialization : materializations) {
planner.addMaterialization(materialization);
}
planner.setRoot(rel);
return planner.findBestExp();
};
return ImmutableList.of(
program.run(hepPlanner, queryRel, queryRel.getCluster().traitSet(),
materializationList, ImmutableList.of()));
}
};

String materialize = "select \"depts\".\"deptno\", \"dependents\".\"empid\"\n"
+ "from \"depts\"\n"
+ "join \"dependents\" on (\"depts\".\"name\" = \"dependents\".\"name\")\n"
+ "join \"emps\" on (\"emps\".\"deptno\" = \"depts\".\"deptno\")\n"
+ "where \"depts\".\"deptno\" > 30";
String query = "select \"dependents\".\"empid\"\n"
+ "from \"depts\"\n"
+ "join \"dependents\" on (\"depts\".\"name\" = \"dependents\".\"name\")\n"
+ "join \"emps\" on (\"emps\".\"deptno\" = \"depts\".\"deptno\")\n"
+ "where \"depts\".\"deptno\" > 10";

MaterializedViewFixture.create(query, hepTester)
.withMaterializations(ImmutableList.of(Pair.of(materialize, "MV0")))
.checkingThatResultContains("EnumerableTableScan(table=[[hr, MV0]])")
.ok();
}

@Test void testJoinMaterialization11() {
sql("select \"empid\" from \"emps\"\n"
+ "join \"depts\" using (\"deptno\")",
Expand Down
Loading