Search in sources :

Example 36 with ConstantExpression

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

the class RecordRemoveFieldsTypeComputer method getListFromExpression.

private List<String> getListFromExpression(String funcName, ILogicalExpression expression) throws AlgebricksException {
    AbstractFunctionCallExpression funcExp = (AbstractFunctionCallExpression) expression;
    List<Mutable<ILogicalExpression>> args = funcExp.getArguments();
    List<String> list = new ArrayList<>();
    for (Mutable<ILogicalExpression> arg : args) {
        // At this point all elements has to be a constant
        // Input list has only one level of nesting (list of list or list of strings)
        ConstantExpression ce = (ConstantExpression) arg.getValue();
        if (!(ce.getValue() instanceof AsterixConstantValue)) {
            throw new InvalidExpressionException(funcName, 1, ce, LogicalExpressionTag.CONSTANT);
        }
        IAObject item = ((AsterixConstantValue) ce.getValue()).getObject();
        ATypeTag type = item.getType().getTypeTag();
        if (type == ATypeTag.STRING) {
            list.add(((AString) item).getStringValue());
        } else {
            throw new UnsupportedTypeException(funcName, type);
        }
    }
    return list;
}
Also used : AbstractFunctionCallExpression(org.apache.hyracks.algebricks.core.algebra.expressions.AbstractFunctionCallExpression) ConstantExpression(org.apache.hyracks.algebricks.core.algebra.expressions.ConstantExpression) IAObject(org.apache.asterix.om.base.IAObject) ArrayList(java.util.ArrayList) AString(org.apache.asterix.om.base.AString) Mutable(org.apache.commons.lang3.mutable.Mutable) ILogicalExpression(org.apache.hyracks.algebricks.core.algebra.base.ILogicalExpression) AsterixConstantValue(org.apache.asterix.om.constants.AsterixConstantValue) ATypeTag(org.apache.asterix.om.types.ATypeTag) InvalidExpressionException(org.apache.asterix.om.exceptions.InvalidExpressionException) UnsupportedTypeException(org.apache.asterix.om.exceptions.UnsupportedTypeException)

Example 37 with ConstantExpression

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

the class DisjunctivePredicateToJoinRule method rewritePost.

