Search in sources :

Example 1 with BuiltInFunctionInfo

use of org.apache.phoenix.parse.FunctionParseNode.BuiltInFunctionInfo in project phoenix by apache.

the class BuiltInFunctionInfoTest method testConstruct_WithOneDefaultArg.

@Test
public void testConstruct_WithOneDefaultArg() {
    BuiltInFunctionInfo funcInfo = getBuiltInFunctionInfo(WithOneDefaultArg.class);
    assertEquals(3, funcInfo.getArgs().length);
    assertEquals(2, funcInfo.getRequiredArgCount());
    assertEquals("WITH_ONE_DEFAULT_ARG", funcInfo.getName());
}
Also used : BuiltInFunctionInfo(org.apache.phoenix.parse.FunctionParseNode.BuiltInFunctionInfo) Test(org.junit.Test)

Example 2 with BuiltInFunctionInfo

use of org.apache.phoenix.parse.FunctionParseNode.BuiltInFunctionInfo in project phoenix by apache.

the class BuiltInFunctionInfoTest method testConstruct_WithMultipleDefaultArgs.

@Test
public void testConstruct_WithMultipleDefaultArgs() {
    BuiltInFunctionInfo funcInfo = getBuiltInFunctionInfo(WithMultipleDefaultArgs.class);
    assertEquals(3, funcInfo.getArgs().length);
    assertEquals(1, funcInfo.getRequiredArgCount());
    assertEquals("WITH_MULTIPLE_DEFAULT_ARGS", funcInfo.getName());
}
Also used : BuiltInFunctionInfo(org.apache.phoenix.parse.FunctionParseNode.BuiltInFunctionInfo) Test(org.junit.Test)

Example 3 with BuiltInFunctionInfo

use of org.apache.phoenix.parse.FunctionParseNode.BuiltInFunctionInfo in project phoenix by apache.

the class BuiltInFunctionInfoTest method testConstruct_NoDefaultArgs.

@Test
public void testConstruct_NoDefaultArgs() {
    BuiltInFunctionInfo funcInfo = getBuiltInFunctionInfo(NoDefaultArgsFunction.class);
    assertEquals(2, funcInfo.getArgs().length);
    assertEquals(2, funcInfo.getRequiredArgCount());
    assertEquals("NO_DEFAULT_ARGS", funcInfo.getName());
}
Also used : BuiltInFunctionInfo(org.apache.phoenix.parse.FunctionParseNode.BuiltInFunctionInfo) Test(org.junit.Test)

Example 4 with BuiltInFunctionInfo

use of org.apache.phoenix.parse.FunctionParseNode.BuiltInFunctionInfo in project phoenix by apache.

the class ParseNodeFactory method addBuiltInFunction.

private static void addBuiltInFunction(Class<? extends FunctionExpression> f) throws Exception {
    BuiltInFunction d = f.getAnnotation(BuiltInFunction.class);
    if (d == null) {
        return;
    }
    int nArgs = d.args().length;
    BuiltInFunctionInfo value = new BuiltInFunctionInfo(f, d);
    if (d.classType() != FunctionParseNode.FunctionClassType.ABSTRACT) {
        BUILT_IN_FUNCTION_MULTIMAP.put(value.getName(), value);
    }
    if (d.classType() != FunctionParseNode.FunctionClassType.DERIVED) {
        do {
            // Add function to function map, throwing if conflicts found
            // Add entry for each possible version of function based on arguments that are not required to be present (i.e. arg with default value)
            BuiltInFunctionKey key = new BuiltInFunctionKey(value.getName(), nArgs);
            if (BUILT_IN_FUNCTION_MAP.put(key, value) != null) {
                throw new IllegalStateException("Multiple " + value.getName() + " functions with " + nArgs + " arguments");
            }
        } while (--nArgs >= 0 && d.args()[nArgs].defaultValue().length() > 0);
        // Look for default values that aren't at the end and throw
        while (--nArgs >= 0) {
            if (d.args()[nArgs].defaultValue().length() > 0) {
                throw new IllegalStateException("Function " + value.getName() + " has non trailing default value of '" + d.args()[nArgs].defaultValue() + "'. Only trailing arguments may have default values");
            }
        }
    }
}
Also used : BuiltInFunction(org.apache.phoenix.parse.FunctionParseNode.BuiltInFunction) BuiltInFunctionInfo(org.apache.phoenix.parse.FunctionParseNode.BuiltInFunctionInfo)

Example 5 with BuiltInFunctionInfo

use of org.apache.phoenix.parse.FunctionParseNode.BuiltInFunctionInfo in project phoenix by apache.

the class ParseNodeFactory method function.

public FunctionParseNode function(String name, List<ParseNode> valueNodes, List<ParseNode> columnNodes, boolean isAscending) {
    List<ParseNode> args = Lists.newArrayListWithExpectedSize(columnNodes.size() + valueNodes.size() + 1);
    args.addAll(columnNodes);
    args.add(new LiteralParseNode(Boolean.valueOf(isAscending)));
    args.addAll(valueNodes);
    BuiltInFunctionInfo info = getInfo(name, args);
    if (info == null) {
        return new UDFParseNode(name, args, info);
    }
    Constructor<? extends FunctionParseNode> ctor = info.getNodeCtor();
    if (ctor == null) {
        return new AggregateFunctionWithinGroupParseNode(name, args, info);
    } else {
        try {
            return ctor.newInstance(name, args, info);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }
}
Also used : TypeMismatchException(org.apache.phoenix.schema.TypeMismatchException) SQLException(java.sql.SQLException) BuiltInFunctionInfo(org.apache.phoenix.parse.FunctionParseNode.BuiltInFunctionInfo)

Aggregations

BuiltInFunctionInfo (org.apache.phoenix.parse.FunctionParseNode.BuiltInFunctionInfo)8 Test (org.junit.Test)3 SQLException (java.sql.SQLException)2 ImmutableBytesWritable (org.apache.hadoop.hbase.io.ImmutableBytesWritable)1 AndExpression (org.apache.phoenix.expression.AndExpression)1 ArrayConstructorExpression (org.apache.phoenix.expression.ArrayConstructorExpression)1 ByteBasedLikeExpression (org.apache.phoenix.expression.ByteBasedLikeExpression)1 CaseExpression (org.apache.phoenix.expression.CaseExpression)1 CoerceExpression (org.apache.phoenix.expression.CoerceExpression)1 ComparisonExpression (org.apache.phoenix.expression.ComparisonExpression)1 DateAddExpression (org.apache.phoenix.expression.DateAddExpression)1 DateSubtractExpression (org.apache.phoenix.expression.DateSubtractExpression)1 DecimalAddExpression (org.apache.phoenix.expression.DecimalAddExpression)1 DecimalDivideExpression (org.apache.phoenix.expression.DecimalDivideExpression)1 DecimalMultiplyExpression (org.apache.phoenix.expression.DecimalMultiplyExpression)1 DecimalSubtractExpression (org.apache.phoenix.expression.DecimalSubtractExpression)1 DoubleAddExpression (org.apache.phoenix.expression.DoubleAddExpression)1 DoubleDivideExpression (org.apache.phoenix.expression.DoubleDivideExpression)1 DoubleMultiplyExpression (org.apache.phoenix.expression.DoubleMultiplyExpression)1 DoubleSubtractExpression (org.apache.phoenix.expression.DoubleSubtractExpression)1