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"));
}
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"));
}
}
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);
}
}
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);
}
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());
}
Aggregations