Commit graph

472 commits

Author SHA1 Message Date
Nikhil Benesch
b5621c0fe8
Don't lose precision when parsing decimal fractions
The SQL standard requires that numeric literals with a decimal point,
like 1.23, are represented exactly, up to some precision. That means
that parsing these literals into f64s is invalid, as it is impossible
to represent many decimal numbers exactly in binary floating point (for
example, 0.3).

This commit parses all numeric literals into a new `Value` variant
`Number(String)`, removing the old `Long(u64)` and `Double(f64)`
variants. This is slightly less convenient for downstream consumers, but
far more flexible, as numbers that do not fit into a u64 and f64 are now
representable.
2019-09-01 13:21:30 -04:00
Nikhil Benesch
2bef9ec30a
Merge pull request #121 from andygrove/ci-fmt
Check that CI enforces rustfmt
2019-08-27 18:15:43 -04:00
Nikhil Benesch
35a20091ea
Merge pull request #134 from umanwizard/expect_keywords
add `expect_keywords` function
2019-08-13 16:00:38 -04:00
Brennan Vincent
41d4ea480f
Add and use expect_keywords function
The code for parsing chains of expected keywords is more readable with
this helper function.

Co-authored-by: Nikhil Benesch <nikhil.benesch@gmail.com>
2019-08-13 15:42:14 -04:00
Nikhil Benesch
070d0192bf
Add missing license header 2019-08-13 15:04:42 -04:00
Nikhil Benesch
bc97543df3
Actually check rustfmt in CI
The previous incantation was simply *never* running rustfmt in CI,
rather than only running rustfmt on the nightly build.
2019-08-13 15:03:00 -04:00
Nickolay Ponomarev
391a54b5a3
Merge pull request #131 from nickolay/pr/refactoring
Assorted code simplification and doc improvements
2019-07-11 02:19:43 +03:00
Nickolay Ponomarev
086ba1281c Amend WindowFrame docs
The note about WindowFrameBound::Following being only valid "in
WindowFrame::end_bound" was both

- confusing, as it was based on the ANSI SQL syntax the parser doesn't
  adhere to -- though it sounded like a promise about the AST one could
  expect to get from the parser
- and incomplete, as the reality is that the bounds validation the SQL
  engine might want to perform is more complex. For example Postgres
  documentation says <https://www.postgresql.org/docs/11/sql-expressions.html#SYNTAX-WINDOW-FUNCTIONS>:

> Restrictions are that frame_start cannot be UNBOUNDED FOLLOWING,
> frame_end cannot be UNBOUNDED PRECEDING, and the frame_end choice
> cannot appear earlier in the above list of frame_start and frame_end
> options than the frame_start choice does — for example RANGE BETWEEN
> CURRENT ROW AND offset PRECEDING is not allowed. But, for example,
> ROWS BETWEEN 7 PRECEDING AND 8 PRECEDING is allowed, even though it
> would never select any rows.
2019-07-09 03:27:20 +03:00
Nickolay Ponomarev
f31636d339 Simplify parse_window_frame
It used to consume the `RParen` closing the encompassing `OVER (`, even
when no window frame was parsed, which confused me a bit, even though
I wrote it initially.

After fixing that, I took the opportunity to reduce nesting and
duplication a bit.
2019-07-09 03:27:20 +03:00
Nickolay Ponomarev
9314371d3b Remove parse_expr_list, as it's now trivial 2019-07-09 03:27:20 +03:00
Nickolay Ponomarev
03efcf6fa6 Add parse_comma_separated to simplify the parser
To use the new helper effectively, a few related changes were required:

- Each of the parse_..._list functions (`parse_cte_list`,
  `parse_order_by_expr_list`, `parse_select_list`) was replaced with a
  version that parses a single element of the list (e.g. `parse_cte`),
  with their callers now using
  `self.parse_comma_separated(Parser::parse_<one_element>)?`

- `parse_with_options` now parses the WITH keyword and a separate
  `parse_sql_option` function (named after the struct it produces) was
  added to parse a single k=v option.

- `parse_list_of_ids` is gone, with the '.'-separated parsing moved to
  `parse_object_name`.


Custom comma-separated parsing is still used in:
- parse_transaction_modes (where the comma separator is optional)
- parse_columns (allows optional trailing comma, before the closing ')')
2019-07-09 03:27:20 +03:00
Nickolay Ponomarev
f11d74a64d Update Statement::Drop doc comment 2019-07-09 03:27:20 +03:00
Nickolay Ponomarev
64e7be0c68 Move ObjectName closer to Ident in mod.rs 2019-07-09 03:27:20 +03:00
Nickolay Ponomarev
ac8ba107e3 Simplify Display implementations introduced in #124 2019-07-09 01:38:49 +03:00
Nickolay Ponomarev
1b31f03732 Change write! to write_str for better rustfmt formatting 2019-07-09 01:38:49 +03:00
Nickolay Ponomarev
275e6b13c9
Merge pull request #129 from nickolay/pr/changelog
Add a changelog
2019-07-09 01:17:00 +03:00
Nickolay Ponomarev
7c153e1ab0 Add changelog 2019-07-06 03:47:24 +03:00
Nikhil Benesch
8cd64e7b94
Merge pull request #128 from andygrove/readme
README: update for recent improvements
2019-07-03 10:30:35 -04:00
Nikhil Benesch
4172b68319
README: update for recent improvements
Remove outdated bits that claim shoddy SQL support and code
structure--we're much better on those fronts now! Also add a few
paragraphs about the current state of SQL compliance, why it's hard to
say anything detailed about SQL compliance, and what our long-term goals
are.
2019-07-03 01:39:14 -04:00
Andy Grove
7a4eb505b3 (cargo-release) start next development iteration 0.4.0 2019-07-01 18:19:02 -06:00
Andy Grove
4f805d6c96 (cargo-release) version 0.4.0 2019-07-01 18:17:47 -06:00
Nikhil Benesch
ed76cd68f8
Merge pull request #124 from vemoo/impl-display
implement fmt::Display instead of ToString
2019-07-01 16:24:53 -04:00
Nickolay Ponomarev
b6538592da
Merge pull request #126 from nickolay/pr/renames-comments
Update comments after the renaming done in PR #105
2019-07-01 04:56:18 +03:00
Nickolay Ponomarev
7d4b488336 Update comments after the renaming done in PR #105 2019-07-01 04:45:08 +03:00
Bernardo
b2b159fed1 implement fmt::Display instead of ToString 2019-06-30 17:32:51 +02:00
Nikhil Benesch
cdba43682f
Merge pull request #123 from benesch/eq
Implement Eq alongside Hash
2019-06-25 17:45:12 -04:00
Jamie Brandon
add898c2bd
Implement Eq alongside Hash
It turns out implementing Hash alone is not very useful, as
std::collection::HashMap keys are required to implement both Hash and
Eq.

Co-authored-by: Nikhil Benesch <nikhil.benesch@gmail.com>
2019-06-25 14:14:28 -04:00
Nikhil Benesch
f7199ec99f
Merge pull request #122 from benesch/sqlprefix-missed
Remove "SQL" prefix from "SQLDateTimeField" struct
2019-06-25 14:13:26 -04:00
Nikhil Benesch
106c9f8efb
Remove "SQL" prefix from "SQLDateTimeField" struct
I realized a moment too late that I'd missed a type name in
when removing the "SQL" prefix from types in ac555d7e8. As far as I can
tell, this was the only oversight.
2019-06-25 13:24:31 -04:00
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