org.apache.calcite.plan.RelOptRule Java Examples

The following examples show how to use org.apache.calcite.plan.RelOptRule. 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 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 #2
Source File: PlannerTest.java    From calcite with Apache License 2.0 6 votes vote down vote up
private void checkUnionPruning(String sql, String plan, RelOptRule... extraRules)
    throws SqlParseException, ValidationException, RelConversionException {
  ImmutableList.Builder<RelOptRule> rules = ImmutableList.<RelOptRule>builder().add(
      PruneEmptyRules.UNION_INSTANCE,
      ValuesReduceRule.PROJECT_FILTER_INSTANCE,
      EnumerableRules.ENUMERABLE_PROJECT_RULE,
      EnumerableRules.ENUMERABLE_FILTER_RULE,
      EnumerableRules.ENUMERABLE_VALUES_RULE,
      EnumerableRules.ENUMERABLE_UNION_RULE);
  rules.add(extraRules);
  Program program = Programs.ofRules(rules.build());
  Planner planner = getPlanner(null, program);
  SqlNode parse = planner.parse(sql);
  SqlNode validate = planner.validate(parse);
  RelNode convert = planner.rel(validate).project();
  RelTraitSet traitSet = convert.getTraitSet()
      .replace(EnumerableConvention.INSTANCE);
  RelNode transform = planner.transform(0, traitSet, convert);
  assertThat("Empty values should be removed from " + sql,
      toString(transform), equalTo(plan));
}
 
Example #3
Source File: JdbcRules.java    From calcite with Apache License 2.0 6 votes vote down vote up
public static List<RelOptRule> rules(JdbcConvention out,
    RelBuilderFactory relBuilderFactory) {
  return ImmutableList.of(
      new JdbcToEnumerableConverterRule(out, relBuilderFactory),
      new JdbcJoinRule(out, relBuilderFactory),
      new JdbcCalcRule(out, relBuilderFactory),
      new JdbcProjectRule(out, relBuilderFactory),
      new JdbcFilterRule(out, relBuilderFactory),
      new JdbcAggregateRule(out, relBuilderFactory),
      new JdbcSortRule(out, relBuilderFactory),
      new JdbcUnionRule(out, relBuilderFactory),
      new JdbcIntersectRule(out, relBuilderFactory),
      new JdbcMinusRule(out, relBuilderFactory),
      new JdbcTableModificationRule(out, relBuilderFactory),
      new JdbcValuesRule(out, relBuilderFactory));
}
 
Example #4
Source File: HepPlanner.java    From Bats with Apache License 2.0 6 votes vote down vote up
private int depthFirstApply(Iterator<HepRelVertex> iter,
    Collection<RelOptRule> rules,
    boolean forceConversions, int nMatches) {
  while (iter.hasNext()) {
    HepRelVertex vertex = iter.next();
    for (RelOptRule rule : rules) {
      HepRelVertex newVertex =
          applyRule(rule, vertex, forceConversions);
      if (newVertex == null || newVertex == vertex) {
        continue;
      }
      ++nMatches;
      if (nMatches >= currentProgram.matchLimit) {
        return nMatches;
      }
      // To the extent possible, pick up where we left
      // off; have to create a new iterator because old
      // one was invalidated by transformation.
      Iterator<HepRelVertex> depthIter = getGraphIterator(newVertex);
      nMatches = depthFirstApply(depthIter, rules, forceConversions,
          nMatches);
      break;
    }
  }
  return nMatches;
}
 
Example #5
Source File: VolcanoPlanner.java    From calcite with Apache License 2.0 6 votes vote down vote up
@Override protected void onNewClass(RelNode node) {
  super.onNewClass(node);

  final boolean isPhysical = node instanceof PhysicalNode;
  // Create mappings so that instances of this class will match existing
  // operands.
  final Class<? extends RelNode> clazz = node.getClass();
  for (RelOptRule rule : mapDescToRule.values()) {
    if (isPhysical && rule instanceof TransformationRule) {
      continue;
    }
    for (RelOptRuleOperand operand : rule.getOperands()) {
      if (operand.getMatchedClass().isAssignableFrom(clazz)) {
        classOperands.put(clazz, operand);
      }
    }
  }
}
 
Example #6
Source File: MaterializedViewOnlyAggregateRule.java    From calcite with Apache License 2.0 6 votes vote down vote up
public MaterializedViewOnlyAggregateRule(RelOptRuleOperand operand,
    RelBuilderFactory relBuilderFactory, String description,
    boolean generateUnionRewriting, HepProgram unionRewritingPullProgram,
    RelOptRule filterProjectTransposeRule,
    RelOptRule filterAggregateTransposeRule,
    RelOptRule aggregateProjectPullUpConstantsRule,
    RelOptRule projectMergeRule) {
  super(
      operand(Aggregate.class, any()),
      relBuilderFactory,
      "MaterializedViewAggregateRule(Aggregate)",
      generateUnionRewriting, unionRewritingPullProgram,
      filterProjectTransposeRule,
      filterAggregateTransposeRule,
      aggregateProjectPullUpConstantsRule,
      projectMergeRule);
}
 
Example #7
Source File: HintStrategyTable.java    From calcite with Apache License 2.0 6 votes vote down vote up
/** Returns whether the {@code hintable} has hints that imply
 * the given {@code rule} should be excluded. */
public boolean isRuleExcluded(Hintable hintable, RelOptRule rule) {
  final List<RelHint> hints = hintable.getHints();
  if (hints.size() == 0) {
    return false;
  }

  for (RelHint hint : hints) {
    final Key key = Key.of(hint.hintName);
    assert this.strategies.containsKey(key);
    final HintStrategy strategy = strategies.get(key);
    if (strategy.excludedRules.contains(rule)) {
      return isDesiredConversionPossible(strategy.converterRules, hintable);
    }
  }

  return false;
}
 
Example #8
Source File: VolcanoPlanner.java    From Bats with Apache License 2.0 6 votes vote down vote up
public boolean removeRule(RelOptRule rule) {
  if (!ruleSet.remove(rule)) {
    // Rule was not present.
    return false;
  }

  // Remove description.
  unmapRuleDescription(rule);

  // Remove operands.
  classOperands.values().removeIf(entry -> entry.getRule().equals(rule));

  // Remove trait mappings. (In particular, entries from conversion
  // graph.)
  if (rule instanceof ConverterRule) {
    ConverterRule converterRule = (ConverterRule) rule;
    final RelTrait ruleTrait = converterRule.getInTrait();
    final RelTraitDef ruleTraitDef = ruleTrait.getTraitDef();
    if (traitDefs.contains(ruleTraitDef)) {
      ruleTraitDef.deregisterConverterRule(this, converterRule);
    }
  }
  return true;
}
 
Example #9
Source File: InfoSchemaRulesFactory.java    From dremio-oss with Apache License 2.0 6 votes vote down vote up
@Override
public Set<RelOptRule> getRules(OptimizerRulesContext optimizerContext, PlannerPhase phase, SourceType pluginType) {
  switch(phase) {
  case LOGICAL:
    return ImmutableSet.of(InfoSchemaScanDrule.INSTANCE);

  case PHYSICAL:
    return ImmutableSet.of(
        InfoSchemaScanPrule.INSTANCE,
        InfoSchemaPushFilterIntoScan.IS_FILTER_ON_PROJECT,
        InfoSchemaPushFilterIntoScan.IS_FILTER_ON_SCAN);

  default:
    return ImmutableSet.of();
  }
}
 
Example #10
Source File: HepPlanner.java    From calcite with Apache License 2.0 6 votes vote down vote up
void executeInstruction(
    HepInstruction.RuleClass<?> instruction) {
  if (skippingGroup()) {
    return;
  }
  LOGGER.trace("Applying rule class {}", instruction.ruleClass);
  if (instruction.ruleSet == null) {
    instruction.ruleSet = new LinkedHashSet<>();
    for (RelOptRule rule : mapDescToRule.values()) {
      if (instruction.ruleClass.isInstance(rule)) {
        instruction.ruleSet.add(rule);
      }
    }
  }
  applyRules(instruction.ruleSet, true);
}
 
