Search in sources :

Example 6 with AOrderedList

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

the class RecordRemoveFieldsTypeComputer method setFieldNameSet.

private boolean setFieldNameSet(ILogicalExpression expr, Set<String> fieldNameSet) {
    if (expr.getExpressionTag() == LogicalExpressionTag.CONSTANT) {
        AOrderedList orderedList = (AOrderedList) (((AsterixConstantValue) ((ConstantExpression) expr).getValue()).getObject());
        for (int i = 0; i < orderedList.size(); i++) {
            AString as = (AString) orderedList.getItem(i);
            fieldNameSet.add(as.getStringValue());
        }
        // Success
        return true;
    }
    return false;
}
Also used : AOrderedList(org.apache.asterix.om.base.AOrderedList) AsterixConstantValue(org.apache.asterix.om.constants.AsterixConstantValue) AString(org.apache.asterix.om.base.AString)

Example 7 with AOrderedList

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

the class RecordRemoveFieldsTypeComputer method getPathFromConstantExpression.

private void getPathFromConstantExpression(String funcName, ILogicalExpression expression, Set<String> fieldNameSet, List<List<String>> pathList) throws AlgebricksException {
    ConstantExpression ce = (ConstantExpression) expression;
    if (!(ce.getValue() instanceof AsterixConstantValue)) {
        throw new InvalidExpressionException(funcName, 1, ce, LogicalExpressionTag.CONSTANT);
    }
    IAObject item = ((AsterixConstantValue) ce.getValue()).getObject();
    ATypeTag type = item.getType().getTypeTag();
    switch(type) {
        case STRING:
            String fn = ((AString) item).getStringValue();
            fieldNameSet.add(fn);
            break;
        case ARRAY:
            AOrderedList pathOrdereList = (AOrderedList) item;
            String fieldName = ((AString) pathOrdereList.getItem(0)).getStringValue();
            fieldNameSet.add(fieldName);
            List<String> path = new ArrayList<>();
            for (int i = 0; i < pathOrdereList.size(); i++) {
                path.add(((AString) pathOrdereList.getItem(i)).getStringValue());
            }
            pathList.add(path);
            break;
        default:
            throw new UnsupportedTypeException(funcName, type);
    }
}
Also used : AOrderedList(org.apache.asterix.om.base.AOrderedList) AsterixConstantValue(org.apache.asterix.om.constants.AsterixConstantValue) ATypeTag(org.apache.asterix.om.types.ATypeTag) InvalidExpressionException(org.apache.asterix.om.exceptions.InvalidExpressionException) ConstantExpression(org.apache.hyracks.algebricks.core.algebra.expressions.ConstantExpression) IAObject(org.apache.asterix.om.base.IAObject) ArrayList(java.util.ArrayList) UnsupportedTypeException(org.apache.asterix.om.exceptions.UnsupportedTypeException) AString(org.apache.asterix.om.base.AString) AString(org.apache.asterix.om.base.AString)

Example 8 with AOrderedList

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

the class DisjunctivePredicateToJoinRule method rewritePost.

@Override
public boolean rewritePost(Mutable<ILogicalOperator> opRef, IOptimizationContext context) throws AlgebricksException {
    MetadataProvider metadataProvider = (MetadataProvider) context.getMetadataProvider();
    if (metadataProvider.isBlockingOperatorDisabled()) {
        return false;
    }
    SelectOperator select;
    if ((select = asSelectOperator(opRef)) == null) {
        return false;
    }
    AbstractFunctionCallExpression condEx;
    if ((condEx = asFunctionCallExpression(select.getCondition(), AlgebricksBuiltinFunctions.OR)) == null) {
        return false;
    }
    List<Mutable<ILogicalExpression>> args = condEx.getArguments();
    VariableReferenceExpression varEx = null;
    IAType valType = null;
    HashSet<AsterixConstantValue> values = new HashSet<AsterixConstantValue>();
    for (Mutable<ILogicalExpression> arg : args) {
        AbstractFunctionCallExpression fctCall;
        if ((fctCall = asFunctionCallExpression(arg, AlgebricksBuiltinFunctions.EQ)) == null) {
            return false;
        }
        boolean haveConst = false;
        boolean haveVar = false;
        List<Mutable<ILogicalExpression>> fctArgs = fctCall.getArguments();
        for (Mutable<ILogicalExpression> fctArg : fctArgs) {
            final ILogicalExpression argExpr = fctArg.getValue();
            switch(argExpr.getExpressionTag()) {
                case CONSTANT:
                    haveConst = true;
                    AsterixConstantValue value = (AsterixConstantValue) ((ConstantExpression) argExpr).getValue();
                    if (valType == null) {
                        valType = value.getObject().getType();
                    } else if (!isCompatible(valType, value.getObject().getType())) {
                        return false;
                    }
                    values.add(value);
                    break;
                case VARIABLE:
                    haveVar = true;
                    final VariableReferenceExpression varArg = (VariableReferenceExpression) argExpr;
                    if (varEx == null) {
                        varEx = varArg;
                    } else if (!varEx.getVariableReference().equals(varArg.getVariableReference())) {
                        return false;
                    }
                    break;
                default:
                    return false;
            }
        }
        if (!(haveVar && haveConst)) {
            return false;
        }
    }
    AOrderedList list = new AOrderedList(new AOrderedListType(valType, "orderedlist"));
    for (AsterixConstantValue value : values) {
        list.add(value.getObject());
    }
    EmptyTupleSourceOperator ets = new EmptyTupleSourceOperator();
    context.computeAndSetTypeEnvironmentForOperator(ets);
    ILogicalExpression cExp = new ConstantExpression(new AsterixConstantValue(list));
    Mutable<ILogicalExpression> mutCExp = new MutableObject<>(cExp);
    IFunctionInfo scanFctInfo = BuiltinFunctions.getAsterixFunctionInfo(BuiltinFunctions.SCAN_COLLECTION);
    UnnestingFunctionCallExpression scanExp = new UnnestingFunctionCallExpression(scanFctInfo, mutCExp);
    LogicalVariable scanVar = context.newVar();
    UnnestOperator unn = new UnnestOperator(scanVar, new MutableObject<>(scanExp));
    unn.getInputs().add(new MutableObject<>(ets));
    context.computeAndSetTypeEnvironmentForOperator(unn);
    IFunctionInfo eqFctInfo = BuiltinFunctions.getAsterixFunctionInfo(AlgebricksBuiltinFunctions.EQ);
    AbstractFunctionCallExpression eqExp = new ScalarFunctionCallExpression(eqFctInfo);
    eqExp.getArguments().add(new MutableObject<>(new VariableReferenceExpression(scanVar)));
    eqExp.getArguments().add(new MutableObject<>(varEx.cloneExpression()));
    eqExp.getAnnotations().put(IndexedNLJoinExpressionAnnotation.INSTANCE, IndexedNLJoinExpressionAnnotation.INSTANCE);
    BroadcastExpressionAnnotation bcast = new BroadcastExpressionAnnotation();
    // Broadcast the OR predicates branch.
    bcast.setObject(BroadcastExpressionAnnotation.BroadcastSide.LEFT);
    eqExp.getAnnotations().put(BroadcastExpressionAnnotation.BROADCAST_ANNOTATION_KEY, bcast);
    InnerJoinOperator jOp = new InnerJoinOperator(new MutableObject<>(eqExp));
    jOp.getInputs().add(new MutableObject<>(unn));
    jOp.getInputs().add(select.getInputs().get(0));
    opRef.setValue(jOp);
    context.computeAndSetTypeEnvironmentForOperator(jOp);
    return true;
}
Also used : IFunctionInfo(org.apache.hyracks.algebricks.core.algebra.functions.IFunctionInfo) ConstantExpression(org.apache.hyracks.algebricks.core.algebra.expressions.ConstantExpression) InnerJoinOperator(org.apache.hyracks.algebricks.core.algebra.operators.logical.InnerJoinOperator) UnnestOperator(org.apache.hyracks.algebricks.core.algebra.operators.logical.UnnestOperator) SelectOperator(org.apache.hyracks.algebricks.core.algebra.operators.logical.SelectOperator) AOrderedList(org.apache.asterix.om.base.AOrderedList) AsterixConstantValue(org.apache.asterix.om.constants.AsterixConstantValue) BroadcastExpressionAnnotation(org.apache.hyracks.algebricks.core.algebra.expressions.BroadcastExpressionAnnotation) EmptyTupleSourceOperator(org.apache.hyracks.algebricks.core.algebra.operators.logical.EmptyTupleSourceOperator) HashSet(java.util.HashSet) MutableObject(org.apache.commons.lang3.mutable.MutableObject) ScalarFunctionCallExpression(org.apache.hyracks.algebricks.core.algebra.expressions.ScalarFunctionCallExpression) LogicalVariable(org.apache.hyracks.algebricks.core.algebra.base.LogicalVariable) UnnestingFunctionCallExpression(org.apache.hyracks.algebricks.core.algebra.expressions.UnnestingFunctionCallExpression) AbstractFunctionCallExpression(org.apache.hyracks.algebricks.core.algebra.expressions.AbstractFunctionCallExpression) AOrderedListType(org.apache.asterix.om.types.AOrderedListType) Mutable(org.apache.commons.lang3.mutable.Mutable) ILogicalExpression(org.apache.hyracks.algebricks.core.algebra.base.ILogicalExpression) MetadataProvider(org.apache.asterix.metadata.declared.MetadataProvider) VariableReferenceExpression(org.apache.hyracks.algebricks.core.algebra.expressions.VariableReferenceExpression) IAType(org.apache.asterix.om.types.IAType)

