Search in sources :

Example 16 with IFunctionInfo

use of org.apache.hyracks.algebricks.core.algebra.functions.IFunctionInfo in project asterixdb by apache.

the class LangExpressionToPlanTranslator method lookupUserDefinedFunction.

private AbstractFunctionCallExpression lookupUserDefinedFunction(FunctionSignature signature, List<Mutable<ILogicalExpression>> args) throws MetadataException {
    if (signature.getNamespace() == null) {
        return null;
    }
    Function function = MetadataManager.INSTANCE.getFunction(metadataProvider.getMetadataTxnContext(), signature);
    if (function == null) {
        return null;
    }
    AbstractFunctionCallExpression f;
    if (function.getLanguage().equalsIgnoreCase(Function.LANGUAGE_JAVA)) {
        IFunctionInfo finfo = ExternalFunctionCompilerUtil.getExternalFunctionInfo(metadataProvider.getMetadataTxnContext(), function);
        f = new ScalarFunctionCallExpression(finfo, args);
    } else if (function.getLanguage().equalsIgnoreCase(Function.LANGUAGE_AQL)) {
        IFunctionInfo finfo = FunctionUtil.getFunctionInfo(signature);
        f = new ScalarFunctionCallExpression(finfo, args);
    } else {
        throw new MetadataException(" User defined functions written in " + function.getLanguage() + " are not supported");
    }
    return f;
}
Also used : Function(org.apache.asterix.metadata.entities.Function) IFunctionInfo(org.apache.hyracks.algebricks.core.algebra.functions.IFunctionInfo) AbstractFunctionCallExpression(org.apache.hyracks.algebricks.core.algebra.expressions.AbstractFunctionCallExpression) MetadataException(org.apache.asterix.metadata.MetadataException) ScalarFunctionCallExpression(org.apache.hyracks.algebricks.core.algebra.expressions.ScalarFunctionCallExpression)

Example 17 with IFunctionInfo

use of org.apache.hyracks.algebricks.core.algebra.functions.IFunctionInfo in project asterixdb by apache.

the class LangExpressionToPlanTranslator method translateUpsert.