Example #11
Source File: RelOptTestBase.java    From calcite with Apache License 2.0 5 votes vote down vote up
public Sql withRule(RelOptRule... rules) {
  final HepProgramBuilder builder = HepProgram.builder();
  for (RelOptRule rule : rules) {
    builder.addRuleInstance(rule);
  }
  return with(builder.build());
}
 
Example #12
Source File: StreamExecPythonCorrelateRule.java    From flink with Apache License 2.0 5 votes vote down vote up
StreamExecPythonCorrelateFactory(RelNode rel) {
	this.correlate = (FlinkLogicalCorrelate) rel;
	this.traitSet = rel.getTraitSet().replace(FlinkConventions.STREAM_PHYSICAL());
	this.convInput = RelOptRule.convert(
		correlate.getInput(0), FlinkConventions.STREAM_PHYSICAL());
	this.right = correlate.getInput(1);
}
 
Example #13
Source File: DremioRelFactories.java    From dremio-oss with Apache License 2.0 5 votes vote down vote up
@Override
public RelNode createProject(RelNode child,
                             List<? extends RexNode> childExprs, List<String> fieldNames) {
  final RelOptCluster cluster = child.getCluster();
  final RelDataType rowType = RexUtil.createStructType(cluster.getTypeFactory(), childExprs, fieldNames, SqlValidatorUtil.F_SUGGESTER);
  final RelNode project = ProjectRel.create(
      cluster,
      child.getTraitSet().plus(Rel.LOGICAL),
      RelOptRule.convert(child, child.getTraitSet().plus(Rel.LOGICAL).simplify()),
      childExprs,
      rowType);

  return project;
}
 
Example #14
Source File: TraitPropagationTest.java    From calcite with Apache License 2.0 5 votes vote down vote up
private static RelNode run(PropAction action, RuleSet rules)
    throws Exception {

  FrameworkConfig config = Frameworks.newConfigBuilder()
      .ruleSets(rules).build();

  final Properties info = new Properties();
  final Connection connection = DriverManager
      .getConnection("jdbc:calcite:", info);
  final CalciteServerStatement statement = connection
      .createStatement().unwrap(CalciteServerStatement.class);
  final CalcitePrepare.Context prepareContext =
        statement.createPrepareContext();
  final JavaTypeFactory typeFactory = prepareContext.getTypeFactory();
  CalciteCatalogReader catalogReader =
        new CalciteCatalogReader(prepareContext.getRootSchema(),
            prepareContext.getDefaultSchemaPath(),
            typeFactory,
            prepareContext.config());
  final RexBuilder rexBuilder = new RexBuilder(typeFactory);
  final RelOptPlanner planner = new VolcanoPlanner(config.getCostFactory(),
      config.getContext());

  // set up rules before we generate cluster
  planner.clearRelTraitDefs();
  planner.addRelTraitDef(RelCollationTraitDef.INSTANCE);
  planner.addRelTraitDef(ConventionTraitDef.INSTANCE);

  planner.clear();
  for (RelOptRule r : rules) {
    planner.addRule(r);
  }

  final RelOptCluster cluster = RelOptCluster.create(planner, rexBuilder);
  return action.apply(cluster, catalogReader,
      prepareContext.getRootSchema().plus());
}
 
Example #15
Source File: DremioRelFactories.java    From dremio-oss with Apache License 2.0 5 votes vote down vote up
@Override
public RelNode createFilter(RelNode child, RexNode condition, Set<CorrelationId> correlVariables) {
  Preconditions.checkArgument(correlVariables.isEmpty());
  return FilterRel.create(
      RelOptRule.convert(child, child.getTraitSet().plus(Rel.LOGICAL).simplify()),
      condition);
}
 
Example #16
Source File: VolcanoPlanner.java    From Bats with Apache License 2.0 5 votes vote down vote up
@Override protected void onNewClass(RelNode node) {
  super.onNewClass(node);

  // Create mappings so that instances of this class will match existing
  // operands.
  final Class<? extends RelNode> clazz = node.getClass();
  for (RelOptRule rule : ruleSet) {
    for (RelOptRuleOperand operand : rule.getOperands()) {
      if (operand.getMatchedClass().isAssignableFrom(clazz)) {
        classOperands.put(clazz, operand);
      }
    }
  }
}
 
