Search in sources :

Example 6 with Signature

use of io.crate.metadata.functions.Signature in project crate by crate.

the class PgGetFunctionResultFunctionTest method test_system_function_result_type_text_representation.

@Test
public void test_system_function_result_type_text_representation() {
    for (List<FunctionProvider> providers : sqlExpressions.nodeCtx.functions().functionResolvers().values()) {
        for (FunctionProvider sysFunc : providers) {
            Signature signature = sysFunc.getSignature();
            Integer funcOid = OidHash.functionOid(signature);
            assertEvaluate("pg_get_function_result(" + funcOid + ")", signature.getReturnType().toString());
        }
    }
}
Also used : FunctionProvider(io.crate.metadata.FunctionProvider) Signature(io.crate.metadata.functions.Signature) Test(org.junit.Test)

Example 7 with Signature

use of io.crate.metadata.functions.Signature in project crate by crate.

the class ExpressionAnalyzer method allocateBuiltinOrUdfFunction.

/**
 * Creates a function symbol and tries to normalize the new function's
 * {@link FunctionImplementation} iff only Literals are supplied as arguments.
 * This folds any constant expressions like '1 + 1' => '2'.
 * @param schema The schema for udf functions
 * @param functionName The function name of the new function.
 * @param arguments The arguments to provide to the {@link Function}.
 * @param filter The filter clause to filter {@link Function}'s input values.
 * @param context Context holding the state for the current translation.
 * @param windowDefinition The definition of the window the allocated function will be executed against.
 * @param coordinatorTxnCtx {@link CoordinatorTxnCtx} for this transaction.
 * @param nodeCtx The {@link NodeContext} to normalize constant expressions.
 * @return The supplied {@link Function} or a {@link Literal} in case of constant folding.
 */
private static Symbol allocateBuiltinOrUdfFunction(@Nullable String schema, String functionName, List<Symbol> arguments, @Nullable Symbol filter, @Nullable Boolean ignoreNulls, ExpressionAnalysisContext context, @Nullable WindowDefinition windowDefinition, CoordinatorTxnCtx coordinatorTxnCtx, NodeContext nodeCtx) {
    FunctionImplementation funcImpl = nodeCtx.functions().get(schema, functionName, arguments, coordinatorTxnCtx.sessionContext().searchPath());
    Signature signature = funcImpl.signature();
    Signature boundSignature = funcImpl.boundSignature();
    List<Symbol> castArguments = cast(arguments, boundSignature.getArgumentDataTypes());
    Function newFunction;
    if (windowDefinition == null) {
        if (signature.getKind() == FunctionType.AGGREGATE) {
            context.indicateAggregates();
        } else if (filter != null) {
            throw new UnsupportedOperationException("Only aggregate functions allow a FILTER clause");
        }
        if (ignoreNulls != null) {
            throw new IllegalArgumentException(String.format(Locale.ENGLISH, "%s cannot accept RESPECT or IGNORE NULLS flag.", functionName));
        }
        newFunction = new Function(signature, castArguments, boundSignature.getReturnType().createType(), filter);
    } else {
        if (signature.getKind() != FunctionType.WINDOW) {
            if (signature.getKind() != FunctionType.AGGREGATE) {
                throw new IllegalArgumentException(String.format(Locale.ENGLISH, "OVER clause was specified, but %s is neither a window nor an aggregate function.", functionName));
            } else {
                if (ignoreNulls != null) {
                    throw new IllegalArgumentException(String.format(Locale.ENGLISH, "%s cannot accept RESPECT or IGNORE NULLS flag.", functionName));
                }
            }
        }
        newFunction = new WindowFunction(signature, castArguments, boundSignature.getReturnType().createType(), filter, windowDefinition, ignoreNulls);
    }
    return newFunction;
}
Also used : CurrentDateFunction(io.crate.expression.scalar.CurrentDateFunction) MapFunction(io.crate.expression.scalar.arithmetic.MapFunction) CurrentTimestampFunction(io.crate.expression.scalar.timestamp.CurrentTimestampFunction) WindowFunction(io.crate.expression.symbol.WindowFunction) ArrayFunction(io.crate.expression.scalar.arithmetic.ArrayFunction) CurrentTimeFunction(io.crate.expression.scalar.timestamp.CurrentTimeFunction) IfFunction(io.crate.expression.scalar.conditional.IfFunction) ArraySliceFunction(io.crate.expression.scalar.ArraySliceFunction) Function(io.crate.expression.symbol.Function) SubscriptFunction(io.crate.expression.scalar.SubscriptFunction) WindowFunction(io.crate.expression.symbol.WindowFunction) SelectSymbol(io.crate.expression.symbol.SelectSymbol) ScopedSymbol(io.crate.expression.symbol.ScopedSymbol) Symbol(io.crate.expression.symbol.Symbol) Signature(io.crate.metadata.functions.Signature) FunctionImplementation(io.crate.metadata.FunctionImplementation)

Example 8 with Signature

use of io.crate.metadata.functions.Signature in project crate by crate.

the class UserDefinedFunctionsIntegrationTest method test_pg_function_is_visible_when_oid_is_retrieved_from_column.

