Search in sources :

Example 1 with IndexParts

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

the class UserPrivileges method matchPrivilegeOfAnyType.

/**
 * Try to match a privilege at the given collection ignoring the type.
 */
public boolean matchPrivilegeOfAnyType(Privilege.Clazz clazz, @Nullable String ident) {
    boolean foundPrivilege;
    switch(clazz) {
        case CLUSTER:
            foundPrivilege = hasAnyClusterPrivilege();
            break;
        case SCHEMA:
            foundPrivilege = hasAnySchemaPrivilege(ident);
            if (foundPrivilege == false) {
                foundPrivilege = hasAnyClusterPrivilege();
            }
            break;
        case TABLE:
            foundPrivilege = hasAnyTablePrivilege(ident);
            if (foundPrivilege == false) {
                String schemaIdent = new IndexParts(ident).getSchema();
                foundPrivilege = hasAnySchemaPrivilege(schemaIdent);
                if (foundPrivilege == false) {
                    foundPrivilege = hasAnyClusterPrivilege();
                }
            }
            break;
        case VIEW:
            foundPrivilege = hasAnyViewPrivilege(ident);
            if (foundPrivilege == false) {
                String schemaIdent = new IndexParts(ident).getSchema();
                foundPrivilege = hasAnySchemaPrivilege(schemaIdent);
                if (foundPrivilege == false) {
                    foundPrivilege = hasAnyClusterPrivilege();
                }
            }
            break;
        default:
            throw new IllegalStateException("Unsupported privilege class=" + clazz);
    }
    return foundPrivilege;
}
Also used : IndexParts(io.crate.metadata.IndexParts)

Example 2 with IndexParts

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

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

the class UserPrivileges method matchPrivilege.

/**
 * Try to match a privilege at the given collection.
 * If none is found for the current {@link Privilege.Clazz}, try to find one on the upper class.
 * If a privilege with a {@link Privilege.State#DENY} state is found, false is returned.
 */
public boolean matchPrivilege(@Nullable Privilege.Type type, Privilege.Clazz clazz, @Nullable String ident, String defaultSchema) {
    Privilege foundPrivilege = privilegeByIdent.get(new PrivilegeIdent(type, clazz, ident));
    if (foundPrivilege == null) {
        switch(clazz) {
            case SCHEMA:
                foundPrivilege = privilegeByIdent.get(new PrivilegeIdent(type, Privilege.Clazz.CLUSTER, null));
                break;
            case TABLE:
            case VIEW:
                String schemaIdent = new IndexParts(ident, defaultSchema).getSchema();
                foundPrivilege = privilegeByIdent.get(new PrivilegeIdent(type, Privilege.Clazz.SCHEMA, schemaIdent));
                if (foundPrivilege == null) {
                    foundPrivilege = privilegeByIdent.get(new PrivilegeIdent(type, Privilege.Clazz.CLUSTER, null));
                }
                break;
            default:
        }
    }
    if (foundPrivilege == null) {
        return false;
    }
    switch(foundPrivilege.state()) {
        case GRANT:
            return true;
        case DENY:
        default:
            return false;
    }
}
Also used : IndexParts(io.crate.metadata.IndexParts)

Example 4 with IndexParts

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

the class SQLExecutor method resolveTableInfo.

public <T extends TableInfo> T resolveTableInfo(String tableName) {
    IndexParts indexParts = new IndexParts(tableName);
    QualifiedName qualifiedName = QualifiedName.of(indexParts.getSchema(), indexParts.getTable());
    return (T) schemas.resolveTableInfo(qualifiedName, Operation.READ, sessionContext.sessionUser(), sessionContext.searchPath());
}
Also used : IndexParts(io.crate.metadata.IndexParts) QualifiedName(io.crate.sql.tree.QualifiedName)

Aggregations

IndexParts (io.crate.metadata.IndexParts)4 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 CoordinatorTxnCtx (io.crate.metadata.CoordinatorTxnCtx)1 FunctionName (io.crate.metadata.FunctionName)1 FunctionProvider (io.crate.metadata.FunctionProvider)1 FunctionType (io.crate.metadata.FunctionType)1 GeneratedReference (io.crate.metadata.GeneratedReference)1 NodeContext (io.crate.metadata.NodeContext)1 Scalar (io.crate.metadata.Scalar)1 NO_BLOB_NOR_DANGLING (io.crate.metadata.doc.DocSchemaInfo.NO_BLOB_NOR_DANGLING)1 DocTableInfoBuilder (io.crate.metadata.doc.DocTableInfoBuilder)1 Signature (io.crate.metadata.functions.Signature)1