Commit graph

252 commits

Author SHA1 Message Date
joshwd36
a95c81fb13
Add referential actions to TableConstraint foreign key (#306)
* Add referential actions to TableConstraint foreign key

* Remove copy/paste error

* Add referential actions to TableConstraint foreign key

* Add additional tests
2021-08-25 12:03:10 -04:00
Jiseok CHOI
af1ac865b1
Add TrimWhereField for parse_trim_expr (#334)
* Remove Box from `trim_where`

* Add TrimWhereField for `parse_trim_expr`

* Add negative test case
2021-08-23 10:51:36 -04:00
Andy Grove
5ebc06b20c Update links to reflect repository move to sqlparser-rs GitHub org 2021-08-20 17:03:06 -06:00
Jiayu Liu
950daf39bb add default value for window frame 2021-08-20 13:55:53 -04:00
Jiseok CHOI
e548d38de8
Support TRIM syntax (#331)
* Support TRIM syntax

- Implement the following cases
- TRIM([BOTH | LEADING | TRAILING] <expr> [FROM <expr>])
- TRIM(<expr>)

* Resolve 1.54.0 clippy error

- Remove needless borrow

* Add test cases for TRIM
2021-08-20 08:53:52 +02:00
Qinxuan Chen
67e17b27f5
Make clippy happy (#330)
Signed-off-by: koushiro <koushiro.cqx@gmail.com>
2021-08-19 23:02:15 +02:00
sundyli
e5991f3ae5
Support tinyint (#320)
* Support tinyint

* Add tests
2021-07-17 14:16:38 +02:00
Daniël Heres
d8775e2815
Fix linting errors (#318)
* Fix linting error

* Fix linting errors
2021-06-22 07:49:25 +02:00
BohuTANG
56e50dccd4
[parser] Return error instead of panic (#316)
* [parser] return error instead of panic

* Fix clippy warning

* Fix cargo fmt warning
2021-06-22 07:05:43 +02:00
Max Countryman
a9e6f77d62 provide ILIKE support
This introduces support for ILIKE and NOT ILIKE. ILIKE is the
case-insensitive variant of LIKE. Systems such as Postgres, Redshift,
and Snowflake provide this variant.[1][2][3]

[1] https://www.postgresql.org/docs/7.3/functions-matching.html
[2] https://docs.aws.amazon.com/redshift/latest/dg/r_patternmatching_condition_like.html
[3] https://docs.snowflake.com/en/sql-reference/functions/ilike.html
2021-03-22 07:31:42 -07:00
Mike Seddon
e6e37b47db
Implement TRY_CAST (#299)
Adds support for `TRY_CAST` and fixes a clippy error
2021-03-21 23:26:16 +01:00
zhangli-pear
add8991144
feat: support sqlite insert or statement (#281) 2021-02-09 21:04:54 +01:00
Daniël Heres
6f0b2dcd92
Implement SUBSTRING(col [FROM <expr>] [FOR <expr>]) syntax (#293) 2021-02-07 08:06:50 -07:00
Stephen Carman
8a214f9919
Implement Hive QL Parsing (#235) 2021-02-04 12:53:20 -07:00
Daniël Heres
94ff46802c
Support ANALYZE TABLE syntax (#285)
* Support analyze table

* Cleanup
2020-12-28 10:08:32 -07:00
Dmitry Patsura
17f2930885
Introduce support for EXPLAIN [ANALYZE] [VERBOSE] <STATEMENT> syntax
Introduce support for EXPLAIN [ANALYZE] [VERBOSE] <STATEMENT> syntax
2020-12-28 12:22:03 +01:00
Nickolay Ponomarev
929fc6764f
Merge pull request #260 from eyalleshem/single_tables_in_parens
[snowflake] Support `FROM (table_name) alias`
2020-10-13 09:59:38 +03:00
Nickolay Ponomarev
ad72cda6b0 [snowflake] Support specifying an alias after FROM (table_factor)
Snowflake diverges from the standard and from most of the other
implementations by allowing extra parentheses not only around a join,
but around lone table names (e.g. `FROM (mytable [AS alias])`) and
around derived tables (e.g. `FROM ((SELECT ...)  [AS alias])`) as well.

Initially this was implemented in https://github.com/ballista-compute/sqlparser-rs/issues/154
by (ab)using `TableFactor::NestedJoin` to represent anything nested in
extra set of parens.

Afterwards we learned in https://github.com/ballista-compute/sqlparser-rs/issues/223
that in cases of such extraneous nesting Snowflake allows specifying the
alias both inside and outside parens, but not both - consider:

    FROM (table_factor AS inner_alias) AS outer_alias

We've considered implementing this by changing `TableFactor::NestedJoin`
to a `TableFactor::Nested { inner: TableWithJoins, alias:
Option<TableAlias> }`, but that seemed too generic, as no known dialect
supports duplicate aliases, as shown above, nor naming nested joins
`(foo NATURAL JOIN bar) alias`. So we decided on making a smaller change
(with no modifications to the AST), that is also more appropriate to the
contributors to the Snowflake dialect:


1) Revert #154 by rejecting `FROM (table or derived table)` in most dialects.

2) For `dialect_of!(self is SnowflakeDialect | GenericDialect)` parse
and strip the extraneous parentheses, e.g.

   `(mytable) AS alias` -> `(mytable AS alias)`


Co-authored-by: Eyal Leshem <eyal@satoricyber.com>
2020-10-13 09:51:02 +03:00
Nickolay Ponomarev
4128dfe1db Introduce tests/test_utils/mod.rs and use it consistently
To share helper macros between various tests/* we added a new module
(tests/macros/mod.rs). This made the prologue to be used in tests quite
long and a little weird:
```
#[macro_use]
#[path = "macros/mod.rs"]
mod macros;
use sqlparser::test_utils::*;
```

This simplifies it to:
```
#[macro_use]
mod test_utils;
use test_utils::*;
```
- and switches all existing tests to the new prologue simultaneously...

...while fixing a few other inconsistencies and adding a few comments
about the way `test_utils` work.
2020-10-12 06:52:00 +03:00
rhanqtl
9f772f03b0
Add support for Recursive CTEs (#278)
i.e. `WITH RECURSIVE ... AS ( ... ) SELECT` - see https://jakewheat.github.io/sql-overview/sql-2016-foundation-grammar.html#with-clause

Fixes #277
2020-10-11 09:43:51 +03:00
Nickolay Ponomarev
99fb633221 Move existing SF tests to sqlparser_snowflake.rs
Co-authored-by: Eyal Leshem <eyal@satoricyber.com>
2020-10-05 08:42:26 +03:00
Alex Dukhno
1ac208307c
Support IF NOT EXISTS for CREATE SCHEMA (#276)
This is a Postgres-specific clause: https://www.postgresql.org/docs/12/sql-createschema.html

Also add a test for `DROP SCHEMA IF EXISTS schema_name`, which is already supported in the parser.
2020-10-02 17:35:20 +03:00
Alex Dukhno
926b03a31d
Add parsing for PostgreSQL math operators (#267) 2020-09-30 05:29:31 +03:00
Daniël Heres
a5b752484e
Fix clippy linting error, use enumerate (#266) 2020-08-27 21:32:06 +02:00
Nickolay Ponomarev
66505ebf9e Don't fail parsing a column definition with unexpected tokens
Since PR https://github.com/ballista-compute/sqlparser-rs/pull/93
`parse_column_def` parses a set of column options in a loop, e.g. given:

```
                  _______ column_def _______
CREATE TABLE foo (bar INT NOT NULL DEFAULT 1, )
                          -------- ---------
                          option 1  option 2
````

it parses column options until it encounters one of the delimiter tokens

First when we only supported `CREATE TABLE`, the set of delimiters that
stopped the parsing used to be `Token::Comma | Token::RParen`.

Then we added support for `ALTER TABLE ADD COLUMN <column_def>`. Turns
out the parser started to bail if the statement ended with a semicolon,
while attempting to parse the semicolon as a column option, as we forgot
to add it to the set of delimiter tokens.

This was recently fixed in https://github.com/ballista-compute/sqlparser-rs/pull/246
by including Token::SemiColon to the list, but it felt wrong to have
to update this list, and to have a common list of delimiters for two
different contexts (CREATE TABLE with parens vs ALTER TABLE ADD COLUMN
without parens).

Also our current approach cannot handle multiple statements NOT
separated by a semicolon, as is common in MS SQL DDL. We don't
explicitly support it in `parse_statements`, but that's a use-case
like to keep in mind nevertheless.
2020-08-10 17:12:33 +03:00
eyalleshem
1b46e82eec
Enable dialect specific behaviours in the parser (#254)
* Change `Parser { ... }` to store the dialect used:
    `Parser<'a> { ... dialect: &'a dyn Dialect }`

    Thanks to @c7hm4r for the initial version of this submitted as
    part of https://github.com/ballista-compute/sqlparser-rs/pull/170

* Introduce `dialect_of!(parser is SQLiteDialect |  GenericDialect)` helper
    to branch on the dialect's type

* Use the new functionality to make `AUTO_INCREMENT` and `AUTOINCREMENT`
  parsing dialect-dependent.


Co-authored-by: Christoph Müller <pmzqxfmn@runbox.com>
Co-authored-by: Nickolay Ponomarev <asqueella@gmail.com>
2020-08-10 16:51:59 +03:00
eyalleshem
61431b087d
Support TABLE functions in FROM (#253)
Support `TABLE(...)` syntax in `FROM`, for example:

    select * from TABLE(SOME_FUNCTION(some_arg))

The ANSI spec allows routine invocations (and some other kinds of expressions we don't currently support) inside TABLE:
https://jakewheat.github.io/sql-overview/sql-2016-foundation-grammar.html#PTF-derived-table
https://jakewheat.github.io/sql-overview/sql-2016-foundation-grammar.html#table-function-derived-table
2020-08-05 08:59:43 +03:00
eyalleshem
1cc3bf4099
Support named arguments in function invocations (#250)
This commit supports functions with argument names.

the format is :
"Select some_function( a => exp, b => exp2 .. ) FROM table1
OR
"select * from table(function(a => exp)) f;"

see:
https://jakewheat.github.io/sql-overview/sql-2016-foundation-grammar.html#named-argument-assignment-token
or the motivating example from snowflake:
https://docs.snowflake.com/en/sql-reference/functions/flatten.html
2020-08-02 08:04:55 +03:00
mz
9c1a5a781d
Don't fail parsing ALTER TABLE ADD COLUMN ending with a semicolon (#246)
This is a follow-up to https://github.com/ballista-compute/sqlparser-rs/pull/203
where ALTER TABLE ADD COLUMN support was initially implemented.

Fixes #233.
2020-07-31 18:10:53 +03:00
mz
4452f9bad1
Support specifying ASC/DESC in index columns (#249)
...by reusing `OrderByExpr` for `columns` in `Statement::CreateIndex`.

This supports SQLite's indexed-column syntax https://www.sqlite.org/syntax/indexed-column.html

MSSQL's (`ON <object> ( column [ ASC | DESC ] [ ,...n ] )`)
https://docs.microsoft.com/en-us/sql/t-sql/statements/create-index-transact-sql?view=sql-server-ver15

And most of PostgreSQL syntax (except for opclass):
`( { column_name | ( expression ) } [ COLLATE collation ] [ opclass ] [ ASC | DESC ] [ NULLS { FIRST | LAST } ] [, ...] )`
https://www.postgresql.org/docs/12/sql-createindex.html
2020-07-30 15:37:58 +03:00
Nickolay Ponomarev
9a2d86dcb5 Change CREATE INDEX serialization to not end with a semicolon 2020-07-29 02:08:17 +03:00
Daniël Heres
d2e4340a32
Support create or replace view/table (#239)
* Support create or replace table

* Support create or replace view

* Simplify create or replace table parser

* Add tests for create or replace external table and materialized view

* Formatting

* Address review comments

* Create error if we didn't see a (external) table or (materialized) view afer create or replace
2020-07-27 21:59:08 +02:00
Daniël Heres
583f22b929
Remove PostgreSQL version of assert (#229)
Remove PostgreSQL procedural assert statement. This also simplifies code somewhat.
2020-07-17 13:20:49 +02:00
Daniël Heres
c24b0e01db
Implement ASSERT statement (#226)
As supported by PostgreSQL and BigQuery (with some differences between them)
2020-07-16 17:28:03 +02:00
Max Countryman
8cc7702a8c
update branch references to main (#215)
* update branch references to `main`

* ensure we point to ballista-compute

* update a couple of links to point to ballista-compute
2020-07-02 21:31:54 +02:00
mz
0c83e5d9e8
Support SQLite's WITHOUT ROWID in CREATE TABLE (#208)
Per https://sqlite.org/lang_createtable.html

Co-authored-by: mashuai <mashuai@bytedance.com>
2020-06-26 15:11:46 +03:00
Daniël Heres
15d5f71646
Add CREATE TABLE AS support (#206)
We parse it as a regular `CREATE TABLE` statement
followed by an `AS <query>`, which is how BigQuery works:
https://cloud.google.com/bigquery/docs/reference/standard-sql/data-definition-language#create_table_statement


ANSI SQL and PostgreSQL only support a plain list of columns
after the table name in a CTAS
    `CREATE TABLE t (a) AS SELECT a FROM foo`

We currently only allow specifying a full schema with data
types, or omitting it altogether.

https://www.postgresql.org/docs/12/sql-createtableas.html
https://jakewheat.github.io/sql-overview/sql-2016-foundation-grammar.html#as-subquery-clause


Finally, when no schema is specified, we print empty parens after a
plain `CREATE TABLE t ();` as required by PostgreSQL, but skip them
in a CTAS: `CREATE TABLE t AS ...`. This affects serialization only,
the parser allows omitting the schema in a regular `CREATE TABLE` too
since the first release of the parser:
7d27abdfb4/src/sqlparser.rs (L325-L332)

Co-authored-by: Nickolay Ponomarev <asqueella@gmail.com>
2020-06-23 16:30:22 +03:00
Jovansonlee Cesar
26361fd854
Implement ALTER TABLE DROP COLUMN (#148)
This implements `DROP [ COLUMN ] [ IF EXISTS ] column_name [ CASCADE ]`
sub-command of `ALTER TABLE`, which is what PostgreSQL supports https://www.postgresql.org/docs/12/sql-altertable.html
(except for the RESTRICT option)

Co-authored-by: Nickolay Ponomarev <asqueella@gmail.com>
2020-06-16 23:39:52 +03:00
mz
faeb7d440a
Implement ALTER TABLE ADD COLUMN and RENAME (#203)
Based on sqlite grammar
https://www.sqlite.org/lang_altertable.html
2020-06-16 22:52:37 +03:00
Daniël Heres
fab6e28271
Output DataType capitalized (#202)
This makes it consistent with other output which also prints keywords capitalized.
2020-06-13 16:18:44 +03:00
Daniël Heres
68afa2a764
Make FileFormat case insensitive (#200) 2020-06-12 18:10:44 +03:00
Max Countryman
6cdd4a146d
Support general "typed string" literals (#187)
Fixes #168 by enabling `DATE` and other keywords to be used as
identifiers when not followed by a string literal.

A "typed string" is our term for generalized version of `DATE '...'`/`TIME '...'`/
`TIMESTAMP '...'` literals, represented as `TypedString { data_type, value }`
in the AST.

Unlike DATE/TIME/TIMESTAMP literals, this is a non-standard extension
supported by PostgreSQL at least.

This is a port of MaterializeInc/materialize#3146

Co-authored-by: Nikhil Benesch <nikhil.benesch@gmail.com>
Co-authored-by: Nickolay Ponomarev <asqueella@gmail.com>
2020-06-12 00:04:43 +03:00
Daniël Heres
34548e890b
Change Word::keyword to a enum (#193)
This improves performance and paves the way to future API enhancements as discussed in the PR https://github.com/andygrove/sqlparser-rs/pull/193
2020-06-11 22:00:35 +03:00
Max Countryman
846c52f450
Allow omitting units after INTERVAL (#184)
Alter INTERVAL to support postgres syntax

This patch updates our INTERVAL implementation such that the Postgres
and Redshfit variation of the syntax is supported: namely that 'leading
field' is optional.

Fixes #177.
2020-06-10 09:32:13 +03:00
Daniël Heres
a42121de52
Use binary search to speed up matching keywords (#191) 2020-06-07 20:25:10 +03:00
Daniël Heres
b4699bd4a7
Support bitwise and, or, xor (#181)
Operator precedence is coming from:

https://cloud.google.com/bigquery/docs/reference/standard-sql/operators
2020-06-03 19:02:05 +03:00
Daniël Heres
00dc490f72
Support the string concat operator (#178)
The selected precedence is based on BigQuery documentation, where it is equal to `*` and `/`:

https://cloud.google.com/bigquery/docs/reference/standard-sql/operators
2020-06-02 21:24:30 +03:00
Max Countryman
5f3c1bda01
Provide LISTAGG implementation (#174)
This patch provides an initial implemenation of LISTAGG[1]. Notably this
implemenation deviates from ANSI SQL by allowing both WITHIN GROUP and
the delimiter to be optional. We do so because Redshift SQL works this
way and this approach is ultimately more flexible.

Fixes #169.

[1] https://modern-sql.com/feature/listagg
2020-05-30 18:50:17 +03:00
QP Hou
418b9631ce
add nulls first/last support to order by expression (#176)
Following `<sort specification list>` from the standard https://jakewheat.github.io/sql-overview/sql-2016-foundation-grammar.html#_10_10_sort_specification_list
2020-05-30 17:05:15 +03:00
Alex Dukhno
91f769e460 added create and drop schema 2020-05-28 19:50:16 +03:00