Java Code Examples for org.apache.calcite.rex.RexUtil#isIdentity()

The following examples show how to use org.apache.calcite.rex.RexUtil#isIdentity() . 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: RelBuilder.java    From Bats with Apache License 2.0 6 votes vote down vote up
/** Creates a {@link Project} of the given
 * expressions and field names, and optionally optimizing.
 *
 * <p>If {@code fieldNames} is null, or if a particular entry in
 * {@code fieldNames} is null, derives field names from the input
 * expressions.
 *
 * <p>If {@code force} is false,
 * and the input is a {@code Project},
 * and the expressions  make the trivial projection ($0, $1, ...),
 * modifies the input.
 *
 * @param nodes       Expressions
 * @param fieldNames  Suggested field names, or null to generate
 * @param force       Whether to create a renaming Project if the
 *                    projections are trivial
 */
public RelBuilder projectNamed(Iterable<? extends RexNode> nodes, Iterable<String> fieldNames, boolean force) {
    @SuppressWarnings({ "unchecked", "rawtypes" })
    final List<? extends RexNode> nodeList = nodes instanceof List ? (List) nodes : ImmutableList.copyOf(nodes);
    final List<String> fieldNameList = fieldNames == null ? null
            : fieldNames instanceof List ? (List<String>) fieldNames : ImmutableNullableList.copyOf(fieldNames);
    final RelNode input = peek();
    final RelDataType rowType = RexUtil.createStructType(cluster.getTypeFactory(), nodeList, fieldNameList,
            SqlValidatorUtil.F_SUGGESTER);
    if (!force && RexUtil.isIdentity(nodeList, input.getRowType())) {
        if (input instanceof Project && fieldNames != null) {
            // Rename columns of child projection if desired field names are given.
            final Frame frame = stack.pop();
            final Project childProject = (Project) frame.rel;
            final Project newInput = childProject.copy(childProject.getTraitSet(), childProject.getInput(),
                    childProject.getProjects(), rowType);
            stack.push(new Frame(newInput, frame.fields));
        }
    } else {
        project(nodeList, rowType.getFieldNames(), force);
    }
    return this;
}
 
Example 2
Source File: RelBuilder.java    From calcite with Apache License 2.0 5 votes vote down vote up
/** Creates a {@link Project} of the given
 * expressions and field names, and optionally optimizing.
 *
 * <p>If {@code fieldNames} is null, or if a particular entry in
 * {@code fieldNames} is null, derives field names from the input
 * expressions.
 *
 * <p>If {@code force} is false,
 * and the input is a {@code Project},
 * and the expressions  make the trivial projection ($0, $1, ...),
 * modifies the input.
 *
 * @param nodes       Expressions
 * @param fieldNames  Suggested field names, or null to generate
 * @param force       Whether to create a renaming Project if the
 *                    projections are trivial
 */
public RelBuilder projectNamed(Iterable<? extends RexNode> nodes,
    Iterable<String> fieldNames, boolean force) {
  @SuppressWarnings("unchecked") final List<? extends RexNode> nodeList =
      nodes instanceof List ? (List) nodes : ImmutableList.copyOf(nodes);
  final List<String> fieldNameList =
      fieldNames == null ? null
        : fieldNames instanceof List ? (List<String>) fieldNames
        : ImmutableNullableList.copyOf(fieldNames);
  final RelNode input = peek();
  final RelDataType rowType =
      RexUtil.createStructType(cluster.getTypeFactory(), nodeList,
          fieldNameList, SqlValidatorUtil.F_SUGGESTER);
  if (!force
      && RexUtil.isIdentity(nodeList, input.getRowType())) {
    if (input instanceof Project && fieldNames != null) {
      // Rename columns of child projection if desired field names are given.
      final Frame frame = stack.pop();
      final Project childProject = (Project) frame.rel;
      final Project newInput = childProject.copy(childProject.getTraitSet(),
          childProject.getInput(), childProject.getProjects(), rowType);
      stack.push(new Frame(newInput.attachHints(childProject.getHints()), frame.fields));
    }
  } else {
    project(nodeList, rowType.getFieldNames(), force);
  }
  return this;
}
 
