Search in sources :

Example 41 with ILogicalExpression

use of org.apache.hyracks.algebricks.core.algebra.base.ILogicalExpression in project asterixdb by apache.

the class FeedScanCollectionToUnnest method findVarOriginExpression.

private ILogicalExpression findVarOriginExpression(LogicalVariable v, ILogicalOperator op) throws AlgebricksException {
    boolean searchInputs = false;
    if (!(op instanceof AbstractAssignOperator)) {
        searchInputs = true;
    } else {
        AbstractAssignOperator aao = (AbstractAssignOperator) op;
        List<LogicalVariable> producedVars = new ArrayList<>();
        VariableUtilities.getProducedVariables(op, producedVars);
        int exprIndex = producedVars.indexOf(v);
        if (exprIndex == -1) {
            searchInputs = true;
        } else {
            ILogicalExpression originalCandidate = aao.getExpressions().get(exprIndex).getValue();
            if (originalCandidate.getExpressionTag() == LogicalExpressionTag.VARIABLE) {
                searchInputs = true;
            } else {
                return originalCandidate;
            }
        }
    }
    if (searchInputs) {
        for (Mutable<ILogicalOperator> childOp : op.getInputs()) {
            ILogicalExpression ret = findVarOriginExpression(v, childOp.getValue());
            if (ret != null) {
                return ret;
            }
        }
    }
    throw new IllegalStateException("Unable to find the original expression that produced variable " + v);
}
Also used : LogicalVariable(org.apache.hyracks.algebricks.core.algebra.base.LogicalVariable) ILogicalExpression(org.apache.hyracks.algebricks.core.algebra.base.ILogicalExpression) AbstractAssignOperator(org.apache.hyracks.algebricks.core.algebra.operators.logical.AbstractAssignOperator) ILogicalOperator(org.apache.hyracks.algebricks.core.algebra.base.ILogicalOperator) ArrayList(java.util.ArrayList)

Example 42 with ILogicalExpression

use of org.apache.hyracks.algebricks.core.algebra.base.ILogicalExpression in project asterixdb by apache.

the class FeedScanCollectionToUnnest method rewritePost.

@Override
public boolean rewritePost(Mutable<ILogicalOperator> opRef, IOptimizationContext context) throws AlgebricksException {
    AbstractLogicalOperator op = (AbstractLogicalOperator) opRef.getValue();
    if (context.checkIfInDontApplySet(this, op)) {
        return false;
    }
    if (op.getOperatorTag() != LogicalOperatorTag.UNNEST) {
        context.addToDontApplySet(this, op);
        return false;
    }
    UnnestOperator unnest = (UnnestOperator) op;
    ILogicalExpression unnestExpr = unnest.getExpressionRef().getValue();
    if (needsScanCollection(unnestExpr, op)) {
        ILogicalExpression newExpr = new UnnestingFunctionCallExpression(FunctionUtil.getFunctionInfo(BuiltinFunctions.SCAN_COLLECTION), new MutableObject<ILogicalExpression>(unnestExpr));
        unnest.getExpressionRef().setValue(newExpr);
        context.addToDontApplySet(this, op);
        return true;
    }
    context.addToDontApplySet(this, op);
    return false;
}
Also used : UnnestOperator(org.apache.hyracks.algebricks.core.algebra.operators.logical.UnnestOperator) ILogicalExpression(org.apache.hyracks.algebricks.core.algebra.base.ILogicalExpression) UnnestingFunctionCallExpression(org.apache.hyracks.algebricks.core.algebra.expressions.UnnestingFunctionCallExpression) AbstractLogicalOperator(org.apache.hyracks.algebricks.core.algebra.operators.logical.AbstractLogicalOperator)

Example 43 with ILogicalExpression

use of org.apache.hyracks.algebricks.core.algebra.base.ILogicalExpression in project asterixdb by apache.

the class FullTextContainsParameterCheckRule method checkFirstAndSecondParamter.

/**
     * Checks the correctness of the first and second argument. If the argument is a constant, we can check
     * it now. If the argument is not a constant, we will defer the checking until run-time.
     */
void checkFirstAndSecondParamter(List<Mutable<ILogicalExpression>> exprs, String functionName) throws AlgebricksException {
    // Check the first parameter - Expression1. If it's a constant, then we can check the type here.
    ILogicalExpression firstExpr = exprs.get(0).getValue();
    if (firstExpr.getExpressionTag() == LogicalExpressionTag.CONSTANT && ConstantExpressionUtil.getConstantIaObjectType(firstExpr) != ATypeTag.STRING) {
        throw new AlgebricksException("The first expression of " + functionName + " should be a string.");
    }
    // Check the second parameter - Expression2. If it's a constant, then we can check the type here.
    ILogicalExpression secondExpr = exprs.get(1).getValue();
    if (secondExpr.getExpressionTag() == LogicalExpressionTag.CONSTANT) {
        ATypeTag exprTypeTag = ConstantExpressionUtil.getConstantIaObjectType(secondExpr);
        switch(exprTypeTag) {
            case STRING:
            case MULTISET:
            case ARRAY:
                break;
            default:
                throw new AlgebricksException("The second expression of " + functionName + "should be a string, an unordered list, or an ordered list.");
        }
    }
}
Also used : ILogicalExpression(org.apache.hyracks.algebricks.core.algebra.base.ILogicalExpression) ATypeTag(org.apache.asterix.om.types.ATypeTag) AlgebricksException(org.apache.hyracks.algebricks.common.exceptions.AlgebricksException)

Example 44 with ILogicalExpression

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

Example 45 with ILogicalExpression

use of org.apache.hyracks.algebricks.core.algebra.base.ILogicalExpression in project asterixdb by apache.

