Commit graph

693 commits

Author SHA1 Message Date
Nikhil Benesch
bafb20746f
Merge pull request #105 from benesch/renames
Remove "SQL" from types (and other renames)
2019-06-25 13:16:13 -04:00
Nikhil Benesch
ac555d7e86
Remove "SQL" prefix from types
The rationale here is the same as the last commit: since this crate
exclusively parses SQL, there's no need to restate that in every type
name. (The prefix seems to be an artifact of this crate's history as a
submodule of Datafusion, where it was useful to explicitly call out
which types were related to SQL parsing.)

This commit has the additional benefit of making all type names
consistent; over type we'd added some types which were not prefixed with
"SQL".
2019-06-25 13:11:11 -04:00
Nikhil Benesch
cf655ad1a6
Remove "sql" prefix from module names
Since this crate only deals with SQL parsing, the modules are understood
to refer to SQL and don't need to restate that explicitly.
2019-06-24 12:56:26 -04:00
Nikhil Benesch
5b23ad1d4c
Merge pull request #119 from andygrove/astnode-expr
Rename ASTNode to Expr
2019-06-19 21:45:49 -04:00
Nickolay Ponomarev
3c401d5e4f
Merge pull request #120 from nickolay/pr/outer-apply
[mssql/oracle] Support CROSS/OUTER APPLY
2019-06-19 13:26:35 +03:00
Nikhil Benesch
646d1e13ca
Rename ASTNode to Expr
The ASTNode enum was confusingly named. In the past, the name made
sense, as the enum contained nearly all of the nodes in the AST, but
over time, pieces have been split into different structs, like
SQLStatement and SQLQuery. The ASTNode enum now contains only contains
expression nodes, so Expr is a better name.

