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

Extra "Aggregate_Sample" operators in algebra #2982

Open
joc-proxem opened this issue Nov 11, 2024 · 0 comments
Open

Extra "Aggregate_Sample" operators in algebra #2982

joc-proxem opened this issue Nov 11, 2024 · 0 comments

Comments

@joc-proxem
Copy link

In the following code:

query = """
SELECT
    (COUNT(?t1) AS ?c1)
    (COUNT(?t2) AS ?c2)
    (?c1 / ?c2 * 100 AS ?p)
WHERE {
    ?t1 a :Sample .
    ?t2 a :Sample
}
"""

ns = { "": "http://example.org/" }
q = parser.parseQuery(query)
algebra = translateQuery(q, initNs = ns)

?c1 and ?c2 are wrapped in "Aggregate_Sample" operators, whereas they should not be : algorithm 18.2.4.1 (https://www.w3.org/TR/sparql11-query/#convertGroupAggSelectExpressions) states that

For each **unaggregated variable** V in X
    Replace V with Sample(V)

A possible fix could be as follows (in algebra.py):

  • in translateAggregates:
   # collect/replace aggs in :
    #    select expr as ?var
    if q.projection:
        aggregated_vars = set(v.evar for v in q.projection if isinstance(v.expr, CompValue) and v.expr.name.startswith("Aggregate_"))
        for v in q.projection:
            if v.evar:
                v.expr = traverse(v.expr, functools.partial(_sample, v=v.evar, s=aggregated_vars))
  • in _sample
# type error: Missing return statement
def _sample(e: typing.Union[CompValue, List[Expr], Expr, List[str], Variable], v: Optional[Variable] = None, s: Optional[set] = None) -> Optional[CompValue]:  # type: ignore[return]
    """
    For each unaggregated variable V in expr
    Replace V with Sample(V)
    """
    if isinstance(e, CompValue) and e.name.startswith("Aggregate_"):
        return e  # do not replace vars in aggregates
    if isinstance(e, Variable) and v != e:
        if s is None or e not in s:
            return CompValue("Aggregate_Sample", vars=e)
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

1 participant