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

The following examples show how to use org.apache.calcite.rex.RexUtil#generateCastExpressions() . 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: 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 2
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 3
Source File: StandardConvertletTable.java    From Bats with Apache License 2.0 5 votes vote down vote up
private static RexNode makeConstructorCall(
    SqlRexContext cx,
    SqlFunction constructor,
    List<RexNode> exprs) {
  final RexBuilder rexBuilder = cx.getRexBuilder();
  RelDataType type = rexBuilder.deriveReturnType(constructor, exprs);

  int n = type.getFieldCount();
  ImmutableList.Builder<RexNode> initializationExprs =
      ImmutableList.builder();
  final InitializerContext initializerContext = new InitializerContext() {
    public RexBuilder getRexBuilder() {
      return rexBuilder;
    }

    public RexNode convertExpression(SqlNode e) {
      throw new UnsupportedOperationException();
    }
  };
  for (int i = 0; i < n; ++i) {
    initializationExprs.add(
        cx.getInitializerExpressionFactory().newAttributeInitializer(
            type, constructor, i, exprs, initializerContext));
  }

  List<RexNode> defaultCasts =
      RexUtil.generateCastExpressions(
          rexBuilder,
          type,
          initializationExprs.build());

  return rexBuilder.makeNewInvocation(type, defaultCasts);
}
 
Example 4
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 5
Source File: StandardConvertletTable.java    From calcite with Apache License 2.0 5 votes vote down vote up
private static RexNode makeConstructorCall(
    SqlRexContext cx,
    SqlFunction constructor,
    List<RexNode> exprs) {
  final RexBuilder rexBuilder = cx.getRexBuilder();
  RelDataType type = rexBuilder.deriveReturnType(constructor, exprs);

  int n = type.getFieldCount();
  ImmutableList.Builder<RexNode> initializationExprs =
      ImmutableList.builder();
  final InitializerContext initializerContext = new InitializerContext() {
    public RexBuilder getRexBuilder() {
      return rexBuilder;
    }

    public SqlNode validateExpression(RelDataType rowType, SqlNode expr) {
      throw new UnsupportedOperationException();
    }

    public RexNode convertExpression(SqlNode e) {
      throw new UnsupportedOperationException();
    }
  };
  for (int i = 0; i < n; ++i) {
    initializationExprs.add(
        cx.getInitializerExpressionFactory().newAttributeInitializer(
            type, constructor, i, exprs, initializerContext));
  }

  List<RexNode> defaultCasts =
      RexUtil.generateCastExpressions(
          rexBuilder,
          type,
          initializationExprs.build());

  return rexBuilder.makeNewInvocation(type, defaultCasts);
}