Search in sources :

Example 91 with AbstractFunctionCallExpression

use of org.apache.hyracks.algebricks.core.algebra.expressions.AbstractFunctionCallExpression in project asterixdb by apache.

the class TranslateIntervalExpressionRule method rewritePost.

@Override
public boolean rewritePost(Mutable<ILogicalOperator> opRef, IOptimizationContext context) throws AlgebricksException {
    AbstractLogicalOperator op = (AbstractLogicalOperator) opRef.getValue();
    if (op.getOperatorTag() != LogicalOperatorTag.SELECT) {
        return false;
    }
    SelectOperator selectOp = (SelectOperator) op;
    Mutable<ILogicalExpression> exprRef = selectOp.getCondition();
    ILogicalExpression expr = exprRef.getValue();
    if (expr.getExpressionTag() != LogicalExpressionTag.FUNCTION_CALL) {
        return false;
    }
    AbstractFunctionCallExpression funcExpr = (AbstractFunctionCallExpression) expr;
    if (funcExpr.getArguments().size() != 2) {
        return false;
    }
    if (!hasTranslatableInterval(funcExpr)) {
        return false;
    }
    return translateIntervalExpression(exprRef, funcExpr);
}
Also used : ILogicalExpression(org.apache.hyracks.algebricks.core.algebra.base.ILogicalExpression) SelectOperator(org.apache.hyracks.algebricks.core.algebra.operators.logical.SelectOperator) AbstractLogicalOperator(org.apache.hyracks.algebricks.core.algebra.operators.logical.AbstractLogicalOperator) AbstractFunctionCallExpression(org.apache.hyracks.algebricks.core.algebra.expressions.AbstractFunctionCallExpression)

Example 92 with AbstractFunctionCallExpression

use of org.apache.hyracks.algebricks.core.algebra.expressions.AbstractFunctionCallExpression in project asterixdb by apache.

the class InvertedIndexAccessMethod method findTokensFunc.

private ScalarFunctionCallExpression findTokensFunc(FunctionIdentifier funcId, IOptimizableFuncExpr optFuncExpr, int subTreeIndex) {
    //find either a gram-tokens or a word-tokens function that exists in optFuncExpr.subTrees' assignsAndUnnests
    OptimizableOperatorSubTree subTree = null;
    LogicalVariable targetVar = null;
    subTree = optFuncExpr.getOperatorSubTree(subTreeIndex);
    if (subTree == null) {
        return null;
    }
    targetVar = optFuncExpr.getLogicalVar(subTreeIndex);
    if (targetVar == null) {
        return null;
    }
    for (AbstractLogicalOperator op : subTree.getAssignsAndUnnests()) {
        if (op.getOperatorTag() != LogicalOperatorTag.ASSIGN) {
            continue;
        }
        List<Mutable<ILogicalExpression>> exprList = ((AssignOperator) op).getExpressions();
        for (Mutable<ILogicalExpression> expr : exprList) {
            if (expr.getValue().getExpressionTag() != LogicalExpressionTag.FUNCTION_CALL) {
                continue;
            }
            AbstractFunctionCallExpression funcExpr = (AbstractFunctionCallExpression) expr.getValue();
            if (funcExpr.getFunctionIdentifier() != funcId) {
                continue;
            }
            ILogicalExpression varExpr = funcExpr.getArguments().get(0).getValue();
            if (varExpr.getExpressionTag() != LogicalExpressionTag.VARIABLE) {
                continue;
            }
            if (((VariableReferenceExpression) varExpr).getVariableReference() == targetVar) {
                continue;
            }
            return (ScalarFunctionCallExpression) funcExpr;
        }
    }
    return null;
}
Also used : LogicalVariable(org.apache.hyracks.algebricks.core.algebra.base.LogicalVariable) Mutable(org.apache.commons.lang3.mutable.Mutable) ILogicalExpression(org.apache.hyracks.algebricks.core.algebra.base.ILogicalExpression) AbstractLogicalOperator(org.apache.hyracks.algebricks.core.algebra.operators.logical.AbstractLogicalOperator) AbstractFunctionCallExpression(org.apache.hyracks.algebricks.core.algebra.expressions.AbstractFunctionCallExpression) AssignOperator(org.apache.hyracks.algebricks.core.algebra.operators.logical.AssignOperator) ScalarFunctionCallExpression(org.apache.hyracks.algebricks.core.algebra.expressions.ScalarFunctionCallExpression)

