use of org.apache.asterix.lang.sqlpp.clause.FromTerm 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.FromTerm 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.FromTerm in project asterixdb by apache.
the class SqlppCloneAndSubstituteVariablesVisitor method visit.
@Override
public Pair<ILangExpression, VariableSubstitutionEnvironment> visit(FromClause fromClause, VariableSubstitutionEnvironment env) throws CompilationException {
VariableSubstitutionEnvironment currentEnv = new VariableSubstitutionEnvironment(env);
List<FromTerm> newFromTerms = new ArrayList<>();
for (FromTerm fromTerm : fromClause.getFromTerms()) {
Pair<ILangExpression, VariableSubstitutionEnvironment> p = fromTerm.accept(this, currentEnv);
newFromTerms.add((FromTerm) p.first);
// A right from term could be correlated from a left from term,
// therefore we propagate the substitution environment.
currentEnv = p.second;
}
return new Pair<>(new FromClause(newFromTerms), currentEnv);
}
use of org.apache.asterix.lang.sqlpp.clause.FromTerm in project asterixdb by apache.
the class SqlppCloneAndSubstituteVariablesVisitor method visit.
@Override
public Pair<ILangExpression, VariableSubstitutionEnvironment> visit(FromTerm fromTerm, VariableSubstitutionEnvironment env) throws CompilationException {
VariableExpr leftVar = fromTerm.getLeftVariable();
VariableExpr newLeftVar = generateNewVariable(context, leftVar);
VariableExpr newLeftPosVar = fromTerm.hasPositionalVariable() ? generateNewVariable(context, fromTerm.getPositionalVariable()) : null;
Expression newLeftExpr = (Expression) visitUnnesBindingExpression(fromTerm.getLeftExpression(), env).first;
List<AbstractBinaryCorrelateClause> newCorrelateClauses = new ArrayList<>();
VariableSubstitutionEnvironment currentEnv = new VariableSubstitutionEnvironment(env);
currentEnv.removeSubstitution(newLeftVar);
if (newLeftPosVar != null) {
currentEnv.removeSubstitution(newLeftPosVar);
}
for (AbstractBinaryCorrelateClause correlateClause : fromTerm.getCorrelateClauses()) {
if (correlateClause.getClauseType() == ClauseType.UNNEST_CLAUSE) {
// The right-hand-side of unnest could be correlated with the left side,
// therefore we propagate the substitution environment of the left-side.
Pair<ILangExpression, VariableSubstitutionEnvironment> p = correlateClause.accept(this, currentEnv);
currentEnv = p.second;
newCorrelateClauses.add((AbstractBinaryCorrelateClause) p.first);
} else {
// The right-hand-side of join and nest could not be correlated with the left side,
// therefore we propagate the original substitution environment.
newCorrelateClauses.add((AbstractBinaryCorrelateClause) correlateClause.accept(this, env).first);
// Join binding variables should be removed for further traversal.
currentEnv.removeSubstitution(correlateClause.getRightVariable());
if (correlateClause.hasPositionalVariable()) {
currentEnv.removeSubstitution(correlateClause.getPositionalVariable());
}
}
}
return new Pair<>(new FromTerm(newLeftExpr, newLeftVar, newLeftPosVar, newCorrelateClauses), currentEnv);
}
use of org.apache.asterix.lang.sqlpp.clause.FromTerm 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