-
Notifications
You must be signed in to change notification settings - Fork 19
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
GROUP_CONCAT sorting #9
Comments
Looking at the grammar for Aggregate I never realized one could jam any Expression into an aggregate. Would it be possible to extend that grammar rule to something like:
To allow things like |
Yes, I think that would be a logical approach to implementing this feature. More complex options may exist for allowing sorting by a value not being used in the CONCAT expression. |
Indeed, sorting within a group would be great, I think that could be in scope for #23 |
A |
If we wanted to pursue the more complex option of allowing sorting of
Any thoughts? I'm not sure if all of these would be valuable in practice, but I think allowing arbitrary ordering in general is a valuable feature. |
@kasei I suppose SELECT ?location (IRI(?photoStr) AS ?photo) {
{
SELELCT ?location (group_concat(?photo; limit=6; separator=" ") AS ?photos) {
?location a :Location.
?photo :depicts ?location.
}
GROUP BY ?location
}
?photoStr apf:strSplit (?photos " ")
} With VALUES ?photoStr OF split(?photos, " ") In vanilla SPARQL 1.1, if neither
P.S.: I have not yet had a chance to really look into window functions, so am open to explore them as a probably superior alternative to this kind of hackery. |
@cygri if the SERVICE Variables from SPARQL 1.1 Federated Query were available, it may work to federate to itself, whereby a series of separate invocations of SPARQL query services would happen where the results of each invocation are combined using union. SELECT ?location ?photo {
{
SELECT ?location (<http://example.com/sparql> AS ?endpoint) {
?location a :Location.
}
}
SERVICE ?endpoint {
SELECT ?location ?photo {
?photo :depicts ?location.
}
LIMIT 6
}
} |
@jaw111 Subqueries are evaluated inside-out. So |
@cygri yes, that's true. I was being optimistic that the query engine would use a |
Another (off the cuff) suggestion borrowing SELECT ?location ?photo {
?location a :Location .
?photo :depicts ?location ;
:date ?date .
}
PARTITION BY ?location
PARTITION ORDER BY DESC(?date)
PARTITION LIMIT 6
ORDER BY ?location ?date
LIMIT 60 So this should return (at most) the 6 most recent photos per location. |
I've added some prototype tests for ordering by arbitrary expression here using a strawman syntax: SELECT (GROUP_CONCAT(?name; ORDER BY ?number) AS ?names)
WHERE {
VALUES (?name ?number) {
("four" 4)
("one" 1)
("three" 3)
("two" 2)
}
} The |
Speaking of syntax, it would be good to align it with named arguments for functions (#64). |
@jindrichmynarz I'm torn about this. Absent something like #64, I think it would be obvious that we'd want to align the syntax with the existing |
There should be a way to sort group rows before aggregation, particularly for GROUP_CONCAT.
Why
The use of
GROUP_CONCAT
is limited by the output not being deterministic with respect to the ordering of values in the group. For example, aGROUP_CONCAT(?name)
aggregate might produce"Alice Bob Eve"
in one implementation, and"Bob Alice Eve"
in another (or even within the same implementation from different query evaluations). Allowing users to specify an implicit/explicit ordering for rows in an aggregation group would improve interoperability.Previous work
This was raised as ISSUE-66 and subsequently postponed during work on SPARQL 1.1.
Jindřich Mynarz discusses this as a potential SPARQL 1.2 feature.
Considerations for backward compatibility
At the user-level, this is purely additive as SPARQL 1.1 aggregate groups do not have any explicit ordering. However, it would require updates to the existing definition of SPARQL Algebra Set Functions which are defined in terms of multisets, not ordered sequences.
The text was updated successfully, but these errors were encountered: