Java Code Examples for org.apache.calcite.plan.RelOptRuleCall#rel()

The following examples show how to use org.apache.calcite.plan.RelOptRuleCall#rel() . 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: FilterMultiJoinMergeRule.java    From calcite with Apache License 2.0 6 votes vote down vote up
public void onMatch(RelOptRuleCall call) {
  Filter filter = call.rel(0);
  MultiJoin multiJoin = call.rel(1);

  // Create a new post-join filter condition
  // Conditions are nullable, so ImmutableList can't be used here
  List<RexNode> filters = Arrays.asList(
      filter.getCondition(),
      multiJoin.getPostJoinFilter());

  final RexBuilder rexBuilder = multiJoin.getCluster().getRexBuilder();
  MultiJoin newMultiJoin =
      new MultiJoin(
          multiJoin.getCluster(),
          multiJoin.getInputs(),
          multiJoin.getJoinFilter(),
          multiJoin.getRowType(),
          multiJoin.isFullOuterJoin(),
          multiJoin.getOuterJoinConditions(),
          multiJoin.getJoinTypes(),
          multiJoin.getProjFields(),
          multiJoin.getJoinFieldRefCountsMap(),
          RexUtil.composeConjunction(rexBuilder, filters, true));

  call.transformTo(newMultiJoin);
}
 
Example 2
Source File: ProjectSortTransposeRule.java    From Bats with Apache License 2.0 6 votes vote down vote up
public void onMatch(RelOptRuleCall call) {
  final Project project = call.rel(0);
  final Sort sort = call.rel(1);
  if (sort.getClass() != Sort.class) {
    return;
  }
  RelNode newProject =
      project.copy(
          project.getTraitSet(), ImmutableList.of(sort.getInput()));
  final Sort newSort =
      sort.copy(
          sort.getTraitSet(),
          newProject,
          sort.getCollation(),
          sort.offset,
          sort.fetch);
  call.transformTo(newSort);
}
 
Example 3
Source File: FilterNLJMergeRule.java    From dremio-oss with Apache License 2.0 6 votes vote down vote up
@Override
public void onMatch(RelOptRuleCall call) {
  FilterPrel filter = call.rel(0);
  NestedLoopJoinPrel join = call.rel(1);

  if ((join.getProjectedFields() == null) || join.getProjectedFields().cardinality() == join.getInputRowType().getFieldCount()) {
    call.transformTo(NestedLoopJoinPrel.create(join.getCluster(), join.getTraitSet(), join.getLeft(), join.getRight(), join.getJoinType(), RelOptUtil.andJoinFilters(join.getCluster().getRexBuilder(), join.getCondition(), filter.getCondition()), join.getProjectedFields()));
  } else {
    // Current filter condition is written based on projected fields on join. In order to push this filter down we need to rewrite filter condition
    final ImmutableBitSet topProjectedColumns = RelOptUtil.InputFinder.bits(filter.getCondition());
    final ImmutableBitSet bottomProjectedColumns = join.getProjectedFields();

    Mapping mapping = Mappings.create(MappingType.SURJECTION, join.getRowType().getFieldCount(), join.getInputRowType().getFieldCount());
    for (Ord<Integer> ord : Ord.zip(bottomProjectedColumns)) {
      if (topProjectedColumns.get(ord.i)) {
        mapping.set(ord.i, ord.e);
      }
    }

    RexShuttle shuttle = new RexPermuteInputsShuttle(mapping);
    RexNode updatedCondition = shuttle.apply(filter.getCondition());

    call.transformTo(NestedLoopJoinPrel.create(join.getCluster(), join.getTraitSet(), join.getLeft(), join.getRight(), join.getJoinType(), RelOptUtil.andJoinFilters(join.getCluster().getRexBuilder(), join.getCondition(), updatedCondition), join.getProjectedFields()));
  }
}
 
