Search in sources :

Example 1 with FunctionIdentifier

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

the class LangExpressionToPlanTranslator method createComparisonExpression.

protected AbstractFunctionCallExpression createComparisonExpression(OperatorType t) {
    FunctionIdentifier fi = operatorTypeToFunctionIdentifier(t);
    IFunctionInfo finfo = FunctionUtil.getFunctionInfo(fi);
    return new ScalarFunctionCallExpression(finfo);
}
Also used : FunctionIdentifier(org.apache.hyracks.algebricks.core.algebra.functions.FunctionIdentifier) IFunctionInfo(org.apache.hyracks.algebricks.core.algebra.functions.IFunctionInfo) ScalarFunctionCallExpression(org.apache.hyracks.algebricks.core.algebra.expressions.ScalarFunctionCallExpression)

Example 2 with FunctionIdentifier

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

the class LangExpressionToPlanTranslator method visit.

@Override
public Pair<ILogicalOperator, LogicalVariable> visit(ListConstructor lc, Mutable<ILogicalOperator> tupSource) throws CompilationException {
    FunctionIdentifier fid = (lc.getType() == ListConstructor.Type.ORDERED_LIST_CONSTRUCTOR) ? BuiltinFunctions.ORDERED_LIST_CONSTRUCTOR : BuiltinFunctions.UNORDERED_LIST_CONSTRUCTOR;
    AbstractFunctionCallExpression f = new ScalarFunctionCallExpression(FunctionUtil.getFunctionInfo(fid));
    LogicalVariable v1 = context.newVar();
    AssignOperator a = new AssignOperator(v1, new MutableObject<>(f));
    Mutable<ILogicalOperator> topOp = tupSource;
    for (Expression expr : lc.getExprList()) {
        Pair<ILogicalExpression, Mutable<ILogicalOperator>> eo = langExprToAlgExpression(expr, topOp);
        f.getArguments().add(new MutableObject<>(eo.first));
        topOp = eo.second;
    }
    a.getInputs().add(topOp);
    return new Pair<>(a, v1);
}
Also used : LogicalVariable(org.apache.hyracks.algebricks.core.algebra.base.LogicalVariable) AbstractFunctionCallExpression(org.apache.hyracks.algebricks.core.algebra.expressions.AbstractFunctionCallExpression) ILogicalOperator(org.apache.hyracks.algebricks.core.algebra.base.ILogicalOperator) AssignOperator(org.apache.hyracks.algebricks.core.algebra.operators.logical.AssignOperator) FunctionIdentifier(org.apache.hyracks.algebricks.core.algebra.functions.FunctionIdentifier) Mutable(org.apache.commons.lang3.mutable.Mutable) ILogicalExpression(org.apache.hyracks.algebricks.core.algebra.base.ILogicalExpression) 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) ScalarFunctionCallExpression(org.apache.hyracks.algebricks.core.algebra.expressions.ScalarFunctionCallExpression) 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 3 with FunctionIdentifier

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

the class ExpressionTypeComputer method getTypeForFunction.

private IAType getTypeForFunction(AbstractFunctionCallExpression expr, IVariableTypeEnvironment env, IMetadataProvider<?, ?> mp) throws AlgebricksException {
    FunctionIdentifier fi = expr.getFunctionIdentifier();
    // Note: built-in functions + udfs
    IResultTypeComputer rtc;
    FunctionSignature signature = new FunctionSignature(fi.getNamespace(), fi.getName(), fi.getArity());
    if (BuiltinFunctions.isBuiltinCompilerFunction(signature, true)) {
        rtc = BuiltinFunctions.getResultTypeComputer(fi);
    } else {
        rtc = ((ExternalFunctionInfo) expr.getFunctionInfo()).getResultTypeComputer();
    }
    if (rtc == null) {
        throw new AlgebricksException("Type computer missing for " + fi);
    }
    return rtc.computeType(expr, env, mp);
}
Also used : IResultTypeComputer(org.apache.asterix.om.typecomputer.base.IResultTypeComputer) FunctionIdentifier(org.apache.hyracks.algebricks.core.algebra.functions.FunctionIdentifier) AlgebricksException(org.apache.hyracks.algebricks.common.exceptions.AlgebricksException) FunctionSignature(org.apache.asterix.common.functions.FunctionSignature)

Example 4 with FunctionIdentifier

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

the class ExtractFunctionsFromJoinConditionRule method assignFunctionExpressions.

