Aklakan commented on code in PR #2882:
URL: https://github.com/apache/jena/pull/2882#discussion_r1884611607


##########
jena-arq/src/main/java/org/apache/jena/sparql/engine/main/iterator/QueryIterUnion.java:
##########
@@ -51,21 +50,51 @@ public QueryIterUnion(QueryIterator input,
     @Override
     protected QueryIterator nextStage(Binding binding)
     {
-        QueryIterConcat unionQIter = new QueryIterConcat(getExecContext()) ;
-        for (Op subOp : subOps)
-        {
-            subOp = QC.substitute(subOp, binding) ;
-            QueryIterator parent = QueryIterSingleton.create(binding, 
getExecContext()) ;
-            QueryIterator qIter = QC.execute(subOp, parent, getExecContext()) ;
-            unionQIter.add(qIter) ;
-        }
-        
-        return unionQIter ;
+        Iterator<Op> subOpIt = subOps.iterator();
+        return new QueryIter(getExecContext()) {
+            QueryIterator qIter = null;
+
+            @Override
+            protected void requestCancel() {
+                performRequestCancel(qIter);
+            }
+
+            @Override
+            protected Binding moveToNextBinding() {
+                return qIter.next();
+            }
+
+            @Override
+            protected boolean hasNextBinding() {
+                for (;;) {
+                    if (qIter != null) {
+                        if (qIter.hasNext()) {
+                            return true;
+                        } else {
+                            qIter.close();
+                            qIter = null;
+                        }
+                    } else {
+                        if (subOpIt.hasNext()) {
+                            Op subOp = subOpIt.next();
+                            qIter = QC.execute(subOp, binding, 
getExecContext());
+                        } else {
+                            return false;
+                        }
+                    }
+                }
+            }

Review Comment:
   Making the nextStage() method of `QueryIterUnion` truly lazy removes the 
need for my previously proposed try-catch blocks: With this change, 
sub-iterators are only opened when needed, and if a creation fails then there 
are no previously opened resources that need to be cleaned up.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: pr-unsubscr...@jena.apache.org

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: pr-unsubscr...@jena.apache.org
For additional commands, e-mail: pr-h...@jena.apache.org

Reply via email to