Search in sources :

Example 1 with UserDefinedFunctionMetadata

use of io.crate.expression.udf.UserDefinedFunctionMetadata in project crate by crate.

the class SchemasTest method testSchemasFromUDF.

@Test
public void testSchemasFromUDF() {
    Metadata metadata = Metadata.builder().putCustom(UserDefinedFunctionsMetadata.TYPE, UserDefinedFunctionsMetadata.of(new UserDefinedFunctionMetadata("new_schema", "my_function", List.of(), DataTypes.STRING, "burlesque", "Hello, World!Q"))).build();
    assertThat(Schemas.getNewCurrentSchemas(metadata), containsInAnyOrder("doc", "new_schema"));
}
Also used : UserDefinedFunctionMetadata(io.crate.expression.udf.UserDefinedFunctionMetadata) IndexMetadata(org.elasticsearch.cluster.metadata.IndexMetadata) UserDefinedFunctionsMetadata(io.crate.expression.udf.UserDefinedFunctionsMetadata) Metadata(org.elasticsearch.cluster.metadata.Metadata) ViewsMetadata(io.crate.metadata.view.ViewsMetadata) UserDefinedFunctionMetadata(io.crate.expression.udf.UserDefinedFunctionMetadata) CrateDummyClusterServiceUnitTest(io.crate.test.integration.CrateDummyClusterServiceUnitTest) Test(org.junit.Test) ViewsMetadataTest(io.crate.metadata.view.ViewsMetadataTest)

Example 2 with UserDefinedFunctionMetadata

use of io.crate.expression.udf.UserDefinedFunctionMetadata in project crate by crate.

the class JavascriptUserDefinedFunctionTest method testValidateCatchesAssertionError.

@Test
public void testValidateCatchesAssertionError() {
    var udfMeta = new UserDefinedFunctionMetadata(Schemas.DOC_SCHEMA_NAME, "f", Collections.singletonList(FunctionArgumentDefinition.of(DataTypes.DOUBLE)), DataTypes.DOUBLE_ARRAY, JS, "var f = (a) => a * a;");
    String validation = udfService.getLanguage(JS).validate(udfMeta);
    String javaVersion = System.getProperty("java.specification.version");
    try {
        if (Integer.parseInt(javaVersion) >= 9) {
            assertThat(validation, is(nullValue()));
        }
    } catch (NumberFormatException e) {
        assertThat(validation, startsWith("Invalid JavaScript in function 'doc.f(double)'"));
        assertThat(validation, endsWith("Failed generating bytecode for <eval>:1"));
    }
}
Also used : UserDefinedFunctionMetadata(io.crate.expression.udf.UserDefinedFunctionMetadata) Matchers.containsString(org.hamcrest.Matchers.containsString) Test(org.junit.Test)

Example 3 with UserDefinedFunctionMetadata

use of io.crate.expression.udf.UserDefinedFunctionMetadata in project crate by crate.

the class JavascriptUserDefinedFunctionTest method registerUserDefinedFunction.

private void registerUserDefinedFunction(String name, DataType<?> returnType, List<DataType<?>> types, String definition) throws ScriptException {
    UserDefinedFunctionMetadata udf = new UserDefinedFunctionMetadata(Schemas.DOC_SCHEMA_NAME, name, types.stream().map(FunctionArgumentDefinition::of).collect(Collectors.toList()), returnType, JS, definition);
    String validation = udfService.getLanguage(JS).validate(udf);
    if (validation == null) {
        var functionName = new FunctionName(Schemas.DOC_SCHEMA_NAME, udf.name());
        var resolvers = functionImplementations.computeIfAbsent(functionName, k -> new ArrayList<>());
        resolvers.add(udfService.buildFunctionResolver(udf));
        sqlExpressions.nodeCtx.functions().registerUdfFunctionImplementationsForSchema(Schemas.DOC_SCHEMA_NAME, functionImplementations);
    } else {
        throw new ScriptException(validation);
    }
}
Also used : FunctionArgumentDefinition(io.crate.analyze.FunctionArgumentDefinition) FunctionName(io.crate.metadata.FunctionName) ScriptException(javax.script.ScriptException) UserDefinedFunctionMetadata(io.crate.expression.udf.UserDefinedFunctionMetadata) Matchers.containsString(org.hamcrest.Matchers.containsString)

Example 4 with UserDefinedFunctionMetadata

use of io.crate.expression.udf.UserDefinedFunctionMetadata in project crate by crate.

the class CreateFunctionPlan method executeOrFail.

