Java Code Examples for org.apache.calcite.plan.RelOptUtil#areRowTypesEqual()

The following examples show how to use org.apache.calcite.plan.RelOptUtil#areRowTypesEqual() . 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: 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 2
Source File: WriterUpdater.java    From dremio-oss with Apache License 2.0 6 votes vote down vote up
private Prel renameAsNecessary(RelDataType expectedRowType, Prel initialInput, WriterOptions.IcebergWriterOperation icebergWriterOperation) {
  boolean typesAndNamesExactMatch = RelOptUtil.areRowTypesEqual(initialInput.getRowType(), expectedRowType, true);
  boolean compatibleTypes;
  if (icebergWriterOperation == WriterOptions.IcebergWriterOperation.INSERT) {
    compatibleTypes = MoreRelOptUtil.areRowTypesCompatibleForInsert(initialInput.getRowType(), expectedRowType, false, true);
  } else {
    compatibleTypes = MoreRelOptUtil.areRowTypesCompatible(initialInput.getRowType(), expectedRowType, false, true);
  }

  // schemas match exactly, or no chance of matching
  // we don't need to do any transformation or there is no use of transformation
  if (typesAndNamesExactMatch || !compatibleTypes) {
    return initialInput;
  }

  final RexBuilder rexBuilder = initialInput.getCluster().getRexBuilder();
  final List<RexNode> castExps =
    RexUtil.generateCastExpressions(rexBuilder, expectedRowType, initialInput.getRowType());

  return ProjectPrel.create(initialInput.getCluster(), initialInput.getTraitSet(), initialInput,
    castExps, expectedRowType);
}
 
Example 3
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 4
Source File: MutableRels.java    From Bats with Apache License 2.0 5 votes vote down vote up
/** Equivalence to {@link org.apache.calcite.plan.RelOptUtil#createCastRel}
 * for {@link MutableRel}. */
public static MutableRel createCastRel(MutableRel rel,
    RelDataType castRowType, boolean rename) {
  RelDataType rowType = rel.rowType;
  if (RelOptUtil.areRowTypesEqual(rowType, castRowType, rename)) {
    // nothing to do
    return rel;
  }
  List<RexNode> castExps =
      RexUtil.generateCastExpressions(rel.cluster.getRexBuilder(),
          castRowType, rowType);
  final List<String> fieldNames =
      rename ? castRowType.getFieldNames() : rowType.getFieldNames();
  return MutableProject.of(rel, castExps, fieldNames);
}
 
Example 5
Source File: MutableRels.java    From calcite with Apache License 2.0 5 votes vote down vote up
/** Equivalence to {@link org.apache.calcite.plan.RelOptUtil#createCastRel}
 * for {@link MutableRel}. */
public static MutableRel createCastRel(MutableRel rel,
    RelDataType castRowType, boolean rename) {
  RelDataType rowType = rel.rowType;
  if (RelOptUtil.areRowTypesEqual(rowType, castRowType, rename)) {
    // nothing to do
    return rel;
  }
  List<RexNode> castExps =
      RexUtil.generateCastExpressions(rel.cluster.getRexBuilder(),
          castRowType, rowType);
  final List<String> fieldNames =
      rename ? castRowType.getFieldNames() : rowType.getFieldNames();
  return MutableProject.of(rel, castExps, fieldNames);
}
 
Example 6
Source File: AggregateUnionAggregateRule.java    From calcite with Apache License 2.0 5 votes vote down vote up
/**
 * Returns an input with the same row type with the input Aggregate,
 * create a Project node if needed.
 */
private RelNode getInputWithSameRowType(Aggregate bottomAggRel) {
  if (RelOptUtil.areRowTypesEqual(
          bottomAggRel.getRowType(),
          bottomAggRel.getInput(0).getRowType(),
          false)) {
    return bottomAggRel.getInput(0);
  } else {
    return RelOptUtil.createProject(
        bottomAggRel.getInput(),
        bottomAggRel.getGroupSet().asList());
  }
}
 
Example 7
Source File: InvalidViewRel.java    From dremio-oss with Apache License 2.0 2 votes vote down vote up
/**
 * Compare row types using deep equality.
 * @param r1 RowType 1
 * @param r2 RowType 2
 * @return True if field types and names are equal, even if the top level type is not identity equal.
 */
public static boolean equalsRowTypeDeep(RelDataType r1, RelDataType r2) {
  // Note that we compare names separately because there is an issue with record type canonicalization. See DX-17725
  return RelOptUtil.areRowTypesEqual(r1, r2, false) && Objects.deepEquals(r1.getFieldNames(), r2.getFieldNames());
}