Search in sources :

Example 1 with CreateFunctionStatement

use of org.apache.asterix.lang.common.statement.CreateFunctionStatement in project asterixdb by apache.

the class QueryTranslator method handleCreateFunctionStatement.

protected void handleCreateFunctionStatement(MetadataProvider metadataProvider, Statement stmt) throws Exception {
    CreateFunctionStatement cfs = (CreateFunctionStatement) stmt;
    String dataverse = getActiveDataverseName(cfs.getSignature().getNamespace());
    cfs.getSignature().setNamespace(dataverse);
    String functionName = cfs.getaAterixFunction().getName();
    MetadataTransactionContext mdTxnCtx = MetadataManager.INSTANCE.beginTransaction();
    metadataProvider.setMetadataTxnContext(mdTxnCtx);
    MetadataLockManager.INSTANCE.functionStatementBegin(metadataProvider.getLocks(), dataverse, dataverse + "." + functionName);
    try {
        Dataverse dv = MetadataManager.INSTANCE.getDataverse(mdTxnCtx, dataverse);
        if (dv == null) {
            throw new AlgebricksException("There is no dataverse with this name " + dataverse + ".");
        }
        // If the function body contains function calls, theirs reference count won't be increased.
        Function function = new Function(dataverse, functionName, cfs.getaAterixFunction().getArity(), cfs.getParamList(), Function.RETURNTYPE_VOID, cfs.getFunctionBody(), Function.LANGUAGE_AQL, FunctionKind.SCALAR.toString(), 0);
        MetadataManager.INSTANCE.addFunction(mdTxnCtx, function);
        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) CreateFunctionStatement(org.apache.asterix.lang.common.statement.CreateFunctionStatement) AlgebricksException(org.apache.hyracks.algebricks.common.exceptions.AlgebricksException) MetadataTransactionContext(org.apache.asterix.metadata.MetadataTransactionContext) Dataverse(org.apache.asterix.metadata.entities.Dataverse) 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)

Aggregations

IOException (java.io.IOException)1 RemoteException (java.rmi.RemoteException)1 ACIDException (org.apache.asterix.common.exceptions.ACIDException)1 AsterixException (org.apache.asterix.common.exceptions.AsterixException)1 CompilationException (org.apache.asterix.common.exceptions.CompilationException)1 CreateFunctionStatement (org.apache.asterix.lang.common.statement.CreateFunctionStatement)1 MetadataException (org.apache.asterix.metadata.MetadataException)1 MetadataTransactionContext (org.apache.asterix.metadata.MetadataTransactionContext)1 Dataverse (org.apache.asterix.metadata.entities.Dataverse)1 Function (org.apache.asterix.metadata.entities.Function)1 AlgebricksException (org.apache.hyracks.algebricks.common.exceptions.AlgebricksException)1 HyracksDataException (org.apache.hyracks.api.exceptions.HyracksDataException)1