use of org.apache.asterix.lang.sqlpp.clause.SelectSetOperation 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.SelectSetOperation 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.SelectSetOperation 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.SelectSetOperation in project asterixdb by apache.
the class SqlppCloneAndSubstituteVariablesVisitor method visit.
@Override
public Pair<ILangExpression, VariableSubstitutionEnvironment> visit(SelectSetOperation selectSetOperation, VariableSubstitutionEnvironment env) throws CompilationException {
SetOperationInput leftInput = selectSetOperation.getLeftInput();
SetOperationInput newLeftInput;
Pair<ILangExpression, VariableSubstitutionEnvironment> leftResult;
// Sets the left input.
if (leftInput.selectBlock()) {
leftResult = leftInput.getSelectBlock().accept(this, env);
newLeftInput = new SetOperationInput((SelectBlock) leftResult.first, null);
} else {
leftResult = leftInput.getSubquery().accept(this, env);
newLeftInput = new SetOperationInput(null, (SelectExpression) leftResult.first);
}
// Sets the right input
List<SetOperationRight> newRightInputs = new ArrayList<>();
if (selectSetOperation.hasRightInputs()) {
for (SetOperationRight right : selectSetOperation.getRightInputs()) {
SetOperationInput newRightInput;
SetOperationInput rightInput = right.getSetOperationRightInput();
if (rightInput.selectBlock()) {
Pair<ILangExpression, VariableSubstitutionEnvironment> rightResult = rightInput.getSelectBlock().accept(this, env);
newRightInput = new SetOperationInput((SelectBlock) rightResult.first, null);
} else {
Pair<ILangExpression, VariableSubstitutionEnvironment> rightResult = rightInput.getSubquery().accept(this, env);
newRightInput = new SetOperationInput(null, (SelectExpression) rightResult.first);
}
newRightInputs.add(new SetOperationRight(right.getSetOpType(), right.isSetSemantics(), newRightInput));
}
}
SelectSetOperation newSelectSetOperation = new SelectSetOperation(newLeftInput, newRightInputs);
return new Pair<>(newSelectSetOperation, selectSetOperation.hasRightInputs() ? env : leftResult.second);
}
use of org.apache.asterix.lang.sqlpp.clause.SelectSetOperation in project asterixdb by apache.
the class DeepCopyVisitor method visit.
@Override
public SelectSetOperation visit(SelectSetOperation selectSetOperation, Void arg) throws CompilationException {
SetOperationInput leftInput = selectSetOperation.getLeftInput();
SetOperationInput newLeftInput;
if (leftInput.selectBlock()) {
newLeftInput = new SetOperationInput((SelectBlock) leftInput.accept(this, arg), null);
} else {
newLeftInput = new SetOperationInput(null, (SelectExpression) leftInput.accept(this, arg));
}
List<SetOperationRight> rightInputs = new ArrayList<>();
for (SetOperationRight right : selectSetOperation.getRightInputs()) {
SetOperationInput newRightInput;
SetOperationInput setOpRightInput = right.getSetOperationRightInput();
if (setOpRightInput.selectBlock()) {
newRightInput = new SetOperationInput((SelectBlock) setOpRightInput.accept(this, arg), null);
} else {
newRightInput = new SetOperationInput(null, (SelectExpression) setOpRightInput.accept(this, arg));
}
rightInputs.add(new SetOperationRight(right.getSetOpType(), right.isSetSemantics(), newRightInput));
}
return new SelectSetOperation(newLeftInput, rightInputs);
}
Aggregations