Search in sources :

Example 6 with AsterixConstantValue

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

the class AccessMethodJobGenParams method writeVarList.

protected void writeVarList(List<LogicalVariable> varList, List<Mutable<ILogicalExpression>> funcArgs) {
    Mutable<ILogicalExpression> numKeysRef = new MutableObject<>(new ConstantExpression(new AsterixConstantValue(new AInt32(varList.size()))));
    funcArgs.add(numKeysRef);
    for (LogicalVariable keyVar : varList) {
        Mutable<ILogicalExpression> keyVarRef = new MutableObject<>(new VariableReferenceExpression(keyVar));
        funcArgs.add(keyVarRef);
    }
}
Also used : LogicalVariable(org.apache.hyracks.algebricks.core.algebra.base.LogicalVariable) 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) ConstantExpression(org.apache.hyracks.algebricks.core.algebra.expressions.ConstantExpression) AInt32(org.apache.asterix.om.base.AInt32) MutableObject(org.apache.commons.lang3.mutable.MutableObject)

Example 7 with AsterixConstantValue

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

the class AccessMethodUtils method createSearchKeyExpr.

/**
     * Returns the search key expression which feeds a secondary-index search. If we are optimizing a selection query then this method returns
     * the a ConstantExpression from the first constant value in the optimizable function expression.
     * If we are optimizing a join, then this method returns the VariableReferenceExpression that should feed the secondary index probe.
     *
     * @throws AlgebricksException
     */
public static Pair<ILogicalExpression, Boolean> createSearchKeyExpr(IOptimizableFuncExpr optFuncExpr, OptimizableOperatorSubTree indexSubTree, OptimizableOperatorSubTree probeSubTree) throws AlgebricksException {
    if (probeSubTree == null) {
        // We are optimizing a selection query. Search key is a constant.
        // Type Checking and type promotion is done here
        IAType fieldType = optFuncExpr.getFieldType(0);
        if (optFuncExpr.getNumConstantExpr() == 0) {
            //TODO: Right now we miss on type promotion for nonpure functions
            return new Pair<>(new VariableReferenceExpression(optFuncExpr.getLogicalVar(1)), false);
        }
        ILogicalExpression constantAtRuntimeExpression = null;
        AsterixConstantValue constantValue = null;
        ATypeTag constantValueTag = null;
        constantAtRuntimeExpression = optFuncExpr.getConstantExpr(0);
        if (constantAtRuntimeExpression.getExpressionTag() == LogicalExpressionTag.CONSTANT) {
            constantValue = (AsterixConstantValue) ((ConstantExpression) constantAtRuntimeExpression).getValue();
        }
        constantValueTag = optFuncExpr.getConstantType(0).getTypeTag();
        // type casting applied?
        boolean typeCastingApplied = false;
        // type casting happened from real (FLOAT, DOUBLE) value -> INT value?
        boolean realTypeConvertedToIntegerType = false;
        AsterixConstantValue replacedConstantValue = null;
        // if the constant type and target type does not match, we do a type conversion
        if (constantValueTag != fieldType.getTypeTag() && constantValue != null) {
            try {
                replacedConstantValue = ATypeHierarchy.getAsterixConstantValueFromNumericTypeObject(constantValue.getObject(), fieldType.getTypeTag(), true);
            } catch (HyracksDataException e) {
                throw new AlgebricksException(e);
            }
            if (replacedConstantValue != null) {
                typeCastingApplied = true;
            }
            // In this case, we need to change the search parameter. Refer to the caller section for the detail.
            switch(constantValueTag) {
                case DOUBLE:
                case FLOAT:
                    switch(fieldType.getTypeTag()) {
                        case TINYINT:
                        case SMALLINT:
                        case INTEGER:
                        case BIGINT:
                            realTypeConvertedToIntegerType = true;
                            break;
                        default:
                            break;
                    }
                default:
                    break;
            }
        }
        if (typeCastingApplied) {
            return new Pair<>(new ConstantExpression(replacedConstantValue), realTypeConvertedToIntegerType);
        } else {
            return new Pair<>(optFuncExpr.getConstantExpr(0), false);
        }
    } else {
        // We are optimizing a join query. Determine which variable feeds the secondary index.
        if (optFuncExpr.getOperatorSubTree(0) == null || optFuncExpr.getOperatorSubTree(0) == probeSubTree) {
            return new Pair<>(new VariableReferenceExpression(optFuncExpr.getLogicalVar(0)), false);
        } else {
            return new Pair<>(new VariableReferenceExpression(optFuncExpr.getLogicalVar(1)), false);
        }
    }
}
Also used : ILogicalExpression(org.apache.hyracks.algebricks.core.algebra.base.ILogicalExpression) AsterixConstantValue(org.apache.asterix.om.constants.AsterixConstantValue) ATypeTag(org.apache.asterix.om.types.ATypeTag) VariableReferenceExpression(org.apache.hyracks.algebricks.core.algebra.expressions.VariableReferenceExpression) ConstantExpression(org.apache.hyracks.algebricks.core.algebra.expressions.ConstantExpression) AlgebricksException(org.apache.hyracks.algebricks.common.exceptions.AlgebricksException) HyracksDataException(org.apache.hyracks.api.exceptions.HyracksDataException) IAType(org.apache.asterix.om.types.IAType) Pair(org.apache.hyracks.algebricks.common.utils.Pair)

