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

Subselect clause under optional clause fails in RDFlib #2957

Open
floresbakker opened this issue Oct 28, 2024 · 3 comments
Open

Subselect clause under optional clause fails in RDFlib #2957

floresbakker opened this issue Oct 28, 2024 · 3 comments

Comments

@floresbakker
Copy link

floresbakker commented Oct 28, 2024

A subselect clause under an optional clause fails in RDFlib. Let us consider the following example:

prefix ex:  <https://www.example.org/>

ex:document ex:subject "Nice cars" .

ex:someCar   ex:type "Car" .

I want to be able to query this graph and put all cars in some document about cars, if there are any cars. I use a subselect clause under an optional clause. The real reason for the subselect is that I want to count the number of cars (optionally being zero), but that is not relevant here. The example code is simple, does not involve an aggregate, but fails to yield the correct results.

prefix ex:  <https://www.example.org/> 

select ?subject ?car 

where  {
        $this ex:subject ?subject.
 
        # optional clause
        optional 
        { 
         # an offending subselect clause
         select ?car 
         where {
                   ?car ex:type "Car".       
               }
          }
    }

I would expect the following results:

subject: "Nice cars"
car: https://www.example.org/someCar

Instead I get:

subject: None
car: https://www.example.org/someCar

So the variable subject is incorrectly not bound in the result set.

If I would query with the optional clause and an ordinary triple pattern without a subselect clause, then I get the results I want:

prefix ex:  <https://www.example.org/> 

select ?subject ?car 

where  {
        $this ex:subject ?subject.

        # optional clause
       optional 
        
        {
        # legit triple pattern without subselect 
        ?car ex:type "Car".       
        }
      }

Expected and achieved results:

subject: "Nice cars"
car: https://www.example.org/someCar

I have tested the offending subselect query under the following engines:

RDFlib, JENA, Speedy and Virtuoso. Only RDFlib results in the 'none' binding for subject, the three other engines show the expected results.

@floresbakker
Copy link
Author

Is there anything I can do to get this issue fixed? I hope my example is clear but if not, let me know and I ll try to explain it better.

@WhiteGobo
Copy link
Contributor

For your example query a workaround would be, to rearrange the search order:

prefix ex:  <https://www.example.org/>
    
select ?subject ?car
            
where  {
    
        # optional clause
        optional
        { 
         # an offending subselect clause
         select ?car
         where {
                   ?car ex:type "Car".
               }
          }
        $this ex:subject ?subject.
    }

I searched a little bit for an error and found out, that the processor kicks out the solution for ?subject around evalProject. evalProject processes the subselect, i think.

def evalProject(ctx: QueryContext, project: CompValue):
res = evalPart(ctx, project.p)
return (row.project(project.PV) for row in res)

I wont get any further today. I hope this helps a little bit. I dont know, when i'm continuing this.

@floresbakker
Copy link
Author

Thanks WhiteGobo! In my opinion this is a high impact issue that lowers the reliability of the SPARQL engine considerably. I often write queries with subselect queries, hence this issue worries me. Thank you for your analysis!

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

No branches or pull requests

4 participants
@WhiteGobo @floresbakker and others