Search in sources :

Example 26 with AString

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

the class InlineAllNtsInSubplanVisitor method createRecordConstructorAssignOp.

private Pair<ILogicalOperator, LogicalVariable> createRecordConstructorAssignOp(Set<LogicalVariable> inputLiveVars) {
    // Creates a nested record.
    List<Mutable<ILogicalExpression>> recordConstructorArgs = new ArrayList<>();
    for (LogicalVariable inputLiveVar : inputLiveVars) {
        if (!correlatedKeyVars.contains(inputLiveVar)) {
            recordConstructorArgs.add(new MutableObject<ILogicalExpression>(new ConstantExpression(new AsterixConstantValue(new AString(Integer.toString(inputLiveVar.getId()))))));
            recordConstructorArgs.add(new MutableObject<ILogicalExpression>(new VariableReferenceExpression(inputLiveVar)));
        }
    }
    LogicalVariable recordVar = context.newVar();
    Mutable<ILogicalExpression> recordExprRef = new MutableObject<ILogicalExpression>(new ScalarFunctionCallExpression(FunctionUtil.getFunctionInfo(BuiltinFunctions.OPEN_RECORD_CONSTRUCTOR), recordConstructorArgs));
    AssignOperator assignOp = new AssignOperator(recordVar, recordExprRef);
    return new Pair<ILogicalOperator, LogicalVariable>(assignOp, recordVar);
}
Also used : LogicalVariable(org.apache.hyracks.algebricks.core.algebra.base.LogicalVariable) ConstantExpression(org.apache.hyracks.algebricks.core.algebra.expressions.ConstantExpression) ArrayList(java.util.ArrayList) AssignOperator(org.apache.hyracks.algebricks.core.algebra.operators.logical.AssignOperator) 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) AString(org.apache.asterix.om.base.AString) MutableObject(org.apache.commons.lang3.mutable.MutableObject) ScalarFunctionCallExpression(org.apache.hyracks.algebricks.core.algebra.expressions.ScalarFunctionCallExpression) Pair(org.apache.hyracks.algebricks.common.utils.Pair)

Example 27 with AString

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

the class InvertedIndexAccessMethod method isEditDistanceFuncSelectOptimizable.

private boolean isEditDistanceFuncSelectOptimizable(Index index, IOptimizableFuncExpr optFuncExpr) throws AlgebricksException {
    // Check for panic in selection query.
    // TODO: Panic also depends on prePost which is currently hardcoded to be true.
    AsterixConstantValue listOrStrConstVal = (AsterixConstantValue) ((ConstantExpression) optFuncExpr.getConstantExpr(0)).getValue();
    IAObject listOrStrObj = listOrStrConstVal.getObject();
    ATypeTag typeTag = listOrStrObj.getType().getTypeTag();
    if (!isEditDistanceFuncCompatible(typeTag, index.getIndexType())) {
        return false;
    }
    AsterixConstantValue intConstVal = (AsterixConstantValue) ((ConstantExpression) optFuncExpr.getConstantExpr(1)).getValue();
    IAObject intObj = intConstVal.getObject();
    AInt32 edThresh = null;
    // Apply type casting based on numeric types of the input to INTEGER type.
    try {
        edThresh = (AInt32) ATypeHierarchy.convertNumericTypeObject(intObj, ATypeTag.INTEGER);
    } catch (HyracksDataException e) {
        throw new AlgebricksException(e);
    }
    int mergeThreshold = 0;
    if (typeTag == ATypeTag.STRING) {
        AString astr = (AString) listOrStrObj;
        // Compute merge threshold depending on the query grams contain pre- and postfixing
        if (optFuncExpr.containsPartialField()) {
            mergeThreshold = (astr.getStringValue().length() - index.getGramLength() + 1) - edThresh.getIntegerValue() * index.getGramLength();
        } else {
            mergeThreshold = (astr.getStringValue().length() + index.getGramLength() - 1) - edThresh.getIntegerValue() * index.getGramLength();
        }
    }
    if ((typeTag == ATypeTag.ARRAY) && (index.getIndexType() == IndexType.SINGLE_PARTITION_WORD_INVIX || index.getIndexType() == IndexType.LENGTH_PARTITIONED_WORD_INVIX)) {
        IACollection alist = (IACollection) listOrStrObj;
        // Compute merge threshold.
        mergeThreshold = alist.size() - edThresh.getIntegerValue();
    }
    if (mergeThreshold <= 0) {
        // We cannot use index to optimize expr.
        return false;
    }
    return true;
}
Also used : AsterixConstantValue(org.apache.asterix.om.constants.AsterixConstantValue) ATypeTag(org.apache.asterix.om.types.ATypeTag) IAObject(org.apache.asterix.om.base.IAObject) IACollection(org.apache.asterix.om.base.IACollection) AlgebricksException(org.apache.hyracks.algebricks.common.exceptions.AlgebricksException) AString(org.apache.asterix.om.base.AString) AInt32(org.apache.asterix.om.base.AInt32) HyracksDataException(org.apache.hyracks.api.exceptions.HyracksDataException)

