/** * 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.setSourceLocation(expression.getSourceLocation()); wrapper.setBody(expression); // Creates a substitution visitor. SqlppSubstituteExpressionVisitor visitor = new SqlppSubstituteExpressionVisitor(context, exprMap); wrapper.accept(visitor, wrapper); return wrapper.getBody(); } }
@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); newQ.setSourceLocation(q.getSourceLocation()); return new Pair<>(newQ, p1.second); }
@Override public Query visit(Query q, Void arg) throws CompilationException { Query copy = new Query(q.isExplain(), q.isTopLevel(), (Expression) q.getBody().accept(this, arg), q.getVarCounter()); copy.setSourceLocation(q.getSourceLocation()); return copy; }
query.setSourceLocation(expr.getSourceLocation()); {if (true) return query;} throw new Error("Missing return statement in function");
private Expression rewriteFunctionBody(FunctionDecl fnDecl) throws CompilationException { SourceLocation sourceLoc = fnDecl.getSourceLocation(); Query wrappedQuery = new Query(false); wrappedQuery.setSourceLocation(sourceLoc); wrappedQuery.setBody(fnDecl.getFuncBody()); wrappedQuery.setTopLevel(false); String fnNamespace = fnDecl.getSignature().getNamespace(); Dataverse defaultDataverse = metadataProvider.getDefaultDataverse(); Dataverse fnDataverse; if (fnNamespace == null || fnNamespace.equals(defaultDataverse.getDataverseName())) { fnDataverse = defaultDataverse; } else { try { fnDataverse = metadataProvider.findDataverse(fnNamespace); } catch (AlgebricksException e) { throw new CompilationException(ErrorCode.UNKNOWN_DATAVERSE, e, sourceLoc, fnNamespace); } } metadataProvider.setDefaultDataverse(fnDataverse); try { IQueryRewriter queryRewriter = rewriterFactory.createQueryRewriter(); queryRewriter.rewrite(declaredFunctions, wrappedQuery, metadataProvider, context, true, fnDecl.getParamList()); return wrappedQuery.getBody(); } finally { metadataProvider.setDefaultDataverse(defaultDataverse); } }
wrappedQuery.setSourceLocation(sourceLoc); wrappedQuery.setBody(cfs.getFunctionBodyExpression()); wrappedQuery.setTopLevel(false);
Query query = new Query(false, false, selectExpression, 0); query.setBody(selectExpression); query.setSourceLocation(deleteStmt.getSourceLocation());