Search in sources :

Example 1 with ArgumentTypeMismatchException

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));
        }
    }
}
Also used : LiteralExpression(org.apache.phoenix.expression.LiteralExpression) ValueRangeExcpetion(org.apache.phoenix.schema.ValueRangeExcpetion) ArgumentTypeMismatchException(org.apache.phoenix.schema.ArgumentTypeMismatchException)

Example 2 with ArgumentTypeMismatchException

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;
}
Also used : PDatum(org.apache.phoenix.schema.PDatum) PDataType(org.apache.phoenix.schema.types.PDataType) ArgumentTypeMismatchException(org.apache.phoenix.schema.ArgumentTypeMismatchException)

Aggregations

ArgumentTypeMismatchException (org.apache.phoenix.schema.ArgumentTypeMismatchException)2 LiteralExpression (org.apache.phoenix.expression.LiteralExpression)1 PDatum (org.apache.phoenix.schema.PDatum)1 ValueRangeExcpetion (org.apache.phoenix.schema.ValueRangeExcpetion)1 PDataType (org.apache.phoenix.schema.types.PDataType)1