@Override
public boolean rewritePost(Mutable<ILogicalOperator> opRef, IOptimizationContext context) throws AlgebricksException {
    MetadataProvider metadataProvider = (MetadataProvider) context.getMetadataProvider();
    if (metadataProvider.isBlockingOperatorDisabled()) {
        return false;
    }
    SelectOperator select;
    if ((select = asSelectOperator(opRef)) == null) {
        return false;
    }
    AbstractFunctionCallExpression condEx;
    if ((condEx = asFunctionCallExpression(select.getCondition(), AlgebricksBuiltinFunctions.OR)) == null) {
        return false;
    }
    List<Mutable<ILogicalExpression>> args = condEx.getArguments();
    VariableReferenceExpression varEx = null;
    IAType valType = null;
    HashSet<AsterixConstantValue> values = new HashSet<AsterixConstantValue>();
    for (Mutable<ILogicalExpression> arg : args) {
        AbstractFunctionCallExpression fctCall;
        if ((fctCall = asFunctionCallExpression(arg, AlgebricksBuiltinFunctions.EQ)) == null) {
            return false;
        }
        boolean haveConst = false;
        boolean haveVar = false;
        List<Mutable<ILogicalExpression>> fctArgs = fctCall.getArguments();
        for (Mutable<ILogicalExpression> fctArg : fctArgs) {
            final ILogicalExpression argExpr = fctArg.getValue();
            switch(argExpr.getExpressionTag()) {
                case CONSTANT:
                    haveConst = true;
                    AsterixConstantValue value = (AsterixConstantValue) ((ConstantExpression) argExpr).getValue();
                    if (valType == null) {
                        valType = value.getObject().getType();
                    } else if (!isCompatible(valType, value.getObject().getType())) {
                        return false;
                    }
                    values.add(value);
                    break;
                case VARIABLE:
                    haveVar = true;
                    final VariableReferenceExpression varArg = (VariableReferenceExpression) argExpr;
                    if (varEx == null) {
                        varEx = varArg;
                    } else if (!varEx.getVariableReference().equals(varArg.getVariableReference())) {
                        return false;
                    }
                    break;
                default:
                    return false;
            }
        }
        if (!(haveVar && haveConst)) {
            return false;
        }
    }
    AOrderedList list = new AOrderedList(new AOrderedListType(valType, "orderedlist"));
    for (AsterixConstantValue value : values) {
        list.add(value.getObject());
    }
    EmptyTupleSourceOperator ets = new EmptyTupleSourceOperator();
    context.computeAndSetTypeEnvironmentForOperator(ets);
    ILogicalExpression cExp = new ConstantExpression(new AsterixConstantValue(list));
    Mutable<ILogicalExpression> mutCExp = new MutableObject<>(cExp);
    IFunctionInfo scanFctInfo = BuiltinFunctions.getAsterixFunctionInfo(BuiltinFunctions.SCAN_COLLECTION);
    UnnestingFunctionCallExpression scanExp = new UnnestingFunctionCallExpression(scanFctInfo, mutCExp);
    LogicalVariable scanVar = context.newVar();
    UnnestOperator unn = new UnnestOperator(scanVar, new MutableObject<>(scanExp));
    unn.getInputs().add(new MutableObject<>(ets));
    context.computeAndSetTypeEnvironmentForOperator(unn);
    IFunctionInfo eqFctInfo = BuiltinFunctions.getAsterixFunctionInfo(AlgebricksBuiltinFunctions.EQ);
    AbstractFunctionCallExpression eqExp = new ScalarFunctionCallExpression(eqFctInfo);
    eqExp.getArguments().add(new MutableObject<>(new VariableReferenceExpression(scanVar)));
    eqExp.getArguments().add(new MutableObject<>(varEx.cloneExpression()));
    eqExp.getAnnotations().put(IndexedNLJoinExpressionAnnotation.INSTANCE, IndexedNLJoinExpressionAnnotation.INSTANCE);
    BroadcastExpressionAnnotation bcast = new BroadcastExpressionAnnotation();
    // Broadcast the OR predicates branch.
    bcast.setObject(BroadcastExpressionAnnotation.BroadcastSide.LEFT);
    eqExp.getAnnotations().put(BroadcastExpressionAnnotation.BROADCAST_ANNOTATION_KEY, bcast);
    InnerJoinOperator jOp = new InnerJoinOperator(new MutableObject<>(eqExp));
    jOp.getInputs().add(new MutableObject<>(unn));
    jOp.getInputs().add(select.getInputs().get(0));
    opRef.setValue(jOp);
    context.computeAndSetTypeEnvironmentForOperator(jOp);
    return true;
}
Also used : IFunctionInfo(org.apache.hyracks.algebricks.core.algebra.functions.IFunctionInfo) ConstantExpression(org.apache.hyracks.algebricks.core.algebra.expressions.ConstantExpression) InnerJoinOperator(org.apache.hyracks.algebricks.core.algebra.operators.logical.InnerJoinOperator) UnnestOperator(org.apache.hyracks.algebricks.core.algebra.operators.logical.UnnestOperator) SelectOperator(org.apache.hyracks.algebricks.core.algebra.operators.logical.SelectOperator) AOrderedList(org.apache.asterix.om.base.AOrderedList) AsterixConstantValue(org.apache.asterix.om.constants.AsterixConstantValue) BroadcastExpressionAnnotation(org.apache.hyracks.algebricks.core.algebra.expressions.BroadcastExpressionAnnotation) EmptyTupleSourceOperator(org.apache.hyracks.algebricks.core.algebra.operators.logical.EmptyTupleSourceOperator) HashSet(java.util.HashSet) MutableObject(org.apache.commons.lang3.mutable.MutableObject) ScalarFunctionCallExpression(org.apache.hyracks.algebricks.core.algebra.expressions.ScalarFunctionCallExpression) LogicalVariable(org.apache.hyracks.algebricks.core.algebra.base.LogicalVariable) UnnestingFunctionCallExpression(org.apache.hyracks.algebricks.core.algebra.expressions.UnnestingFunctionCallExpression) AbstractFunctionCallExpression(org.apache.hyracks.algebricks.core.algebra.expressions.AbstractFunctionCallExpression) AOrderedListType(org.apache.asterix.om.types.AOrderedListType) Mutable(org.apache.commons.lang3.mutable.Mutable) ILogicalExpression(org.apache.hyracks.algebricks.core.algebra.base.ILogicalExpression) MetadataProvider(org.apache.asterix.metadata.declared.MetadataProvider) VariableReferenceExpression(org.apache.hyracks.algebricks.core.algebra.expressions.VariableReferenceExpression) IAType(org.apache.asterix.om.types.IAType)

Example 38 with ConstantExpression

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

the class FullTextContainsParameterCheckRule method setDefaultValueForThirdParameter.

/**
     * Sets the default option value(s) when a user doesn't provide any option.
     */