Example #17
Source File: DremioRelFactories.java    From dremio-oss with Apache License 2.0 5 votes vote down vote up
@Override
public RelNode createJoin(RelNode left, RelNode right,
                          RexNode condition,
                          Set<CorrelationId> variablesSet,
                          JoinRelType joinType, boolean semiJoinDone) {
  return JoinRel.create(
      left.getCluster(),
      left.getTraitSet().plus(Rel.LOGICAL),
      RelOptRule.convert(left, left.getTraitSet().plus(Rel.LOGICAL).simplify()),
      RelOptRule.convert(right, right.getTraitSet().plus(Rel.LOGICAL).simplify()),
      condition,
      joinType);
}
 
Example #18
Source File: CatalogServiceImpl.java    From dremio-oss with Apache License 2.0 5 votes vote down vote up
@Override
public RuleSet getStorageRules(OptimizerRulesContext context, PlannerPhase phase) {
  final ImmutableSet.Builder<RelOptRule> rules = ImmutableSet.builder();
  final Set<SourceType> types = new HashSet<>();

  try {
    for(ManagedStoragePlugin plugin : plugins.managed()){
      if(plugin.getState().getStatus() == SourceStatus.bad) {
        // we shouldn't consider rules for misbehaving plugins.
        continue;
      }
      final StoragePluginId pluginId = plugin.getId();

      StoragePluginRulesFactory factory = plugin.getRulesFactory();
      if(factory != null) {
        // add instance level rules.
        rules.addAll(factory.getRules(context, phase, pluginId));

        // add type level rules.
        if(types.add(pluginId.getType())) {
          rules.addAll(factory.getRules(context, phase, pluginId.getType()));
        }
      }
    }
  } catch (InstantiationException | IllegalAccessException e) {
    throw UserException.validationError(e).message("Failure getting plugin rules.").build(logger);
  }

  ImmutableSet<RelOptRule> rulesSet = rules.build();
  return RuleSets.ofList(rulesSet);
}
 
Example #19
Source File: PigConverter.java    From calcite with Apache License 2.0 5 votes vote down vote up
private void resetPlannerRules(RelOptPlanner planner,
    List<RelOptRule> rulesToSet) {
  planner.clear();
  for (RelOptRule rule : rulesToSet) {
    planner.addRule(rule);
  }
}
 
Example #20
Source File: VolcanoPlanner.java    From calcite with Apache License 2.0 5 votes vote down vote up
@Override public void clear() {
  super.clear();
  for (RelOptRule rule : getRules()) {
    removeRule(rule);
  }
  this.classOperands.clear();
  this.allSets.clear();
  this.mapDigestToRel.clear();
  this.mapRel2Subset.clear();
  this.prunedNodes.clear();
  this.ruleQueue.clear();
  this.materializations.clear();
  this.latticeByName.clear();
  this.provenanceMap.clear();
}
 
Example #21
Source File: FileSystemRulesFactory.java    From dremio-oss with Apache License 2.0 5 votes vote down vote up
@Override
public Set<RelOptRule> getRules(OptimizerRulesContext optimizerContext, PlannerPhase phase, SourceType pluginType) {

  switch(phase){
    case LOGICAL:
      ImmutableSet.Builder<RelOptRule> builder = ImmutableSet.builder();
      builder.add(new FileSystemDrule(pluginType));

      if(optimizerContext.getPlannerSettings().isPartitionPruningEnabled()){
        builder.add(new PruneScanRuleFilterOnProject<>(pluginType, FilesystemScanDrel.class, optimizerContext));
        builder.add(new PruneScanRuleFilterOnScan<>(pluginType, FilesystemScanDrel.class, optimizerContext));
      }

      return builder.build();

    case PHYSICAL:
      return ImmutableSet.<RelOptRule>of(
          new EasyFilesystemScanPrule(pluginType),
          new ParquetFilesystemScanPrule(pluginType),
          ConvertCountToDirectScan.getAggOnScan(pluginType),
          ConvertCountToDirectScan.getAggProjOnScan(pluginType)
          );

    default:
      return ImmutableSet.<RelOptRule>of();

  }
}
 