@Override
public void executeOrFail(DependencyCarrier dependencies, PlannerContext plannerContext, RowConsumer consumer, Row params, SubQueryResults subQueryResults) throws Exception {
    Function<? super Symbol, Object> eval = x -> SymbolEvaluator.evaluate(plannerContext.transactionContext(), plannerContext.nodeContext(), x, params, subQueryResults);
    UserDefinedFunctionMetadata metadata = new UserDefinedFunctionMetadata(createFunction.schema(), createFunction.name(), createFunction.arguments(), createFunction.returnType(), StringType.INSTANCE.sanitizeValue(eval.apply(createFunction.language())), StringType.INSTANCE.sanitizeValue(eval.apply(createFunction.definition())));
    CreateUserDefinedFunctionRequest request = new CreateUserDefinedFunctionRequest(metadata, createFunction.replace());
    OneRowActionListener<AcknowledgedResponse> listener = new OneRowActionListener<>(consumer, r -> new Row1(1L));
    dependencies.createFunctionAction().execute(request, listener);
}
Also used : CreateUserDefinedFunctionRequest(io.crate.expression.udf.CreateUserDefinedFunctionRequest) StringType(io.crate.types.StringType) AnalyzedCreateFunction(io.crate.analyze.AnalyzedCreateFunction) AcknowledgedResponse(org.elasticsearch.action.support.master.AcknowledgedResponse) Function(java.util.function.Function) RowConsumer(io.crate.data.RowConsumer) DependencyCarrier(io.crate.planner.DependencyCarrier) UserDefinedFunctionMetadata(io.crate.expression.udf.UserDefinedFunctionMetadata) SymbolEvaluator(io.crate.analyze.SymbolEvaluator) Row(io.crate.data.Row) Symbol(io.crate.expression.symbol.Symbol) PlannerContext(io.crate.planner.PlannerContext) Plan(io.crate.planner.Plan) SubQueryResults(io.crate.planner.operators.SubQueryResults) OneRowActionListener(io.crate.execution.support.OneRowActionListener) Row1(io.crate.data.Row1) Row1(io.crate.data.Row1) OneRowActionListener(io.crate.execution.support.OneRowActionListener) UserDefinedFunctionMetadata(io.crate.expression.udf.UserDefinedFunctionMetadata) AcknowledgedResponse(org.elasticsearch.action.support.master.AcknowledgedResponse) CreateUserDefinedFunctionRequest(io.crate.expression.udf.CreateUserDefinedFunctionRequest)

Example 5 with UserDefinedFunctionMetadata

use of io.crate.expression.udf.UserDefinedFunctionMetadata in project crate by crate.

the class DocSchemaInfoTest method testInvalidFunction.

@Test
public void testInvalidFunction() throws Exception {
    UserDefinedFunctionMetadata invalid = new UserDefinedFunctionMetadata("my_schema", "invalid", List.of(), DataTypes.INTEGER, "burlesque", "this is not valid burlesque code");
    UserDefinedFunctionMetadata valid = new UserDefinedFunctionMetadata("my_schema", "valid", List.of(), DataTypes.INTEGER, "burlesque", "\"Hello, World!\"Q");
    UserDefinedFunctionsMetadata metadata = UserDefinedFunctionsMetadata.of(invalid, valid);
    // if a functionImpl can't be created, it won't be registered
    udfService.updateImplementations("my_schema", metadata.functionsMetadata().stream());
    assertThat(nodeCtx.functions().get("my_schema", "valid", List.of(), pathWithPGCatalogAndDoc()), Matchers.notNullValue());
    expectedException.expectMessage("Unknown function: my_schema.invalid()");
    nodeCtx.functions().get("my_schema", "invalid", List.of(), pathWithPGCatalogAndDoc());
}
Also used : UserDefinedFunctionMetadata(io.crate.expression.udf.UserDefinedFunctionMetadata) UserDefinedFunctionsMetadata(io.crate.expression.udf.UserDefinedFunctionsMetadata) Test(org.junit.Test) CrateDummyClusterServiceUnitTest(io.crate.test.integration.CrateDummyClusterServiceUnitTest)

Aggregations

UserDefinedFunctionMetadata (io.crate.expression.udf.UserDefinedFunctionMetadata)6 Test (org.junit.Test)4 UserDefinedFunctionsMetadata (io.crate.expression.udf.UserDefinedFunctionsMetadata)3 CrateDummyClusterServiceUnitTest (io.crate.test.integration.CrateDummyClusterServiceUnitTest)3 ScriptException (javax.script.ScriptException)2 Metadata (org.elasticsearch.cluster.metadata.Metadata)2 Matchers.containsString (org.hamcrest.Matchers.containsString)2 AnalyzedCreateFunction (io.crate.analyze.AnalyzedCreateFunction)1 FunctionArgumentDefinition (io.crate.analyze.FunctionArgumentDefinition)1 SymbolEvaluator (io.crate.analyze.SymbolEvaluator)1 Input (io.crate.data.Input)1 Row (io.crate.data.Row)1 Row1 (io.crate.data.Row1)1 RowConsumer (io.crate.data.RowConsumer)1 OneRowActionListener (io.crate.execution.support.OneRowActionListener)1 Symbol (io.crate.expression.symbol.Symbol)1 CreateUserDefinedFunctionRequest (io.crate.expression.udf.CreateUserDefinedFunctionRequest)1 UDFLanguage (io.crate.expression.udf.UDFLanguage)1 UserDefinedFunctionService (io.crate.expression.udf.UserDefinedFunctionService)1 FunctionName (io.crate.metadata.FunctionName)1