Search in sources :

Example 1 with FunctionName

use of io.crate.metadata.FunctionName in project crate by crate.

the class Function method printFunctionWithParenthesis.

private void printFunctionWithParenthesis(StringBuilder builder, Style style) {
    FunctionName functionName = info.ident().fqnName();
    builder.append(functionName.displayName());
    builder.append("(");
    for (int i = 0; i < arguments.size(); i++) {
        Symbol argument = arguments.get(i);
        builder.append(argument.toString(style));
        if (i + 1 < arguments.size()) {
            builder.append(", ");
        }
    }
    builder.append(")");
    printFilter(builder, style);
}
Also used : FunctionName(io.crate.metadata.FunctionName)

Example 2 with FunctionName

use of io.crate.metadata.FunctionName in project crate by crate.

the class UserDefinedFunctionService method validateFunctionIsNotInUseByGeneratedColumn.

void validateFunctionIsNotInUseByGeneratedColumn(String schema, String functionName, UserDefinedFunctionsMetadata functionsMetadata, ClusterState currentState) {
    // The iteration of schemas/tables must happen on the node context WITHOUT the UDF already removed.
    // Otherwise the lazy table factories will already fail while evaluating generated functionsMetadata.
    // To avoid that, a copy of the node context with the removed UDF function is used on concrete expression evaluation.
    var nodeCtxWithRemovedFunction = new NodeContext(nodeCtx.functions().copyOf());
    updateImplementations(schema, functionsMetadata.functionsMetadata().stream(), nodeCtxWithRemovedFunction);
    var metadata = currentState.metadata();
    var indices = Stream.of(metadata.getConcreteAllIndices()).filter(NO_BLOB_NOR_DANGLING).map(IndexParts::new).filter(indexParts -> !indexParts.isPartitioned()).collect(Collectors.toList());
    var templates = metadata.getTemplates().keysIt();
    while (templates.hasNext()) {
        var indexParts = new IndexParts(templates.next());
        if (indexParts.isPartitioned()) {
            indices.add(indexParts);
        }
    }
    var indexNameExpressionResolver = new IndexNameExpressionResolver();
    for (var indexParts : indices) {
        var tableInfo = new DocTableInfoBuilder(nodeCtx, indexParts.toRelationName(), currentState, indexNameExpressionResolver).build();
        TableReferenceResolver tableReferenceResolver = new TableReferenceResolver(tableInfo.columns(), tableInfo.ident());
        CoordinatorTxnCtx coordinatorTxnCtx = CoordinatorTxnCtx.systemTransactionContext();
        ExpressionAnalyzer exprAnalyzer = new ExpressionAnalyzer(coordinatorTxnCtx, nodeCtxWithRemovedFunction, ParamTypeHints.EMPTY, tableReferenceResolver, null);
        for (var ref : tableInfo.columns()) {
            if (ref instanceof GeneratedReference) {
                var genRef = (GeneratedReference) ref;
                Expression expression = SqlParser.createExpression(genRef.formattedGeneratedExpression());
                try {
                    exprAnalyzer.convert(expression, new ExpressionAnalysisContext(coordinatorTxnCtx.sessionContext()));
                } catch (UnsupportedOperationException e) {
                    throw new IllegalArgumentException("Cannot drop function '" + functionName + "', it is still in use by '" + tableInfo + "." + genRef + "'");
                }
            }
        }
    }
}
Also used : ParamTypeHints(io.crate.analyze.ParamTypeHints) ExpressionAnalyzer(io.crate.analyze.expressions.ExpressionAnalyzer) IndexParts(io.crate.metadata.IndexParts) UserDefinedFunctionAlreadyExistsException(io.crate.exceptions.UserDefinedFunctionAlreadyExistsException) FunctionName(io.crate.metadata.FunctionName) ClusterService(org.elasticsearch.cluster.service.ClusterService) FunctionProvider(io.crate.metadata.FunctionProvider) HashMap(java.util.HashMap) TableReferenceResolver(io.crate.analyze.expressions.TableReferenceResolver) Inject(org.elasticsearch.common.inject.Inject) ArrayList(java.util.ArrayList) ClusterState(org.elasticsearch.cluster.ClusterState) Metadata(org.elasticsearch.cluster.metadata.Metadata) ClusterStateUpdateTask(org.elasticsearch.cluster.ClusterStateUpdateTask) Locale(java.util.Locale) Map(java.util.Map) SqlParser(io.crate.sql.parser.SqlParser) Nullable(javax.annotation.Nullable) ScriptException(javax.script.ScriptException) NO_BLOB_NOR_DANGLING(io.crate.metadata.doc.DocSchemaInfo.NO_BLOB_NOR_DANGLING) NodeContext(io.crate.metadata.NodeContext) Iterator(java.util.Iterator) GeneratedReference(io.crate.metadata.GeneratedReference) ExpressionAnalysisContext(io.crate.analyze.expressions.ExpressionAnalysisContext) DataType(io.crate.types.DataType) Signature(io.crate.metadata.functions.Signature) AcknowledgedResponse(org.elasticsearch.action.support.master.AcknowledgedResponse) Collectors(java.util.stream.Collectors) Lists2(io.crate.common.collections.Lists2) List(java.util.List) Logger(org.apache.logging.log4j.Logger) Stream(java.util.stream.Stream) UserDefinedFunctionUnknownException(io.crate.exceptions.UserDefinedFunctionUnknownException) FunctionType(io.crate.metadata.FunctionType) Singleton(org.elasticsearch.common.inject.Singleton) TimeValue(io.crate.common.unit.TimeValue) VisibleForTesting(io.crate.common.annotations.VisibleForTesting) Scalar(io.crate.metadata.Scalar) IndexNameExpressionResolver(org.elasticsearch.cluster.metadata.IndexNameExpressionResolver) LogManager(org.apache.logging.log4j.LogManager) Expression(io.crate.sql.tree.Expression) ActionListener(org.elasticsearch.action.ActionListener) CoordinatorTxnCtx(io.crate.metadata.CoordinatorTxnCtx) DocTableInfoBuilder(io.crate.metadata.doc.DocTableInfoBuilder) CoordinatorTxnCtx(io.crate.metadata.CoordinatorTxnCtx) ExpressionAnalysisContext(io.crate.analyze.expressions.ExpressionAnalysisContext) GeneratedReference(io.crate.metadata.GeneratedReference) NodeContext(io.crate.metadata.NodeContext) ExpressionAnalyzer(io.crate.analyze.expressions.ExpressionAnalyzer) IndexParts(io.crate.metadata.IndexParts) Expression(io.crate.sql.tree.Expression) DocTableInfoBuilder(io.crate.metadata.doc.DocTableInfoBuilder) IndexNameExpressionResolver(org.elasticsearch.cluster.metadata.IndexNameExpressionResolver) TableReferenceResolver(io.crate.analyze.expressions.TableReferenceResolver)