private boolean assignFunctionExpressions(AbstractLogicalOperator joinOp, ILogicalExpression expr, IOptimizationContext context) throws AlgebricksException {
    if (expr.getExpressionTag() != LogicalExpressionTag.FUNCTION_CALL) {
        return false;
    }
    AbstractFunctionCallExpression fexp = (AbstractFunctionCallExpression) expr;
    FunctionIdentifier fi = fexp.getFunctionIdentifier();
    boolean modified = false;
    if (fi.equals(AlgebricksBuiltinFunctions.AND) || fi.equals(AlgebricksBuiltinFunctions.OR) || processArgumentsToFunction(fi)) {
        for (Mutable<ILogicalExpression> a : fexp.getArguments()) {
            if (assignFunctionExpressions(joinOp, a.getValue(), context)) {
                modified = true;
            }
        }
        return modified;
    } else if (AlgebricksBuiltinFunctions.isComparisonFunction(fi) || isComparisonFunction(fi)) {
        for (Mutable<ILogicalExpression> exprRef : fexp.getArguments()) {
            if (exprRef.getValue().getExpressionTag() == LogicalExpressionTag.FUNCTION_CALL) {
                LogicalVariable newVar = context.newVar();
                AssignOperator newAssign = new AssignOperator(newVar, new MutableObject<ILogicalExpression>(exprRef.getValue().cloneExpression()));
                newAssign.setExecutionMode(joinOp.getExecutionMode());
                // Place assign below joinOp.
                List<LogicalVariable> used = new ArrayList<LogicalVariable>();
                VariableUtilities.getUsedVariables(newAssign, used);
                Mutable<ILogicalOperator> leftBranchRef = joinOp.getInputs().get(0);
                ILogicalOperator leftBranch = leftBranchRef.getValue();
                List<LogicalVariable> leftBranchVariables = new ArrayList<LogicalVariable>();
                VariableUtilities.getLiveVariables(leftBranch, leftBranchVariables);
                if (leftBranchVariables.containsAll(used)) {
                    // place assign on left branch
                    newAssign.getInputs().add(new MutableObject<ILogicalOperator>(leftBranch));
                    leftBranchRef.setValue(newAssign);
                    modified = true;
                } else {
                    Mutable<ILogicalOperator> rightBranchRef = joinOp.getInputs().get(1);
                    ILogicalOperator rightBranch = rightBranchRef.getValue();
                    List<LogicalVariable> rightBranchVariables = new ArrayList<LogicalVariable>();
                    VariableUtilities.getLiveVariables(rightBranch, rightBranchVariables);
                    if (rightBranchVariables.containsAll(used)) {
                        // place assign on right branch
                        newAssign.getInputs().add(new MutableObject<ILogicalOperator>(rightBranch));
                        rightBranchRef.setValue(newAssign);
                        modified = true;
                    }
                }
                if (modified) {
                    // Replace original expr with variable reference.
                    exprRef.setValue(new VariableReferenceExpression(newVar));
                    context.computeAndSetTypeEnvironmentForOperator(newAssign);
                    context.computeAndSetTypeEnvironmentForOperator(joinOp);
                }
            }
        }
        return modified;
    } else {
        return false;
    }
}
Also used : LogicalVariable(org.apache.hyracks.algebricks.core.algebra.base.LogicalVariable) AbstractFunctionCallExpression(org.apache.hyracks.algebricks.core.algebra.expressions.AbstractFunctionCallExpression) ILogicalOperator(org.apache.hyracks.algebricks.core.algebra.base.ILogicalOperator) AssignOperator(org.apache.hyracks.algebricks.core.algebra.operators.logical.AssignOperator) FunctionIdentifier(org.apache.hyracks.algebricks.core.algebra.functions.FunctionIdentifier) Mutable(org.apache.commons.lang3.mutable.Mutable) ILogicalExpression(org.apache.hyracks.algebricks.core.algebra.base.ILogicalExpression) VariableReferenceExpression(org.apache.hyracks.algebricks.core.algebra.expressions.VariableReferenceExpression) ArrayList(java.util.ArrayList) List(java.util.List) MutableObject(org.apache.commons.lang3.mutable.MutableObject)

Example 5 with FunctionIdentifier

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

the class FullTextContainsParameterCheckRule method checkParamter.

/**
     * Check the correctness of the parameters of the ftcontains(). Also rearrange options as arguments.
     * The expected form of ftcontains() is ftcontains(expression1, expression2, parameters as a record).
     */
