Skip to content

rdflib.Graph.query does not enforce Aggregate Projection Restrictions #2960

@lu-pl

Description

@lu-pl

rdflib.Graph.query does not enforce Aggregate Projection Restrictions.

E.g. the following

query = """
select (count(*) as ?cnt) ?parent ?child ?name
where {
    values (?parent ?child ?name) {
        ('x' 'c' 'foo')
        ('y' 'd' UNDEF)
        ('y' 'e' UNDEF)
        ('z' UNDEF UNDEF)
    }
}
"""

graph = Graph()
result = graph.query(query)
result.bindings

is invalid SPARQL according to Aggregate Projection Restrictions:

In a query level which uses aggregates, only expressions consisting of aggregates and constants may be projected, with one exception. When GROUP BY is given with one or more simple expressions consisting of just a variable, those variables may be projected from the level.

Yet the above code results in

[
    {
        rdflib.term.Variable("parent"): rdflib.term.Literal("x"),
        rdflib.term.Variable("child"): rdflib.term.Literal("c"),
        rdflib.term.Variable("name"): rdflib.term.Literal("foo"),
        rdflib.term.Variable("cnt"): rdflib.term.Literal(
            "4", datatype=rdflib.term.URIRef("http://www.w3.org/2001/XMLSchema#integer")
        ),
    }
]

Blazegraph and GraphDB reject the query with Malformed Query exceptions.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions