Skip to content

Keep the context node after a nested predicate - #193

Open
LuisFigueroaG wants to merge 1 commit into
antchfx:masterfrom
LuisFigueroaG:fix/predicate-context-node
Open

LuisFigueroaG wants to merge 1 commit into
antchfx:masterfrom
LuisFigueroaG:fix/predicate-context-node

Conversation

@LuisFigueroaG

Copy link
Copy Markdown
Contributor

When a predicate contains a path with its own predicate, the operands after that path are evaluated against the wrong context node. //p[a[@x] = @id] compares each a with the id of that a, not the id of p.

filterQuery.Select moves t.Current() to each node while it evaluates the predicate and leaves it there. The left operand of = moves the shared context to the last a it looked at, and @id is then evaluated from there. The same happens with string(), number(), contains(), concat() and the other functions that take a filtered node-set before another argument. numericQuery, booleanQuery and lang() already work around this by copying the context node before evaluating their operands.

On

<r>
  <p id="1"><a x="1">1</a><a>2</a></p>
  <p id="2"><a>1</a><a x="2">2</a></p>
  <p id="3"><a x="3">a</a></p>
</r>
                                      before      after (same as lxml)
//p[a[@x] = @id]                      (empty)     p 1, p 2
//p[a[@x] != @id]                     (empty)     p 3
//p[a[@x]/@x = @id]                   (empty)     p 1, p 2, p 3
//p[string(a[@x]) = @id]              (empty)     p 1, p 2
//p[number(a[@x]) = @id]              (empty)     p 1, p 2
//p[contains(a[@x], @id)]             p 1, 2, 3   p 1, p 2
//p[concat(a[@x], '-', @id) = '1-1']  (empty)     p 1

//p[@id = a[@x]] already worked, because @id is evaluated first.

The filter now moves t.Current() back to the context node after each predicate. The saved node is kept on the filter and reused with MoveTo, so it is copied once per query rather than once per node. The filter benchmarks have one more allocation per run and no measurable change in time.

I added TestNestedPredicateKeepsContextNode, which fails on master.

I also ran 46,027 expressions against lxml: 19 filtered paths (child, descendant, following-sibling, ancestor, parent, positional and (…)[1] predicates) combined with 13 context-dependent operands (@id, ., name(), position(), last(), count(tag)…) in 24 comparisons and function calls, inside 6 location paths, plus count() and [1] on top. 8,992 differ from lxml on master and 1,040 with this change. 72 that matched before no longer do. They all use a reverse axis as a string argument, like starts-with(ancestor::*[@id], @n) (#182 handles this), or (tag[@k])[1] inside a predicate, which only selects for the first context node. On master, the moved context node happened to give the right answer for those. The rest of the remaining differences come from those two cases or are node-set order only (//*[count(tag) >= 0] already returns nodes out of document order).

filterQuery moves t.Current() to each node while it evaluates the
predicate and leaves it there. When the filter is one operand of an
expression, the operands after it were evaluated against that node
instead of the original context node, so //p[a[@x] = @id] compared a
with the id of a instead of the id of p. The same happened with
string(), number(), contains(), concat() and the other functions that
take a filtered node-set before another argument.

Move t.Current() back to the context node after each predicate. The
saved node is kept on the filter and reused with MoveTo, so it is only
copied once per query.
@coveralls

Copy link
Copy Markdown

Coverage Status

coverage: 85.766% (+0.1%) from 85.624% — LuisFigueroaG:fix/predicate-context-node into antchfx:master

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants