Java Code Examples for org.apache.calcite.rex.RexNode#getType()

The following examples show how to use org.apache.calcite.rex.RexNode#getType() . 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: ReduceExpressionsRule.java    From Bats with Apache License 2.0 6 votes vote down vote up
private RexNode visit(final RexNode call) {
  int i = reducibleExps.indexOf(call);
  if (i == -1) {
    return null;
  }
  RexNode replacement = reducedValues.get(i);
  if (addCasts.get(i)
      && (replacement.getType() != call.getType())) {
    // Handle change from nullable to NOT NULL by claiming
    // that the result is still nullable, even though
    // we know it isn't.
    //
    // Also, we cannot reduce CAST('abc' AS VARCHAR(4)) to 'abc'.
    // If we make 'abc' of type VARCHAR(4), we may later encounter
    // the same expression in a Project's digest where it has
    // type VARCHAR(3), and that's wrong.
    replacement =
        simplify.rexBuilder.makeAbstractCast(call.getType(), replacement);
  }
  return replacement;
}
 
Example 2
Source File: ReduceExpressionsRule.java    From calcite with Apache License 2.0 6 votes vote down vote up
private RexNode visit(final RexNode call) {
  int i = reducibleExps.indexOf(call);
  if (i == -1) {
    return null;
  }
  RexNode replacement = reducedValues.get(i);
  if (addCasts.get(i)
      && (replacement.getType() != call.getType())) {
    // Handle change from nullable to NOT NULL by claiming
    // that the result is still nullable, even though
    // we know it isn't.
    //
    // Also, we cannot reduce CAST('abc' AS VARCHAR(4)) to 'abc'.
    // If we make 'abc' of type VARCHAR(4), we may later encounter
    // the same expression in a Project's digest where it has
    // type VARCHAR(3), and that's wrong.
    replacement =
        simplify.rexBuilder.makeAbstractCast(call.getType(), replacement);
  }
  return replacement;
}
 
Example 3
Source File: ReduceDecimalsRule.java    From Bats with Apache License 2.0 5 votes vote down vote up
/**
 * Detect, in a generic, but strict way, whether it is possible to
 * simplify a reinterpret cast. The rules are as follows:
 *
 * <ol>
 * <li>If value is not the same basic type as outer, then we cannot
 * simplify
 * <li>If the value is nullable but the inner or outer are not, then we
 * cannot simplify.
 * <li>If inner is nullable but outer is not, we cannot simplify.
 * <li>If an overflow check is required from either inner or outer, we
 * cannot simplify.
 * <li>Otherwise, given the same type, and sufficient nullability
 * constraints, we can simplify.
 * </ol>
 *
 * @param outer outer call to reinterpret
 * @param inner inner call to reinterpret
 * @param value inner value
 * @return whether the two reinterpret casts can be removed
 */
private boolean canSimplify(RexCall outer, RexCall inner, RexNode value) {
    RelDataType outerType = outer.getType();
    RelDataType innerType = inner.getType();
    RelDataType valueType = value.getType();
    boolean outerCheck = RexUtil.canReinterpretOverflow(outer);
    boolean innerCheck = RexUtil.canReinterpretOverflow(inner);

    if ((outerType.getSqlTypeName() != valueType.getSqlTypeName())
            || (outerType.getPrecision() != valueType.getPrecision())
            || (outerType.getScale() != valueType.getScale())) {
        return false;
    }
    if (valueType.isNullable() && (!innerType.isNullable() || !outerType.isNullable())) {
        return false;
    }
    if (innerType.isNullable() && !outerType.isNullable()) {
        return false;
    }

    // One would think that we could go from Nullable -> Not Nullable
    // since we are substituting a general type with a more specific
    // type. However the optimizer doesn't like it.
    if (valueType.isNullable() != outerType.isNullable()) {
        return false;
    }
    if (innerCheck || outerCheck) {
        return false;
    }
    return true;
}
 
Example 4
Source File: StandardConvertletTable.java    From Bats with Apache License 2.0 5 votes vote down vote up
/**
 * Casts a RexNode value to the validated type of a SqlCall. If the value
 * was already of the validated type, then the value is returned without an
 * additional cast.
 */
public static RexNode castToValidatedType(SqlNode node, RexNode e,
    SqlValidator validator, RexBuilder rexBuilder) {
  final RelDataType type = validator.getValidatedNodeType(node);
  if (e.getType() == type) {
    return e;
  }
  return rexBuilder.makeCast(type, e);
}
 
Example 5
Source File: PigRelBuilder.java    From calcite with Apache License 2.0 5 votes vote down vote up
public RexNode dot(RexNode node, Object field) {
  if (field instanceof Integer) {
    int fieldIndex = (Integer) field;
    final RelDataType type = node.getType();
    if (type instanceof DynamicTupleRecordType) {
      ((DynamicTupleRecordType) type).resize(fieldIndex + 1);
    }
    return super.dot(node, fieldIndex);
  }
  return super.dot(node, (String) field);
}
 
Example 6
Source File: DruidDateTimeUtils.java    From calcite with Apache License 2.0 5 votes vote down vote up
/**
 * Infers granularity from a time unit.
 * It supports {@code FLOOR(<time> TO <timeunit>)}
 * and {@code EXTRACT(<timeunit> FROM <time>)}.
 * Returns null if it cannot be inferred.
 *
 * @param node the Rex node
 * @return the granularity, or null if it cannot be inferred
 */
@Nullable
public static Granularity extractGranularity(RexNode node, String timeZone) {
  final int valueIndex;
  final int flagIndex;

  if (TimeExtractionFunction.isValidTimeExtract(node)) {
    flagIndex = 0;
    valueIndex = 1;
  } else if (TimeExtractionFunction.isValidTimeFloor(node)) {
    valueIndex = 0;
    flagIndex = 1;
  } else {
    // We can only infer granularity from floor and extract.
    return null;
  }
  final RexCall call = (RexCall) node;
  final RexNode value = call.operands.get(valueIndex);
  final RexLiteral flag = (RexLiteral) call.operands.get(flagIndex);
  final TimeUnitRange timeUnit = (TimeUnitRange) flag.getValue();

  final RelDataType valueType = value.getType();
  if (valueType.getSqlTypeName() == SqlTypeName.DATE
      || valueType.getSqlTypeName() == SqlTypeName.TIMESTAMP) {
    // We use 'UTC' for date/timestamp type as Druid needs timezone information
    return Granularities.createGranularity(timeUnit, "UTC");
  } else if (valueType.getSqlTypeName() == SqlTypeName.TIMESTAMP_WITH_LOCAL_TIME_ZONE) {
    return Granularities.createGranularity(timeUnit, timeZone);
  }
  // Type not recognized
  return null;
}
 
Example 7
Source File: ReduceDecimalsRule.java    From calcite with Apache License 2.0 5 votes vote down vote up
/**
 * Detect, in a generic, but strict way, whether it is possible to
 * simplify a reinterpret cast. The rules are as follows:
 *
 * <ol>
 * <li>If value is not the same basic type as outer, then we cannot
 * simplify
 * <li>If the value is nullable but the inner or outer are not, then we
 * cannot simplify.
 * <li>If inner is nullable but outer is not, we cannot simplify.
 * <li>If an overflow check is required from either inner or outer, we
 * cannot simplify.
 * <li>Otherwise, given the same type, and sufficient nullability
 * constraints, we can simplify.
 * </ol>
 *
 * @param outer outer call to reinterpret
 * @param inner inner call to reinterpret
 * @param value inner value
 * @return whether the two reinterpret casts can be removed
 */
private boolean canSimplify(
    RexCall outer,
    RexCall inner,
    RexNode value) {
  RelDataType outerType = outer.getType();
  RelDataType innerType = inner.getType();
  RelDataType valueType = value.getType();
  boolean outerCheck = RexUtil.canReinterpretOverflow(outer);
  boolean innerCheck = RexUtil.canReinterpretOverflow(inner);

  if ((outerType.getSqlTypeName() != valueType.getSqlTypeName())
      || (outerType.getPrecision() != valueType.getPrecision())
      || (outerType.getScale() != valueType.getScale())) {
    return false;
  }
  if (valueType.isNullable()
      && (!innerType.isNullable() || !outerType.isNullable())) {
    return false;
  }
  if (innerType.isNullable() && !outerType.isNullable()) {
    return false;
  }

  // One would think that we could go from Nullable -> Not Nullable
  // since we are substituting a general type with a more specific
  // type. However the optimizer doesn't like it.
  if (valueType.isNullable() != outerType.isNullable()) {
    return false;
  }
  if (innerCheck || outerCheck) {
    return false;
  }
  return true;
}
 
Example 8
Source File: StandardConvertletTable.java    From calcite with Apache License 2.0 5 votes vote down vote up
/**
 * Casts a RexNode value to the validated type of a SqlCall. If the value
 * was already of the validated type, then the value is returned without an
 * additional cast.
 */
public static RexNode castToValidatedType(SqlNode node, RexNode e,
    SqlValidator validator, RexBuilder rexBuilder) {
  final RelDataType type = validator.getValidatedNodeType(node);
  if (e.getType() == type) {
    return e;
  }
  return rexBuilder.makeCast(type, e);
}
 
Example 9
Source File: VisitorDataContext.java    From Bats with Apache License 2.0 4 votes vote down vote up
public static Pair<Integer, ?> getValue(RexNode inputRef, RexNode literal) {
  inputRef = removeCast(inputRef);
  literal = removeCast(literal);

  if (inputRef instanceof RexInputRef
      && literal instanceof RexLiteral)  {
    final int index = ((RexInputRef) inputRef).getIndex();
    final RexLiteral rexLiteral = (RexLiteral) literal;
    final RelDataType type = inputRef.getType();

    if (type.getSqlTypeName() == null) {
      LOGGER.warn("{} returned null SqlTypeName", inputRef.toString());
      return null;
    }

    switch (type.getSqlTypeName()) {
    case INTEGER:
      return Pair.of(index, rexLiteral.getValueAs(Integer.class));
    case DOUBLE:
      return Pair.of(index, rexLiteral.getValueAs(Double.class));
    case REAL:
      return Pair.of(index, rexLiteral.getValueAs(Float.class));
    case BIGINT:
      return Pair.of(index, rexLiteral.getValueAs(Long.class));
    case SMALLINT:
      return Pair.of(index, rexLiteral.getValueAs(Short.class));
    case TINYINT:
      return Pair.of(index, rexLiteral.getValueAs(Byte.class));
    case DECIMAL:
      return Pair.of(index, rexLiteral.getValueAs(BigDecimal.class));
    case DATE:
    case TIME:
      return Pair.of(index, rexLiteral.getValueAs(Integer.class));
    case TIMESTAMP:
      return Pair.of(index, rexLiteral.getValueAs(Long.class));
    case CHAR:
      return Pair.of(index, rexLiteral.getValueAs(Character.class));
    case VARCHAR:
      return Pair.of(index, rexLiteral.getValueAs(String.class));
    default:
      // TODO: Support few more supported cases
      LOGGER.warn("{} for value of class {} is being handled in default way",
          type.getSqlTypeName(), rexLiteral.getValue().getClass());
      if (rexLiteral.getValue() instanceof NlsString) {
        return Pair.of(index, ((NlsString) rexLiteral.getValue()).getValue());
      } else {
        return Pair.of(index, rexLiteral.getValue());
      }
    }
  }

  // Unsupported Arguments
  return null;
}
 
