Java Code Examples for org.apache.calcite.rex.RexShuttle#apply()

The following examples show how to use org.apache.calcite.rex.RexShuttle#apply() . 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: MultiJoin.java    From Bats with Apache License 2.0 6 votes vote down vote up
public RelNode accept(RexShuttle shuttle) {
  RexNode joinFilter = shuttle.apply(this.joinFilter);
  List<RexNode> outerJoinConditions = shuttle.apply(this.outerJoinConditions);
  RexNode postJoinFilter = shuttle.apply(this.postJoinFilter);

  if (joinFilter == this.joinFilter
      && outerJoinConditions == this.outerJoinConditions
      && postJoinFilter == this.postJoinFilter) {
    return this;
  }

  return new MultiJoin(
      getCluster(),
      inputs,
      joinFilter,
      rowType,
      isFullOuterJoin,
      outerJoinConditions,
      joinTypes,
      projFields,
      joinFieldRefCountsMap,
      postJoinFilter);
}
 
Example 2
Source File: AbstractMaterializedViewRule.java    From Bats with Apache License 2.0 6 votes vote down vote up
/**
 * Replaces all the input references by the position in the
 * input column set. If a reference index cannot be found in
 * the input set, then we return null.
 */
private static RexNode shuttleReferences(final RexBuilder rexBuilder, final RexNode node, final Mapping mapping) {
    try {
        RexShuttle visitor = new RexShuttle() {
            @Override
            public RexNode visitInputRef(RexInputRef inputRef) {
                int pos = mapping.getTargetOpt(inputRef.getIndex());
                if (pos != -1) {
                    // Found it
                    return rexBuilder.makeInputRef(inputRef.getType(), pos);
                }
                throw Util.FoundOne.NULL;
            }
        };
        return visitor.apply(node);
    } catch (Util.FoundOne ex) {
        Util.swallow(ex, null);
        return null;
    }
}
 
Example 3
Source File: MultiJoin.java    From calcite with Apache License 2.0 6 votes vote down vote up
public RelNode accept(RexShuttle shuttle) {
  RexNode joinFilter = shuttle.apply(this.joinFilter);
  List<RexNode> outerJoinConditions = shuttle.apply(this.outerJoinConditions);
  RexNode postJoinFilter = shuttle.apply(this.postJoinFilter);

  if (joinFilter == this.joinFilter
      && outerJoinConditions == this.outerJoinConditions
      && postJoinFilter == this.postJoinFilter) {
    return this;
  }

  return new MultiJoin(
      getCluster(),
      inputs,
      joinFilter,
      rowType,
      isFullOuterJoin,
      outerJoinConditions,
      joinTypes,
      projFields,
      joinFieldRefCountsMap,
      postJoinFilter);
}
 
Example 4
Source File: MaterializedViewSubstitutionVisitor.java    From Bats with Apache License 2.0 6 votes vote down vote up
private static List<RexNode> transformRex(List<RexNode> nodes, final List<RelDataTypeField> oldFields,
        final List<RelDataTypeField> newFields) {
    RexShuttle shuttle = new RexShuttle() {
        @Override
        public RexNode visitInputRef(RexInputRef ref) {
            RelDataTypeField f = oldFields.get(ref.getIndex());
            for (int index = 0; index < newFields.size(); index++) {
                RelDataTypeField newf = newFields.get(index);
                if (f.getKey().equals(newf.getKey()) && f.getValue() == newf.getValue()) {
                    return RexBuilder.getRexFactory().makeInputRef(index, f.getValue());
                }
            }
            throw MatchFailed.INSTANCE;
        }
    };
    return shuttle.apply(nodes);
}
 
