org.apache.calcite.plan.RelOptMaterialization Java Examples

The following examples show how to use org.apache.calcite.plan.RelOptMaterialization. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: Programs.java    From Bats with Apache License 2.0 6 votes vote down vote up
public RelNode run(RelOptPlanner planner, RelNode rel,
    RelTraitSet requiredOutputTraits,
    List<RelOptMaterialization> materializations,
    List<RelOptLattice> lattices) {
  planner.clear();
  for (RelOptRule rule : ruleSet) {
    planner.addRule(rule);
  }
  for (RelOptMaterialization materialization : materializations) {
    planner.addMaterialization(materialization);
  }
  for (RelOptLattice lattice : lattices) {
    planner.addLattice(lattice);
  }
  if (!rel.getTraitSet().equals(requiredOutputTraits)) {
    rel = planner.changeTraits(rel, requiredOutputTraits);
  }
  planner.setRoot(rel);
  return planner.findBestExp();

}
 
Example #2
Source File: Programs.java    From calcite with Apache License 2.0 6 votes vote down vote up
public RelNode run(RelOptPlanner planner, RelNode rel,
    RelTraitSet requiredOutputTraits,
    List<RelOptMaterialization> materializations,
    List<RelOptLattice> lattices) {
  planner.clear();
  for (RelOptRule rule : ruleSet) {
    planner.addRule(rule);
  }
  for (RelOptMaterialization materialization : materializations) {
    planner.addMaterialization(materialization);
  }
  for (RelOptLattice lattice : lattices) {
    planner.addLattice(lattice);
  }
  if (!rel.getTraitSet().equals(requiredOutputTraits)) {
    rel = planner.changeTraits(rel, requiredOutputTraits);
  }
  planner.setRoot(rel);
  return planner.findBestExp();

}
 
Example #3
Source File: Programs.java    From calcite with Apache License 2.0 6 votes vote down vote up
/** Creates a program that executes a {@link HepProgram}. */
public static Program of(final HepProgram hepProgram, final boolean noDag,
    final RelMetadataProvider metadataProvider) {
  return (planner, rel, requiredOutputTraits, materializations, lattices) -> {
    final HepPlanner hepPlanner = new HepPlanner(hepProgram,
        null, noDag, null, RelOptCostImpl.FACTORY);

    List<RelMetadataProvider> list = new ArrayList<>();
    if (metadataProvider != null) {
      list.add(metadataProvider);
    }
    hepPlanner.registerMetadataProviders(list);
    for (RelOptMaterialization materialization : materializations) {
      hepPlanner.addMaterialization(materialization);
    }
    for (RelOptLattice lattice : lattices) {
      hepPlanner.addLattice(lattice);
    }
    RelMetadataProvider plannerChain =
        ChainedRelMetadataProvider.of(list);
    rel.getCluster().setMetadataProvider(plannerChain);

    hepPlanner.setRoot(rel);
    return hepPlanner.findBestExp();
  };
}
 
Example #4
Source File: MaterializedViewFilterScanRule.java    From calcite with Apache License 2.0 6 votes vote down vote up
protected void apply(RelOptRuleCall call, Filter filter, TableScan scan) {
  final RelOptPlanner planner = call.getPlanner();
  final List<RelOptMaterialization> materializations =
      planner.getMaterializations();
  if (!materializations.isEmpty()) {
    RelNode root = filter.copy(filter.getTraitSet(),
        Collections.singletonList((RelNode) scan));
    List<RelOptMaterialization> applicableMaterializations =
        RelOptMaterializations.getApplicableMaterializations(root, materializations);
    for (RelOptMaterialization materialization : applicableMaterializations) {
      if (RelOptUtil.areRowTypesEqual(scan.getRowType(),
          materialization.queryRel.getRowType(), false)) {
        RelNode target = materialization.queryRel;
        final HepPlanner hepPlanner =
            new HepPlanner(program, planner.getContext());
        hepPlanner.setRoot(target);
        target = hepPlanner.findBestExp();
        List<RelNode> subs = new SubstitutionVisitor(target, root)
            .go(materialization.tableRel);
        for (RelNode s : subs) {
          call.transformTo(s);
        }
      }
    }
  }
}
 
