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

The following examples show how to use org.apache.calcite.sql.SqlCallBinding#newValidationSignatureError() . 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: 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 2
Source File: CompositeOperandTypeChecker.java    From Bats with Apache License 2.0 6 votes vote down vote up
public boolean checkOperandTypes(
    SqlCallBinding callBinding,
    boolean throwOnFailure) {
  if (check(callBinding)) {
    return true;
  }
  if (!throwOnFailure) {
    return false;
  }
  if (composition == Composition.OR) {
    for (SqlOperandTypeChecker allowedRule : allowedRules) {
      allowedRule.checkOperandTypes(callBinding, true);
    }
  }

  // If no exception thrown, just throw a generic validation
  // signature error.
  throw callBinding.newValidationSignatureError();
}
 
Example 3
Source File: AssignableOperandTypeChecker.java    From calcite with Apache License 2.0 6 votes vote down vote up
public boolean checkOperandTypes(
    SqlCallBinding callBinding,
    boolean throwOnFailure) {
  // Do not use callBinding.operands(). We have not resolved to a function
  // yet, therefore we do not know the ordered parameter names.
  final List<SqlNode> operands = callBinding.getCall().getOperandList();
  for (Pair<RelDataType, SqlNode> pair : Pair.zip(paramTypes, operands)) {
    RelDataType argType =
        callBinding.getValidator().deriveType(
            callBinding.getScope(),
            pair.right);
    if (!SqlTypeUtil.canAssignFrom(pair.left, argType)) {
      // TODO: add in unresolved function type cast.
      if (throwOnFailure) {
        throw callBinding.newValidationSignatureError();
      } else {
        return false;
      }
    }
  }
  return true;
}
 
Example 4
Source File: AssignableOperandTypeChecker.java    From Bats with Apache License 2.0 6 votes vote down vote up
public boolean checkOperandTypes(
    SqlCallBinding callBinding,
    boolean throwOnFailure) {
  // Do not use callBinding.operands(). We have not resolved to a function
  // yet, therefore we do not know the ordered parameter names.
  final List<SqlNode> operands = callBinding.getCall().getOperandList();
  for (Pair<RelDataType, SqlNode> pair : Pair.zip(paramTypes, operands)) {
    RelDataType argType =
        callBinding.getValidator().deriveType(
            callBinding.getScope(),
            pair.right);
    if (!SqlTypeUtil.canAssignFrom(pair.left, argType)) {
      if (throwOnFailure) {
        throw callBinding.newValidationSignatureError();
      } else {
        return false;
      }
    }
  }
  return true;
}
 
Example 5
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 6
Source File: DrillCalciteSqlBetweenOperatorWrapper.java    From Bats with Apache License 2.0 6 votes vote down vote up
/**
 * Since Calcite has its rule for type compatibility
 * (see {@link org.apache.calcite.sql.type.SqlTypeUtil#isComparable(org.apache.calcite.rel.type.RelDataType,
 * org.apache.calcite.rel.type.RelDataType)}), which is usually different from Drill's, this method is overridden here to avoid
 * Calcite early terminating the queries.
 */
@Override
public boolean checkOperandTypes(SqlCallBinding callBinding, boolean throwOnFailure) {
  final List<TypeProtos.MinorType> types = new ArrayList<>();
  for (int i = 0; i < callBinding.getOperandCount(); i++) {
    final TypeProtos.MinorType inMinorType = TypeInferenceUtils.getDrillTypeFromCalciteType(callBinding.getOperandType(i));
    if (inMinorType == TypeProtos.MinorType.LATE) {
      return true;
    }
    types.add(inMinorType);
  }

  final boolean isCompatible = TypeCastRules.getLeastRestrictiveType(types) != null;
  if (!isCompatible && throwOnFailure) {
    throw callBinding.newValidationSignatureError();
  }
  return isCompatible;
}
 
