Project

Profile

Help

Bug #5361

closed

Bug in XPath optimizer with positional filter, when LHS of filter is empty sequence

Added by Clément Fournier about 2 years ago. Updated about 2 years ago.

Status:
Closed
Priority:
Normal
Assignee:
Category:
XPath conformance
Sprint/Milestone:
-
Start date:
2020-06-23
Due date:
% Done:

100%

Estimated time:
Legacy ID:
Applies to branch:
10, 11, 9.9, trunk
Fix Committed on Branch:
10, 11, trunk
Fixed in Maintenance Release:
Platforms:
.NET, Java

Description

Hello,

This is a similar issue and context to #4610.

I'm using the XPath engine of Saxon HE 10.6 (JRE) and inlining statically known variables. The optimizer fails to reduce the following expression to () when $foo is replaced with false():

//A[$foo][child::*[2]]

with the following warning

Warning on line 1 column 5 
  Evaluation will always throw a dynamic error: Axis step child::element() cannot be used here:
  the context item is absent

Note that if you write //A[false()][child::*[2]], the parser does some early optimization, prunes the branch, and the optimizer isn't run at all. Only if you use a variable and set its static value to false() in IndependentContext::bindVariable, you get the bug. You can find a MWE attached.

Also note that the following work fine:

//A[$foo]/*[2]
//A[$foo=true()][child::*[2]]

I reproduced the issue on Saxon HE 10.2 and 10.6.

While debugging I found that the offending expression is represented like so:

(/)/((descendant::element(Q{}A)[false()])[child::element()[2]])
     ------------------------------------    

Then, the LHS of the FilterExpression (underlined part above) is reduced to (). At this point the entire FilterExpression could already be folded to (), but optimization of the FilterExpression continues by optimizing the filter child::element()[2]. This is initially a FilterExpression and is replaced by a SubscriptExpression. Type checking the SubscriptExpression fails with "the context item is absent" (the warning is reported at this point) and the SubscriptExpression becomes error("Axis step child::element() cannot be used here: the context item is absent")[2]. This shouldn't have to happen as the LHS of the filter is ().

A possible fix would be to end the optimization of a FilterExpression as soon as the LHS has been optimized to (), without considering the predicate.


Files

SaxonMwe.java (1.55 KB) SaxonMwe.java MWE java class Clément Fournier, 2022-02-27 17:17

Related issues

Copied from Saxon - Bug #4610: Bug in XPath optimizer with positional filterClosedMichael Kay2020-06-23

Actions

Please register to edit this issue

Also available in: Atom PDF