Example #5
Source File: MaterializedViewFilterScanRule.java    From Bats with Apache License 2.0 6 votes vote down vote up
protected void apply(RelOptRuleCall call, Filter filter, TableScan scan) {
  final RelOptPlanner planner = call.getPlanner();
  final List<RelOptMaterialization> materializations =
      planner.getMaterializations();
  if (!materializations.isEmpty()) {
    RelNode root = filter.copy(filter.getTraitSet(),
        Collections.singletonList((RelNode) scan));
    List<RelOptMaterialization> applicableMaterializations =
        RelOptMaterializations.getApplicableMaterializations(root, materializations);
    for (RelOptMaterialization materialization : applicableMaterializations) {
      if (RelOptUtil.areRowTypesEqual(scan.getRowType(),
          materialization.queryRel.getRowType(), false)) {
        RelNode target = materialization.queryRel;
        final HepPlanner hepPlanner =
            new HepPlanner(program, planner.getContext());
        hepPlanner.setRoot(target);
        target = hepPlanner.findBestExp();
        List<RelNode> subs = new MaterializedViewSubstitutionVisitor(target, root)
            .go(materialization.tableRel);
        for (RelNode s : subs) {
          call.transformTo(s);
        }
      }
    }
  }
}
 
Example #6
Source File: SqlWorker.java    From quark with Apache License 2.0 5 votes vote down vote up
public RelNode run(RelOptPlanner planner, RelNode rel,
                   RelTraitSet requiredOutputTraits,
                   List<RelOptMaterialization> materializations,
                   List<RelOptLattice> lattices) {
  planner.clear();

  planner.addRelTraitDef(ConventionTraitDef.INSTANCE);

  planner.addRelTraitDef(RelCollationTraitDef.INSTANCE);
  //((VolcanoPlanner) planner).registerAbstractRelationalRules();

  RelOptUtil.registerAbstractRels(planner);
  for (RelOptRule rule : ruleSet) {
    planner.addRule(rule);
  }

  planner.addRule(Bindables.BINDABLE_TABLE_SCAN_RULE);
  planner.addRule(ProjectTableScanRule.INSTANCE);
  planner.addRule(ProjectTableScanRule.INTERPRETER);
  planner.addRule(EnumerableInterpreterRule.INSTANCE);

  final CalciteSchema rootSchema = CalciteSchema.from(context.getRootSchema());
  planner.setExecutor(new RexExecutorImpl(null));
  planner.setRoot(rel);

  MaterializationService.setThreadLocal(materializationService);
  plannerHolder.setPlanner(planner);
  populateMaterializationsAndLattice(plannerHolder, rootSchema);
  if (!rel.getTraitSet().equals(requiredOutputTraits)) {
    rel = planner.changeTraits(rel, requiredOutputTraits);
    planner.setRoot(rel);
  }

  RelOptPlanner planner2 = planner.chooseDelegate();
  return planner2.findBestExp();
}
 
Example #7
Source File: MaterializedViewSubstitutionVisitorTest.java    From calcite with Apache License 2.0 5 votes vote down vote up
protected List<RelNode> optimize(TestConfig testConfig) {
  RelNode queryRel = testConfig.queryRel;
  RelOptMaterialization materialization = testConfig.materializations.get(0);
  List<RelNode> substitutes =
      new SubstitutionVisitor(canonicalize(materialization.queryRel), canonicalize(queryRel))
          .go(materialization.tableRel);
  return substitutes;
}
 
Example #8
Source File: HepPlannerTest.java    From calcite with Apache License 2.0 5 votes vote down vote up
@Test void testMaterialization() throws Exception {
  HepPlanner planner = new HepPlanner(HepProgram.builder().build());
  RelNode tableRel = tester.convertSqlToRel("select * from dept").rel;
  RelNode queryRel = tableRel;
  RelOptMaterialization mat1 = new RelOptMaterialization(
      tableRel, queryRel, null, ImmutableList.of("default", "mv"));
  planner.addMaterialization(mat1);
  assertEquals(planner.getMaterializations().size(), 1);
  assertEquals(planner.getMaterializations().get(0), mat1);
  planner.clear();
  assertEquals(planner.getMaterializations().size(), 0);
}
 
Example #9
Source File: Programs.java    From calcite with Apache License 2.0 5 votes vote down vote up
public RelNode run(RelOptPlanner planner, RelNode rel,
    RelTraitSet requiredOutputTraits,
    List<RelOptMaterialization> materializations,
    List<RelOptLattice> lattices) {
  final RelBuilder relBuilder =
      RelFactories.LOGICAL_BUILDER.create(rel.getCluster(), null);
  return new RelFieldTrimmer(null, relBuilder).trim(rel);
}
 