Example 7
Source File: CompositeOperandTypeChecker.java    From calcite with Apache License 2.0 6 votes vote down vote up
public boolean checkOperandTypes(
    SqlCallBinding callBinding,
    boolean throwOnFailure) {
  // 1. Check eagerly for binary arithmetic expressions.
  // 2. Check the comparability.
  // 3. Check if the operands have the right type.
  if (callBinding.isTypeCoercionEnabled()) {
    final TypeCoercion typeCoercion = callBinding.getValidator().getTypeCoercion();
    typeCoercion.binaryArithmeticCoercion(callBinding);
  }
  if (check(callBinding)) {
    return true;
  }
  if (!throwOnFailure) {
    return false;
  }
  if (composition == Composition.OR) {
    for (SqlOperandTypeChecker allowedRule : allowedRules) {
      allowedRule.checkOperandTypes(callBinding, true);
    }
  }

  // If no exception thrown, just throw a generic validation
  // signature error.
  throw callBinding.newValidationSignatureError();
}
 
Example 8
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 9
Source File: SqlDatePartOperator.java    From dremio-oss with Apache License 2.0 5 votes vote down vote up
@Override
public boolean checkSingleOperandType(SqlCallBinding callBinding, SqlNode node,
    int iFormalOperand, boolean throwOnFailure) {

  // check that the input is a literal.
  if(!super.checkSingleOperandType(callBinding, node, iFormalOperand, throwOnFailure)) {
    return false;
  }

  final RelDataType type = callBinding.getValidator().deriveType(callBinding.getScope(), node);
  final SqlTypeName typeName = type.getSqlTypeName();

  // Pass type checking for operators if it's of type 'ANY'.
  if (typeName.getFamily() == SqlTypeFamily.ANY) {
    return true;
  }

  if(!(typeName == SqlTypeName.CHAR || typeName == SqlTypeName.VARCHAR)) {
    if(throwOnFailure) {
      throw callBinding.newValidationSignatureError();
    }
    return false;
  }

  final SqlLiteral literal = (SqlLiteral) node;
  final String value = ((NlsString)literal.getValue()).getValue();
  if(validStrings.contains(value.toLowerCase())) {
    return true;
  }

  if(throwOnFailure) {
    throw callBinding.newValidationSignatureError();
    //throw new SqlValidatorException(String.format("DATE_PART function only accepts the following values for a date type: %s.", Joiner.on(", ").join(validStrings)), null);
  }

  return false;
}
 
Example 10
Source File: FamilyOperandTypeChecker.java    From calcite with Apache License 2.0 5 votes vote down vote up
public boolean checkSingleOperandType(
    SqlCallBinding callBinding,
    SqlNode node,
    int iFormalOperand,
    boolean throwOnFailure) {
  SqlTypeFamily family = families.get(iFormalOperand);
  if (family == SqlTypeFamily.ANY) {
    // no need to check
    return true;
  }
  if (SqlUtil.isNullLiteral(node, false)) {
    if (callBinding.isTypeCoercionEnabled()) {
      return true;
    } else if (throwOnFailure) {
      throw callBinding.getValidator().newValidationError(node,
          RESOURCE.nullIllegal());
    } else {
      return false;
    }
  }
  RelDataType type =
      callBinding.getValidator().deriveType(
          callBinding.getScope(),
          node);
  SqlTypeName typeName = type.getSqlTypeName();

  // Pass type checking for operators if it's of type 'ANY'.
  if (typeName.getFamily() == SqlTypeFamily.ANY) {
    return true;
  }

  if (!family.getTypeNames().contains(typeName)) {
    if (throwOnFailure) {
      throw callBinding.newValidationSignatureError();
    }
    return false;
  }
  return true;
}
 
Example 11
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;
}
 
Example 12
Source File: SqlLiteralChainOperator.java    From calcite with Apache License 2.0 5 votes vote down vote up
public boolean checkOperandTypes(
    SqlCallBinding callBinding,
    boolean throwOnFailure) {
  if (!argTypesValid(callBinding)) {
    if (throwOnFailure) {
      throw callBinding.newValidationSignatureError();
    }
    return false;
  }
  return true;
}
 
Example 13
Source File: NumericExceptFirstOperandChecker.java    From flink with Apache License 2.0 5 votes vote down vote up
@Override
public boolean checkOperandTypes(SqlCallBinding callBinding, boolean throwOnFailure) {
	for (int i = 1; i < callBinding.getOperandCount(); i++) {
		if (!SqlTypeUtil.isNumeric(callBinding.getOperandType(i))) {
			if (!throwOnFailure) {
				return false;
			}
			throw callBinding.newValidationSignatureError();
		}
	}
	return true;
}
 
