Java Code Examples for org.apache.calcite.sql.SqlCallBinding#getOperandType()

The following examples show how to use org.apache.calcite.sql.SqlCallBinding#getOperandType() . 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: SqlJsonObjectFunction.java    From Bats with Apache License 2.0 6 votes vote down vote up
@Override public boolean checkOperandTypes(SqlCallBinding callBinding,
    boolean throwOnFailure) {
  final int count = callBinding.getOperandCount();
  for (int i = 1; i < count; i += 2) {
    RelDataType nameType = callBinding.getOperandType(i);
    if (!SqlTypeUtil.inCharFamily(nameType)) {
      if (throwOnFailure) {
        throw callBinding.newError(RESOURCE.expectedCharacter());
      }
      return false;
    }
    if (nameType.isNullable()) {
      if (throwOnFailure) {
        throw callBinding.newError(
            RESOURCE.argumentMustNotBeNull(
                callBinding.operand(i).toString()));
      }
      return false;
    }
  }
  return true;
}
 
Example 2
Source File: SqlDotOperator.java    From Bats with Apache License 2.0 6 votes vote down vote up
@Override public boolean checkOperandTypes(SqlCallBinding callBinding,
    boolean throwOnFailure) {
  final SqlNode left = callBinding.operand(0);
  final SqlNode right = callBinding.operand(1);
  final RelDataType type =
      callBinding.getValidator().deriveType(callBinding.getScope(), left);
  if (type.getSqlTypeName() != SqlTypeName.ROW) {
    return false;
  } else if (type.getSqlIdentifier().isStar()) {
    return false;
  }
  final RelDataType operandType = callBinding.getOperandType(0);
  final SqlSingleOperandTypeChecker checker = getChecker(operandType);
  return checker.checkSingleOperandType(callBinding, right, 0,
      throwOnFailure);
}
 
Example 3
Source File: ComparableOperandTypeChecker.java    From Bats with Apache License 2.0 6 votes vote down vote up
public boolean checkOperandTypes(
    SqlCallBinding callBinding,
    boolean throwOnFailure) {
  boolean b = true;
  for (int i = 0; i < nOperands; ++i) {
    RelDataType type = callBinding.getOperandType(i);
    if (!checkType(callBinding, throwOnFailure, type)) {
      b = false;
    }
  }
  if (b) {
    b = super.checkOperandTypes(callBinding, false);
    if (!b && throwOnFailure) {
      throw callBinding.newValidationSignatureError();
    }
  }
  return b;
}
 
Example 4
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 5
Source File: SqlItemOperator.java    From calcite with Apache License 2.0 6 votes vote down vote up
private SqlSingleOperandTypeChecker getChecker(SqlCallBinding callBinding) {
  final RelDataType operandType = callBinding.getOperandType(0);
  switch (operandType.getSqlTypeName()) {
  case ARRAY:
    return OperandTypes.family(SqlTypeFamily.INTEGER);
  case MAP:
    return OperandTypes.family(
        operandType.getKeyType().getSqlTypeName().getFamily());
  case ROW:
    return OperandTypes.CHARACTER;
  case ANY:
  case DYNAMIC_STAR:
    return OperandTypes.or(
        OperandTypes.family(SqlTypeFamily.INTEGER),
        OperandTypes.family(SqlTypeFamily.CHARACTER));
  default:
    throw callBinding.newValidationSignatureError();
  }
}
 
Example 6
Source File: SqlJsonObjectFunction.java    From calcite with Apache License 2.0 6 votes vote down vote up
@Override public boolean checkOperandTypes(SqlCallBinding callBinding,
    boolean throwOnFailure) {
  final int count = callBinding.getOperandCount();
  for (int i = 1; i < count; i += 2) {
    RelDataType nameType = callBinding.getOperandType(i);
    if (!SqlTypeUtil.inCharFamily(nameType)) {
      if (throwOnFailure) {
        throw callBinding.newError(RESOURCE.expectedCharacter());
      }
      return false;
    }
    if (nameType.isNullable()) {
      if (throwOnFailure) {
        throw callBinding.newError(
            RESOURCE.argumentMustNotBeNull(
                callBinding.operand(i).toString()));
      }
      return false;
    }
  }
  return true;
}
 
Example 7
Source File: SqlDotOperator.java    From calcite with Apache License 2.0 6 votes vote down vote up
@Override public boolean checkOperandTypes(SqlCallBinding callBinding,
    boolean throwOnFailure) {
  final SqlNode left = callBinding.operand(0);
  final SqlNode right = callBinding.operand(1);
  final RelDataType type =
      callBinding.getValidator().deriveType(callBinding.getScope(), left);
  if (type.getSqlTypeName() != SqlTypeName.ROW) {
    return false;
  } else if (type.getSqlIdentifier().isStar()) {
    return false;
  }
  final RelDataType operandType = callBinding.getOperandType(0);
  final SqlSingleOperandTypeChecker checker = getChecker(operandType);
  // Actually operand0 always comes from parsing the SqlIdentifier, so there
  // is no need to make implicit type coercion.
  return checker.checkSingleOperandType(callBinding, right, 0,
      throwOnFailure);
}
 
Example 8
Source File: SqlPosixRegexOperator.java    From calcite with Apache License 2.0 6 votes vote down vote up
public boolean checkOperandTypes(
    SqlCallBinding callBinding,
    boolean throwOnFailure) {
  int operandCount = callBinding.getOperandCount();
  if (operandCount != 2 && operandCount != 3) {
    throw new AssertionError(
        "Unexpected number of args to " + callBinding.getCall() + ": " + operandCount);
  }

  RelDataType op1Type = callBinding.getOperandType(0);
  RelDataType op2Type = callBinding.getOperandType(1);

  if (!SqlTypeUtil.isComparable(op1Type, op2Type)) {
    throw new AssertionError(
        "Incompatible first two operand types " + op1Type + " and " + op2Type);
  }

  return SqlTypeUtil.isCharTypeComparable(
      callBinding,
      callBinding.operands().subList(0, 2),
      throwOnFailure);
}
 