@Test
public void test_pg_function_is_visible_when_oid_is_retrieved_from_column() throws Exception {
    Signature signature = Signature.builder().kind(FunctionType.SCALAR).name(new FunctionName(null, CurrentTimeFunction.NAME)).argumentTypes().returnType(DataTypes.TIMETZ.getTypeSignature()).build();
    int functionOid = OidHash.functionOid(signature);
    execute("create table oid_test(oid integer)");
    execute("insert into oid_test values(" + functionOid + ")");
    execute("refresh table oid_test");
    execute("select pg_function_is_visible(t.oid) from oid_test t");
    assertThat(response.rows()[0][0], is(true));
    execute("drop table oid_test");
}
Also used : FunctionName(io.crate.metadata.FunctionName) Signature(io.crate.metadata.functions.Signature) TypeSignature(io.crate.types.TypeSignature) Test(org.junit.Test)

Example 9 with Signature

use of io.crate.metadata.functions.Signature in project crate by crate.

the class UserDefinedFunctionsIntegrationTest method test_pg_get_function_result.

@Test
public void test_pg_get_function_result() throws Exception {
    TypeSignature returnTypeSig = TypeSignature.parseTypeSignature("array(array(integer))");
    String returnType = returnTypeSig.toString();
    Signature signature = Signature.builder().kind(FunctionType.SCALAR).name(new FunctionName(Schemas.DOC_SCHEMA_NAME, "make_2d_array")).argumentTypes(DataTypes.INTEGER.getTypeSignature()).returnType(returnTypeSig).build();
    int functionOid = OidHash.functionOid(signature);
    execute("select pg_get_function_result(?)", new Object[] { functionOid });
    assertThat(response.rows()[0][0], nullValue());
    execute("create function doc.make_2d_array(integer) returns array(array(integer)) language dummy_lang as ?", new Object[] { returnType });
    execute("select pg_get_function_result(" + functionOid + ")");
    assertThat(response.rows()[0][0], is(returnType));
    execute("drop function doc.make_2d_array(integer)");
    execute("select pg_get_function_result(" + functionOid + ")");
    assertThat(response.rows()[0][0], nullValue());
}
Also used : FunctionName(io.crate.metadata.FunctionName) TypeSignature(io.crate.types.TypeSignature) Signature(io.crate.metadata.functions.Signature) TypeSignature(io.crate.types.TypeSignature) Matchers.containsString(org.hamcrest.Matchers.containsString) Test(org.junit.Test)

Example 10 with Signature

use of io.crate.metadata.functions.Signature in project crate by crate.

the class UserDefinedFunctionsIntegrationTest method test_pg_function_is_visible.

@Test
public void test_pg_function_is_visible() throws Exception {
    Signature signature = Signature.builder().kind(FunctionType.SCALAR).name(new FunctionName(Schemas.DOC_SCHEMA_NAME, "my_func")).argumentTypes(TypeSignature.parseTypeSignature("array(array(integer))"), TypeSignature.parseTypeSignature("integer"), TypeSignature.parseTypeSignature("text")).returnType(TypeSignature.parseTypeSignature("text")).build();
    int functionOid = OidHash.functionOid(signature);
    execute("select pg_function_is_visible(" + functionOid + ")");
    assertThat(response.rows()[0][0], is(false));
    execute("create function doc.my_func(array(array(integer)), integer, text) returns text language dummy_lang as '42'");
    execute("select pg_function_is_visible(" + functionOid + ")");
    assertThat(response.rows()[0][0], is(true));
    execute("drop function doc.my_func(array(array(integer)), integer, text)");
    execute("select pg_function_is_visible(" + functionOid + ")");
    assertThat(response.rows()[0][0], is(false));
}
Also used : FunctionName(io.crate.metadata.FunctionName) Signature(io.crate.metadata.functions.Signature) TypeSignature(io.crate.types.TypeSignature) Test(org.junit.Test)

Aggregations

Signature (io.crate.metadata.functions.Signature)12 Test (org.junit.Test)8 FunctionName (io.crate.metadata.FunctionName)4 FunctionImplementation (io.crate.metadata.FunctionImplementation)3 FunctionProvider (io.crate.metadata.FunctionProvider)3 TypeSignature (io.crate.types.TypeSignature)3 Input (io.crate.data.Input)2 Symbol (io.crate.expression.symbol.Symbol)2 Scalar (io.crate.metadata.Scalar)2 CrateDummyClusterServiceUnitTest (io.crate.test.integration.CrateDummyClusterServiceUnitTest)2 List (java.util.List)2 Map (java.util.Map)2 UnsupportedFeatureException (io.crate.exceptions.UnsupportedFeatureException)1 ArraySliceFunction (io.crate.expression.scalar.ArraySliceFunction)1 CurrentDateFunction (io.crate.expression.scalar.CurrentDateFunction)1 SubscriptFunction (io.crate.expression.scalar.SubscriptFunction)1 ArrayFunction (io.crate.expression.scalar.arithmetic.ArrayFunction)1 MapFunction (io.crate.expression.scalar.arithmetic.MapFunction)1 IfFunction (io.crate.expression.scalar.conditional.IfFunction)1 CurrentTimeFunction (io.crate.expression.scalar.timestamp.CurrentTimeFunction)1