Example 93 with AbstractFunctionCallExpression

use of org.apache.hyracks.algebricks.core.algebra.expressions.AbstractFunctionCallExpression in project asterixdb by apache.

the class IntroduceLSMComponentFilterRule method changePlan.

private void changePlan(List<IOptimizableFuncExpr> optFuncExprs, AbstractLogicalOperator op, Dataset dataset, IOptimizationContext context) throws AlgebricksException {
    Queue<Mutable<ILogicalOperator>> queue = new LinkedList<>(op.getInputs());
    while (!queue.isEmpty()) {
        AbstractLogicalOperator descendantOp = (AbstractLogicalOperator) queue.poll().getValue();
        if (descendantOp == null) {
            continue;
        }
        if (descendantOp.getOperatorTag() == LogicalOperatorTag.DATASOURCESCAN) {
            DataSourceScanOperator dataSourceScanOp = (DataSourceScanOperator) descendantOp;
            DataSource ds = (DataSource) dataSourceScanOp.getDataSource();
            if (dataset.getDatasetName().compareTo(((DatasetDataSource) ds).getDataset().getDatasetName()) == 0) {
                List<LogicalVariable> minFilterVars = new ArrayList<>();
                List<LogicalVariable> maxFilterVars = new ArrayList<>();
                AssignOperator assignOp = createAssignOperator(optFuncExprs, minFilterVars, maxFilterVars, context);
                dataSourceScanOp.setMinFilterVars(minFilterVars);
                dataSourceScanOp.setMaxFilterVars(maxFilterVars);
                List<Mutable<ILogicalExpression>> additionalFilteringExpressions = new ArrayList<>();
                for (LogicalVariable var : assignOp.getVariables()) {
                    additionalFilteringExpressions.add(new MutableObject<ILogicalExpression>(new VariableReferenceExpression(var)));
                }
                dataSourceScanOp.setAdditionalFilteringExpressions(additionalFilteringExpressions);
                assignOp.getInputs().add(new MutableObject<>(dataSourceScanOp.getInputs().get(0).getValue()));
                dataSourceScanOp.getInputs().get(0).setValue(assignOp);
            }
        } else if (descendantOp.getOperatorTag() == LogicalOperatorTag.UNNEST_MAP) {
            UnnestMapOperator unnestMapOp = (UnnestMapOperator) descendantOp;
            ILogicalExpression unnestExpr = unnestMapOp.getExpressionRef().getValue();
            if (unnestExpr.getExpressionTag() == LogicalExpressionTag.FUNCTION_CALL) {
                AbstractFunctionCallExpression f = (AbstractFunctionCallExpression) unnestExpr;
                FunctionIdentifier fid = f.getFunctionIdentifier();
                if (!fid.equals(BuiltinFunctions.INDEX_SEARCH)) {
                    throw new IllegalStateException();
                }
                AccessMethodJobGenParams jobGenParams = new AccessMethodJobGenParams();
                jobGenParams.readFromFuncArgs(f.getArguments());
                if (dataset.getDatasetName().compareTo(jobGenParams.datasetName) == 0) {
                    List<LogicalVariable> minFilterVars = new ArrayList<>();
                    List<LogicalVariable> maxFilterVars = new ArrayList<>();
                    AssignOperator assignOp = createAssignOperator(optFuncExprs, minFilterVars, maxFilterVars, context);
                    unnestMapOp.setMinFilterVars(minFilterVars);
                    unnestMapOp.setMaxFilterVars(maxFilterVars);
                    List<Mutable<ILogicalExpression>> additionalFilteringExpressions = new ArrayList<>();
                    for (LogicalVariable var : assignOp.getVariables()) {
                        additionalFilteringExpressions.add(new MutableObject<ILogicalExpression>(new VariableReferenceExpression(var)));
                    }
                    unnestMapOp.setAdditionalFilteringExpressions(additionalFilteringExpressions);
                    assignOp.getInputs().add(new MutableObject<>(unnestMapOp.getInputs().get(0).getValue()));
                    unnestMapOp.getInputs().get(0).setValue(assignOp);
                }
            }
        }
        queue.addAll(descendantOp.getInputs());
    }
}
Also used : LogicalVariable(org.apache.hyracks.algebricks.core.algebra.base.LogicalVariable) AbstractLogicalOperator(org.apache.hyracks.algebricks.core.algebra.operators.logical.AbstractLogicalOperator) UnnestMapOperator(org.apache.hyracks.algebricks.core.algebra.operators.logical.UnnestMapOperator) AbstractFunctionCallExpression(org.apache.hyracks.algebricks.core.algebra.expressions.AbstractFunctionCallExpression) ArrayList(java.util.ArrayList) AssignOperator(org.apache.hyracks.algebricks.core.algebra.operators.logical.AssignOperator) LinkedList(java.util.LinkedList) DataSource(org.apache.asterix.metadata.declared.DataSource) DatasetDataSource(org.apache.asterix.metadata.declared.DatasetDataSource) Mutable(org.apache.commons.lang3.mutable.Mutable) FunctionIdentifier(org.apache.hyracks.algebricks.core.algebra.functions.FunctionIdentifier) ILogicalExpression(org.apache.hyracks.algebricks.core.algebra.base.ILogicalExpression) DataSourceScanOperator(org.apache.hyracks.algebricks.core.algebra.operators.logical.DataSourceScanOperator) VariableReferenceExpression(org.apache.hyracks.algebricks.core.algebra.expressions.VariableReferenceExpression) ArrayList(java.util.ArrayList) LinkedList(java.util.LinkedList) List(java.util.List) MutableObject(org.apache.commons.lang3.mutable.MutableObject)