Example 8 with AsterixConstantValue

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

the class AccessMethodUtils method writeVarList.

private static void writeVarList(List<LogicalVariable> varList, List<Mutable<ILogicalExpression>> funcArgs) {
    Mutable<ILogicalExpression> numKeysRef = new MutableObject<>(new ConstantExpression(new AsterixConstantValue(new AInt32(varList.size()))));
    funcArgs.add(numKeysRef);
    for (LogicalVariable keyVar : varList) {
        Mutable<ILogicalExpression> keyVarRef = new MutableObject<>(new VariableReferenceExpression(keyVar));
        funcArgs.add(keyVarRef);
    }
}
Also used : LogicalVariable(org.apache.hyracks.algebricks.core.algebra.base.LogicalVariable) 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) ConstantExpression(org.apache.hyracks.algebricks.core.algebra.expressions.ConstantExpression) AInt32(org.apache.asterix.om.base.AInt32) MutableObject(org.apache.commons.lang3.mutable.MutableObject)

Example 9 with AsterixConstantValue

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

the class SimilarityCheckRule method getSimilarityCheckExpr.

private ScalarFunctionCallExpression getSimilarityCheckExpr(FunctionIdentifier normFuncIdent, AsterixConstantValue constVal, AbstractFunctionCallExpression funcExpr) throws AlgebricksException {
    // Remember args from original similarity function to add them to the similarity-check function later.
    ArrayList<Mutable<ILogicalExpression>> similarityArgs = null;
    ScalarFunctionCallExpression simCheckFuncExpr = null;
    // Look for jaccard function call, and GE or GT.
    if (funcExpr.getFunctionIdentifier() == BuiltinFunctions.SIMILARITY_JACCARD) {
        IAObject jaccThresh;
        if (normFuncIdent == AlgebricksBuiltinFunctions.GE) {
            if (constVal.getObject() instanceof AFloat) {
                jaccThresh = constVal.getObject();
            } else {
                jaccThresh = new AFloat((float) ((ADouble) constVal.getObject()).getDoubleValue());
            }
        } else if (normFuncIdent == AlgebricksBuiltinFunctions.GT) {
            float threshVal = 0.0f;
            if (constVal.getObject() instanceof AFloat) {
                threshVal = ((AFloat) constVal.getObject()).getFloatValue();
            } else {
                threshVal = (float) ((ADouble) constVal.getObject()).getDoubleValue();
            }
            float f = threshVal + Float.MIN_VALUE;
            if (f > 1.0f)
                f = 1.0f;
            jaccThresh = new AFloat(f);
        } else {
            return null;
        }
        similarityArgs = new ArrayList<Mutable<ILogicalExpression>>();
        similarityArgs.addAll(funcExpr.getArguments());
        similarityArgs.add(new MutableObject<ILogicalExpression>(new ConstantExpression(new AsterixConstantValue(jaccThresh))));
        simCheckFuncExpr = new ScalarFunctionCallExpression(FunctionUtil.getFunctionInfo(BuiltinFunctions.SIMILARITY_JACCARD_CHECK), similarityArgs);
    }
    // Look for edit-distance function call, and LE or LT.
    if (funcExpr.getFunctionIdentifier() == BuiltinFunctions.EDIT_DISTANCE) {
        AInt32 aInt = new AInt32(0);
        try {
            aInt = (AInt32) ATypeHierarchy.convertNumericTypeObject(constVal.getObject(), ATypeTag.INTEGER);
        } catch (HyracksDataException e) {
            throw new AlgebricksException(e);
        }
        AInt32 edThresh;
        if (normFuncIdent == AlgebricksBuiltinFunctions.LE) {
            edThresh = aInt;
        } else if (normFuncIdent == AlgebricksBuiltinFunctions.LT) {
            int ed = aInt.getIntegerValue() - 1;
            if (ed < 0)
                ed = 0;
            edThresh = new AInt32(ed);
        } else {
            return null;
        }
        similarityArgs = new ArrayList<Mutable<ILogicalExpression>>();
        similarityArgs.addAll(funcExpr.getArguments());
        similarityArgs.add(new MutableObject<ILogicalExpression>(new ConstantExpression(new AsterixConstantValue(edThresh))));
        simCheckFuncExpr = new ScalarFunctionCallExpression(FunctionUtil.getFunctionInfo(BuiltinFunctions.EDIT_DISTANCE_CHECK), similarityArgs);
    }
    // Preserve all annotations.
    if (simCheckFuncExpr != null) {
        simCheckFuncExpr.getAnnotations().putAll(funcExpr.getAnnotations());
    }
    return simCheckFuncExpr;
}
Also used : Mutable(org.apache.commons.lang3.mutable.Mutable) ILogicalExpression(org.apache.hyracks.algebricks.core.algebra.base.ILogicalExpression) AFloat(org.apache.asterix.om.base.AFloat) AsterixConstantValue(org.apache.asterix.om.constants.AsterixConstantValue) IAObject(org.apache.asterix.om.base.IAObject) ConstantExpression(org.apache.hyracks.algebricks.core.algebra.expressions.ConstantExpression) AlgebricksException(org.apache.hyracks.algebricks.common.exceptions.AlgebricksException) AInt32(org.apache.asterix.om.base.AInt32) HyracksDataException(org.apache.hyracks.api.exceptions.HyracksDataException) ScalarFunctionCallExpression(org.apache.hyracks.algebricks.core.algebra.expressions.ScalarFunctionCallExpression)

