use of org.apache.phoenix.schema.ArgumentTypeMismatchException in project phoenix by apache.
the class FunctionParseNode method validateFunctionArguement.
public static void validateFunctionArguement(BuiltInFunctionInfo info, int childIndex, Expression child) throws ArgumentTypeMismatchException, ValueRangeExcpetion {
BuiltInFunctionArgInfo arg = info.getArgs()[childIndex];
if (arg.getAllowedTypes().length > 0) {
boolean isCoercible = false;
for (Class<? extends PDataType> type : arg.getAllowedTypes()) {
if (child.getDataType().isCoercibleTo(PDataTypeFactory.getInstance().instanceFromClass(type))) {
isCoercible = true;
break;
}
}
if (!isCoercible) {
throw new ArgumentTypeMismatchException(arg.getAllowedTypes(), child.getDataType(), info.getName() + " argument " + (childIndex + 1));
}
if (child instanceof LiteralExpression) {
LiteralExpression valueExp = (LiteralExpression) child;
LiteralExpression minValue = arg.getMinValue();
LiteralExpression maxValue = arg.getMaxValue();
if (minValue != null && minValue.getDataType().compareTo(minValue.getValue(), valueExp.getValue(), valueExp.getDataType()) > 0) {
throw new ValueRangeExcpetion(minValue, maxValue == null ? "" : maxValue, valueExp.getValue(), info.getName() + " argument " + (childIndex + 1));
}
if (maxValue != null && maxValue.getDataType().compareTo(maxValue.getValue(), valueExp.getValue(), valueExp.getDataType()) < 0) {
throw new ValueRangeExcpetion(minValue == null ? "" : minValue, maxValue, valueExp.getValue(), info.getName() + " argument " + (childIndex + 1));
}
}
}
if (arg.isConstant() && !(child instanceof LiteralExpression)) {
throw new ArgumentTypeMismatchException("constant", child.toString(), info.getName() + " argument " + (childIndex + 1));
}
if (!arg.getAllowedValues().isEmpty()) {
Object value = ((LiteralExpression) child).getValue();
if (!arg.getAllowedValues().contains(value.toString().toUpperCase())) {
throw new ArgumentTypeMismatchException(Arrays.toString(arg.getAllowedValues().toArray(new String[0])), value.toString(), info.getName() + " argument " + (childIndex + 1));
}
}
}
use of org.apache.phoenix.schema.ArgumentTypeMismatchException in project phoenix by apache.
the class ProjectionCompiler method coerceIfNecessary.
private static Expression coerceIfNecessary(int index, List<? extends PDatum> targetColumns, Expression expression) throws SQLException {
if (index < targetColumns.size()) {
PDatum targetColumn = targetColumns.get(index);
if (targetColumn.getDataType() != expression.getDataType()) {
PDataType targetType = targetColumn.getDataType();
// (an actual value we can specifically check against).
if (expression.getDataType() != null && !expression.getDataType().isCastableTo(targetType)) {
throw new ArgumentTypeMismatchException(targetType, expression.getDataType(), "column: " + targetColumn);
}
expression = CoerceExpression.create(expression, targetType, targetColumn.getSortOrder(), targetColumn.getMaxLength());
}
}
return expression;
}
Aggregations