Example 3
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 4
Source File: MutableRels.java    From Bats with Apache License 2.0 4 votes vote down vote up
/** Based on
 * {@link org.apache.calcite.rel.rules.ProjectRemoveRule#isTrivial(org.apache.calcite.rel.core.Project)}. */
public static boolean isTrivial(MutableProject project) {
  MutableRel child = project.getInput();
  return RexUtil.isIdentity(project.projects, child.rowType);
}
 
Example 5
Source File: ProjectRemoveRule.java    From Bats with Apache License 2.0 4 votes vote down vote up
public static boolean isTrivial(Project project) {
  return RexUtil.isIdentity(project.getProjects(),
      project.getInput().getRowType());
}
 
Example 6
Source File: ProjectRemoveRule.java    From Bats with Apache License 2.0 4 votes vote down vote up
@Deprecated // to be removed before 1.5
public static boolean isIdentity(List<? extends RexNode> exps,
    RelDataType childRowType) {
  return RexUtil.isIdentity(exps, childRowType);
}
 
Example 7
Source File: ProjectMergeRule.java    From Bats 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.pushPastProject(topProject.getProjects(), bottomProject);
  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());
}
 
Example 8
Source File: DrillMergeProjectRule.java    From Bats with Apache License 2.0 4 votes vote down vote up
@Override
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> pushedProjects =
      RelOptUtil.pushPastProject(topProject.getProjects(), bottomProject);
  final List<RexNode> newProjects = simplifyCast(pushedProjects);
  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());
}
 
Example 9
Source File: MergeProjectRule.java    From dremio-oss with Apache License 2.0 4 votes vote down vote up
@Override
public void onMatch(RelOptRuleCall call) {
  final Project topProject = call.rel(0);
  final Project bottomProject = call.rel(1);
  final RelBuilder relBuilder = call.builder();

  // merge projects assuming it doesn't alter the unique count of flattens.
  final FlattenCounter counter = new FlattenCounter();
  counter.add(topProject);
  counter.add(bottomProject);
  final int uniqueFlattens = counter.getCount();

  // 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());
      List<RexNode> exprs = relBuilder.fields(product);
      relBuilder.project(exprs, topProject.getRowType().getFieldNames());

      if(FlattenVisitors.count(exprs) == uniqueFlattens){
        call.transformTo(relBuilder.build());
      }
      return;
    }
  }

  final List<RexNode> newProjects =
      RelOptUtil.pushPastProject(topProject.getProjects(), bottomProject);
  final RelNode input = bottomProject.getInput();
  if (RexUtil.isIdentity(newProjects, input.getRowType()) && uniqueFlattens == 0) {
    call.transformTo(input);
    return;
  }

  // replace the two projects with a combined projection
  relBuilder.push(bottomProject.getInput());
  relBuilder.project(newProjects, topProject.getRowType().getFieldNames());
  if(FlattenVisitors.count(newProjects) == uniqueFlattens){
    call.transformTo(relBuilder.build());
  }
}
 
Example 10
Source File: MutableRels.java    From calcite with Apache License 2.0 4 votes vote down vote up
/** Based on
 * {@link org.apache.calcite.rel.rules.ProjectRemoveRule#isTrivial(org.apache.calcite.rel.core.Project)}. */
public static boolean isTrivial(MutableProject project) {
  MutableRel child = project.getInput();
  return RexUtil.isIdentity(project.projects, child.rowType);
}
 
Example 11
Source File: ProjectRemoveRule.java    From calcite with Apache License 2.0 4 votes vote down vote up
public static boolean isTrivial(Project project) {
  return RexUtil.isIdentity(project.getProjects(),
      project.getInput().getRowType());
}
 
Example 12
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());
}
 
Example 13
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;
  }
}