Example #22
Source File: HintStrategy.java    From calcite with Apache License 2.0 5 votes vote down vote up
private HintStrategy(
    HintPredicate predicate,
    HintOptionChecker hintOptionChecker,
    ImmutableSet<RelOptRule> excludedRules,
    ImmutableSet<ConverterRule> converterRules) {
  this.predicate = predicate;
  this.hintOptionChecker = hintOptionChecker;
  this.excludedRules = excludedRules;
  this.converterRules = converterRules;
}
 
Example #23
Source File: HepPlanner.java    From calcite with Apache License 2.0 5 votes vote down vote up
@Override public void clear() {
  super.clear();
  for (RelOptRule rule : getRules()) {
    removeRule(rule);
  }
  this.materializations.clear();
}
 
Example #24
Source File: BatchExecPythonCorrelateRule.java    From flink with Apache License 2.0 5 votes vote down vote up
BatchExecPythonCorrelateFactory(RelNode rel) {
	this.correlate = (FlinkLogicalCorrelate) rel;
	this.traitSet = rel.getTraitSet().replace(FlinkConventions.BATCH_PHYSICAL());
	this.convInput = RelOptRule.convert(
		correlate.getInput(0), FlinkConventions.BATCH_PHYSICAL());
	this.right = correlate.getInput(1);
}
 
Example #25
Source File: FlinkFilterJoinRule.java    From flink with Apache License 2.0 5 votes vote down vote up
public FlinkFilterIntoJoinRule(boolean smart,
		RelBuilderFactory relBuilderFactory, Predicate predicate) {
	super(
			operand(Filter.class,
					operand(Join.class, RelOptRule.any())),
			"FlinkFilterJoinRule:filter", smart, relBuilderFactory,
			predicate);
}
 
Example #26
Source File: FlinkFilterJoinRule.java    From flink with Apache License 2.0 5 votes vote down vote up
public FlinkFilterIntoJoinRule(boolean smart,
		RelBuilderFactory relBuilderFactory, Predicate predicate) {
	super(
			operand(Filter.class,
					operand(Join.class, RelOptRule.any())),
			"FlinkFilterJoinRule:filter", smart, relBuilderFactory,
			predicate);
}
 
Example #27
Source File: MaterializationExpander.java    From dremio-oss with Apache License 2.0 5 votes vote down vote up
private static RelNode applyRule(RelNode node, RelOptRule rule) {
  final HepProgramBuilder builder = HepProgram.builder();
  builder.addMatchOrder(HepMatchOrder.ARBITRARY);
  builder.addRuleCollection(ImmutableList.of(rule));
  final HepProgram program = builder.build();

  final HepPlanner planner = new HepPlanner(program);
  planner.setRoot(node);
  return planner.findBestExp();
}
 
Example #28
Source File: OLAPTableScan.java    From kylin-on-parquet-v2 with Apache License 2.0 5 votes vote down vote up
protected void addRules(final RelOptPlanner planner, List<String> rules) {
    modifyRules(rules, new Function<RelOptRule, Void>() {
        @Nullable
        @Override
        public Void apply(@Nullable RelOptRule input) {
            planner.addRule(input);
            return null;
        }
    });
}
 
Example #29
Source File: OLAPTableScan.java    From kylin-on-parquet-v2 with Apache License 2.0 5 votes vote down vote up
protected void removeRules(final RelOptPlanner planner, List<String> rules) {
    modifyRules(rules, new Function<RelOptRule, Void>() {
        @Nullable
        @Override
        public Void apply(@Nullable RelOptRule input) {
            planner.removeRule(input);
            return null;
        }
    });
}
 
Example #30
Source File: FlinkFilterJoinRule.java    From flink with Apache License 2.0 5 votes vote down vote up
public FlinkFilterIntoJoinRule(boolean smart,
		RelBuilderFactory relBuilderFactory, Predicate predicate) {
	super(
			operand(Filter.class,
					operand(Join.class, RelOptRule.any())),
			"FlinkFilterJoinRule:filter", smart, relBuilderFactory,
			predicate);
}