void setDefaultValueForThirdParameter(List<Mutable<ILogicalExpression>> newArgs) throws AlgebricksException {
    // Sets the search mode option: the default option is conjunctive search.
    ILogicalExpression searchModeOptionExpr = new ConstantExpression(new AsterixConstantValue(new AString(FullTextContainsDescriptor.SEARCH_MODE_OPTION)));
    ILogicalExpression searchModeValExpr = new ConstantExpression(new AsterixConstantValue(new AString(FullTextContainsDescriptor.CONJUNCTIVE_SEARCH_MODE_OPTION)));
    // Add this option as arguments to the ftcontains().
    newArgs.add(new MutableObject<ILogicalExpression>(searchModeOptionExpr));
    newArgs.add(new MutableObject<ILogicalExpression>(searchModeValExpr));
}
Also used : ILogicalExpression(org.apache.hyracks.algebricks.core.algebra.base.ILogicalExpression) AsterixConstantValue(org.apache.asterix.om.constants.AsterixConstantValue) ConstantExpression(org.apache.hyracks.algebricks.core.algebra.expressions.ConstantExpression) AString(org.apache.asterix.om.base.AString)

Example 39 with ConstantExpression

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

the class FuzzyEqRule method expandFuzzyEq.

private boolean expandFuzzyEq(Mutable<ILogicalExpression> expRef, IOptimizationContext context, IVariableTypeEnvironment env, MetadataProvider metadataProvider) throws AlgebricksException {
    ILogicalExpression exp = expRef.getValue();
    if (exp.getExpressionTag() != LogicalExpressionTag.FUNCTION_CALL) {
        return false;
    }
    boolean expanded = false;
    AbstractFunctionCallExpression funcExp = (AbstractFunctionCallExpression) exp;
    FunctionIdentifier fi = funcExp.getFunctionIdentifier();
    if (fi.equals(BuiltinFunctions.FUZZY_EQ)) {
        List<Mutable<ILogicalExpression>> inputExps = funcExp.getArguments();
        String simFuncName = FuzzyUtils.getSimFunction(metadataProvider);
        ArrayList<Mutable<ILogicalExpression>> similarityArgs = new ArrayList<Mutable<ILogicalExpression>>();
        for (int i = 0; i < inputExps.size(); ++i) {
            Mutable<ILogicalExpression> inputExpRef = inputExps.get(i);
            similarityArgs.add(inputExpRef);
        }
        FunctionIdentifier simFunctionIdentifier = FuzzyUtils.getFunctionIdentifier(simFuncName);
        ScalarFunctionCallExpression similarityExp = new ScalarFunctionCallExpression(FunctionUtil.getFunctionInfo(simFunctionIdentifier), similarityArgs);
        // Add annotations from the original fuzzy-eq function.
        similarityExp.getAnnotations().putAll(funcExp.getAnnotations());
        ArrayList<Mutable<ILogicalExpression>> cmpArgs = new ArrayList<Mutable<ILogicalExpression>>();
        cmpArgs.add(new MutableObject<ILogicalExpression>(similarityExp));
        IAObject simThreshold = FuzzyUtils.getSimThreshold(metadataProvider, simFuncName);
        cmpArgs.add(new MutableObject<ILogicalExpression>(new ConstantExpression(new AsterixConstantValue(simThreshold))));
        ScalarFunctionCallExpression cmpExpr = FuzzyUtils.getComparisonExpr(simFuncName, cmpArgs);
        expRef.setValue(cmpExpr);
        return true;
    } else if (fi.equals(AlgebricksBuiltinFunctions.AND) || fi.equals(AlgebricksBuiltinFunctions.OR)) {
        for (int i = 0; i < 2; i++) {
            if (expandFuzzyEq(funcExp.getArguments().get(i), context, env, metadataProvider)) {
                expanded = true;
            }
        }
    }
    return expanded;
}
Also used : AbstractFunctionCallExpression(org.apache.hyracks.algebricks.core.algebra.expressions.AbstractFunctionCallExpression) IAObject(org.apache.asterix.om.base.IAObject) ConstantExpression(org.apache.hyracks.algebricks.core.algebra.expressions.ConstantExpression) ArrayList(java.util.ArrayList) 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) AsterixConstantValue(org.apache.asterix.om.constants.AsterixConstantValue) ScalarFunctionCallExpression(org.apache.hyracks.algebricks.core.algebra.expressions.ScalarFunctionCallExpression)

Example 40 with ConstantExpression

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

the class AddEquivalenceClassForRecordConstructorRule method propagateEquivalenceClassesForRecordConstructor.

@SuppressWarnings("unchecked")
private boolean propagateEquivalenceClassesForRecordConstructor(LogicalVariable recordVar, AbstractFunctionCallExpression funcExpr, AssignOperator assignOp, IOptimizationContext context) {
    List<Mutable<ILogicalExpression>> argRefs = funcExpr.getArguments();
    boolean changed = false;
    // Only odd position arguments are field value expressions.
    for (int parameterIndex = 1; parameterIndex < argRefs.size(); parameterIndex += 2) {
        ILogicalExpression fieldExpr = argRefs.get(parameterIndex).getValue();
        // Adds equivalent classes if a field is from a variable reference.
        if (fieldExpr.getExpressionTag() == LogicalExpressionTag.VARIABLE) {
            VariableReferenceExpression varExpr = (VariableReferenceExpression) fieldExpr;
            LogicalVariable fieldVar = varExpr.getVariableReference();
            Map<LogicalVariable, EquivalenceClass> ecs = context.getEquivalenceClassMap(assignOp);
            if (ecs == null) {
                ecs = new HashMap<LogicalVariable, EquivalenceClass>();
                context.putEquivalenceClassMap(assignOp, ecs);
            }
            ILogicalExpression expr = new ScalarFunctionCallExpression(FunctionUtil.getFunctionInfo(BuiltinFunctions.FIELD_ACCESS_BY_INDEX), new MutableObject<ILogicalExpression>(new VariableReferenceExpression(recordVar)), new MutableObject<ILogicalExpression>(// Every two parameters corresponds to a field.
            new ConstantExpression(new AsterixConstantValue(new AInt32(parameterIndex / 2)))));
            EquivalenceClass equivClass = new EquivalenceClass(Collections.singletonList(fieldVar), fieldVar, Collections.singletonList(expr));
            ecs.put(fieldVar, equivClass);
            changed = true;
        }
    }
    return changed;
}
Also used : LogicalVariable(org.apache.hyracks.algebricks.core.algebra.base.LogicalVariable) ConstantExpression(org.apache.hyracks.algebricks.core.algebra.expressions.ConstantExpression) AInt32(org.apache.asterix.om.base.AInt32) Mutable(org.apache.commons.lang3.mutable.Mutable) ILogicalExpression(org.apache.hyracks.algebricks.core.algebra.base.ILogicalExpression) AsterixConstantValue(org.apache.asterix.om.constants.AsterixConstantValue) VariableReferenceExpression(org.apache.hyracks.algebricks.core.algebra.expressions.VariableReferenceExpression) EquivalenceClass(org.apache.hyracks.algebricks.core.algebra.base.EquivalenceClass) ScalarFunctionCallExpression(org.apache.hyracks.algebricks.core.algebra.expressions.ScalarFunctionCallExpression)

Aggregations

ConstantExpression (org.apache.hyracks.algebricks.core.algebra.expressions.ConstantExpression)43 AsterixConstantValue (org.apache.asterix.om.constants.AsterixConstantValue)41 ILogicalExpression (org.apache.hyracks.algebricks.core.algebra.base.ILogicalExpression)36 ScalarFunctionCallExpression (org.apache.hyracks.algebricks.core.algebra.expressions.ScalarFunctionCallExpression)23 ArrayList (java.util.ArrayList)22 AString (org.apache.asterix.om.base.AString)22 Mutable (org.apache.commons.lang3.mutable.Mutable)22 LogicalVariable (org.apache.hyracks.algebricks.core.algebra.base.LogicalVariable)21 AbstractFunctionCallExpression (org.apache.hyracks.algebricks.core.algebra.expressions.AbstractFunctionCallExpression)20 VariableReferenceExpression (org.apache.hyracks.algebricks.core.algebra.expressions.VariableReferenceExpression)19 AssignOperator (org.apache.hyracks.algebricks.core.algebra.operators.logical.AssignOperator)15 AInt32 (org.apache.asterix.om.base.AInt32)14 MutableObject (org.apache.commons.lang3.mutable.MutableObject)13 IAObject (org.apache.asterix.om.base.IAObject)11 AlgebricksException (org.apache.hyracks.algebricks.common.exceptions.AlgebricksException)10 ILogicalOperator (org.apache.hyracks.algebricks.core.algebra.base.ILogicalOperator)10 AOrderedList (org.apache.asterix.om.base.AOrderedList)9 IAType (org.apache.asterix.om.types.IAType)9 Pair (org.apache.hyracks.algebricks.common.utils.Pair)9 ARecordType (org.apache.asterix.om.types.ARecordType)7