Example 5
Source File: SubstitutionVisitor.java    From Bats with Apache License 2.0 6 votes vote down vote up
@Override
public UnifyResult apply(UnifyRuleCall call) {
    final MutableProject target = (MutableProject) call.target;
    final MutableScan query = (MutableScan) call.query;
    // We do not need to check query's parent type to avoid duplication
    // of ProjectToProjectUnifyRule or FilterToProjectUnifyRule, since
    // SubstitutionVisitor performs a top-down match.
    if (!query.equals(target.getInput())) {
        return null;
    }
    final RexShuttle shuttle = getRexShuttle(target);
    final RexBuilder rexBuilder = target.cluster.getRexBuilder();
    final List<RexNode> newProjects;
    try {
        newProjects = (List<RexNode>) shuttle.apply(rexBuilder.identityProjects(query.rowType));
    } catch (MatchFailed e) {
        return null;
    }
    final MutableProject newProject = MutableProject.of(query.rowType, target, newProjects);
    final MutableRel newProject2 = MutableRels.strip(newProject);
    return call.result(newProject2);
}
 
Example 6
Source File: RelMdColumnOrigins.java    From calcite with Apache License 2.0 6 votes vote down vote up
public Set<RelColumnOrigin> getColumnOrigins(Calc rel,
    final RelMetadataQuery mq, int iOutputColumn) {
  final RelNode input = rel.getInput();
  final RexShuttle rexShuttle = new RexShuttle() {
    @Override public RexNode visitLocalRef(RexLocalRef localRef) {
      return rel.getProgram().expandLocalRef(localRef);
    }
  };
  final List<RexNode> projects = new ArrayList<>();
  for (RexNode rex: rexShuttle.apply(rel.getProgram().getProjectList())) {
    projects.add(rex);
  }
  final RexNode rexNode = projects.get(iOutputColumn);
  if (rexNode instanceof RexInputRef) {
    // Direct reference:  no derivation added.
    RexInputRef inputRef = (RexInputRef) rexNode;
    return mq.getColumnOrigins(input, inputRef.getIndex());
  }
  // Anything else is a derivation, possibly from multiple columns.
  final Set<RelColumnOrigin> set = getMultipleColumns(rexNode, input, mq);
  return createDerivedColumnOrigins(set);
}
 
Example 7
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 8
Source File: Project.java    From calcite with Apache License 2.0 5 votes vote down vote up
public RelNode accept(RexShuttle shuttle) {
  List<RexNode> exps = shuttle.apply(this.exps);
  if (this.exps == exps) {
    return this;
  }
  final RelDataType rowType =
      RexUtil.createStructType(
          getInput().getCluster().getTypeFactory(),
          exps,
          this.rowType.getFieldNames(),
          null);
  return copy(traitSet, getInput(), exps, rowType);
}
 
Example 9
Source File: Snapshot.java    From calcite with Apache License 2.0 5 votes vote down vote up
public RelNode accept(RexShuttle shuttle) {
  RexNode condition = shuttle.apply(this.period);
  if (this.period == condition) {
    return this;
  }
  return copy(traitSet, getInput(), condition);
}
 
Example 10
Source File: Filter.java    From calcite with Apache License 2.0 5 votes vote down vote up
public RelNode accept(RexShuttle shuttle) {
  RexNode condition = shuttle.apply(this.condition);
  if (this.condition == condition) {
    return this;
  }
  return copy(traitSet, getInput(), condition);
}
 
Example 11
Source File: Join.java    From calcite with Apache License 2.0 5 votes vote down vote up
@Override public RelNode accept(RexShuttle shuttle) {
  RexNode condition = shuttle.apply(this.condition);
  if (this.condition == condition) {
    return this;
  }
  return copy(traitSet, condition, left, right, joinType, isSemiJoinDone());
}
 