Example 9
Source File: ComparableOperandTypeChecker.java    From calcite with Apache License 2.0 6 votes vote down vote up
public boolean checkOperandTypes(
    SqlCallBinding callBinding,
    boolean throwOnFailure) {
  boolean b = true;
  for (int i = 0; i < nOperands; ++i) {
    RelDataType type = callBinding.getOperandType(i);
    if (!checkType(callBinding, throwOnFailure, type)) {
      b = false;
      break;
    }
  }
  if (b) {
    // Coerce type first.
    if (callBinding.isTypeCoercionEnabled()) {
      TypeCoercion typeCoercion = callBinding.getValidator().getTypeCoercion();
      // For comparison operators, i.e. >, <, =, >=, <=.
      typeCoercion.binaryComparisonCoercion(callBinding);
    }
    b = super.checkOperandTypes(callBinding, false);
  }
  if (!b && throwOnFailure) {
    throw callBinding.newValidationSignatureError();
  }
  return b;
}
 
Example 10
Source File: ComparableOperandTypeChecker.java    From calcite with Apache License 2.0 6 votes vote down vote up
/**
 * Similar functionality to
 * {@link #checkOperandTypes(SqlCallBinding, boolean)}, but not part of the
 * interface, and cannot throw an error.
 */
public boolean checkOperandTypes(
    SqlOperatorBinding operatorBinding, SqlCallBinding callBinding) {
  boolean b = true;
  for (int i = 0; i < nOperands; ++i) {
    RelDataType type = callBinding.getOperandType(i);
    if (type.getComparability().ordinal() < requiredComparability.ordinal()) {
      b = false;
      break;
    }
  }
  if (b) {
    b = super.checkOperandTypes(operatorBinding, callBinding);
  }
  return b;
}
 
Example 11
Source File: SqlItemOperator.java    From Bats with Apache License 2.0 5 votes vote down vote up
@Override public boolean checkOperandTypes(
    SqlCallBinding callBinding,
    boolean throwOnFailure) {
  final SqlNode left = callBinding.operand(0);
  final SqlNode right = callBinding.operand(1);
  if (!ARRAY_OR_MAP.checkSingleOperandType(callBinding, left, 0,
      throwOnFailure)) {
    return false;
  }
  final RelDataType operandType = callBinding.getOperandType(0);
  final SqlSingleOperandTypeChecker checker = getChecker(operandType);
  return checker.checkSingleOperandType(callBinding, right, 0,
      throwOnFailure);
}
 
Example 12
Source File: SqlOverlapsOperator.java    From Bats with Apache License 2.0 5 votes vote down vote up
public boolean checkOperandTypes(SqlCallBinding callBinding,
    boolean throwOnFailure) {
  if (!OperandTypes.PERIOD.checkSingleOperandType(callBinding,
      callBinding.operand(0), 0, throwOnFailure)) {
    return false;
  }
  final SqlSingleOperandTypeChecker rightChecker;
  switch (kind) {
  case CONTAINS:
    rightChecker = OperandTypes.PERIOD_OR_DATETIME;
    break;
  default:
    rightChecker = OperandTypes.PERIOD;
    break;
  }
  if (!rightChecker.checkSingleOperandType(callBinding,
      callBinding.operand(1), 0, throwOnFailure)) {
    return false;
  }
  final RelDataType t0 = callBinding.getOperandType(0);
  final RelDataType t1 = callBinding.getOperandType(1);
  if (!SqlTypeUtil.isDatetime(t1)) {
    final RelDataType t00 = t0.getFieldList().get(0).getType();
    final RelDataType t10 = t1.getFieldList().get(0).getType();
    if (!SqlTypeUtil.sameNamedType(t00, t10)) {
      if (throwOnFailure) {
        throw callBinding.newValidationSignatureError();
      }
      return false;
    }
  }
  return true;
}
 
Example 13
Source File: SqlOverlapsOperator.java    From calcite with Apache License 2.0 5 votes vote down vote up
public boolean checkOperandTypes(SqlCallBinding callBinding,
    boolean throwOnFailure) {
  if (!OperandTypes.PERIOD.checkSingleOperandType(callBinding,
      callBinding.operand(0), 0, throwOnFailure)) {
    return false;
  }
  final SqlSingleOperandTypeChecker rightChecker;
  switch (kind) {
  case CONTAINS:
    rightChecker = OperandTypes.PERIOD_OR_DATETIME;
    break;
  default:
    rightChecker = OperandTypes.PERIOD;
    break;
  }
  if (!rightChecker.checkSingleOperandType(callBinding,
      callBinding.operand(1), 0, throwOnFailure)) {
    return false;
  }
  final RelDataType t0 = callBinding.getOperandType(0);
  final RelDataType t1 = callBinding.getOperandType(1);
  if (!SqlTypeUtil.isDatetime(t1)) {
    final RelDataType t00 = t0.getFieldList().get(0).getType();
    final RelDataType t10 = t1.getFieldList().get(0).getType();
    if (!SqlTypeUtil.sameNamedType(t00, t10)) {
      if (throwOnFailure) {
        throw callBinding.newValidationSignatureError();
      }
      return false;
    }
  }
  return true;
}