private boolean checkParamter(ILogicalOperator op, Mutable<ILogicalExpression> exprRef, IOptimizationContext context) throws AlgebricksException {
    ILogicalExpression expr = exprRef.getValue();
    if (expr.getExpressionTag() != LogicalExpressionTag.FUNCTION_CALL) {
        return false;
    }
    AbstractFunctionCallExpression funcExpr = (AbstractFunctionCallExpression) expr;
    FunctionIdentifier fi = funcExpr.getFunctionIdentifier();
    // Collects the correct number of arguments - it can be 2 if a user doesn't provide any option.
    int numberOfCorrectArguments = 0;
    String functionName = "";
    if (fi == BuiltinFunctions.FULLTEXT_CONTAINS) {
        numberOfCorrectArguments = FULLTEXT_QUERY_WITH_OPTION_NO_OF_ARGUMENTS;
        functionName = BuiltinFunctions.FULLTEXT_CONTAINS.getName();
    } else if (fi == BuiltinFunctions.FULLTEXT_CONTAINS_WO_OPTION) {
        numberOfCorrectArguments = FULLTEXT_QUERY_WITHOUT_OPTION_NO_OF_ARGUMENTS;
        functionName = BuiltinFunctions.FULLTEXT_CONTAINS_WO_OPTION.getName();
    }
    // If numberOfCorrectArguments is greater than zero, then this is a full-text search query.
    if (numberOfCorrectArguments > 0) {
        // Don't need to check this operator again.
        context.addToDontApplySet(this, op);
        List<Mutable<ILogicalExpression>> oldExprs = funcExpr.getArguments();
        List<Mutable<ILogicalExpression>> newExprs = new ArrayList<>();
        // The number of parameters should be three: exp1, exp2, and the option
        if (oldExprs.size() != numberOfCorrectArguments) {
            throw new AlgebricksException(functionName + " should have " + numberOfCorrectArguments + " parameters.");
        }
        // The last expression before the option needs to be copied first.
        for (int i = 0; i <= LAST_EXPRESSION_POS_BEFORE_OPTION; i++) {
            newExprs.add(new MutableObject<ILogicalExpression>((ILogicalExpression) oldExprs.get(i).getValue()));
        }
        // Sanity check for the types of the first two parameters
        checkFirstAndSecondParamter(oldExprs, functionName);
        // Checks and transforms the actual full-text parameters.
        if (numberOfCorrectArguments == FULLTEXT_QUERY_WITH_OPTION_NO_OF_ARGUMENTS) {
            checkValueForThirdParameter(oldExprs.get(2), newExprs);
        } else {
            // no option provided case: sets the default option here.
            setDefaultValueForThirdParameter(newExprs);
        }
        // Resets the last argument.
        funcExpr.getArguments().clear();
        funcExpr.getArguments().addAll(newExprs);
        return true;
    }
    return false;
}
Also used : FunctionIdentifier(org.apache.hyracks.algebricks.core.algebra.functions.FunctionIdentifier) Mutable(org.apache.commons.lang3.mutable.Mutable) ILogicalExpression(org.apache.hyracks.algebricks.core.algebra.base.ILogicalExpression) AbstractFunctionCallExpression(org.apache.hyracks.algebricks.core.algebra.expressions.AbstractFunctionCallExpression) ArrayList(java.util.ArrayList) AlgebricksException(org.apache.hyracks.algebricks.common.exceptions.AlgebricksException) AString(org.apache.asterix.om.base.AString)

Aggregations

FunctionIdentifier (org.apache.hyracks.algebricks.core.algebra.functions.FunctionIdentifier)50 AbstractFunctionCallExpression (org.apache.hyracks.algebricks.core.algebra.expressions.AbstractFunctionCallExpression)33 ILogicalExpression (org.apache.hyracks.algebricks.core.algebra.base.ILogicalExpression)31 ArrayList (java.util.ArrayList)15 Mutable (org.apache.commons.lang3.mutable.Mutable)14 LogicalVariable (org.apache.hyracks.algebricks.core.algebra.base.LogicalVariable)13 AbstractLogicalOperator (org.apache.hyracks.algebricks.core.algebra.operators.logical.AbstractLogicalOperator)11 AlgebricksException (org.apache.hyracks.algebricks.common.exceptions.AlgebricksException)10 ILogicalOperator (org.apache.hyracks.algebricks.core.algebra.base.ILogicalOperator)10 VariableReferenceExpression (org.apache.hyracks.algebricks.core.algebra.expressions.VariableReferenceExpression)10 MetadataProvider (org.apache.asterix.metadata.declared.MetadataProvider)7 IAType (org.apache.asterix.om.types.IAType)7 AssignOperator (org.apache.hyracks.algebricks.core.algebra.operators.logical.AssignOperator)7 List (java.util.List)6 MutableObject (org.apache.commons.lang3.mutable.MutableObject)6 AggregateFunctionCallExpression (org.apache.hyracks.algebricks.core.algebra.expressions.AggregateFunctionCallExpression)6 ConstantExpression (org.apache.hyracks.algebricks.core.algebra.expressions.ConstantExpression)6 ScalarFunctionCallExpression (org.apache.hyracks.algebricks.core.algebra.expressions.ScalarFunctionCallExpression)6 AString (org.apache.asterix.om.base.AString)5 Pair (org.apache.hyracks.algebricks.common.utils.Pair)5