Skip to content

Commit 73dce43

Browse files
committed
[bugfix] eXist-db was previously doing more work than needed by
evaluating steps upon an empty context sequence. This was also not XQuery specification compliant which led to incorrect results. Closes #4466
1 parent 484fdd4 commit 73dce43

File tree

3 files changed

+19
-2
lines changed

3 files changed

+19
-2
lines changed

exist-core/src/main/java/org/exist/xquery/PathExpr.java

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -229,7 +229,7 @@ public Sequence eval(Sequence contextSequence, final Item contextItem) throws XP
229229
//TODO : let the parser do it ? -pb
230230
boolean gotAtomicResult = false;
231231
Expression prev = null;
232-
for (Expression step : steps) {
232+
for (final Expression step : steps) {
233233
prev = expr;
234234
expr = step;
235235
context.getWatchDog().proceed(expr);
@@ -299,7 +299,22 @@ public Sequence eval(Sequence contextSequence, final Item contextItem) throws XP
299299
// expression with more than one step
300300
result.removeDuplicates();
301301
}
302+
303+
/**
304+
* If the result is an Empty Sequence, we don't need to process
305+
* any more steps as there is no Context Item for the next step!
306+
*
307+
* There is one exception to this rule, which is a TextConstructor,
308+
* that only contains whitespace but has been configured
309+
* to strip-whitespace. In this instance the TextConstructor will
310+
* return an {@link Sequence.EMPTY_SEQUENCE_VALID} to indicate
311+
* that.
312+
*/
313+
if (result != Sequence.EMPTY_SEQUENCE_VALID && result.isEmpty()) {
314+
break;
315+
}
302316
}
317+
303318
if (!staticContext) {
304319
currentContext = result;
305320
}

exist-core/src/main/java/org/exist/xquery/TextConstructor.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ public TextConstructor(XQueryContext context, String text) throws XPathException
5454
*/
5555
public Sequence eval(Sequence contextSequence, Item contextItem) throws XPathException {
5656
if (isWhitespaceOnly && context.stripWhitespace())
57-
{return Sequence.EMPTY_SEQUENCE;}
57+
{return Sequence.EMPTY_SEQUENCE_VALID;}
5858
if (newDocumentContext)
5959
{context.pushDocumentContext();}
6060
try {

exist-core/src/main/java/org/exist/xquery/value/Sequence.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,8 @@ public interface Sequence {
5454
*/
5555
Sequence EMPTY_SEQUENCE = new EmptySequence();
5656

57+
Sequence EMPTY_SEQUENCE_VALID = new EmptySequence();
58+
5759
/**
5860
* The purpose of ordered and unordered flag is to set the ordering mode
5961
* in the static context to ordered or unordered for a certain region in a query.

0 commit comments

Comments
 (0)