Example 28 with AString

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

the class IntroduceSecondaryIndexInsertDeleteRule method getOpenOrNestedFieldAccessFunction.

private static AbstractFunctionCallExpression getOpenOrNestedFieldAccessFunction(Mutable<ILogicalExpression> varRef, List<String> fields) {
    ScalarFunctionCallExpression func;
    if (fields.size() > 1) {
        IAObject fieldList = stringListToAOrderedList(fields);
        Mutable<ILogicalExpression> fieldRef = constantToMutableLogicalExpression(fieldList);
        // Create an expression for the nested case
        func = new ScalarFunctionCallExpression(FunctionUtil.getFunctionInfo(BuiltinFunctions.FIELD_ACCESS_NESTED), varRef, fieldRef);
    } else {
        IAObject fieldList = new AString(fields.get(0));
        Mutable<ILogicalExpression> fieldRef = constantToMutableLogicalExpression(fieldList);
        // Create an expression for the open field case (By name)
        func = new ScalarFunctionCallExpression(FunctionUtil.getFunctionInfo(BuiltinFunctions.FIELD_ACCESS_BY_NAME), varRef, fieldRef);
    }
    return func;
}
Also used : ILogicalExpression(org.apache.hyracks.algebricks.core.algebra.base.ILogicalExpression) IAObject(org.apache.asterix.om.base.IAObject) AString(org.apache.asterix.om.base.AString) ScalarFunctionCallExpression(org.apache.hyracks.algebricks.core.algebra.expressions.ScalarFunctionCallExpression)

Example 29 with AString

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

the class AbstractIntroduceAccessMethodRule method getFieldNameFromSubTree.

/**
     * Returns the field name corresponding to the assigned variable at
     * varIndex. Returns null if the expr at varIndex does not yield to a field
     * access function after following a set of allowed functions.
     *
     * @throws AlgebricksException
     */
