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

Fixing bug applying VALUES outside of a GROUP BY #2188

Merged
merged 4 commits into from
Jan 29, 2023
Merged

Fixing bug applying VALUES outside of a GROUP BY #2188

merged 4 commits into from
Jan 29, 2023

Conversation

robons
Copy link
Contributor

@robons robons commented Dec 29, 2022

Summary of changes

Altering order in which aggregate variable aliases are renamed to user-defined variable names to ensure that when defining a VALUES pattern outside of a GROUP BY, the variables in the query are correctly joined to those defined in the VALUES pattern.

The change fixes the following bug...

Example of bug

from pprint import pprint

from rdflib import Graph, URIRef, Literal, RDFS

graph = Graph()

graph.add((URIRef("http://example.com/something"), RDFS.label, Literal("Match on this.")))
graph.add((URIRef("http://example.com/something-else"), RDFS.label, Literal("Don't match on this.")))

results = graph.query("""
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>

SELECT ?obj ?label
WHERE {
    ?obj rdfs:label ?label
}
GROUP BY ?obj ?label
VALUES ( ?obj ) {
    ( <http://example.com/something> )
}
""")

pprint([row.asdict() for row in results])

results in the following erroneous output:

[{'label': rdflib.term.Literal('Match on this.'),
  'obj': rdflib.term.URIRef('http://example.com/something')},
 {'label': rdflib.term.Literal("Don't match on this."),
  'obj': rdflib.term.URIRef('http://example.com/something-else')}]

I would expect the the VALUES pattern to have filtered the results down to only include the result where ?obj is http://example.com/something, i.e.:

[{'label': rdflib.term.Literal('Match on this.'),
  'obj': rdflib.term.URIRef('http://example.com/something')}]

this is indeed the outcome when passing the data and query through the Apache JENA command line tools.

Checklist

  • Checked that there aren't other open pull requests for
    the same change.
  • Added tests for any changes that have a runtime impact.
  • Checked that all tests and type checking passes.
  • For changes that have a potential impact on users of this project:
    • (NA) Updated relevant documentation to avoid inaccuracies.
    • (NA) Considered adding additional documentation.
    • (NA) Considered adding an example in ./examples for new features.
    • (NA) Considered updating our changelog (CHANGELOG.md).
  • Considered granting push permissions to the PR branch,
    so maintainers can fix minor issues and keep your PR up to date.

@aucampia aucampia self-assigned this Jan 26, 2023
@coveralls
Copy link

Coverage Status

Coverage: 90.658% (+0.001%) from 90.656% when pulling a44c2fc on robons:main into fbb8279 on RDFLib:main.

Copy link
Member

@aucampia aucampia left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the PR @robons and apologies for the late review. The change and test looks good to me, so I'm going to merge it before Monday if there is no further feedback.

@aucampia aucampia requested a review from a team January 26, 2023 20:35
@aucampia aucampia added ready to merge The PR will be merged soon if no further feedback is provided. review wanted This indicates that the PR is ready for review labels Jan 26, 2023
@aucampia aucampia merged commit 9625ed0 into RDFLib:main Jan 29, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
ready to merge The PR will be merged soon if no further feedback is provided. review wanted This indicates that the PR is ready for review
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants