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

The following examples show how to use org.apache.calcite.sql.SqlCallBinding#operand() . 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: 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 2
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 3
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 4
Source File: SqlCastFunction.java    From Bats with Apache License 2.0 5 votes vote down vote up
/**
 * Makes sure that the number and types of arguments are allowable.
 * Operators (such as "ROW" and "AS") which do not check their arguments can
 * override this method.
 */
public boolean checkOperandTypes(
    SqlCallBinding callBinding,
    boolean throwOnFailure) {
  final SqlNode left = callBinding.operand(0);
  final SqlNode right = callBinding.operand(1);
  if (SqlUtil.isNullLiteral(left, false)
      || left instanceof SqlDynamicParam) {
    return true;
  }
  RelDataType validatedNodeType =
      callBinding.getValidator().getValidatedNodeType(left);
  RelDataType returnType =
      callBinding.getValidator().deriveType(callBinding.getScope(), right);
  if (!SqlTypeUtil.canCastFrom(returnType, validatedNodeType, true)) {
    if (throwOnFailure) {
      throw callBinding.newError(
          RESOURCE.cannotCastValue(validatedNodeType.toString(),
              returnType.toString()));
    }
    return false;
  }
  if (SqlTypeUtil.areCharacterSetsMismatched(
      validatedNodeType,
      returnType)) {
    if (throwOnFailure) {
      // Include full type string to indicate character
      // set mismatch.
      throw callBinding.newError(
          RESOURCE.cannotCastValue(validatedNodeType.getFullTypeString(),
              returnType.getFullTypeString()));
    }
    return false;
  }
  return true;
}
 
Example 5
Source File: TypeInferenceUtils.java    From Bats with Apache License 2.0 5 votes vote down vote up
@Override
@SuppressWarnings("deprecation")
public RelDataType inferReturnType(SqlOperatorBinding opBinding) {
  final RelDataTypeFactory factory = opBinding.getTypeFactory();
  final boolean isNullable = opBinding
      .getOperandType(0)
      .isNullable();

  RelDataType ret = factory.createTypeWithNullability(
      opBinding.getOperandType(1),
      isNullable);
  if (opBinding instanceof SqlCallBinding) {
    SqlCallBinding callBinding = (SqlCallBinding) opBinding;
    SqlNode operand0 = callBinding.operand(0);

    // dynamic parameters and null constants need their types assigned
    // to them using the type they are casted to.
    if(((operand0 instanceof SqlLiteral)
        && (((SqlLiteral) operand0).getValue() == null))
            || (operand0 instanceof SqlDynamicParam)) {
      callBinding.getValidator().setValidatedNodeType(
          operand0,
          ret);
    }
  }

  return ret;
}
 
Example 6
Source File: SqlItemOperator.java    From calcite 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 SqlSingleOperandTypeChecker checker = getChecker(callBinding);
  return checker.checkSingleOperandType(callBinding, right, 0,
      throwOnFailure);
}
 
Example 7
Source File: SqlCastFunction.java    From calcite with Apache License 2.0 5 votes vote down vote up
/**
 * Makes sure that the number and types of arguments are allowable.
 * Operators (such as "ROW" and "AS") which do not check their arguments can
 * override this method.
 */
public boolean checkOperandTypes(
    SqlCallBinding callBinding,
    boolean throwOnFailure) {
  final SqlNode left = callBinding.operand(0);
  final SqlNode right = callBinding.operand(1);
  if (SqlUtil.isNullLiteral(left, false)
      || left instanceof SqlDynamicParam) {
    return true;
  }
  RelDataType validatedNodeType =
      callBinding.getValidator().getValidatedNodeType(left);
  RelDataType returnType =
      callBinding.getValidator().deriveType(callBinding.getScope(), right);
  if (!SqlTypeUtil.canCastFrom(returnType, validatedNodeType, true)) {
    if (throwOnFailure) {
      throw callBinding.newError(
          RESOURCE.cannotCastValue(validatedNodeType.toString(),
              returnType.toString()));
    }
    return false;
  }
  if (SqlTypeUtil.areCharacterSetsMismatched(
      validatedNodeType,
      returnType)) {
    if (throwOnFailure) {
      // Include full type string to indicate character
      // set mismatch.
      throw callBinding.newError(
          RESOURCE.cannotCastValue(validatedNodeType.getFullTypeString(),
              returnType.getFullTypeString()));
    }
    return false;
  }
  return true;
}
 
