Skip to content

Commit

Permalink
Merge pull request MaterializeInc#2063 from sploiselle/select-set-ope…
Browse files Browse the repository at this point in the history
…rations

docs: add set operations to SELECT
  • Loading branch information
Sean Loiselle authored Feb 19, 2020
2 parents 04aef61 + 73fe903 commit cadea93
Show file tree
Hide file tree
Showing 7 changed files with 123 additions and 189 deletions.
2 changes: 1 addition & 1 deletion doc/user/sql/join.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ Field | Use
------|-----
_select_pred_ | The predicating [`SELECT`](../select) clauses you want to use, e.g. `SELECT col_ref FROM table_ref...`. The _table_ref_ from the _select_pred_ is the left-hand table.
**NATURAL** | Join table expressions on all columns with the same names in both tables. This is similar to the `USING` clause naming all identically named columns in both tables.
_join_type_ | The type of `JOIN` you want to use (_`INNER` is implied default_).
_join_type_ | The type of `JOIN` you want to use _(`INNER` is implied default)_.
_table_ref_ | The table expression you want to join, i.e. the right-hand table.
**USING (** _col_ref..._ **)** | If the join condition does not require table-level qualification (i.e. joining tables on columns with the same name), the columns to join the tables on. For example, `USING (customer_id)`.
**ON** _expression_ | The condition on which to join the tables. For example `ON purchase.customer_id = customer.id`.
Expand Down
7 changes: 5 additions & 2 deletions doc/user/sql/select.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,11 @@ Scenario | `SELECT` dataflow behavior

## Syntax

{{< diagram "select.html" >}}
{{< diagram "select_stmt.html" >}}

Field | Use
------|-----
**ALL** | Return all rows from query (_implied default_).
**ALL** | Return all rows from query _(implied default)_.
**DISTINCT** | Return only distinct values from query.
**DISTINCT ON (** _col&lowbar;ref..._ **)** | Return only the first row with a distinct value for _col&lowbar;ref_.
_target&lowbar;elem_ | Return identified columns or functions.
Expand All @@ -47,6 +47,9 @@ _join&lowbar;expr_ | A join expression; for more details, see our [`JOIN` docume
**ORDER BY** _col&lowbar;ref_ ... | Order results in either **ASC** or **DESC** order (_**ASC** is implied default_).<br/><br>**ORDER BY** does not work when creating views, but does work when reading from views.
**LIMIT** | Limit the number of returned results to _expr_.
**OFFSET** | Skip the first _expr_ number of rows.
**UNION** | Records present in `select_stmt` or `another_select_stmt`.<br/><br/>**DISTINCT** returns only unique rows from these results _(implied default)_.<br/><br/>With **ALL** specified, each record occurs a number of times equal to the sum of the times it occurs in each input statement.
**INTERSECT** | Records present in both `select_stmt` and `another_select_stmt`.<br/><br/>**DISTINCT** returns only unique rows from these results _(implied default)_.<br/><br/>With **ALL** specified, each record occurs a number of times equal to the lesser of the times it occurs in each input statement.
**EXCEPT** | Records present in `select_stmt` but not in `another_select_stmt`.<br/><br/>**DISTINCT** returns only unique rows from these results _(implied default)_.<br/><br/>With **ALL** specified, each record occurs a number of times equal to the times it occurs in `select_stmt` less the times it occurs in `another_select_stmt`, or not at all if the former is greater than latter.

## Details

Expand Down
14 changes: 13 additions & 1 deletion src/sql-parser/src/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2446,10 +2446,22 @@ impl Parser {
break;
}
self.next_token(); // skip past the set operator

let all = self.parse_keyword("ALL");
let all_range = self.peek_prev_range();
let distinct = self.parse_keyword("DISTINCT");
let distinct_range = self.peek_prev_range();
if all && distinct {
return parser_err!(
self,
all_range.start..distinct_range.end,
"Cannot specify both ALL and DISTINCT in set operation"
);
}
expr = SetExpr::SetOperation {
left: Box::new(expr),
op: op.unwrap(),
all: self.parse_keyword("ALL"),
all,
right: Box::new(self.parse_query_body(next_precedence)?),
};
}
Expand Down
4 changes: 4 additions & 0 deletions www/assets/sass/_docs_body.scss
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,10 @@ main {
padding-bottom: 0.9rem;
}

td {
vertical-align: top;
}

