Search in sources :

Example 11 with Signature

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

the class BaseImplementationSymbolVisitor method visitFunction.

@Override
public Input<?> visitFunction(Function function, C context) {
    Signature signature = function.signature();
    FunctionImplementation functionImplementation = nodeCtx.functions().getQualified(function, txnCtx.sessionSettings().searchPath());
    assert functionImplementation != null : "Function implementation not found using full qualified lookup";
    if (functionImplementation instanceof Scalar<?, ?>) {
        List<Symbol> arguments = function.arguments();
        Scalar<?, ?> scalarImpl = ((Scalar) functionImplementation).compile(arguments);
        Input[] argumentInputs = new Input[arguments.size()];
        int i = 0;
        for (Symbol argument : function.arguments()) {
            argumentInputs[i++] = argument.accept(this, context);
        }
        return new FunctionExpression<>(txnCtx, nodeCtx, scalarImpl, argumentInputs);
    } else {
        throw new UnsupportedFeatureException(String.format(Locale.ENGLISH, "Function %s(%s) is not a scalar function.", signature.getName(), Lists2.joinOn(", ", function.arguments(), x -> x.valueType().getName())));
    }
}
Also used : Input(io.crate.data.Input) UnsupportedFeatureException(io.crate.exceptions.UnsupportedFeatureException) AliasSymbol(io.crate.expression.symbol.AliasSymbol) Symbol(io.crate.expression.symbol.Symbol) Signature(io.crate.metadata.functions.Signature) FunctionImplementation(io.crate.metadata.FunctionImplementation) Scalar(io.crate.metadata.Scalar)

Example 12 with Signature

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

the class DocSchemaInfoTest method setup.

@Before
public void setup() throws Exception {
    nodeCtx = createNodeContext();
    udfService = new UserDefinedFunctionService(clusterService, nodeCtx);
    udfService.registerLanguage(new UDFLanguage() {

        @Override
        public Scalar createFunctionImplementation(UserDefinedFunctionMetadata metadata, Signature signature) throws ScriptException {
            String error = validate(metadata);
            if (error != null) {
                throw new ScriptException("this is not Burlesque");
            }
            return new Scalar<>() {

                @Override
                public Object evaluate(TransactionContext txnCtx, NodeContext nodeCtx, Input[] args) {
                    return null;
                }

                @Override
                public Signature signature() {
                    return signature;
                }

                @Override
                public Signature boundSignature() {
                    return signature;
                }
            };
        }

        @Override
        @Nullable
        public String validate(UserDefinedFunctionMetadata metadata) {
            if (!metadata.definition().equals("\"Hello, World!\"Q")) {
                return "this is not Burlesque";
            }
            return null;
        }

        @Override
        public String name() {
            return "burlesque";
        }
    });
    docSchemaInfo = new DocSchemaInfo("doc", clusterService, nodeCtx, udfService, (ident, state) -> null, new TestingDocTableInfoFactory(Map.of()));
}
Also used : UserDefinedFunctionService(io.crate.expression.udf.UserDefinedFunctionService) Input(io.crate.data.Input) UDFLanguage(io.crate.expression.udf.UDFLanguage) TransactionContext(io.crate.metadata.TransactionContext) NodeContext(io.crate.metadata.NodeContext) UserDefinedFunctionService(io.crate.expression.udf.UserDefinedFunctionService) Matchers(org.hamcrest.Matchers) Test(org.junit.Test) Signature(io.crate.metadata.functions.Signature) CrateDummyClusterServiceUnitTest(io.crate.test.integration.CrateDummyClusterServiceUnitTest) Index(org.elasticsearch.index.Index) UserDefinedFunctionsMetadata(io.crate.expression.udf.UserDefinedFunctionsMetadata) UserDefinedFunctionMetadata(io.crate.expression.udf.UserDefinedFunctionMetadata) Metadata(org.elasticsearch.cluster.metadata.Metadata) List(java.util.List) DataTypes(io.crate.types.DataTypes) Map(java.util.Map) Scalar(io.crate.metadata.Scalar) TestingHelpers.createNodeContext(io.crate.testing.TestingHelpers.createNodeContext) Nullable(javax.annotation.Nullable) SearchPath.pathWithPGCatalogAndDoc(io.crate.metadata.SearchPath.pathWithPGCatalogAndDoc) Before(org.junit.Before) ScriptException(javax.script.ScriptException) NodeContext(io.crate.metadata.NodeContext) TestingHelpers.createNodeContext(io.crate.testing.TestingHelpers.createNodeContext) UserDefinedFunctionMetadata(io.crate.expression.udf.UserDefinedFunctionMetadata) Scalar(io.crate.metadata.Scalar) ScriptException(javax.script.ScriptException) Input(io.crate.data.Input) TransactionContext(io.crate.metadata.TransactionContext) Signature(io.crate.metadata.functions.Signature) UDFLanguage(io.crate.expression.udf.UDFLanguage) Nullable(javax.annotation.Nullable) Before(org.junit.Before)

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