Search in sources :

Example 31 with AString

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

the class ResolveVariableRule method resolveInternal.

// Resolves an undefined name to a dataset or a fully qualified variable/field-access path
// based on the given information of dataset matches and candidate paths.
private boolean resolveInternal(Mutable<ILogicalExpression> funcRef, boolean hasMatchedDataset, Collection<Pair<ILogicalExpression, List<String>>> varAccessCandidates, String unresolvedVarName, Triple<Boolean, String, String> fullyQualifiedDatasetPathCandidateFromParent, Mutable<ILogicalExpression> parentFuncRef, IOptimizationContext context) throws AlgebricksException {
    AbstractFunctionCallExpression func = (AbstractFunctionCallExpression) funcRef.getValue();
    int numVarCandidates = varAccessCandidates.size();
    // The resolution order: 1. field-access 2. datasets (standalone-name or fully-qualified)
    if (numVarCandidates > 0) {
        if (numVarCandidates == 1) {
            resolveAsFieldAccess(funcRef, varAccessCandidates.iterator().next());
        } else {
            // More than one possibilities.
            throw new AlgebricksException("Cannot resolve ambiguous alias reference for undefined identifier " + unresolvedVarName);
        }
    } else if (hasMatchedDataset) {
        // Rewrites the "resolve" function to a "dataset" function and only keep the dataset name argument.
        func.setFunctionInfo(FunctionUtil.getFunctionInfo(BuiltinFunctions.DATASET));
        Mutable<ILogicalExpression> datasetNameExpression = func.getArguments().get(0);
        func.getArguments().clear();
        func.getArguments().add(datasetNameExpression);
    } else if (fullyQualifiedDatasetPathCandidateFromParent.first) {
        // Rewrites the parent "field-access" function to a "dataset" function.
        AbstractFunctionCallExpression parentFunc = (AbstractFunctionCallExpression) parentFuncRef.getValue();
        parentFunc.setFunctionInfo(FunctionUtil.getFunctionInfo(BuiltinFunctions.DATASET));
        parentFunc.getArguments().clear();
        parentFunc.getArguments().add(new MutableObject<>(new ConstantExpression(new AsterixConstantValue(new AString(fullyQualifiedDatasetPathCandidateFromParent.second + "." + fullyQualifiedDatasetPathCandidateFromParent.third)))));
    } else {
        MetadataProvider metadataProvider = (MetadataProvider) context.getMetadataProvider();
        // Cannot find any resolution.
        throw new AlgebricksException("Cannot find dataset " + unresolvedVarName + " in dataverse " + metadataProvider.getDefaultDataverseName() + " nor an alias with name " + unresolvedVarName);
    }
    return true;
}
Also used : Mutable(org.apache.commons.lang3.mutable.Mutable) AsterixConstantValue(org.apache.asterix.om.constants.AsterixConstantValue) MetadataProvider(org.apache.asterix.metadata.declared.MetadataProvider) AbstractFunctionCallExpression(org.apache.hyracks.algebricks.core.algebra.expressions.AbstractFunctionCallExpression) ConstantExpression(org.apache.hyracks.algebricks.core.algebra.expressions.ConstantExpression) AlgebricksException(org.apache.hyracks.algebricks.common.exceptions.AlgebricksException) AString(org.apache.asterix.om.base.AString)

Example 32 with AString

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

the class AccessMethodUtils method addStringArg.

private static void addStringArg(String argument, List<Mutable<ILogicalExpression>> funcArgs) {
    Mutable<ILogicalExpression> stringRef = new MutableObject<>(new ConstantExpression(new AsterixConstantValue(new AString(argument))));
    funcArgs.add(stringRef);
}
Also used : 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) AString(org.apache.asterix.om.base.AString) MutableObject(org.apache.commons.lang3.mutable.MutableObject)

Example 33 with AString

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

the class InvertedIndexAccessMethod method isContainsFuncSelectOptimizable.