Example 94 with AbstractFunctionCallExpression

use of org.apache.hyracks.algebricks.core.algebra.expressions.AbstractFunctionCallExpression 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 95 with AbstractFunctionCallExpression

use of org.apache.hyracks.algebricks.core.algebra.expressions.AbstractFunctionCallExpression 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)

Aggregations

AbstractFunctionCallExpression (org.apache.hyracks.algebricks.core.algebra.expressions.AbstractFunctionCallExpression)138 ILogicalExpression (org.apache.hyracks.algebricks.core.algebra.base.ILogicalExpression)111 Mutable (org.apache.commons.lang3.mutable.Mutable)59 LogicalVariable (org.apache.hyracks.algebricks.core.algebra.base.LogicalVariable)54 ArrayList (java.util.ArrayList)50 VariableReferenceExpression (org.apache.hyracks.algebricks.core.algebra.expressions.VariableReferenceExpression)38 IAType (org.apache.asterix.om.types.IAType)37 AssignOperator (org.apache.hyracks.algebricks.core.algebra.operators.logical.AssignOperator)37 ILogicalOperator (org.apache.hyracks.algebricks.core.algebra.base.ILogicalOperator)35 AbstractLogicalOperator (org.apache.hyracks.algebricks.core.algebra.operators.logical.AbstractLogicalOperator)35 FunctionIdentifier (org.apache.hyracks.algebricks.core.algebra.functions.FunctionIdentifier)34 ScalarFunctionCallExpression (org.apache.hyracks.algebricks.core.algebra.expressions.ScalarFunctionCallExpression)33 ConstantExpression (org.apache.hyracks.algebricks.core.algebra.expressions.ConstantExpression)25 ARecordType (org.apache.asterix.om.types.ARecordType)20 AsterixConstantValue (org.apache.asterix.om.constants.AsterixConstantValue)19 AlgebricksException (org.apache.hyracks.algebricks.common.exceptions.AlgebricksException)19 Pair (org.apache.hyracks.algebricks.common.utils.Pair)19 MutableObject (org.apache.commons.lang3.mutable.MutableObject)18 AString (org.apache.asterix.om.base.AString)17 MetadataProvider (org.apache.asterix.metadata.declared.MetadataProvider)13