-
-
Notifications
You must be signed in to change notification settings - Fork 188
Fix duplicate node elimination for function calls in path expressions #6110
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
joewiz
wants to merge
3
commits into
eXist-db:develop
Choose a base branch
from
joewiz:fix/path-expr-dedup-function-calls
base: develop
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+182
−4
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
147 changes: 147 additions & 0 deletions
147
exist-core/src/test/java/org/exist/xquery/PathExprDedupTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,147 @@ | ||
| /* | ||
| * eXist-db Open Source Native XML Database | ||
| * Copyright (C) 2001 The eXist-db Authors | ||
| * | ||
| * info@exist-db.org | ||
| * http://www.exist-db.org | ||
| * | ||
| * This library is free software; you can redistribute it and/or | ||
| * modify it under the terms of the GNU Lesser General Public | ||
| * License as published by the Free Software Foundation; either | ||
| * version 2.1 of the License, or (at your option) any later version. | ||
| * | ||
| * This library is distributed in the hope that it will be useful, | ||
| * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
| * Lesser General Public License for more details. | ||
| * | ||
| * You should have received a copy of the GNU Lesser General Public | ||
| * License along with this library; if not, write to the Free Software | ||
| * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA | ||
| */ | ||
| package org.exist.xquery; | ||
|
|
||
| import org.exist.test.ExistXmldbEmbeddedServer; | ||
| import org.junit.ClassRule; | ||
| import org.junit.Test; | ||
| import org.xmldb.api.base.ResourceSet; | ||
| import org.xmldb.api.base.XMLDBException; | ||
| import org.xmldb.api.modules.XQueryService; | ||
|
|
||
| import static org.junit.Assert.assertEquals; | ||
|
|
||
| /** | ||
| * Tests for duplicate node elimination in path expressions. | ||
| * | ||
| * Per XPath 3.1 §3.3.1.1, the path operator '/' must eliminate duplicate | ||
| * nodes (by identity) and return results in document order when every | ||
| * evaluation of E2 returns nodes. This must apply regardless of whether | ||
| * E2 is an axis step, function call, or other PostfixExpr. | ||
| * | ||
| * @see <a href="https://www.w3.org/TR/xpath-31/#id-path-operator">XPath 3.1 §3.3.1.1</a> | ||
| */ | ||
| public class PathExprDedupTest { | ||
|
|
||
| @ClassRule | ||
| public static final ExistXmldbEmbeddedServer existEmbeddedServer = | ||
| new ExistXmldbEmbeddedServer(false, true, true); | ||
|
|
||
| private String query(final String xquery) throws XMLDBException { | ||
| final XQueryService xqs = existEmbeddedServer.getRoot().getService(XQueryService.class); | ||
| final ResourceSet result = xqs.query(xquery); | ||
| return result.getResource(0).getContent().toString(); | ||
| } | ||
|
|
||
| /** | ||
| * XQTS K2-Steps-31 (explicit expansion): function call in path where | ||
| * multiple context items produce the same result node. The path operator | ||
| * must deduplicate results per §3.3.1.1. | ||
| */ | ||
| @Test | ||
| public void functionCallInPathDedup() throws XMLDBException { | ||
| final String result = query(""" | ||
| declare variable $root := <root><c/></root>; | ||
| declare function local:function($arg) { $root[$arg] }; | ||
| count($root/descendant-or-self::node()/local:function(.))"""); | ||
| assertEquals("1", result); | ||
| } | ||
|
|
||
| /** | ||
| * Simpler case: child::* / function that always returns the same node. | ||
| */ | ||
| @Test | ||
| public void functionReturnsConstantNodeDedup() throws XMLDBException { | ||
| final String result = query(""" | ||
| declare variable $root := <root><a/><b/></root>; | ||
| declare function local:getroot($x) { $root }; | ||
| count($root/*/local:getroot(.))"""); | ||
| assertEquals("1", result); | ||
| } | ||
|
|
||
| /** | ||
| * Ensure for-loop results are NOT incorrectly deduplicated. | ||
| * This is the scenario from the 2009 bug fix (SF #2880394). | ||
| */ | ||
| @Test | ||
| public void forLoopPreservesDuplicates() throws XMLDBException { | ||
| final String result = query(""" | ||
| declare variable $root := <root/>; | ||
| count(for $x in (1, 2, 3) return $root)"""); | ||
| assertEquals("3", result); | ||
| } | ||
|
|
||
| /** | ||
| * Global variable with for-loop should preserve all results. | ||
| */ | ||
| @Test | ||
| public void globalVarForLoopPreservesDuplicates() throws XMLDBException { | ||
| final String result = query(""" | ||
| declare variable $data := <item/>; | ||
| count(for $i in 1 to 5 return $data)"""); | ||
| assertEquals("5", result); | ||
| } | ||
|
|
||
| /** | ||
| * XQTS K2-Axes-48: path expression ending with integer literal. | ||
| * Must not NPE when removeDuplicates is called on atomic results. | ||
| */ | ||
| @Test | ||
| public void pathEndingWithIntegerLiteral() throws XMLDBException { | ||
| final XQueryService xqs = existEmbeddedServer.getRoot().getService(XQueryService.class); | ||
| final ResourceSet result = xqs.query(""" | ||
| declare variable $myVar := <e/>; | ||
| $myVar/(<a/>, <b/>, <?d ?>, <!-- e-->, attribute name {}, document {()})/3"""); | ||
| assertEquals(6, (int) result.getSize()); | ||
| for (int i = 0; i < 6; i++) { | ||
| assertEquals("3", result.getResource(i).getContent().toString()); | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * XQTS K2-Axes-49: path expression ending with number() function. | ||
| * Must not NPE when removeDuplicates is called on atomic results. | ||
| */ | ||
| @Test | ||
| public void pathEndingWithNumberFunction() throws XMLDBException { | ||
| final XQueryService xqs = existEmbeddedServer.getRoot().getService(XQueryService.class); | ||
| final ResourceSet result = xqs.query(""" | ||
| declare variable $myVar := <e/>; | ||
| $myVar/(<a/>, <b/>, <?d ?>, <!-- e-->, attribute name {}, document {()})/number()"""); | ||
| assertEquals(6, (int) result.getSize()); | ||
| for (int i = 0; i < 6; i++) { | ||
| assertEquals("NaN", result.getResource(i).getContent().toString()); | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Path with axis step followed by function call — dedup should apply. | ||
| */ | ||
| @Test | ||
| public void axisStepThenFunctionCallDedup() throws XMLDBException { | ||
| final String result = query(""" | ||
| declare variable $doc := <doc><a/><b/><c/></doc>; | ||
| declare function local:parent($n) { $n/.. }; | ||
| count($doc/*/local:parent(.))"""); | ||
| assertEquals("1", result); | ||
| } | ||
| } | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.