Search in sources :

Example 6 with AInt32

use of org.apache.asterix.om.base.AInt32 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 AInt32

use of org.apache.asterix.om.base.AInt32 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 AInt32

use of org.apache.asterix.om.base.AInt32 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 AInt32

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

Example 10 with AInt32

use of org.apache.asterix.om.base.AInt32 in project asterixdb by apache.

the class LoadRecordFieldsRule method findAndEliminateRedundantFieldAccess.

/**
     * Rewrite
     * assign $x := field-access($y, "field")
     * assign $y := record-constructor { "field": Expr, ... }
     * into
     * assign $x := Expr
     * assign $y := record-constructor { "field": Expr, ... }
     */
private static boolean findAndEliminateRedundantFieldAccess(AssignOperator assign, IOptimizationContext context) throws AlgebricksException {
    ILogicalExpression expr = getFirstExpr(assign);
    AbstractFunctionCallExpression f = (AbstractFunctionCallExpression) expr;
    ILogicalExpression arg0 = f.getArguments().get(0).getValue();
    if (arg0.getExpressionTag() != LogicalExpressionTag.VARIABLE) {
        return false;
    }
    VariableReferenceExpression vre = (VariableReferenceExpression) arg0;
    LogicalVariable recordVar = vre.getVariableReference();
    ILogicalExpression arg1 = f.getArguments().get(1).getValue();
    if (arg1.getExpressionTag() != LogicalExpressionTag.CONSTANT) {
        return false;
    }
    IVariableTypeEnvironment typeEnvironment = context.getOutputTypeEnvironment(assign);
    ConstantExpression ce = (ConstantExpression) arg1;
    ILogicalExpression fldExpr;
    if (f.getFunctionIdentifier().equals(BuiltinFunctions.FIELD_ACCESS_BY_NAME)) {
        String fldName = ((AString) ((AsterixConstantValue) ce.getValue()).getObject()).getStringValue();
        fldExpr = findFieldExpression(assign, recordVar, fldName, typeEnvironment, (name, expression, env) -> findFieldByNameFromRecordConstructor(name, expression));
    } else if (f.getFunctionIdentifier().equals(BuiltinFunctions.FIELD_ACCESS_BY_INDEX)) {
        Integer fldIdx = ((AInt32) ((AsterixConstantValue) ce.getValue()).getObject()).getIntegerValue();
        fldExpr = findFieldExpression(assign, recordVar, fldIdx, typeEnvironment, LoadRecordFieldsRule::findFieldByIndexFromRecordConstructor);
    } else if (f.getFunctionIdentifier().equals(BuiltinFunctions.FIELD_ACCESS_NESTED)) {
        return false;
    } else {
        throw new IllegalStateException();
    }
    if (fldExpr == null) {
        return false;
    }
    // check the liveness of the new expression
    List<LogicalVariable> usedVariables = new ArrayList<>();
    fldExpr.getUsedVariables(usedVariables);
    List<LogicalVariable> liveInputVars = new ArrayList<>();
    VariableUtilities.getLiveVariables(assign, liveInputVars);
    usedVariables.removeAll(liveInputVars);
    if (usedVariables.isEmpty()) {
        assign.getExpressions().get(0).setValue(fldExpr);
        return true;
    } else {
        return false;
    }
}
Also used : LogicalVariable(org.apache.hyracks.algebricks.core.algebra.base.LogicalVariable) OperatorPropertiesUtil(org.apache.hyracks.algebricks.core.algebra.util.OperatorPropertiesUtil) IOptimizationContext(org.apache.hyracks.algebricks.core.algebra.base.IOptimizationContext) NestedTupleSourceOperator(org.apache.hyracks.algebricks.core.algebra.operators.logical.NestedTupleSourceOperator) AbstractOperatorWithNestedPlans(org.apache.hyracks.algebricks.core.algebra.operators.logical.AbstractOperatorWithNestedPlans) ConstantExpressionUtil(org.apache.asterix.om.utils.ConstantExpressionUtil) LogicalExpressionTag(org.apache.hyracks.algebricks.core.algebra.base.LogicalExpressionTag) AbstractLogicalExpression(org.apache.hyracks.algebricks.core.algebra.expressions.AbstractLogicalExpression) VariableUtilities(org.apache.hyracks.algebricks.core.algebra.operators.logical.visitors.VariableUtilities) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) ILogicalExpressionReferenceTransform(org.apache.hyracks.algebricks.core.algebra.visitors.ILogicalExpressionReferenceTransform) OperatorAnnotation(org.apache.asterix.algebra.base.OperatorAnnotation) ARecordType(org.apache.asterix.om.types.ARecordType) AbstractFunctionCallExpression(org.apache.hyracks.algebricks.core.algebra.expressions.AbstractFunctionCallExpression) AlgebricksBuiltinFunctions(org.apache.hyracks.algebricks.core.algebra.functions.AlgebricksBuiltinFunctions) ILogicalPlan(org.apache.hyracks.algebricks.core.algebra.base.ILogicalPlan) IAlgebraicRewriteRule(org.apache.hyracks.algebricks.core.rewriter.base.IAlgebraicRewriteRule) FunctionalDependency(org.apache.hyracks.algebricks.core.algebra.properties.FunctionalDependency) LinkedList(java.util.LinkedList) BuiltinFunctions(org.apache.asterix.om.functions.BuiltinFunctions) MutableObject(org.apache.commons.lang3.mutable.MutableObject) LogicalVariable(org.apache.hyracks.algebricks.core.algebra.base.LogicalVariable) AString(org.apache.asterix.om.base.AString) Iterator(java.util.Iterator) AsterixConstantValue(org.apache.asterix.om.constants.AsterixConstantValue) AlgebricksException(org.apache.hyracks.algebricks.common.exceptions.AlgebricksException) AssignOperator(org.apache.hyracks.algebricks.core.algebra.operators.logical.AssignOperator) LogicalOperatorTag(org.apache.hyracks.algebricks.core.algebra.base.LogicalOperatorTag) ConstantExpression(org.apache.hyracks.algebricks.core.algebra.expressions.ConstantExpression) ILogicalOperator(org.apache.hyracks.algebricks.core.algebra.base.ILogicalOperator) AInt32(org.apache.asterix.om.base.AInt32) ILogicalExpression(org.apache.hyracks.algebricks.core.algebra.base.ILogicalExpression) List(java.util.List) AnalysisUtil(org.apache.asterix.optimizer.base.AnalysisUtil) IVariableTypeEnvironment(org.apache.hyracks.algebricks.core.algebra.expressions.IVariableTypeEnvironment) AbstractLogicalOperator(org.apache.hyracks.algebricks.core.algebra.operators.logical.AbstractLogicalOperator) SelectOperator(org.apache.hyracks.algebricks.core.algebra.operators.logical.SelectOperator) Mutable(org.apache.commons.lang3.mutable.Mutable) VariableReferenceExpression(org.apache.hyracks.algebricks.core.algebra.expressions.VariableReferenceExpression) FunctionIdentifier(org.apache.hyracks.algebricks.core.algebra.functions.FunctionIdentifier) AbstractFunctionCallExpression(org.apache.hyracks.algebricks.core.algebra.expressions.AbstractFunctionCallExpression) ConstantExpression(org.apache.hyracks.algebricks.core.algebra.expressions.ConstantExpression) ArrayList(java.util.ArrayList) AString(org.apache.asterix.om.base.AString) 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) AString(org.apache.asterix.om.base.AString) IVariableTypeEnvironment(org.apache.hyracks.algebricks.core.algebra.expressions.IVariableTypeEnvironment)

