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

Missing variables in method to_sparql #47

Open
danielhz opened this issue Apr 17, 2023 · 3 comments
Open

Missing variables in method to_sparql #47

danielhz opened this issue Apr 17, 2023 · 3 comments
Labels

Comments

@danielhz
Copy link

I tested the following code with sparql 3.2.5:

require 'sparql'

exp = <<-EOS
(extend ((?a1 "a1") (?a2 "a2"))
	(union
	 (extend
	  ((?a2 "a3") (?a4 "a4"))
	  (join (extend
		 ((?a5 "a5") (?a6 "a6"))
		 (bgp (triple ?s1 ?p1 ?o1) (triple ?s2 ?p2 ?o2) (triple ?s3 ?p3 ?o3)))
		(extend
		 ((?a7 "a7") (?a8 "a8"))
		 (bgp (triple ?s4 ?p4 ?o4) (triple ?s5 ?p5 ?o5)))))
	 (extend ((?a9 "a9") (?a10 "a10")) (bgp (triple ?s6 ?p6 ?o6)))))
EOS

puts SPARQL::Algebra.parse(exp).to_sparql

I expect a query where the variables ?a3 and ?a4. However, in the output listed below, variables ?a3 and ?a4 do not appear.

SELECT *
WHERE {
{
{
{?s1 ?p1 ?o1 . 
?s2 ?p2 ?o2 . 
?s3 ?p3 ?o3 . 

BIND ("a5" AS ?a5) .
BIND ("a6" AS ?a6) .}
{
{?s4 ?p4 ?o4 . 
?s5 ?p5 ?o5 . 

BIND ("a7" AS ?a7) .
BIND ("a8" AS ?a8) .}
}
}
} UNION {
{?s6 ?p6 ?o6 . 

BIND ("a1" AS ?a1) .
BIND ("a2" AS ?a2) .
BIND ("a9" AS ?a9) .
BIND ("a10" AS ?a10) .}
}
BIND ("a1" AS ?a1) .
BIND ("a2" AS ?a2) .
}
@gkellogg
Copy link
Member

Generating the query form from the algebra is a bit experimental, and it looks like you've encountered a corner-case. There is a problem in the SSE (((?a2 "a3") (?a4 "a4")) should probably be ((?a3 "a3") (?a4 "a4"))), but I don't even get that far, with an argument error being generated along the way, so there is some mismatch in gem versions resulting in different behavior. Regardless, this shouldn't happen.

I can't promise when I'll be able to look at this in any more detail, as I'm pretty swamped with standards work, and this is challenging code to work with.

@gkellogg gkellogg added the bug label Apr 17, 2023
@danielhz
Copy link
Author

Yes, I intended to write (?e3 "a3"). The same issue occurs if join is replaced with leftjoin.

@danielhz
Copy link
Author

I tested the following code with JRuby using the Jena library.

require 'jruby/core_ext'
require 'maven_require'

maven_require 'org.apache.jena:jena-core'
maven_require 'org.apache.jena:jena-arq'
maven_require 'org.apache.logging.log4j:log4j-api'
maven_require 'org.apache.logging.log4j:log4j-core'
maven_require 'org.apache.logging.log4j:log4j-slf4j-impl'

java_import 'org.apache.jena.sys.JenaSystem'
java_import 'org.apache.jena.sparql.algebra.Algebra'
java_import 'org.apache.jena.sparql.algebra.OpAsQuery'

JenaSystem.init

exp = <<-EOS
(extend ((?a1 "a1") (?a2 "a2"))
	(union
	 (extend
	  ((?a2 "a3") (?a4 "a4"))
	  (join (extend
		 ((?a5 "a5") (?a6 "a6"))
		 (bgp (triple ?s1 ?p1 ?o1) (triple ?s2 ?p2 ?o2) (triple ?s3 ?p3 ?o3)))
		(extend
		 ((?a7 "a7") (?a8 "a8"))
		 (bgp (triple ?s4 ?p4 ?o4) (triple ?s5 ?p5 ?o5)))))
	 (extend ((?a9 "a9") (?a10 "a10")) (bgp (triple ?s6 ?p6 ?o6)))))
EOS

ops = Algebra.parse(exp)
puts OpAsQuery.as_query(ops).to_s

The output is the following:

SELECT  *
WHERE
  {   { { { ?s1  ?p1  ?o1 .
            ?s2  ?p2  ?o2 .
            ?s3  ?p3  ?o3
            BIND("a5" AS ?a5)
            BIND("a6" AS ?a6)
          }
          { ?s4  ?p4  ?o4 .
            ?s5  ?p5  ?o5
            BIND("a7" AS ?a7)
            BIND("a8" AS ?a8)
          }
        }
        BIND("a3" AS ?a2)
        BIND("a4" AS ?a4)
      }
    UNION
      { ?s6  ?p6  ?o6
        BIND("a9" AS ?a9)
        BIND("a10" AS ?a10)
      }
    BIND("a1" AS ?a1)
    BIND("a2" AS ?a2)
  }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants