Java Code Examples for org.apache.calcite.sql.type.SqlTypeUtil#isInterval()

The following examples show how to use org.apache.calcite.sql.type.SqlTypeUtil#isInterval() . 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: StandardConvertletTable.java    From Bats with Apache License 2.0 6 votes vote down vote up
private Pair<RexNode, RexNode> convertOverlapsOperand(SqlRexContext cx,
    SqlParserPos pos, SqlNode operand) {
  final SqlNode a0;
  final SqlNode a1;
  switch (operand.getKind()) {
  case ROW:
    a0 = ((SqlCall) operand).operand(0);
    final SqlNode a10 = ((SqlCall) operand).operand(1);
    final RelDataType t1 = cx.getValidator().getValidatedNodeType(a10);
    if (SqlTypeUtil.isInterval(t1)) {
      // make t1 = t0 + t1 when t1 is an interval.
      a1 = plus(pos, a0, a10);
    } else {
      a1 = a10;
    }
    break;
  default:
    a0 = operand;
    a1 = operand;
  }

  final RexNode r0 = cx.convertExpression(a0);
  final RexNode r1 = cx.convertExpression(a1);
  return Pair.of(r0, r1);
}
 
Example 2
Source File: DremioRexBuilder.java    From dremio-oss with Apache License 2.0 6 votes vote down vote up
@Override
public RexNode makeCast(
  RelDataType type,
  RexNode exp,
  boolean matchNullability) {
  // Special case: bypassing Calcite for interval types
  if (!(exp instanceof RexLiteral)
      && SqlTypeUtil.isExactNumeric(type)
      && SqlTypeUtil.isInterval(exp.getType())) {
    return makeAbstractCast(type, exp);
  }
  RexNode castRexNode = super.makeCast(type, exp, matchNullability);

  // If we have a CAST(A, TYPE) and A is already of the same TYPE (including nullability),
  // then return just A.
  if (castRexNode instanceof RexCall
    && castRexNode.getKind() == SqlKind.CAST
    && castRexNode.getType().equals(((RexCall) castRexNode).getOperands().get(0).getType())) {
    return ((RexCall) castRexNode).getOperands().get(0);
  }

  return castRexNode;
}
 
Example 3
Source File: StandardConvertletTable.java    From calcite with Apache License 2.0 6 votes vote down vote up
private Pair<RexNode, RexNode> convertOverlapsOperand(SqlRexContext cx,
    SqlParserPos pos, SqlNode operand) {
  final SqlNode a0;
  final SqlNode a1;
  switch (operand.getKind()) {
  case ROW:
    a0 = ((SqlCall) operand).operand(0);
    final SqlNode a10 = ((SqlCall) operand).operand(1);
    final RelDataType t1 = cx.getValidator().getValidatedNodeType(a10);
    if (SqlTypeUtil.isInterval(t1)) {
      // make t1 = t0 + t1 when t1 is an interval.
      a1 = plus(pos, a0, a10);
    } else {
      a1 = a10;
    }
    break;
  default:
    a0 = operand;
    a1 = operand;
  }

  final RexNode r0 = cx.convertExpression(a0);
  final RexNode r1 = cx.convertExpression(a1);
  return Pair.of(r0, r1);
}
 
Example 4
Source File: SqlTumbleTableFunction.java    From calcite with Apache License 2.0 6 votes vote down vote up
@Override public boolean checkOperandTypes(SqlCallBinding callBinding,
    boolean throwOnFailure) {
  // There should only be three operands, and number of operands are checked before
  // this call.
  final SqlNode operand0 = callBinding.operand(0);
  final SqlValidator validator = callBinding.getValidator();
  final RelDataType type = validator.getValidatedNodeType(operand0);
  if (type.getSqlTypeName() != SqlTypeName.ROW) {
    return throwValidationSignatureErrorOrReturnFalse(callBinding, throwOnFailure);
  }
  final SqlNode operand1 = callBinding.operand(1);
  if (operand1.getKind() != SqlKind.DESCRIPTOR) {
    return throwValidationSignatureErrorOrReturnFalse(callBinding, throwOnFailure);
  }
  validateColumnNames(validator, type.getFieldNames(), ((SqlCall) operand1).getOperandList());
  final RelDataType type2 = validator.getValidatedNodeType(callBinding.operand(2));
  if (!SqlTypeUtil.isInterval(type2)) {
    return throwValidationSignatureErrorOrReturnFalse(callBinding, throwOnFailure);
  }
  return true;
}
 
