Search in sources :

Example 6 with ConstantExpression

use of org.apache.hyracks.algebricks.core.algebra.expressions.ConstantExpression 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 7 with ConstantExpression

use of org.apache.hyracks.algebricks.core.algebra.expressions.ConstantExpression 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 8 with ConstantExpression

use of org.apache.hyracks.algebricks.core.algebra.expressions.ConstantExpression 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 9 with ConstantExpression

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

Example 10 with ConstantExpression

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

the class SimilarityCheckRule method replaceWithFunctionCallArg.

private boolean replaceWithFunctionCallArg(Mutable<ILogicalExpression> expRef, FunctionIdentifier normFuncIdent, AsterixConstantValue constVal, AbstractFunctionCallExpression funcExpr) throws AlgebricksException {
    // Analyze func expr to see if it is an optimizable similarity function.
    ScalarFunctionCallExpression simCheckFuncExpr = getSimilarityCheckExpr(normFuncIdent, constVal, funcExpr);
    // Replace the expr in the select condition.
    if (simCheckFuncExpr != null) {
        // Get item 0 from var.
        List<Mutable<ILogicalExpression>> getItemArgs = new ArrayList<Mutable<ILogicalExpression>>();
        // First arg is the similarity-check function call.
        getItemArgs.add(new MutableObject<ILogicalExpression>(simCheckFuncExpr));
        // Second arg is the item index to be accessed.
        getItemArgs.add(new MutableObject<ILogicalExpression>(new ConstantExpression(new AsterixConstantValue(new AInt32(0)))));
        ILogicalExpression getItemExpr = new ScalarFunctionCallExpression(FunctionUtil.getFunctionInfo(BuiltinFunctions.GET_ITEM), getItemArgs);
        // Replace the old similarity function call with the new getItemExpr.
        expRef.setValue(getItemExpr);
        return true;
    }
    return false;
}
Also used : Mutable(org.apache.commons.lang3.mutable.Mutable) 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) ArrayList(java.util.ArrayList) AInt32(org.apache.asterix.om.base.AInt32) 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