in asterix-graphix/src/main/java/org/apache/asterix/graphix/lang/rewrite/GraphixQueryRewriter.java [183:245]
public void rewriteGraphixASTNodes(GraphixRewritingContext graphixRewritingContext,
IReturningStatement topStatement, boolean allowNonStoredUDFCalls) throws CompilationException {
// Generate names for unnamed graph elements, projections in our SELECT CLAUSE, and keys in our GROUP BY.
LOGGER.trace("Populating unknowns (both graph and non-graph) in our AST.");
rewriteExpr(topStatement, new PopulateUnknownsVisitor(graphixRewritingContext));
// Verify that variables are properly within scope.
LOGGER.trace("Verifying that variables are unique and are properly scoped.");
rewriteExpr(topStatement, new VariableScopingCheckVisitor(graphixRewritingContext));
// Resolve all of our (Graphix, SQL++, and user-defined) function calls.
LOGGER.trace("Resolving Graphix, SQL++, and user-defined function calls.");
rewriteExpr(topStatement, new FunctionResolutionVisitor(graphixRewritingContext, allowNonStoredUDFCalls));
// Rewrite implicit correlated vertex JOINs as explicit JOINs.
LOGGER.trace("Rewriting correlated implicit vertex JOINs into explicit JOINs.");
rewriteExpr(topStatement, new SubqueryVertexJoinVisitor(graphixRewritingContext));
// Resolve our vertex labels, edge labels, and edge directions.
LOGGER.trace("Performing label and edge direction resolution.");
Map<GraphIdentifier, SchemaKnowledgeTable> knowledgeTableMap = new HashMap<>();
Map<GraphIdentifier, PatternGroup> resolutionPatternMap = new HashMap<>();
topStatement.accept(new PatternGraphGroupVisitor(resolutionPatternMap, graphixRewritingContext) {
@Override
public Expression visit(FromGraphClause fromGraphClause, ILangExpression arg) throws CompilationException {
GraphIdentifier graphIdentifier = fromGraphClause.getGraphIdentifier(metadataProvider);
SchemaKnowledgeTable schemaTable = new SchemaKnowledgeTable(fromGraphClause, graphixRewritingContext);
knowledgeTableMap.put(graphIdentifier, schemaTable);
return super.visit(fromGraphClause, arg);
}
}, null);
for (Map.Entry<GraphIdentifier, PatternGroup> mapEntry : resolutionPatternMap.entrySet()) {
SchemaKnowledgeTable knowledgeTable = knowledgeTableMap.get(mapEntry.getKey());
new ExhaustiveSearchResolver(knowledgeTable).resolve(mapEntry.getValue());
}
// Fetch all relevant graph element declarations, using the element labels.
LOGGER.trace("Fetching relevant edge and vertex bodies from our graph schema.");
ElementLookupTable elementLookupTable = new ElementLookupTable();
ElementLookupTableVisitor elementLookupTableVisitor =
new ElementLookupTableVisitor(graphixRewritingContext, elementLookupTable, parserFactory);
rewriteExpr(topStatement, elementLookupTableVisitor);
for (GraphElementDeclaration graphElementDeclaration : elementLookupTable) {
loadNormalizedGraphElement(graphixRewritingContext, graphElementDeclaration);
}
// Expand / enumerate vertex and edge patterns to snuff out all ambiguities.
LOGGER.trace("Performing a canonicalization pass to expand or enumerate patterns.");
BranchLookupTable branchLookupTable = new BranchLookupTable();
QueryCanonicalizationVisitor queryCanonicalizationVisitor =
new QueryCanonicalizationVisitor(branchLookupTable, graphixRewritingContext);
rewriteExpr(topStatement, queryCanonicalizationVisitor);
// Transform all graph AST nodes (i.e. perform the representation lowering).
LOGGER.trace("Lowering the Graphix AST-specific nodes representation to a SQL++ representation.");
GraphixLoweringVisitor graphixLoweringVisitor =
new GraphixLoweringVisitor(graphixRewritingContext, elementLookupTable, branchLookupTable);
rewriteExpr(topStatement, graphixLoweringVisitor);
// Lower all of our Graphix function calls (and perform schema-enrichment).
LOGGER.trace("Lowering the Graphix CALL-EXPR nodes to a pure SQL++ representation.");
rewriteExpr(topStatement, new GraphixFunctionCallVisitor(graphixRewritingContext));
}