Search in sources :

Example 11 with FunctionSignature

use of org.apache.asterix.common.functions.FunctionSignature in project asterixdb by apache.

the class QueryTranslator method handleConnectFeedStatement.

private void handleConnectFeedStatement(MetadataProvider metadataProvider, Statement stmt) throws Exception {
    FeedConnection fc;
    ConnectFeedStatement cfs = (ConnectFeedStatement) stmt;
    String dataverseName = getActiveDataverse(cfs.getDataverseName());
    String feedName = cfs.getFeedName();
    String datasetName = cfs.getDatasetName().getValue();
    String policyName = cfs.getPolicy();
    MetadataTransactionContext mdTxnCtx = MetadataManager.INSTANCE.beginTransaction();
    metadataProvider.setMetadataTxnContext(mdTxnCtx);
    // Check whether feed is alive
    ActiveLifecycleListener activeListener = (ActiveLifecycleListener) appCtx.getActiveLifecycleListener();
    ActiveJobNotificationHandler activeEventHandler = activeListener.getNotificationHandler();
    if (activeEventHandler.getActiveEntityListener(new EntityId(Feed.EXTENSION_NAME, dataverseName, feedName)) != null) {
        throw new CompilationException(ErrorCode.FEED_CHANGE_FEED_CONNECTIVITY_ON_ALIVE_FEED, feedName);
    }
    // Transaction handling
    MetadataLockManager.INSTANCE.connectFeedBegin(metadataProvider.getLocks(), dataverseName, dataverseName + "." + datasetName, dataverseName + "." + feedName);
    try {
        // validation
        FeedMetadataUtil.validateIfDatasetExists(metadataProvider, dataverseName, datasetName, mdTxnCtx);
        Feed feed = FeedMetadataUtil.validateIfFeedExists(dataverseName, feedName, metadataProvider.getMetadataTxnContext());
        ARecordType outputType = FeedMetadataUtil.getOutputType(feed, feed.getAdapterConfiguration(), ExternalDataConstants.KEY_TYPE_NAME);
        List<FunctionSignature> appliedFunctions = cfs.getAppliedFunctions();
        for (FunctionSignature func : appliedFunctions) {
            if (MetadataManager.INSTANCE.getFunction(mdTxnCtx, func) == null) {
                throw new CompilationException(ErrorCode.FEED_CONNECT_FEED_APPLIED_INVALID_FUNCTION, func.getName());
            }
        }
        fc = MetadataManager.INSTANCE.getFeedConnection(metadataProvider.getMetadataTxnContext(), dataverseName, feedName, datasetName);
        if (fc != null) {
            throw new AlgebricksException("Feed" + feedName + " is already connected dataset " + datasetName);
        }
        fc = new FeedConnection(dataverseName, feedName, datasetName, appliedFunctions, policyName, outputType.toString());
        MetadataManager.INSTANCE.addFeedConnection(metadataProvider.getMetadataTxnContext(), fc);
        // Increase function reference count.
        for (FunctionSignature funcSig : appliedFunctions) {
            // The function should be cached in Metadata manager, so this operation is not that expensive.
            Function func = MetadataManager.INSTANCE.getFunction(mdTxnCtx, funcSig);
            func.reference();
            MetadataManager.INSTANCE.updateFunction(mdTxnCtx, func);
        }
        MetadataManager.INSTANCE.commitTransaction(mdTxnCtx);
    } catch (Exception e) {
        abort(e, e, mdTxnCtx);
        throw e;
    } finally {
        metadataProvider.getLocks().unlock();
    }
}
Also used : CompilationException(org.apache.asterix.common.exceptions.CompilationException) FeedConnection(org.apache.asterix.metadata.entities.FeedConnection) AlgebricksException(org.apache.hyracks.algebricks.common.exceptions.AlgebricksException) MetadataTransactionContext(org.apache.asterix.metadata.MetadataTransactionContext) ConnectFeedStatement(org.apache.asterix.lang.common.statement.ConnectFeedStatement) FunctionSignature(org.apache.asterix.common.functions.FunctionSignature) ACIDException(org.apache.asterix.common.exceptions.ACIDException) MetadataException(org.apache.asterix.metadata.MetadataException) AlgebricksException(org.apache.hyracks.algebricks.common.exceptions.AlgebricksException) HyracksDataException(org.apache.hyracks.api.exceptions.HyracksDataException) CompilationException(org.apache.asterix.common.exceptions.CompilationException) IOException(java.io.IOException) RemoteException(java.rmi.RemoteException) AsterixException(org.apache.asterix.common.exceptions.AsterixException) EntityId(org.apache.asterix.active.EntityId) Function(org.apache.asterix.metadata.entities.Function) ActiveLifecycleListener(org.apache.asterix.active.ActiveLifecycleListener) ActiveJobNotificationHandler(org.apache.asterix.active.ActiveJobNotificationHandler) ARecordType(org.apache.asterix.om.types.ARecordType) Feed(org.apache.asterix.metadata.entities.Feed)

Example 12 with FunctionSignature

use of org.apache.asterix.common.functions.FunctionSignature in project asterixdb by apache.

the class LangExpressionToPlanTranslator method visit.

@Override
public Pair<ILogicalOperator, LogicalVariable> visit(CallExpr fcall, Mutable<ILogicalOperator> tupSource) throws CompilationException {
    LogicalVariable v = context.newVar();
    FunctionSignature signature = fcall.getFunctionSignature();
    List<Mutable<ILogicalExpression>> args = new ArrayList<>();
    Mutable<ILogicalOperator> topOp = tupSource;
    for (Expression expr : fcall.getExprList()) {
        switch(expr.getKind()) {
            case VARIABLE_EXPRESSION:
                LogicalVariable var = context.getVar(((VariableExpr) expr).getVar().getId());
                args.add(new MutableObject<>(new VariableReferenceExpression(var)));
                break;
            case LITERAL_EXPRESSION:
                LiteralExpr val = (LiteralExpr) expr;
                args.add(new MutableObject<>(new ConstantExpression(new AsterixConstantValue(ConstantHelper.objectFromLiteral(val.getValue())))));
                break;
            default:
                Pair<ILogicalExpression, Mutable<ILogicalOperator>> eo = langExprToAlgExpression(expr, topOp);
                AbstractLogicalOperator o1 = (AbstractLogicalOperator) eo.second.getValue();
                args.add(new MutableObject<>(eo.first));
                if (o1 != null && !(o1.getOperatorTag() == LogicalOperatorTag.ASSIGN && hasOnlyChild(o1, topOp))) {
                    topOp = eo.second;
                }
                break;
        }
    }
    AbstractFunctionCallExpression f;
    if ((f = lookupUserDefinedFunction(signature, args)) == null) {
        f = lookupBuiltinFunction(signature.getName(), signature.getArity(), args);
    }
    if (f == null) {
        throw new CompilationException(" Unknown function " + signature.getName() + "@" + signature.getArity());
    }
    // Put hints into function call expr.
    if (fcall.hasHints()) {
        for (IExpressionAnnotation hint : fcall.getHints()) {
            f.getAnnotations().put(hint, hint);
        }
    }
    AssignOperator op = new AssignOperator(v, new MutableObject<>(f));
    if (topOp != null) {
        op.getInputs().add(topOp);
    }
    return new Pair<>(op, v);
}
Also used : LogicalVariable(org.apache.hyracks.algebricks.core.algebra.base.LogicalVariable) CompilationException(org.apache.asterix.common.exceptions.CompilationException) AbstractLogicalOperator(org.apache.hyracks.algebricks.core.algebra.operators.logical.AbstractLogicalOperator) ILogicalOperator(org.apache.hyracks.algebricks.core.algebra.base.ILogicalOperator) ConstantExpression(org.apache.hyracks.algebricks.core.algebra.expressions.ConstantExpression) AbstractFunctionCallExpression(org.apache.hyracks.algebricks.core.algebra.expressions.AbstractFunctionCallExpression) ArrayList(java.util.ArrayList) IExpressionAnnotation(org.apache.hyracks.algebricks.core.algebra.expressions.IExpressionAnnotation) AssignOperator(org.apache.hyracks.algebricks.core.algebra.operators.logical.AssignOperator) FunctionSignature(org.apache.asterix.common.functions.FunctionSignature) Mutable(org.apache.commons.lang3.mutable.Mutable) ILogicalExpression(org.apache.hyracks.algebricks.core.algebra.base.ILogicalExpression) AsterixConstantValue(org.apache.asterix.om.constants.AsterixConstantValue) ILangExpression(org.apache.asterix.lang.common.base.ILangExpression) AggregateFunctionCallExpression(org.apache.hyracks.algebricks.core.algebra.expressions.AggregateFunctionCallExpression) Expression(org.apache.asterix.lang.common.base.Expression) VariableReferenceExpression(org.apache.hyracks.algebricks.core.algebra.expressions.VariableReferenceExpression) QuantifiedExpression(org.apache.asterix.lang.common.expression.QuantifiedExpression) ScalarFunctionCallExpression(org.apache.hyracks.algebricks.core.algebra.expressions.ScalarFunctionCallExpression) ConstantExpression(org.apache.hyracks.algebricks.core.algebra.expressions.ConstantExpression) ILogicalExpression(org.apache.hyracks.algebricks.core.algebra.base.ILogicalExpression) AbstractFunctionCallExpression(org.apache.hyracks.algebricks.core.algebra.expressions.AbstractFunctionCallExpression) UnnestingFunctionCallExpression(org.apache.hyracks.algebricks.core.algebra.expressions.UnnestingFunctionCallExpression) VariableReferenceExpression(org.apache.hyracks.algebricks.core.algebra.expressions.VariableReferenceExpression) LiteralExpr(org.apache.asterix.lang.common.expression.LiteralExpr) VariableExpr(org.apache.asterix.lang.common.expression.VariableExpr) GbyVariableExpressionPair(org.apache.asterix.lang.common.expression.GbyVariableExpressionPair) Pair(org.apache.hyracks.algebricks.common.utils.Pair) QuantifiedPair(org.apache.asterix.lang.common.struct.QuantifiedPair)