Example #10
Source File: Programs.java    From calcite with Apache License 2.0 5 votes vote down vote up
public RelNode run(RelOptPlanner planner, RelNode rel,
    RelTraitSet requiredOutputTraits,
    List<RelOptMaterialization> materializations,
    List<RelOptLattice> lattices) {
  final CalciteConnectionConfig config =
      planner.getContext().unwrap(CalciteConnectionConfig.class);
  if (config != null && config.forceDecorrelate()) {
    final RelBuilder relBuilder =
        RelFactories.LOGICAL_BUILDER.create(rel.getCluster(), null);
    return RelDecorrelator.decorrelateQuery(rel, relBuilder);
  }
  return rel;
}
 
Example #11
Source File: Programs.java    From calcite with Apache License 2.0 5 votes vote down vote up
public RelNode run(RelOptPlanner planner, RelNode rel,
    RelTraitSet requiredOutputTraits,
    List<RelOptMaterialization> materializations,
    List<RelOptLattice> lattices) {
  for (Program program : programs) {
    rel = program.run(
        planner, rel, requiredOutputTraits, materializations, lattices);
  }
  return rel;
}
 
Example #12
Source File: MaterializedViewFilterScanRule.java    From quark with Apache License 2.0 5 votes vote down vote up
protected void apply(RelOptRuleCall call, Filter filter, TableScan scan) {
  //Avoid optimizing already optimized scan
  if (scan instanceof QuarkViewScan || scan instanceof QuarkTileScan) {
    return;
  }
  RelNode root = filter.copy(filter.getTraitSet(),
      Collections.singletonList((RelNode) scan));
  RelOptPlanner planner = call.getPlanner();
  if (planner instanceof VolcanoPlanner) {
    List<RelOptMaterialization> materializations
        = ((VolcanoPlanner) planner).getMaterializations();
    for (RelOptMaterialization materialization : materializations) {
      if (scan.getRowType().equals(materialization.queryRel.getRowType())) {
        RelNode target = materialization.queryRel;
        final HepPlanner hepPlanner =
            new HepPlanner(program, planner.getContext());
        hepPlanner.setRoot(target);
        target = hepPlanner.findBestExp();
        List<RelNode> subs = new MaterializedViewSubstitutionVisitor(target, root)
            .go(materialization.tableRel);
        for (RelNode s : subs) {
          call.transformTo(s);
        }
      }
    }
  }
}
 
Example #13
Source File: ForcedRulesProgram.java    From calcite-sql-rewriter with Apache License 2.0 5 votes vote down vote up
@Override
public RelNode run(RelOptPlanner relOptPlanner,
		RelNode relNode,
		RelTraitSet relTraitSet,
		List<RelOptMaterialization> relOptMaterializationList,
		List<RelOptLattice> list1) {

	logger.debug("Running forced rules on:\n" + RelOptUtil.toString(relNode));

	return replace(relNode, rules, relBuilderFactoryFactory.create(relOptPlanner.getContext()));
}
 
Example #14
Source File: Programs.java    From Bats with Apache License 2.0 5 votes vote down vote up
public RelNode run(RelOptPlanner planner, RelNode rel,
    RelTraitSet requiredOutputTraits,
    List<RelOptMaterialization> materializations,
    List<RelOptLattice> lattices) {
  for (Program program : programs) {
    rel = program.run(
        planner, rel, requiredOutputTraits, materializations, lattices);
  }
  return rel;
}
 
Example #15
Source File: Programs.java    From Bats with Apache License 2.0 5 votes vote down vote up
public RelNode run(RelOptPlanner planner, RelNode rel,
    RelTraitSet requiredOutputTraits,
    List<RelOptMaterialization> materializations,
    List<RelOptLattice> lattices) {
  final CalciteConnectionConfig config =
      planner.getContext().unwrap(CalciteConnectionConfig.class);
  if (config != null && config.forceDecorrelate()) {
    final RelBuilder relBuilder =
        RelFactories.LOGICAL_BUILDER.create(rel.getCluster(), null);
    return RelDecorrelator.decorrelateQuery(rel, relBuilder);
  }
  return rel;
}
 
