use of org.apache.asterix.lang.sqlpp.clause.SelectClause in project asterixdb by apache.
the class SqlppExpressionToPlanTranslator method processSelectClause.
// Generates the return expression for a select clause.
private Pair<ILogicalOperator, LogicalVariable> processSelectClause(SelectBlock selectBlock, Mutable<ILogicalOperator> tupSrc) throws CompilationException {
SelectClause selectClause = selectBlock.getSelectClause();
Expression returnExpr;
if (selectClause.selectElement()) {
returnExpr = selectClause.getSelectElement().getExpression();
} else {
returnExpr = generateReturnExpr(selectClause, selectBlock);
}
Pair<ILogicalExpression, Mutable<ILogicalOperator>> eo = langExprToAlgExpression(returnExpr, tupSrc);
LogicalVariable returnVar;
ILogicalOperator returnOperator;
if (returnExpr.getKind() == Kind.VARIABLE_EXPRESSION) {
VariableExpr varExpr = (VariableExpr) returnExpr;
returnOperator = eo.second.getValue();
returnVar = context.getVar(varExpr.getVar().getId());
} else {
returnVar = context.newVar();
returnOperator = new AssignOperator(returnVar, new MutableObject<ILogicalExpression>(eo.first));
returnOperator.getInputs().add(eo.second);
}
if (selectClause.distinct()) {
DistinctOperator distinctOperator = new DistinctOperator(mkSingletonArrayList(new MutableObject<ILogicalExpression>(new VariableReferenceExpression(returnVar))));
distinctOperator.getInputs().add(new MutableObject<ILogicalOperator>(returnOperator));
return new Pair<>(distinctOperator, returnVar);
} else {
return new Pair<>(returnOperator, returnVar);
}
}
use of org.apache.asterix.lang.sqlpp.clause.SelectClause 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.SelectClause in project asterixdb by apache.
the class SqlppGroupByVisitor method visit.
@Override
public Expression visit(SelectBlock selectBlock, ILangExpression arg) throws CompilationException {
// Traverses the select block in the order of "from", "let"s, "where",
// "group by", "let"s, "having" and "select".
FromClause fromClause = selectBlock.getFromClause();
if (selectBlock.hasFromClause()) {
fromClause.accept(this, arg);
}
if (selectBlock.hasLetClauses()) {
List<LetClause> letList = selectBlock.getLetList();
for (LetClause letClause : letList) {
letClause.accept(this, arg);
}
}
if (selectBlock.hasWhereClause()) {
selectBlock.getWhereClause().accept(this, arg);
}
if (selectBlock.hasGroupbyClause()) {
GroupbyClause groupbyClause = selectBlock.getGroupbyClause();
groupbyClause.accept(this, fromClause);
Collection<VariableExpr> visibleVarsInCurrentScope = SqlppVariableUtil.getBindingVariables(groupbyClause);
VariableExpr groupVar = groupbyClause.getGroupVar();
Set<VariableExpr> groupFieldVars = getGroupFieldVariables(groupbyClause);
Collection<VariableExpr> freeVariablesInGbyLets = new HashSet<>();
if (selectBlock.hasLetClausesAfterGroupby()) {
List<LetClause> letListAfterGby = selectBlock.getLetListAfterGroupby();
for (LetClause letClauseAfterGby : letListAfterGby) {
letClauseAfterGby.accept(this, arg);
// Rewrites each let clause after the group-by.
SqlppRewriteUtil.rewriteExpressionUsingGroupVariable(groupVar, groupFieldVars, letClauseAfterGby, context);
Collection<VariableExpr> freeVariablesInLet = SqlppVariableUtil.getFreeVariables(letClauseAfterGby.getBindingExpr());
freeVariablesInLet.removeAll(visibleVarsInCurrentScope);
freeVariablesInGbyLets.addAll(freeVariablesInLet);
visibleVarsInCurrentScope.add(letClauseAfterGby.getVarExpr());
}
}
Collection<VariableExpr> freeVariables = new HashSet<>();
if (selectBlock.hasHavingClause()) {
// Rewrites the having clause.
HavingClause havingClause = selectBlock.getHavingClause();
havingClause.accept(this, arg);
SqlppRewriteUtil.rewriteExpressionUsingGroupVariable(groupVar, groupFieldVars, havingClause, context);
freeVariables.addAll(SqlppVariableUtil.getFreeVariables(havingClause));
}
SelectExpression parentSelectExpression = (SelectExpression) arg;
// We cannot rewrite ORDER BY and LIMIT if it's a SET operation query.
if (!parentSelectExpression.getSelectSetOperation().hasRightInputs()) {
if (parentSelectExpression.hasOrderby()) {
// Rewrites the ORDER BY clause.
OrderbyClause orderbyClause = parentSelectExpression.getOrderbyClause();
orderbyClause.accept(this, arg);
SqlppRewriteUtil.rewriteExpressionUsingGroupVariable(groupVar, groupFieldVars, orderbyClause, context);
freeVariables.addAll(SqlppVariableUtil.getFreeVariables(orderbyClause));
}
if (parentSelectExpression.hasLimit()) {
// Rewrites the LIMIT clause.
LimitClause limitClause = parentSelectExpression.getLimitClause();
limitClause.accept(this, arg);
SqlppRewriteUtil.rewriteExpressionUsingGroupVariable(groupVar, groupFieldVars, limitClause, context);
freeVariables.addAll(SqlppVariableUtil.getFreeVariables(limitClause));
}
}
// Visits the select clause.
SelectClause selectClause = selectBlock.getSelectClause();
selectClause.accept(this, arg);
// Rewrites the select clause.
SqlppRewriteUtil.rewriteExpressionUsingGroupVariable(groupVar, groupFieldVars, selectClause, context);
freeVariables.addAll(SqlppVariableUtil.getFreeVariables(selectClause));
freeVariables.removeAll(visibleVarsInCurrentScope);
// Gets the final free variables.
freeVariables.addAll(freeVariablesInGbyLets);
// Gets outer scope variables.
Collection<VariableExpr> decorVars = SqlppVariableUtil.getLiveVariables(scopeChecker.getCurrentScope(), true);
decorVars.removeAll(visibleVarsInCurrentScope);
// Need path resolution or not?
boolean needResolution = !decorVars.containsAll(freeVariables);
// Otherwise, we only need to retain used free variables.
if (needResolution) {
// Tracks used variables, including WITH variables.
decorVars.retainAll(freeVariables);
// Adds all non-WITH outer scope variables, for path resolution.
Collection<VariableExpr> visibleOuterScopeNonWithVars = SqlppVariableUtil.getLiveVariables(scopeChecker.getCurrentScope(), false);
visibleOuterScopeNonWithVars.removeAll(visibleVarsInCurrentScope);
decorVars.addAll(visibleOuterScopeNonWithVars);
} else {
// Only retains used free variables.
decorVars.retainAll(freeVariables);
}
if (!decorVars.isEmpty()) {
// Adds used WITH variables.
Collection<VariableExpr> visibleOuterScopeNonWithVars = SqlppVariableUtil.getLiveVariables(scopeChecker.getCurrentScope(), false);
visibleOuterScopeNonWithVars.retainAll(freeVariables);
decorVars.addAll(visibleOuterScopeNonWithVars);
// Adds necessary decoration variables for the GROUP BY.
// NOTE: we need to include WITH binding variables so as they can be evaluated before
// the GROUP BY instead of being inlined as part of nested pipepline. The current optimzier
// is not able to optimize the latter case. The following query is such an example:
// asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/dapd/q2-11
List<GbyVariableExpressionPair> decorList = new ArrayList<>();
for (VariableExpr var : decorVars) {
decorList.add(new GbyVariableExpressionPair((VariableExpr) SqlppRewriteUtil.deepCopy(var), (Expression) SqlppRewriteUtil.deepCopy(var)));
}
groupbyClause.getDecorPairList().addAll(decorList);
}
} else {
selectBlock.getSelectClause().accept(this, arg);
}
return null;
}
use of org.apache.asterix.lang.sqlpp.clause.SelectClause 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.SelectClause 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;
}
Aggregations