svg {
margin-top: 2rem;
}
Expand Down
9 changes: 6 additions & 3 deletions www/layouts/partials/sql-grammar/bnf/select_stmt.bnf
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
select_stmt ::=
'SELECT' ( 'ALL' )? ( ( target_elem ) ( ( ',' target_elem ) )* ) ( 'FROM' ( ( table_expr ) ( ( ',' table_expr ) )* ) )? ( ( 'WHERE' expr ) )? ( 'GROUP' 'BY' ( ( col_ref ) ( ( ',' col_ref ) )* ) )? ( 'HAVING' expr )? ( 'ORDER' 'BY' ( ( col_ref ( 'ASC' | 'DESC' )? ) ( ( ',' col_ref ( 'ASC' | 'DESC' )? ) )* ) )? ( 'LIMIT' expr )? ( 'OFFSET' expr )?
| 'SELECT' ( 'DISTINCT' ) ( ( target_elem ) ( ( ',' target_elem ) )* ) ( 'FROM' ( ( table_expr ) ( ( ',' table_expr ) )* ) )? ( ( 'WHERE' expr ) )? ( 'GROUP' 'BY' ( ( col_ref ) ( ( ',' col_ref ) )* ) )? ( 'HAVING' expr )? ( 'ORDER' 'BY' ( ( col_ref ( 'ASC' | 'DESC' )? ) ( ( ',' col_ref ( 'ASC' | 'DESC' )? ) )* ) )? ( 'LIMIT' expr )? ( 'OFFSET' expr )?
| 'SELECT' ( 'DISTINCT' 'ON' '(' ( ( col_ref ) ( ( ',' col_ref ) )* ) ')' ) ( ( target_elem ) ( ( ',' target_elem ) )* ) ( 'FROM' ( ( table_expr ) ( ( ',' table_expr ) )* ) )? ( ( 'WHERE' expr ) )? ( 'GROUP' 'BY' ( ( col_ref ) ( ( ',' col_ref ) )* ) )? ( 'HAVING' expr )? ( 'ORDER' 'BY' ( ( col_ref ( 'ASC' | 'DESC' )? ) ( ( ',' col_ref ( 'ASC' | 'DESC' )? ) )* ) )? ( 'LIMIT' expr )? ( 'OFFSET' expr )?
'SELECT' (
( 'ALL' )? ( ( target_elem ) ( ( ',' target_elem ) )* ) ( 'FROM' ( ( table_expr ) ( ( ',' table_expr ) )* ) )? join_expr? ( ( 'WHERE' expr ) )? ( 'GROUP' 'BY' ( ( col_ref ) ( ( ',' col_ref ) )* ) )? ( 'HAVING' expr )? ( 'ORDER' 'BY' ( ( col_ref ( 'ASC' | 'DESC' )? ) ( ( ',' col_ref ( 'ASC' | 'DESC' )? ) )* ) )? ( 'LIMIT' expr )? ( 'OFFSET' expr )?
| ( 'DISTINCT' ) ( ( target_elem ) ( ( ',' target_elem ) )* ) ( 'FROM' ( ( table_expr ) ( ( ',' table_expr ) )* ) )? join_expr? ( ( 'WHERE' expr ) )? ( 'GROUP' 'BY' ( ( col_ref ) ( ( ',' col_ref ) )* ) )? ( 'HAVING' expr )? ( 'ORDER' 'BY' ( ( col_ref ( 'ASC' | 'DESC' )? ) ( ( ',' col_ref ( 'ASC' | 'DESC' )? ) )* ) )? ( 'LIMIT' expr )? ( 'OFFSET' expr )?
| ( 'DISTINCT' 'ON' '(' ( ( col_ref ) ( ( ',' col_ref ) )* ) ')' ) ( ( target_elem ) ( ( ',' target_elem ) )* ) ( 'FROM' ( ( table_expr ) ( ( ',' table_expr ) )* ) )? join_expr? ( ( 'WHERE' expr ) )? ( 'GROUP' 'BY' ( ( col_ref ) ( ( ',' col_ref ) )* ) )? ( 'HAVING' expr )? ( 'ORDER' 'BY' ( ( col_ref ( 'ASC' | 'DESC' )? ) ( ( ',' col_ref ( 'ASC' | 'DESC' )? ) )* ) )? ( 'LIMIT' expr )? ( 'OFFSET' expr )?
)
( ( 'UNION' | 'INTERSECT' | 'EXCEPT' ) ('ALL' | 'DISTINCT')? another_select_stmt)?
109 changes: 0 additions & 109 deletions www/layouts/partials/sql-grammar/rr-diagrams/select.html

This file was deleted.

Loading

0 comments on commit cadea93

Please sign in to comment.