private ILogicalOperator translateUpsert(DatasetDataSource targetDatasource, Mutable<ILogicalExpression> varRef, List<Mutable<ILogicalExpression>> varRefsForLoading, List<Mutable<ILogicalExpression>> additionalFilteringExpressions, ILogicalOperator assign, List<String> additionalFilteringField, LogicalVariable unnestVar, ILogicalOperator topOp, List<Mutable<ILogicalExpression>> exprs, LogicalVariable resVar, AssignOperator additionalFilteringAssign, ICompiledDmlStatement stmt) throws AlgebricksException {
    if (!targetDatasource.getDataset().allow(topOp, DatasetUtil.OP_UPSERT)) {
        throw new AlgebricksException(targetDatasource.getDataset().getDatasetName() + ": upsert into dataset is not supported on Datasets with Meta records");
    }
    ProjectOperator project = (ProjectOperator) topOp;
    CompiledUpsertStatement compiledUpsert = (CompiledUpsertStatement) stmt;
    Expression returnExpression = compiledUpsert.getReturnExpression();
    InsertDeleteUpsertOperator upsertOp;
    ILogicalOperator rootOperator;
    if (targetDatasource.getDataset().hasMetaPart()) {
        if (returnExpression != null) {
            throw new AlgebricksException("Returning not allowed on datasets with Meta records");
        }
        AssignOperator metaAndKeysAssign;
        List<LogicalVariable> metaAndKeysVars;
        List<Mutable<ILogicalExpression>> metaAndKeysExprs;
        List<Mutable<ILogicalExpression>> metaExpSingletonList;
        metaAndKeysVars = new ArrayList<>();
        metaAndKeysExprs = new ArrayList<>();
        // add the meta function
        IFunctionInfo finfoMeta = FunctionUtil.getFunctionInfo(BuiltinFunctions.META);
        ScalarFunctionCallExpression metaFunction = new ScalarFunctionCallExpression(finfoMeta, new MutableObject<>(new VariableReferenceExpression(unnestVar)));
        // create assign for the meta part
        LogicalVariable metaVar = context.newVar();
        metaExpSingletonList = new ArrayList<>(1);
        metaExpSingletonList.add(new MutableObject<>(new VariableReferenceExpression(metaVar)));
        metaAndKeysVars.add(metaVar);
        metaAndKeysExprs.add(new MutableObject<>(metaFunction));
        project.getVariables().add(metaVar);
        varRefsForLoading.clear();
        for (Mutable<ILogicalExpression> assignExpr : exprs) {
            if (assignExpr.getValue().getExpressionTag() == LogicalExpressionTag.FUNCTION_CALL) {
                AbstractFunctionCallExpression funcCall = (AbstractFunctionCallExpression) assignExpr.getValue();
                funcCall.substituteVar(resVar, unnestVar);
                LogicalVariable pkVar = context.newVar();
                metaAndKeysVars.add(pkVar);
                metaAndKeysExprs.add(new MutableObject<>(assignExpr.getValue()));
                project.getVariables().add(pkVar);
                varRefsForLoading.add(new MutableObject<>(new VariableReferenceExpression(pkVar)));
            }
        }
        // A change feed, we don't need the assign to access PKs
        upsertOp = new InsertDeleteUpsertOperator(targetDatasource, varRef, varRefsForLoading, metaExpSingletonList, InsertDeleteUpsertOperator.Kind.UPSERT, false);
        // Create and add a new variable used for representing the original record
        upsertOp.setPrevRecordVar(context.newVar());
        upsertOp.setPrevRecordType(targetDatasource.getItemType());
        if (targetDatasource.getDataset().hasMetaPart()) {
            List<LogicalVariable> metaVars = new ArrayList<>();
            metaVars.add(context.newVar());
            upsertOp.setPrevAdditionalNonFilteringVars(metaVars);
            List<Object> metaTypes = new ArrayList<>();
            metaTypes.add(targetDatasource.getMetaItemType());
            upsertOp.setPrevAdditionalNonFilteringTypes(metaTypes);
        }
        if (additionalFilteringField != null) {
            upsertOp.setPrevFilterVar(context.newVar());
            upsertOp.setPrevFilterType(((ARecordType) targetDatasource.getItemType()).getFieldType(additionalFilteringField.get(0)));
            additionalFilteringAssign.getInputs().clear();
            additionalFilteringAssign.getInputs().add(assign.getInputs().get(0));
            upsertOp.getInputs().add(new MutableObject<>(additionalFilteringAssign));
        } else {
            upsertOp.getInputs().add(assign.getInputs().get(0));
        }
        metaAndKeysAssign = new AssignOperator(metaAndKeysVars, metaAndKeysExprs);
        metaAndKeysAssign.getInputs().add(topOp.getInputs().get(0));
        topOp.getInputs().set(0, new MutableObject<>(metaAndKeysAssign));
        upsertOp.setAdditionalFilteringExpressions(additionalFilteringExpressions);
    } else {
        upsertOp = new InsertDeleteUpsertOperator(targetDatasource, varRef, varRefsForLoading, InsertDeleteUpsertOperator.Kind.UPSERT, false);
        upsertOp.setAdditionalFilteringExpressions(additionalFilteringExpressions);
        upsertOp.getInputs().add(new MutableObject<>(assign));
        // Create and add a new variable used for representing the original record
        ARecordType recordType = (ARecordType) targetDatasource.getItemType();
        upsertOp.setPrevRecordVar(context.newVar());
        upsertOp.setPrevRecordType(recordType);
        if (additionalFilteringField != null) {
            upsertOp.setPrevFilterVar(context.newVar());
            upsertOp.setPrevFilterType(recordType.getFieldType(additionalFilteringField.get(0)));
        }
    }
    rootOperator = new DelegateOperator(new CommitOperator(returnExpression == null));
    rootOperator.getInputs().add(new MutableObject<>(upsertOp));
    // Compiles the return expression.
    return processReturningExpression(rootOperator, upsertOp, compiledUpsert);
}
Also used : IFunctionInfo(org.apache.hyracks.algebricks.core.algebra.functions.IFunctionInfo) ArrayList(java.util.ArrayList) DelegateOperator(org.apache.hyracks.algebricks.core.algebra.operators.logical.DelegateOperator) CommitOperator(org.apache.asterix.algebra.operators.CommitOperator) ScalarFunctionCallExpression(org.apache.hyracks.algebricks.core.algebra.expressions.ScalarFunctionCallExpression) LogicalVariable(org.apache.hyracks.algebricks.core.algebra.base.LogicalVariable) CompiledUpsertStatement(org.apache.asterix.translator.CompiledStatements.CompiledUpsertStatement) ProjectOperator(org.apache.hyracks.algebricks.core.algebra.operators.logical.ProjectOperator) ILogicalOperator(org.apache.hyracks.algebricks.core.algebra.base.ILogicalOperator) AbstractFunctionCallExpression(org.apache.hyracks.algebricks.core.algebra.expressions.AbstractFunctionCallExpression) AlgebricksException(org.apache.hyracks.algebricks.common.exceptions.AlgebricksException) AssignOperator(org.apache.hyracks.algebricks.core.algebra.operators.logical.AssignOperator) Mutable(org.apache.commons.lang3.mutable.Mutable) ILogicalExpression(org.apache.hyracks.algebricks.core.algebra.base.ILogicalExpression) InsertDeleteUpsertOperator(org.apache.hyracks.algebricks.core.algebra.operators.logical.InsertDeleteUpsertOperator) 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) MutableObject(org.apache.commons.lang3.mutable.MutableObject) ARecordType(org.apache.asterix.om.types.ARecordType)

