Search in sources :

Example 1 with ValueRangeExcpetion

use of org.apache.phoenix.schema.ValueRangeExcpetion 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 ValueRangeExcpetion

use of org.apache.phoenix.schema.ValueRangeExcpetion in project phoenix by apache.

the class UserDefinedFunctionsIT method testVerifyCreateFunctionArguments.

@Test
public void testVerifyCreateFunctionArguments() throws Exception {
    Connection conn = driver.connect(url, EMPTY_PROPS);
    Statement stmt = conn.createStatement();
    conn.createStatement().execute("create table t4(k integer primary key, k1 integer, lastname varchar)");
    stmt.execute("upsert into t4 values(1,1,'jock')");
    conn.commit();
    stmt.execute("create function mysum(INTEGER, INTEGER CONSTANT defaultValue=10 minvalue=1 maxvalue=15 ) returns INTEGER as 'org.apache.phoenix.end2end." + MY_SUM_CLASS_NAME + "' using jar " + "'" + util.getConfiguration().get(DYNAMIC_JARS_DIR_KEY) + "/myjar2.jar" + "'");
    ResultSet rs = stmt.executeQuery("select mysum(k,12) from t4");
    assertTrue(rs.next());
    assertEquals(13, rs.getInt(1));
    rs = stmt.executeQuery("select mysum(k) from t4");
    assertTrue(rs.next());
    assertEquals(11, rs.getInt(1));
    try {
        stmt.executeQuery("select mysum(k,20) from t4");
        fail("Value Range Exception should be thrown.");
    } catch (ValueRangeExcpetion e) {
    }
}
Also used : PreparedStatement(java.sql.PreparedStatement) Statement(java.sql.Statement) Connection(java.sql.Connection) ResultSet(java.sql.ResultSet) ValueRangeExcpetion(org.apache.phoenix.schema.ValueRangeExcpetion) Test(org.junit.Test)

Aggregations

ValueRangeExcpetion (org.apache.phoenix.schema.ValueRangeExcpetion)2 Connection (java.sql.Connection)1 PreparedStatement (java.sql.PreparedStatement)1 ResultSet (java.sql.ResultSet)1 Statement (java.sql.Statement)1 LiteralExpression (org.apache.phoenix.expression.LiteralExpression)1 ArgumentTypeMismatchException (org.apache.phoenix.schema.ArgumentTypeMismatchException)1 Test (org.junit.Test)1