Search in sources :

Example 46 with AsterixConstantValue

use of org.apache.asterix.om.constants.AsterixConstantValue 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 47 with AsterixConstantValue

use of org.apache.asterix.om.constants.AsterixConstantValue 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)

Example 48 with AsterixConstantValue

use of org.apache.asterix.om.constants.AsterixConstantValue in project asterixdb by apache.

the class ByNameToByIndexFieldAccessRule method createFieldAccessByIndex.

@SuppressWarnings("unchecked")
private static ILogicalExpression createFieldAccessByIndex(ARecordType recType, AbstractFunctionCallExpression fce) {
    String s = ConstantExpressionUtil.getStringArgument(fce, 1);
    if (s == null) {
        return null;
    }
    int k = recType.getFieldIndex(s);
    if (k < 0) {
        return null;
    }
    return new ScalarFunctionCallExpression(FunctionUtil.getFunctionInfo(BuiltinFunctions.FIELD_ACCESS_BY_INDEX), fce.getArguments().get(0), new MutableObject<>(new ConstantExpression(new AsterixConstantValue(new AInt32(k)))));
}
Also used : AsterixConstantValue(org.apache.asterix.om.constants.AsterixConstantValue) ConstantExpression(org.apache.hyracks.algebricks.core.algebra.expressions.ConstantExpression) AInt32(org.apache.asterix.om.base.AInt32) ScalarFunctionCallExpression(org.apache.hyracks.algebricks.core.algebra.expressions.ScalarFunctionCallExpression)

Example 49 with AsterixConstantValue

use of org.apache.asterix.om.constants.AsterixConstantValue in project asterixdb by apache.

the class NonTaggedDataFormat method getExpressionEvalSizeComputer.

@Override
public IExpressionEvalSizeComputer getExpressionEvalSizeComputer() {
    return new IExpressionEvalSizeComputer() {

        @Override
        public int getEvalSize(ILogicalExpression expr, IVariableEvalSizeEnvironment env) throws AlgebricksException {
            switch(expr.getExpressionTag()) {
                case CONSTANT:
                    {
                        ConstantExpression c = (ConstantExpression) expr;
                        if (c == ConstantExpression.MISSING) {
                            return 1;
                        } else if (c == ConstantExpression.FALSE || c == ConstantExpression.TRUE) {
                            return 2;
                        } else {
                            AsterixConstantValue acv = (AsterixConstantValue) c.getValue();
                            IAObject o = acv.getObject();
                            switch(o.getType().getTypeTag()) {
                                case DOUBLE:
                                    return 9;
                                case FLOAT:
                                    return 5;
                                case BOOLEAN:
                                    return 2;
                                case MISSING:
                                    return 1;
                                case NULL:
                                    return 1;
                                case TINYINT:
                                    return 2;
                                case SMALLINT:
                                    return 3;
                                case INTEGER:
                                    return 5;
                                case BIGINT:
                                    return 9;
                                default:
                                    return -1;
                            }
                        }
                    }
                case FUNCTION_CALL:
                    {
                        AbstractFunctionCallExpression f = (AbstractFunctionCallExpression) expr;
                        if (f.getFunctionIdentifier().equals(BuiltinFunctions.TID)) {
                            return 5;
                        } else {
                            // TODO
                            return -1;
                        }
                    }
                default:
                    {
                        // TODO
                        return -1;
                    }
            }
        }
    };
}
Also used : IVariableEvalSizeEnvironment(org.apache.hyracks.algebricks.core.algebra.expressions.IVariableEvalSizeEnvironment) 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) IAObject(org.apache.asterix.om.base.IAObject) AbstractFunctionCallExpression(org.apache.hyracks.algebricks.core.algebra.expressions.AbstractFunctionCallExpression) IExpressionEvalSizeComputer(org.apache.hyracks.algebricks.core.algebra.expressions.IExpressionEvalSizeComputer)

Aggregations

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