Skip to content

Commit 8e6828c

Browse files
committed
GH-4790 describe iteration needs to close the source iteration
1 parent 18e018d commit 8e6828c

3 files changed

Lines changed: 77 additions & 43 deletions

File tree

core/queryalgebra/evaluation/src/main/java/org/eclipse/rdf4j/query/algebra/evaluation/impl/DefaultEvaluationStrategy.java

Lines changed: 47 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -352,50 +352,58 @@ public TupleExpr optimize(TupleExpr expr, EvaluationStatistics evaluationStatist
352352
public CloseableIteration<BindingSet, QueryEvaluationException> evaluate(TupleExpr expr, BindingSet bindings)
353353
throws QueryEvaluationException {
354354

355-
CloseableIteration<BindingSet, QueryEvaluationException> ret;
355+
CloseableIteration<BindingSet, QueryEvaluationException> ret = null;
356356

357-
if (expr instanceof StatementPattern) {
358-
ret = evaluate((StatementPattern) expr, bindings);
359-
} else if (expr instanceof UnaryTupleOperator) {
360-
ret = evaluate((UnaryTupleOperator) expr, bindings);
361-
} else if (expr instanceof BinaryTupleOperator) {
362-
ret = evaluate((BinaryTupleOperator) expr, bindings);
363-
} else if (expr instanceof SingletonSet) {
364-
ret = evaluate((SingletonSet) expr, bindings);
365-
} else if (expr instanceof EmptySet) {
366-
ret = evaluate((EmptySet) expr, bindings);
367-
} else if (expr instanceof ZeroLengthPath) {
368-
ret = evaluate((ZeroLengthPath) expr, bindings);
369-
} else if (expr instanceof ArbitraryLengthPath) {
370-
ret = evaluate((ArbitraryLengthPath) expr, bindings);
371-
} else if (expr instanceof BindingSetAssignment) {
372-
ret = evaluate((BindingSetAssignment) expr, bindings);
373-
} else if (expr instanceof TripleRef) {
374-
ret = evaluate((TripleRef) expr, bindings);
375-
} else if (expr instanceof TupleFunctionCall) {
376-
if (getQueryEvaluationMode().compareTo(QueryEvaluationMode.STANDARD) < 0) {
377-
throw new QueryEvaluationException(
378-
"Tuple function call not supported in query evaluation mode " + getQueryEvaluationMode());
357+
try {
358+
if (expr instanceof StatementPattern) {
359+
ret = evaluate((StatementPattern) expr, bindings);
360+
} else if (expr instanceof UnaryTupleOperator) {
361+
ret = evaluate((UnaryTupleOperator) expr, bindings);
362+
} else if (expr instanceof BinaryTupleOperator) {
363+
ret = evaluate((BinaryTupleOperator) expr, bindings);
364+
} else if (expr instanceof SingletonSet) {
365+
ret = evaluate((SingletonSet) expr, bindings);
366+
} else if (expr instanceof EmptySet) {
367+
ret = evaluate((EmptySet) expr, bindings);
368+
} else if (expr instanceof ZeroLengthPath) {
369+
ret = evaluate((ZeroLengthPath) expr, bindings);
370+
} else if (expr instanceof ArbitraryLengthPath) {
371+
ret = evaluate((ArbitraryLengthPath) expr, bindings);
372+
} else if (expr instanceof BindingSetAssignment) {
373+
ret = evaluate((BindingSetAssignment) expr, bindings);
374+
} else if (expr instanceof TripleRef) {
375+
ret = evaluate((TripleRef) expr, bindings);
376+
} else if (expr instanceof TupleFunctionCall) {
377+
if (getQueryEvaluationMode().compareTo(QueryEvaluationMode.STANDARD) < 0) {
378+
throw new QueryEvaluationException(
379+
"Tuple function call not supported in query evaluation mode " + getQueryEvaluationMode());
380+
}
381+
return evaluate(expr, bindings);
382+
} else if (expr == null) {
383+
throw new IllegalArgumentException("expr must not be null");
384+
} else {
385+
throw new QueryEvaluationException("Unsupported tuple expr type: " + expr.getClass());
379386
}
380-
return evaluate((TupleFunctionCall) expr, bindings);
381-
} else if (expr == null) {
382-
throw new IllegalArgumentException("expr must not be null");
383-
} else {
384-
throw new QueryEvaluationException("Unsupported tuple expr type: " + expr.getClass());
385-
}
386387

387-
if (trackTime) {
388-
// set resultsSizeActual to at least be 0 so we can track iterations that don't procude anything
389-
expr.setTotalTimeNanosActual(Math.max(0, expr.getTotalTimeNanosActual()));
390-
ret = new TimedIterator(ret, expr);
391-
}
388+
if (trackTime) {
389+
// set resultsSizeActual to at least be 0 so we can track iterations that don't procude anything
390+
expr.setTotalTimeNanosActual(Math.max(0, expr.getTotalTimeNanosActual()));
391+
ret = new TimedIterator(ret, expr);
392+
}
392393

393-
if (trackResultSize) {
394-
// set resultsSizeActual to at least be 0 so we can track iterations that don't procude anything
395-
expr.setResultSizeActual(Math.max(0, expr.getResultSizeActual()));
396-
ret = new ResultSizeCountingIterator(ret, expr);
394+
if (trackResultSize) {
395+
// set resultsSizeActual to at least be 0 so we can track iterations that don't procude anything
396+
expr.setResultSizeActual(Math.max(0, expr.getResultSizeActual()));
397+
ret = new ResultSizeCountingIterator(ret, expr);
398+
}
399+
return ret;
400+
401+
} catch (Throwable t) {
402+
if (ret != null) {
403+
ret.close();
404+
}
405+
throw t;
397406
}
398-
return ret;
399407
}
400408

401409
@Override

core/queryalgebra/evaluation/src/main/java/org/eclipse/rdf4j/query/algebra/evaluation/iterator/DescribeIteration.java

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -220,4 +220,21 @@ protected CloseableIteration<BindingSet, QueryEvaluationException> createNextIte
220220
return strategy.evaluate(pattern, parentBindings);
221221
}
222222

223+
@Override
224+
protected void handleClose() throws QueryEvaluationException {
225+
try {
226+
super.handleClose();
227+
228+
} finally {
229+
try {
230+
if (currentDescribeExprIter != null)
231+
currentDescribeExprIter.close();
232+
} finally {
233+
234+
}
235+
if (sourceIter instanceof CloseableIteration) {
236+
((CloseableIteration<?, QueryEvaluationException>) sourceIter).close();
237+
}
238+
}
239+
}
223240
}

tools/federation/src/main/java/org/eclipse/rdf4j/federated/evaluation/FederationEvalStrategy.java

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -872,10 +872,19 @@ public CloseableIteration<BindingSet, QueryEvaluationException> evaluate(Describ
872872
throw new FedXRuntimeException(
873873
"Expected a FedXDescribeOperator Node. Found " + operator.getClass() + " instead.");
874874
}
875-
CloseableIteration<BindingSet, QueryEvaluationException> iter = evaluate(operator.getArg(), bindings);
876-
// Note: we need to evaluate the DESCRIBE over the entire federation
877-
return new FederatedDescribeIteration(iter, this, operator.getBindingNames(), bindings,
878-
((FederatedDescribeOperator) operator).getQueryInfo());
875+
CloseableIteration<BindingSet, QueryEvaluationException> iter = null;
876+
try {
877+
iter = evaluate(operator.getArg(), bindings);
878+
// Note: we need to evaluate the DESCRIBE over the entire federation
879+
return new FederatedDescribeIteration(iter, this, operator.getBindingNames(), bindings,
880+
((FederatedDescribeOperator) operator).getQueryInfo());
881+
} catch (Throwable t) {
882+
if (iter != null) {
883+
iter.close();
884+
}
885+
throw t;
886+
}
887+
879888
}
880889

881890
protected CloseableIteration<BindingSet, QueryEvaluationException> evaluateAtStatementSources(Object preparedQuery,

0 commit comments

Comments
 (0)