protected List<String> getFieldNameFromSubTree(IOptimizableFuncExpr optFuncExpr, OptimizableOperatorSubTree subTree, int opIndex, int assignVarIndex, ARecordType recordType, int funcVarIndex, ILogicalExpression parentFuncExpr, LogicalVariable recordVar, ARecordType metaType, LogicalVariable metaVar) throws AlgebricksException {
    // Get expression corresponding to opVar at varIndex.
    AbstractLogicalExpression expr = null;
    AbstractFunctionCallExpression childFuncExpr = null;
    AbstractLogicalOperator op = subTree.getAssignsAndUnnests().get(opIndex);
    if (op.getOperatorTag() == LogicalOperatorTag.ASSIGN) {
        AssignOperator assignOp = (AssignOperator) op;
        expr = (AbstractLogicalExpression) assignOp.getExpressions().get(assignVarIndex).getValue();
        // Can't get a field name from a constant expression. So, return null.
        if (expr.getExpressionTag() == LogicalExpressionTag.CONSTANT) {
            return Collections.emptyList();
        }
        childFuncExpr = (AbstractFunctionCallExpression) expr;
    } else {
        UnnestOperator unnestOp = (UnnestOperator) op;
        expr = (AbstractLogicalExpression) unnestOp.getExpressionRef().getValue();
        if (expr.getExpressionTag() != LogicalExpressionTag.FUNCTION_CALL) {
            return Collections.emptyList();
        }
        childFuncExpr = (AbstractFunctionCallExpression) expr;
        if (childFuncExpr.getFunctionIdentifier() != BuiltinFunctions.SCAN_COLLECTION) {
            return Collections.emptyList();
        }
        expr = (AbstractLogicalExpression) childFuncExpr.getArguments().get(0).getValue();
    }
    if (expr.getExpressionTag() != LogicalExpressionTag.FUNCTION_CALL) {
        return Collections.emptyList();
    }
    AbstractFunctionCallExpression funcExpr = (AbstractFunctionCallExpression) expr;
    FunctionIdentifier funcIdent = funcExpr.getFunctionIdentifier();
    boolean isByName = false;
    boolean isFieldAccess = false;
    String fieldName = null;
    List<String> nestedAccessFieldName = null;
    int fieldIndex = -1;
    if (funcIdent == BuiltinFunctions.FIELD_ACCESS_BY_NAME) {
        fieldName = ConstantExpressionUtil.getStringArgument(funcExpr, 1);
        if (fieldName == null) {
            return Collections.emptyList();
        }
        isFieldAccess = true;
        isByName = true;
    } else if (funcIdent == BuiltinFunctions.FIELD_ACCESS_BY_INDEX) {
        Integer idx = ConstantExpressionUtil.getIntArgument(funcExpr, 1);
        if (idx == null) {
            return Collections.emptyList();
        }
        fieldIndex = idx;
        isFieldAccess = true;
    } else if (funcIdent == BuiltinFunctions.FIELD_ACCESS_NESTED) {
        ILogicalExpression nameArg = funcExpr.getArguments().get(1).getValue();
        if (nameArg.getExpressionTag() != LogicalExpressionTag.CONSTANT) {
            return Collections.emptyList();
        }
        ConstantExpression constExpr = (ConstantExpression) nameArg;
        AOrderedList orderedNestedFieldName = (AOrderedList) ((AsterixConstantValue) constExpr.getValue()).getObject();
        nestedAccessFieldName = new ArrayList<>();
        for (int i = 0; i < orderedNestedFieldName.size(); i++) {
            nestedAccessFieldName.add(((AString) orderedNestedFieldName.getItem(i)).getStringValue());
        }
        isFieldAccess = true;
        isByName = true;
    }
    if (isFieldAccess) {
        LogicalVariable sourceVar = ((VariableReferenceExpression) funcExpr.getArguments().get(0).getValue()).getVariableReference();
        optFuncExpr.setLogicalExpr(funcVarIndex, parentFuncExpr);
        int[] assignAndExpressionIndexes = null;
        //go forward through nested assigns until you find the relevant one
        for (int i = opIndex + 1; i < subTree.getAssignsAndUnnests().size(); i++) {
            AbstractLogicalOperator subOp = subTree.getAssignsAndUnnests().get(i);
            List<LogicalVariable> varList;
            if (subOp.getOperatorTag() == LogicalOperatorTag.ASSIGN) {
                //Nested was an assign
                varList = ((AssignOperator) subOp).getVariables();
            } else if (subOp.getOperatorTag() == LogicalOperatorTag.UNNEST) {
                //Nested is not an assign
                varList = ((UnnestOperator) subOp).getVariables();
            } else {
                break;
            }
            //Go through variables in assign to check for match
            for (int varIndex = 0; varIndex < varList.size(); varIndex++) {
                LogicalVariable var = varList.get(varIndex);
                ArrayList<LogicalVariable> parentVars = new ArrayList<>();
                expr.getUsedVariables(parentVars);
                if (parentVars.contains(var)) {
                    //Found the variable we are looking for.
                    //return assign and index of expression
                    int[] returnValues = { i, varIndex };
                    assignAndExpressionIndexes = returnValues;
                }
            }
        }
        if (assignAndExpressionIndexes != null && assignAndExpressionIndexes[0] > -1) {
            //We found the nested assign
            //Recursive call on nested assign
            List<String> parentFieldNames = getFieldNameFromSubTree(optFuncExpr, subTree, assignAndExpressionIndexes[0], assignAndExpressionIndexes[1], recordType, funcVarIndex, parentFuncExpr, recordVar, metaType, metaVar);
            if (parentFieldNames.isEmpty()) {
                //We will not use index
                return Collections.emptyList();
            }
            if (!isByName) {
                fieldName = sourceVar.equals(metaVar) ? ((ARecordType) metaType.getSubFieldType(parentFieldNames)).getFieldNames()[fieldIndex] : ((ARecordType) recordType.getSubFieldType(parentFieldNames)).getFieldNames()[fieldIndex];
            }
            optFuncExpr.setSourceVar(funcVarIndex, ((AssignOperator) op).getVariables().get(assignVarIndex));
            //add fieldName to the nested fieldName, return
            if (nestedAccessFieldName != null) {
                for (int i = 0; i < nestedAccessFieldName.size(); i++) {
                    parentFieldNames.add(nestedAccessFieldName.get(i));
                }
            } else {
                parentFieldNames.add(fieldName);
            }
            return (parentFieldNames);
        }
        optFuncExpr.setSourceVar(funcVarIndex, ((AssignOperator) op).getVariables().get(assignVarIndex));
        //no nested assign, we are at the lowest level.
        if (isByName) {
            if (nestedAccessFieldName != null) {
                return nestedAccessFieldName;
            }
            return new ArrayList<>(Arrays.asList(fieldName));
        }
        return new ArrayList<>(Arrays.asList(sourceVar.equals(metaVar) ? metaType.getFieldNames()[fieldIndex] : recordType.getFieldNames()[fieldIndex]));
    }
    if (!funcIDSetThatRetainFieldName.contains(funcIdent)) {
        return Collections.emptyList();
    }
    // We use a part of the field in edit distance computation
    if (optFuncExpr.getFuncExpr().getFunctionIdentifier() == BuiltinFunctions.EDIT_DISTANCE_CHECK) {
        optFuncExpr.setPartialField(true);
    }
    // We expect the function's argument to be a variable, otherwise we
    // cannot apply an index.
    ILogicalExpression argExpr = funcExpr.getArguments().get(0).getValue();
    if (argExpr.getExpressionTag() != LogicalExpressionTag.VARIABLE) {
        return Collections.emptyList();
    }
    LogicalVariable curVar = ((VariableReferenceExpression) argExpr).getVariableReference();
    // the current operator
    for (int assignOrUnnestIndex = opIndex + 1; assignOrUnnestIndex < subTree.getAssignsAndUnnests().size(); assignOrUnnestIndex++) {
        AbstractLogicalOperator curOp = subTree.getAssignsAndUnnests().get(assignOrUnnestIndex);
        if (curOp.getOperatorTag() == LogicalOperatorTag.ASSIGN) {
            AssignOperator assignOp = (AssignOperator) curOp;
            List<LogicalVariable> varList = assignOp.getVariables();
            for (int varIndex = 0; varIndex < varList.size(); varIndex++) {
                LogicalVariable var = varList.get(varIndex);
                if (var.equals(curVar)) {
                    optFuncExpr.setSourceVar(funcVarIndex, var);
                    return getFieldNameFromSubTree(optFuncExpr, subTree, assignOrUnnestIndex, varIndex, recordType, funcVarIndex, childFuncExpr, recordVar, metaType, metaVar);
                }
            }
        } else {
            UnnestOperator unnestOp = (UnnestOperator) curOp;
            LogicalVariable var = unnestOp.getVariable();
            if (var.equals(curVar)) {
                getFieldNameFromSubTree(optFuncExpr, subTree, assignOrUnnestIndex, 0, recordType, funcVarIndex, childFuncExpr, recordVar, metaType, metaVar);
            }
        }
    }
    return Collections.emptyList();
}
Also used : LogicalVariable(org.apache.hyracks.algebricks.core.algebra.base.LogicalVariable) AbstractLogicalExpression(org.apache.hyracks.algebricks.core.algebra.expressions.AbstractLogicalExpression) AbstractLogicalOperator(org.apache.hyracks.algebricks.core.algebra.operators.logical.AbstractLogicalOperator) 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) AssignOperator(org.apache.hyracks.algebricks.core.algebra.operators.logical.AssignOperator) UnnestOperator(org.apache.hyracks.algebricks.core.algebra.operators.logical.UnnestOperator) FunctionIdentifier(org.apache.hyracks.algebricks.core.algebra.functions.FunctionIdentifier) ILogicalExpression(org.apache.hyracks.algebricks.core.algebra.base.ILogicalExpression) AOrderedList(org.apache.asterix.om.base.AOrderedList) VariableReferenceExpression(org.apache.hyracks.algebricks.core.algebra.expressions.VariableReferenceExpression)