private boolean isContainsFuncSelectOptimizable(Index index, IOptimizableFuncExpr optFuncExpr) {
    AsterixConstantValue strConstVal = (AsterixConstantValue) ((ConstantExpression) optFuncExpr.getConstantExpr(0)).getValue();
    IAObject strObj = strConstVal.getObject();
    ATypeTag typeTag = strObj.getType().getTypeTag();
    if (!isContainsFuncCompatible(typeTag, index.getIndexType())) {
        return false;
    }
    // Check that the constant search string has at least gramLength characters.
    if (strObj.getType().getTypeTag() == ATypeTag.STRING) {
        AString astr = (AString) strObj;
        if (astr.getStringValue().length() >= index.getGramLength()) {
            return true;
        }
    }
    return false;
}
Also used : AsterixConstantValue(org.apache.asterix.om.constants.AsterixConstantValue) ATypeTag(org.apache.asterix.om.types.ATypeTag) IAObject(org.apache.asterix.om.base.IAObject) AString(org.apache.asterix.om.base.AString)

Example 34 with AString

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

the class InlineAllNtsInSubplanVisitor method createFieldAccessAssignOperator.

private ILogicalOperator createFieldAccessAssignOperator(LogicalVariable recordVar, Set<LogicalVariable> inputLiveVars) {
    List<LogicalVariable> fieldAccessVars = new ArrayList<>();
    List<Mutable<ILogicalExpression>> fieldAccessExprs = new ArrayList<>();
    // Adds field access by name.
    for (LogicalVariable inputLiveVar : inputLiveVars) {
        if (!correlatedKeyVars.contains(inputLiveVar)) {
            // field Var
            LogicalVariable newVar = context.newVar();
            fieldAccessVars.add(newVar);
            // fieldAcess expr
            List<Mutable<ILogicalExpression>> argRefs = new ArrayList<>();
            argRefs.add(new MutableObject<ILogicalExpression>(new VariableReferenceExpression(recordVar)));
            argRefs.add(new MutableObject<ILogicalExpression>(new ConstantExpression(new AsterixConstantValue(new AString(Integer.toString(inputLiveVar.getId()))))));
            fieldAccessExprs.add(new MutableObject<ILogicalExpression>(new ScalarFunctionCallExpression(FunctionUtil.getFunctionInfo(BuiltinFunctions.FIELD_ACCESS_BY_NAME), argRefs)));
            // Updates variable mapping for ancestor operators.
            updateInputToOutputVarMapping(inputLiveVar, newVar, false);
        }
    }
    AssignOperator fieldAccessAssignOp = new AssignOperator(fieldAccessVars, fieldAccessExprs);
    return fieldAccessAssignOp;
}
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) ScalarFunctionCallExpression(org.apache.hyracks.algebricks.core.algebra.expressions.ScalarFunctionCallExpression)

Example 35 with AString

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

the class SqlppExpressionToPlanTranslator method visit.