Example 13 with FunctionSignature

use of org.apache.asterix.common.functions.FunctionSignature in project asterixdb by apache.

the class QueryTranslator method handleFunctionDropStatement.

protected void handleFunctionDropStatement(MetadataProvider metadataProvider, Statement stmt) throws Exception {
    FunctionDropStatement stmtDropFunction = (FunctionDropStatement) stmt;
    FunctionSignature signature = stmtDropFunction.getFunctionSignature();
    signature.setNamespace(getActiveDataverseName(signature.getNamespace()));
    MetadataTransactionContext mdTxnCtx = MetadataManager.INSTANCE.beginTransaction();
    metadataProvider.setMetadataTxnContext(mdTxnCtx);
    MetadataLockManager.INSTANCE.functionStatementBegin(metadataProvider.getLocks(), signature.getNamespace(), signature.getNamespace() + "." + signature.getName());
    try {
        Function function = MetadataManager.INSTANCE.getFunction(mdTxnCtx, signature);
        if (function == null) {
            if (!stmtDropFunction.getIfExists()) {
                throw new AlgebricksException("Unknonw function " + signature);
            }
        } else if (function.getReferenceCount() != 0) {
            throw new AlgebricksException("Function " + signature + " is being used. It cannot be dropped.");
        } else {
            MetadataManager.INSTANCE.dropFunction(mdTxnCtx, signature);
        }
        MetadataManager.INSTANCE.commitTransaction(mdTxnCtx);
    } catch (Exception e) {
        abort(e, e, mdTxnCtx);
        throw e;
    } finally {
        metadataProvider.getLocks().unlock();
    }
}
Also used : Function(org.apache.asterix.metadata.entities.Function) AlgebricksException(org.apache.hyracks.algebricks.common.exceptions.AlgebricksException) MetadataTransactionContext(org.apache.asterix.metadata.MetadataTransactionContext) FunctionDropStatement(org.apache.asterix.lang.common.statement.FunctionDropStatement) FunctionSignature(org.apache.asterix.common.functions.FunctionSignature) ACIDException(org.apache.asterix.common.exceptions.ACIDException) MetadataException(org.apache.asterix.metadata.MetadataException) AlgebricksException(org.apache.hyracks.algebricks.common.exceptions.AlgebricksException) HyracksDataException(org.apache.hyracks.api.exceptions.HyracksDataException) CompilationException(org.apache.asterix.common.exceptions.CompilationException) IOException(java.io.IOException) RemoteException(java.rmi.RemoteException) AsterixException(org.apache.asterix.common.exceptions.AsterixException)