Example 30 with AString

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

the class ResolveVariableRule method resolveAsFieldAccess.

// Resolves a "resolve" function call as a field access.
private void resolveAsFieldAccess(Mutable<ILogicalExpression> funcRef, Pair<ILogicalExpression, List<String>> varAndPath) {
    // Rewrites to field-access-by-names.
    ILogicalExpression expr = varAndPath.first;
    List<String> path = varAndPath.second;
    Mutable<ILogicalExpression> firstArgRef = new MutableObject<>(expr);
    ILogicalExpression newFunc = null;
    for (String fieldName : path) {
        List<Mutable<ILogicalExpression>> args = new ArrayList<>();
        args.add(firstArgRef);
        args.add(new MutableObject<>(new ConstantExpression(new AsterixConstantValue(new AString(fieldName)))));
        newFunc = new ScalarFunctionCallExpression(FunctionUtil.getFunctionInfo(BuiltinFunctions.FIELD_ACCESS_BY_NAME), args);
        firstArgRef = new MutableObject<>(newFunc);
    }
    funcRef.setValue(newFunc);
}
Also used : ConstantExpression(org.apache.hyracks.algebricks.core.algebra.expressions.ConstantExpression) 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) AString(org.apache.asterix.om.base.AString) MutableObject(org.apache.commons.lang3.mutable.MutableObject) ScalarFunctionCallExpression(org.apache.hyracks.algebricks.core.algebra.expressions.ScalarFunctionCallExpression)

