use of org.apache.asterix.lang.common.statement.Query 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.common.statement.Query in project asterixdb by apache.
the class SqlppRewriteUtil method substituteExpression.
/**
* Substitutes expression with replacement expressions according to the exprMap.
*
* @param expression
* ,
* an input expression.
* @param exprMap
* a map that maps expressions to their corresponding replacement expressions.
* @return an expression, where sub-expressions of the input expression (including the input expression itself)
* are replaced with deep copies with their mapped replacements in the exprMap if there exists such a
* replacement expression.
* @throws CompilationException
*/
public static Expression substituteExpression(Expression expression, Map<Expression, Expression> exprMap, LangRewritingContext context) throws CompilationException {
if (exprMap.isEmpty()) {
return expression;
}
// Creates a wrapper query for the expression so that if the expression itself
// is the key, it can also be replaced.
Query wrapper = new Query(false);
wrapper.setBody(expression);
// Creates a substitution visitor.
SqlppSubstituteExpressionVisitor visitor = new SqlppSubstituteExpressionVisitor(context, exprMap);
wrapper.accept(visitor, wrapper);
return wrapper.getBody();
}
use of org.apache.asterix.lang.common.statement.Query in project asterixdb by apache.
the class AbstractInlineUdfsVisitor method rewriteFunctionBody.
protected Expression rewriteFunctionBody(Expression expr) throws CompilationException {
Query wrappedQuery = new Query(false);
wrappedQuery.setBody(expr);
wrappedQuery.setTopLevel(false);
IQueryRewriter queryRewriter = rewriterFactory.createQueryRewriter();
queryRewriter.rewrite(declaredFunctions, wrappedQuery, metadataProvider, context);
return wrappedQuery.getBody();
}
use of org.apache.asterix.lang.common.statement.Query in project asterixdb by apache.
the class CloneAndSubstituteVariablesVisitor method visit.
@Override
public Pair<ILangExpression, VariableSubstitutionEnvironment> visit(Query q, VariableSubstitutionEnvironment env) throws CompilationException {
Query newQ = new Query(q.isExplain());
Pair<ILangExpression, VariableSubstitutionEnvironment> p1 = q.getBody().accept(this, env);
newQ.setBody((Expression) p1.first);
return new Pair<>(newQ, p1.second);
}
use of org.apache.asterix.lang.common.statement.Query in project asterixdb by apache.
the class AqlDeleteRewriteVisitor 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);
List<Clause> clauseList = new ArrayList<>();
VariableExpr var = deleteStmt.getVariableExpr();
Clause forClause = new ForClause(var, callExpression);
clauseList.add(forClause);
Clause whereClause = null;
Expression condition = deleteStmt.getCondition();
if (condition != null) {
whereClause = new WhereClause(condition);
clauseList.add(whereClause);
}
VariableExpr returnExpr = new VariableExpr(var.getVar());
returnExpr.setIsNewVar(false);
FLWOGRExpression flowgr = new FLWOGRExpression(clauseList, returnExpr);
Query query = new Query(false);
query.setBody(flowgr);
deleteStmt.setQuery(query);
return null;
}
Aggregations