[teiid-commits] teiid SVN: r4306 - in trunk/engine/src: test/java/org/teiid/query/sql/visitor and 1 other directory.

teiid-commits at lists.jboss.org teiid-commits at lists.jboss.org
Tue Aug 7 19:34:56 EDT 2012


Author: shawkins
Date: 2012-08-07 19:34:56 -0400 (Tue, 07 Aug 2012)
New Revision: 4306

Added:
   trunk/engine/src/test/java/org/teiid/query/sql/visitor/TestCorrelatedReferenceCollectorVisitor.java
Modified:
   trunk/engine/src/main/java/org/teiid/query/sql/visitor/CorrelatedReferenceCollectorVisitor.java
Log:
TEIID-1374 Added removal of intermediate groups to ensure that we get a proper set of references.

Modified: trunk/engine/src/main/java/org/teiid/query/sql/visitor/CorrelatedReferenceCollectorVisitor.java
===================================================================
--- trunk/engine/src/main/java/org/teiid/query/sql/visitor/CorrelatedReferenceCollectorVisitor.java	2012-08-07 17:43:21 UTC (rev 4305)
+++ trunk/engine/src/main/java/org/teiid/query/sql/visitor/CorrelatedReferenceCollectorVisitor.java	2012-08-07 23:34:56 UTC (rev 4306)
@@ -23,12 +23,17 @@
 package org.teiid.query.sql.visitor;
 
 import java.util.Collection;
+import java.util.HashSet;
 import java.util.List;
+import java.util.Set;
 
 import org.teiid.query.sql.LanguageObject;
 import org.teiid.query.sql.LanguageVisitor;
-import org.teiid.query.sql.navigator.DeepPreOrderNavigator;
-import org.teiid.query.sql.symbol.*;
+import org.teiid.query.sql.lang.Query;
+import org.teiid.query.sql.navigator.PreOrPostOrderNavigator;
+import org.teiid.query.sql.symbol.ElementSymbol;
+import org.teiid.query.sql.symbol.GroupSymbol;
+import org.teiid.query.sql.symbol.Reference;
 
 
 /**
@@ -79,11 +84,29 @@
      * that the client (outer query) is interested in references to from the correlated subquery
      * @param correlatedReferences List of References collected
      */
-    public static final void collectReferences(LanguageObject obj, Collection<GroupSymbol> groupSymbols, List<Reference> correlatedReferences){
+    public static final void collectReferences(final LanguageObject obj, final Collection<GroupSymbol> groupSymbols, List<Reference> correlatedReferences){
+    	final Set<GroupSymbol> groups = new HashSet<GroupSymbol>(groupSymbols);
+        CorrelatedReferenceCollectorVisitor visitor =
+            new CorrelatedReferenceCollectorVisitor(groups, correlatedReferences);
+        obj.acceptVisitor(new PreOrPostOrderNavigator(visitor, PreOrPostOrderNavigator.PRE_ORDER, true) {
 
-        CorrelatedReferenceCollectorVisitor visitor =
-            new CorrelatedReferenceCollectorVisitor(groupSymbols, correlatedReferences);
-        DeepPreOrderNavigator.doVisit(obj, visitor);
+        	@Override
+        	public void visit(Query query) {
+        		//don't allow confusion with deep nesting by removing intermediate groups
+        		List<GroupSymbol> fromGroups = null;
+				if (query != obj && query.getFrom() != null) {
+					fromGroups = query.getFrom().getGroups();
+        			if (!groups.removeAll(fromGroups)) {
+        				fromGroups = null;
+        			}
+        		}
+    			super.visit(query);
+    			if (fromGroups != null) {
+    				fromGroups.retainAll(groupSymbols);
+    				groups.addAll(fromGroups);
+    			}
+        	}
+        });
     }
 
 }

Added: trunk/engine/src/test/java/org/teiid/query/sql/visitor/TestCorrelatedReferenceCollectorVisitor.java
===================================================================
--- trunk/engine/src/test/java/org/teiid/query/sql/visitor/TestCorrelatedReferenceCollectorVisitor.java	                        (rev 0)
+++ trunk/engine/src/test/java/org/teiid/query/sql/visitor/TestCorrelatedReferenceCollectorVisitor.java	2012-08-07 23:34:56 UTC (rev 4306)
@@ -0,0 +1,56 @@
+/*
+ * JBoss, Home of Professional Open Source.
+ * See the COPYRIGHT.txt file distributed with this work for information
+ * regarding copyright ownership.  Some portions may be licensed
+ * to Red Hat, Inc. under one or more contributor license agreements.
+ * 
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ * 
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ * 
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+ * 02110-1301 USA.
+ */
+
+package org.teiid.query.sql.visitor;
+
+import static org.junit.Assert.*;
+
+import java.util.Arrays;
+import java.util.LinkedList;
+
+import org.junit.Test;
+import org.teiid.query.resolver.TestResolver;
+import org.teiid.query.resolver.util.ResolverUtil;
+import org.teiid.query.rewriter.QueryRewriter;
+import org.teiid.query.sql.lang.Command;
+import org.teiid.query.sql.lang.ExistsCriteria;
+import org.teiid.query.sql.lang.Query;
+import org.teiid.query.sql.symbol.GroupSymbol;
+import org.teiid.query.sql.symbol.Reference;
+import org.teiid.query.unittest.RealMetadataFactory;
+
+ at SuppressWarnings("nls")
+public class TestCorrelatedReferenceCollectorVisitor {
+	
+	@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);
+		assertEquals(1, correlatedReferences.size());
+	}
+
+}


Property changes on: trunk/engine/src/test/java/org/teiid/query/sql/visitor/TestCorrelatedReferenceCollectorVisitor.java
___________________________________________________________________
Added: svn:mime-type
   + text/plain



More information about the teiid-commits mailing list