Example 18 with IFunctionInfo

use of org.apache.hyracks.algebricks.core.algebra.functions.IFunctionInfo in project asterixdb by apache.

the class BuiltinFunctions method makeSerializableAggregateFunctionExpression.

public static AggregateFunctionCallExpression makeSerializableAggregateFunctionExpression(FunctionIdentifier fi, List<Mutable<ILogicalExpression>> args) {
    IFunctionInfo finfo = getAsterixFunctionInfo(fi);
    IFunctionInfo serializableFinfo = aggregateToSerializableAggregate.get(finfo);
    if (serializableFinfo == null) {
        throw new IllegalStateException("no serializable implementation for aggregate function " + serializableFinfo);
    }
    IFunctionInfo fiLocal = aggregateToLocalAggregate.get(serializableFinfo);
    IFunctionInfo fiGlobal = aggregateToGlobalAggregate.get(serializableFinfo);
    if (fiLocal != null && fiGlobal != null) {
        AggregateFunctionCallExpression fun = new AggregateFunctionCallExpression(serializableFinfo, true, args);
        fun.setStepTwoAggregate(fiGlobal);
        fun.setStepOneAggregate(fiLocal);
        return fun;
    } else {
        return new AggregateFunctionCallExpression(serializableFinfo, false, args);
    }
}
Also used : AggregateFunctionCallExpression(org.apache.hyracks.algebricks.core.algebra.expressions.AggregateFunctionCallExpression) IFunctionInfo(org.apache.hyracks.algebricks.core.algebra.functions.IFunctionInfo)

Example 19 with IFunctionInfo

use of org.apache.hyracks.algebricks.core.algebra.functions.IFunctionInfo in project asterixdb by apache.

the class BuiltinFunctions method makeAggregateFunctionExpression.

