use of org.apache.asterix.lang.sqlpp.clause.SelectElement in project asterixdb by apache.
the class SqlppGroupBySugarVisitor method wrapAggregationArgument.
private Expression wrapAggregationArgument(Expression argExpr) throws CompilationException {
Expression expr = argExpr;
Set<VariableExpr> freeVars = SqlppRewriteUtil.getFreeVariable(expr);
VariableExpr fromBindingVar = new VariableExpr(context.newVariable());
FromTerm fromTerm = new FromTerm(groupVar, fromBindingVar, null, null);
FromClause fromClause = new FromClause(Collections.singletonList(fromTerm));
// Maps field variable expressions to field accesses.
Map<Expression, Expression> varExprMap = new HashMap<>();
for (VariableExpr usedVar : freeVars) {
// Reference to a field in the group variable.
if (fieldVars.contains(usedVar)) {
// Rewrites to a reference to a field in the group variable.
varExprMap.put(usedVar, new FieldAccessor(fromBindingVar, SqlppVariableUtil.toUserDefinedVariableName(usedVar.getVar())));
}
}
// Select clause.
SelectElement selectElement = new SelectElement(SqlppRewriteUtil.substituteExpression(expr, varExprMap, context));
SelectClause selectClause = new SelectClause(selectElement, null, false);
// Construct the select expression.
SelectBlock selectBlock = new SelectBlock(selectClause, fromClause, null, null, null, null, null);
SelectSetOperation selectSetOperation = new SelectSetOperation(new SetOperationInput(selectBlock, null), null);
return new SelectExpression(null, selectSetOperation, null, null, true);
}
use of org.apache.asterix.lang.sqlpp.clause.SelectElement in project asterixdb by apache.
the class SetOperationVisitor method visit.
@Override
public Expression visit(SelectExpression selectExpression, ILangExpression arg) throws CompilationException {
// Recursively visit nested select expressions.
SelectSetOperation selectSetOperation = selectExpression.getSelectSetOperation();
if (!selectSetOperation.hasRightInputs() || !(selectExpression.hasOrderby() || selectExpression.hasLimit())) {
return super.visit(selectExpression, arg);
}
OrderbyClause orderBy = selectExpression.getOrderbyClause();
LimitClause limit = selectExpression.getLimitClause();
// Wraps the set operation part with a subquery.
SelectExpression nestedSelectExpression = new SelectExpression(null, selectSetOperation, null, null, true);
// Binding variable for the subquery.
VariableExpr newBindingVar = new VariableExpr(context.newVariable());
FromTerm newFromTerm = new FromTerm(nestedSelectExpression, newBindingVar, null, null);
FromClause newFromClause = new FromClause(new ArrayList<>(Collections.singletonList(newFromTerm)));
SelectClause selectClause = new SelectClause(new SelectElement(newBindingVar), null, false);
SelectBlock selectBlock = new SelectBlock(selectClause, newFromClause, null, null, null, null, null);
SelectSetOperation newSelectSetOperation = new SelectSetOperation(new SetOperationInput(selectBlock, null), null);
// Puts together the generated select-from-where query and order by/limit.
SelectExpression newSelectExpression = new SelectExpression(selectExpression.getLetList(), newSelectSetOperation, orderBy, limit, selectExpression.isSubquery());
return super.visit(newSelectExpression, arg);
}
use of org.apache.asterix.lang.sqlpp.clause.SelectElement in project asterixdb by apache.
the class SqlppDeleteRewriteVisitor method visit.
@Override
public Void visit(DeleteStatement deleteStmt, Void visitArg) {
List<Expression> arguments = new ArrayList<>();
Identifier dataverseName = deleteStmt.getDataverseName();
Identifier datasetName = deleteStmt.getDatasetName();
String arg = dataverseName == null ? datasetName.getValue() : dataverseName.getValue() + "." + datasetName.getValue();
LiteralExpr argumentLiteral = new LiteralExpr(new StringLiteral(arg));
arguments.add(argumentLiteral);
CallExpr callExpression = new CallExpr(new FunctionSignature(FunctionConstants.ASTERIX_NS, "dataset", 1), arguments);
// From clause.
VariableExpr var = deleteStmt.getVariableExpr();
FromTerm fromTerm = new FromTerm(callExpression, var, null, null);
@SuppressWarnings("unchecked") FromClause fromClause = new FromClause(Collections.singletonList(fromTerm));
// Where clause.
WhereClause whereClause = null;
Expression condition = deleteStmt.getCondition();
if (condition != null) {
whereClause = new WhereClause(condition);
}
// Select clause.
VariableExpr returnExpr = new VariableExpr(var.getVar());
returnExpr.setIsNewVar(false);
SelectElement selectElement = new SelectElement(returnExpr);
SelectClause selectClause = new SelectClause(selectElement, null, false);
// Construct the select expression.
SelectBlock selectBlock = new SelectBlock(selectClause, fromClause, null, whereClause, null, null, null);
SelectSetOperation selectSetOperation = new SelectSetOperation(new SetOperationInput(selectBlock, null), null);
SelectExpression selectExpression = new SelectExpression(null, selectSetOperation, null, null, false);
Query query = new Query(false, false, selectExpression, 0);
query.setBody(selectExpression);
// return the delete statement.
deleteStmt.setQuery(query);
return null;
}
use of org.apache.asterix.lang.sqlpp.clause.SelectElement in project asterixdb by apache.
the class DeepCopyVisitor method visit.
@Override
public SelectClause visit(SelectClause selectClause, Void arg) throws CompilationException {
SelectElement selectElement = null;
SelectRegular selectRegular = null;
if (selectClause.selectElement()) {
selectElement = (SelectElement) selectClause.getSelectElement().accept(this, arg);
}
if (selectClause.selectRegular()) {
selectRegular = (SelectRegular) selectClause.getSelectRegular().accept(this, arg);
}
return new SelectClause(selectElement, selectRegular, selectClause.distinct());
}
Aggregations