Aggregations

AInt32 (org.apache.asterix.om.base.AInt32)24 AsterixConstantValue (org.apache.asterix.om.constants.AsterixConstantValue)15 ConstantExpression (org.apache.hyracks.algebricks.core.algebra.expressions.ConstantExpression)14 AString (org.apache.asterix.om.base.AString)13 ILogicalExpression (org.apache.hyracks.algebricks.core.algebra.base.ILogicalExpression)13 ArrayList (java.util.ArrayList)11 LogicalVariable (org.apache.hyracks.algebricks.core.algebra.base.LogicalVariable)10 ScalarFunctionCallExpression (org.apache.hyracks.algebricks.core.algebra.expressions.ScalarFunctionCallExpression)10 VariableReferenceExpression (org.apache.hyracks.algebricks.core.algebra.expressions.VariableReferenceExpression)10 AlgebricksException (org.apache.hyracks.algebricks.common.exceptions.AlgebricksException)9 Mutable (org.apache.commons.lang3.mutable.Mutable)8 List (java.util.List)7 AOrderedList (org.apache.asterix.om.base.AOrderedList)6 MutableObject (org.apache.commons.lang3.mutable.MutableObject)6 ILogicalOperator (org.apache.hyracks.algebricks.core.algebra.base.ILogicalOperator)6 AssignOperator (org.apache.hyracks.algebricks.core.algebra.operators.logical.AssignOperator)6 Dataset (org.apache.asterix.metadata.entities.Dataset)5 IAObject (org.apache.asterix.om.base.IAObject)5 ARecordType (org.apache.asterix.om.types.ARecordType)5 IAType (org.apache.asterix.om.types.IAType)5