@Test public void testDeepNesting() throws Exception { String sql = "select * from bqt1.smalla where exists (select intnum from bqt1.smalla x where smalla.intnum = x.intnum and exists (select intnum from bqt1.smalla where exists (select intnum from bqt1.smalla x where smalla.intnum = x.intnum)))"; Command command = TestResolver.helpResolve(sql, RealMetadataFactory.exampleBQTCached()); command = QueryRewriter.rewrite(command, RealMetadataFactory.exampleBQTCached(), null); command = ((ExistsCriteria)((Query)command).getCriteria()).getCommand(); LinkedList<Reference> correlatedReferences = new LinkedList<Reference>(); GroupSymbol gs = new GroupSymbol("bqt1.smalla"); ResolverUtil.resolveGroup(gs, RealMetadataFactory.exampleBQTCached()); CorrelatedReferenceCollectorVisitor.collectReferences(command, Arrays.asList(gs), correlatedReferences, RealMetadataFactory.exampleBQTCached()); assertEquals(1, correlatedReferences.size()); }
public static final void collectReferences(final LanguageObject obj, final Collection<GroupSymbol> groupSymbols, List<Reference> correlatedReferences, QueryMetadataInterface metadata) { final Set<GroupSymbol> groups = new HashSet<GroupSymbol>(groupSymbols); final CorrelatedReferenceCollectorVisitor visitor = new CorrelatedReferenceCollectorVisitor(groups, correlatedReferences); visitor.metadata = metadata; visitor.queryRoot = obj instanceof Command; obj.acceptVisitor(new PreOrPostOrderNavigator(visitor, PreOrPostOrderNavigator.PRE_ORDER, true) { @Override public void visit(Query query) { List<GroupSymbol> fromGroups = null; if (query.getFrom() != null) { fromGroups = query.getFrom().getGroups(); HashMap<GroupSymbol, GroupSymbol> groupMap = new HashMap<GroupSymbol, GroupSymbol>(); for (GroupSymbol g : fromGroups) { groupMap.put(g, g); } visitor.outerGroups.add(groupMap); } else { visitor.outerGroups.add(Collections.emptyMap()); } super.visit(query); visitor.outerGroups.remove(visitor.outerGroups.size() - 1); } }); }
CorrelatedReferenceCollectorVisitor extends LanguageVisitor { public static final void collectReferences(final LanguageObject obj, final Collection<GroupSymbol> groupSymbols, List<Reference> correlatedReferences, QueryMetadataInterface metadata) { final Set<GroupSymbol> groups = new HashSet<GroupSymbol>(groupSymbols); final CorrelatedReferenceCollectorVisitor visitor = new CorrelatedReferenceCollectorVisitor(groups, correlatedReferences); visitor.metadata = metadata; visitor.queryRoot = obj instanceof Command; obj.acceptVisitor(new PreOrPostOrderNavigator(visitor, PreOrPostOrderNavigator.PRE_ORDER, true) { @Override public void visit(Query query) { List<GroupSymbol> fromGroups = null; if (query.getFrom() != null) { fromGroups = query.getFrom().getGroups(); HashMap<GroupSymbol, GroupSymbol> groupMap = new HashMap<GroupSymbol, GroupSymbol>(); for (GroupSymbol g : fromGroups) { groupMap.put(g, g); } visitor.outerGroups.add(groupMap); } else { visitor.outerGroups.add(Collections.emptyMap()); } super.visit(query); visitor.outerGroups.remove(visitor.outerGroups.size() - 1); } }); } }
CorrelatedReferenceCollectorVisitor extends LanguageVisitor { public static final void collectReferences(final LanguageObject obj, final Collection<GroupSymbol> groupSymbols, List<Reference> correlatedReferences, QueryMetadataInterface metadata) { final Set<GroupSymbol> groups = new HashSet<GroupSymbol>(groupSymbols); final CorrelatedReferenceCollectorVisitor visitor = new CorrelatedReferenceCollectorVisitor(groups, correlatedReferences); visitor.metadata = metadata; visitor.queryRoot = obj instanceof Command; obj.acceptVisitor(new PreOrPostOrderNavigator(visitor, PreOrPostOrderNavigator.PRE_ORDER, true) { @Override public void visit(Query query) { List<GroupSymbol> fromGroups = null; if (query.getFrom() != null) { fromGroups = query.getFrom().getGroups(); HashMap<GroupSymbol, GroupSymbol> groupMap = new HashMap<GroupSymbol, GroupSymbol>(); for (GroupSymbol g : fromGroups) { groupMap.put(g, g); } visitor.outerGroups.add(groupMap); } else { visitor.outerGroups.add(Collections.emptyMap()); } super.visit(query); visitor.outerGroups.remove(visitor.outerGroups.size() - 1); } }); } CorrelatedReferenceCollectorVisitor(Collection<GroupSymbol> groupSymbols, List<Reference> correlatedReferences); }
CorrelatedReferenceCollectorVisitor extends LanguageVisitor { public static final void collectReferences(final LanguageObject obj, final Collection<GroupSymbol> groupSymbols, List<Reference> correlatedReferences, QueryMetadataInterface metadata) { final Set<GroupSymbol> groups = new HashSet<GroupSymbol>(groupSymbols); final CorrelatedReferenceCollectorVisitor visitor = new CorrelatedReferenceCollectorVisitor(groups, correlatedReferences); visitor.metadata = metadata; visitor.queryRoot = obj instanceof Command; obj.acceptVisitor(new PreOrPostOrderNavigator(visitor, PreOrPostOrderNavigator.PRE_ORDER, true) { @Override public void visit(Query query) { List<GroupSymbol> fromGroups = null; if (query.getFrom() != null) { fromGroups = query.getFrom().getGroups(); HashMap<GroupSymbol, GroupSymbol> groupMap = new HashMap<GroupSymbol, GroupSymbol>(); for (GroupSymbol g : fromGroups) { groupMap.put(g, g); } visitor.outerGroups.add(groupMap); } else { visitor.outerGroups.add(Collections.emptyMap()); } super.visit(query); visitor.outerGroups.remove(visitor.outerGroups.size() - 1); } }); } CorrelatedReferenceCollectorVisitor(Collection<GroupSymbol> groupSymbols, List<Reference> correlatedReferences); List<Reference> getReferences(); void visit(Reference obj); static final void collectReferences(final LanguageObject obj, final Collection<GroupSymbol> groupSymbols, List<Reference> correlatedReferences, QueryMetadataInterface metadata); }
CorrelatedReferenceCollectorVisitor extends LanguageVisitor { public static final void collectReferences(final LanguageObject obj, final Collection<GroupSymbol> groupSymbols, List<Reference> correlatedReferences, QueryMetadataInterface metadata) { final Set<GroupSymbol> groups = new HashSet<GroupSymbol>(groupSymbols); final CorrelatedReferenceCollectorVisitor visitor = new CorrelatedReferenceCollectorVisitor(groups, correlatedReferences); visitor.metadata = metadata; visitor.queryRoot = obj instanceof Command; obj.acceptVisitor(new PreOrPostOrderNavigator(visitor, PreOrPostOrderNavigator.PRE_ORDER, true) { @Override public void visit(Query query) { List<GroupSymbol> fromGroups = null; if (query.getFrom() != null) { fromGroups = query.getFrom().getGroups(); HashMap<GroupSymbol, GroupSymbol> groupMap = new HashMap<GroupSymbol, GroupSymbol>(); for (GroupSymbol g : fromGroups) { groupMap.put(g, g); } visitor.outerGroups.add(groupMap); } else { visitor.outerGroups.add(Collections.emptyMap()); } super.visit(query); visitor.outerGroups.remove(visitor.outerGroups.size() - 1); } }); } CorrelatedReferenceCollectorVisitor(Collection<GroupSymbol> groupSymbols, List<Reference> correlatedReferences); List<Reference> getReferences(); void visit(Reference obj); static final void collectReferences(final LanguageObject obj, final Collection<GroupSymbol> groupSymbols, List<Reference> correlatedReferences, QueryMetadataInterface metadata);  }