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

Functional XQuery causes NullPointerException #2385

Open
adamretter opened this issue Jan 14, 2019 · 8 comments
Open

Functional XQuery causes NullPointerException #2385

adamretter opened this issue Jan 14, 2019 · 8 comments
Assignees
Labels
bug issue confirmed as bug
Milestone

Comments

@adamretter
Copy link
Member

The following XQuery causes a NullPointerException:

declare namespace io = "http://io";

(: IO version of get-char :)
declare function local:get-char() as map(*) {
  local:create-io(function($realworld as element(io:realworld)) {
    ($realworld,
       123)
  })
};

(: IO version of put-char :)
declare function local:put-char($codepoint as xs:integer) as map(*) {
  local:create-io(function($realworld as element(io:realworld)) {
    ($realworld,
        ())
  })
};

declare function local:create-io($apply-fn as function(element(io:realworld)) as item()+) as map(*) {
  map {
    'apply': $apply-fn,
    'bind' : function($binder as function(item()*) as map(*)) as map(*) {
      let $bound-apply-fn := function($realworld) {
        let $io-res := $apply-fn($realworld)
        let $new-world := $io-res[1]
        let $res := subsequence($io-res, 2)
        return
          $binder($res)("apply")($new-world)
      }
      return
        local:create-io($bound-apply-fn)
    }
  }
};

let $x :=
        local:get-char()
          ?bind(local:put-char#1)
return $x?apply(<io:realworld/>)

The query should return the value <io:realworld xmlns:io="http://io"/> as it does in both BaseX and Saxon.

In eXist-db the following Java stacktrace is the result of the NPE:

Exception in thread "java-admin-client-0.query-0" java.lang.NullPointerException
	at org.exist.xquery.AnalyzeContextInfo.<init>(AnalyzeContextInfo.java:70)
	at org.exist.xquery.DynamicFunctionCall.eval(DynamicFunctionCall.java:73)
	at org.exist.xquery.LetExpr.eval(LetExpr.java:99)
	at org.exist.xquery.UserDefinedFunction.eval(UserDefinedFunction.java:162)
	at org.exist.xquery.FunctionCall$DeferredFunctionCallImpl.execute(FunctionCall.java:434)
	at org.exist.xquery.DeferredFunctionCall.realize(DeferredFunctionCall.java:57)
	at org.exist.xquery.DeferredFunctionCall.isEmpty(DeferredFunctionCall.java:199)
	at org.exist.xmldb.LocalResourceSet.<init>(LocalResourceSet.java:58)
	at org.exist.xmldb.LocalXPathQueryService.execute(LocalXPathQueryService.java:224)

The problem seems to be that Expr#analyze is not called on the body of the UserDefinedFunction.

@adamretter adamretter added the bug issue confirmed as bug label Jan 14, 2019
@adamretter adamretter added this to the eXist-5.0.0 milestone Jan 14, 2019
@line-o
Copy link
Member

line-o commented Jan 14, 2019

@adamretter interesting implementation!
Slightly modified, your example does output the expected value in eXist-db as well.
https://gist.github.com/line-o/d60f286383dd74db48b40478f4cea5cd

@adamretter
Copy link
Member Author

@line-o That's a great work around thank you. How the hell did you figure out that would work though?

@line-o
Copy link
Member

line-o commented Jan 14, 2019

@adamretter try and error while figuring out what your code actually does :)

@line-o
Copy link
Member

line-o commented Jan 14, 2019

@adamretter I thought: "Maybe, less inline functions could help me and the parser understand"

@duncdrum
Copy link
Contributor

@line-o nice.
@adamretter NPE are bad, can you wiggle down the sample query into an xqsuite test, so we can see more clearly what causes it?

P.S.: Ran both queries and get the same results NPE for a) and desired output for b).

@carnevlu
Copy link

any news?

@dizzzz
Copy link
Member

dizzzz commented Aug 22, 2021

@carnevlu is it still a problem?

@line-o
Copy link
Member

line-o commented Aug 22, 2021

@dizzzz I believe yes. I have to workaround this limitation every once in a while. Last time it was code running on the latest release 5.3.0.

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

6 participants