Example 10
Source File: VisitorDataContext.java    From calcite with Apache License 2.0 4 votes vote down vote up
public static Pair<Integer, ?> getValue(RexNode inputRef, RexNode literal) {
  inputRef = inputRef == null ? null : RexUtil.removeCast(inputRef);
  literal = literal == null ? null : RexUtil.removeCast(literal);

  if (inputRef instanceof RexInputRef
      && literal instanceof RexLiteral)  {
    final int index = ((RexInputRef) inputRef).getIndex();
    final RexLiteral rexLiteral = (RexLiteral) literal;
    final RelDataType type = inputRef.getType();

    if (type.getSqlTypeName() == null) {
      LOGGER.warn("{} returned null SqlTypeName", inputRef.toString());
      return null;
    }

    switch (type.getSqlTypeName()) {
    case INTEGER:
      return Pair.of(index, rexLiteral.getValueAs(Integer.class));
    case DOUBLE:
      return Pair.of(index, rexLiteral.getValueAs(Double.class));
    case REAL:
      return Pair.of(index, rexLiteral.getValueAs(Float.class));
    case BIGINT:
      return Pair.of(index, rexLiteral.getValueAs(Long.class));
    case SMALLINT:
      return Pair.of(index, rexLiteral.getValueAs(Short.class));
    case TINYINT:
      return Pair.of(index, rexLiteral.getValueAs(Byte.class));
    case DECIMAL:
      return Pair.of(index, rexLiteral.getValueAs(BigDecimal.class));
    case DATE:
    case TIME:
      return Pair.of(index, rexLiteral.getValueAs(Integer.class));
    case TIMESTAMP:
      return Pair.of(index, rexLiteral.getValueAs(Long.class));
    case CHAR:
      return Pair.of(index, rexLiteral.getValueAs(Character.class));
    case VARCHAR:
      return Pair.of(index, rexLiteral.getValueAs(String.class));
    default:
      // TODO: Support few more supported cases
      LOGGER.warn("{} for value of class {} is being handled in default way",
          type.getSqlTypeName(), rexLiteral.getValue().getClass());
      if (rexLiteral.getValue() instanceof NlsString) {
        return Pair.of(index, ((NlsString) rexLiteral.getValue()).getValue());
      } else {
        return Pair.of(index, rexLiteral.getValue());
      }
    }
  }

  // Unsupported Arguments
  return null;
}
 
Example 11
Source File: RewriteProjectToFlattenRule.java    From dremio-oss with Apache License 2.0 3 votes vote down vote up
@Override
public RexNode visitCall(RexCall function, LevelHolder outgoingLevel) {
  if (!(function.getOperator() instanceof SqlFlattenOperator)) {
    return super.visitCall(function, outgoingLevel);
  }


  final int index = ((SqlFlattenOperator) function.getOperator()).getIndex();

  FlattenExpression flattenOutput = flattenOutputs.get(index);

  if(flattenOutput != null){
    outgoingLevel.index = flattenOutput.level + 1;
    return flattenOutput.inputReference;

  } else {
    Preconditions.checkArgument(function.getOperands().size() == 1, "Flatten only supports a single operand.");

    LevelHolder inputLevel = new LevelHolder();
    RexNode newRexInput = function.getOperands().get(0).accept(this, inputLevel);


    final MutableRexInputRef inputPointer = new MutableRexInputRef(newRexInput.getType());
    projectLevels.put(inputLevel.index, new ProjectSlotHolder(newRexInput, inputPointer));


    final FlattenExpression flatten = new FlattenExpression(inputLevel.index, inputPointer);
    flattenOutputs.put(index, flatten);
    flattenLevels.put(inputLevel.index, flatten);

    // the output level will be one more than the input.
    outgoingLevel.index = inputLevel.index + 1;
    return inputPointer;
  }

}