Example 14
Source File: DremioArgChecker.java    From dremio-oss with Apache License 2.0 5 votes vote down vote up
private boolean checkOp(Checker checker, SqlCallBinding callBinding, SqlNode node, int iFormalOperand,
    boolean throwOnFailure) {

  if (SqlUtil.isNullLiteral(node, false)) {
    if (throwOnFailure) {
      throw callBinding.getValidator().newValidationError(node,
          RESOURCE.nullIllegal());
    } else {
      return false;
    }
  }
  RelDataType type = callBinding.getValidator().deriveType(callBinding.getScope(), node);
  SqlTypeName typeName = type.getSqlTypeName();

  // Pass type checking for operators if it's of type 'ANY'.
  if (typeName.getFamily() == SqlTypeFamily.ANY && allowAny) {
    return true;
  }

  if (!checker.check(type)) {
    if (throwOnFailure) {
      throw callBinding.newValidationSignatureError();
    }
    return false;
  }
  return true;
}
 
Example 15
Source File: SqlLiteralChainOperator.java    From Bats with Apache License 2.0 5 votes vote down vote up
public boolean checkOperandTypes(
    SqlCallBinding callBinding,
    boolean throwOnFailure) {
  if (!argTypesValid(callBinding)) {
    if (throwOnFailure) {
      throw callBinding.newValidationSignatureError();
    }
    return false;
  }
  return true;
}
 
Example 16
Source File: NumericExceptFirstOperandChecker.java    From flink with Apache License 2.0 5 votes vote down vote up
@Override
public boolean checkOperandTypes(SqlCallBinding callBinding, boolean throwOnFailure) {
	for (int i = 1; i < callBinding.getOperandCount(); i++) {
		if (!SqlTypeUtil.isNumeric(callBinding.getOperandType(i))) {
			if (!throwOnFailure) {
				return false;
			}
			throw callBinding.newValidationSignatureError();
		}
	}
	return true;
}
 
Example 17
Source File: RepeatFamilyOperandTypeChecker.java    From flink with Apache License 2.0 5 votes vote down vote up
public boolean checkSingleOperandType(
	SqlCallBinding callBinding,
	SqlNode node,
	boolean throwOnFailure) {

	if (SqlUtil.isNullLiteral(node, false)) {
		if (throwOnFailure) {
			throw callBinding.getValidator().newValidationError(node,
				RESOURCE.nullIllegal());
		} else {
			return false;
		}
	}

	RelDataType type = callBinding.getValidator().deriveType(
		callBinding.getScope(),
		node);
	SqlTypeName typeName = type.getSqlTypeName();

	// Pass type checking for operators if it's of type 'ANY'.
	if (typeName.getFamily() == SqlTypeFamily.ANY) {
		return true;
	}

	if (!family.getTypeNames().contains(typeName)) {
		if (throwOnFailure) {
			throw callBinding.newValidationSignatureError();
		}
		return false;
	}
	return true;
}
 
Example 18
Source File: ComparableOperandTypeChecker.java    From Bats with Apache License 2.0 5 votes vote down vote up
private boolean checkType(
    SqlCallBinding callBinding,
    boolean throwOnFailure,
    RelDataType type) {
  if (type.getComparability().ordinal()
      < requiredComparability.ordinal()) {
    if (throwOnFailure) {
      throw callBinding.newValidationSignatureError();
    } else {
      return false;
    }
  } else {
    return true;
  }
}
 
Example 19
Source File: ComparableOperandTypeChecker.java    From calcite with Apache License 2.0 5 votes vote down vote up
private boolean checkType(
    SqlCallBinding callBinding,
    boolean throwOnFailure,
    RelDataType type) {
  if (type.getComparability().ordinal()
      < requiredComparability.ordinal()) {
    if (throwOnFailure) {
      throw callBinding.newValidationSignatureError();
    } else {
      return false;
    }
  } else {
    return true;
  }
}
 
Example 20
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;
}