Example 4
Source File: UnionMergeRule.java    From calcite with Apache License 2.0 6 votes vote down vote up
@Override public boolean matches(RelOptRuleCall call) {
  // It avoids adding the rule match to the match queue in case the rule is known to be a no-op
  final SetOp topOp = call.rel(0);
  @SuppressWarnings("unchecked") final Class<? extends SetOp> setOpClass =
      (Class<? extends SetOp>) operands.get(0).getMatchedClass();
  final SetOp bottomOp;
  if (setOpClass.isInstance(call.rel(2))
      && !Minus.class.isAssignableFrom(setOpClass)) {
    bottomOp = call.rel(2);
  } else if (setOpClass.isInstance(call.rel(1))) {
    bottomOp = call.rel(1);
  } else {
    return false;
  }

  if (topOp.all && !bottomOp.all) {
    return false;
  }

  return true;
}
 
Example 5
Source File: CalcRemoveRule.java    From calcite with Apache License 2.0 5 votes vote down vote up
public void onMatch(RelOptRuleCall call) {
  LogicalCalc calc = call.rel(0);
  RexProgram program = calc.getProgram();
  if (!program.isTrivial()) {
    return;
  }
  RelNode input = calc.getInput();
  input = call.getPlanner().register(input, calc);
  call.transformTo(
      convert(
          input,
          calc.getTraitSet()));
}
 
Example 6
Source File: VolcanoPlannerTest.java    From calcite with Apache License 2.0 5 votes vote down vote up
@Override public void onMatch(RelOptRuleCall call) {
  PhysSingleRel singleRel = call.rel(0);
  PhysLeafRel input = call.rel(1);
  RelNode newInput =
      new PhysLeafRel(input.getCluster(), PHYS_CALLING_CONVENTION_3, "a");

  VolcanoPlanner planner = (VolcanoPlanner) call.getPlanner();
  // Register into a new RelSet first
  planner.ensureRegistered(newInput, null);
  // Merge into the old RelSet
  planner.ensureRegistered(newInput, input);
}
 
Example 7
Source File: StreamRules.java    From calcite with Apache License 2.0 5 votes vote down vote up
@Override public void onMatch(RelOptRuleCall call) {
  final Delta delta = call.rel(0);
  Util.discard(delta);
  final Union union = call.rel(1);
  final List<RelNode> newInputs = new ArrayList<>();
  for (RelNode input : union.getInputs()) {
    final LogicalDelta newDelta =
        LogicalDelta.create(input);
    newInputs.add(newDelta);
  }
  final LogicalUnion newUnion = LogicalUnion.create(newInputs, union.all);
  call.transformTo(newUnion);
}
 
Example 8
Source File: DirectScanPrule.java    From Bats with Apache License 2.0 5 votes vote down vote up
@Override
public void onMatch(RelOptRuleCall call) {
  final DrillDirectScanRel scan = call.rel(0);
  final RelTraitSet traits = scan.getTraitSet().plus(Prel.DRILL_PHYSICAL);

  final DirectScanPrel newScan = new DirectScanPrel(scan.getCluster(), traits, scan.getGroupScan(), scan.getRowType());
  call.transformTo(newScan);
}
 
Example 9
Source File: FilterSetOpTransposeRule.java    From Bats with Apache License 2.0 5 votes vote down vote up
public void onMatch(RelOptRuleCall call) {
  Filter filterRel = call.rel(0);
  SetOp setOp = call.rel(1);

  RexNode condition = filterRel.getCondition();

  // create filters on top of each setop child, modifying the filter
  // condition to reference each setop child
  RexBuilder rexBuilder = filterRel.getCluster().getRexBuilder();
  final RelBuilder relBuilder = call.builder();
  List<RelDataTypeField> origFields =
      setOp.getRowType().getFieldList();
  int[] adjustments = new int[origFields.size()];
  final List<RelNode> newSetOpInputs = new ArrayList<>();
  for (RelNode input : setOp.getInputs()) {
    RexNode newCondition =
        condition.accept(
            new RelOptUtil.RexInputConverter(
                rexBuilder,
                origFields,
                input.getRowType().getFieldList(),
                adjustments));
    newSetOpInputs.add(relBuilder.push(input).filter(newCondition).build());
  }

  // create a new setop whose children are the filters created above
  SetOp newSetOp =
      setOp.copy(setOp.getTraitSet(), newSetOpInputs);

  call.transformTo(newSetOp);
}
 
