Skip to content
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

wrapping nodes using for-each after an arrow-operator #1960

Closed
line-o opened this issue Jun 19, 2018 · 6 comments
Closed

wrapping nodes using for-each after an arrow-operator #1960

line-o opened this issue Jun 19, 2018 · 6 comments
Assignees
Labels
bug issue confirmed as bug
Milestone

Comments

@line-o
Copy link
Member

line-o commented Jun 19, 2018

What is the problem

Wrapping nodes in a function used in fn:for-each after an Arrow Operator incorrectly outputs one element per item in the source sequence.

let $xml := <root><i/><i/><i/></root>
return
    $xml/i => for-each(function ($i) { <wrap>{$i}</wrap> })

returns

<wrap><i/><i/><i/></wrap>
<wrap><i/><i/><i/></wrap>
<wrap><i/><i/><i/></wrap>

What did you expect

<wrap><i/></wrap>
<wrap><i/></wrap>
<wrap><i/></wrap>

Describe how to reproduce or add a test

2 tests:

  • ao:wrap-atomic-sequence does work
  • ao:wrap-element-sequence outputs 9 instead of 3
xquery version "3.1";

module namespace ao="http://exist-db.org/xquery/test/arrowop";
declare namespace test="http://exist-db.org/xquery/xqsuite";

declare
	%private
function ao:wrap ($item as item()) as item() { 
    <wrap>{$item}</wrap>
};

declare
	%private
function ao:wrap-explicit-type-conversion ($item as item()) as item() { 
    <wrap>{xs:string($item)}</wrap>
};

declare
	%private 
function ao:first-element-text ($s as node()*) as text() {
    $s[1]/text()
};

declare
    %test:assertEquals("1")
function ao:wrap-atomic-sequence () {
    (1, 2, 3) => for-each(ao:wrap#1) => ao:first-element-text()
};

declare
    %test:assertEquals("1")
function ao:wrap-atomic-sequence-with-explicit-type-conversion () {
    (1, 2, 3) => for-each(ao:wrap-with-explicit-type-conversion#1) => ao:first-element-text()
};

declare
	%private 
function ao:get-i-elements ($i as item()) { $i//i };

declare
    %test:assertEquals(3)
function ao:wrap-element-sequence () {
    let $xml := <root><i/><i/><i/></root>
    return $xml/node()
        => for-each(ao:wrap#1)
        => for-each(ao:get-i-elements#1)
        => count()
};

Context information

  • Tested on 4.1.0 and 5.0.0-RC1 (8339007)
  • java version "1.8.0_152"
  • Operating system: MacOs (64 bit)
  • both are clones from GitHub
  • the 4.1.0 is a customised version of existdb
@triage-new-issues triage-new-issues bot added the triage issue needs to be investigated label Jun 19, 2018
@adamretter
Copy link
Member

I can confirm that BaseX and Saxon both produce the correct result as indicated by @line-o, whilst eXist-db returns the incorrect result

@adamretter adamretter added the bug issue confirmed as bug label Jun 19, 2018
@triage-new-issues triage-new-issues bot removed the triage issue needs to be investigated label Jun 19, 2018
@adamretter adamretter modified the milestones: eXist-5.0.0, eXist-4.2.2 Jun 19, 2018
@line-o
Copy link
Member Author

line-o commented Jun 19, 2018

Thanks for the edit @adamretter, looks much better.
I found more cases that do fail, but I think they all have the same root cause.

For example this will fail with a duplicate attribute exception err:XQDY0025:

(<item n="1"/>, <item n="2"/>, <item n="3"/>) 
    => for-each(function ($item as item()) as element(a) { <a>{$item/@n}</a> })

@line-o
Copy link
Member Author

line-o commented Jul 3, 2018

(1,2,3) => for-each(function ($a) {<a>{xs:string($a)}</a>})

does also trigger the behaviour while

(1,2,3) => for-each(function ($a) {<a>{$a}</a>})

does not

@line-o
Copy link
Member Author

line-o commented Jul 3, 2018

Added a test case for wrapping atomic values with explicit type conversion.

@line-o
Copy link
Member Author

line-o commented Oct 12, 2018

Hint from @wolfgangmm

UserDefinedFunction.eval (for the inner inline function) [...] is called way more often than 3 times. for-each (FunHigherOrderFun) iterates over 3 items only as expected. So somewhere between those two there's a bug.

@duncdrum duncdrum modified the milestones: eXist-4.4.1, eXist-4.5.1 Nov 30, 2018
line-o added a commit to line-o/exist that referenced this issue Jan 15, 2019
line-o added a commit to line-o/exist that referenced this issue Jan 15, 2019
line-o added a commit to line-o/exist that referenced this issue Jan 15, 2019
line-o added a commit to line-o/exist that referenced this issue Jan 15, 2019
tuurma added a commit to tuurma/exist that referenced this issue Jan 28, 2019
@line-o
Copy link
Member Author

line-o commented Jan 28, 2019

It turns out, that it is the context that is handed down to the called function. The spec clearly states that the focus (context etc.) must not be present in the called function.

Also, more functions are affected:

  • filter
  • for-each-pair
  • fold-left and ~-right
  • sort

tuurma added a commit to tuurma/exist that referenced this issue Jan 28, 2019
tuurma added a commit to tuurma/exist that referenced this issue Feb 11, 2019
tuurma added a commit to tuurma/exist that referenced this issue Feb 11, 2019
tuurma added a commit to tuurma/exist that referenced this issue Feb 11, 2019
tuurma added a commit to tuurma/exist that referenced this issue Feb 11, 2019
tuurma added a commit to tuurma/exist that referenced this issue Mar 18, 2019
tuurma added a commit to tuurma/exist that referenced this issue Mar 18, 2019
tuurma added a commit to tuurma/exist that referenced this issue Mar 18, 2019
tuurma added a commit to tuurma/exist that referenced this issue Mar 18, 2019
tuurma added a commit to tuurma/exist that referenced this issue Mar 18, 2019
tuurma added a commit to tuurma/exist that referenced this issue Mar 18, 2019
tuurma added a commit to tuurma/exist that referenced this issue Mar 26, 2019
tuurma added a commit to tuurma/exist that referenced this issue Mar 26, 2019
tuurma added a commit to tuurma/exist that referenced this issue Apr 3, 2019
@duncdrum duncdrum closed this as completed Apr 3, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug issue confirmed as bug
Projects
None yet
Development

No branches or pull requests

4 participants