the class FullTextContainsParameterCheckRule method checkValueForThirdParameter.

/**
     * Checks the option of the given ftcontains() function. Also, sets default value.
     *
     * @param expr
     * @throws AlgebricksException
     */
void checkValueForThirdParameter(Mutable<ILogicalExpression> expr, List<Mutable<ILogicalExpression>> newArgs) throws AlgebricksException {
    // Get the last parameter - this should be a record-constructor.
    AbstractFunctionCallExpression openRecConsExpr = (AbstractFunctionCallExpression) expr.getValue();
    FunctionIdentifier openRecConsFi = openRecConsExpr.getFunctionIdentifier();
    if (openRecConsFi != BuiltinFunctions.OPEN_RECORD_CONSTRUCTOR && openRecConsFi != BuiltinFunctions.CLOSED_RECORD_CONSTRUCTOR) {
        throw new AlgebricksException("ftcontains() option should be the form of a record { }.");
    }
    // We multiply 2 because the layout of the arguments are: [expr, val, expr1, val1, ...]
    if (openRecConsExpr.getArguments().size() > FullTextContainsDescriptor.getParamTypeMap().size() * 2) {
        throw new AlgebricksException("Too many options were specified.");
    }
    for (int i = 0; i < openRecConsExpr.getArguments().size(); i = i + 2) {
        ILogicalExpression optionExpr = openRecConsExpr.getArguments().get(i).getValue();
        ILogicalExpression optionExprVal = openRecConsExpr.getArguments().get(i + 1).getValue();
        if (optionExpr.getExpressionTag() != LogicalExpressionTag.CONSTANT) {
            throw new AlgebricksException("Options must be in the form of constant strings. Check that the option at " + (i % 2 + 1) + " is indeed a constant string");
        }
        String option = ConstantExpressionUtil.getStringArgument(openRecConsExpr, i).toLowerCase();
        if (!FullTextContainsDescriptor.getParamTypeMap().containsKey(option)) {
            throw new AlgebricksException("The given option " + option + " is not a valid argument to ftcontains()");
        }
        boolean typeError = false;
        String optionTypeStringVal = null;
        // If the option value is a constant, then we can check here.
        if (optionExprVal.getExpressionTag() == LogicalExpressionTag.CONSTANT) {
            switch(FullTextContainsDescriptor.getParamTypeMap().get(option)) {
                case STRING:
                    optionTypeStringVal = ConstantExpressionUtil.getStringArgument(openRecConsExpr, i + 1).toLowerCase();
                    if (optionTypeStringVal == null) {
                        typeError = true;
                    }
                    break;
                default:
                    // Currently, we only have a string parameter. So, the flow doesn't reach here.
                    typeError = true;
                    break;
            }
        }
        if (typeError) {
            throw new AlgebricksException("The given value for option " + option + " was not of the expected type");
        }
        // Check the validity of option value
        switch(option) {
            case FullTextContainsDescriptor.SEARCH_MODE_OPTION:
                checkSearchModeOption(optionTypeStringVal);
                break;
            default:
                break;
        }
        // Add this option as arguments to the ftcontains().
        newArgs.add(new MutableObject<ILogicalExpression>(optionExpr));
        newArgs.add(new MutableObject<ILogicalExpression>(optionExprVal));
    }
}
Also used : FunctionIdentifier(org.apache.hyracks.algebricks.core.algebra.functions.FunctionIdentifier) ILogicalExpression(org.apache.hyracks.algebricks.core.algebra.base.ILogicalExpression) AbstractFunctionCallExpression(org.apache.hyracks.algebricks.core.algebra.expressions.AbstractFunctionCallExpression) AlgebricksException(org.apache.hyracks.algebricks.common.exceptions.AlgebricksException) AString(org.apache.asterix.om.base.AString)

Aggregations

ILogicalExpression (org.apache.hyracks.algebricks.core.algebra.base.ILogicalExpression)312 LogicalVariable (org.apache.hyracks.algebricks.core.algebra.base.LogicalVariable)182 Mutable (org.apache.commons.lang3.mutable.Mutable)160 ILogicalOperator (org.apache.hyracks.algebricks.core.algebra.base.ILogicalOperator)130 ArrayList (java.util.ArrayList)125 VariableReferenceExpression (org.apache.hyracks.algebricks.core.algebra.expressions.VariableReferenceExpression)125 AbstractFunctionCallExpression (org.apache.hyracks.algebricks.core.algebra.expressions.AbstractFunctionCallExpression)121 AbstractLogicalOperator (org.apache.hyracks.algebricks.core.algebra.operators.logical.AbstractLogicalOperator)84 AssignOperator (org.apache.hyracks.algebricks.core.algebra.operators.logical.AssignOperator)75 Pair (org.apache.hyracks.algebricks.common.utils.Pair)70 ScalarFunctionCallExpression (org.apache.hyracks.algebricks.core.algebra.expressions.ScalarFunctionCallExpression)68 MutableObject (org.apache.commons.lang3.mutable.MutableObject)62 ConstantExpression (org.apache.hyracks.algebricks.core.algebra.expressions.ConstantExpression)50 IAType (org.apache.asterix.om.types.IAType)42 AlgebricksException (org.apache.hyracks.algebricks.common.exceptions.AlgebricksException)38 ILogicalPlan (org.apache.hyracks.algebricks.core.algebra.base.ILogicalPlan)36 AsterixConstantValue (org.apache.asterix.om.constants.AsterixConstantValue)34 FunctionIdentifier (org.apache.hyracks.algebricks.core.algebra.functions.FunctionIdentifier)34 HashSet (java.util.HashSet)32 AString (org.apache.asterix.om.base.AString)32