Example 10
Source File: HashJoinPrule.java    From dremio-oss with Apache License 2.0 5 votes vote down vote up
@Override
public void onMatch(RelOptRuleCall call) {
  PlannerSettings settings = PrelUtil.getPlannerSettings(call.getPlanner());
  if (!settings.isHashJoinEnabled()) {
    return;
  }

  final JoinRel join = (JoinRel) call.rel(0);
  final RelNode left = join.getLeft();
  final RelNode right = join.getRight();

  if (!checkPreconditions(join, left, right, settings)) {
    return;
  }

  boolean hashSingleKey = PrelUtil.getPlannerSettings(call.getPlanner()).isHashSingleKey();

  try {

    if (isDist) {
      createDistBothPlan(call, join,
          left, right, null /* left collation */, null /* right collation */, hashSingleKey);
    } else {
      if (checkBroadcastConditions(call.getPlanner(), join, left, right)) {
        createBroadcastPlan(call, join, join.getCondition(), left, right, null, null);
      }
    }


  } catch (InvalidRelException | UnsupportedRelOperatorException e) {
    tracer.warn(e.toString());
  }
}
 
Example 11
Source File: DrillSortRule.java    From Bats with Apache License 2.0 5 votes vote down vote up
@Override
public void onMatch(RelOptRuleCall call) {

  final Sort sort = call.rel(0);

  final RelNode input = sort.getInput();
  final RelTraitSet traits = sort.getTraitSet().plus(DrillRel.DRILL_LOGICAL);

  final RelNode convertedInput = convert(input, input.getTraitSet().plus(DrillRel.DRILL_LOGICAL).simplify());
  call.transformTo(new DrillSortRel(sort.getCluster(), traits, convertedInput, sort.getCollation()));
}
 
Example 12
Source File: VolcanoPlannerTest.java    From calcite with Apache License 2.0 5 votes vote down vote up
public void onMatch(RelOptRuleCall call) {
  // Do not transform to anything; just log the calls.
  TestSingleRel singleRel = call.rel(0);
  RelSubset childRel = call.rel(1);
  assertThat(call.rels.length, equalTo(2));
  buf.add(singleRel.getClass().getSimpleName() + ":"
      + childRel.getDigest());
}
 
Example 13
Source File: TraitPropagationTest.java    From calcite with Apache License 2.0 4 votes vote down vote up
public void onMatch(RelOptRuleCall call) {
  LogicalTableScan rel = call.rel(0);
  call.transformTo(new PhysTable(rel.getCluster()));
}
 
Example 14
Source File: DrillScanRule.java    From Bats with Apache License 2.0 4 votes vote down vote up
@Override
public void onMatch(RelOptRuleCall call) {
  final EnumerableTableScan access = call.rel(0);
  final RelTraitSet traits = access.getTraitSet().plus(DrillRel.DRILL_LOGICAL);
  call.transformTo(new DrillScanRel(access.getCluster(), traits, access.getTable()));
}
 
Example 15
Source File: FlinkFilterJoinRule.java    From flink with Apache License 2.0 4 votes vote down vote up
@Override public void onMatch(RelOptRuleCall call) {
	Join join = call.rel(0);
	perform(call, null, join);
}
 
Example 16
Source File: SimplifyNLJConditionRule.java    From dremio-oss with Apache License 2.0 4 votes vote down vote up
@Override
public boolean matches(RelOptRuleCall call) {
  NestedLoopJoinPrel nestedLoopJoin = call.rel(0);
  return !nestedLoopJoin.hasVectorExpression();
}
 