@Override
public Pair<ILogicalOperator, LogicalVariable> visit(JoinClause joinClause, Mutable<ILogicalOperator> inputRef) throws CompilationException {
    Mutable<ILogicalOperator> leftInputRef = uncorrelatedLeftBranchStack.pop();
    if (joinClause.getJoinType() == JoinType.INNER) {
        Pair<ILogicalOperator, LogicalVariable> rightBranch = generateUnnestForBinaryCorrelateRightBranch(joinClause, inputRef, true);
        // A join operator with condition TRUE.
        AbstractBinaryJoinOperator joinOperator = new InnerJoinOperator(new MutableObject<ILogicalExpression>(ConstantExpression.TRUE), leftInputRef, new MutableObject<ILogicalOperator>(rightBranch.first));
        Mutable<ILogicalOperator> joinOpRef = new MutableObject<>(joinOperator);
        // Add an additional filter operator.
        Pair<ILogicalExpression, Mutable<ILogicalOperator>> conditionExprOpPair = langExprToAlgExpression(joinClause.getConditionExpression(), joinOpRef);
        SelectOperator filter = new SelectOperator(new MutableObject<ILogicalExpression>(conditionExprOpPair.first), false, null);
        filter.getInputs().add(conditionExprOpPair.second);
        return new Pair<>(filter, rightBranch.second);
    } else {
        // Creates a subplan operator.
        SubplanOperator subplanOp = new SubplanOperator();
        Mutable<ILogicalOperator> ntsRef = new MutableObject<>(new NestedTupleSourceOperator(new MutableObject<ILogicalOperator>(subplanOp)));
        subplanOp.getInputs().add(leftInputRef);
        // Enters the translation for a subplan.
        context.enterSubplan();
        // Adds an unnest operator to unnest to right expression.
        Pair<ILogicalOperator, LogicalVariable> rightBranch = generateUnnestForBinaryCorrelateRightBranch(joinClause, ntsRef, true);
        AbstractUnnestNonMapOperator rightUnnestOp = (AbstractUnnestNonMapOperator) rightBranch.first;
        // Adds an additional filter operator for the join condition.
        Pair<ILogicalExpression, Mutable<ILogicalOperator>> conditionExprOpPair = langExprToAlgExpression(joinClause.getConditionExpression(), new MutableObject<ILogicalOperator>(rightUnnestOp));
        SelectOperator filter = new SelectOperator(new MutableObject<ILogicalExpression>(conditionExprOpPair.first), false, null);
        filter.getInputs().add(conditionExprOpPair.second);
        ILogicalOperator currentTopOp = filter;
        LogicalVariable varToListify;
        boolean hasRightPosVar = rightUnnestOp.getPositionalVariable() != null;
        if (hasRightPosVar) {
            // Creates record to get correlation between the two aggregate variables.
            ScalarFunctionCallExpression recordCreationFunc = new ScalarFunctionCallExpression(FunctionUtil.getFunctionInfo(BuiltinFunctions.CLOSED_RECORD_CONSTRUCTOR), // Field name for the listified right unnest var.
            new MutableObject<ILogicalExpression>(new ConstantExpression(new AsterixConstantValue(new AString("unnestvar")))), // The listified right unnest var
            new MutableObject<ILogicalExpression>(new VariableReferenceExpression(rightUnnestOp.getVariable())), // Field name for the listified right unnest positional var.
            new MutableObject<ILogicalExpression>(new ConstantExpression(new AsterixConstantValue(new AString("posvar")))), // The listified right unnest positional var.
            new MutableObject<ILogicalExpression>(new VariableReferenceExpression(rightUnnestOp.getPositionalVariable())));
            // Assigns the record constructor function to a record variable.
            LogicalVariable recordVar = context.newVar();
            AssignOperator assignOp = new AssignOperator(recordVar, new MutableObject<ILogicalExpression>(recordCreationFunc));
            assignOp.getInputs().add(new MutableObject<ILogicalOperator>(currentTopOp));
            // Sets currentTopOp and varToListify for later usages.
            currentTopOp = assignOp;
            varToListify = recordVar;
        } else {
            varToListify = rightUnnestOp.getVariable();
        }
        // Adds an aggregate operator to listfy unnest variables.
        AggregateFunctionCallExpression fListify = BuiltinFunctions.makeAggregateFunctionExpression(BuiltinFunctions.LISTIFY, mkSingletonArrayList(new MutableObject<ILogicalExpression>(new VariableReferenceExpression(varToListify))));
        LogicalVariable aggVar = context.newSubplanOutputVar();
        AggregateOperator aggOp = new AggregateOperator(mkSingletonArrayList(aggVar), mkSingletonArrayList(new MutableObject<ILogicalExpression>(fListify)));
        aggOp.getInputs().add(new MutableObject<ILogicalOperator>(currentTopOp));
        // Exits the translation of a subplan.
        context.exitSubplan();
        // Sets the nested subplan of the subplan operator.
        ILogicalPlan subplan = new ALogicalPlanImpl(new MutableObject<ILogicalOperator>(aggOp));
        subplanOp.getNestedPlans().add(subplan);
        // Outer unnest the aggregated var from the subplan.
        LogicalVariable outerUnnestVar = context.newVar();
        LeftOuterUnnestOperator outerUnnestOp = new LeftOuterUnnestOperator(outerUnnestVar, new MutableObject<ILogicalExpression>(makeUnnestExpression(new VariableReferenceExpression(aggVar))));
        outerUnnestOp.getInputs().add(new MutableObject<ILogicalOperator>(subplanOp));
        currentTopOp = outerUnnestOp;
        if (hasRightPosVar) {
            ScalarFunctionCallExpression fieldAccessForRightUnnestVar = new ScalarFunctionCallExpression(FunctionUtil.getFunctionInfo(BuiltinFunctions.FIELD_ACCESS_BY_INDEX), new MutableObject<ILogicalExpression>(new VariableReferenceExpression(outerUnnestVar)), new MutableObject<ILogicalExpression>(new ConstantExpression(new AsterixConstantValue(new AInt32(0)))));
            ScalarFunctionCallExpression fieldAccessForRightPosVar = new ScalarFunctionCallExpression(FunctionUtil.getFunctionInfo(BuiltinFunctions.FIELD_ACCESS_BY_INDEX), new MutableObject<ILogicalExpression>(new VariableReferenceExpression(outerUnnestVar)), new MutableObject<ILogicalExpression>(new ConstantExpression(new AsterixConstantValue(new AInt32(1)))));
            // Creates variables for assign.
            LogicalVariable rightUnnestVar = context.newVar();
            LogicalVariable rightPosVar = context.newVar();
            // Relate the assigned variables to the variable expression in AST.
            context.setVar(joinClause.getRightVariable(), rightUnnestVar);
            context.setVar(joinClause.getPositionalVariable(), rightPosVar);
            // Varaibles to assign.
            List<LogicalVariable> assignVars = new ArrayList<>();
            assignVars.add(rightUnnestVar);
            assignVars.add(rightPosVar);
            // Expressions for assign.
            List<Mutable<ILogicalExpression>> assignExprs = new ArrayList<>();
            assignExprs.add(new MutableObject<ILogicalExpression>(fieldAccessForRightUnnestVar));
            assignExprs.add(new MutableObject<ILogicalExpression>(fieldAccessForRightPosVar));
            // Creates the assign operator.
            AssignOperator assignOp = new AssignOperator(assignVars, assignExprs);
            assignOp.getInputs().add(new MutableObject<ILogicalOperator>(currentTopOp));
            currentTopOp = assignOp;
        } else {
            context.setVar(joinClause.getRightVariable(), outerUnnestVar);
        }
        return new Pair<>(currentTopOp, null);
    }
}
Also used : NestedTupleSourceOperator(org.apache.hyracks.algebricks.core.algebra.operators.logical.NestedTupleSourceOperator) AbstractUnnestNonMapOperator(org.apache.hyracks.algebricks.core.algebra.operators.logical.AbstractUnnestNonMapOperator) ConstantExpression(org.apache.hyracks.algebricks.core.algebra.expressions.ConstantExpression) ArrayList(java.util.ArrayList) InnerJoinOperator(org.apache.hyracks.algebricks.core.algebra.operators.logical.InnerJoinOperator) AbstractBinaryJoinOperator(org.apache.hyracks.algebricks.core.algebra.operators.logical.AbstractBinaryJoinOperator) SubplanOperator(org.apache.hyracks.algebricks.core.algebra.operators.logical.SubplanOperator) SelectOperator(org.apache.hyracks.algebricks.core.algebra.operators.logical.SelectOperator) AsterixConstantValue(org.apache.asterix.om.constants.AsterixConstantValue) ALogicalPlanImpl(org.apache.hyracks.algebricks.core.algebra.plan.ALogicalPlanImpl) AggregateOperator(org.apache.hyracks.algebricks.core.algebra.operators.logical.AggregateOperator) AString(org.apache.asterix.om.base.AString) MutableObject(org.apache.commons.lang3.mutable.MutableObject) GbyVariableExpressionPair(org.apache.asterix.lang.common.expression.GbyVariableExpressionPair) Pair(org.apache.hyracks.algebricks.common.utils.Pair) ScalarFunctionCallExpression(org.apache.hyracks.algebricks.core.algebra.expressions.ScalarFunctionCallExpression) LogicalVariable(org.apache.hyracks.algebricks.core.algebra.base.LogicalVariable) AggregateFunctionCallExpression(org.apache.hyracks.algebricks.core.algebra.expressions.AggregateFunctionCallExpression) ILogicalOperator(org.apache.hyracks.algebricks.core.algebra.base.ILogicalOperator) LeftOuterUnnestOperator(org.apache.hyracks.algebricks.core.algebra.operators.logical.LeftOuterUnnestOperator) AssignOperator(org.apache.hyracks.algebricks.core.algebra.operators.logical.AssignOperator) AInt32(org.apache.asterix.om.base.AInt32) Mutable(org.apache.commons.lang3.mutable.Mutable) ILogicalExpression(org.apache.hyracks.algebricks.core.algebra.base.ILogicalExpression) VariableReferenceExpression(org.apache.hyracks.algebricks.core.algebra.expressions.VariableReferenceExpression) ILogicalPlan(org.apache.hyracks.algebricks.core.algebra.base.ILogicalPlan)

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