Example 8
Source File: MultisetOperandTypeChecker.java    From Bats with Apache License 2.0 4 votes vote down vote up
public boolean checkOperandTypes(
    SqlCallBinding callBinding,
    boolean throwOnFailure) {
  final SqlNode op0 = callBinding.operand(0);
  if (!OperandTypes.MULTISET.checkSingleOperandType(
      callBinding,
      op0,
      0,
      throwOnFailure)) {
    return false;
  }

  final SqlNode op1 = callBinding.operand(1);
  if (!OperandTypes.MULTISET.checkSingleOperandType(
      callBinding,
      op1,
      0,
      throwOnFailure)) {
    return false;
  }

  // TODO: this won't work if element types are of ROW types and there is
  // a mismatch.
  RelDataType biggest =
      callBinding.getTypeFactory().leastRestrictive(
          ImmutableList.of(
              callBinding.getValidator()
                  .deriveType(callBinding.getScope(), op0)
                  .getComponentType(),
              callBinding.getValidator()
                  .deriveType(callBinding.getScope(), op1)
                  .getComponentType()));
  if (null == biggest) {
    if (throwOnFailure) {
      throw callBinding.newError(
          RESOURCE.typeNotComparable(
              op0.getParserPosition().toString(),
              op1.getParserPosition().toString()));
    }

    return false;
  }
  return true;
}
 
Example 9
Source File: TypeInferenceUtils.java    From dremio-oss with Apache License 2.0 4 votes vote down vote up
@Override
public RelDataType inferReturnType(SqlOperatorBinding opBinding) {
  final RelDataTypeFactory factory = opBinding.getTypeFactory();
  SqlTypeName typeToCastTo = null;
  if (opBinding instanceof SqlCallBinding) {
    SqlCallBinding sqlCallBinding = (SqlCallBinding) opBinding;
    if (sqlCallBinding.operand(1).getKind() == SqlKind.LITERAL) {
      String type = null;
      try {
        SqlLiteral sqlLiteral = (SqlLiteral) sqlCallBinding.operand(1);
        type = ((NlsString) sqlLiteral.getValue()).getValue();
        switch(type) {
          case "JSON":
            typeToCastTo = SqlTypeName.ANY;
            break;
          case "UTF8":
          case "UTF16":
            typeToCastTo = SqlTypeName.VARCHAR;
            break;
          case "BOOLEAN_BYTE":
            typeToCastTo = SqlTypeName.BOOLEAN;
            break;
          case "TINYINT_BE":
          case "TINYINT":
            typeToCastTo = SqlTypeName.TINYINT;
            break;
          case "SMALLINT_BE":
          case "SMALLINT":
            typeToCastTo = SqlTypeName.SMALLINT;
            break;
          case "INT_BE":
          case "INT":
          case "INT_HADOOPV":
            typeToCastTo = SqlTypeName.INTEGER;
            break;
          case "BIGINT_BE":
          case "BIGINT":
          case "BIGINT_HADOOPV":
            typeToCastTo = SqlTypeName.BIGINT;
            break;
          case "FLOAT":
            typeToCastTo = SqlTypeName.FLOAT;
            break;
          case "DOUBLE":
            typeToCastTo = SqlTypeName.DOUBLE;
            break;
          case "DATE_EPOCH_BE":
          case "DATE_EPOCH":
            typeToCastTo = SqlTypeName.DATE;
            break;
          case "TIME_EPOCH_BE":
          case "TIME_EPOCH":
            typeToCastTo = SqlTypeName.TIME;
            break;
          case "TIMESTAMP_EPOCH":
          case "TIMESTAMP_IMPALA":
            typeToCastTo = SqlTypeName.TIMESTAMP;
            break;
          default:
            typeToCastTo = SqlTypeName.ANY;
            break;
        }
      } catch (final ClassCastException e) {
        logger.debug("Failed to parse string for convert_from()");
      }
    }
  }

  if (typeToCastTo == null) {
    typeToCastTo = SqlTypeName.ANY;
  }
  return factory.createTypeWithNullability(
      factory.createSqlType(typeToCastTo),
      true);
}
 
Example 10
Source File: MultisetOperandTypeChecker.java    From calcite with Apache License 2.0 4 votes vote down vote up
public boolean checkOperandTypes(
    SqlCallBinding callBinding,
    boolean throwOnFailure) {
  final SqlNode op0 = callBinding.operand(0);
  if (!OperandTypes.MULTISET.checkSingleOperandType(
      callBinding,
      op0,
      0,
      throwOnFailure)) {
    return false;
  }

  final SqlNode op1 = callBinding.operand(1);
  if (!OperandTypes.MULTISET.checkSingleOperandType(
      callBinding,
      op1,
      0,
      throwOnFailure)) {
    return false;
  }

  // TODO: this won't work if element types are of ROW types and there is
  // a mismatch.
  RelDataType biggest =
      callBinding.getTypeFactory().leastRestrictive(
          ImmutableList.of(
              callBinding.getValidator()
                  .deriveType(callBinding.getScope(), op0)
                  .getComponentType(),
              callBinding.getValidator()
                  .deriveType(callBinding.getScope(), op1)
                  .getComponentType()));
  if (null == biggest) {
    if (throwOnFailure) {
      throw callBinding.newError(
          RESOURCE.typeNotComparable(
              op0.getParserPosition().toString(),
              op1.getParserPosition().toString()));
    }

    return false;
  }
  return true;
}