pub struct Combines {
pub queries: Vec<Combine>,
pub order_by: OrderBy,
pub limit: Limit,
pub offset: Offset,
pub fetch: Fetch,
}Expand description
Every set operation chained onto one statement, and the ORDER BY / LIMIT
/ OFFSET / FETCH that belong to the combination rather than to any one
operand.
That second half is the part worth getting right. PostgreSQL 17, https://www.postgresql.org/docs/17/sql-select.html:
select_statement UNION [ALL] select_statement
ORDER BYandLIMIT… can be attached to a subexpression if it is enclosed in parentheses. Without parentheses, these clauses will be taken to apply to the result of theUNION, not to its right-hand input expression.
So a statement with set operations has two sets of trailing clauses that render in different places, and the difference is invisible in the SQL text except through the parentheses:
(SELECT … LIMIT 1) UNION ALL (SELECT …) ORDER BY 1 LIMIT 5
^ the leading query's own LIMIT ^ the combination'sThe leading query’s clauses stay where the query writes them, and are wrapped;
the combination’s live here and are written after the last operand.
parenthesises_leading_query is the
condition for the wrapping.
No keyword of its own: every Combine starts with its operator.
Fields§
§queries: Vec<Combine>The operations, applied left to right.
order_by: OrderByORDER BY over the result of the combination.
limit: LimitLIMIT over the result of the combination.
offset: OffsetOFFSET over the result of the combination.
fetch: FetchFETCH over the result of the combination.
Implementations§
Source§impl Combines
impl Combines
Sourcepub fn append_combine(&mut self, combine: Combine)
pub fn append_combine(&mut self, combine: Combine)
Append one set operation.
Sourcepub fn parenthesises_leading_query(
&self,
leading_has_tail_clauses: bool,
) -> bool
pub fn parenthesises_leading_query( &self, leading_has_tail_clauses: bool, ) -> bool
Whether the statement in front of these operations has to be parenthesised.
It does exactly when it carries a trailing clause of its own —
ORDER BY, LIMIT, OFFSET, FETCH or a locking clause — and
something is combined onto it, because without the parentheses that clause
would silently move to the whole combination. leading_has_tail_clauses is
what only the query type can know.
With nothing combined the parentheses would be legal but pointless, so they
are left out; that is also why an all-default Combines changes nothing
about how a statement renders.