Example 9 with AOrderedList

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

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

the class PlanTranslationUtil method prepareMetaKeyAccessExpression.

public static void prepareMetaKeyAccessExpression(List<String> field, LogicalVariable resVar, List<Mutable<ILogicalExpression>> assignExpressions, List<LogicalVariable> vars, List<Mutable<ILogicalExpression>> varRefs, IVariableContext context) {
    IAObject value = (field.size() > 1) ? new AOrderedList(field) : new AString(field.get(0));
    ScalarFunctionCallExpression metaKeyFunction = new ScalarFunctionCallExpression(FunctionUtil.getFunctionInfo(BuiltinFunctions.META_KEY));
    metaKeyFunction.getArguments().add(new MutableObject<ILogicalExpression>(new VariableReferenceExpression(resVar)));
    metaKeyFunction.getArguments().add(new MutableObject<>(new ConstantExpression(new AsterixConstantValue(value))));
    assignExpressions.add(new MutableObject<ILogicalExpression>(metaKeyFunction));
    LogicalVariable v = context.newVar();
    vars.add(v);
    if (varRefs != null) {
        varRefs.add(new MutableObject<ILogicalExpression>(new VariableReferenceExpression(v)));
    }
}
Also used : LogicalVariable(org.apache.hyracks.algebricks.core.algebra.base.LogicalVariable) ILogicalExpression(org.apache.hyracks.algebricks.core.algebra.base.ILogicalExpression) AOrderedList(org.apache.asterix.om.base.AOrderedList) AsterixConstantValue(org.apache.asterix.om.constants.AsterixConstantValue) VariableReferenceExpression(org.apache.hyracks.algebricks.core.algebra.expressions.VariableReferenceExpression) IAObject(org.apache.asterix.om.base.IAObject) ConstantExpression(org.apache.hyracks.algebricks.core.algebra.expressions.ConstantExpression) AString(org.apache.asterix.om.base.AString) ScalarFunctionCallExpression(org.apache.hyracks.algebricks.core.algebra.expressions.ScalarFunctionCallExpression)

Aggregations

AOrderedList (org.apache.asterix.om.base.AOrderedList)16 AString (org.apache.asterix.om.base.AString)13 ArrayList (java.util.ArrayList)8 ConstantExpression (org.apache.hyracks.algebricks.core.algebra.expressions.ConstantExpression)8 AsterixConstantValue (org.apache.asterix.om.constants.AsterixConstantValue)7 IAObject (org.apache.asterix.om.base.IAObject)5 ILogicalExpression (org.apache.hyracks.algebricks.core.algebra.base.ILogicalExpression)5 AInt32 (org.apache.asterix.om.base.AInt32)4 ARecord (org.apache.asterix.om.base.ARecord)4 AOrderedListType (org.apache.asterix.om.types.AOrderedListType)4 IAType (org.apache.asterix.om.types.IAType)4 AlgebricksException (org.apache.hyracks.algebricks.common.exceptions.AlgebricksException)4 AbstractFunctionCallExpression (org.apache.hyracks.algebricks.core.algebra.expressions.AbstractFunctionCallExpression)4 ScalarFunctionCallExpression (org.apache.hyracks.algebricks.core.algebra.expressions.ScalarFunctionCallExpression)4 List (java.util.List)3 IACursor (org.apache.asterix.om.base.IACursor)3 ARecordType (org.apache.asterix.om.types.ARecordType)3 LogicalVariable (org.apache.hyracks.algebricks.core.algebra.base.LogicalVariable)3 VariableReferenceExpression (org.apache.hyracks.algebricks.core.algebra.expressions.VariableReferenceExpression)3 DataOutput (java.io.DataOutput)2