Skip to content
Merged
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 15 additions & 9 deletions spec/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -2960,10 +2960,17 @@ <h3>HAVING</h3>
</section>
<section id="aggregateRestrictions">
<h3>Aggregate Projection Restrictions</h3>
<p>In a query level which uses aggregates, only expressions consisting of aggregates and
constants may be projected, with one exception. When <code>GROUP BY</code> is given with one
or more simple expressions consisting of just a variable, those variables may be projected
from the level.</p>
<p>In a query level which uses grouping (either by the explicit use of a <code>GROUP BY</code> clause
or through the use of aggregates in projection, <code>HAVING</code>, or <code>ORDER BY</code> clauses),
every variable that appears in projection or SELECT expressions of that query level
MUST satisfy exactly one of the following conditions:</p>

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Now that I understand what the first bullet point is about, I would say that "exactly one" is too restrictive here. It would also be okay if a variable satisfies both the first and the second condition. Likewise, it would be okay if a variable satisfies both the second and the third condition. It may even be the case that a variable satisfies the first condition in multiple different ways (by being an argument to multiple aggregates within the corresponding SELECT clause).

Having said that, I don't think we can simply replace "exactly one" by "at least one" because, then, a query such as the following would be permitted (because ?x satisfies the first condition).

SELECT ?x (MIN(?x) AS ?y) {
 # ... ?z ...
}
GROUP BY ?z

The underlying issue is that the conditions are actually not about the variables per se, but about (all) the occurrences of the variables within the SELECT clause.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Agreed it's about occurrences, not the variables themselves. I'm hesitant to get to into the weeds with this language, but I'll think about how we might tweak it to talk about occurrences.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Perhaps it's as simple as introducing "occurrence":

every occurrence of a variable that appears in projection or SELECT expressions of that query level MUST satisfy exactly one of the following conditions:

If such a variable occurrence does not satisfy one of these conditions, the query is syntactically invalid.

?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Yes! Introducing "occurrence" in this way works, together with just saying "one of" instead of saying "exactly one of."

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Done.

<ul>
<li>the variable is aggregated</li>
Comment thread
kasei marked this conversation as resolved.
Outdated
<li>the variable, <var>V</var>, appears as a named <code>GROUP BY</code> variable (with the corresponding <code>GROUP BY</code> expression consisting of just the variable <var>V</var>, or having the form <code>(expr AS <var>V</var>)</code>)</li>
<li>the variable is introduced by an earlier SELECT expression in the same SELECT clause</li>
Comment thread
kasei marked this conversation as resolved.
</ul>
<p>If such a variable does not satisfy one of these conditions, the query is syntactically invalid.</p>

<p>For example, the following query is legal as ?x is given as a <code>GROUP BY</code>
term.</p>
<pre class="query nohighlight">
Expand All @@ -2974,12 +2981,11 @@ <h3>Aggregate Projection Restrictions</h3>
?x :q ?z .
} GROUP BY ?x (STR(?z))
</pre>
<p>Note that it would not be legal to project <code>STR(?z)</code> as this is not a simple
variable expression. However, with <code>GROUP BY (STR(?z) AS ?strZ)</code> it would be
<p>Note that it would not be legal to project <code>STR(?z)</code> as this expression is neither a simple variable,
nor a named <code>GROUP BY</code> expression. However, with <code>GROUP BY ?x (STR(?z) AS ?strZ)</code> it would be
possible to project <code>?strZ</code>.</p>
<p>Other expressions, not using <code>GROUP BY</code> variables, or aggregates may have
non-deterministic values projected from their groups using the <code>SAMPLE</code>
aggregate.</p>
<p>Other expressions which use variables not satisfying the above conditions may be
projected from their groups using the <code>SAMPLE</code> aggregate.</p>
</section>
<section id="aggregateExample2">
<h3>Aggregate Example (with errors)</h3>
Expand Down
Loading