Example 12
Source File: SubstitutionVisitor.java    From calcite with Apache License 2.0 5 votes vote down vote up
@Override protected UnifyResult apply(UnifyRuleCall call) {

      final MutableScan query = (MutableScan) call.query;

      final MutableCalc target = (MutableCalc) call.target;
      final MutableScan targetInput = (MutableScan) target.getInput();
      final Pair<RexNode, List<RexNode>> targetExplained = explainCalc(target);
      final RexNode targetCond = targetExplained.left;
      final List<RexNode> targetProjs = targetExplained.right;

      final RexBuilder rexBuilder = call.getCluster().getRexBuilder();

      if (!query.equals(targetInput) || !targetCond.isAlwaysTrue()) {
        return null;
      }
      final RexShuttle shuttle = getRexShuttle(targetProjs);
      final List<RexNode> compenProjs;
      try {
        compenProjs = (List<RexNode>) shuttle.apply(
            rexBuilder.identityProjects(query.rowType));
      } catch (MatchFailed e) {
        return null;
      }
      if (RexUtil.isIdentity(compenProjs, target.rowType)) {
        return call.result(target);
      } else {
        RexProgram compenRexProgram = RexProgram.create(
            target.rowType, compenProjs, null, query.rowType, rexBuilder);
        MutableCalc compenCalc = MutableCalc.of(target, compenRexProgram);
        return tryMergeParentCalcAndGenResult(call, compenCalc);
      }
    }
 
Example 13
Source File: TableFunctionScan.java    From calcite with Apache License 2.0 5 votes vote down vote up
public RelNode accept(RexShuttle shuttle) {
  RexNode rexCall = shuttle.apply(this.rexCall);
  if (rexCall == this.rexCall) {
    return this;
  }
  return copy(traitSet, inputs, rexCall, elementType, rowType,
      columnMappings);
}
 
Example 14
Source File: SubstitutionVisitor.java    From Bats with Apache License 2.0 5 votes vote down vote up
@Override
public UnifyResult apply(UnifyRuleCall call) {
    final MutableProject target = (MutableProject) call.target;
    final MutableProject query = (MutableProject) call.query;
    final RexShuttle shuttle = getRexShuttle(target);
    final List<RexNode> newProjects;
    try {
        newProjects = shuttle.apply(query.projects);
    } catch (MatchFailed e) {
        return null;
    }
    final MutableProject newProject = MutableProject.of(query.rowType, target, newProjects);
    final MutableRel newProject2 = MutableRels.strip(newProject);
    return call.result(newProject2);
}
 
Example 15
Source File: TableFunctionScan.java    From Bats with Apache License 2.0 5 votes vote down vote up
public RelNode accept(RexShuttle shuttle) {
  RexNode rexCall = shuttle.apply(this.rexCall);
  if (rexCall == this.rexCall) {
    return this;
  }
  return copy(traitSet, inputs, rexCall, elementType, rowType,
      columnMappings);
}
 
Example 16
Source File: SubstitutionVisitor.java    From calcite with Apache License 2.0 5 votes vote down vote up
/** Explain filtering condition and projections from MutableCalc. */
private static Pair<RexNode, List<RexNode>> explainCalc(MutableCalc calc) {
  final RexShuttle shuttle = getExpandShuttle(calc.program);
  final RexNode condition = shuttle.apply(calc.program.getCondition());
  final List<RexNode> projects = new ArrayList<>();
  for (RexNode rex: shuttle.apply(calc.program.getProjectList())) {
    projects.add(rex);
  }
  if (condition == null) {
    return Pair.of(calc.cluster.getRexBuilder().makeLiteral(true), projects);
  } else {
    return Pair.of(condition, projects);
  }
}
 
Example 17
Source File: Filter.java    From Bats with Apache License 2.0 5 votes vote down vote up
public RelNode accept(RexShuttle shuttle) {
  RexNode condition = shuttle.apply(this.condition);
  if (this.condition == condition) {
    return this;
  }
  return copy(traitSet, getInput(), condition);
}
 
Example 18
Source File: Join.java    From Bats with Apache License 2.0 5 votes vote down vote up
@Override public RelNode accept(RexShuttle shuttle) {
  RexNode condition = shuttle.apply(this.condition);
  if (this.condition == condition) {
    return this;
  }
  return copy(traitSet, condition, left, right, joinType, isSemiJoinDone());
}
 