Example 17
Source File: JoinCalcTransposeRule.java    From quark with Apache License 2.0 4 votes vote down vote up
public void onMatch(RelOptRuleCall call) {
  final Join joinRel = call.rel(0);
  final RelNode otherNode;
  final Calc calc;

  final RelNode leftJoinChild;
  final RelNode rightJoinChild;

  if (call.rel(1) instanceof Calc) {
    otherNode = call.rel(2);
    calc = call.rel(1);
    rightJoinChild = otherNode;
    leftJoinChild = calc.getInput();
  } else {
    otherNode = call.rel(1);
    calc = call.rel(2);
    rightJoinChild = calc.getInput();
    leftJoinChild = otherNode;
  }
  /**
   * Currently not supporting calc which doesnot
   * project star (all the columns of input)
   * or has aggregates.
   */
  if (!isStar(calc.getProgram())
      || calc.getProgram().containsAggs()) {
    return;
  }

  final List<RelDataTypeField> origFields =
      calc.getRowType().getFieldList();
  final int[] adjustments = new int[calc.getProgram().getExprCount()];
  if (rightJoinChild == calc.getInput()) {
    int offset = leftJoinChild.getRowType().getFieldList().size();
    for (int i = 0; i < origFields.size(); i++) {
      adjustments[i] = offset;
    }
  }
  Join newJoinRel =
      joinRel.copy(joinRel.getTraitSet(), joinRel.getCondition(),
          leftJoinChild, rightJoinChild, joinRel.getJoinType(),
          joinRel.isSemiJoinDone());

  RexProgramBuilder topProgramBuilder =
      new RexProgramBuilder(
          joinRel.getRowType(),
          joinRel.getCluster().getRexBuilder());
  topProgramBuilder.addIdentity();
  final RelOptUtil.RexInputConverter rexInputConverter =
      new RelOptUtil.RexInputConverter(calc.getCluster().getRexBuilder(),
          origFields,
          joinRel.getRowType().getFieldList(),
          adjustments);
  if (calc.getProgram().getCondition() != null) {
    RexNode cond =
        calc.getProgram().expandLocalRef(calc.getProgram().getCondition());
    final RexLocalRef rexLocalRef =
        topProgramBuilder.addExpr(cond.accept(rexInputConverter));
    topProgramBuilder.addCondition(rexLocalRef);
  }
  Calc newCalcRel =
      calc.copy(calc.getTraitSet(), newJoinRel, topProgramBuilder.getProgram());

  call.transformTo(newCalcRel);
}
 
Example 18
Source File: ExpansionDrule.java    From dremio-oss with Apache License 2.0 4 votes vote down vote up
@Override
public void onMatch(RelOptRuleCall call) {
  final RelNode input = call.rel(1);
  call.transformTo(convert(input, input.getTraitSet().plus(Rel.LOGICAL).simplify()));
}
 
Example 19
Source File: AbstractMaterializedViewRule.java    From Bats with Apache License 2.0 4 votes vote down vote up
@Override
public void onMatch(RelOptRuleCall call) {
    final Join join = call.rel(0);
    perform(call, null, join);
}
 
Example 20
Source File: ProjectMergeRule.java    From calcite with Apache License 2.0 4 votes vote down vote up
public void onMatch(RelOptRuleCall call) {
  final Project topProject = call.rel(0);
  final Project bottomProject = call.rel(1);
  final RelBuilder relBuilder = call.builder();

  // If one or both projects are permutations, short-circuit the complex logic
  // of building a RexProgram.
  final Permutation topPermutation = topProject.getPermutation();
  if (topPermutation != null) {
    if (topPermutation.isIdentity()) {
      // Let ProjectRemoveRule handle this.
      return;
    }
    final Permutation bottomPermutation = bottomProject.getPermutation();
    if (bottomPermutation != null) {
      if (bottomPermutation.isIdentity()) {
        // Let ProjectRemoveRule handle this.
        return;
      }
      final Permutation product = topPermutation.product(bottomPermutation);
      relBuilder.push(bottomProject.getInput());
      relBuilder.project(relBuilder.fields(product),
          topProject.getRowType().getFieldNames());
      call.transformTo(relBuilder.build());
      return;
    }
  }

  // If we're not in force mode and the two projects reference identical
  // inputs, then return and let ProjectRemoveRule replace the projects.
  if (!force) {
    if (RexUtil.isIdentity(topProject.getProjects(),
        topProject.getInput().getRowType())) {
      return;
    }
  }

  final List<RexNode> newProjects =
      RelOptUtil.pushPastProjectUnlessBloat(topProject.getProjects(),
          bottomProject, bloat);
  if (newProjects == null) {
    // Merged projects are significantly more complex. Do not merge.
    return;
  }
  final RelNode input = bottomProject.getInput();
  if (RexUtil.isIdentity(newProjects, input.getRowType())) {
    if (force
        || input.getRowType().getFieldNames()
            .equals(topProject.getRowType().getFieldNames())) {
      call.transformTo(input);
      return;
    }
  }

  // replace the two projects with a combined projection
  relBuilder.push(bottomProject.getInput());
  relBuilder.project(newProjects, topProject.getRowType().getFieldNames());
  call.transformTo(relBuilder.build());
}