Example 14 with FunctionSignature

use of org.apache.asterix.common.functions.FunctionSignature in project asterixdb by apache.

the class MetadataCache method dropDataverse.

public Dataverse dropDataverse(Dataverse dataverse) {
    synchronized (dataverses) {
        synchronized (datasets) {
            synchronized (indexes) {
                synchronized (datatypes) {
                    synchronized (functions) {
                        synchronized (adapters) {
                            synchronized (libraries) {
                                synchronized (feeds) {
                                    synchronized (compactionPolicies) {
                                        datasets.remove(dataverse.getDataverseName());
                                        indexes.remove(dataverse.getDataverseName());
                                        datatypes.remove(dataverse.getDataverseName());
                                        adapters.remove(dataverse.getDataverseName());
                                        compactionPolicies.remove(dataverse.getDataverseName());
                                        List<FunctionSignature> markedFunctionsForRemoval = new ArrayList<>();
                                        for (FunctionSignature signature : functions.keySet()) {
                                            if (signature.getNamespace().equals(dataverse.getDataverseName())) {
                                                markedFunctionsForRemoval.add(signature);
                                            }
                                        }
                                        for (FunctionSignature signature : markedFunctionsForRemoval) {
                                            functions.remove(signature);
                                        }
                                        libraries.remove(dataverse.getDataverseName());
                                        feeds.remove(dataverse.getDataverseName());
                                        return dataverses.remove(dataverse.getDataverseName());
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
    }
}
Also used : ArrayList(java.util.ArrayList) FunctionSignature(org.apache.asterix.common.functions.FunctionSignature)

Example 15 with FunctionSignature

use of org.apache.asterix.common.functions.FunctionSignature in project asterixdb by apache.

the class MetadataCache method dropFunction.

public Function dropFunction(Function function) {
    synchronized (functions) {
        FunctionSignature signature = new FunctionSignature(function.getDataverseName(), function.getName(), function.getArity());
        Function fun = functions.get(signature);
        if (fun == null) {
            return null;
        }
        return functions.remove(signature);
    }
}
Also used : Function(org.apache.asterix.metadata.entities.Function) FunctionSignature(org.apache.asterix.common.functions.FunctionSignature)

Aggregations

FunctionSignature (org.apache.asterix.common.functions.FunctionSignature)30 ArrayList (java.util.ArrayList)11 Expression (org.apache.asterix.lang.common.base.Expression)11 Function (org.apache.asterix.metadata.entities.Function)9 CompilationException (org.apache.asterix.common.exceptions.CompilationException)8 ILangExpression (org.apache.asterix.lang.common.base.ILangExpression)6 ACIDException (org.apache.asterix.common.exceptions.ACIDException)5 IOException (java.io.IOException)4 RemoteException (java.rmi.RemoteException)4 AsterixException (org.apache.asterix.common.exceptions.AsterixException)4 CallExpr (org.apache.asterix.lang.common.expression.CallExpr)4 LiteralExpr (org.apache.asterix.lang.common.expression.LiteralExpr)4 VariableExpr (org.apache.asterix.lang.common.expression.VariableExpr)4 CaseExpression (org.apache.asterix.lang.sqlpp.expression.CaseExpression)4 MetadataException (org.apache.asterix.metadata.MetadataException)4 HyracksDataException (org.apache.hyracks.api.exceptions.HyracksDataException)4 EntityId (org.apache.asterix.active.EntityId)3 SelectExpression (org.apache.asterix.lang.sqlpp.expression.SelectExpression)3 MetadataTransactionContext (org.apache.asterix.metadata.MetadataTransactionContext)3 FeedConnection (org.apache.asterix.metadata.entities.FeedConnection)3