use of org.apache.asterix.lang.common.expression.LiteralExpr in project asterixdb by apache.
the class SqlppExpressionToPlanTranslator method generateReturnExpr.
// Generates the return expression for a select clause.
private Expression generateReturnExpr(SelectClause selectClause, SelectBlock selectBlock) {
SelectRegular selectRegular = selectClause.getSelectRegular();
List<FieldBinding> fieldBindings = new ArrayList<>();
List<Projection> projections = selectRegular.getProjections();
for (Projection projection : projections) {
if (projection.star()) {
if (selectBlock.hasGroupbyClause()) {
fieldBindings.addAll(getGroupBindings(selectBlock.getGroupbyClause()));
} else if (selectBlock.hasFromClause()) {
fieldBindings.addAll(getFromBindings(selectBlock.getFromClause()));
}
} else {
fieldBindings.add(new FieldBinding(new LiteralExpr(new StringLiteral(projection.getName())), projection.getExpression()));
}
}
return new RecordConstructor(fieldBindings);
}
use of org.apache.asterix.lang.common.expression.LiteralExpr in project asterixdb by apache.
the class LangExpressionToPlanTranslator method visit.
@Override
public Pair<ILogicalOperator, LogicalVariable> visit(GroupbyClause gc, Mutable<ILogicalOperator> tupSource) throws CompilationException {
Mutable<ILogicalOperator> topOp = tupSource;
if (gc.hasGroupVar()) {
List<Pair<Expression, Identifier>> groupFieldList = gc.getGroupFieldList();
List<Mutable<ILogicalExpression>> groupRecordConstructorArgList = new ArrayList<>();
for (Pair<Expression, Identifier> groupField : groupFieldList) {
ILogicalExpression groupFieldNameExpr = langExprToAlgExpression(new LiteralExpr(new StringLiteral(groupField.second.getValue())), topOp).first;
groupRecordConstructorArgList.add(new MutableObject<>(groupFieldNameExpr));
ILogicalExpression groupFieldExpr = langExprToAlgExpression(groupField.first, topOp).first;
groupRecordConstructorArgList.add(new MutableObject<>(groupFieldExpr));
}
LogicalVariable groupVar = context.newVarFromExpression(gc.getGroupVar());
AssignOperator groupVarAssignOp = new AssignOperator(groupVar, new MutableObject<>(new ScalarFunctionCallExpression(FunctionUtil.getFunctionInfo(BuiltinFunctions.OPEN_RECORD_CONSTRUCTOR), groupRecordConstructorArgList)));
groupVarAssignOp.getInputs().add(topOp);
topOp = new MutableObject<>(groupVarAssignOp);
}
GroupByOperator gOp = new GroupByOperator();
for (GbyVariableExpressionPair ve : gc.getGbyPairList()) {
VariableExpr vexpr = ve.getVar();
LogicalVariable v = vexpr == null ? context.newVar() : context.newVarFromExpression(vexpr);
Pair<ILogicalExpression, Mutable<ILogicalOperator>> eo = langExprToAlgExpression(ve.getExpr(), topOp);
gOp.addGbyExpression(v, eo.first);
topOp = eo.second;
}
for (GbyVariableExpressionPair ve : gc.getDecorPairList()) {
VariableExpr vexpr = ve.getVar();
LogicalVariable v = vexpr == null ? context.newVar() : context.newVarFromExpression(vexpr);
Pair<ILogicalExpression, Mutable<ILogicalOperator>> eo = langExprToAlgExpression(ve.getExpr(), topOp);
gOp.addDecorExpression(v, eo.first);
topOp = eo.second;
}
gOp.getInputs().add(topOp);
for (Entry<Expression, VariableExpr> entry : gc.getWithVarMap().entrySet()) {
Pair<ILogicalExpression, Mutable<ILogicalOperator>> listifyInput = langExprToAlgExpression(entry.getKey(), new MutableObject<>(new NestedTupleSourceOperator(new MutableObject<>(gOp))));
List<Mutable<ILogicalExpression>> flArgs = new ArrayList<>(1);
flArgs.add(new MutableObject<>(listifyInput.first));
AggregateFunctionCallExpression fListify = BuiltinFunctions.makeAggregateFunctionExpression(BuiltinFunctions.LISTIFY, flArgs);
LogicalVariable aggVar = context.newVar();
AggregateOperator agg = new AggregateOperator(mkSingletonArrayList(aggVar), mkSingletonArrayList(new MutableObject<>(fListify)));
agg.getInputs().add(listifyInput.second);
ILogicalPlan plan = new ALogicalPlanImpl(new MutableObject<>(agg));
gOp.getNestedPlans().add(plan);
// Hide the variable that was part of the "with", replacing it with
// the one bound by the aggregation op.
context.setVar(entry.getValue(), aggVar);
}
gOp.setGroupAll(gc.isGroupAll());
gOp.getAnnotations().put(OperatorAnnotations.USE_HASH_GROUP_BY, gc.hasHashGroupByHint());
return new Pair<>(gOp, null);
}
use of org.apache.asterix.lang.common.expression.LiteralExpr in project asterixdb by apache.
the class VariableCheckAndRewriteVisitor method wrapWithDatasetFunction.
private Expression wrapWithDatasetFunction(String dataverseName, String datasetName) throws CompilationException {
String fullyQualifiedName = dataverseName == null ? datasetName : dataverseName + "." + datasetName;
List<Expression> argList = new ArrayList<>();
argList.add(new LiteralExpr(new StringLiteral(fullyQualifiedName)));
return new CallExpr(datasetFunction, argList);
}
use of org.apache.asterix.lang.common.expression.LiteralExpr in project asterixdb by apache.
the class SqlppBuiltinFunctionRewriteVisitor method normalizeCaseExpr.
// Normalizes WHEN expressions so that it can have correct NULL/MISSING semantics as well
// as type promotion semantics.
private CaseExpression normalizeCaseExpr(CaseExpression caseExpr) throws CompilationException {
LiteralExpr trueLiteral = new LiteralExpr(TrueLiteral.INSTANCE);
Expression conditionExpr = caseExpr.getConditionExpr();
if (trueLiteral.equals(conditionExpr)) {
return caseExpr;
}
List<Expression> normalizedWhenExprs = new ArrayList<>();
for (Expression expr : caseExpr.getWhenExprs()) {
OperatorExpr operatorExpr = new OperatorExpr();
operatorExpr.addOperand((Expression) SqlppRewriteUtil.deepCopy(expr));
operatorExpr.addOperand(caseExpr.getConditionExpr());
operatorExpr.addOperator("=");
normalizedWhenExprs.add(operatorExpr);
}
return new CaseExpression(trueLiteral, normalizedWhenExprs, caseExpr.getThenExprs(), caseExpr.getElseExpr());
}
use of org.apache.asterix.lang.common.expression.LiteralExpr in project asterixdb by apache.
the class AbstractSqlppExpressionScopingVisitor method wrapWithResolveFunction.
// Rewrites for an undefined variable reference, which potentially could be a syntatic sugar.
protected Expression wrapWithResolveFunction(VariableExpr expr, Set<VariableExpr> liveVars) throws CompilationException {
List<Expression> argList = new ArrayList<>();
//Ignore the parser-generated prefix "$" for a dataset.
String varName = SqlppVariableUtil.toUserDefinedVariableName(expr.getVar().getValue()).getValue();
argList.add(new LiteralExpr(new StringLiteral(varName)));
argList.addAll(liveVars);
return new CallExpr(resolveFunction, argList);
}
Aggregations