Example 5
Source File: SqlHopTableFunction.java    From calcite with Apache License 2.0 6 votes vote down vote up
@Override public boolean checkOperandTypes(SqlCallBinding callBinding,
    boolean throwOnFailure) {
  final SqlNode operand0 = callBinding.operand(0);
  final SqlValidator validator = callBinding.getValidator();
  final RelDataType type = validator.getValidatedNodeType(operand0);
  if (type.getSqlTypeName() != SqlTypeName.ROW) {
    return throwValidationSignatureErrorOrReturnFalse(callBinding, throwOnFailure);
  }
  final SqlNode operand1 = callBinding.operand(1);
  if (operand1.getKind() != SqlKind.DESCRIPTOR) {
    return throwValidationSignatureErrorOrReturnFalse(callBinding, throwOnFailure);
  }
  validateColumnNames(validator, type.getFieldNames(), ((SqlCall) operand1).getOperandList());
  final RelDataType type2 = validator.getValidatedNodeType(callBinding.operand(2));
  if (!SqlTypeUtil.isInterval(type2)) {
    return throwValidationSignatureErrorOrReturnFalse(callBinding, throwOnFailure);
  }
  final RelDataType type3 = validator.getValidatedNodeType(callBinding.operand(3));
  if (!SqlTypeUtil.isInterval(type3)) {
    return throwValidationSignatureErrorOrReturnFalse(callBinding, throwOnFailure);
  }
  return true;
}
 
Example 6
Source File: TypeCoercionImpl.java    From calcite with Apache License 2.0 6 votes vote down vote up
/**
 * Coerces operands in binary arithmetic expressions to NUMERIC types.
 *
 * <p>For binary arithmetic operators like [+, -, *, /, %]:
 * If the operand is VARCHAR,
 * coerce it to data type of the other operand if its data type is NUMERIC;
 * If the other operand is DECIMAL,
 * coerce the STRING operand to max precision/scale DECIMAL.
 */
public boolean binaryArithmeticCoercion(SqlCallBinding binding) {
  // Assume the operator has NUMERIC family operand type checker.
  SqlOperator operator = binding.getOperator();
  SqlKind kind = operator.getKind();
  boolean coerced = false;
  // Binary operator
  if (binding.getOperandCount() == 2) {
    final RelDataType type1 = binding.getOperandType(0);
    final RelDataType type2 = binding.getOperandType(1);
    // Special case for datetime + interval or datetime - interval
    if (kind == SqlKind.PLUS || kind == SqlKind.MINUS) {
      if (SqlTypeUtil.isInterval(type1) || SqlTypeUtil.isInterval(type2)) {
        return false;
      }
    }
    // Binary arithmetic operator like: + - * / %
    if (kind.belongsTo(SqlKind.BINARY_ARITHMETIC)) {
      coerced = binaryArithmeticWithStrings(binding, type1, type2);
    }
  }
  return coerced;
}
 
Example 7
Source File: SqlSessionTableFunction.java    From calcite with Apache License 2.0 6 votes vote down vote up
@Override public boolean checkOperandTypes(SqlCallBinding callBinding,
    boolean throwOnFailure) {
  final SqlNode operand0 = callBinding.operand(0);
  final SqlValidator validator = callBinding.getValidator();
  final RelDataType type = validator.getValidatedNodeType(operand0);
  if (type.getSqlTypeName() != SqlTypeName.ROW) {
    return throwValidationSignatureErrorOrReturnFalse(callBinding, throwOnFailure);
  }
  final SqlNode operand1 = callBinding.operand(1);
  if (operand1.getKind() != SqlKind.DESCRIPTOR) {
    return throwValidationSignatureErrorOrReturnFalse(callBinding, throwOnFailure);
  }
  validateColumnNames(validator, type.getFieldNames(), ((SqlCall) operand1).getOperandList());
  final SqlNode operand2 = callBinding.operand(2);
  if (operand2.getKind() != SqlKind.DESCRIPTOR) {
    return throwValidationSignatureErrorOrReturnFalse(callBinding, throwOnFailure);
  }
  validateColumnNames(validator, type.getFieldNames(), ((SqlCall) operand2).getOperandList());
  final RelDataType type3 = validator.getValidatedNodeType(callBinding.operand(3));
  if (!SqlTypeUtil.isInterval(type3)) {
    return throwValidationSignatureErrorOrReturnFalse(callBinding, throwOnFailure);
  }
  return true;
}