Example #16
Source File: Programs.java    From Bats with Apache License 2.0 5 votes vote down vote up
public RelNode run(RelOptPlanner planner, RelNode rel,
    RelTraitSet requiredOutputTraits,
    List<RelOptMaterialization> materializations,
    List<RelOptLattice> lattices) {
  final RelBuilder relBuilder =
      RelFactories.LOGICAL_BUILDER.create(rel.getCluster(), null);
  return new RelFieldTrimmer(null, relBuilder).trim(rel);
}
 
Example #17
Source File: SequenceProgram.java    From calcite-sql-rewriter with Apache License 2.0 5 votes vote down vote up
@Override
public RelNode run(RelOptPlanner relOptPlanner,
		RelNode relNode,
		RelTraitSet relTraitSet,
		List<RelOptMaterialization> relOptMaterializationList,
		List<RelOptLattice> relOptLatticeList) {
	for (Program program : programs) {
		relNode = program.run(relOptPlanner, relNode, relTraitSet, relOptMaterializationList, relOptLatticeList);
		logger.debug("After running " + program + ":\n" + RelOptUtil.toString(relNode));
	}
	return relNode;
}
 
Example #18
Source File: Materializer.java    From quark with Apache License 2.0 5 votes vote down vote up
public void populateMaterializations(CalcitePrepare.Context context,
                                     QuarkMaterializeCluster.RelOptPlannerHolder holder) {
  MaterializePrepare prepare = new MaterializePrepare(holder);
  for (Prepare.Materialization materialization : materializations) {
    if (materialization.queryRel == null || materialization.tableRel == null) {
      prepare.populateMaterializations(context, materialization);
    }
    holder.getPlanner().addMaterialization(
        new RelOptMaterialization(materialization.tableRel,
            materialization.queryRel,
            materialization.getStarTableIdentified()));
  }
}
 
Example #19
Source File: Program.java    From calcite with Apache License 2.0 4 votes vote down vote up
RelNode run(RelOptPlanner planner, RelNode rel,
RelTraitSet requiredOutputTraits,
List<RelOptMaterialization> materializations,
List<RelOptLattice> lattices);
 
Example #20
Source File: HepPlanner.java    From Bats with Apache License 2.0 4 votes vote down vote up
@Override public ImmutableList<RelOptMaterialization> getMaterializations() {
  return ImmutableList.copyOf(materializations);
}
 
Example #21
Source File: VolcanoPlanner.java    From Bats with Apache License 2.0 4 votes vote down vote up
@Override public void addMaterialization(
    RelOptMaterialization materialization) {
  materializations.add(materialization);
}
 
Example #22
Source File: VolcanoPlanner.java    From Bats with Apache License 2.0 4 votes vote down vote up
@Override public List<RelOptMaterialization> getMaterializations() {
  return ImmutableList.copyOf(materializations);
}
 
Example #23
Source File: HepPlanner.java    From Bats with Apache License 2.0 4 votes vote down vote up
@Override public void addMaterialization(RelOptMaterialization materialization) {
  materializations.add(materialization);
}
 
Example #24
Source File: VolcanoPlanner.java    From calcite with Apache License 2.0 4 votes vote down vote up
@Override public List<RelOptMaterialization> getMaterializations() {
  return ImmutableList.copyOf(materializations);
}
 
Example #25
Source File: VolcanoPlanner.java    From calcite with Apache License 2.0 4 votes vote down vote up
@Override public void addMaterialization(
    RelOptMaterialization materialization) {
  materializations.add(materialization);
}
 
Example #26
Source File: HepPlanner.java    From calcite with Apache License 2.0 4 votes vote down vote up
@Override public ImmutableList<RelOptMaterialization> getMaterializations() {
  return ImmutableList.copyOf(materializations);
}
 
Example #27
Source File: HepPlanner.java    From calcite with Apache License 2.0 4 votes vote down vote up
@Override public void addMaterialization(RelOptMaterialization materialization) {
  materializations.add(materialization);
}
 
Example #28
Source File: AbstractMaterializedViewTest.java    From calcite with Apache License 2.0 4 votes vote down vote up
public TestConfig(String defaultSchema, RelNode queryRel,
    List<RelOptMaterialization> materializations) {
  this.defaultSchema = defaultSchema;
  this.queryRel = queryRel;
  this.materializations = materializations;
}
 
Example #29
Source File: Program.java    From Bats with Apache License 2.0 4 votes vote down vote up
RelNode run(RelOptPlanner planner, RelNode rel,
RelTraitSet requiredOutputTraits,
List<RelOptMaterialization> materializations,
List<RelOptLattice> lattices);