public static AggregateFunctionCallExpression makeAggregateFunctionExpression(FunctionIdentifier fi, List<Mutable<ILogicalExpression>> args) {
    IFunctionInfo finfo = getAsterixFunctionInfo(fi);
    IFunctionInfo fiLocal = aggregateToLocalAggregate.get(finfo);
    IFunctionInfo fiGlobal = aggregateToGlobalAggregate.get(finfo);
    if (fiLocal != null && fiGlobal != null) {
        AggregateFunctionCallExpression fun = new AggregateFunctionCallExpression(finfo, true, args);
        fun.setStepTwoAggregate(fiGlobal);
        fun.setStepOneAggregate(fiLocal);
        return fun;
    } else {
        return new AggregateFunctionCallExpression(finfo, false, args);
    }
}
Also used : AggregateFunctionCallExpression(org.apache.hyracks.algebricks.core.algebra.expressions.AggregateFunctionCallExpression) IFunctionInfo(org.apache.hyracks.algebricks.core.algebra.functions.IFunctionInfo)

Example 20 with IFunctionInfo

use of org.apache.hyracks.algebricks.core.algebra.functions.IFunctionInfo in project asterixdb by apache.

the class BuiltinFunctions method isBuiltinCompilerFunction.

public static boolean isBuiltinCompilerFunction(FunctionSignature signature, boolean includePrivateFunctions) {
    FunctionIdentifier fi = new FunctionIdentifier(FunctionConstants.ASTERIX_NS, signature.getName(), signature.getArity());
    IFunctionInfo finfo = getAsterixFunctionInfo(fi);
    if (builtinPublicFunctionsSet.keySet().contains(finfo) || (includePrivateFunctions && builtinPrivateFunctionsSet.keySet().contains(finfo))) {
        return true;
    }
    fi = new FunctionIdentifier(AlgebricksBuiltinFunctions.ALGEBRICKS_NS, signature.getName(), signature.getArity());
    finfo = getAsterixFunctionInfo(fi);
    if (builtinPublicFunctionsSet.keySet().contains(finfo) || (includePrivateFunctions && builtinPrivateFunctionsSet.keySet().contains(finfo))) {
        return true;
    }
    return false;
}
Also used : FunctionIdentifier(org.apache.hyracks.algebricks.core.algebra.functions.FunctionIdentifier) IFunctionInfo(org.apache.hyracks.algebricks.core.algebra.functions.IFunctionInfo)

Aggregations

IFunctionInfo (org.apache.hyracks.algebricks.core.algebra.functions.IFunctionInfo)24 ILogicalExpression (org.apache.hyracks.algebricks.core.algebra.base.ILogicalExpression)14 ScalarFunctionCallExpression (org.apache.hyracks.algebricks.core.algebra.expressions.ScalarFunctionCallExpression)13 LogicalVariable (org.apache.hyracks.algebricks.core.algebra.base.LogicalVariable)12 Mutable (org.apache.commons.lang3.mutable.Mutable)11 VariableReferenceExpression (org.apache.hyracks.algebricks.core.algebra.expressions.VariableReferenceExpression)11 MutableObject (org.apache.commons.lang3.mutable.MutableObject)10 ArrayList (java.util.ArrayList)9 ILogicalOperator (org.apache.hyracks.algebricks.core.algebra.base.ILogicalOperator)9 AbstractFunctionCallExpression (org.apache.hyracks.algebricks.core.algebra.expressions.AbstractFunctionCallExpression)7 AggregateFunctionCallExpression (org.apache.hyracks.algebricks.core.algebra.expressions.AggregateFunctionCallExpression)5 UnnestingFunctionCallExpression (org.apache.hyracks.algebricks.core.algebra.expressions.UnnestingFunctionCallExpression)5 IAObject (org.apache.asterix.om.base.IAObject)4 AlgebricksException (org.apache.hyracks.algebricks.common.exceptions.AlgebricksException)4 ConstantExpression (org.apache.hyracks.algebricks.core.algebra.expressions.ConstantExpression)4 FunctionIdentifier (org.apache.hyracks.algebricks.core.algebra.functions.FunctionIdentifier)4 AbstractUnnestMapOperator (org.apache.hyracks.algebricks.core.algebra.operators.logical.AbstractUnnestMapOperator)4 AssignOperator (org.apache.hyracks.algebricks.core.algebra.operators.logical.AssignOperator)4 HashSet (java.util.HashSet)3 AOrderedList (org.apache.asterix.om.base.AOrderedList)3