Example 3 with FunctionName

use of io.crate.metadata.FunctionName in project crate by crate.

the class UserDefinedFunctionService method updateImplementations.

public void updateImplementations(String schema, Stream<UserDefinedFunctionMetadata> userDefinedFunctions, NodeContext nodeCtx) {
    final Map<FunctionName, List<FunctionProvider>> implementations = new HashMap<>();
    Iterator<UserDefinedFunctionMetadata> it = userDefinedFunctions.iterator();
    while (it.hasNext()) {
        UserDefinedFunctionMetadata udf = it.next();
        FunctionProvider resolver = buildFunctionResolver(udf);
        if (resolver == null) {
            continue;
        }
        var functionName = new FunctionName(udf.schema(), udf.name());
        var resolvers = implementations.computeIfAbsent(functionName, k -> new ArrayList<>());
        resolvers.add(resolver);
    }
    nodeCtx.functions().registerUdfFunctionImplementationsForSchema(schema, implementations);
}
Also used : FunctionName(io.crate.metadata.FunctionName) HashMap(java.util.HashMap) FunctionProvider(io.crate.metadata.FunctionProvider) ArrayList(java.util.ArrayList) List(java.util.List)

Example 4 with FunctionName

use of io.crate.metadata.FunctionName 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 5 with FunctionName

use of io.crate.metadata.FunctionName 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)

Aggregations

FunctionName (io.crate.metadata.FunctionName)7 Signature (io.crate.metadata.functions.Signature)4 TypeSignature (io.crate.types.TypeSignature)3 Test (org.junit.Test)3 FunctionProvider (io.crate.metadata.FunctionProvider)2 ArrayList (java.util.ArrayList)2 HashMap (java.util.HashMap)2 List (java.util.List)2 ScriptException (javax.script.ScriptException)2 Matchers.containsString (org.hamcrest.Matchers.containsString)2 FunctionArgumentDefinition (io.crate.analyze.FunctionArgumentDefinition)1 ParamTypeHints (io.crate.analyze.ParamTypeHints)1 ExpressionAnalysisContext (io.crate.analyze.expressions.ExpressionAnalysisContext)1 ExpressionAnalyzer (io.crate.analyze.expressions.ExpressionAnalyzer)1 TableReferenceResolver (io.crate.analyze.expressions.TableReferenceResolver)1 VisibleForTesting (io.crate.common.annotations.VisibleForTesting)1 Lists2 (io.crate.common.collections.Lists2)1 TimeValue (io.crate.common.unit.TimeValue)1 UserDefinedFunctionAlreadyExistsException (io.crate.exceptions.UserDefinedFunctionAlreadyExistsException)1 UserDefinedFunctionUnknownException (io.crate.exceptions.UserDefinedFunctionUnknownException)1