Example 19
Source File: ProjectNLJMergeRule.java    From dremio-oss with Apache License 2.0 4 votes vote down vote up
@Override
public void onMatch(RelOptRuleCall call) {
  ProjectPrel project = call.rel(0);
  NestedLoopJoinPrel nlj = call.rel(1);

  ImmutableBitSet topProjectedColumns = InputFinder.bits(project.getProjects(), null);

  ImmutableBitSet bottomProjectedColumns = null;
  if (nlj.getProjectedFields() == null) {
    bottomProjectedColumns = ImmutableBitSet.range(nlj.getRowType().getFieldCount());
  } else {
    bottomProjectedColumns = nlj.getProjectedFields();
  }

  ImmutableBitSet.Builder builder = ImmutableBitSet.builder();
  int field = 0;
  Mapping mapping = Mappings.create(MappingType.SURJECTION, bottomProjectedColumns.cardinality(), topProjectedColumns.cardinality());
  for (Ord<Integer> ord : Ord.zip(bottomProjectedColumns)) {
    if (topProjectedColumns.get(ord.i)) {
      builder.set(ord.e);
      mapping.set(ord.i, field);
      field++;
    }
  }

  if (builder.cardinality() == 0) {
    //project at least one column
    builder.set(0);
  }

  ImmutableBitSet newJoinProjectedFields = builder.build();

  if (newJoinProjectedFields.equals(nlj.getProjectedFields())) {
    return;
  }

  RexShuttle shuttle = new RexPermuteInputsShuttle(mapping);
  List<RexNode> newProjects = shuttle.apply(project.getProjects());

  NestedLoopJoinPrel newJoin = (NestedLoopJoinPrel) nlj.copy(newJoinProjectedFields);
  ProjectPrel newProject = ProjectPrel.create(nlj.getCluster(), project.getTraitSet(), newJoin, newProjects, project.getRowType());
  call.transformTo(newProject);
}
 
Example 20
Source File: SubstitutionVisitor.java    From calcite with Apache License 2.0 4 votes vote down vote up
public UnifyResult apply(UnifyRuleCall call) {
  final MutableCalc query = (MutableCalc) call.query;
  final Pair<RexNode, List<RexNode>> queryExplained = 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 = explainCalc(target);
  final RexNode targetCond = targetExplained.left;
  final List<RexNode> targetProjs = targetExplained.right;

  final RexBuilder rexBuilder = call.getCluster().getRexBuilder();

  try {
    final RexShuttle shuttle = getRexShuttle(targetProjs);
    final RexNode splitted =
        splitFilter(call.getSimplify(), queryCond, targetCond);

    final RexNode compenCond;
    if (splitted != null) {
      if (splitted.isAlwaysTrue()) {
        compenCond = null;
      } else {
        // Compensate the residual filtering condition.
        compenCond = shuttle.apply(splitted);
      }
    } else if (implies(
        call.getCluster(), queryCond, targetCond, query.getInput().rowType)) {
      // Fail to split filtering condition, but implies that target contains
      // all lines of query, thus just set compensating filtering condition
      // as the filtering condition of query.
      compenCond = shuttle.apply(queryCond);
    } else {
      return null;
    }

    final List<RexNode> compenProjs = shuttle.apply(queryProjs);
    if (compenCond == null
        && RexUtil.isIdentity(compenProjs, target.rowType)) {
      return call.result(target);
    } else {
      final RexProgram compenRexProgram = RexProgram.create(
          target.rowType, compenProjs, compenCond,
          query.rowType, rexBuilder);
      final MutableCalc compenCalc = MutableCalc.of(target, compenRexProgram);
      return tryMergeParentCalcAndGenResult(call, compenCalc);
    }
  } catch (MatchFailed e) {
    return null;
  }
}