Aggregations

AString (org.apache.asterix.om.base.AString)58 AsterixConstantValue (org.apache.asterix.om.constants.AsterixConstantValue)21 ConstantExpression (org.apache.hyracks.algebricks.core.algebra.expressions.ConstantExpression)20 ArrayList (java.util.ArrayList)19 ILogicalExpression (org.apache.hyracks.algebricks.core.algebra.base.ILogicalExpression)18 AMutableString (org.apache.asterix.om.base.AMutableString)16 ArrayBackedValueStorage (org.apache.hyracks.data.std.util.ArrayBackedValueStorage)16 AOrderedList (org.apache.asterix.om.base.AOrderedList)13 Mutable (org.apache.commons.lang3.mutable.Mutable)12 RecordBuilder (org.apache.asterix.builders.RecordBuilder)11 AInt32 (org.apache.asterix.om.base.AInt32)11 ScalarFunctionCallExpression (org.apache.hyracks.algebricks.core.algebra.expressions.ScalarFunctionCallExpression)10 IARecordBuilder (org.apache.asterix.builders.IARecordBuilder)9 IACursor (org.apache.asterix.om.base.IACursor)9 IAObject (org.apache.asterix.om.base.IAObject)9 IAType (org.apache.asterix.om.types.IAType)9 AlgebricksException (org.apache.hyracks.algebricks.common.exceptions.AlgebricksException)9 LogicalVariable (org.apache.hyracks.algebricks.core.algebra.base.LogicalVariable)9 AbstractFunctionCallExpression (org.apache.hyracks.algebricks.core.algebra.expressions.AbstractFunctionCallExpression)9 ARecord (org.apache.asterix.om.base.ARecord)8