use of org.apache.asterix.om.constants.AsterixConstantValue in project asterixdb by apache.
the class LangExpressionToPlanTranslator method visit.
@Override
public Pair<ILogicalOperator, LogicalVariable> visit(LiteralExpr l, Mutable<ILogicalOperator> tupSource) {
LogicalVariable var = context.newVar();
AssignOperator a = new AssignOperator(var, new MutableObject<>(new ConstantExpression(new AsterixConstantValue(ConstantHelper.objectFromLiteral(l.getValue())))));
if (tupSource != null) {
a.getInputs().add(tupSource);
}
return new Pair<>(a, var);
}
use of org.apache.asterix.om.constants.AsterixConstantValue in project asterixdb by apache.
the class LangExpressionToPlanTranslator method langExprToAlgExpression.
protected Pair<ILogicalExpression, Mutable<ILogicalOperator>> langExprToAlgExpression(Expression expr, Mutable<ILogicalOperator> topOpRef) throws CompilationException {
switch(expr.getKind()) {
case VARIABLE_EXPRESSION:
VariableReferenceExpression ve = new VariableReferenceExpression(context.getVar(((VariableExpr) expr).getVar().getId()));
return new Pair<>(ve, topOpRef);
case LITERAL_EXPRESSION:
LiteralExpr val = (LiteralExpr) expr;
return new Pair<>(new ConstantExpression(new AsterixConstantValue(ConstantHelper.objectFromLiteral(val.getValue()))), topOpRef);
default:
if (expressionNeedsNoNesting(expr)) {
Pair<ILogicalOperator, LogicalVariable> p = expr.accept(this, topOpRef);
ILogicalExpression exp = ((AssignOperator) p.first).getExpressions().get(0).getValue();
return new Pair<>(exp, p.first.getInputs().get(0));
} else {
Mutable<ILogicalOperator> srcRef = new MutableObject<>();
Pair<ILogicalOperator, LogicalVariable> p = expr.accept(this, srcRef);
if (p.first.getOperatorTag() == LogicalOperatorTag.SUBPLAN) {
if (topOpRef.getValue() != null) {
srcRef.setValue(topOpRef.getValue());
} else {
// Re-binds the bottom operator reference to {@code topOpRef}.
rebindBottomOpRef(p.first, srcRef, topOpRef);
}
Mutable<ILogicalOperator> top2 = new MutableObject<>(p.first);
return new Pair<>(new VariableReferenceExpression(p.second), top2);
} else {
SubplanOperator s = new SubplanOperator();
s.getInputs().add(topOpRef);
srcRef.setValue(new NestedTupleSourceOperator(new MutableObject<>(s)));
Mutable<ILogicalOperator> planRoot = new MutableObject<>(p.first);
s.setRootOp(planRoot);
return new Pair<>(new VariableReferenceExpression(p.second), new MutableObject<>(s));
}
}
}
}
use of org.apache.asterix.om.constants.AsterixConstantValue in project asterixdb by apache.
the class SqlppExpressionToPlanTranslator method visit.
@Override
public Pair<ILogicalOperator, LogicalVariable> visit(CaseExpression caseExpression, Mutable<ILogicalOperator> tupSource) throws CompilationException {
//Creates a series of subplan operators, one for each branch.
Mutable<ILogicalOperator> currentOpRef = tupSource;
ILogicalOperator currentOperator = null;
List<Expression> whenExprList = caseExpression.getWhenExprs();
List<Expression> thenExprList = caseExpression.getThenExprs();
List<ILogicalExpression> branchCondVarReferences = new ArrayList<>();
List<ILogicalExpression> allVarReferences = new ArrayList<>();
for (int index = 0; index < whenExprList.size(); ++index) {
Pair<ILogicalOperator, LogicalVariable> whenExprResult = whenExprList.get(index).accept(this, currentOpRef);
currentOperator = whenExprResult.first;
// Variable whenConditionVar is corresponds to the current "WHEN" condition.
LogicalVariable whenConditionVar = whenExprResult.second;
Mutable<ILogicalExpression> branchEntraceConditionExprRef = new MutableObject<>(new VariableReferenceExpression(whenConditionVar));
// even though multiple "WHEN" conditions can be satisfied.
if (!branchCondVarReferences.isEmpty()) {
// The additional filter generated here makes sure the the tuple has not
// entered other matched "WHEN...THEN" case.
List<Mutable<ILogicalExpression>> andArgs = new ArrayList<>();
andArgs.add(generateNoMatchedPrecedingWhenBranchesFilter(branchCondVarReferences));
andArgs.add(branchEntraceConditionExprRef);
// A "THEN" branch can be entered only when the tuple has not enter any other preceding
// branches and the current "WHEN" condition is TRUE.
branchEntraceConditionExprRef = new MutableObject<>(new ScalarFunctionCallExpression(FunctionUtil.getFunctionInfo(BuiltinFunctions.AND), andArgs));
}
// Translates the corresponding "THEN" expression.
Pair<ILogicalOperator, LogicalVariable> opAndVarForThen = constructSubplanOperatorForBranch(currentOperator, branchEntraceConditionExprRef, thenExprList.get(index));
branchCondVarReferences.add(new VariableReferenceExpression(whenConditionVar));
allVarReferences.add(new VariableReferenceExpression(whenConditionVar));
allVarReferences.add(new VariableReferenceExpression(opAndVarForThen.second));
currentOperator = opAndVarForThen.first;
currentOpRef = new MutableObject<>(currentOperator);
}
// Creates a subplan for the "ELSE" branch.
Mutable<ILogicalExpression> elseCondExprRef = generateNoMatchedPrecedingWhenBranchesFilter(branchCondVarReferences);
Pair<ILogicalOperator, LogicalVariable> opAndVarForElse = constructSubplanOperatorForBranch(currentOperator, elseCondExprRef, caseExpression.getElseExpr());
// Uses switch-case function to select the results of two branches.
LogicalVariable selectVar = context.newVar();
List<Mutable<ILogicalExpression>> arguments = new ArrayList<>();
arguments.add(new MutableObject<>(new ConstantExpression(new AsterixConstantValue(ABoolean.TRUE))));
for (ILogicalExpression argVar : allVarReferences) {
arguments.add(new MutableObject<>(argVar));
}
arguments.add(new MutableObject<>(new VariableReferenceExpression(opAndVarForElse.second)));
AbstractFunctionCallExpression swithCaseExpr = new ScalarFunctionCallExpression(FunctionUtil.getFunctionInfo(BuiltinFunctions.SWITCH_CASE), arguments);
AssignOperator assignOp = new AssignOperator(selectVar, new MutableObject<>(swithCaseExpr));
assignOp.getInputs().add(new MutableObject<>(opAndVarForElse.first));
// Unnests the selected (a "THEN" or "ELSE" branch) result.
LogicalVariable unnestVar = context.newVar();
UnnestOperator unnestOp = new UnnestOperator(unnestVar, new MutableObject<>(new UnnestingFunctionCallExpression(FunctionUtil.getFunctionInfo(BuiltinFunctions.SCAN_COLLECTION), Collections.singletonList(new MutableObject<>(new VariableReferenceExpression(selectVar))))));
unnestOp.getInputs().add(new MutableObject<>(assignOp));
// Produces the final assign operator.
LogicalVariable resultVar = context.newVar();
AssignOperator finalAssignOp = new AssignOperator(resultVar, new MutableObject<>(new VariableReferenceExpression(unnestVar)));
finalAssignOp.getInputs().add(new MutableObject<>(unnestOp));
return new Pair<>(finalAssignOp, resultVar);
}
use of org.apache.asterix.om.constants.AsterixConstantValue 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);
}
}
use of org.apache.asterix.om.constants.AsterixConstantValue 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)));
}
}
Aggregations