Also rename the UnnamedExpression and ExpressionWithAlias variants
of SQLSelectItem to UnnamedExpr and ExprWithAlias, respectively, to
match the new shorthand for the word "expression".
2019-06-19 00:00:59 -04:00
Nickolay Ponomarev
4294581ded [mssql] Parse CROSS/OUTER APPLY
T-SQL (and Oracle) support non-standard syntax, which is similar in
functionality to LATERAL joins in ANSI and PostgreSQL
<https://blog.jooq.org/tag/lateral-derived-table/>: it allows to use
the columns from the tables defined to the left of `APPLY` in the
"derived tables" (subqueries) to the right of `APPLY`. Unlike ANSI
LATERAL (but like Postgres' implementation), APPLY is also used with
table-valued function calls.

Despite them being similar, we represent "APPLY" joins with
`JoinOperator`s of its own (`CrossApply` and `OuterApply`). Doing
otherwise seemed like it would cause unnecessary confusion, as those
interested in dialect-specific parsing would probably not expect APPLY
being parsed as LATERAL, and those wanting to forbid non-standard SQL
would not be helped by this either.

This also renames existing JoinOperator::Cross -> CrossJoin to avoid
confusion with CrossApply.
2019-06-19 02:45:47 +03:00
Nickolay Ponomarev
0f6bf15258 Fix bad merge in #118 2019-06-19 02:45:47 +03:00
Nikhil Benesch
e6b26330df
Merge pull request #118 from andygrove/outer-join
Don't silently accept naked OUTER JOINS
2019-06-18 13:15:25 -04:00
Nikhil Benesch
2c99635709
Don't silently accept naked OUTER JOINS
`SELECT * FROM a OUTER JOIN b` was previously being parsed as an inner
join where table `a` was aliased to `OUTER`. This is extremely
surprising, as the user likely intended to say FULL OUTER JOIN. Since
the SQL specification lists OUTER as a keyword, we are well within our
rights to return an error here.
2019-06-18 12:03:15 -04:00
Nickolay Ponomarev
7857543749
Merge pull request #116 from nickolay/pr/more-followups
Support HAVING/LIMIT/OFFSET/FETCH without FROM and other follow-ups
2019-06-18 02:58:31 +03:00
Nickolay Ponomarev
a37ba089ec Add a comment about RESERVED_FOR_TABLE_ALIAS to parse_table_and_joins 2019-06-17 10:55:20 +03:00
Nickolay Ponomarev
eb3450dd51 Support HAVING without GROUP BY
...which is weird but allowed:
https://jakewheat.github.io/sql-overview/sql-2011-foundation-grammar.html#table-expression
https://dba.stackexchange.com/a/57453/15599

Also add a test for GROUP BY .. HAVING
2019-06-17 01:06:32 +03:00
Nickolay Ponomarev
d60bdc0b92 Allow LIMIT/OFFSET/FETCH without FROM
Postgres allows it, as does ANSI SQL per the <query expression> definition:
https://jakewheat.github.io/sql-overview/sql-2011-foundation-grammar.html#_7_13_query_expression
2019-06-17 00:54:37 +03:00
Nickolay Ponomarev
c1509b36ec Use FETCH_FIRST_TWO_ROWS_ONLY in tests to reduce duplication 2019-06-17 00:49:25 +03:00
Nickolay Ponomarev
f87e8d5158 Don't duplicate all the parse_simple_select assertions in the LIMIT test 2019-06-17 00:39:00 +03:00
Nickolay Ponomarev
3c073a4c34 Use TableAlias in Cte 2019-06-16 21:18:57 +03:00
Nickolay Ponomarev
4f239745bc Update comments now that get_precedence is no more 2019-06-16 21:01:29 +03:00
Nickolay Ponomarev
dc26c4abd5
Merge pull request #115 from nickolay/pr/followups
Doc improvements and follow-ups to the recent PRs
2019-06-15 01:57:52 +03:00
Nikhil Benesch
98a06d6d23
Merge pull request #111 from benesch/join-tweaks
Refine join parsing
2019-06-14 16:45:06 -04:00
Nickolay Ponomarev
535505bb96
Update the error message in parse_query_body 2019-06-14 16:28:53 -04:00
Nikhil Benesch
4ee461bae4
Require that nested joins always have one join
The SQL specification prohibits constructions like

    SELECT * FROM a NATURAL JOIN (b)

where b sits alone inside parentheses. Parentheses in a FROM entry
always introduce either a derived table or a join.
2019-06-14 16:28:52 -04:00
Nikhil Benesch
8bee74277a
Handle derived tables with set operations
This commit adds support for derived tables (i.e., subqueries) that
incorporate set operations, like:

    SELECT * FROM (((SELECT 1) UNION (SELECT 2)) t1 AS NATURAL JOIN t2)

This introduces a bit of complexity around determining whether a left
paren starts a subquery, starts a nested join, or belongs to an
already-started subquery. The details are explained in a comment within
the patch.
2019-06-14 16:28:52 -04:00
Nickolay Ponomarev
5c7ff79e78 Add a test for parsing the NULL literal
(Coveralls notices we didn't have one.)
2019-06-13 11:17:36 +03:00
Nickolay Ponomarev
45c9aa1cc2 Use self.expected() more 2019-06-13 11:15:10 +03:00
Nickolay Ponomarev
f18fbe5cda Remove unused parse_literal_double
`parse_value` handles parsing a non-integer value in expression context,
and we use parse_literal_uint() when expecting a number in other 
contexts (such as `FETCH FIRST ... ROWS`)
2019-06-13 11:11:23 +03:00
Nickolay Ponomarev
7041850b33 Reduce nesting in parse_table_and_joins
This is a follow-up to #109 which moved the handling of Token::Comma
from parse_table_and_joins to the `FROM` parser.
2019-06-13 04:30:14 +03:00
Nickolay Ponomarev
32cf36e64f Add a testcase, which passes thanks to PR #109 2019-06-12 21:04:31 +03:00
Nickolay Ponomarev
6b0a396785 Remove table_key.rs accidentally re-added in PR #79 2019-06-12 21:04:31 +03:00
Nickolay Ponomarev
cfb77912f7 Mention SQLSelectItem::Wildcard in ASTNode::SQLWildcard docs
as a follow-up to https://github.com/andygrove/sqlparser-rs/issues/52
2019-06-12 21:04:31 +03:00
Nickolay Ponomarev
846f2ff1d9 Fix intra_doc_link_resolution_failure in cargo doc
..and other formatting errors.
2019-06-12 21:04:27 +03:00
Nickolay Ponomarev
26fac099b1 Update Value docs 2019-06-12 21:04:23 +03:00
Andy Grove
1998910bfa
Merge pull request #113 from andygrove/revert-78-visitor
Revert "Add an AST visitor"
2019-06-10 21:56:42 -06:00
Andy Grove
b07c7a90f1
Revert "Add an AST visitor" 2019-06-10 21:51:10 -06:00
Andy Grove
7ed6e03880
Merge pull request #78 from benesch/visitor
Add an AST visitor
2019-06-10 21:19:45 -06:00
Nikhil Benesch
f2fda57e36
Merge pull request #112 from benesch/expr-consistency
Improve consistency of binary/unary op nodes
2019-06-10 23:08:33 -04:00
Nikhil Benesch
ae25dce246
Split operators by arity
It is useful downstream to have two separate enums, one for unary
operators and one for binary operators, so that the compiler can check
exhaustiveness. Otherwise downstream consumers need to manually encode
which operators are unary and which operators are binary when matching
on an Operator enum.
2019-06-10 23:03:11 -04:00
Nikhil Benesch
9e33cea9b8
Standardize BinaryOp and UnaryOp nodes
These were previously called "BinaryExpr" and "Unary"; besides being
inconsistent, it's also not correct to say "binary expression" or "unary
expression", as it's the operators that have arities, not the
expression. Adjust the naming of the variants accordingly.
2019-06-10 23:02:17 -04:00
Andy Grove
b379480b7a
Merge pull request #79 from benesch/license
Standardize license headers
2019-06-10 19:39:12 -06:00
Nikhil Benesch
5896f01ce0
Merge pull request #109 from benesch/implicit-join-fix
Properly handle mixed implicit and explicit joins
2019-06-10 11:17:01 -04:00
Nikhil Benesch
ce171c2a3d
Support VALUES and parenthesized SELECTs as top-level statements 2019-06-10 11:11:26 -04:00
Nikhil Benesch
b841dccc2c
Properly handle mixed implicit and explicit joins
Parse a query like

    SELECT * FROM a NATURAL JOIN b, c NATURAL JOIN d

as the SQL specification requires, i.e.:

    from: [
        TableReference {
            relation: TableFactor::Table("a"),
            joins: [Join {
                relation: TableFactor::Table("b"),
                join_operator: JoinOperator::Natural,
            }]
        },
        TableReference {
            relation: TableFactor::Table("c"),
            joins: [Join {
                relation: TableFactor::Table("d"),
                join_operator: JoinOperator::Natural,
            }]
        }
    ]

Previously we were parsing such queries as

    relation: TableFactor::Table("a"),
    joins: [
        Join {
            relation: TableFactor::Table("b"),
            join_operator: JoinOperator::Natural,
        },
        Join {
            relation: TableFactor::Table("c"),
            join_operator: JoinOperator::Implicit,
        },
        Join {
            relation: TableFactor::Table("d"),
            join_operator: JoinOperator::Natural,
        },
    ]

which did not make the join hierarchy clear.
2019-06-10 11:11:25 -04:00
Nickolay Ponomarev
518c8833d2
Merge pull request #110 from nickolay/pr/cleanups
Minor code clean-ups
2019-06-09 20:24:13 +03:00
Nikhil Benesch
2d1e05e21d
Merge pull request #103 from benesch/intervals
Support interval literals
2019-06-09 12:41:57 -04:00
Nikhil Benesch
2798ddf5fd
Support interval literals 2019-06-09 12:37:57 -04:00
Nickolay Ponomarev
99768711dc Fix redundant closures in tests
We don't have the tests checked by clippy on CI, despite [passing the
`--all-targets`](5536cd1f9e/.travis.yml (L46)),
but VSCode+RLS display warnings for these.

See: https://rust-lang.github.io/rust-clippy/master/index.html#redundant_closure
2019-06-09 17:05:19 +03:00
Nickolay Ponomarev
b6dac5099d Use the same pattern to parse comma-separated lists of things 2019-06-09 16:47:02 +03:00
Nickolay Ponomarev
ab88e02f0d Raise a TokenizerError when a delimited identifier is not closed before EOF 2019-06-09 16:42:23 +03:00
Nickolay Ponomarev
20637f0327 Introduce peeking_take_while to simplify tokenizer
I could probably look into using an existing crate like
https://github.com/fitzgen/peeking_take_while - but as a small helper
works as well I didn't have the reason to.
2019-06-09 16:42:23 +03:00
Nickolay Ponomarev
ebc5efda98 Reduce duplication in tokenizer by matching on Some('...') directly 2019-06-09 16:42:23 +03:00