Example 10 with AsterixConstantValue

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

the class SimilarityCheckRule method replaceSelectConditionExprs.

private boolean replaceSelectConditionExprs(Mutable<ILogicalExpression> expRef, List<AssignOperator> assigns, IOptimizationContext context) throws AlgebricksException {
    ILogicalExpression expr = expRef.getValue();
    if (expr.getExpressionTag() != LogicalExpressionTag.FUNCTION_CALL) {
        return false;
    }
    AbstractFunctionCallExpression funcExpr = (AbstractFunctionCallExpression) expr;
    FunctionIdentifier funcIdent = funcExpr.getFunctionIdentifier();
    // then we may still need the true similarity value even if the GE/GT/LE/LE comparison returns false.
    if (funcIdent == AlgebricksBuiltinFunctions.AND) {
        boolean found = true;
        for (int i = 0; i < funcExpr.getArguments().size(); ++i) {
            found = found && replaceSelectConditionExprs(funcExpr.getArguments().get(i), assigns, context);
        }
        return found;
    }
    // Look for GE/GT/LE/LT.
    if (funcIdent != AlgebricksBuiltinFunctions.GE && funcIdent != AlgebricksBuiltinFunctions.GT && funcIdent != AlgebricksBuiltinFunctions.LE && funcIdent != AlgebricksBuiltinFunctions.LT) {
        return false;
    }
    // One arg should be a function call or a variable, the other a constant.
    AsterixConstantValue constVal = null;
    ILogicalExpression nonConstExpr = null;
    ILogicalExpression arg1 = funcExpr.getArguments().get(0).getValue();
    ILogicalExpression arg2 = funcExpr.getArguments().get(1).getValue();
    // Normalized GE/GT/LE/LT as if constant was on the right hand side.
    FunctionIdentifier normFuncIdent = null;
    // One of the args must be a constant.
    if (arg1.getExpressionTag() == LogicalExpressionTag.CONSTANT) {
        ConstantExpression constExpr = (ConstantExpression) arg1;
        constVal = (AsterixConstantValue) constExpr.getValue();
        nonConstExpr = arg2;
        // Get func ident as if swapping lhs and rhs.
        normFuncIdent = getLhsAndRhsSwappedFuncIdent(funcIdent);
    } else if (arg2.getExpressionTag() == LogicalExpressionTag.CONSTANT) {
        ConstantExpression constExpr = (ConstantExpression) arg2;
        constVal = (AsterixConstantValue) constExpr.getValue();
        nonConstExpr = arg1;
        // Constant is already on rhs, so nothing to be done for normalizedFuncIdent.
        normFuncIdent = funcIdent;
    } else {
        return false;
    }
    // The other arg is a function call. We can directly replace the select condition with an equivalent similarity check expression.
    if (nonConstExpr.getExpressionTag() == LogicalExpressionTag.FUNCTION_CALL) {
        return replaceWithFunctionCallArg(expRef, normFuncIdent, constVal, (AbstractFunctionCallExpression) nonConstExpr);
    }
    // The other arg ist a variable. We may have to introduce an assign operator that assigns the result of a similarity-check function to a variable.
    if (nonConstExpr.getExpressionTag() == LogicalExpressionTag.VARIABLE) {
        return replaceWithVariableArg(expRef, normFuncIdent, constVal, (VariableReferenceExpression) nonConstExpr, assigns, context);
    }
    return false;
}
Also used : FunctionIdentifier(org.apache.hyracks.algebricks.core.algebra.functions.FunctionIdentifier) ILogicalExpression(org.apache.hyracks.algebricks.core.algebra.base.ILogicalExpression) AsterixConstantValue(org.apache.asterix.om.constants.AsterixConstantValue) AbstractFunctionCallExpression(org.apache.hyracks.algebricks.core.algebra.expressions.AbstractFunctionCallExpression) ConstantExpression(org.apache.hyracks.algebricks.core.algebra.expressions.ConstantExpression)

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