mirror of
https://github.com/apache/datafusion-sqlparser-rs.git
synced 2025-07-08 17:35:00 +00:00
Compare commits
124 commits
v0.55.0-rc
...
main
Author | SHA1 | Date | |
---|---|---|---|
![]() |
93450cc250 | ||
![]() |
1a33abda63 | ||
![]() |
f2fba48a7a | ||
![]() |
cf9e50474e | ||
![]() |
ed8757f2f0 | ||
![]() |
d2466af20a | ||
![]() |
b0bcc46e22 | ||
![]() |
942d747d89 | ||
![]() |
239e30a97c | ||
![]() |
9020385c02 | ||
![]() |
be2d2f14e7 | ||
![]() |
418b94227a | ||
![]() |
015caca611 | ||
![]() |
a3398223d7 | ||
![]() |
f32a41a004 | ||
![]() |
9ffc546870 | ||
![]() |
abd80f9ecb | ||
![]() |
3bc94234df | ||
![]() |
50c605a471 | ||
![]() |
6c38cdcadb | ||
![]() |
5f2b5fe7be | ||
![]() |
95d16e3b2d | ||
![]() |
1bbc05cdff | ||
![]() |
b2ab0061c1 | ||
![]() |
b9365b3853 | ||
![]() |
44f3be38e5 | ||
![]() |
5d63663bc6 | ||
![]() |
7865de015f | ||
![]() |
1d0dc7cdd8 | ||
![]() |
204d3b484d | ||
![]() |
185a490218 | ||
![]() |
b1b379e570 | ||
![]() |
be30697efb | ||
![]() |
6f423969b0 | ||
![]() |
e406422bac | ||
![]() |
0f2208d293 | ||
![]() |
703ba2cf48 | ||
![]() |
c2e83d49f6 | ||
![]() |
9fc9009b94 | ||
![]() |
559b7e36d9 | ||
![]() |
40d12b98bd | ||
![]() |
84c3a1b325 | ||
![]() |
ff29dd25b2 | ||
![]() |
e2b1ae36e9 | ||
![]() |
4cf5e571d3 | ||
![]() |
de2cc7b502 | ||
![]() |
5327f0ce13 | ||
![]() |
394a534486 | ||
![]() |
80d47eee84 | ||
![]() |
a8bde39efb | ||
![]() |
eacf00d269 | ||
![]() |
9159d08c5e | ||
![]() |
301726541a | ||
![]() |
bf2b72fbe0 | ||
![]() |
05d7ffb1d5 | ||
![]() |
3f4d5f96ee | ||
![]() |
a496f78803 | ||
![]() |
525ed81fde | ||
![]() |
e7bf186e44 | ||
![]() |
ae587dcbec | ||
![]() |
3c59950060 | ||
![]() |
c6e897dc12 | ||
![]() |
74a95fdbd1 | ||
![]() |
178a351812 | ||
![]() |
6120bb59cc | ||
![]() |
052ad4a759 | ||
![]() |
2182f7ea71 | ||
![]() |
6cd237ea43 | ||
![]() |
ac1c339666 | ||
![]() |
a497358c3a | ||
![]() |
728645fb31 | ||
![]() |
a464f8e8d7 | ||
![]() |
483394cd1a | ||
![]() |
e5d2215267 | ||
![]() |
a5b9821d1d | ||
![]() |
c0921dceb9 | ||
![]() |
2b5bdcef0b | ||
![]() |
4e392f5c07 | ||
![]() |
7703fd0d31 | ||
![]() |
87d190734c | ||
![]() |
2eb1e7bdd4 | ||
![]() |
945f8e0534 | ||
![]() |
3ec80e187d | ||
![]() |
4a487290ce | ||
![]() |
81d8909e00 | ||
![]() |
3ad13af563 | ||
![]() |
514d2ecdaf | ||
![]() |
6566c47593 | ||
![]() |
896c088153 | ||
![]() |
bbc80d7537 | ||
![]() |
d090ad4ccf | ||
![]() |
67c3be075e | ||
![]() |
cfd8951452 | ||
![]() |
0d2976d723 | ||
![]() |
4deed26006 | ||
![]() |
610096cad8 | ||
![]() |
3ed4ad9c66 | ||
![]() |
a847e44105 | ||
![]() |
7efa686d78 | ||
![]() |
776b10afe6 | ||
![]() |
45420cedd6 | ||
![]() |
25bb871175 | ||
![]() |
91327bb0c0 | ||
![]() |
95d7b86da5 | ||
![]() |
be98b30eb3 | ||
![]() |
62495f2f0d | ||
![]() |
53aba68e2d | ||
![]() |
3a8a3bb7a5 | ||
![]() |
939fbdd4f6 | ||
![]() |
f487cbe004 | ||
![]() |
e3e88290cd | ||
![]() |
da5892802f | ||
![]() |
10cf7c164e | ||
![]() |
f81aed6359 | ||
![]() |
862e887a66 | ||
![]() |
cf4ab7f9ab | ||
![]() |
fb578bb419 | ||
![]() |
85f855150f | ||
![]() |
3392623b00 | ||
![]() |
1e54a34acd | ||
![]() |
6ec5223f50 | ||
![]() |
d5dbe86da9 | ||
![]() |
9e09b617e8 | ||
![]() |
a629ddf89b |
54 changed files with 13090 additions and 2734 deletions
39
.github/workflows/license.yml
vendored
Normal file
39
.github/workflows/license.yml
vendored
Normal file
|
@ -0,0 +1,39 @@
|
|||
# Licensed to the Apache Software Foundation (ASF) under one
|
||||
# or more contributor license agreements. See the NOTICE file
|
||||
# distributed with this work for additional information
|
||||
# regarding copyright ownership. The ASF licenses this file
|
||||
# to you under the Apache License, Version 2.0 (the
|
||||
# "License"); you may not use this file except in compliance
|
||||
# with the License. You may obtain a copy of the License at
|
||||
#
|
||||
# http://www.apache.org/licenses/LICENSE-2.0
|
||||
#
|
||||
# Unless required by applicable law or agreed to in writing,
|
||||
# software distributed under the License is distributed on an
|
||||
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
||||
# KIND, either express or implied. See the License for the
|
||||
# specific language governing permissions and limitations
|
||||
# under the License.
|
||||
|
||||
name: license
|
||||
|
||||
# trigger for all PRs and changes to main
|
||||
on:
|
||||
push:
|
||||
branches:
|
||||
- main
|
||||
pull_request:
|
||||
|
||||
jobs:
|
||||
|
||||
rat:
|
||||
name: Release Audit Tool (RAT)
|
||||
runs-on: ubuntu-latest
|
||||
steps:
|
||||
- uses: actions/checkout@v4
|
||||
- name: Setup Python
|
||||
uses: actions/setup-python@v5
|
||||
with:
|
||||
python-version: 3.8
|
||||
- name: Audit licenses
|
||||
run: ./dev/release/run-rat.sh .
|
10
.github/workflows/rust.yml
vendored
10
.github/workflows/rust.yml
vendored
|
@ -19,6 +19,9 @@ name: Rust
|
|||
|
||||
on: [push, pull_request]
|
||||
|
||||
permissions:
|
||||
contents: read
|
||||
|
||||
jobs:
|
||||
|
||||
codestyle:
|
||||
|
@ -85,11 +88,8 @@ jobs:
|
|||
uses: ./.github/actions/setup-builder
|
||||
with:
|
||||
rust-version: ${{ matrix.rust }}
|
||||
- uses: Swatinem/rust-cache@9d47c6ad4b02e050fd481d890b2ea34778fd09d6 # v2.7.8
|
||||
- name: Install Tarpaulin
|
||||
uses: actions-rs/install@v0.1
|
||||
with:
|
||||
crate: cargo-tarpaulin
|
||||
version: 0.14.2
|
||||
use-tool-cache: true
|
||||
run: cargo install cargo-tarpaulin
|
||||
- name: Test
|
||||
run: cargo test --all-features
|
||||
|
|
|
@ -28,6 +28,7 @@ technically be breaking and thus will result in a `0.(N+1)` version.
|
|||
|
||||
|
||||
- Unreleased: Check https://github.com/sqlparser-rs/sqlparser-rs/commits/main for undocumented changes.
|
||||
- `0.56.0`: [changelog/0.56.0.md](changelog/0.56.0.md)
|
||||
- `0.55.0`: [changelog/0.55.0.md](changelog/0.55.0.md)
|
||||
- `0.54.0`: [changelog/0.54.0.md](changelog/0.54.0.md)
|
||||
- `0.53.0`: [changelog/0.53.0.md](changelog/0.53.0.md)
|
||||
|
|
|
@ -18,7 +18,7 @@
|
|||
[package]
|
||||
name = "sqlparser"
|
||||
description = "Extensible SQL Lexer and Parser with support for ANSI SQL:2011"
|
||||
version = "0.55.0"
|
||||
version = "0.57.0"
|
||||
authors = ["Apache DataFusion <dev@datafusion.apache.org>"]
|
||||
homepage = "https://github.com/apache/datafusion-sqlparser-rs"
|
||||
documentation = "https://docs.rs/sqlparser/"
|
||||
|
|
|
@ -89,10 +89,14 @@ keywords, the following should hold true for all SQL:
|
|||
|
||||
```rust
|
||||
// Parse SQL
|
||||
let sql = "SELECT 'hello'";
|
||||
let ast = Parser::parse_sql(&GenericDialect, sql).unwrap();
|
||||
|
||||
// The original SQL text can be generated from the AST
|
||||
assert_eq!(ast[0].to_string(), sql);
|
||||
|
||||
// The SQL can also be pretty-printed with newlines and indentation
|
||||
assert_eq!(format!("{:#}", ast[0]), "SELECT\n 'hello'");
|
||||
```
|
||||
|
||||
There are still some cases in this crate where different SQL with seemingly
|
||||
|
@ -156,7 +160,8 @@ $ cargo run --features json_example --example cli FILENAME.sql [--dialectname]
|
|||
## Users
|
||||
|
||||
This parser is currently being used by the [DataFusion] query engine, [LocustDB],
|
||||
[Ballista], [GlueSQL], [Opteryx], [Polars], [PRQL], [Qrlew], [JumpWire], and [ParadeDB].
|
||||
[Ballista], [GlueSQL], [Opteryx], [Polars], [PRQL], [Qrlew], [JumpWire], [ParadeDB], [CipherStash Proxy],
|
||||
and [GreptimeDB].
|
||||
|
||||
If your project is using sqlparser-rs feel free to make a PR to add it
|
||||
to this list.
|
||||
|
@ -275,3 +280,5 @@ licensed as above, without any additional terms or conditions.
|
|||
[sql-standard]: https://en.wikipedia.org/wiki/ISO/IEC_9075
|
||||
[`Dialect`]: https://docs.rs/sqlparser/latest/sqlparser/dialect/trait.Dialect.html
|
||||
[`GenericDialect`]: https://docs.rs/sqlparser/latest/sqlparser/dialect/struct.GenericDialect.html
|
||||
[CipherStash Proxy]: https://github.com/cipherstash/proxy
|
||||
[GreptimeDB]: https://github.com/GreptimeTeam/greptimedb
|
||||
|
|
102
changelog/0.56.0.md
Normal file
102
changelog/0.56.0.md
Normal file
|
@ -0,0 +1,102 @@
|
|||
<!--
|
||||
Licensed to the Apache Software Foundation (ASF) under one
|
||||
or more contributor license agreements. See the NOTICE file
|
||||
distributed with this work for additional information
|
||||
regarding copyright ownership. The ASF licenses this file
|
||||
to you under the Apache License, Version 2.0 (the
|
||||
"License"); you may not use this file except in compliance
|
||||
with the License. You may obtain a copy of the License at
|
||||
|
||||
http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
||||
Unless required by applicable law or agreed to in writing,
|
||||
software distributed under the License is distributed on an
|
||||
"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
||||
KIND, either express or implied. See the License for the
|
||||
specific language governing permissions and limitations
|
||||
under the License.
|
||||
-->
|
||||
|
||||
# sqlparser-rs 0.56.0 Changelog
|
||||
|
||||
This release consists of 48 commits from 19 contributors. See credits at the end of this changelog for more information.
|
||||
|
||||
**Other:**
|
||||
|
||||
- Ignore escaped LIKE wildcards in MySQL [#1735](https://github.com/apache/datafusion-sqlparser-rs/pull/1735) (mvzink)
|
||||
- Parse SET NAMES syntax in Postgres [#1752](https://github.com/apache/datafusion-sqlparser-rs/pull/1752) (mvzink)
|
||||
- re-add support for nested comments in mssql [#1754](https://github.com/apache/datafusion-sqlparser-rs/pull/1754) (lovasoa)
|
||||
- Extend support for INDEX parsing [#1707](https://github.com/apache/datafusion-sqlparser-rs/pull/1707) (LucaCappelletti94)
|
||||
- Parse MySQL `ALTER TABLE DROP FOREIGN KEY` syntax [#1762](https://github.com/apache/datafusion-sqlparser-rs/pull/1762) (mvzink)
|
||||
- add support for `with` clauses (CTEs) in `delete` statements [#1764](https://github.com/apache/datafusion-sqlparser-rs/pull/1764) (lovasoa)
|
||||
- SET with a list of comma separated assignments [#1757](https://github.com/apache/datafusion-sqlparser-rs/pull/1757) (MohamedAbdeen21)
|
||||
- Preserve MySQL-style `LIMIT <offset>, <limit>` syntax [#1765](https://github.com/apache/datafusion-sqlparser-rs/pull/1765) (mvzink)
|
||||
- Add support for `DROP MATERIALIZED VIEW` [#1743](https://github.com/apache/datafusion-sqlparser-rs/pull/1743) (iffyio)
|
||||
- Add `CASE` and `IF` statement support [#1741](https://github.com/apache/datafusion-sqlparser-rs/pull/1741) (iffyio)
|
||||
- BigQuery: Add support for `CREATE SCHEMA` options [#1742](https://github.com/apache/datafusion-sqlparser-rs/pull/1742) (iffyio)
|
||||
- Snowflake: Support dollar quoted comments [#1755](https://github.com/apache/datafusion-sqlparser-rs/pull/1755)
|
||||
- Add LOCK operation for ALTER TABLE [#1768](https://github.com/apache/datafusion-sqlparser-rs/pull/1768) (MohamedAbdeen21)
|
||||
- Add support for `RAISE` statement [#1766](https://github.com/apache/datafusion-sqlparser-rs/pull/1766) (iffyio)
|
||||
- Add GLOBAL context/modifier to SET statements [#1767](https://github.com/apache/datafusion-sqlparser-rs/pull/1767) (MohamedAbdeen21)
|
||||
- Parse `SUBSTR` as alias for `SUBSTRING` [#1769](https://github.com/apache/datafusion-sqlparser-rs/pull/1769) (mvzink)
|
||||
- SET statements: scope modifier for multiple assignments [#1772](https://github.com/apache/datafusion-sqlparser-rs/pull/1772) (MohamedAbdeen21)
|
||||
- Support qualified column names in `MATCH AGAINST` clause [#1774](https://github.com/apache/datafusion-sqlparser-rs/pull/1774) (tomershaniii)
|
||||
- Mysql: Add support for := operator [#1779](https://github.com/apache/datafusion-sqlparser-rs/pull/1779) (barsela1)
|
||||
- Add cipherstash-proxy to list of users in README.md [#1782](https://github.com/apache/datafusion-sqlparser-rs/pull/1782) (coderdan)
|
||||
- Fix typos [#1785](https://github.com/apache/datafusion-sqlparser-rs/pull/1785) (jayvdb)
|
||||
- Add support for Databricks TIMESTAMP_NTZ. [#1781](https://github.com/apache/datafusion-sqlparser-rs/pull/1781) (romanb)
|
||||
- Enable double-dot-notation for mssql. [#1787](https://github.com/apache/datafusion-sqlparser-rs/pull/1787) (romanb)
|
||||
- Fix: Snowflake ALTER SESSION cannot be followed by other statements. [#1786](https://github.com/apache/datafusion-sqlparser-rs/pull/1786) (romanb)
|
||||
- Add GreptimeDB to the "Users" in README [#1788](https://github.com/apache/datafusion-sqlparser-rs/pull/1788) (MichaelScofield)
|
||||
- Extend snowflake grant options support [#1794](https://github.com/apache/datafusion-sqlparser-rs/pull/1794) (yoavcloud)
|
||||
- Fix clippy lint on rust 1.86 [#1796](https://github.com/apache/datafusion-sqlparser-rs/pull/1796) (iffyio)
|
||||
- Allow single quotes in EXTRACT() for Redshift. [#1795](https://github.com/apache/datafusion-sqlparser-rs/pull/1795) (romanb)
|
||||
- MSSQL: Add support for functionality `MERGE` output clause [#1790](https://github.com/apache/datafusion-sqlparser-rs/pull/1790) (dilovancelik)
|
||||
- Support additional DuckDB integer types such as HUGEINT, UHUGEINT, etc [#1797](https://github.com/apache/datafusion-sqlparser-rs/pull/1797) (alexander-beedie)
|
||||
- Add support for MSSQL IF/ELSE statements. [#1791](https://github.com/apache/datafusion-sqlparser-rs/pull/1791) (romanb)
|
||||
- Allow literal backslash escapes for string literals in Redshift dialect. [#1801](https://github.com/apache/datafusion-sqlparser-rs/pull/1801) (romanb)
|
||||
- Add support for MySQL's STRAIGHT_JOIN join operator. [#1802](https://github.com/apache/datafusion-sqlparser-rs/pull/1802) (romanb)
|
||||
- Snowflake COPY INTO target columns, select items and optional alias [#1805](https://github.com/apache/datafusion-sqlparser-rs/pull/1805) (yoavcloud)
|
||||
- Fix tokenization of qualified identifiers with numeric prefix. [#1803](https://github.com/apache/datafusion-sqlparser-rs/pull/1803) (romanb)
|
||||
- Add support for `INHERITS` option in `CREATE TABLE` statement [#1806](https://github.com/apache/datafusion-sqlparser-rs/pull/1806) (LucaCappelletti94)
|
||||
- Add `DROP TRIGGER` support for SQL Server [#1813](https://github.com/apache/datafusion-sqlparser-rs/pull/1813) (aharpervc)
|
||||
- Snowflake: support nested join without parentheses [#1799](https://github.com/apache/datafusion-sqlparser-rs/pull/1799) (barsela1)
|
||||
- Add support for parenthesized subquery as `IN` predicate [#1793](https://github.com/apache/datafusion-sqlparser-rs/pull/1793) (adamchainz)
|
||||
- Fix `STRAIGHT_JOIN` constraint when table alias is absent [#1812](https://github.com/apache/datafusion-sqlparser-rs/pull/1812) (killertux)
|
||||
- Add support for `PRINT` statement for SQL Server [#1811](https://github.com/apache/datafusion-sqlparser-rs/pull/1811) (aharpervc)
|
||||
- enable `supports_filter_during_aggregation` for Generic dialect [#1815](https://github.com/apache/datafusion-sqlparser-rs/pull/1815) (goldmedal)
|
||||
- Add support for `XMLTABLE` [#1817](https://github.com/apache/datafusion-sqlparser-rs/pull/1817) (lovasoa)
|
||||
- Add `CREATE FUNCTION` support for SQL Server [#1808](https://github.com/apache/datafusion-sqlparser-rs/pull/1808) (aharpervc)
|
||||
- Add `OR ALTER` support for `CREATE VIEW` [#1818](https://github.com/apache/datafusion-sqlparser-rs/pull/1818) (aharpervc)
|
||||
- Add `DECLARE ... CURSOR FOR` support for SQL Server [#1821](https://github.com/apache/datafusion-sqlparser-rs/pull/1821) (aharpervc)
|
||||
- Handle missing login in changelog generate script [#1823](https://github.com/apache/datafusion-sqlparser-rs/pull/1823) (iffyio)
|
||||
- Snowflake: Add support for `CONNECT_BY_ROOT` [#1780](https://github.com/apache/datafusion-sqlparser-rs/pull/1780) (tomershaniii)
|
||||
|
||||
## Credits
|
||||
|
||||
Thank you to everyone who contributed to this release. Here is a breakdown of commits (PRs merged) per contributor.
|
||||
|
||||
```
|
||||
8 Roman Borschel
|
||||
6 Ifeanyi Ubah
|
||||
5 Andrew Harper
|
||||
5 Michael Victor Zink
|
||||
4 Mohamed Abdeen
|
||||
3 Ophir LOJKINE
|
||||
2 Luca Cappelletti
|
||||
2 Yoav Cohen
|
||||
2 bar sela
|
||||
2 tomershaniii
|
||||
1 Adam Johnson
|
||||
1 Aleksei Piianin
|
||||
1 Alexander Beedie
|
||||
1 Bruno Clemente
|
||||
1 Dan Draper
|
||||
1 DilovanCelik
|
||||
1 Jax Liu
|
||||
1 John Vandenberg
|
||||
1 LFC
|
||||
```
|
||||
|
||||
Thank you also to everyone who contributed in other ways such as filing issues, reviewing PRs, and providing feedback on this release.
|
||||
|
95
changelog/0.57.0.md
Normal file
95
changelog/0.57.0.md
Normal file
|
@ -0,0 +1,95 @@
|
|||
<!--
|
||||
Licensed to the Apache Software Foundation (ASF) under one
|
||||
or more contributor license agreements. See the NOTICE file
|
||||
distributed with this work for additional information
|
||||
regarding copyright ownership. The ASF licenses this file
|
||||
to you under the Apache License, Version 2.0 (the
|
||||
"License"); you may not use this file except in compliance
|
||||
with the License. You may obtain a copy of the License at
|
||||
|
||||
http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
||||
Unless required by applicable law or agreed to in writing,
|
||||
software distributed under the License is distributed on an
|
||||
"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
||||
KIND, either express or implied. See the License for the
|
||||
specific language governing permissions and limitations
|
||||
under the License.
|
||||
-->
|
||||
|
||||
# sqlparser-rs 0.57.0 Changelog
|
||||
|
||||
This release consists of 39 commits from 19 contributors. See credits at the end of this changelog for more information.
|
||||
|
||||
**Implemented enhancements:**
|
||||
|
||||
- feat: Hive: support `SORT BY` direction [#1873](https://github.com/apache/datafusion-sqlparser-rs/pull/1873) (chenkovsky)
|
||||
|
||||
**Other:**
|
||||
|
||||
- Support some of pipe operators [#1759](https://github.com/apache/datafusion-sqlparser-rs/pull/1759) (simonvandel)
|
||||
- Added support for `DROP DOMAIN` [#1828](https://github.com/apache/datafusion-sqlparser-rs/pull/1828) (LucaCappelletti94)
|
||||
- Improve support for cursors for SQL Server [#1831](https://github.com/apache/datafusion-sqlparser-rs/pull/1831) (aharpervc)
|
||||
- Add all missing table options to be handled in any order [#1747](https://github.com/apache/datafusion-sqlparser-rs/pull/1747) (benrsatori)
|
||||
- Add `CREATE TRIGGER` support for SQL Server [#1810](https://github.com/apache/datafusion-sqlparser-rs/pull/1810) (aharpervc)
|
||||
- Added support for `CREATE DOMAIN` [#1830](https://github.com/apache/datafusion-sqlparser-rs/pull/1830) (LucaCappelletti94)
|
||||
- Allow stored procedures to be defined without `BEGIN`/`END` [#1834](https://github.com/apache/datafusion-sqlparser-rs/pull/1834) (aharpervc)
|
||||
- Add support for the MATCH and REGEXP binary operators [#1840](https://github.com/apache/datafusion-sqlparser-rs/pull/1840) (lovasoa)
|
||||
- Fix: parsing ident starting with underscore in certain dialects [#1835](https://github.com/apache/datafusion-sqlparser-rs/pull/1835) (MohamedAbdeen21)
|
||||
- implement pretty-printing with `{:#}` [#1847](https://github.com/apache/datafusion-sqlparser-rs/pull/1847) (lovasoa)
|
||||
- Fix big performance issue in string serialization [#1848](https://github.com/apache/datafusion-sqlparser-rs/pull/1848) (lovasoa)
|
||||
- Add support for `DENY` statements [#1836](https://github.com/apache/datafusion-sqlparser-rs/pull/1836) (aharpervc)
|
||||
- Postgresql: Add `REPLICA IDENTITY` operation for `ALTER TABLE` [#1844](https://github.com/apache/datafusion-sqlparser-rs/pull/1844) (MohamedAbdeen21)
|
||||
- Add support for INCLUDE/EXCLUDE NULLS for UNPIVOT [#1849](https://github.com/apache/datafusion-sqlparser-rs/pull/1849) (Vedin)
|
||||
- pretty print improvements [#1851](https://github.com/apache/datafusion-sqlparser-rs/pull/1851) (lovasoa)
|
||||
- fix new rust 1.87 cargo clippy warnings [#1856](https://github.com/apache/datafusion-sqlparser-rs/pull/1856) (lovasoa)
|
||||
- Update criterion requirement from 0.5 to 0.6 in /sqlparser_bench [#1857](https://github.com/apache/datafusion-sqlparser-rs/pull/1857) (dependabot[bot])
|
||||
- pretty-print CREATE TABLE statements [#1854](https://github.com/apache/datafusion-sqlparser-rs/pull/1854) (lovasoa)
|
||||
- pretty-print CREATE VIEW statements [#1855](https://github.com/apache/datafusion-sqlparser-rs/pull/1855) (lovasoa)
|
||||
- Handle optional datatypes properly in `CREATE FUNCTION` statements [#1826](https://github.com/apache/datafusion-sqlparser-rs/pull/1826) (LucaCappelletti94)
|
||||
- Mysql: Add `SRID` column option [#1852](https://github.com/apache/datafusion-sqlparser-rs/pull/1852) (MohamedAbdeen21)
|
||||
- Add support for table valued functions for SQL Server [#1839](https://github.com/apache/datafusion-sqlparser-rs/pull/1839) (aharpervc)
|
||||
- Keep the COLUMN keyword only if it exists when dropping the column [#1862](https://github.com/apache/datafusion-sqlparser-rs/pull/1862) (git-hulk)
|
||||
- Add support for parameter default values in SQL Server [#1866](https://github.com/apache/datafusion-sqlparser-rs/pull/1866) (aharpervc)
|
||||
- Add support for `TABLESAMPLE` pipe operator [#1860](https://github.com/apache/datafusion-sqlparser-rs/pull/1860) (hendrikmakait)
|
||||
- Adds support for mysql's drop index [#1864](https://github.com/apache/datafusion-sqlparser-rs/pull/1864) (dmzmk)
|
||||
- Fix: GROUPING SETS accept values without parenthesis [#1867](https://github.com/apache/datafusion-sqlparser-rs/pull/1867) (Vedin)
|
||||
- Add ICEBERG keyword support to ALTER TABLE statement [#1869](https://github.com/apache/datafusion-sqlparser-rs/pull/1869) (osipovartem)
|
||||
- MySQL: Support `index_name` in FK constraints [#1871](https://github.com/apache/datafusion-sqlparser-rs/pull/1871) (MohamedAbdeen21)
|
||||
- Postgres: Apply `ONLY` keyword per table in TRUNCATE stmt [#1872](https://github.com/apache/datafusion-sqlparser-rs/pull/1872) (MohamedAbdeen21)
|
||||
- Fix `CASE` expression spans [#1874](https://github.com/apache/datafusion-sqlparser-rs/pull/1874) (eliaperantoni)
|
||||
- MySQL: `[[NOT] ENFORCED]` in CHECK constraint [#1870](https://github.com/apache/datafusion-sqlparser-rs/pull/1870) (MohamedAbdeen21)
|
||||
- Add support for `CREATE SCHEMA WITH ( <properties> )` [#1877](https://github.com/apache/datafusion-sqlparser-rs/pull/1877) (utay)
|
||||
- Add support for `ALTER TABLE DROP INDEX` [#1865](https://github.com/apache/datafusion-sqlparser-rs/pull/1865) (vimko)
|
||||
- chore: Replace archived actions-rs/install action [#1876](https://github.com/apache/datafusion-sqlparser-rs/pull/1876) (assignUser)
|
||||
- Allow `IF NOT EXISTS` after table name for Snowflake [#1881](https://github.com/apache/datafusion-sqlparser-rs/pull/1881) (bombsimon)
|
||||
- Support `DISTINCT AS { STRUCT | VALUE }` for BigQuery [#1880](https://github.com/apache/datafusion-sqlparser-rs/pull/1880) (bombsimon)
|
||||
|
||||
## Credits
|
||||
|
||||
Thank you to everyone who contributed to this release. Here is a breakdown of commits (PRs merged) per contributor.
|
||||
|
||||
```
|
||||
7 Ophir LOJKINE
|
||||
6 Andrew Harper
|
||||
6 Mohamed Abdeen
|
||||
3 Luca Cappelletti
|
||||
2 Denys Tsomenko
|
||||
2 Simon Sawert
|
||||
1 Andrew Lamb
|
||||
1 Artem Osipov
|
||||
1 Chen Chongchen
|
||||
1 Dmitriy Mazurin
|
||||
1 Elia Perantoni
|
||||
1 Hendrik Makait
|
||||
1 Jacob Wujciak-Jens
|
||||
1 Simon Vandel Sillesen
|
||||
1 Yannick Utard
|
||||
1 benrsatori
|
||||
1 dependabot[bot]
|
||||
1 hulk
|
||||
1 vimko
|
||||
```
|
||||
|
||||
Thank you also to everyone who contributed in other ways such as filing issues, reviewing PRs, and providing feedback on this release.
|
||||
|
|
@ -28,7 +28,8 @@ def print_pulls(repo_name, title, pulls):
|
|||
print()
|
||||
for (pull, commit) in pulls:
|
||||
url = "https://github.com/{}/pull/{}".format(repo_name, pull.number)
|
||||
print("- {} [#{}]({}) ({})".format(pull.title, pull.number, url, commit.author.login))
|
||||
author = f"({commit.author.login})" if commit.author else ''
|
||||
print("- {} [#{}]({}) {}".format(pull.title, pull.number, url, author))
|
||||
print()
|
||||
|
||||
|
||||
|
|
|
@ -1,7 +1,8 @@
|
|||
# Files to exclude from the Apache Rat (license) check
|
||||
.gitignore
|
||||
.tool-versions
|
||||
target/*
|
||||
**.gitignore
|
||||
rat.txt
|
||||
dev/release/rat_exclude_files.txt
|
||||
fuzz/.gitignore
|
||||
sqlparser_bench/img/flamegraph.svg
|
||||
|
||||
**Cargo.lock
|
||||
filtered_rat.txt
|
||||
|
|
|
@ -63,7 +63,7 @@ $ cargo run --example cli - [--dialectname]
|
|||
};
|
||||
|
||||
let contents = if filename == "-" {
|
||||
println!("Parsing from stdin using {:?}", dialect);
|
||||
println!("Parsing from stdin using {dialect:?}");
|
||||
let mut buf = Vec::new();
|
||||
stdin()
|
||||
.read_to_end(&mut buf)
|
||||
|
|
|
@ -26,7 +26,7 @@ edition = "2018"
|
|||
sqlparser = { path = "../" }
|
||||
|
||||
[dev-dependencies]
|
||||
criterion = "0.5"
|
||||
criterion = "0.6"
|
||||
|
||||
[[bench]]
|
||||
name = "sqlparser_bench"
|
||||
|
|
|
@ -45,30 +45,29 @@ fn basic_queries(c: &mut Criterion) {
|
|||
|
||||
let large_statement = {
|
||||
let expressions = (0..1000)
|
||||
.map(|n| format!("FN_{}(COL_{})", n, n))
|
||||
.map(|n| format!("FN_{n}(COL_{n})"))
|
||||
.collect::<Vec<_>>()
|
||||
.join(", ");
|
||||
let tables = (0..1000)
|
||||
.map(|n| format!("TABLE_{}", n))
|
||||
.map(|n| format!("TABLE_{n}"))
|
||||
.collect::<Vec<_>>()
|
||||
.join(" JOIN ");
|
||||
let where_condition = (0..1000)
|
||||
.map(|n| format!("COL_{} = {}", n, n))
|
||||
.map(|n| format!("COL_{n} = {n}"))
|
||||
.collect::<Vec<_>>()
|
||||
.join(" OR ");
|
||||
let order_condition = (0..1000)
|
||||
.map(|n| format!("COL_{} DESC", n))
|
||||
.map(|n| format!("COL_{n} DESC"))
|
||||
.collect::<Vec<_>>()
|
||||
.join(", ");
|
||||
|
||||
format!(
|
||||
"SELECT {} FROM {} WHERE {} ORDER BY {}",
|
||||
expressions, tables, where_condition, order_condition
|
||||
"SELECT {expressions} FROM {tables} WHERE {where_condition} ORDER BY {order_condition}"
|
||||
)
|
||||
};
|
||||
|
||||
group.bench_function("parse_large_statement", |b| {
|
||||
b.iter(|| Parser::parse_sql(&dialect, criterion::black_box(large_statement.as_str())));
|
||||
b.iter(|| Parser::parse_sql(&dialect, std::hint::black_box(large_statement.as_str())));
|
||||
});
|
||||
|
||||
let large_statement = Parser::parse_sql(&dialect, large_statement.as_str())
|
||||
|
|
|
@ -36,7 +36,7 @@ pub enum EnumMember {
|
|||
Name(String),
|
||||
/// ClickHouse allows to specify an integer value for each enum value.
|
||||
///
|
||||
/// [clickhouse](https://clickhouse.com/docs/en/sql-reference/data-types/enum)
|
||||
/// [ClickHouse](https://clickhouse.com/docs/en/sql-reference/data-types/enum)
|
||||
NamedValue(String, Expr),
|
||||
}
|
||||
|
||||
|
@ -45,292 +45,327 @@ pub enum EnumMember {
|
|||
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
|
||||
#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
|
||||
pub enum DataType {
|
||||
/// Table type in [postgresql]. e.g. CREATE FUNCTION RETURNS TABLE(...)
|
||||
/// Table type in [PostgreSQL], e.g. CREATE FUNCTION RETURNS TABLE(...).
|
||||
///
|
||||
/// [postgresql]: https://www.postgresql.org/docs/15/sql-createfunction.html
|
||||
Table(Vec<ColumnDef>),
|
||||
/// Fixed-length character type e.g. CHARACTER(10)
|
||||
/// [PostgreSQL]: https://www.postgresql.org/docs/15/sql-createfunction.html
|
||||
/// [MsSQL]: https://learn.microsoft.com/en-us/sql/t-sql/statements/create-function-transact-sql?view=sql-server-ver16#c-create-a-multi-statement-table-valued-function
|
||||
Table(Option<Vec<ColumnDef>>),
|
||||
/// Table type with a name, e.g. CREATE FUNCTION RETURNS @result TABLE(...).
|
||||
///
|
||||
/// [MsSQl]: https://learn.microsoft.com/en-us/sql/t-sql/statements/create-function-transact-sql?view=sql-server-ver16#table
|
||||
NamedTable {
|
||||
/// Table name.
|
||||
name: ObjectName,
|
||||
/// Table columns.
|
||||
columns: Vec<ColumnDef>,
|
||||
},
|
||||
/// Fixed-length character type, e.g. CHARACTER(10).
|
||||
Character(Option<CharacterLength>),
|
||||
/// Fixed-length char type e.g. CHAR(10)
|
||||
/// Fixed-length char type, e.g. CHAR(10).
|
||||
Char(Option<CharacterLength>),
|
||||
/// Character varying type e.g. CHARACTER VARYING(10)
|
||||
/// Character varying type, e.g. CHARACTER VARYING(10).
|
||||
CharacterVarying(Option<CharacterLength>),
|
||||
/// Char varying type e.g. CHAR VARYING(10)
|
||||
/// Char varying type, e.g. CHAR VARYING(10).
|
||||
CharVarying(Option<CharacterLength>),
|
||||
/// Variable-length character type e.g. VARCHAR(10)
|
||||
/// Variable-length character type, e.g. VARCHAR(10).
|
||||
Varchar(Option<CharacterLength>),
|
||||
/// Variable-length character type e.g. NVARCHAR(10)
|
||||
/// Variable-length character type, e.g. NVARCHAR(10).
|
||||
Nvarchar(Option<CharacterLength>),
|
||||
/// Uuid type
|
||||
/// Uuid type.
|
||||
Uuid,
|
||||
/// Large character object with optional length e.g. CHARACTER LARGE OBJECT, CHARACTER LARGE OBJECT(1000), [standard]
|
||||
/// Large character object with optional length,
|
||||
/// e.g. CHARACTER LARGE OBJECT, CHARACTER LARGE OBJECT(1000), [SQL Standard].
|
||||
///
|
||||
/// [standard]: https://jakewheat.github.io/sql-overview/sql-2016-foundation-grammar.html#character-large-object-type
|
||||
/// [SQL Standard]: https://jakewheat.github.io/sql-overview/sql-2016-foundation-grammar.html#character-large-object-type
|
||||
CharacterLargeObject(Option<u64>),
|
||||
/// Large character object with optional length e.g. CHAR LARGE OBJECT, CHAR LARGE OBJECT(1000), [standard]
|
||||
/// Large character object with optional length,
|
||||
/// e.g. CHAR LARGE OBJECT, CHAR LARGE OBJECT(1000), [SQL Standard].
|
||||
///
|
||||
/// [standard]: https://jakewheat.github.io/sql-overview/sql-2016-foundation-grammar.html#character-large-object-type
|
||||
/// [SQL Standard]: https://jakewheat.github.io/sql-overview/sql-2016-foundation-grammar.html#character-large-object-type
|
||||
CharLargeObject(Option<u64>),
|
||||
/// Large character object with optional length e.g. CLOB, CLOB(1000), [standard]
|
||||
/// Large character object with optional length,
|
||||
/// e.g. CLOB, CLOB(1000), [SQL Standard].
|
||||
///
|
||||
/// [standard]: https://jakewheat.github.io/sql-overview/sql-2016-foundation-grammar.html#character-large-object-type
|
||||
/// [SQL Standard]: https://jakewheat.github.io/sql-overview/sql-2016-foundation-grammar.html#character-large-object-type
|
||||
/// [Oracle]: https://docs.oracle.com/javadb/10.10.1.2/ref/rrefclob.html
|
||||
Clob(Option<u64>),
|
||||
/// Fixed-length binary type with optional length e.g. [standard], [MS SQL Server]
|
||||
/// Fixed-length binary type with optional length,
|
||||
/// see [SQL Standard], [MS SQL Server].
|
||||
///
|
||||
/// [standard]: https://jakewheat.github.io/sql-overview/sql-2016-foundation-grammar.html#binary-string-type
|
||||
/// [SQL Standard]: https://jakewheat.github.io/sql-overview/sql-2016-foundation-grammar.html#binary-string-type
|
||||
/// [MS SQL Server]: https://learn.microsoft.com/pt-br/sql/t-sql/data-types/binary-and-varbinary-transact-sql?view=sql-server-ver16
|
||||
Binary(Option<u64>),
|
||||
/// Variable-length binary with optional length type e.g. [standard], [MS SQL Server]
|
||||
/// Variable-length binary with optional length type,
|
||||
/// see [SQL Standard], [MS SQL Server].
|
||||
///
|
||||
/// [standard]: https://jakewheat.github.io/sql-overview/sql-2016-foundation-grammar.html#binary-string-type
|
||||
/// [SQL Standard]: https://jakewheat.github.io/sql-overview/sql-2016-foundation-grammar.html#binary-string-type
|
||||
/// [MS SQL Server]: https://learn.microsoft.com/pt-br/sql/t-sql/data-types/binary-and-varbinary-transact-sql?view=sql-server-ver16
|
||||
Varbinary(Option<BinaryLength>),
|
||||
/// Large binary object with optional length e.g. BLOB, BLOB(1000), [standard], [Oracle]
|
||||
/// Large binary object with optional length,
|
||||
/// see [SQL Standard], [Oracle].
|
||||
///
|
||||
/// [standard]: https://jakewheat.github.io/sql-overview/sql-2016-foundation-grammar.html#binary-large-object-string-type
|
||||
/// [SQL Standard]: https://jakewheat.github.io/sql-overview/sql-2016-foundation-grammar.html#binary-large-object-string-type
|
||||
/// [Oracle]: https://docs.oracle.com/javadb/10.8.3.0/ref/rrefblob.html
|
||||
Blob(Option<u64>),
|
||||
/// [MySQL] blob with up to 2**8 bytes
|
||||
/// [MySQL] blob with up to 2**8 bytes.
|
||||
///
|
||||
/// [MySQL]: https://dev.mysql.com/doc/refman/9.1/en/blob.html
|
||||
TinyBlob,
|
||||
/// [MySQL] blob with up to 2**24 bytes
|
||||
/// [MySQL] blob with up to 2**24 bytes.
|
||||
///
|
||||
/// [MySQL]: https://dev.mysql.com/doc/refman/9.1/en/blob.html
|
||||
MediumBlob,
|
||||
/// [MySQL] blob with up to 2**32 bytes
|
||||
/// [MySQL] blob with up to 2**32 bytes.
|
||||
///
|
||||
/// [MySQL]: https://dev.mysql.com/doc/refman/9.1/en/blob.html
|
||||
LongBlob,
|
||||
/// Variable-length binary data with optional length.
|
||||
///
|
||||
/// [bigquery]: https://cloud.google.com/bigquery/docs/reference/standard-sql/data-types#bytes_type
|
||||
/// [BigQuery]: https://cloud.google.com/bigquery/docs/reference/standard-sql/data-types#bytes_type
|
||||
Bytes(Option<u64>),
|
||||
/// Numeric type with optional precision and scale e.g. NUMERIC(10,2), [standard][1]
|
||||
/// Numeric type with optional precision and scale, e.g. NUMERIC(10,2), [SQL Standard][1].
|
||||
///
|
||||
/// [1]: https://jakewheat.github.io/sql-overview/sql-2016-foundation-grammar.html#exact-numeric-type
|
||||
Numeric(ExactNumberInfo),
|
||||
/// Decimal type with optional precision and scale e.g. DECIMAL(10,2), [standard][1]
|
||||
/// Decimal type with optional precision and scale, e.g. DECIMAL(10,2), [SQL Standard][1].
|
||||
///
|
||||
/// [1]: https://jakewheat.github.io/sql-overview/sql-2016-foundation-grammar.html#exact-numeric-type
|
||||
Decimal(ExactNumberInfo),
|
||||
/// [BigNumeric] type used in BigQuery
|
||||
/// [BigNumeric] type used in BigQuery.
|
||||
///
|
||||
/// [BigNumeric]: https://cloud.google.com/bigquery/docs/reference/standard-sql/lexical#bignumeric_literals
|
||||
BigNumeric(ExactNumberInfo),
|
||||
/// This is alias for `BigNumeric` type used in BigQuery
|
||||
/// This is alias for `BigNumeric` type used in BigQuery.
|
||||
///
|
||||
/// [BigDecimal]: https://cloud.google.com/bigquery/docs/reference/standard-sql/data-types#decimal_types
|
||||
BigDecimal(ExactNumberInfo),
|
||||
/// Dec type with optional precision and scale e.g. DEC(10,2), [standard][1]
|
||||
/// Dec type with optional precision and scale, e.g. DEC(10,2), [SQL Standard][1].
|
||||
///
|
||||
/// [1]: https://jakewheat.github.io/sql-overview/sql-2016-foundation-grammar.html#exact-numeric-type
|
||||
Dec(ExactNumberInfo),
|
||||
/// Floating point with optional precision e.g. FLOAT(8)
|
||||
/// Floating point with optional precision, e.g. FLOAT(8).
|
||||
Float(Option<u64>),
|
||||
/// Tiny integer with optional display width e.g. TINYINT or TINYINT(3)
|
||||
/// Tiny integer with optional display width, e.g. TINYINT or TINYINT(3).
|
||||
TinyInt(Option<u64>),
|
||||
/// Unsigned tiny integer with optional display width e.g. TINYINT UNSIGNED or TINYINT(3) UNSIGNED
|
||||
/// Unsigned tiny integer with optional display width,
|
||||
/// e.g. TINYINT UNSIGNED or TINYINT(3) UNSIGNED.
|
||||
TinyIntUnsigned(Option<u64>),
|
||||
/// Int2 as alias for SmallInt in [postgresql]
|
||||
/// Note: Int2 mean 2 bytes in postgres (not 2 bits)
|
||||
/// Int2 with optional display width e.g. INT2 or INT2(5)
|
||||
/// Unsigned tiny integer, e.g. UTINYINT
|
||||
UTinyInt,
|
||||
/// Int2 is an alias for SmallInt in [PostgreSQL].
|
||||
/// Note: Int2 means 2 bytes in PostgreSQL (not 2 bits).
|
||||
/// Int2 with optional display width, e.g. INT2 or INT2(5).
|
||||
///
|
||||
/// [postgresql]: https://www.postgresql.org/docs/15/datatype.html
|
||||
/// [PostgreSQL]: https://www.postgresql.org/docs/current/datatype.html
|
||||
Int2(Option<u64>),
|
||||
/// Unsigned Int2 with optional display width e.g. INT2 UNSIGNED or INT2(5) UNSIGNED
|
||||
/// Unsigned Int2 with optional display width, e.g. INT2 UNSIGNED or INT2(5) UNSIGNED.
|
||||
Int2Unsigned(Option<u64>),
|
||||
/// Small integer with optional display width e.g. SMALLINT or SMALLINT(5)
|
||||
/// Small integer with optional display width, e.g. SMALLINT or SMALLINT(5).
|
||||
SmallInt(Option<u64>),
|
||||
/// Unsigned small integer with optional display width e.g. SMALLINT UNSIGNED or SMALLINT(5) UNSIGNED
|
||||
/// Unsigned small integer with optional display width,
|
||||
/// e.g. SMALLINT UNSIGNED or SMALLINT(5) UNSIGNED.
|
||||
SmallIntUnsigned(Option<u64>),
|
||||
/// MySQL medium integer ([1]) with optional display width e.g. MEDIUMINT or MEDIUMINT(5)
|
||||
/// Unsigned small integer, e.g. USMALLINT.
|
||||
USmallInt,
|
||||
/// MySQL medium integer ([1]) with optional display width,
|
||||
/// e.g. MEDIUMINT or MEDIUMINT(5).
|
||||
///
|
||||
/// [1]: https://dev.mysql.com/doc/refman/8.0/en/integer-types.html
|
||||
MediumInt(Option<u64>),
|
||||
/// Unsigned medium integer ([1]) with optional display width e.g. MEDIUMINT UNSIGNED or MEDIUMINT(5) UNSIGNED
|
||||
/// Unsigned medium integer ([1]) with optional display width,
|
||||
/// e.g. MEDIUMINT UNSIGNED or MEDIUMINT(5) UNSIGNED.
|
||||
///
|
||||
/// [1]: https://dev.mysql.com/doc/refman/8.0/en/integer-types.html
|
||||
MediumIntUnsigned(Option<u64>),
|
||||
/// Int with optional display width e.g. INT or INT(11)
|
||||
/// Int with optional display width, e.g. INT or INT(11).
|
||||
Int(Option<u64>),
|
||||
/// Int4 as alias for Integer in [postgresql]
|
||||
/// Note: Int4 mean 4 bytes in postgres (not 4 bits)
|
||||
/// Int4 with optional display width e.g. Int4 or Int4(11)
|
||||
/// Int4 is an alias for Integer in [PostgreSQL].
|
||||
/// Note: Int4 means 4 bytes in PostgreSQL (not 4 bits).
|
||||
/// Int4 with optional display width, e.g. Int4 or Int4(11).
|
||||
///
|
||||
/// [postgresql]: https://www.postgresql.org/docs/15/datatype.html
|
||||
/// [PostgreSQL]: https://www.postgresql.org/docs/current/datatype.html
|
||||
Int4(Option<u64>),
|
||||
/// Int8 as alias for Bigint in [postgresql] and integer type in [clickhouse]
|
||||
/// Note: Int8 mean 8 bytes in [postgresql] (not 8 bits)
|
||||
/// Int8 with optional display width e.g. INT8 or INT8(11)
|
||||
/// Note: Int8 mean 8 bits in [clickhouse]
|
||||
/// Int8 is an alias for BigInt in [PostgreSQL] and Integer type in [ClickHouse].
|
||||
/// Int8 with optional display width, e.g. INT8 or INT8(11).
|
||||
/// Note: Int8 means 8 bytes in [PostgreSQL], but 8 bits in [ClickHouse].
|
||||
///
|
||||
/// [postgresql]: https://www.postgresql.org/docs/15/datatype.html
|
||||
/// [clickhouse]: https://clickhouse.com/docs/en/sql-reference/data-types/int-uint
|
||||
/// [PostgreSQL]: https://www.postgresql.org/docs/current/datatype.html
|
||||
/// [ClickHouse]: https://clickhouse.com/docs/en/sql-reference/data-types/int-uint
|
||||
Int8(Option<u64>),
|
||||
/// Integer type in [clickhouse]
|
||||
/// Note: Int16 mean 16 bits in [clickhouse]
|
||||
/// Integer type in [ClickHouse].
|
||||
/// Note: Int16 means 16 bits in [ClickHouse].
|
||||
///
|
||||
/// [clickhouse]: https://clickhouse.com/docs/en/sql-reference/data-types/int-uint
|
||||
/// [ClickHouse]: https://clickhouse.com/docs/en/sql-reference/data-types/int-uint
|
||||
Int16,
|
||||
/// Integer type in [clickhouse]
|
||||
/// Note: Int16 mean 32 bits in [clickhouse]
|
||||
/// Integer type in [ClickHouse].
|
||||
/// Note: Int32 means 32 bits in [ClickHouse].
|
||||
///
|
||||
/// [clickhouse]: https://clickhouse.com/docs/en/sql-reference/data-types/int-uint
|
||||
/// [ClickHouse]: https://clickhouse.com/docs/en/sql-reference/data-types/int-uint
|
||||
Int32,
|
||||
/// Integer type in [bigquery], [clickhouse]
|
||||
/// Integer type in [BigQuery], [ClickHouse].
|
||||
///
|
||||
/// [bigquery]: https://cloud.google.com/bigquery/docs/reference/standard-sql/data-types#integer_types
|
||||
/// [clickhouse]: https://clickhouse.com/docs/en/sql-reference/data-types/int-uint
|
||||
/// [BigQuery]: https://cloud.google.com/bigquery/docs/reference/standard-sql/data-types#integer_types
|
||||
/// [ClickHouse]: https://clickhouse.com/docs/en/sql-reference/data-types/int-uint
|
||||
Int64,
|
||||
/// Integer type in [clickhouse]
|
||||
/// Note: Int128 mean 128 bits in [clickhouse]
|
||||
/// Integer type in [ClickHouse].
|
||||
/// Note: Int128 means 128 bits in [ClickHouse].
|
||||
///
|
||||
/// [clickhouse]: https://clickhouse.com/docs/en/sql-reference/data-types/int-uint
|
||||
/// [ClickHouse]: https://clickhouse.com/docs/en/sql-reference/data-types/int-uint
|
||||
Int128,
|
||||
/// Integer type in [clickhouse]
|
||||
/// Note: Int256 mean 256 bits in [clickhouse]
|
||||
/// Integer type in [ClickHouse].
|
||||
/// Note: Int256 means 256 bits in [ClickHouse].
|
||||
///
|
||||
/// [clickhouse]: https://clickhouse.com/docs/en/sql-reference/data-types/int-uint
|
||||
/// [ClickHouse]: https://clickhouse.com/docs/en/sql-reference/data-types/int-uint
|
||||
Int256,
|
||||
/// Integer with optional display width e.g. INTEGER or INTEGER(11)
|
||||
/// Integer with optional display width, e.g. INTEGER or INTEGER(11).
|
||||
Integer(Option<u64>),
|
||||
/// Unsigned int with optional display width e.g. INT UNSIGNED or INT(11) UNSIGNED
|
||||
/// Unsigned int with optional display width, e.g. INT UNSIGNED or INT(11) UNSIGNED.
|
||||
IntUnsigned(Option<u64>),
|
||||
/// Unsigned int4 with optional display width e.g. INT4 UNSIGNED or INT4(11) UNSIGNED
|
||||
/// Unsigned int4 with optional display width, e.g. INT4 UNSIGNED or INT4(11) UNSIGNED.
|
||||
Int4Unsigned(Option<u64>),
|
||||
/// Unsigned integer with optional display width e.g. INTEGER UNSIGNED or INTEGER(11) UNSIGNED
|
||||
/// Unsigned integer with optional display width, e.g. INTEGER UNSIGNED or INTEGER(11) UNSIGNED.
|
||||
IntegerUnsigned(Option<u64>),
|
||||
/// Unsigned integer type in [clickhouse]
|
||||
/// Note: UInt8 mean 8 bits in [clickhouse]
|
||||
/// 128-bit integer type, e.g. HUGEINT.
|
||||
HugeInt,
|
||||
/// Unsigned 128-bit integer type, e.g. UHUGEINT.
|
||||
UHugeInt,
|
||||
/// Unsigned integer type in [ClickHouse].
|
||||
/// Note: UInt8 means 8 bits in [ClickHouse].
|
||||
///
|
||||
/// [clickhouse]: https://clickhouse.com/docs/en/sql-reference/data-types/int-uint
|
||||
/// [ClickHouse]: https://clickhouse.com/docs/en/sql-reference/data-types/int-uint
|
||||
UInt8,
|
||||
/// Unsigned integer type in [clickhouse]
|
||||
/// Note: UInt16 mean 16 bits in [clickhouse]
|
||||
/// Unsigned integer type in [ClickHouse].
|
||||
/// Note: UInt16 means 16 bits in [ClickHouse].
|
||||
///
|
||||
/// [clickhouse]: https://clickhouse.com/docs/en/sql-reference/data-types/int-uint
|
||||
/// [ClickHouse]: https://clickhouse.com/docs/en/sql-reference/data-types/int-uint
|
||||
UInt16,
|
||||
/// Unsigned integer type in [clickhouse]
|
||||
/// Note: UInt32 mean 32 bits in [clickhouse]
|
||||
/// Unsigned integer type in [ClickHouse].
|
||||
/// Note: UInt32 means 32 bits in [ClickHouse].
|
||||
///
|
||||
/// [clickhouse]: https://clickhouse.com/docs/en/sql-reference/data-types/int-uint
|
||||
/// [ClickHouse]: https://clickhouse.com/docs/en/sql-reference/data-types/int-uint
|
||||
UInt32,
|
||||
/// Unsigned integer type in [clickhouse]
|
||||
/// Note: UInt64 mean 64 bits in [clickhouse]
|
||||
/// Unsigned integer type in [ClickHouse].
|
||||
/// Note: UInt64 means 64 bits in [ClickHouse].
|
||||
///
|
||||
/// [clickhouse]: https://clickhouse.com/docs/en/sql-reference/data-types/int-uint
|
||||
/// [ClickHouse]: https://clickhouse.com/docs/en/sql-reference/data-types/int-uint
|
||||
UInt64,
|
||||
/// Unsigned integer type in [clickhouse]
|
||||
/// Note: UInt128 mean 128 bits in [clickhouse]
|
||||
/// Unsigned integer type in [ClickHouse].
|
||||
/// Note: UInt128 means 128 bits in [ClickHouse].
|
||||
///
|
||||
/// [clickhouse]: https://clickhouse.com/docs/en/sql-reference/data-types/int-uint
|
||||
/// [ClickHouse]: https://clickhouse.com/docs/en/sql-reference/data-types/int-uint
|
||||
UInt128,
|
||||
/// Unsigned integer type in [clickhouse]
|
||||
/// Note: UInt256 mean 256 bits in [clickhouse]
|
||||
/// Unsigned integer type in [ClickHouse].
|
||||
/// Note: UInt256 means 256 bits in [ClickHouse].
|
||||
///
|
||||
/// [clickhouse]: https://clickhouse.com/docs/en/sql-reference/data-types/int-uint
|
||||
/// [ClickHouse]: https://clickhouse.com/docs/en/sql-reference/data-types/int-uint
|
||||
UInt256,
|
||||
/// Big integer with optional display width e.g. BIGINT or BIGINT(20)
|
||||
/// Big integer with optional display width, e.g. BIGINT or BIGINT(20).
|
||||
BigInt(Option<u64>),
|
||||
/// Unsigned big integer with optional display width e.g. BIGINT UNSIGNED or BIGINT(20) UNSIGNED
|
||||
/// Unsigned big integer with optional display width, e.g. BIGINT UNSIGNED or BIGINT(20) UNSIGNED.
|
||||
BigIntUnsigned(Option<u64>),
|
||||
/// Unsigned Int8 with optional display width e.g. INT8 UNSIGNED or INT8(11) UNSIGNED
|
||||
/// Unsigned big integer, e.g. UBIGINT.
|
||||
UBigInt,
|
||||
/// Unsigned Int8 with optional display width, e.g. INT8 UNSIGNED or INT8(11) UNSIGNED.
|
||||
Int8Unsigned(Option<u64>),
|
||||
/// Signed integer as used in [MySQL CAST] target types, without optional `INTEGER` suffix:
|
||||
/// `SIGNED`
|
||||
/// Signed integer as used in [MySQL CAST] target types, without optional `INTEGER` suffix,
|
||||
/// e.g. `SIGNED`
|
||||
///
|
||||
/// [MySQL CAST]: https://dev.mysql.com/doc/refman/8.4/en/cast-functions.html
|
||||
Signed,
|
||||
/// Signed integer as used in [MySQL CAST] target types, with optional `INTEGER` suffix:
|
||||
/// `SIGNED INTEGER`
|
||||
/// Signed integer as used in [MySQL CAST] target types, with optional `INTEGER` suffix,
|
||||
/// e.g. `SIGNED INTEGER`
|
||||
///
|
||||
/// [MySQL CAST]: https://dev.mysql.com/doc/refman/8.4/en/cast-functions.html
|
||||
SignedInteger,
|
||||
/// Signed integer as used in [MySQL CAST] target types, without optional `INTEGER` suffix:
|
||||
/// `SIGNED`
|
||||
/// Signed integer as used in [MySQL CAST] target types, without optional `INTEGER` suffix,
|
||||
/// e.g. `SIGNED`
|
||||
///
|
||||
/// [MySQL CAST]: https://dev.mysql.com/doc/refman/8.4/en/cast-functions.html
|
||||
Unsigned,
|
||||
/// Unsigned integer as used in [MySQL CAST] target types, with optional `INTEGER` suffix:
|
||||
/// `UNSIGNED INTEGER`
|
||||
/// Unsigned integer as used in [MySQL CAST] target types, with optional `INTEGER` suffix,
|
||||
/// e.g. `UNSIGNED INTEGER`.
|
||||
///
|
||||
/// [MySQL CAST]: https://dev.mysql.com/doc/refman/8.4/en/cast-functions.html
|
||||
UnsignedInteger,
|
||||
/// Float4 as alias for Real in [postgresql]
|
||||
/// Float4 is an alias for Real in [PostgreSQL].
|
||||
///
|
||||
/// [postgresql]: https://www.postgresql.org/docs/15/datatype.html
|
||||
/// [PostgreSQL]: https://www.postgresql.org/docs/current/datatype.html
|
||||
Float4,
|
||||
/// Floating point in [clickhouse]
|
||||
/// Floating point in [ClickHouse].
|
||||
///
|
||||
/// [clickhouse]: https://clickhouse.com/docs/en/sql-reference/data-types/float
|
||||
/// [ClickHouse]: https://clickhouse.com/docs/en/sql-reference/data-types/float
|
||||
Float32,
|
||||
/// Floating point in [bigquery]
|
||||
/// Floating point in [BigQuery].
|
||||
///
|
||||
/// [bigquery]: https://cloud.google.com/bigquery/docs/reference/standard-sql/data-types#floating_point_types
|
||||
/// [clickhouse]: https://clickhouse.com/docs/en/sql-reference/data-types/float
|
||||
/// [BigQuery]: https://cloud.google.com/bigquery/docs/reference/standard-sql/data-types#floating_point_types
|
||||
/// [ClickHouse]: https://clickhouse.com/docs/en/sql-reference/data-types/float
|
||||
Float64,
|
||||
/// Floating point e.g. REAL
|
||||
/// Floating point, e.g. REAL.
|
||||
Real,
|
||||
/// Float8 as alias for Double in [postgresql]
|
||||
/// Float8 is an alias for Double in [PostgreSQL].
|
||||
///
|
||||
/// [postgresql]: https://www.postgresql.org/docs/15/datatype.html
|
||||
/// [PostgreSQL]: https://www.postgresql.org/docs/current/datatype.html
|
||||
Float8,
|
||||
/// Double
|
||||
Double(ExactNumberInfo),
|
||||
/// Double PRECISION e.g. [standard], [postgresql]
|
||||
/// Double Precision, see [SQL Standard], [PostgreSQL].
|
||||
///
|
||||
/// [standard]: https://jakewheat.github.io/sql-overview/sql-2016-foundation-grammar.html#approximate-numeric-type
|
||||
/// [postgresql]: https://www.postgresql.org/docs/current/datatype-numeric.html
|
||||
/// [SQL Standard]: https://jakewheat.github.io/sql-overview/sql-2016-foundation-grammar.html#approximate-numeric-type
|
||||
/// [PostgreSQL]: https://www.postgresql.org/docs/current/datatype-numeric.html
|
||||
DoublePrecision,
|
||||
/// Bool as alias for Boolean in [postgresql]
|
||||
/// Bool is an alias for Boolean, see [PostgreSQL].
|
||||
///
|
||||
/// [postgresql]: https://www.postgresql.org/docs/15/datatype.html
|
||||
/// [PostgreSQL]: https://www.postgresql.org/docs/current/datatype.html
|
||||
Bool,
|
||||
/// Boolean
|
||||
/// Boolean type.
|
||||
Boolean,
|
||||
/// Date
|
||||
/// Date type.
|
||||
Date,
|
||||
/// Date32 with the same range as Datetime64
|
||||
/// Date32 with the same range as Datetime64.
|
||||
///
|
||||
/// [1]: https://clickhouse.com/docs/en/sql-reference/data-types/date32
|
||||
Date32,
|
||||
/// Time with optional time precision and time zone information e.g. [standard][1].
|
||||
/// Time with optional time precision and time zone information, see [SQL Standard][1].
|
||||
///
|
||||
/// [1]: https://jakewheat.github.io/sql-overview/sql-2016-foundation-grammar.html#datetime-type
|
||||
Time(Option<u64>, TimezoneInfo),
|
||||
/// Datetime with optional time precision e.g. [MySQL][1].
|
||||
/// Datetime with optional time precision, see [MySQL][1].
|
||||
///
|
||||
/// [1]: https://dev.mysql.com/doc/refman/8.0/en/datetime.html
|
||||
Datetime(Option<u64>),
|
||||
/// Datetime with time precision and optional timezone e.g. [ClickHouse][1].
|
||||
/// Datetime with time precision and optional timezone, see [ClickHouse][1].
|
||||
///
|
||||
/// [1]: https://clickhouse.com/docs/en/sql-reference/data-types/datetime64
|
||||
Datetime64(u64, Option<String>),
|
||||
/// Timestamp with optional time precision and time zone information e.g. [standard][1].
|
||||
/// Timestamp with optional time precision and time zone information, see [SQL Standard][1].
|
||||
///
|
||||
/// [1]: https://jakewheat.github.io/sql-overview/sql-2016-foundation-grammar.html#datetime-type
|
||||
Timestamp(Option<u64>, TimezoneInfo),
|
||||
/// Interval
|
||||
/// Databricks timestamp without time zone. See [1].
|
||||
///
|
||||
/// [1]: https://docs.databricks.com/aws/en/sql/language-manual/data-types/timestamp-ntz-type
|
||||
TimestampNtz,
|
||||
/// Interval type.
|
||||
Interval,
|
||||
/// JSON type
|
||||
/// JSON type.
|
||||
JSON,
|
||||
/// Binary JSON type
|
||||
/// Binary JSON type.
|
||||
JSONB,
|
||||
/// Regclass used in postgresql serial
|
||||
/// Regclass used in [PostgreSQL] serial.
|
||||
///
|
||||
/// [PostgreSQL]: https://www.postgresql.org/docs/current/datatype.html
|
||||
Regclass,
|
||||
/// Text
|
||||
/// Text type.
|
||||
Text,
|
||||
/// [MySQL] text with up to 2**8 bytes
|
||||
/// [MySQL] text with up to 2**8 bytes.
|
||||
///
|
||||
/// [MySQL]: https://dev.mysql.com/doc/refman/9.1/en/blob.html
|
||||
TinyText,
|
||||
/// [MySQL] text with up to 2**24 bytes
|
||||
/// [MySQL] text with up to 2**24 bytes.
|
||||
///
|
||||
/// [MySQL]: https://dev.mysql.com/doc/refman/9.1/en/blob.html
|
||||
MediumText,
|
||||
/// [MySQL] text with up to 2**32 bytes
|
||||
/// [MySQL] text with up to 2**32 bytes.
|
||||
///
|
||||
/// [MySQL]: https://dev.mysql.com/doc/refman/9.1/en/blob.html
|
||||
LongText,
|
||||
|
@ -340,76 +375,85 @@ pub enum DataType {
|
|||
///
|
||||
/// [1]: https://clickhouse.com/docs/en/sql-reference/data-types/fixedstring
|
||||
FixedString(u64),
|
||||
/// Bytea
|
||||
Bytea,
|
||||
/// Bit string, e.g. [Postgres], [MySQL], or [MSSQL]
|
||||
/// Bytea type, see [PostgreSQL].
|
||||
///
|
||||
/// [Postgres]: https://www.postgresql.org/docs/current/datatype-bit.html
|
||||
/// [PostgreSQL]: https://www.postgresql.org/docs/current/datatype-bit.html
|
||||
Bytea,
|
||||
/// Bit string, see [PostgreSQL], [MySQL], or [MSSQL].
|
||||
///
|
||||
/// [PostgreSQL]: https://www.postgresql.org/docs/current/datatype-bit.html
|
||||
/// [MySQL]: https://dev.mysql.com/doc/refman/9.1/en/bit-type.html
|
||||
/// [MSSQL]: https://learn.microsoft.com/en-us/sql/t-sql/data-types/bit-transact-sql?view=sql-server-ver16
|
||||
Bit(Option<u64>),
|
||||
/// `BIT VARYING(n)`: Variable-length bit string e.g. [Postgres]
|
||||
/// `BIT VARYING(n)`: Variable-length bit string, see [PostgreSQL].
|
||||
///
|
||||
/// [Postgres]: https://www.postgresql.org/docs/current/datatype-bit.html
|
||||
/// [PostgreSQL]: https://www.postgresql.org/docs/current/datatype-bit.html
|
||||
BitVarying(Option<u64>),
|
||||
/// `VARBIT(n)`: Variable-length bit string. [Postgres] alias for `BIT VARYING`
|
||||
/// `VARBIT(n)`: Variable-length bit string. [PostgreSQL] alias for `BIT VARYING`.
|
||||
///
|
||||
/// [Postgres]: https://www.postgresql.org/docs/current/datatype.html
|
||||
/// [PostgreSQL]: https://www.postgresql.org/docs/current/datatype.html
|
||||
VarBit(Option<u64>),
|
||||
///
|
||||
/// Custom type such as enums
|
||||
/// Custom types.
|
||||
Custom(ObjectName, Vec<String>),
|
||||
/// Arrays
|
||||
/// Arrays.
|
||||
Array(ArrayElemTypeDef),
|
||||
/// Map
|
||||
/// Map, see [ClickHouse].
|
||||
///
|
||||
/// [clickhouse]: https://clickhouse.com/docs/en/sql-reference/data-types/map
|
||||
/// [ClickHouse]: https://clickhouse.com/docs/en/sql-reference/data-types/map
|
||||
Map(Box<DataType>, Box<DataType>),
|
||||
/// Tuple
|
||||
/// Tuple, see [ClickHouse].
|
||||
///
|
||||
/// [clickhouse]: https://clickhouse.com/docs/en/sql-reference/data-types/tuple
|
||||
/// [ClickHouse]: https://clickhouse.com/docs/en/sql-reference/data-types/tuple
|
||||
Tuple(Vec<StructField>),
|
||||
/// Nested
|
||||
/// Nested type, see [ClickHouse].
|
||||
///
|
||||
/// [clickhouse]: https://clickhouse.com/docs/en/sql-reference/data-types/nested-data-structures/nested
|
||||
/// [ClickHouse]: https://clickhouse.com/docs/en/sql-reference/data-types/nested-data-structures/nested
|
||||
Nested(Vec<ColumnDef>),
|
||||
/// Enums
|
||||
/// Enum type.
|
||||
Enum(Vec<EnumMember>, Option<u8>),
|
||||
/// Set
|
||||
/// Set type.
|
||||
Set(Vec<String>),
|
||||
/// Struct
|
||||
/// Struct type, see [Hive], [BigQuery].
|
||||
///
|
||||
/// [hive]: https://docs.cloudera.com/cdw-runtime/cloud/impala-sql-reference/topics/impala-struct.html
|
||||
/// [bigquery]: https://cloud.google.com/bigquery/docs/reference/standard-sql/data-types#struct_type
|
||||
/// [Hive]: https://docs.cloudera.com/cdw-runtime/cloud/impala-sql-reference/topics/impala-struct.html
|
||||
/// [BigQuery]: https://cloud.google.com/bigquery/docs/reference/standard-sql/data-types#struct_type
|
||||
Struct(Vec<StructField>, StructBracketKind),
|
||||
/// Union
|
||||
/// Union type, see [DuckDB].
|
||||
///
|
||||
/// [duckdb]: https://duckdb.org/docs/sql/data_types/union.html
|
||||
/// [DuckDB]: https://duckdb.org/docs/sql/data_types/union.html
|
||||
Union(Vec<UnionField>),
|
||||
/// Nullable - special marker NULL represents in ClickHouse as a data type.
|
||||
///
|
||||
/// [clickhouse]: https://clickhouse.com/docs/en/sql-reference/data-types/nullable
|
||||
/// [ClickHouse]: https://clickhouse.com/docs/en/sql-reference/data-types/nullable
|
||||
Nullable(Box<DataType>),
|
||||
/// LowCardinality - changes the internal representation of other data types to be dictionary-encoded.
|
||||
///
|
||||
/// [clickhouse]: https://clickhouse.com/docs/en/sql-reference/data-types/lowcardinality
|
||||
/// [ClickHouse]: https://clickhouse.com/docs/en/sql-reference/data-types/lowcardinality
|
||||
LowCardinality(Box<DataType>),
|
||||
/// No type specified - only used with
|
||||
/// [`SQLiteDialect`](crate::dialect::SQLiteDialect), from statements such
|
||||
/// as `CREATE TABLE t1 (a)`.
|
||||
Unspecified,
|
||||
/// Trigger data type, returned by functions associated with triggers
|
||||
/// Trigger data type, returned by functions associated with triggers, see [PostgreSQL].
|
||||
///
|
||||
/// [postgresql]: https://www.postgresql.org/docs/current/plpgsql-trigger.html
|
||||
/// [PostgreSQL]: https://www.postgresql.org/docs/current/plpgsql-trigger.html
|
||||
Trigger,
|
||||
/// Any data type, used in BigQuery UDF definitions for templated parameters
|
||||
/// Any data type, used in BigQuery UDF definitions for templated parameters, see [BigQuery].
|
||||
///
|
||||
/// [bigquery]: https://cloud.google.com/bigquery/docs/user-defined-functions#templated-sql-udf-parameters
|
||||
/// [BigQuery]: https://cloud.google.com/bigquery/docs/user-defined-functions#templated-sql-udf-parameters
|
||||
AnyType,
|
||||
/// geometric type
|
||||
/// Geometric type, see [PostgreSQL].
|
||||
///
|
||||
/// [Postgres]: https://www.postgresql.org/docs/9.5/functions-geometry.html
|
||||
/// [PostgreSQL]: https://www.postgresql.org/docs/9.5/functions-geometry.html
|
||||
GeometricType(GeometricTypeKind),
|
||||
/// PostgreSQL text search vectors, see [PostgreSQL].
|
||||
///
|
||||
/// [PostgreSQL]: https://www.postgresql.org/docs/17/datatype-textsearch.html
|
||||
TsVector,
|
||||
/// PostgreSQL text search query, see [PostgreSQL].
|
||||
///
|
||||
/// [PostgreSQL]: https://www.postgresql.org/docs/17/datatype-textsearch.html
|
||||
TsQuery,
|
||||
}
|
||||
|
||||
impl fmt::Display for DataType {
|
||||
|
@ -499,6 +543,9 @@ impl fmt::Display for DataType {
|
|||
DataType::Int256 => {
|
||||
write!(f, "Int256")
|
||||
}
|
||||
DataType::HugeInt => {
|
||||
write!(f, "HUGEINT")
|
||||
}
|
||||
DataType::Int4Unsigned(zerofill) => {
|
||||
format_type_with_optional_length(f, "INT4", zerofill, true)
|
||||
}
|
||||
|
@ -517,6 +564,18 @@ impl fmt::Display for DataType {
|
|||
DataType::Int8Unsigned(zerofill) => {
|
||||
format_type_with_optional_length(f, "INT8", zerofill, true)
|
||||
}
|
||||
DataType::UTinyInt => {
|
||||
write!(f, "UTINYINT")
|
||||
}
|
||||
DataType::USmallInt => {
|
||||
write!(f, "USMALLINT")
|
||||
}
|
||||
DataType::UBigInt => {
|
||||
write!(f, "UBIGINT")
|
||||
}
|
||||
DataType::UHugeInt => {
|
||||
write!(f, "UHUGEINT")
|
||||
}
|
||||
DataType::UInt8 => {
|
||||
write!(f, "UInt8")
|
||||
}
|
||||
|
@ -567,6 +626,7 @@ impl fmt::Display for DataType {
|
|||
DataType::Timestamp(precision, timezone_info) => {
|
||||
format_datetime_precision_and_tz(f, "TIMESTAMP", precision, timezone_info)
|
||||
}
|
||||
DataType::TimestampNtz => write!(f, "TIMESTAMP_NTZ"),
|
||||
DataType::Datetime64(precision, timezone) => {
|
||||
format_clickhouse_datetime_precision_and_timezone(
|
||||
f,
|
||||
|
@ -606,7 +666,7 @@ impl fmt::Display for DataType {
|
|||
}
|
||||
DataType::Enum(vals, bits) => {
|
||||
match bits {
|
||||
Some(bits) => write!(f, "ENUM{}", bits),
|
||||
Some(bits) => write!(f, "ENUM{bits}"),
|
||||
None => write!(f, "ENUM"),
|
||||
}?;
|
||||
write!(f, "(")?;
|
||||
|
@ -654,16 +714,16 @@ impl fmt::Display for DataType {
|
|||
}
|
||||
// ClickHouse
|
||||
DataType::Nullable(data_type) => {
|
||||
write!(f, "Nullable({})", data_type)
|
||||
write!(f, "Nullable({data_type})")
|
||||
}
|
||||
DataType::FixedString(character_length) => {
|
||||
write!(f, "FixedString({})", character_length)
|
||||
write!(f, "FixedString({character_length})")
|
||||
}
|
||||
DataType::LowCardinality(data_type) => {
|
||||
write!(f, "LowCardinality({})", data_type)
|
||||
write!(f, "LowCardinality({data_type})")
|
||||
}
|
||||
DataType::Map(key_data_type, value_data_type) => {
|
||||
write!(f, "Map({}, {})", key_data_type, value_data_type)
|
||||
write!(f, "Map({key_data_type}, {value_data_type})")
|
||||
}
|
||||
DataType::Tuple(fields) => {
|
||||
write!(f, "Tuple({})", display_comma_separated(fields))
|
||||
|
@ -674,8 +734,20 @@ impl fmt::Display for DataType {
|
|||
DataType::Unspecified => Ok(()),
|
||||
DataType::Trigger => write!(f, "TRIGGER"),
|
||||
DataType::AnyType => write!(f, "ANY TYPE"),
|
||||
DataType::Table(fields) => write!(f, "TABLE({})", display_comma_separated(fields)),
|
||||
DataType::GeometricType(kind) => write!(f, "{}", kind),
|
||||
DataType::Table(fields) => match fields {
|
||||
Some(fields) => {
|
||||
write!(f, "TABLE({})", display_comma_separated(fields))
|
||||
}
|
||||
None => {
|
||||
write!(f, "TABLE")
|
||||
}
|
||||
},
|
||||
DataType::NamedTable { name, columns } => {
|
||||
write!(f, "{} TABLE ({})", name, display_comma_separated(columns))
|
||||
}
|
||||
DataType::GeometricType(kind) => write!(f, "{kind}"),
|
||||
DataType::TsVector => write!(f, "TSVECTOR"),
|
||||
DataType::TsQuery => write!(f, "TSQUERY"),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -777,19 +849,19 @@ pub enum StructBracketKind {
|
|||
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
|
||||
#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
|
||||
pub enum TimezoneInfo {
|
||||
/// No information about time zone. E.g., TIMESTAMP
|
||||
/// No information about time zone, e.g. TIMESTAMP
|
||||
None,
|
||||
/// Temporal type 'WITH TIME ZONE'. E.g., TIMESTAMP WITH TIME ZONE, [standard], [Oracle]
|
||||
/// Temporal type 'WITH TIME ZONE', e.g. TIMESTAMP WITH TIME ZONE, [SQL Standard], [Oracle]
|
||||
///
|
||||
/// [standard]: https://jakewheat.github.io/sql-overview/sql-2016-foundation-grammar.html#datetime-type
|
||||
/// [SQL Standard]: https://jakewheat.github.io/sql-overview/sql-2016-foundation-grammar.html#datetime-type
|
||||
/// [Oracle]: https://docs.oracle.com/en/database/oracle/oracle-database/12.2/nlspg/datetime-data-types-and-time-zone-support.html#GUID-3F1C388E-C651-43D5-ADBC-1A49E5C2CA05
|
||||
WithTimeZone,
|
||||
/// Temporal type 'WITHOUT TIME ZONE'. E.g., TIME WITHOUT TIME ZONE, [standard], [Postgresql]
|
||||
/// Temporal type 'WITHOUT TIME ZONE', e.g. TIME WITHOUT TIME ZONE, [SQL Standard], [Postgresql]
|
||||
///
|
||||
/// [standard]: https://jakewheat.github.io/sql-overview/sql-2016-foundation-grammar.html#datetime-type
|
||||
/// [SQL Standard]: https://jakewheat.github.io/sql-overview/sql-2016-foundation-grammar.html#datetime-type
|
||||
/// [Postgresql]: https://www.postgresql.org/docs/current/datatype-datetime.html
|
||||
WithoutTimeZone,
|
||||
/// Postgresql specific `WITH TIME ZONE` formatting, for both TIME and TIMESTAMP. E.g., TIMETZ, [Postgresql]
|
||||
/// Postgresql specific `WITH TIME ZONE` formatting, for both TIME and TIMESTAMP, e.g. TIMETZ, [Postgresql]
|
||||
///
|
||||
/// [Postgresql]: https://www.postgresql.org/docs/current/datatype-datetime.html
|
||||
Tz,
|
||||
|
@ -818,18 +890,18 @@ impl fmt::Display for TimezoneInfo {
|
|||
}
|
||||
|
||||
/// Additional information for `NUMERIC`, `DECIMAL`, and `DEC` data types
|
||||
/// following the 2016 [standard].
|
||||
/// following the 2016 [SQL Standard].
|
||||
///
|
||||
/// [standard]: https://jakewheat.github.io/sql-overview/sql-2016-foundation-grammar.html#exact-numeric-type
|
||||
/// [SQL Standard]: https://jakewheat.github.io/sql-overview/sql-2016-foundation-grammar.html#exact-numeric-type
|
||||
#[derive(Debug, Copy, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
|
||||
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
|
||||
#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
|
||||
pub enum ExactNumberInfo {
|
||||
/// No additional information e.g. `DECIMAL`
|
||||
/// No additional information, e.g. `DECIMAL`
|
||||
None,
|
||||
/// Only precision information e.g. `DECIMAL(10)`
|
||||
/// Only precision information, e.g. `DECIMAL(10)`
|
||||
Precision(u64),
|
||||
/// Precision and scale information e.g. `DECIMAL(10,2)`
|
||||
/// Precision and scale information, e.g. `DECIMAL(10,2)`
|
||||
PrecisionAndScale(u64, u64),
|
||||
}
|
||||
|
||||
|
@ -870,7 +942,7 @@ impl fmt::Display for CharacterLength {
|
|||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||
match self {
|
||||
CharacterLength::IntegerLength { length, unit } => {
|
||||
write!(f, "{}", length)?;
|
||||
write!(f, "{length}")?;
|
||||
if let Some(unit) = unit {
|
||||
write!(f, " {unit}")?;
|
||||
}
|
||||
|
@ -883,7 +955,7 @@ impl fmt::Display for CharacterLength {
|
|||
}
|
||||
}
|
||||
|
||||
/// Possible units for characters, initially based on 2016 ANSI [standard][1].
|
||||
/// Possible units for characters, initially based on 2016 ANSI [SQL Standard][1].
|
||||
///
|
||||
/// [1]: https://jakewheat.github.io/sql-overview/sql-2016-foundation-grammar.html#char-length-units
|
||||
#[derive(Debug, Copy, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
|
||||
|
@ -925,7 +997,7 @@ impl fmt::Display for BinaryLength {
|
|||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||
match self {
|
||||
BinaryLength::IntegerLength { length } => {
|
||||
write!(f, "{}", length)?;
|
||||
write!(f, "{length}")?;
|
||||
}
|
||||
BinaryLength::Max => {
|
||||
write!(f, "MAX")?;
|
||||
|
@ -956,7 +1028,7 @@ pub enum ArrayElemTypeDef {
|
|||
/// Represents different types of geometric shapes which are commonly used in
|
||||
/// PostgreSQL/Redshift for spatial operations and geometry-related computations.
|
||||
///
|
||||
/// [Postgres]: https://www.postgresql.org/docs/9.5/functions-geometry.html
|
||||
/// [PostgreSQL]: https://www.postgresql.org/docs/9.5/functions-geometry.html
|
||||
#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash, PartialOrd, Ord)]
|
||||
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
|
||||
#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
|
||||
|
|
|
@ -173,7 +173,7 @@ impl fmt::Display for AlterRoleOperation {
|
|||
in_database,
|
||||
} => {
|
||||
if let Some(database_name) = in_database {
|
||||
write!(f, "IN DATABASE {} ", database_name)?;
|
||||
write!(f, "IN DATABASE {database_name} ")?;
|
||||
}
|
||||
|
||||
match config_value {
|
||||
|
@ -187,7 +187,7 @@ impl fmt::Display for AlterRoleOperation {
|
|||
in_database,
|
||||
} => {
|
||||
if let Some(database_name) = in_database {
|
||||
write!(f, "IN DATABASE {} ", database_name)?;
|
||||
write!(f, "IN DATABASE {database_name} ")?;
|
||||
}
|
||||
|
||||
match config_name {
|
||||
|
@ -218,15 +218,15 @@ impl fmt::Display for Use {
|
|||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
||||
f.write_str("USE ")?;
|
||||
match self {
|
||||
Use::Catalog(name) => write!(f, "CATALOG {}", name),
|
||||
Use::Schema(name) => write!(f, "SCHEMA {}", name),
|
||||
Use::Database(name) => write!(f, "DATABASE {}", name),
|
||||
Use::Warehouse(name) => write!(f, "WAREHOUSE {}", name),
|
||||
Use::Role(name) => write!(f, "ROLE {}", name),
|
||||
Use::Catalog(name) => write!(f, "CATALOG {name}"),
|
||||
Use::Schema(name) => write!(f, "SCHEMA {name}"),
|
||||
Use::Database(name) => write!(f, "DATABASE {name}"),
|
||||
Use::Warehouse(name) => write!(f, "WAREHOUSE {name}"),
|
||||
Use::Role(name) => write!(f, "ROLE {name}"),
|
||||
Use::SecondaryRoles(secondary_roles) => {
|
||||
write!(f, "SECONDARY ROLES {}", secondary_roles)
|
||||
write!(f, "SECONDARY ROLES {secondary_roles}")
|
||||
}
|
||||
Use::Object(name) => write!(f, "{}", name),
|
||||
Use::Object(name) => write!(f, "{name}"),
|
||||
Use::Default => write!(f, "DEFAULT"),
|
||||
}
|
||||
}
|
||||
|
|
352
src/ast/ddl.rs
352
src/ast/ddl.rs
|
@ -30,22 +30,48 @@ use sqlparser_derive::{Visit, VisitMut};
|
|||
|
||||
use crate::ast::value::escape_single_quote_string;
|
||||
use crate::ast::{
|
||||
display_comma_separated, display_separated, CommentDef, CreateFunctionBody,
|
||||
display_comma_separated, display_separated, ArgMode, CommentDef, CreateFunctionBody,
|
||||
CreateFunctionUsing, DataType, Expr, FunctionBehavior, FunctionCalledOnNull,
|
||||
FunctionDeterminismSpecifier, FunctionParallel, Ident, MySQLColumnPosition, ObjectName,
|
||||
OperateFunctionArg, OrderByExpr, ProjectionSelect, SequenceOptions, SqlOption, Tag, Value,
|
||||
ValueWithSpan,
|
||||
FunctionDeterminismSpecifier, FunctionParallel, Ident, IndexColumn, MySQLColumnPosition,
|
||||
ObjectName, OperateFunctionArg, OrderByExpr, ProjectionSelect, SequenceOptions, SqlOption, Tag,
|
||||
Value, ValueWithSpan,
|
||||
};
|
||||
use crate::keywords::Keyword;
|
||||
use crate::tokenizer::Token;
|
||||
|
||||
/// ALTER TABLE operation REPLICA IDENTITY values
|
||||
/// See [Postgres ALTER TABLE docs](https://www.postgresql.org/docs/current/sql-altertable.html)
|
||||
#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
|
||||
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
|
||||
#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
|
||||
pub enum ReplicaIdentity {
|
||||
None,
|
||||
Full,
|
||||
Default,
|
||||
Index(Ident),
|
||||
}
|
||||
|
||||
impl fmt::Display for ReplicaIdentity {
|
||||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
||||
match self {
|
||||
ReplicaIdentity::None => f.write_str("NONE"),
|
||||
ReplicaIdentity::Full => f.write_str("FULL"),
|
||||
ReplicaIdentity::Default => f.write_str("DEFAULT"),
|
||||
ReplicaIdentity::Index(idx) => write!(f, "USING INDEX {idx}"),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// An `ALTER TABLE` (`Statement::AlterTable`) operation
|
||||
#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
|
||||
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
|
||||
#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
|
||||
pub enum AlterTableOperation {
|
||||
/// `ADD <table_constraint>`
|
||||
AddConstraint(TableConstraint),
|
||||
/// `ADD <table_constraint> [NOT VALID]`
|
||||
AddConstraint {
|
||||
constraint: TableConstraint,
|
||||
not_valid: bool,
|
||||
},
|
||||
/// `ADD [COLUMN] [IF NOT EXISTS] <column_def>`
|
||||
AddColumn {
|
||||
/// `[COLUMN]`.
|
||||
|
@ -114,9 +140,10 @@ pub enum AlterTableOperation {
|
|||
name: Ident,
|
||||
drop_behavior: Option<DropBehavior>,
|
||||
},
|
||||
/// `DROP [ COLUMN ] [ IF EXISTS ] <column_name> [ CASCADE ]`
|
||||
/// `DROP [ COLUMN ] [ IF EXISTS ] <column_name> [ , <column_name>, ... ] [ CASCADE ]`
|
||||
DropColumn {
|
||||
column_name: Ident,
|
||||
has_column_keyword: bool,
|
||||
column_names: Vec<Ident>,
|
||||
if_exists: bool,
|
||||
drop_behavior: Option<DropBehavior>,
|
||||
},
|
||||
|
@ -151,8 +178,24 @@ pub enum AlterTableOperation {
|
|||
},
|
||||
/// `DROP PRIMARY KEY`
|
||||
///
|
||||
/// Note: this is a MySQL-specific operation.
|
||||
/// Note: this is a [MySQL]-specific operation.
|
||||
///
|
||||
/// [MySQL]: https://dev.mysql.com/doc/refman/8.4/en/alter-table.html
|
||||
DropPrimaryKey,
|
||||
/// `DROP FOREIGN KEY <fk_symbol>`
|
||||
///
|
||||
/// Note: this is a [MySQL]-specific operation.
|
||||
///
|
||||
/// [MySQL]: https://dev.mysql.com/doc/refman/8.4/en/alter-table.html
|
||||
DropForeignKey {
|
||||
name: Ident,
|
||||
},
|
||||
/// `DROP INDEX <index_name>`
|
||||
///
|
||||
/// [MySQL]: https://dev.mysql.com/doc/refman/8.4/en/alter-table.html
|
||||
DropIndex {
|
||||
name: Ident,
|
||||
},
|
||||
/// `ENABLE ALWAYS RULE rewrite_rule_name`
|
||||
///
|
||||
/// Note: this is a PostgreSQL-specific operation.
|
||||
|
@ -198,6 +241,13 @@ pub enum AlterTableOperation {
|
|||
old_partitions: Vec<Expr>,
|
||||
new_partitions: Vec<Expr>,
|
||||
},
|
||||
/// REPLICA IDENTITY { DEFAULT | USING INDEX index_name | FULL | NOTHING }
|
||||
///
|
||||
/// Note: this is a PostgreSQL-specific operation.
|
||||
/// Please refer to [PostgreSQL documentation](https://www.postgresql.org/docs/current/sql-altertable.html)
|
||||
ReplicaIdentity {
|
||||
identity: ReplicaIdentity,
|
||||
},
|
||||
/// Add Partitions
|
||||
AddPartitions {
|
||||
if_not_exists: bool,
|
||||
|
@ -278,6 +328,16 @@ pub enum AlterTableOperation {
|
|||
equals: bool,
|
||||
algorithm: AlterTableAlgorithm,
|
||||
},
|
||||
|
||||
/// `LOCK [=] { DEFAULT | NONE | SHARED | EXCLUSIVE }`
|
||||
///
|
||||
/// [MySQL]-specific table alter lock.
|
||||
///
|
||||
/// [MySQL]: https://dev.mysql.com/doc/refman/8.4/en/alter-table.html
|
||||
Lock {
|
||||
equals: bool,
|
||||
lock: AlterTableLock,
|
||||
},
|
||||
/// `AUTO_INCREMENT [=] <value>`
|
||||
///
|
||||
/// [MySQL]-specific table option for raising current auto increment value.
|
||||
|
@ -287,6 +347,10 @@ pub enum AlterTableOperation {
|
|||
equals: bool,
|
||||
value: ValueWithSpan,
|
||||
},
|
||||
/// `VALIDATE CONSTRAINT <name>`
|
||||
ValidateConstraint {
|
||||
name: Ident,
|
||||
},
|
||||
}
|
||||
|
||||
/// An `ALTER Policy` (`Statement::AlterPolicy`) operation
|
||||
|
@ -356,6 +420,30 @@ impl fmt::Display for AlterTableAlgorithm {
|
|||
}
|
||||
}
|
||||
|
||||
/// [MySQL] `ALTER TABLE` lock.
|
||||
///
|
||||
/// [MySQL]: https://dev.mysql.com/doc/refman/8.4/en/alter-table.html
|
||||
#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
|
||||
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
|
||||
#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
|
||||
pub enum AlterTableLock {
|
||||
Default,
|
||||
None,
|
||||
Shared,
|
||||
Exclusive,
|
||||
}
|
||||
|
||||
impl fmt::Display for AlterTableLock {
|
||||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
||||
f.write_str(match self {
|
||||
Self::Default => "DEFAULT",
|
||||
Self::None => "NONE",
|
||||
Self::Shared => "SHARED",
|
||||
Self::Exclusive => "EXCLUSIVE",
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
|
||||
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
|
||||
#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
|
||||
|
@ -369,7 +457,7 @@ pub enum Owner {
|
|||
impl fmt::Display for Owner {
|
||||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
||||
match self {
|
||||
Owner::Ident(ident) => write!(f, "{}", ident),
|
||||
Owner::Ident(ident) => write!(f, "{ident}"),
|
||||
Owner::CurrentRole => write!(f, "CURRENT_ROLE"),
|
||||
Owner::CurrentUser => write!(f, "CURRENT_USER"),
|
||||
Owner::SessionUser => write!(f, "SESSION_USER"),
|
||||
|
@ -413,7 +501,16 @@ impl fmt::Display for AlterTableOperation {
|
|||
display_separated(new_partitions, " "),
|
||||
ine = if *if_not_exists { " IF NOT EXISTS" } else { "" }
|
||||
),
|
||||
AlterTableOperation::AddConstraint(c) => write!(f, "ADD {c}"),
|
||||
AlterTableOperation::AddConstraint {
|
||||
not_valid,
|
||||
constraint,
|
||||
} => {
|
||||
write!(f, "ADD {constraint}")?;
|
||||
if *not_valid {
|
||||
write!(f, " NOT VALID")?;
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
AlterTableOperation::AddColumn {
|
||||
column_keyword,
|
||||
if_not_exists,
|
||||
|
@ -444,7 +541,7 @@ impl fmt::Display for AlterTableOperation {
|
|||
if *if_not_exists {
|
||||
write!(f, " IF NOT EXISTS")?;
|
||||
}
|
||||
write!(f, " {} ({})", name, query)
|
||||
write!(f, " {name} ({query})")
|
||||
}
|
||||
AlterTableOperation::Algorithm { equals, algorithm } => {
|
||||
write!(
|
||||
|
@ -459,7 +556,7 @@ impl fmt::Display for AlterTableOperation {
|
|||
if *if_exists {
|
||||
write!(f, " IF EXISTS")?;
|
||||
}
|
||||
write!(f, " {}", name)
|
||||
write!(f, " {name}")
|
||||
}
|
||||
AlterTableOperation::MaterializeProjection {
|
||||
if_exists,
|
||||
|
@ -470,9 +567,9 @@ impl fmt::Display for AlterTableOperation {
|
|||
if *if_exists {
|
||||
write!(f, " IF EXISTS")?;
|
||||
}
|
||||
write!(f, " {}", name)?;
|
||||
write!(f, " {name}")?;
|
||||
if let Some(partition) = partition {
|
||||
write!(f, " IN PARTITION {}", partition)?;
|
||||
write!(f, " IN PARTITION {partition}")?;
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
@ -485,9 +582,9 @@ impl fmt::Display for AlterTableOperation {
|
|||
if *if_exists {
|
||||
write!(f, " IF EXISTS")?;
|
||||
}
|
||||
write!(f, " {}", name)?;
|
||||
write!(f, " {name}")?;
|
||||
if let Some(partition) = partition {
|
||||
write!(f, " IN PARTITION {}", partition)?;
|
||||
write!(f, " IN PARTITION {partition}")?;
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
@ -530,15 +627,19 @@ impl fmt::Display for AlterTableOperation {
|
|||
)
|
||||
}
|
||||
AlterTableOperation::DropPrimaryKey => write!(f, "DROP PRIMARY KEY"),
|
||||
AlterTableOperation::DropForeignKey { name } => write!(f, "DROP FOREIGN KEY {name}"),
|
||||
AlterTableOperation::DropIndex { name } => write!(f, "DROP INDEX {name}"),
|
||||
AlterTableOperation::DropColumn {
|
||||
column_name,
|
||||
has_column_keyword,
|
||||
column_names: column_name,
|
||||
if_exists,
|
||||
drop_behavior,
|
||||
} => write!(
|
||||
f,
|
||||
"DROP COLUMN {}{}{}",
|
||||
"DROP {}{}{}{}",
|
||||
if *has_column_keyword { "COLUMN " } else { "" },
|
||||
if *if_exists { "IF EXISTS " } else { "" },
|
||||
column_name,
|
||||
display_comma_separated(column_name),
|
||||
match drop_behavior {
|
||||
None => "",
|
||||
Some(DropBehavior::Restrict) => " RESTRICT",
|
||||
|
@ -681,6 +782,15 @@ impl fmt::Display for AlterTableOperation {
|
|||
value
|
||||
)
|
||||
}
|
||||
AlterTableOperation::Lock { equals, lock } => {
|
||||
write!(f, "LOCK {}{}", if *equals { "= " } else { "" }, lock)
|
||||
}
|
||||
AlterTableOperation::ReplicaIdentity { identity } => {
|
||||
write!(f, "REPLICA IDENTITY {identity}")
|
||||
}
|
||||
AlterTableOperation::ValidateConstraint { name } => {
|
||||
write!(f, "VALIDATE CONSTRAINT {name}")
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -802,7 +912,10 @@ pub enum AlterColumnOperation {
|
|||
data_type: DataType,
|
||||
/// PostgreSQL specific
|
||||
using: Option<Expr>,
|
||||
/// Set to true if the statement includes the `SET DATA TYPE` keywords
|
||||
had_set: bool,
|
||||
},
|
||||
|
||||
/// `ADD GENERATED { ALWAYS | BY DEFAULT } AS IDENTITY [ ( sequence_options ) ]`
|
||||
///
|
||||
/// Note: this is a PostgreSQL-specific operation.
|
||||
|
@ -820,15 +933,22 @@ impl fmt::Display for AlterColumnOperation {
|
|||
AlterColumnOperation::SetDefault { value } => {
|
||||
write!(f, "SET DEFAULT {value}")
|
||||
}
|
||||
AlterColumnOperation::DropDefault {} => {
|
||||
AlterColumnOperation::DropDefault => {
|
||||
write!(f, "DROP DEFAULT")
|
||||
}
|
||||
AlterColumnOperation::SetDataType { data_type, using } => {
|
||||
if let Some(expr) = using {
|
||||
write!(f, "SET DATA TYPE {data_type} USING {expr}")
|
||||
} else {
|
||||
write!(f, "SET DATA TYPE {data_type}")
|
||||
AlterColumnOperation::SetDataType {
|
||||
data_type,
|
||||
using,
|
||||
had_set,
|
||||
} => {
|
||||
if *had_set {
|
||||
write!(f, "SET DATA ")?;
|
||||
}
|
||||
write!(f, "TYPE {data_type}")?;
|
||||
if let Some(expr) = using {
|
||||
write!(f, " USING {expr}")?;
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
AlterColumnOperation::AddGenerated {
|
||||
generated_as,
|
||||
|
@ -888,7 +1008,7 @@ pub enum TableConstraint {
|
|||
/// [1]: IndexType
|
||||
index_type: Option<IndexType>,
|
||||
/// Identifiers of the columns that are unique.
|
||||
columns: Vec<Ident>,
|
||||
columns: Vec<IndexColumn>,
|
||||
index_options: Vec<IndexOption>,
|
||||
characteristics: Option<ConstraintCharacteristics>,
|
||||
/// Optional Postgres nulls handling: `[ NULLS [ NOT ] DISTINCT ]`
|
||||
|
@ -924,7 +1044,7 @@ pub enum TableConstraint {
|
|||
/// [1]: IndexType
|
||||
index_type: Option<IndexType>,
|
||||
/// Identifiers of the columns that form the primary key.
|
||||
columns: Vec<Ident>,
|
||||
columns: Vec<IndexColumn>,
|
||||
index_options: Vec<IndexOption>,
|
||||
characteristics: Option<ConstraintCharacteristics>,
|
||||
},
|
||||
|
@ -935,6 +1055,9 @@ pub enum TableConstraint {
|
|||
/// }`).
|
||||
ForeignKey {
|
||||
name: Option<Ident>,
|
||||
/// MySQL-specific field
|
||||
/// <https://dev.mysql.com/doc/refman/8.4/en/create-table-foreign-keys.html>
|
||||
index_name: Option<Ident>,
|
||||
columns: Vec<Ident>,
|
||||
foreign_table: ObjectName,
|
||||
referred_columns: Vec<Ident>,
|
||||
|
@ -942,10 +1065,13 @@ pub enum TableConstraint {
|
|||
on_update: Option<ReferentialAction>,
|
||||
characteristics: Option<ConstraintCharacteristics>,
|
||||
},
|
||||
/// `[ CONSTRAINT <name> ] CHECK (<expr>)`
|
||||
/// `[ CONSTRAINT <name> ] CHECK (<expr>) [[NOT] ENFORCED]`
|
||||
Check {
|
||||
name: Option<Ident>,
|
||||
expr: Box<Expr>,
|
||||
/// MySQL-specific syntax
|
||||
/// <https://dev.mysql.com/doc/refman/8.4/en/create-table.html>
|
||||
enforced: Option<bool>,
|
||||
},
|
||||
/// MySQLs [index definition][1] for index creation. Not present on ANSI so, for now, the usage
|
||||
/// is restricted to MySQL, as no other dialects that support this syntax were found.
|
||||
|
@ -963,7 +1089,7 @@ pub enum TableConstraint {
|
|||
/// [1]: IndexType
|
||||
index_type: Option<IndexType>,
|
||||
/// Referred column identifier list.
|
||||
columns: Vec<Ident>,
|
||||
columns: Vec<IndexColumn>,
|
||||
},
|
||||
/// MySQLs [fulltext][1] definition. Since the [`SPATIAL`][2] definition is exactly the same,
|
||||
/// and MySQL displays both the same way, it is part of this definition as well.
|
||||
|
@ -986,7 +1112,7 @@ pub enum TableConstraint {
|
|||
/// Optional index name.
|
||||
opt_index_name: Option<Ident>,
|
||||
/// Referred column identifier list.
|
||||
columns: Vec<Ident>,
|
||||
columns: Vec<IndexColumn>,
|
||||
},
|
||||
}
|
||||
|
||||
|
@ -1045,6 +1171,7 @@ impl fmt::Display for TableConstraint {
|
|||
}
|
||||
TableConstraint::ForeignKey {
|
||||
name,
|
||||
index_name,
|
||||
columns,
|
||||
foreign_table,
|
||||
referred_columns,
|
||||
|
@ -1054,8 +1181,9 @@ impl fmt::Display for TableConstraint {
|
|||
} => {
|
||||
write!(
|
||||
f,
|
||||
"{}FOREIGN KEY ({}) REFERENCES {}",
|
||||
"{}FOREIGN KEY{} ({}) REFERENCES {}",
|
||||
display_constraint_name(name),
|
||||
display_option_spaced(index_name),
|
||||
display_comma_separated(columns),
|
||||
foreign_table,
|
||||
)?;
|
||||
|
@ -1069,12 +1197,21 @@ impl fmt::Display for TableConstraint {
|
|||
write!(f, " ON UPDATE {action}")?;
|
||||
}
|
||||
if let Some(characteristics) = characteristics {
|
||||
write!(f, " {}", characteristics)?;
|
||||
write!(f, " {characteristics}")?;
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
TableConstraint::Check { name, expr } => {
|
||||
write!(f, "{}CHECK ({})", display_constraint_name(name), expr)
|
||||
TableConstraint::Check {
|
||||
name,
|
||||
expr,
|
||||
enforced,
|
||||
} => {
|
||||
write!(f, "{}CHECK ({})", display_constraint_name(name), expr)?;
|
||||
if let Some(b) = enforced {
|
||||
write!(f, " {}", if *b { "ENFORCED" } else { "NOT ENFORCED" })
|
||||
} else {
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
TableConstraint::Index {
|
||||
display_as_key,
|
||||
|
@ -1174,13 +1311,20 @@ impl fmt::Display for KeyOrIndexDisplay {
|
|||
/// [1]: https://dev.mysql.com/doc/refman/8.0/en/create-table.html
|
||||
/// [2]: https://dev.mysql.com/doc/refman/8.0/en/create-index.html
|
||||
/// [3]: https://www.postgresql.org/docs/14/sql-createindex.html
|
||||
#[derive(Debug, Copy, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
|
||||
#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
|
||||
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
|
||||
#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
|
||||
pub enum IndexType {
|
||||
BTree,
|
||||
Hash,
|
||||
// TODO add Postgresql's possible indexes
|
||||
GIN,
|
||||
GiST,
|
||||
SPGiST,
|
||||
BRIN,
|
||||
Bloom,
|
||||
/// Users may define their own index types, which would
|
||||
/// not be covered by the above variants.
|
||||
Custom(Ident),
|
||||
}
|
||||
|
||||
impl fmt::Display for IndexType {
|
||||
|
@ -1188,6 +1332,12 @@ impl fmt::Display for IndexType {
|
|||
match self {
|
||||
Self::BTree => write!(f, "BTREE"),
|
||||
Self::Hash => write!(f, "HASH"),
|
||||
Self::GIN => write!(f, "GIN"),
|
||||
Self::GiST => write!(f, "GIST"),
|
||||
Self::SPGiST => write!(f, "SPGIST"),
|
||||
Self::BRIN => write!(f, "BRIN"),
|
||||
Self::Bloom => write!(f, "BLOOM"),
|
||||
Self::Custom(name) => write!(f, "{name}"),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -1215,9 +1365,9 @@ impl fmt::Display for IndexOption {
|
|||
}
|
||||
}
|
||||
|
||||
/// [Postgres] unique index nulls handling option: `[ NULLS [ NOT ] DISTINCT ]`
|
||||
/// [PostgreSQL] unique index nulls handling option: `[ NULLS [ NOT ] DISTINCT ]`
|
||||
///
|
||||
/// [Postgres]: https://www.postgresql.org/docs/17/sql-altertable.html
|
||||
/// [PostgreSQL]: https://www.postgresql.org/docs/17/sql-altertable.html
|
||||
#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
|
||||
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
|
||||
#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
|
||||
|
@ -1246,13 +1396,18 @@ impl fmt::Display for NullsDistinctOption {
|
|||
pub struct ProcedureParam {
|
||||
pub name: Ident,
|
||||
pub data_type: DataType,
|
||||
pub mode: Option<ArgMode>,
|
||||
}
|
||||
|
||||
impl fmt::Display for ProcedureParam {
|
||||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
||||
if let Some(mode) = &self.mode {
|
||||
write!(f, "{mode} {} {}", self.name, self.data_type)
|
||||
} else {
|
||||
write!(f, "{} {}", self.name, self.data_type)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// SQL column definition
|
||||
#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
|
||||
|
@ -1300,17 +1455,41 @@ impl fmt::Display for ColumnDef {
|
|||
pub struct ViewColumnDef {
|
||||
pub name: Ident,
|
||||
pub data_type: Option<DataType>,
|
||||
pub options: Option<Vec<ColumnOption>>,
|
||||
pub options: Option<ColumnOptions>,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
|
||||
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
|
||||
#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
|
||||
pub enum ColumnOptions {
|
||||
CommaSeparated(Vec<ColumnOption>),
|
||||
SpaceSeparated(Vec<ColumnOption>),
|
||||
}
|
||||
|
||||
impl ColumnOptions {
|
||||
pub fn as_slice(&self) -> &[ColumnOption] {
|
||||
match self {
|
||||
ColumnOptions::CommaSeparated(options) => options.as_slice(),
|
||||
ColumnOptions::SpaceSeparated(options) => options.as_slice(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl fmt::Display for ViewColumnDef {
|
||||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
||||
write!(f, "{}", self.name)?;
|
||||
if let Some(data_type) = self.data_type.as_ref() {
|
||||
write!(f, " {}", data_type)?;
|
||||
write!(f, " {data_type}")?;
|
||||
}
|
||||
if let Some(options) = self.options.as_ref() {
|
||||
write!(f, " {}", display_comma_separated(options.as_slice()))?;
|
||||
match options {
|
||||
ColumnOptions::CommaSeparated(column_options) => {
|
||||
write!(f, " {}", display_comma_separated(column_options.as_slice()))?;
|
||||
}
|
||||
ColumnOptions::SpaceSeparated(column_options) => {
|
||||
write!(f, " {}", display_separated(column_options.as_slice(), " "))?
|
||||
}
|
||||
}
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
@ -1530,7 +1709,7 @@ pub struct ColumnPolicyProperty {
|
|||
/// ```
|
||||
/// [Snowflake]: https://docs.snowflake.com/en/sql-reference/sql/create-table
|
||||
pub with: bool,
|
||||
pub policy_name: Ident,
|
||||
pub policy_name: ObjectName,
|
||||
pub using_columns: Option<Vec<Ident>>,
|
||||
}
|
||||
|
||||
|
@ -1664,6 +1843,13 @@ pub enum ColumnOption {
|
|||
/// ```
|
||||
/// [Snowflake]: https://docs.snowflake.com/en/sql-reference/sql/create-table
|
||||
Tags(TagsColumnOption),
|
||||
/// MySQL specific: Spatial reference identifier
|
||||
/// Syntax:
|
||||
/// ```sql
|
||||
/// CREATE TABLE geom (g GEOMETRY NOT NULL SRID 4326);
|
||||
/// ```
|
||||
/// [MySQL]: https://dev.mysql.com/doc/refman/8.4/en/creating-spatial-indexes.html
|
||||
Srid(Box<Expr>),
|
||||
}
|
||||
|
||||
impl fmt::Display for ColumnOption {
|
||||
|
@ -1688,7 +1874,7 @@ impl fmt::Display for ColumnOption {
|
|||
} => {
|
||||
write!(f, "{}", if *is_primary { "PRIMARY KEY" } else { "UNIQUE" })?;
|
||||
if let Some(characteristics) = characteristics {
|
||||
write!(f, " {}", characteristics)?;
|
||||
write!(f, " {characteristics}")?;
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
@ -1710,7 +1896,7 @@ impl fmt::Display for ColumnOption {
|
|||
write!(f, " ON UPDATE {action}")?;
|
||||
}
|
||||
if let Some(characteristics) = characteristics {
|
||||
write!(f, " {}", characteristics)?;
|
||||
write!(f, " {characteristics}")?;
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
@ -1770,7 +1956,7 @@ impl fmt::Display for ColumnOption {
|
|||
write!(f, "{parameters}")
|
||||
}
|
||||
OnConflict(keyword) => {
|
||||
write!(f, "ON CONFLICT {:?}", keyword)?;
|
||||
write!(f, "ON CONFLICT {keyword:?}")?;
|
||||
Ok(())
|
||||
}
|
||||
Policy(parameters) => {
|
||||
|
@ -1779,6 +1965,9 @@ impl fmt::Display for ColumnOption {
|
|||
Tags(tags) => {
|
||||
write!(f, "{tags}")
|
||||
}
|
||||
Srid(srid) => {
|
||||
write!(f, "SRID {srid}")
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -2092,10 +2281,63 @@ impl fmt::Display for ClusteredBy {
|
|||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
|
||||
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
|
||||
#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
|
||||
/// ```sql
|
||||
/// CREATE DOMAIN name [ AS ] data_type
|
||||
/// [ COLLATE collation ]
|
||||
/// [ DEFAULT expression ]
|
||||
/// [ domain_constraint [ ... ] ]
|
||||
///
|
||||
/// where domain_constraint is:
|
||||
///
|
||||
/// [ CONSTRAINT constraint_name ]
|
||||
/// { NOT NULL | NULL | CHECK (expression) }
|
||||
/// ```
|
||||
/// See [PostgreSQL](https://www.postgresql.org/docs/current/sql-createdomain.html)
|
||||
pub struct CreateDomain {
|
||||
/// The name of the domain to be created.
|
||||
pub name: ObjectName,
|
||||
/// The data type of the domain.
|
||||
pub data_type: DataType,
|
||||
/// The collation of the domain.
|
||||
pub collation: Option<Ident>,
|
||||
/// The default value of the domain.
|
||||
pub default: Option<Expr>,
|
||||
/// The constraints of the domain.
|
||||
pub constraints: Vec<TableConstraint>,
|
||||
}
|
||||
|
||||
impl fmt::Display for CreateDomain {
|
||||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
||||
write!(
|
||||
f,
|
||||
"CREATE DOMAIN {name} AS {data_type}",
|
||||
name = self.name,
|
||||
data_type = self.data_type
|
||||
)?;
|
||||
if let Some(collation) = &self.collation {
|
||||
write!(f, " COLLATE {collation}")?;
|
||||
}
|
||||
if let Some(default) = &self.default {
|
||||
write!(f, " DEFAULT {default}")?;
|
||||
}
|
||||
if !self.constraints.is_empty() {
|
||||
write!(f, " {}", display_separated(&self.constraints, " "))?;
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
|
||||
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
|
||||
#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
|
||||
pub struct CreateFunction {
|
||||
/// True if this is a `CREATE OR ALTER FUNCTION` statement
|
||||
///
|
||||
/// [MsSql](https://learn.microsoft.com/en-us/sql/t-sql/statements/create-function-transact-sql?view=sql-server-ver16#or-alter)
|
||||
pub or_alter: bool,
|
||||
pub or_replace: bool,
|
||||
pub temporary: bool,
|
||||
pub if_not_exists: bool,
|
||||
|
@ -2114,15 +2356,15 @@ pub struct CreateFunction {
|
|||
///
|
||||
/// IMMUTABLE | STABLE | VOLATILE
|
||||
///
|
||||
/// [Postgres](https://www.postgresql.org/docs/current/sql-createfunction.html)
|
||||
/// [PostgreSQL](https://www.postgresql.org/docs/current/sql-createfunction.html)
|
||||
pub behavior: Option<FunctionBehavior>,
|
||||
/// CALLED ON NULL INPUT | RETURNS NULL ON NULL INPUT | STRICT
|
||||
///
|
||||
/// [Postgres](https://www.postgresql.org/docs/current/sql-createfunction.html)
|
||||
/// [PostgreSQL](https://www.postgresql.org/docs/current/sql-createfunction.html)
|
||||
pub called_on_null: Option<FunctionCalledOnNull>,
|
||||
/// PARALLEL { UNSAFE | RESTRICTED | SAFE }
|
||||
///
|
||||
/// [Postgres](https://www.postgresql.org/docs/current/sql-createfunction.html)
|
||||
/// [PostgreSQL](https://www.postgresql.org/docs/current/sql-createfunction.html)
|
||||
pub parallel: Option<FunctionParallel>,
|
||||
/// USING ... (Hive only)
|
||||
pub using: Option<CreateFunctionUsing>,
|
||||
|
@ -2158,9 +2400,10 @@ impl fmt::Display for CreateFunction {
|
|||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
||||
write!(
|
||||
f,
|
||||
"CREATE {or_replace}{temp}FUNCTION {if_not_exists}{name}",
|
||||
"CREATE {or_alter}{or_replace}{temp}FUNCTION {if_not_exists}{name}",
|
||||
name = self.name,
|
||||
temp = if self.temporary { "TEMPORARY " } else { "" },
|
||||
or_alter = if self.or_alter { "OR ALTER " } else { "" },
|
||||
or_replace = if self.or_replace { "OR REPLACE " } else { "" },
|
||||
if_not_exists = if self.if_not_exists {
|
||||
"IF NOT EXISTS "
|
||||
|
@ -2198,6 +2441,12 @@ impl fmt::Display for CreateFunction {
|
|||
if let Some(CreateFunctionBody::Return(function_body)) = &self.function_body {
|
||||
write!(f, " RETURN {function_body}")?;
|
||||
}
|
||||
if let Some(CreateFunctionBody::AsReturnExpr(function_body)) = &self.function_body {
|
||||
write!(f, " AS RETURN {function_body}")?;
|
||||
}
|
||||
if let Some(CreateFunctionBody::AsReturnSelect(function_body)) = &self.function_body {
|
||||
write!(f, " AS RETURN {function_body}")?;
|
||||
}
|
||||
if let Some(using) = &self.using {
|
||||
write!(f, " {using}")?;
|
||||
}
|
||||
|
@ -2211,6 +2460,9 @@ impl fmt::Display for CreateFunction {
|
|||
if let Some(CreateFunctionBody::AsAfterOptions(function_body)) = &self.function_body {
|
||||
write!(f, " AS {function_body}")?;
|
||||
}
|
||||
if let Some(CreateFunctionBody::AsBeginEnd(bes)) = &self.function_body {
|
||||
write!(f, " AS {bes}")?;
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
|
174
src/ast/dml.rs
174
src/ast/dml.rs
|
@ -29,17 +29,38 @@ use serde::{Deserialize, Serialize};
|
|||
#[cfg(feature = "visitor")]
|
||||
use sqlparser_derive::{Visit, VisitMut};
|
||||
|
||||
use crate::display_utils::{indented_list, DisplayCommaSeparated, Indent, NewLine, SpaceOrNewline};
|
||||
|
||||
pub use super::ddl::{ColumnDef, TableConstraint};
|
||||
|
||||
use super::{
|
||||
display_comma_separated, display_separated, query::InputFormatClause, Assignment, ClusteredBy,
|
||||
CommentDef, Expr, FileFormat, FromTable, HiveDistributionStyle, HiveFormat, HiveIOFormat,
|
||||
HiveRowFormat, Ident, InsertAliases, MysqlInsertPriority, ObjectName, OnCommit, OnInsert,
|
||||
OneOrManyWithParens, OrderByExpr, Query, RowAccessPolicy, SelectItem, Setting, SqlOption,
|
||||
SqliteOnConflict, StorageSerializationPolicy, TableEngine, TableObject, TableWithJoins, Tag,
|
||||
CommentDef, CreateTableOptions, Expr, FileFormat, FromTable, HiveDistributionStyle, HiveFormat,
|
||||
HiveIOFormat, HiveRowFormat, Ident, IndexType, InsertAliases, MysqlInsertPriority, ObjectName,
|
||||
OnCommit, OnInsert, OneOrManyWithParens, OrderByExpr, Query, RowAccessPolicy, SelectItem,
|
||||
Setting, SqliteOnConflict, StorageSerializationPolicy, TableObject, TableWithJoins, Tag,
|
||||
WrappedCollection,
|
||||
};
|
||||
|
||||
/// Index column type.
|
||||
#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
|
||||
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
|
||||
#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
|
||||
pub struct IndexColumn {
|
||||
pub column: OrderByExpr,
|
||||
pub operator_class: Option<Ident>,
|
||||
}
|
||||
|
||||
impl Display for IndexColumn {
|
||||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
||||
write!(f, "{}", self.column)?;
|
||||
if let Some(operator_class) = &self.operator_class {
|
||||
write!(f, " {operator_class}")?;
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
/// CREATE INDEX statement.
|
||||
#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
|
||||
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
|
||||
|
@ -49,8 +70,8 @@ pub struct CreateIndex {
|
|||
pub name: Option<ObjectName>,
|
||||
#[cfg_attr(feature = "visitor", visit(with = "visit_relation"))]
|
||||
pub table_name: ObjectName,
|
||||
pub using: Option<Ident>,
|
||||
pub columns: Vec<OrderByExpr>,
|
||||
pub using: Option<IndexType>,
|
||||
pub columns: Vec<IndexColumn>,
|
||||
pub unique: bool,
|
||||
pub concurrently: bool,
|
||||
pub if_not_exists: bool,
|
||||
|
@ -127,19 +148,17 @@ pub struct CreateTable {
|
|||
pub constraints: Vec<TableConstraint>,
|
||||
pub hive_distribution: HiveDistributionStyle,
|
||||
pub hive_formats: Option<HiveFormat>,
|
||||
pub table_properties: Vec<SqlOption>,
|
||||
pub with_options: Vec<SqlOption>,
|
||||
pub table_options: CreateTableOptions,
|
||||
pub file_format: Option<FileFormat>,
|
||||
pub location: Option<String>,
|
||||
pub query: Option<Box<Query>>,
|
||||
pub without_rowid: bool,
|
||||
pub like: Option<ObjectName>,
|
||||
pub clone: Option<ObjectName>,
|
||||
pub engine: Option<TableEngine>,
|
||||
// For Hive dialect, the table comment is after the column definitions without `=`,
|
||||
// so the `comment` field is optional and different than the comment field in the general options list.
|
||||
// [Hive](https://cwiki.apache.org/confluence/display/Hive/LanguageManual+DDL#LanguageManualDDL-CreateTable)
|
||||
pub comment: Option<CommentDef>,
|
||||
pub auto_increment_offset: Option<u32>,
|
||||
pub default_charset: Option<String>,
|
||||
pub collation: Option<String>,
|
||||
pub on_commit: Option<OnCommit>,
|
||||
/// ClickHouse "ON CLUSTER" clause:
|
||||
/// <https://clickhouse.com/docs/en/sql-reference/distributed-ddl/>
|
||||
|
@ -156,13 +175,17 @@ pub struct CreateTable {
|
|||
pub partition_by: Option<Box<Expr>>,
|
||||
/// BigQuery: Table clustering column list.
|
||||
/// <https://cloud.google.com/bigquery/docs/reference/standard-sql/data-definition-language#table_option_list>
|
||||
pub cluster_by: Option<WrappedCollection<Vec<Ident>>>,
|
||||
/// Snowflake: Table clustering list which contains base column, expressions on base columns.
|
||||
/// <https://docs.snowflake.com/en/user-guide/tables-clustering-keys#defining-a-clustering-key-for-a-table>
|
||||
pub cluster_by: Option<WrappedCollection<Vec<Expr>>>,
|
||||
/// Hive: Table clustering column list.
|
||||
/// <https://cwiki.apache.org/confluence/display/Hive/LanguageManual+DDL#LanguageManualDDL-CreateTable>
|
||||
pub clustered_by: Option<ClusteredBy>,
|
||||
/// BigQuery: Table options list.
|
||||
/// <https://cloud.google.com/bigquery/docs/reference/standard-sql/data-definition-language#table_option_list>
|
||||
pub options: Option<Vec<SqlOption>>,
|
||||
/// Postgres `INHERITs` clause, which contains the list of tables from which
|
||||
/// the new table inherits.
|
||||
/// <https://www.postgresql.org/docs/current/ddl-inherit.html>
|
||||
/// <https://www.postgresql.org/docs/current/sql-createtable.html#SQL-CREATETABLE-PARMS-INHERITS>
|
||||
pub inherits: Option<Vec<ObjectName>>,
|
||||
/// SQLite "STRICT" clause.
|
||||
/// if the "STRICT" table-option keyword is added to the end, after the closing ")",
|
||||
/// then strict typing rules apply to that table.
|
||||
|
@ -243,22 +266,27 @@ impl Display for CreateTable {
|
|||
name = self.name,
|
||||
)?;
|
||||
if let Some(on_cluster) = &self.on_cluster {
|
||||
write!(f, " ON CLUSTER {}", on_cluster)?;
|
||||
write!(f, " ON CLUSTER {on_cluster}")?;
|
||||
}
|
||||
if !self.columns.is_empty() || !self.constraints.is_empty() {
|
||||
write!(f, " ({}", display_comma_separated(&self.columns))?;
|
||||
f.write_str(" (")?;
|
||||
NewLine.fmt(f)?;
|
||||
Indent(DisplayCommaSeparated(&self.columns)).fmt(f)?;
|
||||
if !self.columns.is_empty() && !self.constraints.is_empty() {
|
||||
write!(f, ", ")?;
|
||||
f.write_str(",")?;
|
||||
SpaceOrNewline.fmt(f)?;
|
||||
}
|
||||
write!(f, "{})", display_comma_separated(&self.constraints))?;
|
||||
Indent(DisplayCommaSeparated(&self.constraints)).fmt(f)?;
|
||||
NewLine.fmt(f)?;
|
||||
f.write_str(")")?;
|
||||
} else if self.query.is_none() && self.like.is_none() && self.clone.is_none() {
|
||||
// PostgreSQL allows `CREATE TABLE t ();`, but requires empty parens
|
||||
write!(f, " ()")?;
|
||||
f.write_str(" ()")?;
|
||||
}
|
||||
|
||||
// Hive table comment should be after column definitions, please refer to:
|
||||
// [Hive](https://cwiki.apache.org/confluence/display/Hive/LanguageManual+DDL#LanguageManualDDL-CreateTable)
|
||||
if let Some(CommentDef::AfterColumnDefsWithoutEq(comment)) = &self.comment {
|
||||
if let Some(comment) = &self.comment {
|
||||
write!(f, " COMMENT '{comment}'")?;
|
||||
}
|
||||
|
||||
|
@ -351,40 +379,22 @@ impl Display for CreateTable {
|
|||
}
|
||||
write!(f, " LOCATION '{}'", self.location.as_ref().unwrap())?;
|
||||
}
|
||||
if !self.table_properties.is_empty() {
|
||||
write!(
|
||||
f,
|
||||
" TBLPROPERTIES ({})",
|
||||
display_comma_separated(&self.table_properties)
|
||||
)?;
|
||||
}
|
||||
if !self.with_options.is_empty() {
|
||||
write!(f, " WITH ({})", display_comma_separated(&self.with_options))?;
|
||||
}
|
||||
if let Some(engine) = &self.engine {
|
||||
write!(f, " ENGINE={engine}")?;
|
||||
}
|
||||
if let Some(comment_def) = &self.comment {
|
||||
match comment_def {
|
||||
CommentDef::WithEq(comment) => {
|
||||
write!(f, " COMMENT = '{comment}'")?;
|
||||
}
|
||||
CommentDef::WithoutEq(comment) => {
|
||||
write!(f, " COMMENT '{comment}'")?;
|
||||
}
|
||||
// For CommentDef::AfterColumnDefsWithoutEq will be displayed after column definition
|
||||
CommentDef::AfterColumnDefsWithoutEq(_) => (),
|
||||
}
|
||||
|
||||
match &self.table_options {
|
||||
options @ CreateTableOptions::With(_)
|
||||
| options @ CreateTableOptions::Plain(_)
|
||||
| options @ CreateTableOptions::TableProperties(_) => write!(f, " {options}")?,
|
||||
_ => (),
|
||||
}
|
||||
|
||||
if let Some(auto_increment_offset) = self.auto_increment_offset {
|
||||
write!(f, " AUTO_INCREMENT {auto_increment_offset}")?;
|
||||
}
|
||||
if let Some(primary_key) = &self.primary_key {
|
||||
write!(f, " PRIMARY KEY {}", primary_key)?;
|
||||
write!(f, " PRIMARY KEY {primary_key}")?;
|
||||
}
|
||||
if let Some(order_by) = &self.order_by {
|
||||
write!(f, " ORDER BY {}", order_by)?;
|
||||
write!(f, " ORDER BY {order_by}")?;
|
||||
}
|
||||
if let Some(inherits) = &self.inherits {
|
||||
write!(f, " INHERITS ({})", display_comma_separated(inherits))?;
|
||||
}
|
||||
if let Some(partition_by) = self.partition_by.as_ref() {
|
||||
write!(f, " PARTITION BY {partition_by}")?;
|
||||
|
@ -392,15 +402,9 @@ impl Display for CreateTable {
|
|||
if let Some(cluster_by) = self.cluster_by.as_ref() {
|
||||
write!(f, " CLUSTER BY {cluster_by}")?;
|
||||
}
|
||||
|
||||
if let Some(options) = self.options.as_ref() {
|
||||
write!(
|
||||
f,
|
||||
" OPTIONS({})",
|
||||
display_comma_separated(options.as_slice())
|
||||
)?;
|
||||
if let options @ CreateTableOptions::Options(_) = &self.table_options {
|
||||
write!(f, " {options}")?;
|
||||
}
|
||||
|
||||
if let Some(external_volume) = self.external_volume.as_ref() {
|
||||
write!(f, " EXTERNAL_VOLUME = '{external_volume}'")?;
|
||||
}
|
||||
|
@ -476,13 +480,6 @@ impl Display for CreateTable {
|
|||
write!(f, " WITH TAG ({})", display_comma_separated(tag.as_slice()))?;
|
||||
}
|
||||
|
||||
if let Some(default_charset) = &self.default_charset {
|
||||
write!(f, " DEFAULT CHARSET={default_charset}")?;
|
||||
}
|
||||
if let Some(collation) = &self.collation {
|
||||
write!(f, " COLLATE={collation}")?;
|
||||
}
|
||||
|
||||
if self.on_commit.is_some() {
|
||||
let on_commit = match self.on_commit {
|
||||
Some(OnCommit::DeleteRows) => "ON COMMIT DELETE ROWS",
|
||||
|
@ -592,27 +589,31 @@ impl Display for Insert {
|
|||
}
|
||||
if !self.columns.is_empty() {
|
||||
write!(f, "({})", display_comma_separated(&self.columns))?;
|
||||
SpaceOrNewline.fmt(f)?;
|
||||
}
|
||||
if let Some(ref parts) = self.partitioned {
|
||||
if !parts.is_empty() {
|
||||
write!(f, "PARTITION ({})", display_comma_separated(parts))?;
|
||||
SpaceOrNewline.fmt(f)?;
|
||||
}
|
||||
}
|
||||
if !self.after_columns.is_empty() {
|
||||
write!(f, "({})", display_comma_separated(&self.after_columns))?;
|
||||
SpaceOrNewline.fmt(f)?;
|
||||
}
|
||||
|
||||
if let Some(settings) = &self.settings {
|
||||
write!(f, "SETTINGS {}", display_comma_separated(settings))?;
|
||||
SpaceOrNewline.fmt(f)?;
|
||||
}
|
||||
|
||||
if let Some(source) = &self.source {
|
||||
write!(f, "{source}")?;
|
||||
source.fmt(f)?;
|
||||
} else if !self.assignments.is_empty() {
|
||||
write!(f, "SET")?;
|
||||
write!(f, "{}", display_comma_separated(&self.assignments))?;
|
||||
indented_list(f, &self.assignments)?;
|
||||
} else if let Some(format_clause) = &self.format_clause {
|
||||
write!(f, "{format_clause}")?;
|
||||
format_clause.fmt(f)?;
|
||||
} else if self.columns.is_empty() {
|
||||
write!(f, "DEFAULT VALUES")?;
|
||||
}
|
||||
|
@ -632,7 +633,9 @@ impl Display for Insert {
|
|||
}
|
||||
|
||||
if let Some(returning) = &self.returning {
|
||||
write!(f, " RETURNING {}", display_comma_separated(returning))?;
|
||||
SpaceOrNewline.fmt(f)?;
|
||||
f.write_str("RETURNING")?;
|
||||
indented_list(f, returning)?;
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
@ -661,32 +664,45 @@ pub struct Delete {
|
|||
|
||||
impl Display for Delete {
|
||||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||
write!(f, "DELETE ")?;
|
||||
f.write_str("DELETE")?;
|
||||
if !self.tables.is_empty() {
|
||||
write!(f, "{} ", display_comma_separated(&self.tables))?;
|
||||
indented_list(f, &self.tables)?;
|
||||
}
|
||||
match &self.from {
|
||||
FromTable::WithFromKeyword(from) => {
|
||||
write!(f, "FROM {}", display_comma_separated(from))?;
|
||||
f.write_str(" FROM")?;
|
||||
indented_list(f, from)?;
|
||||
}
|
||||
FromTable::WithoutKeyword(from) => {
|
||||
write!(f, "{}", display_comma_separated(from))?;
|
||||
indented_list(f, from)?;
|
||||
}
|
||||
}
|
||||
if let Some(using) = &self.using {
|
||||
write!(f, " USING {}", display_comma_separated(using))?;
|
||||
SpaceOrNewline.fmt(f)?;
|
||||
f.write_str("USING")?;
|
||||
indented_list(f, using)?;
|
||||
}
|
||||
if let Some(selection) = &self.selection {
|
||||
write!(f, " WHERE {selection}")?;
|
||||
SpaceOrNewline.fmt(f)?;
|
||||
f.write_str("WHERE")?;
|
||||
SpaceOrNewline.fmt(f)?;
|
||||
Indent(selection).fmt(f)?;
|
||||
}
|
||||
if let Some(returning) = &self.returning {
|
||||
write!(f, " RETURNING {}", display_comma_separated(returning))?;
|
||||
SpaceOrNewline.fmt(f)?;
|
||||
f.write_str("RETURNING")?;
|
||||
indented_list(f, returning)?;
|
||||
}
|
||||
if !self.order_by.is_empty() {
|
||||
write!(f, " ORDER BY {}", display_comma_separated(&self.order_by))?;
|
||||
SpaceOrNewline.fmt(f)?;
|
||||
f.write_str("ORDER BY")?;
|
||||
indented_list(f, &self.order_by)?;
|
||||
}
|
||||
if let Some(limit) = &self.limit {
|
||||
write!(f, " LIMIT {limit}")?;
|
||||
SpaceOrNewline.fmt(f)?;
|
||||
f.write_str("LIMIT")?;
|
||||
SpaceOrNewline.fmt(f)?;
|
||||
Indent(limit).fmt(f)?;
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
|
|
@ -67,7 +67,7 @@ impl fmt::Display for KeyValueOptions {
|
|||
} else {
|
||||
f.write_str(" ")?;
|
||||
}
|
||||
write!(f, "{}", option)?;
|
||||
write!(f, "{option}")?;
|
||||
}
|
||||
}
|
||||
Ok(())
|
||||
|
|
|
@ -26,10 +26,12 @@ use sqlparser_derive::{Visit, VisitMut};
|
|||
|
||||
use super::super::dml::CreateTable;
|
||||
use crate::ast::{
|
||||
ClusteredBy, ColumnDef, CommentDef, Expr, FileFormat, HiveDistributionStyle, HiveFormat, Ident,
|
||||
ObjectName, OnCommit, OneOrManyWithParens, Query, RowAccessPolicy, SqlOption, Statement,
|
||||
StorageSerializationPolicy, TableConstraint, TableEngine, Tag, WrappedCollection,
|
||||
ClusteredBy, ColumnDef, CommentDef, CreateTableOptions, Expr, FileFormat,
|
||||
HiveDistributionStyle, HiveFormat, Ident, ObjectName, OnCommit, OneOrManyWithParens, Query,
|
||||
RowAccessPolicy, Statement, StorageSerializationPolicy, TableConstraint, Tag,
|
||||
WrappedCollection,
|
||||
};
|
||||
|
||||
use crate::parser::ParserError;
|
||||
|
||||
/// Builder for create table statement variant ([1]).
|
||||
|
@ -76,27 +78,21 @@ pub struct CreateTableBuilder {
|
|||
pub constraints: Vec<TableConstraint>,
|
||||
pub hive_distribution: HiveDistributionStyle,
|
||||
pub hive_formats: Option<HiveFormat>,
|
||||
pub table_properties: Vec<SqlOption>,
|
||||
pub with_options: Vec<SqlOption>,
|
||||
pub file_format: Option<FileFormat>,
|
||||
pub location: Option<String>,
|
||||
pub query: Option<Box<Query>>,
|
||||
pub without_rowid: bool,
|
||||
pub like: Option<ObjectName>,
|
||||
pub clone: Option<ObjectName>,
|
||||
pub engine: Option<TableEngine>,
|
||||
pub comment: Option<CommentDef>,
|
||||
pub auto_increment_offset: Option<u32>,
|
||||
pub default_charset: Option<String>,
|
||||
pub collation: Option<String>,
|
||||
pub on_commit: Option<OnCommit>,
|
||||
pub on_cluster: Option<Ident>,
|
||||
pub primary_key: Option<Box<Expr>>,
|
||||
pub order_by: Option<OneOrManyWithParens<Expr>>,
|
||||
pub partition_by: Option<Box<Expr>>,
|
||||
pub cluster_by: Option<WrappedCollection<Vec<Ident>>>,
|
||||
pub cluster_by: Option<WrappedCollection<Vec<Expr>>>,
|
||||
pub clustered_by: Option<ClusteredBy>,
|
||||
pub options: Option<Vec<SqlOption>>,
|
||||
pub inherits: Option<Vec<ObjectName>>,
|
||||
pub strict: bool,
|
||||
pub copy_grants: bool,
|
||||
pub enable_schema_evolution: Option<bool>,
|
||||
|
@ -112,6 +108,7 @@ pub struct CreateTableBuilder {
|
|||
pub catalog: Option<String>,
|
||||
pub catalog_sync: Option<String>,
|
||||
pub storage_serialization_policy: Option<StorageSerializationPolicy>,
|
||||
pub table_options: CreateTableOptions,
|
||||
}
|
||||
|
||||
impl CreateTableBuilder {
|
||||
|
@ -130,19 +127,13 @@ impl CreateTableBuilder {
|
|||
constraints: vec![],
|
||||
hive_distribution: HiveDistributionStyle::NONE,
|
||||
hive_formats: None,
|
||||
table_properties: vec![],
|
||||
with_options: vec![],
|
||||
file_format: None,
|
||||
location: None,
|
||||
query: None,
|
||||
without_rowid: false,
|
||||
like: None,
|
||||
clone: None,
|
||||
engine: None,
|
||||
comment: None,
|
||||
auto_increment_offset: None,
|
||||
default_charset: None,
|
||||
collation: None,
|
||||
on_commit: None,
|
||||
on_cluster: None,
|
||||
primary_key: None,
|
||||
|
@ -150,7 +141,7 @@ impl CreateTableBuilder {
|
|||
partition_by: None,
|
||||
cluster_by: None,
|
||||
clustered_by: None,
|
||||
options: None,
|
||||
inherits: None,
|
||||
strict: false,
|
||||
copy_grants: false,
|
||||
enable_schema_evolution: None,
|
||||
|
@ -166,6 +157,7 @@ impl CreateTableBuilder {
|
|||
catalog: None,
|
||||
catalog_sync: None,
|
||||
storage_serialization_policy: None,
|
||||
table_options: CreateTableOptions::None,
|
||||
}
|
||||
}
|
||||
pub fn or_replace(mut self, or_replace: bool) -> Self {
|
||||
|
@ -228,15 +220,6 @@ impl CreateTableBuilder {
|
|||
self
|
||||
}
|
||||
|
||||
pub fn table_properties(mut self, table_properties: Vec<SqlOption>) -> Self {
|
||||
self.table_properties = table_properties;
|
||||
self
|
||||
}
|
||||
|
||||
pub fn with_options(mut self, with_options: Vec<SqlOption>) -> Self {
|
||||
self.with_options = with_options;
|
||||
self
|
||||
}
|
||||
pub fn file_format(mut self, file_format: Option<FileFormat>) -> Self {
|
||||
self.file_format = file_format;
|
||||
self
|
||||
|
@ -266,31 +249,11 @@ impl CreateTableBuilder {
|
|||
self
|
||||
}
|
||||
|
||||
pub fn engine(mut self, engine: Option<TableEngine>) -> Self {
|
||||
self.engine = engine;
|
||||
self
|
||||
}
|
||||
|
||||
pub fn comment(mut self, comment: Option<CommentDef>) -> Self {
|
||||
pub fn comment_after_column_def(mut self, comment: Option<CommentDef>) -> Self {
|
||||
self.comment = comment;
|
||||
self
|
||||
}
|
||||
|
||||
pub fn auto_increment_offset(mut self, offset: Option<u32>) -> Self {
|
||||
self.auto_increment_offset = offset;
|
||||
self
|
||||
}
|
||||
|
||||
pub fn default_charset(mut self, default_charset: Option<String>) -> Self {
|
||||
self.default_charset = default_charset;
|
||||
self
|
||||
}
|
||||
|
||||
pub fn collation(mut self, collation: Option<String>) -> Self {
|
||||
self.collation = collation;
|
||||
self
|
||||
}
|
||||
|
||||
pub fn on_commit(mut self, on_commit: Option<OnCommit>) -> Self {
|
||||
self.on_commit = on_commit;
|
||||
self
|
||||
|
@ -316,7 +279,7 @@ impl CreateTableBuilder {
|
|||
self
|
||||
}
|
||||
|
||||
pub fn cluster_by(mut self, cluster_by: Option<WrappedCollection<Vec<Ident>>>) -> Self {
|
||||
pub fn cluster_by(mut self, cluster_by: Option<WrappedCollection<Vec<Expr>>>) -> Self {
|
||||
self.cluster_by = cluster_by;
|
||||
self
|
||||
}
|
||||
|
@ -326,8 +289,8 @@ impl CreateTableBuilder {
|
|||
self
|
||||
}
|
||||
|
||||
pub fn options(mut self, options: Option<Vec<SqlOption>>) -> Self {
|
||||
self.options = options;
|
||||
pub fn inherits(mut self, inherits: Option<Vec<ObjectName>>) -> Self {
|
||||
self.inherits = inherits;
|
||||
self
|
||||
}
|
||||
|
||||
|
@ -415,6 +378,11 @@ impl CreateTableBuilder {
|
|||
self
|
||||
}
|
||||
|
||||
pub fn table_options(mut self, table_options: CreateTableOptions) -> Self {
|
||||
self.table_options = table_options;
|
||||
self
|
||||
}
|
||||
|
||||
pub fn build(self) -> Statement {
|
||||
Statement::CreateTable(CreateTable {
|
||||
or_replace: self.or_replace,
|
||||
|
@ -430,19 +398,13 @@ impl CreateTableBuilder {
|
|||
constraints: self.constraints,
|
||||
hive_distribution: self.hive_distribution,
|
||||
hive_formats: self.hive_formats,
|
||||
table_properties: self.table_properties,
|
||||
with_options: self.with_options,
|
||||
file_format: self.file_format,
|
||||
location: self.location,
|
||||
query: self.query,
|
||||
without_rowid: self.without_rowid,
|
||||
like: self.like,
|
||||
clone: self.clone,
|
||||
engine: self.engine,
|
||||
comment: self.comment,
|
||||
auto_increment_offset: self.auto_increment_offset,
|
||||
default_charset: self.default_charset,
|
||||
collation: self.collation,
|
||||
on_commit: self.on_commit,
|
||||
on_cluster: self.on_cluster,
|
||||
primary_key: self.primary_key,
|
||||
|
@ -450,7 +412,7 @@ impl CreateTableBuilder {
|
|||
partition_by: self.partition_by,
|
||||
cluster_by: self.cluster_by,
|
||||
clustered_by: self.clustered_by,
|
||||
options: self.options,
|
||||
inherits: self.inherits,
|
||||
strict: self.strict,
|
||||
copy_grants: self.copy_grants,
|
||||
enable_schema_evolution: self.enable_schema_evolution,
|
||||
|
@ -466,6 +428,7 @@ impl CreateTableBuilder {
|
|||
catalog: self.catalog,
|
||||
catalog_sync: self.catalog_sync,
|
||||
storage_serialization_policy: self.storage_serialization_policy,
|
||||
table_options: self.table_options,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
@ -491,19 +454,13 @@ impl TryFrom<Statement> for CreateTableBuilder {
|
|||
constraints,
|
||||
hive_distribution,
|
||||
hive_formats,
|
||||
table_properties,
|
||||
with_options,
|
||||
file_format,
|
||||
location,
|
||||
query,
|
||||
without_rowid,
|
||||
like,
|
||||
clone,
|
||||
engine,
|
||||
comment,
|
||||
auto_increment_offset,
|
||||
default_charset,
|
||||
collation,
|
||||
on_commit,
|
||||
on_cluster,
|
||||
primary_key,
|
||||
|
@ -511,7 +468,7 @@ impl TryFrom<Statement> for CreateTableBuilder {
|
|||
partition_by,
|
||||
cluster_by,
|
||||
clustered_by,
|
||||
options,
|
||||
inherits,
|
||||
strict,
|
||||
copy_grants,
|
||||
enable_schema_evolution,
|
||||
|
@ -527,6 +484,7 @@ impl TryFrom<Statement> for CreateTableBuilder {
|
|||
catalog,
|
||||
catalog_sync,
|
||||
storage_serialization_policy,
|
||||
table_options,
|
||||
}) => Ok(Self {
|
||||
or_replace,
|
||||
temporary,
|
||||
|
@ -539,19 +497,13 @@ impl TryFrom<Statement> for CreateTableBuilder {
|
|||
constraints,
|
||||
hive_distribution,
|
||||
hive_formats,
|
||||
table_properties,
|
||||
with_options,
|
||||
file_format,
|
||||
location,
|
||||
query,
|
||||
without_rowid,
|
||||
like,
|
||||
clone,
|
||||
engine,
|
||||
comment,
|
||||
auto_increment_offset,
|
||||
default_charset,
|
||||
collation,
|
||||
on_commit,
|
||||
on_cluster,
|
||||
primary_key,
|
||||
|
@ -559,7 +511,7 @@ impl TryFrom<Statement> for CreateTableBuilder {
|
|||
partition_by,
|
||||
cluster_by,
|
||||
clustered_by,
|
||||
options,
|
||||
inherits,
|
||||
strict,
|
||||
iceberg,
|
||||
copy_grants,
|
||||
|
@ -577,6 +529,7 @@ impl TryFrom<Statement> for CreateTableBuilder {
|
|||
catalog,
|
||||
catalog_sync,
|
||||
storage_serialization_policy,
|
||||
table_options,
|
||||
}),
|
||||
_ => Err(ParserError::ParserError(format!(
|
||||
"Expected create table statement, but received: {stmt}"
|
||||
|
@ -589,8 +542,9 @@ impl TryFrom<Statement> for CreateTableBuilder {
|
|||
#[derive(Default)]
|
||||
pub(crate) struct CreateTableConfiguration {
|
||||
pub partition_by: Option<Box<Expr>>,
|
||||
pub cluster_by: Option<WrappedCollection<Vec<Ident>>>,
|
||||
pub options: Option<Vec<SqlOption>>,
|
||||
pub cluster_by: Option<WrappedCollection<Vec<Expr>>>,
|
||||
pub inherits: Option<Vec<ObjectName>>,
|
||||
pub table_options: CreateTableOptions,
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
|
|
|
@ -21,15 +21,13 @@
|
|||
|
||||
#[cfg(not(feature = "std"))]
|
||||
use alloc::string::String;
|
||||
#[cfg(not(feature = "std"))]
|
||||
use alloc::vec::Vec;
|
||||
use core::fmt;
|
||||
|
||||
#[cfg(feature = "serde")]
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
use crate::ast::helpers::key_value_options::KeyValueOptions;
|
||||
use crate::ast::{Ident, ObjectName};
|
||||
use crate::ast::{Ident, ObjectName, SelectItem};
|
||||
#[cfg(feature = "visitor")]
|
||||
use sqlparser_derive::{Visit, VisitMut};
|
||||
|
||||
|
@ -44,6 +42,25 @@ pub struct StageParamsObject {
|
|||
pub credentials: KeyValueOptions,
|
||||
}
|
||||
|
||||
/// This enum enables support for both standard SQL select item expressions
|
||||
/// and Snowflake-specific ones for data loading.
|
||||
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
|
||||
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
|
||||
#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
|
||||
pub enum StageLoadSelectItemKind {
|
||||
SelectItem(SelectItem),
|
||||
StageLoadSelectItem(StageLoadSelectItem),
|
||||
}
|
||||
|
||||
impl fmt::Display for StageLoadSelectItemKind {
|
||||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
||||
match &self {
|
||||
StageLoadSelectItemKind::SelectItem(item) => write!(f, "{item}"),
|
||||
StageLoadSelectItemKind::StageLoadSelectItem(item) => write!(f, "{item}"),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
|
||||
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
|
||||
#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
|
||||
|
|
2032
src/ast/mod.rs
2032
src/ast/mod.rs
File diff suppressed because it is too large
Load diff
|
@ -139,6 +139,11 @@ pub enum BinaryOperator {
|
|||
DuckIntegerDivide,
|
||||
/// MySQL [`DIV`](https://dev.mysql.com/doc/refman/8.0/en/arithmetic-functions.html) integer division
|
||||
MyIntegerDivide,
|
||||
/// MATCH operator, e.g. `a MATCH b` (SQLite-specific)
|
||||
/// See <https://www.sqlite.org/lang_expr.html#the_like_glob_regexp_match_and_extract_operators>
|
||||
Match,
|
||||
/// REGEXP operator, e.g. `a REGEXP b` (SQLite-specific)
|
||||
Regexp,
|
||||
/// Support for custom operators (such as Postgres custom operators)
|
||||
Custom(String),
|
||||
/// Bitwise XOR, e.g. `a # b` (PostgreSQL-specific)
|
||||
|
@ -321,6 +326,9 @@ pub enum BinaryOperator {
|
|||
/// `~=` Same as? (PostgreSQL/Redshift geometric operator)
|
||||
/// See <https://www.postgresql.org/docs/9.5/functions-geometry.html>
|
||||
TildeEq,
|
||||
/// ':=' Assignment Operator
|
||||
/// See <https://dev.mysql.com/doc/refman/8.4/en/assignment-operators.html#operator_assign-value>
|
||||
Assignment,
|
||||
}
|
||||
|
||||
impl fmt::Display for BinaryOperator {
|
||||
|
@ -347,6 +355,8 @@ impl fmt::Display for BinaryOperator {
|
|||
BinaryOperator::BitwiseXor => f.write_str("^"),
|
||||
BinaryOperator::DuckIntegerDivide => f.write_str("//"),
|
||||
BinaryOperator::MyIntegerDivide => f.write_str("DIV"),
|
||||
BinaryOperator::Match => f.write_str("MATCH"),
|
||||
BinaryOperator::Regexp => f.write_str("REGEXP"),
|
||||
BinaryOperator::Custom(s) => f.write_str(s),
|
||||
BinaryOperator::PGBitwiseXor => f.write_str("#"),
|
||||
BinaryOperator::PGBitwiseShiftLeft => f.write_str("<<"),
|
||||
|
@ -394,6 +404,7 @@ impl fmt::Display for BinaryOperator {
|
|||
BinaryOperator::QuestionDoublePipe => f.write_str("?||"),
|
||||
BinaryOperator::At => f.write_str("@"),
|
||||
BinaryOperator::TildeEq => f.write_str("~="),
|
||||
BinaryOperator::Assignment => f.write_str(":="),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
969
src/ast/query.rs
969
src/ast/query.rs
File diff suppressed because it is too large
Load diff
307
src/ast/spans.rs
307
src/ast/spans.rs
|
@ -15,27 +15,30 @@
|
|||
// specific language governing permissions and limitations
|
||||
// under the License.
|
||||
|
||||
use crate::ast::query::SelectItemQualifiedWildcardKind;
|
||||
use crate::ast::{query::SelectItemQualifiedWildcardKind, ColumnOptions};
|
||||
use core::iter;
|
||||
|
||||
use crate::tokenizer::Span;
|
||||
|
||||
use super::{
|
||||
dcl::SecondaryRoles, value::ValueWithSpan, AccessExpr, AlterColumnOperation,
|
||||
AlterIndexOperation, AlterTableOperation, Array, Assignment, AssignmentTarget, CloseCursor,
|
||||
ClusteredIndex, ColumnDef, ColumnOption, ColumnOptionDef, ConflictTarget, ConnectBy,
|
||||
AlterIndexOperation, AlterTableOperation, Array, Assignment, AssignmentTarget, AttachedToken,
|
||||
BeginEndStatements, CaseStatement, CloseCursor, ClusteredIndex, ColumnDef, ColumnOption,
|
||||
ColumnOptionDef, ConditionalStatementBlock, ConditionalStatements, ConflictTarget, ConnectBy,
|
||||
ConstraintCharacteristics, CopySource, CreateIndex, CreateTable, CreateTableOptions, Cte,
|
||||
Delete, DoUpdate, ExceptSelectItem, ExcludeSelectItem, Expr, ExprWithAlias, Fetch, FromTable,
|
||||
Function, FunctionArg, FunctionArgExpr, FunctionArgumentClause, FunctionArgumentList,
|
||||
FunctionArguments, GroupByExpr, HavingBound, IlikeSelectItem, Insert, Interpolate,
|
||||
InterpolateExpr, Join, JoinConstraint, JoinOperator, JsonPath, JsonPathElem, LateralView,
|
||||
MatchRecognizePattern, Measure, NamedWindowDefinition, ObjectName, ObjectNamePart, Offset,
|
||||
OnConflict, OnConflictAction, OnInsert, OrderBy, OrderByExpr, OrderByKind, Partition,
|
||||
PivotValueSource, ProjectionSelect, Query, ReferentialAction, RenameSelectItem,
|
||||
ReplaceSelectElement, ReplaceSelectItem, Select, SelectInto, SelectItem, SetExpr, SqlOption,
|
||||
Statement, Subscript, SymbolDefinition, TableAlias, TableAliasColumnDef, TableConstraint,
|
||||
TableFactor, TableObject, TableOptionsClustered, TableWithJoins, UpdateTableFromKind, Use,
|
||||
Value, Values, ViewColumnDef, WildcardAdditionalOptions, With, WithFill,
|
||||
FunctionArguments, GroupByExpr, HavingBound, IfStatement, IlikeSelectItem, IndexColumn, Insert,
|
||||
Interpolate, InterpolateExpr, Join, JoinConstraint, JoinOperator, JsonPath, JsonPathElem,
|
||||
LateralView, LimitClause, MatchRecognizePattern, Measure, NamedParenthesizedList,
|
||||
NamedWindowDefinition, ObjectName, ObjectNamePart, Offset, OnConflict, OnConflictAction,
|
||||
OnInsert, OpenStatement, OrderBy, OrderByExpr, OrderByKind, Partition, PivotValueSource,
|
||||
ProjectionSelect, Query, RaiseStatement, RaiseStatementValue, ReferentialAction,
|
||||
RenameSelectItem, ReplaceSelectElement, ReplaceSelectItem, Select, SelectInto, SelectItem,
|
||||
SetExpr, SqlOption, Statement, Subscript, SymbolDefinition, TableAlias, TableAliasColumnDef,
|
||||
TableConstraint, TableFactor, TableObject, TableOptionsClustered, TableWithJoins,
|
||||
UpdateTableFromKind, Use, Value, Values, ViewColumnDef, WhileStatement,
|
||||
WildcardAdditionalOptions, With, WithFill,
|
||||
};
|
||||
|
||||
/// Given an iterator of spans, return the [Span::union] of all spans.
|
||||
|
@ -94,14 +97,13 @@ impl Spanned for Query {
|
|||
with,
|
||||
body,
|
||||
order_by,
|
||||
limit,
|
||||
limit_by,
|
||||
offset,
|
||||
limit_clause,
|
||||
fetch,
|
||||
locks: _, // todo
|
||||
for_clause: _, // todo, mssql specific
|
||||
settings: _, // todo, clickhouse specific
|
||||
format_clause: _, // todo, clickhouse specific
|
||||
pipe_operators: _, // todo bigquery specific
|
||||
} = self;
|
||||
|
||||
union_spans(
|
||||
|
@ -109,14 +111,31 @@ impl Spanned for Query {
|
|||
.map(|i| i.span())
|
||||
.chain(core::iter::once(body.span()))
|
||||
.chain(order_by.as_ref().map(|i| i.span()))
|
||||
.chain(limit.as_ref().map(|i| i.span()))
|
||||
.chain(limit_by.iter().map(|i| i.span()))
|
||||
.chain(offset.as_ref().map(|i| i.span()))
|
||||
.chain(limit_clause.as_ref().map(|i| i.span()))
|
||||
.chain(fetch.as_ref().map(|i| i.span())),
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
impl Spanned for LimitClause {
|
||||
fn span(&self) -> Span {
|
||||
match self {
|
||||
LimitClause::LimitOffset {
|
||||
limit,
|
||||
offset,
|
||||
limit_by,
|
||||
} => union_spans(
|
||||
limit
|
||||
.iter()
|
||||
.map(|i| i.span())
|
||||
.chain(offset.as_ref().map(|i| i.span()))
|
||||
.chain(limit_by.iter().map(|i| i.span())),
|
||||
),
|
||||
LimitClause::OffsetCommaLimit { offset, limit } => offset.span().union(&limit.span()),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Spanned for Offset {
|
||||
fn span(&self) -> Span {
|
||||
let Offset {
|
||||
|
@ -191,6 +210,7 @@ impl Spanned for SetExpr {
|
|||
SetExpr::Insert(statement) => statement.span(),
|
||||
SetExpr::Table(_) => Span::empty(),
|
||||
SetExpr::Update(statement) => statement.span(),
|
||||
SetExpr::Delete(statement) => statement.span(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -229,11 +249,7 @@ impl Spanned for Values {
|
|||
/// - [Statement::Fetch]
|
||||
/// - [Statement::Flush]
|
||||
/// - [Statement::Discard]
|
||||
/// - [Statement::SetRole]
|
||||
/// - [Statement::SetVariable]
|
||||
/// - [Statement::SetTimeZone]
|
||||
/// - [Statement::SetNames]
|
||||
/// - [Statement::SetNamesDefault]
|
||||
/// - [Statement::Set]
|
||||
/// - [Statement::ShowFunctions]
|
||||
/// - [Statement::ShowVariable]
|
||||
/// - [Statement::ShowStatus]
|
||||
|
@ -243,7 +259,6 @@ impl Spanned for Values {
|
|||
/// - [Statement::ShowTables]
|
||||
/// - [Statement::ShowCollation]
|
||||
/// - [Statement::StartTransaction]
|
||||
/// - [Statement::SetTransaction]
|
||||
/// - [Statement::Comment]
|
||||
/// - [Statement::Commit]
|
||||
/// - [Statement::Rollback]
|
||||
|
@ -297,7 +312,6 @@ impl Spanned for Statement {
|
|||
table_names,
|
||||
partitions,
|
||||
table: _,
|
||||
only: _,
|
||||
identity: _,
|
||||
cascade: _,
|
||||
on_cluster: _,
|
||||
|
@ -323,6 +337,10 @@ impl Spanned for Statement {
|
|||
file_format: _,
|
||||
source,
|
||||
} => source.span(),
|
||||
Statement::Case(stmt) => stmt.span(),
|
||||
Statement::If(stmt) => stmt.span(),
|
||||
Statement::While(stmt) => stmt.span(),
|
||||
Statement::Raise(stmt) => stmt.span(),
|
||||
Statement::Call(function) => function.span(),
|
||||
Statement::Copy {
|
||||
source,
|
||||
|
@ -334,6 +352,7 @@ impl Spanned for Statement {
|
|||
} => source.span(),
|
||||
Statement::CopyIntoSnowflake {
|
||||
into: _,
|
||||
into_columns: _,
|
||||
from_obj: _,
|
||||
from_obj_alias: _,
|
||||
stage_params: _,
|
||||
|
@ -347,6 +366,7 @@ impl Spanned for Statement {
|
|||
from_query: _,
|
||||
partition: _,
|
||||
} => Span::empty(),
|
||||
Statement::Open(open) => open.span(),
|
||||
Statement::Close { cursor } => match cursor {
|
||||
CloseCursor::All => Span::empty(),
|
||||
CloseCursor::Specific { name } => name.span,
|
||||
|
@ -367,6 +387,7 @@ impl Spanned for Statement {
|
|||
),
|
||||
Statement::Delete(delete) => delete.span(),
|
||||
Statement::CreateView {
|
||||
or_alter: _,
|
||||
or_replace: _,
|
||||
materialized: _,
|
||||
name,
|
||||
|
@ -402,6 +423,7 @@ impl Spanned for Statement {
|
|||
Statement::CreateIndex(create_index) => create_index.span(),
|
||||
Statement::CreateRole { .. } => Span::empty(),
|
||||
Statement::CreateSecret { .. } => Span::empty(),
|
||||
Statement::CreateServer { .. } => Span::empty(),
|
||||
Statement::CreateConnector { .. } => Span::empty(),
|
||||
Statement::AlterTable {
|
||||
name,
|
||||
|
@ -410,6 +432,7 @@ impl Spanned for Statement {
|
|||
operations,
|
||||
location: _,
|
||||
on_cluster,
|
||||
iceberg: _,
|
||||
} => union_spans(
|
||||
core::iter::once(name.span())
|
||||
.chain(operations.iter().map(|i| i.span()))
|
||||
|
@ -436,6 +459,7 @@ impl Spanned for Statement {
|
|||
Statement::DetachDuckDBDatabase { .. } => Span::empty(),
|
||||
Statement::Drop { .. } => Span::empty(),
|
||||
Statement::DropFunction { .. } => Span::empty(),
|
||||
Statement::DropDomain { .. } => Span::empty(),
|
||||
Statement::DropProcedure { .. } => Span::empty(),
|
||||
Statement::DropSecret { .. } => Span::empty(),
|
||||
Statement::Declare { .. } => Span::empty(),
|
||||
|
@ -444,11 +468,7 @@ impl Spanned for Statement {
|
|||
Statement::Fetch { .. } => Span::empty(),
|
||||
Statement::Flush { .. } => Span::empty(),
|
||||
Statement::Discard { .. } => Span::empty(),
|
||||
Statement::SetRole { .. } => Span::empty(),
|
||||
Statement::SetVariable { .. } => Span::empty(),
|
||||
Statement::SetTimeZone { .. } => Span::empty(),
|
||||
Statement::SetNames { .. } => Span::empty(),
|
||||
Statement::SetNamesDefault {} => Span::empty(),
|
||||
Statement::Set(_) => Span::empty(),
|
||||
Statement::ShowFunctions { .. } => Span::empty(),
|
||||
Statement::ShowVariable { .. } => Span::empty(),
|
||||
Statement::ShowStatus { .. } => Span::empty(),
|
||||
|
@ -459,13 +479,13 @@ impl Spanned for Statement {
|
|||
Statement::ShowCollation { .. } => Span::empty(),
|
||||
Statement::Use(u) => u.span(),
|
||||
Statement::StartTransaction { .. } => Span::empty(),
|
||||
Statement::SetTransaction { .. } => Span::empty(),
|
||||
Statement::Comment { .. } => Span::empty(),
|
||||
Statement::Commit { .. } => Span::empty(),
|
||||
Statement::Rollback { .. } => Span::empty(),
|
||||
Statement::CreateSchema { .. } => Span::empty(),
|
||||
Statement::CreateDatabase { .. } => Span::empty(),
|
||||
Statement::CreateFunction { .. } => Span::empty(),
|
||||
Statement::CreateDomain { .. } => Span::empty(),
|
||||
Statement::CreateTrigger { .. } => Span::empty(),
|
||||
Statement::DropTrigger { .. } => Span::empty(),
|
||||
Statement::CreateProcedure { .. } => Span::empty(),
|
||||
|
@ -473,6 +493,7 @@ impl Spanned for Statement {
|
|||
Statement::CreateStage { .. } => Span::empty(),
|
||||
Statement::Assert { .. } => Span::empty(),
|
||||
Statement::Grant { .. } => Span::empty(),
|
||||
Statement::Deny { .. } => Span::empty(),
|
||||
Statement::Revoke { .. } => Span::empty(),
|
||||
Statement::Deallocate { .. } => Span::empty(),
|
||||
Statement::Execute { .. } => Span::empty(),
|
||||
|
@ -507,8 +528,9 @@ impl Spanned for Statement {
|
|||
Statement::UNLISTEN { .. } => Span::empty(),
|
||||
Statement::RenameTable { .. } => Span::empty(),
|
||||
Statement::RaisError { .. } => Span::empty(),
|
||||
Statement::Print { .. } => Span::empty(),
|
||||
Statement::Return { .. } => Span::empty(),
|
||||
Statement::List(..) | Statement::Remove(..) => Span::empty(),
|
||||
Statement::SetSessionParam { .. } => Span::empty(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -549,27 +571,21 @@ impl Spanned for CreateTable {
|
|||
constraints,
|
||||
hive_distribution: _, // hive specific
|
||||
hive_formats: _, // hive specific
|
||||
table_properties,
|
||||
with_options,
|
||||
file_format: _, // enum
|
||||
location: _, // string, no span
|
||||
query,
|
||||
without_rowid: _, // bool
|
||||
like,
|
||||
clone,
|
||||
engine: _, // todo
|
||||
comment: _, // todo, no span
|
||||
auto_increment_offset: _, // u32, no span
|
||||
default_charset: _, // string, no span
|
||||
collation: _, // string, no span
|
||||
on_commit: _, // enum
|
||||
on_commit: _,
|
||||
on_cluster: _, // todo, clickhouse specific
|
||||
primary_key: _, // todo, clickhouse specific
|
||||
order_by: _, // todo, clickhouse specific
|
||||
partition_by: _, // todo, BigQuery specific
|
||||
cluster_by: _, // todo, BigQuery specific
|
||||
clustered_by: _, // todo, Hive specific
|
||||
options: _, // todo, BigQuery specific
|
||||
inherits: _, // todo, PostgreSQL specific
|
||||
strict: _, // bool
|
||||
copy_grants: _, // bool
|
||||
enable_schema_evolution: _, // bool
|
||||
|
@ -584,15 +600,15 @@ impl Spanned for CreateTable {
|
|||
base_location: _, // todo, Snowflake specific
|
||||
catalog: _, // todo, Snowflake specific
|
||||
catalog_sync: _, // todo, Snowflake specific
|
||||
storage_serialization_policy: _, // todo, Snowflake specific
|
||||
storage_serialization_policy: _,
|
||||
table_options,
|
||||
} = self;
|
||||
|
||||
union_spans(
|
||||
core::iter::once(name.span())
|
||||
.chain(core::iter::once(table_options.span()))
|
||||
.chain(columns.iter().map(|i| i.span()))
|
||||
.chain(constraints.iter().map(|i| i.span()))
|
||||
.chain(table_properties.iter().map(|i| i.span()))
|
||||
.chain(with_options.iter().map(|i| i.span()))
|
||||
.chain(query.iter().map(|i| i.span()))
|
||||
.chain(like.iter().map(|i| i.span()))
|
||||
.chain(clone.iter().map(|i| i.span())),
|
||||
|
@ -636,7 +652,7 @@ impl Spanned for TableConstraint {
|
|||
name.iter()
|
||||
.map(|i| i.span)
|
||||
.chain(index_name.iter().map(|i| i.span))
|
||||
.chain(columns.iter().map(|i| i.span))
|
||||
.chain(columns.iter().map(|i| i.span()))
|
||||
.chain(characteristics.iter().map(|i| i.span())),
|
||||
),
|
||||
TableConstraint::PrimaryKey {
|
||||
|
@ -650,12 +666,13 @@ impl Spanned for TableConstraint {
|
|||
name.iter()
|
||||
.map(|i| i.span)
|
||||
.chain(index_name.iter().map(|i| i.span))
|
||||
.chain(columns.iter().map(|i| i.span))
|
||||
.chain(columns.iter().map(|i| i.span()))
|
||||
.chain(characteristics.iter().map(|i| i.span())),
|
||||
),
|
||||
TableConstraint::ForeignKey {
|
||||
name,
|
||||
columns,
|
||||
index_name,
|
||||
foreign_table,
|
||||
referred_columns,
|
||||
on_delete,
|
||||
|
@ -664,6 +681,7 @@ impl Spanned for TableConstraint {
|
|||
} => union_spans(
|
||||
name.iter()
|
||||
.map(|i| i.span)
|
||||
.chain(index_name.iter().map(|i| i.span))
|
||||
.chain(columns.iter().map(|i| i.span))
|
||||
.chain(core::iter::once(foreign_table.span()))
|
||||
.chain(referred_columns.iter().map(|i| i.span))
|
||||
|
@ -671,9 +689,11 @@ impl Spanned for TableConstraint {
|
|||
.chain(on_update.iter().map(|i| i.span()))
|
||||
.chain(characteristics.iter().map(|i| i.span())),
|
||||
),
|
||||
TableConstraint::Check { name, expr } => {
|
||||
expr.span().union_opt(&name.as_ref().map(|i| i.span))
|
||||
}
|
||||
TableConstraint::Check {
|
||||
name,
|
||||
expr,
|
||||
enforced: _,
|
||||
} => expr.span().union_opt(&name.as_ref().map(|i| i.span)),
|
||||
TableConstraint::Index {
|
||||
display_as_key: _,
|
||||
name,
|
||||
|
@ -682,7 +702,7 @@ impl Spanned for TableConstraint {
|
|||
} => union_spans(
|
||||
name.iter()
|
||||
.map(|i| i.span)
|
||||
.chain(columns.iter().map(|i| i.span)),
|
||||
.chain(columns.iter().map(|i| i.span())),
|
||||
),
|
||||
TableConstraint::FulltextOrSpatial {
|
||||
fulltext: _,
|
||||
|
@ -693,7 +713,7 @@ impl Spanned for TableConstraint {
|
|||
opt_index_name
|
||||
.iter()
|
||||
.map(|i| i.span)
|
||||
.chain(columns.iter().map(|i| i.span)),
|
||||
.chain(columns.iter().map(|i| i.span())),
|
||||
),
|
||||
}
|
||||
}
|
||||
|
@ -704,7 +724,7 @@ impl Spanned for CreateIndex {
|
|||
let CreateIndex {
|
||||
name,
|
||||
table_name,
|
||||
using,
|
||||
using: _,
|
||||
columns,
|
||||
unique: _, // bool
|
||||
concurrently: _, // bool
|
||||
|
@ -719,8 +739,7 @@ impl Spanned for CreateIndex {
|
|||
name.iter()
|
||||
.map(|i| i.span())
|
||||
.chain(core::iter::once(table_name.span()))
|
||||
.chain(using.iter().map(|i| i.span))
|
||||
.chain(columns.iter().map(|i| i.span()))
|
||||
.chain(columns.iter().map(|i| i.column.span()))
|
||||
.chain(include.iter().map(|i| i.span))
|
||||
.chain(with.iter().map(|i| i.span()))
|
||||
.chain(predicate.iter().map(|i| i.span())),
|
||||
|
@ -728,6 +747,98 @@ impl Spanned for CreateIndex {
|
|||
}
|
||||
}
|
||||
|
||||
impl Spanned for IndexColumn {
|
||||
fn span(&self) -> Span {
|
||||
self.column.span()
|
||||
}
|
||||
}
|
||||
|
||||
impl Spanned for CaseStatement {
|
||||
fn span(&self) -> Span {
|
||||
let CaseStatement {
|
||||
case_token: AttachedToken(start),
|
||||
match_expr: _,
|
||||
when_blocks: _,
|
||||
else_block: _,
|
||||
end_case_token: AttachedToken(end),
|
||||
} = self;
|
||||
|
||||
union_spans([start.span, end.span].into_iter())
|
||||
}
|
||||
}
|
||||
|
||||
impl Spanned for IfStatement {
|
||||
fn span(&self) -> Span {
|
||||
let IfStatement {
|
||||
if_block,
|
||||
elseif_blocks,
|
||||
else_block,
|
||||
end_token,
|
||||
} = self;
|
||||
|
||||
union_spans(
|
||||
iter::once(if_block.span())
|
||||
.chain(elseif_blocks.iter().map(|b| b.span()))
|
||||
.chain(else_block.as_ref().map(|b| b.span()))
|
||||
.chain(end_token.as_ref().map(|AttachedToken(t)| t.span)),
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
impl Spanned for WhileStatement {
|
||||
fn span(&self) -> Span {
|
||||
let WhileStatement { while_block } = self;
|
||||
|
||||
while_block.span()
|
||||
}
|
||||
}
|
||||
|
||||
impl Spanned for ConditionalStatements {
|
||||
fn span(&self) -> Span {
|
||||
match self {
|
||||
ConditionalStatements::Sequence { statements } => {
|
||||
union_spans(statements.iter().map(|s| s.span()))
|
||||
}
|
||||
ConditionalStatements::BeginEnd(bes) => bes.span(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Spanned for ConditionalStatementBlock {
|
||||
fn span(&self) -> Span {
|
||||
let ConditionalStatementBlock {
|
||||
start_token: AttachedToken(start_token),
|
||||
condition,
|
||||
then_token,
|
||||
conditional_statements,
|
||||
} = self;
|
||||
|
||||
union_spans(
|
||||
iter::once(start_token.span)
|
||||
.chain(condition.as_ref().map(|c| c.span()))
|
||||
.chain(then_token.as_ref().map(|AttachedToken(t)| t.span))
|
||||
.chain(iter::once(conditional_statements.span())),
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
impl Spanned for RaiseStatement {
|
||||
fn span(&self) -> Span {
|
||||
let RaiseStatement { value } = self;
|
||||
|
||||
union_spans(value.iter().map(|value| value.span()))
|
||||
}
|
||||
}
|
||||
|
||||
impl Spanned for RaiseStatementValue {
|
||||
fn span(&self) -> Span {
|
||||
match self {
|
||||
RaiseStatementValue::UsingMessage(expr) => expr.span(),
|
||||
RaiseStatementValue::Expr(expr) => expr.span(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// # partial span
|
||||
///
|
||||
/// Missing spans:
|
||||
|
@ -772,6 +883,7 @@ impl Spanned for ColumnOption {
|
|||
ColumnOption::OnConflict(..) => Span::empty(),
|
||||
ColumnOption::Policy(..) => Span::empty(),
|
||||
ColumnOption::Tags(..) => Span::empty(),
|
||||
ColumnOption::Srid(..) => Span::empty(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -813,6 +925,7 @@ impl Spanned for AlterColumnOperation {
|
|||
AlterColumnOperation::SetDataType {
|
||||
data_type: _,
|
||||
using,
|
||||
had_set: _,
|
||||
} => using.as_ref().map_or(Span::empty(), |u| u.span()),
|
||||
AlterColumnOperation::AddGenerated { .. } => Span::empty(),
|
||||
}
|
||||
|
@ -880,10 +993,13 @@ impl Spanned for ViewColumnDef {
|
|||
options,
|
||||
} = self;
|
||||
|
||||
union_spans(
|
||||
core::iter::once(name.span)
|
||||
.chain(options.iter().flat_map(|i| i.iter().map(|k| k.span()))),
|
||||
)
|
||||
name.span.union_opt(&options.as_ref().map(|o| o.span()))
|
||||
}
|
||||
}
|
||||
|
||||
impl Spanned for ColumnOptions {
|
||||
fn span(&self) -> Span {
|
||||
union_spans(self.as_slice().iter().map(|i| i.span()))
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -900,6 +1016,14 @@ impl Spanned for SqlOption {
|
|||
} => union_spans(
|
||||
core::iter::once(column_name.span).chain(for_values.iter().map(|i| i.span())),
|
||||
),
|
||||
SqlOption::TableSpace(_) => Span::empty(),
|
||||
SqlOption::Comment(_) => Span::empty(),
|
||||
SqlOption::NamedParenthesizedList(NamedParenthesizedList {
|
||||
key: name,
|
||||
name: value,
|
||||
values,
|
||||
}) => union_spans(core::iter::once(name.span).chain(values.iter().map(|i| i.span)))
|
||||
.union_opt(&value.as_ref().map(|i| i.span)),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -936,7 +1060,11 @@ impl Spanned for CreateTableOptions {
|
|||
match self {
|
||||
CreateTableOptions::None => Span::empty(),
|
||||
CreateTableOptions::With(vec) => union_spans(vec.iter().map(|i| i.span())),
|
||||
CreateTableOptions::Options(vec) => union_spans(vec.iter().map(|i| i.span())),
|
||||
CreateTableOptions::Options(vec) => {
|
||||
union_spans(vec.as_slice().iter().map(|i| i.span()))
|
||||
}
|
||||
CreateTableOptions::Plain(vec) => union_spans(vec.iter().map(|i| i.span())),
|
||||
CreateTableOptions::TableProperties(vec) => union_spans(vec.iter().map(|i| i.span())),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -948,7 +1076,10 @@ impl Spanned for CreateTableOptions {
|
|||
impl Spanned for AlterTableOperation {
|
||||
fn span(&self) -> Span {
|
||||
match self {
|
||||
AlterTableOperation::AddConstraint(table_constraint) => table_constraint.span(),
|
||||
AlterTableOperation::AddConstraint {
|
||||
constraint,
|
||||
not_valid: _,
|
||||
} => constraint.span(),
|
||||
AlterTableOperation::AddColumn {
|
||||
column_keyword: _,
|
||||
if_not_exists: _,
|
||||
|
@ -980,10 +1111,11 @@ impl Spanned for AlterTableOperation {
|
|||
drop_behavior: _,
|
||||
} => name.span,
|
||||
AlterTableOperation::DropColumn {
|
||||
column_name,
|
||||
has_column_keyword: _,
|
||||
column_names,
|
||||
if_exists: _,
|
||||
drop_behavior: _,
|
||||
} => column_name.span,
|
||||
} => union_spans(column_names.iter().map(|i| i.span)),
|
||||
AlterTableOperation::AttachPartition { partition } => partition.span(),
|
||||
AlterTableOperation::DetachPartition { partition } => partition.span(),
|
||||
AlterTableOperation::FreezePartition {
|
||||
|
@ -999,6 +1131,8 @@ impl Spanned for AlterTableOperation {
|
|||
.span()
|
||||
.union_opt(&with_name.as_ref().map(|n| n.span)),
|
||||
AlterTableOperation::DropPrimaryKey => Span::empty(),
|
||||
AlterTableOperation::DropForeignKey { name } => name.span,
|
||||
AlterTableOperation::DropIndex { name } => name.span,
|
||||
AlterTableOperation::EnableAlwaysRule { name } => name.span,
|
||||
AlterTableOperation::EnableAlwaysTrigger { name } => name.span,
|
||||
AlterTableOperation::EnableReplicaRule { name } => name.span,
|
||||
|
@ -1064,6 +1198,9 @@ impl Spanned for AlterTableOperation {
|
|||
AlterTableOperation::ResumeRecluster => Span::empty(),
|
||||
AlterTableOperation::Algorithm { .. } => Span::empty(),
|
||||
AlterTableOperation::AutoIncrement { value, .. } => value.span(),
|
||||
AlterTableOperation::Lock { .. } => Span::empty(),
|
||||
AlterTableOperation::ReplicaIdentity { .. } => Span::empty(),
|
||||
AlterTableOperation::ValidateConstraint { name } => name.span,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -1279,7 +1416,6 @@ impl Spanned for AssignmentTarget {
|
|||
/// f.e. `IS NULL <expr>` reports as `<expr>::span`.
|
||||
///
|
||||
/// Missing spans:
|
||||
/// - [Expr::TypedString] # missing span for data_type
|
||||
/// - [Expr::MatchAgainst] # MySQL specific
|
||||
/// - [Expr::RLike] # MySQL specific
|
||||
/// - [Expr::Struct] # BigQuery specific
|
||||
|
@ -1429,6 +1565,7 @@ impl Spanned for Expr {
|
|||
substring_from,
|
||||
substring_for,
|
||||
special: _,
|
||||
shorthand: _,
|
||||
} => union_spans(
|
||||
core::iter::once(expr.span())
|
||||
.chain(substring_from.as_ref().map(|i| i.span()))
|
||||
|
@ -1448,12 +1585,16 @@ impl Spanned for Expr {
|
|||
.map(|items| union_spans(items.iter().map(|i| i.span()))),
|
||||
),
|
||||
),
|
||||
Expr::IntroducedString { value, .. } => value.span(),
|
||||
Expr::Prefixed { value, .. } => value.span(),
|
||||
Expr::Case {
|
||||
case_token,
|
||||
end_token,
|
||||
operand,
|
||||
conditions,
|
||||
else_result,
|
||||
} => union_spans(
|
||||
iter::once(case_token.0.span)
|
||||
.chain(
|
||||
operand
|
||||
.as_ref()
|
||||
.map(|i| i.span())
|
||||
|
@ -1462,6 +1603,8 @@ impl Spanned for Expr {
|
|||
[case_when.condition.span(), case_when.result.span()]
|
||||
}))
|
||||
.chain(else_result.as_ref().map(|i| i.span())),
|
||||
)
|
||||
.chain(iter::once(end_token.0.span)),
|
||||
),
|
||||
Expr::Exists { subquery, .. } => subquery.span(),
|
||||
Expr::Subquery(query) => query.span(),
|
||||
|
@ -1481,6 +1624,7 @@ impl Spanned for Expr {
|
|||
Expr::OuterJoin(expr) => expr.span(),
|
||||
Expr::Prior(expr) => expr.span(),
|
||||
Expr::Lambda(_) => Span::empty(),
|
||||
Expr::MemberOf(member_of) => member_of.value.span().union(&member_of.array.span()),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -1812,6 +1956,7 @@ impl Spanned for TableFactor {
|
|||
.chain(alias.as_ref().map(|alias| alias.span())),
|
||||
),
|
||||
TableFactor::JsonTable { .. } => Span::empty(),
|
||||
TableFactor::XmlTable { .. } => Span::empty(),
|
||||
TableFactor::Pivot {
|
||||
table,
|
||||
aggregate_functions,
|
||||
|
@ -1830,6 +1975,7 @@ impl Spanned for TableFactor {
|
|||
TableFactor::Unpivot {
|
||||
table,
|
||||
value,
|
||||
null_inclusion: _,
|
||||
name,
|
||||
columns,
|
||||
alias,
|
||||
|
@ -2034,6 +2180,7 @@ impl Spanned for JoinOperator {
|
|||
} => match_condition.span().union(&constraint.span()),
|
||||
JoinOperator::Anti(join_constraint) => join_constraint.span(),
|
||||
JoinOperator::Semi(join_constraint) => join_constraint.span(),
|
||||
JoinOperator::StraightJoin(join_constraint) => join_constraint.span(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -2183,6 +2330,28 @@ impl Spanned for TableObject {
|
|||
}
|
||||
}
|
||||
|
||||
impl Spanned for BeginEndStatements {
|
||||
fn span(&self) -> Span {
|
||||
let BeginEndStatements {
|
||||
begin_token,
|
||||
statements,
|
||||
end_token,
|
||||
} = self;
|
||||
union_spans(
|
||||
core::iter::once(begin_token.0.span)
|
||||
.chain(statements.iter().map(|i| i.span()))
|
||||
.chain(core::iter::once(end_token.0.span)),
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
impl Spanned for OpenStatement {
|
||||
fn span(&self) -> Span {
|
||||
let OpenStatement { cursor_name } = self;
|
||||
cursor_name.span
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
pub mod tests {
|
||||
use crate::dialect::{Dialect, GenericDialect, SnowflakeDialect};
|
||||
|
@ -2322,4 +2491,16 @@ pub mod tests {
|
|||
|
||||
assert_eq!(test.get_source(body_span), "SELECT cte.* FROM cte");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_case_expr_span() {
|
||||
let dialect = &GenericDialect;
|
||||
let mut test = SpanTest::new(dialect, "CASE 1 WHEN 2 THEN 3 ELSE 4 END");
|
||||
let expr = test.0.parse_expr().unwrap();
|
||||
let expr_span = expr.span();
|
||||
assert_eq!(
|
||||
test.get_source(expr_span),
|
||||
"CASE 1 WHEN 2 THEN 3 ELSE 4 END"
|
||||
);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -110,6 +110,7 @@ impl fmt::Display for TriggerEvent {
|
|||
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
|
||||
#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
|
||||
pub enum TriggerPeriod {
|
||||
For,
|
||||
After,
|
||||
Before,
|
||||
InsteadOf,
|
||||
|
@ -118,6 +119,7 @@ pub enum TriggerPeriod {
|
|||
impl fmt::Display for TriggerPeriod {
|
||||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
||||
match self {
|
||||
TriggerPeriod::For => write!(f, "FOR"),
|
||||
TriggerPeriod::After => write!(f, "AFTER"),
|
||||
TriggerPeriod::Before => write!(f, "BEFORE"),
|
||||
TriggerPeriod::InsteadOf => write!(f, "INSTEAD OF"),
|
||||
|
|
|
@ -116,7 +116,6 @@ impl From<ValueWithSpan> for Value {
|
|||
derive(Visit, VisitMut),
|
||||
visit(with = "visit_value")
|
||||
)]
|
||||
|
||||
pub enum Value {
|
||||
/// Numeric literal
|
||||
#[cfg(not(feature = "bigdecimal"))]
|
||||
|
@ -456,30 +455,38 @@ impl fmt::Display for EscapeQuotedString<'_> {
|
|||
// | `"A\"B\"A"` | default | `DoubleQuotedString(String::from("A\"B\"A"))` | `"A""B""A"` |
|
||||
let quote = self.quote;
|
||||
let mut previous_char = char::default();
|
||||
let mut peekable_chars = self.string.chars().peekable();
|
||||
while let Some(&ch) = peekable_chars.peek() {
|
||||
let mut start_idx = 0;
|
||||
let mut peekable_chars = self.string.char_indices().peekable();
|
||||
while let Some(&(idx, ch)) = peekable_chars.peek() {
|
||||
match ch {
|
||||
char if char == quote => {
|
||||
if previous_char == '\\' {
|
||||
write!(f, "{char}")?;
|
||||
// the quote is already escaped with a backslash, skip
|
||||
peekable_chars.next();
|
||||
continue;
|
||||
}
|
||||
peekable_chars.next();
|
||||
if peekable_chars.peek().map(|c| *c == quote).unwrap_or(false) {
|
||||
write!(f, "{char}{char}")?;
|
||||
match peekable_chars.peek() {
|
||||
Some((_, c)) if *c == quote => {
|
||||
// the quote is already escaped with another quote, skip
|
||||
peekable_chars.next();
|
||||
} else {
|
||||
write!(f, "{char}{char}")?;
|
||||
}
|
||||
_ => {
|
||||
// The quote is not escaped.
|
||||
// Including idx in the range, so the quote at idx will be printed twice:
|
||||
// in this call to write_str() and in the next one.
|
||||
f.write_str(&self.string[start_idx..=idx])?;
|
||||
start_idx = idx;
|
||||
}
|
||||
}
|
||||
}
|
||||
_ => {
|
||||
write!(f, "{ch}")?;
|
||||
peekable_chars.next();
|
||||
}
|
||||
}
|
||||
previous_char = ch;
|
||||
}
|
||||
f.write_str(&self.string[start_idx..])?;
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
@ -543,16 +550,16 @@ impl fmt::Display for EscapeUnicodeStringLiteral<'_> {
|
|||
write!(f, r#"\\"#)?;
|
||||
}
|
||||
x if x.is_ascii() => {
|
||||
write!(f, "{}", c)?;
|
||||
write!(f, "{c}")?;
|
||||
}
|
||||
_ => {
|
||||
let codepoint = c as u32;
|
||||
// if the character fits in 32 bits, we can use the \XXXX format
|
||||
// otherwise, we need to use the \+XXXXXX format
|
||||
if codepoint <= 0xFFFF {
|
||||
write!(f, "\\{:04X}", codepoint)?;
|
||||
write!(f, "\\{codepoint:04X}")?;
|
||||
} else {
|
||||
write!(f, "\\+{:06X}", codepoint)?;
|
||||
write!(f, "\\+{codepoint:06X}")?;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -523,7 +523,7 @@ where
|
|||
/// // Remove all select limits in sub-queries
|
||||
/// visit_expressions_mut(&mut statements, |expr| {
|
||||
/// if let Expr::Subquery(q) = expr {
|
||||
/// q.limit = None
|
||||
/// q.limit_clause = None;
|
||||
/// }
|
||||
/// ControlFlow::<()>::Continue(())
|
||||
/// });
|
||||
|
@ -647,7 +647,7 @@ where
|
|||
/// // Remove all select limits in outer statements (not in sub-queries)
|
||||
/// visit_statements_mut(&mut statements, |stmt| {
|
||||
/// if let Statement::Query(q) = stmt {
|
||||
/// q.limit = None
|
||||
/// q.limit_clause = None;
|
||||
/// }
|
||||
/// ControlFlow::<()>::Continue(())
|
||||
/// });
|
||||
|
@ -741,7 +741,7 @@ mod tests {
|
|||
}
|
||||
}
|
||||
|
||||
fn do_visit<V: Visitor>(sql: &str, visitor: &mut V) -> Statement {
|
||||
fn do_visit<V: Visitor<Break = ()>>(sql: &str, visitor: &mut V) -> Statement {
|
||||
let dialect = GenericDialect {};
|
||||
let tokens = Tokenizer::new(&dialect, sql).tokenize().unwrap();
|
||||
let s = Parser::new(&dialect)
|
||||
|
@ -749,7 +749,8 @@ mod tests {
|
|||
.parse_statement()
|
||||
.unwrap();
|
||||
|
||||
s.visit(visitor);
|
||||
let flow = s.visit(visitor);
|
||||
assert_eq!(flow, ControlFlow::Continue(()));
|
||||
s
|
||||
}
|
||||
|
||||
|
@ -925,10 +926,10 @@ mod tests {
|
|||
#[test]
|
||||
fn overflow() {
|
||||
let cond = (0..1000)
|
||||
.map(|n| format!("X = {}", n))
|
||||
.map(|n| format!("X = {n}"))
|
||||
.collect::<Vec<_>>()
|
||||
.join(" OR ");
|
||||
let sql = format!("SELECT x where {0}", cond);
|
||||
let sql = format!("SELECT x where {cond}");
|
||||
|
||||
let dialect = GenericDialect {};
|
||||
let tokens = Tokenizer::new(&dialect, sql.as_str()).tokenize().unwrap();
|
||||
|
@ -938,7 +939,8 @@ mod tests {
|
|||
.unwrap();
|
||||
|
||||
let mut visitor = QuickVisitor {};
|
||||
s.visit(&mut visitor);
|
||||
let flow = s.visit(&mut visitor);
|
||||
assert_eq!(flow, ControlFlow::Continue(()));
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -969,7 +971,7 @@ mod visit_mut_tests {
|
|||
}
|
||||
}
|
||||
|
||||
fn do_visit_mut<V: VisitorMut>(sql: &str, visitor: &mut V) -> Statement {
|
||||
fn do_visit_mut<V: VisitorMut<Break = ()>>(sql: &str, visitor: &mut V) -> Statement {
|
||||
let dialect = GenericDialect {};
|
||||
let tokens = Tokenizer::new(&dialect, sql).tokenize().unwrap();
|
||||
let mut s = Parser::new(&dialect)
|
||||
|
@ -977,7 +979,8 @@ mod visit_mut_tests {
|
|||
.parse_statement()
|
||||
.unwrap();
|
||||
|
||||
s.visit(visitor);
|
||||
let flow = s.visit(visitor);
|
||||
assert_eq!(flow, ControlFlow::Continue(()));
|
||||
s
|
||||
}
|
||||
|
||||
|
|
|
@ -46,7 +46,11 @@ pub struct BigQueryDialect;
|
|||
|
||||
impl Dialect for BigQueryDialect {
|
||||
fn parse_statement(&self, parser: &mut Parser) -> Option<Result<Statement, ParserError>> {
|
||||
self.maybe_parse_statement(parser)
|
||||
if parser.parse_keyword(Keyword::BEGIN) {
|
||||
return Some(parser.parse_begin_exception_end());
|
||||
}
|
||||
|
||||
None
|
||||
}
|
||||
|
||||
/// See <https://cloud.google.com/bigquery/docs/reference/standard-sql/lexical#identifiers>
|
||||
|
@ -136,49 +140,8 @@ impl Dialect for BigQueryDialect {
|
|||
fn is_column_alias(&self, kw: &Keyword, _parser: &mut Parser) -> bool {
|
||||
!RESERVED_FOR_COLUMN_ALIAS.contains(kw)
|
||||
}
|
||||
}
|
||||
|
||||
impl BigQueryDialect {
|
||||
fn maybe_parse_statement(&self, parser: &mut Parser) -> Option<Result<Statement, ParserError>> {
|
||||
if parser.peek_keyword(Keyword::BEGIN) {
|
||||
return Some(self.parse_begin(parser));
|
||||
}
|
||||
None
|
||||
}
|
||||
|
||||
/// Parse a `BEGIN` statement.
|
||||
/// <https://cloud.google.com/bigquery/docs/reference/standard-sql/procedural-language#beginexceptionend>
|
||||
fn parse_begin(&self, parser: &mut Parser) -> Result<Statement, ParserError> {
|
||||
parser.expect_keyword(Keyword::BEGIN)?;
|
||||
|
||||
let statements = parser.parse_statement_list(&[Keyword::EXCEPTION, Keyword::END])?;
|
||||
|
||||
let has_exception_when_clause = parser.parse_keywords(&[
|
||||
Keyword::EXCEPTION,
|
||||
Keyword::WHEN,
|
||||
Keyword::ERROR,
|
||||
Keyword::THEN,
|
||||
]);
|
||||
let exception_statements = if has_exception_when_clause {
|
||||
if !parser.peek_keyword(Keyword::END) {
|
||||
Some(parser.parse_statement_list(&[Keyword::END])?)
|
||||
} else {
|
||||
Some(Default::default())
|
||||
}
|
||||
} else {
|
||||
None
|
||||
};
|
||||
|
||||
parser.expect_keyword(Keyword::END)?;
|
||||
|
||||
Ok(Statement::StartTransaction {
|
||||
begin: true,
|
||||
statements,
|
||||
exception_statements,
|
||||
has_end_keyword: true,
|
||||
transaction: None,
|
||||
modifier: None,
|
||||
modes: Default::default(),
|
||||
})
|
||||
fn supports_pipe_operator(&self) -> bool {
|
||||
true
|
||||
}
|
||||
}
|
||||
|
|
|
@ -52,6 +52,10 @@ impl Dialect for GenericDialect {
|
|||
true
|
||||
}
|
||||
|
||||
fn supports_left_associative_joins_without_parens(&self) -> bool {
|
||||
true
|
||||
}
|
||||
|
||||
fn supports_connect_by(&self) -> bool {
|
||||
true
|
||||
}
|
||||
|
@ -108,6 +112,14 @@ impl Dialect for GenericDialect {
|
|||
true
|
||||
}
|
||||
|
||||
fn supports_from_first_select(&self) -> bool {
|
||||
true
|
||||
}
|
||||
|
||||
fn supports_projection_trailing_commas(&self) -> bool {
|
||||
true
|
||||
}
|
||||
|
||||
fn supports_asc_desc_in_column_definition(&self) -> bool {
|
||||
true
|
||||
}
|
||||
|
@ -155,4 +167,16 @@ impl Dialect for GenericDialect {
|
|||
fn supports_match_against(&self) -> bool {
|
||||
true
|
||||
}
|
||||
|
||||
fn supports_set_names(&self) -> bool {
|
||||
true
|
||||
}
|
||||
|
||||
fn supports_comma_separated_set_assignments(&self) -> bool {
|
||||
true
|
||||
}
|
||||
|
||||
fn supports_filter_during_aggregation(&self) -> bool {
|
||||
true
|
||||
}
|
||||
}
|
||||
|
|
|
@ -49,7 +49,7 @@ pub use self::postgresql::PostgreSqlDialect;
|
|||
pub use self::redshift::RedshiftSqlDialect;
|
||||
pub use self::snowflake::SnowflakeDialect;
|
||||
pub use self::sqlite::SQLiteDialect;
|
||||
use crate::ast::{ColumnOption, Expr, Statement};
|
||||
use crate::ast::{ColumnOption, Expr, GranteesType, Statement};
|
||||
pub use crate::keywords;
|
||||
use crate::keywords::Keyword;
|
||||
use crate::parser::{Parser, ParserError};
|
||||
|
@ -201,6 +201,33 @@ pub trait Dialect: Debug + Any {
|
|||
false
|
||||
}
|
||||
|
||||
/// Determine whether the dialect strips the backslash when escaping LIKE wildcards (%, _).
|
||||
///
|
||||
/// [MySQL] has a special case when escaping single quoted strings which leaves these unescaped
|
||||
/// so they can be used in LIKE patterns without double-escaping (as is necessary in other
|
||||
/// escaping dialects, such as [Snowflake]). Generally, special characters have escaping rules
|
||||
/// causing them to be replaced with a different byte sequences (e.g. `'\0'` becoming the zero
|
||||
/// byte), and the default if an escaped character does not have a specific escaping rule is to
|
||||
/// strip the backslash (e.g. there is no rule for `h`, so `'\h' = 'h'`). MySQL's special case
|
||||
/// for ignoring LIKE wildcard escapes is to *not* strip the backslash, so that `'\%' = '\\%'`.
|
||||
/// This applies to all string literals though, not just those used in LIKE patterns.
|
||||
///
|
||||
/// ```text
|
||||
/// mysql> select '\_', hex('\\'), hex('_'), hex('\_');
|
||||
/// +----+-----------+----------+-----------+
|
||||
/// | \_ | hex('\\') | hex('_') | hex('\_') |
|
||||
/// +----+-----------+----------+-----------+
|
||||
/// | \_ | 5C | 5F | 5C5F |
|
||||
/// +----+-----------+----------+-----------+
|
||||
/// 1 row in set (0.00 sec)
|
||||
/// ```
|
||||
///
|
||||
/// [MySQL]: https://dev.mysql.com/doc/refman/8.4/en/string-literals.html
|
||||
/// [Snowflake]: https://docs.snowflake.com/en/sql-reference/functions/like#usage-notes
|
||||
fn ignores_wildcard_escapes(&self) -> bool {
|
||||
false
|
||||
}
|
||||
|
||||
/// Determine if the dialect supports string literals with `U&` prefix.
|
||||
/// This is used to specify Unicode code points in string literals.
|
||||
/// For example, in PostgreSQL, the following is a valid string literal:
|
||||
|
@ -251,6 +278,34 @@ pub trait Dialect: Debug + Any {
|
|||
false
|
||||
}
|
||||
|
||||
/// Indicates whether the dialect supports left-associative join parsing
|
||||
/// by default when parentheses are omitted in nested joins.
|
||||
///
|
||||
/// Most dialects (like MySQL or Postgres) assume **left-associative** precedence,
|
||||
/// so a query like:
|
||||
///
|
||||
/// ```sql
|
||||
/// SELECT * FROM t1 NATURAL JOIN t5 INNER JOIN t0 ON ...
|
||||
/// ```
|
||||
/// is interpreted as:
|
||||
/// ```sql
|
||||
/// ((t1 NATURAL JOIN t5) INNER JOIN t0 ON ...)
|
||||
/// ```
|
||||
/// and internally represented as a **flat list** of joins.
|
||||
///
|
||||
/// In contrast, some dialects (e.g. **Snowflake**) assume **right-associative**
|
||||
/// precedence and interpret the same query as:
|
||||
/// ```sql
|
||||
/// (t1 NATURAL JOIN (t5 INNER JOIN t0 ON ...))
|
||||
/// ```
|
||||
/// which results in a **nested join** structure in the AST.
|
||||
///
|
||||
/// If this method returns `false`, the parser must build nested join trees
|
||||
/// even in the absence of parentheses to reflect the correct associativity
|
||||
fn supports_left_associative_joins_without_parens(&self) -> bool {
|
||||
true
|
||||
}
|
||||
|
||||
/// Returns true if the dialect supports the `(+)` syntax for OUTER JOIN.
|
||||
fn supports_outer_join_operator(&self) -> bool {
|
||||
false
|
||||
|
@ -372,6 +427,16 @@ pub trait Dialect: Debug + Any {
|
|||
false
|
||||
}
|
||||
|
||||
/// Returns true if the dialect supports multiple `SET` statements
|
||||
/// in a single statement.
|
||||
///
|
||||
/// ```sql
|
||||
/// SET variable = expression [, variable = expression];
|
||||
/// ```
|
||||
fn supports_comma_separated_set_assignments(&self) -> bool {
|
||||
false
|
||||
}
|
||||
|
||||
/// Returns true if the dialect supports an `EXCEPT` clause following a
|
||||
/// wildcard in a select list.
|
||||
///
|
||||
|
@ -481,6 +546,20 @@ pub trait Dialect: Debug + Any {
|
|||
false
|
||||
}
|
||||
|
||||
/// Return true if the dialect supports pipe operator.
|
||||
///
|
||||
/// Example:
|
||||
/// ```sql
|
||||
/// SELECT *
|
||||
/// FROM table
|
||||
/// |> limit 1
|
||||
/// ```
|
||||
///
|
||||
/// See <https://cloud.google.com/bigquery/docs/pipe-syntax-guide#basic_syntax>
|
||||
fn supports_pipe_operator(&self) -> bool {
|
||||
false
|
||||
}
|
||||
|
||||
/// Does the dialect support MySQL-style `'user'@'host'` grantee syntax?
|
||||
fn supports_user_host_grantee(&self) -> bool {
|
||||
false
|
||||
|
@ -536,7 +615,7 @@ pub trait Dialect: Debug + Any {
|
|||
}
|
||||
|
||||
let token = parser.peek_token();
|
||||
debug!("get_next_precedence_full() {:?}", token);
|
||||
debug!("get_next_precedence_full() {token:?}");
|
||||
match token.token {
|
||||
Token::Word(w) if w.keyword == Keyword::OR => Ok(p!(Or)),
|
||||
Token::Word(w) if w.keyword == Keyword::AND => Ok(p!(And)),
|
||||
|
@ -568,7 +647,9 @@ pub trait Dialect: Debug + Any {
|
|||
Token::Word(w) if w.keyword == Keyword::ILIKE => Ok(p!(Like)),
|
||||
Token::Word(w) if w.keyword == Keyword::RLIKE => Ok(p!(Like)),
|
||||
Token::Word(w) if w.keyword == Keyword::REGEXP => Ok(p!(Like)),
|
||||
Token::Word(w) if w.keyword == Keyword::MATCH => Ok(p!(Like)),
|
||||
Token::Word(w) if w.keyword == Keyword::SIMILAR => Ok(p!(Like)),
|
||||
Token::Word(w) if w.keyword == Keyword::MEMBER => Ok(p!(Like)),
|
||||
_ => Ok(self.prec_unknown()),
|
||||
},
|
||||
Token::Word(w) if w.keyword == Keyword::IS => Ok(p!(Is)),
|
||||
|
@ -579,11 +660,14 @@ pub trait Dialect: Debug + Any {
|
|||
Token::Word(w) if w.keyword == Keyword::ILIKE => Ok(p!(Like)),
|
||||
Token::Word(w) if w.keyword == Keyword::RLIKE => Ok(p!(Like)),
|
||||
Token::Word(w) if w.keyword == Keyword::REGEXP => Ok(p!(Like)),
|
||||
Token::Word(w) if w.keyword == Keyword::MATCH => Ok(p!(Like)),
|
||||
Token::Word(w) if w.keyword == Keyword::SIMILAR => Ok(p!(Like)),
|
||||
Token::Word(w) if w.keyword == Keyword::MEMBER => Ok(p!(Like)),
|
||||
Token::Word(w) if w.keyword == Keyword::OPERATOR => Ok(p!(Between)),
|
||||
Token::Word(w) if w.keyword == Keyword::DIV => Ok(p!(MulDivModOp)),
|
||||
Token::Period => Ok(p!(Period)),
|
||||
Token::Eq
|
||||
Token::Assignment
|
||||
| Token::Eq
|
||||
| Token::Lt
|
||||
| Token::LtEq
|
||||
| Token::Neq
|
||||
|
@ -850,6 +934,17 @@ pub trait Dialect: Debug + Any {
|
|||
keywords::RESERVED_FOR_TABLE_FACTOR
|
||||
}
|
||||
|
||||
/// Returns reserved keywords that may prefix a select item expression
|
||||
/// e.g. `SELECT CONNECT_BY_ROOT name FROM Tbl2` (Snowflake)
|
||||
fn get_reserved_keywords_for_select_item_operator(&self) -> &[Keyword] {
|
||||
&[]
|
||||
}
|
||||
|
||||
/// Returns grantee types that should be treated as identifiers
|
||||
fn get_reserved_grantees_types(&self) -> &[GranteesType] {
|
||||
&[]
|
||||
}
|
||||
|
||||
/// Returns true if this dialect supports the `TABLESAMPLE` option
|
||||
/// before the table alias option. For example:
|
||||
///
|
||||
|
@ -953,6 +1048,34 @@ pub trait Dialect: Debug + Any {
|
|||
fn supports_order_by_all(&self) -> bool {
|
||||
false
|
||||
}
|
||||
|
||||
/// Returns true if the dialect supports `SET NAMES <charset_name> [COLLATE <collation_name>]`.
|
||||
///
|
||||
/// - [MySQL](https://dev.mysql.com/doc/refman/8.4/en/set-names.html)
|
||||
/// - [PostgreSQL](https://www.postgresql.org/docs/17/sql-set.html)
|
||||
///
|
||||
/// Note: Postgres doesn't support the `COLLATE` clause, but we permissively parse it anyway.
|
||||
fn supports_set_names(&self) -> bool {
|
||||
false
|
||||
}
|
||||
|
||||
fn supports_space_separated_column_options(&self) -> bool {
|
||||
false
|
||||
}
|
||||
|
||||
/// Returns true if the dialect supports the `USING` clause in an `ALTER COLUMN` statement.
|
||||
/// Example:
|
||||
/// ```sql
|
||||
/// ALTER TABLE tbl ALTER COLUMN col SET DATA TYPE <type> USING <exp>`
|
||||
/// ```
|
||||
fn supports_alter_column_type_using(&self) -> bool {
|
||||
false
|
||||
}
|
||||
|
||||
/// Returns true if the dialect supports `ALTER TABLE tbl DROP COLUMN c1, ..., cn`
|
||||
fn supports_comma_separated_drop_column_list(&self) -> bool {
|
||||
false
|
||||
}
|
||||
}
|
||||
|
||||
/// This represents the operators for which precedence must be defined
|
||||
|
|
|
@ -15,7 +15,19 @@
|
|||
// specific language governing permissions and limitations
|
||||
// under the License.
|
||||
|
||||
use crate::ast::helpers::attached_token::AttachedToken;
|
||||
use crate::ast::{
|
||||
BeginEndStatements, ConditionalStatementBlock, ConditionalStatements, GranteesType,
|
||||
IfStatement, Statement, TriggerObject,
|
||||
};
|
||||
use crate::dialect::Dialect;
|
||||
use crate::keywords::{self, Keyword};
|
||||
use crate::parser::{Parser, ParserError};
|
||||
use crate::tokenizer::Token;
|
||||
#[cfg(not(feature = "std"))]
|
||||
use alloc::{vec, vec::Vec};
|
||||
|
||||
const RESERVED_FOR_COLUMN_ALIAS: &[Keyword] = &[Keyword::IF, Keyword::ELSE];
|
||||
|
||||
/// A [`Dialect`] for [Microsoft SQL Server](https://www.microsoft.com/en-us/sql-server/)
|
||||
#[derive(Debug)]
|
||||
|
@ -40,6 +52,10 @@ impl Dialect for MsSqlDialect {
|
|||
|| ch == '_'
|
||||
}
|
||||
|
||||
fn identifier_quote_style(&self, _identifier: &str) -> Option<char> {
|
||||
Some('[')
|
||||
}
|
||||
|
||||
/// SQL Server has `CONVERT(type, value)` instead of `CONVERT(value, type)`
|
||||
/// <https://learn.microsoft.com/en-us/sql/t-sql/functions/cast-and-convert-transact-sql?view=sql-server-ver16>
|
||||
fn convert_type_before_value(&self) -> bool {
|
||||
|
@ -82,6 +98,7 @@ impl Dialect for MsSqlDialect {
|
|||
fn supports_start_transaction_modifier(&self) -> bool {
|
||||
true
|
||||
}
|
||||
|
||||
fn supports_end_transaction_modifier(&self) -> bool {
|
||||
true
|
||||
}
|
||||
|
@ -95,4 +112,187 @@ impl Dialect for MsSqlDialect {
|
|||
fn supports_timestamp_versioning(&self) -> bool {
|
||||
true
|
||||
}
|
||||
|
||||
/// See <https://learn.microsoft.com/en-us/sql/t-sql/language-elements/slash-star-comment-transact-sql?view=sql-server-ver16>
|
||||
fn supports_nested_comments(&self) -> bool {
|
||||
true
|
||||
}
|
||||
|
||||
/// See <https://learn.microsoft.com/en-us/sql/t-sql/queries/from-transact-sql>
|
||||
fn supports_object_name_double_dot_notation(&self) -> bool {
|
||||
true
|
||||
}
|
||||
|
||||
/// See <https://learn.microsoft.com/en-us/sql/relational-databases/security/authentication-access/server-level-roles>
|
||||
fn get_reserved_grantees_types(&self) -> &[GranteesType] {
|
||||
&[GranteesType::Public]
|
||||
}
|
||||
|
||||
fn is_column_alias(&self, kw: &Keyword, _parser: &mut Parser) -> bool {
|
||||
!keywords::RESERVED_FOR_COLUMN_ALIAS.contains(kw) && !RESERVED_FOR_COLUMN_ALIAS.contains(kw)
|
||||
}
|
||||
|
||||
fn parse_statement(&self, parser: &mut Parser) -> Option<Result<Statement, ParserError>> {
|
||||
if parser.peek_keyword(Keyword::IF) {
|
||||
Some(self.parse_if_stmt(parser))
|
||||
} else if parser.parse_keywords(&[Keyword::CREATE, Keyword::TRIGGER]) {
|
||||
Some(self.parse_create_trigger(parser, false))
|
||||
} else if parser.parse_keywords(&[
|
||||
Keyword::CREATE,
|
||||
Keyword::OR,
|
||||
Keyword::ALTER,
|
||||
Keyword::TRIGGER,
|
||||
]) {
|
||||
Some(self.parse_create_trigger(parser, true))
|
||||
} else {
|
||||
None
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl MsSqlDialect {
|
||||
/// ```sql
|
||||
/// IF boolean_expression
|
||||
/// { sql_statement | statement_block }
|
||||
/// [ ELSE
|
||||
/// { sql_statement | statement_block } ]
|
||||
/// ```
|
||||
fn parse_if_stmt(&self, parser: &mut Parser) -> Result<Statement, ParserError> {
|
||||
let if_token = parser.expect_keyword(Keyword::IF)?;
|
||||
|
||||
let condition = parser.parse_expr()?;
|
||||
|
||||
let if_block = if parser.peek_keyword(Keyword::BEGIN) {
|
||||
let begin_token = parser.expect_keyword(Keyword::BEGIN)?;
|
||||
let statements = self.parse_statement_list(parser, Some(Keyword::END))?;
|
||||
let end_token = parser.expect_keyword(Keyword::END)?;
|
||||
ConditionalStatementBlock {
|
||||
start_token: AttachedToken(if_token),
|
||||
condition: Some(condition),
|
||||
then_token: None,
|
||||
conditional_statements: ConditionalStatements::BeginEnd(BeginEndStatements {
|
||||
begin_token: AttachedToken(begin_token),
|
||||
statements,
|
||||
end_token: AttachedToken(end_token),
|
||||
}),
|
||||
}
|
||||
} else {
|
||||
let stmt = parser.parse_statement()?;
|
||||
ConditionalStatementBlock {
|
||||
start_token: AttachedToken(if_token),
|
||||
condition: Some(condition),
|
||||
then_token: None,
|
||||
conditional_statements: ConditionalStatements::Sequence {
|
||||
statements: vec![stmt],
|
||||
},
|
||||
}
|
||||
};
|
||||
|
||||
let mut prior_statement_ended_with_semi_colon = false;
|
||||
while let Token::SemiColon = parser.peek_token_ref().token {
|
||||
parser.advance_token();
|
||||
prior_statement_ended_with_semi_colon = true;
|
||||
}
|
||||
|
||||
let mut else_block = None;
|
||||
if parser.peek_keyword(Keyword::ELSE) {
|
||||
let else_token = parser.expect_keyword(Keyword::ELSE)?;
|
||||
if parser.peek_keyword(Keyword::BEGIN) {
|
||||
let begin_token = parser.expect_keyword(Keyword::BEGIN)?;
|
||||
let statements = self.parse_statement_list(parser, Some(Keyword::END))?;
|
||||
let end_token = parser.expect_keyword(Keyword::END)?;
|
||||
else_block = Some(ConditionalStatementBlock {
|
||||
start_token: AttachedToken(else_token),
|
||||
condition: None,
|
||||
then_token: None,
|
||||
conditional_statements: ConditionalStatements::BeginEnd(BeginEndStatements {
|
||||
begin_token: AttachedToken(begin_token),
|
||||
statements,
|
||||
end_token: AttachedToken(end_token),
|
||||
}),
|
||||
});
|
||||
} else {
|
||||
let stmt = parser.parse_statement()?;
|
||||
else_block = Some(ConditionalStatementBlock {
|
||||
start_token: AttachedToken(else_token),
|
||||
condition: None,
|
||||
then_token: None,
|
||||
conditional_statements: ConditionalStatements::Sequence {
|
||||
statements: vec![stmt],
|
||||
},
|
||||
});
|
||||
}
|
||||
} else if prior_statement_ended_with_semi_colon {
|
||||
parser.prev_token();
|
||||
}
|
||||
|
||||
Ok(Statement::If(IfStatement {
|
||||
if_block,
|
||||
else_block,
|
||||
elseif_blocks: Vec::new(),
|
||||
end_token: None,
|
||||
}))
|
||||
}
|
||||
|
||||
/// Parse `CREATE TRIGGER` for [MsSql]
|
||||
///
|
||||
/// [MsSql]: https://learn.microsoft.com/en-us/sql/t-sql/statements/create-trigger-transact-sql
|
||||
fn parse_create_trigger(
|
||||
&self,
|
||||
parser: &mut Parser,
|
||||
or_alter: bool,
|
||||
) -> Result<Statement, ParserError> {
|
||||
let name = parser.parse_object_name(false)?;
|
||||
parser.expect_keyword_is(Keyword::ON)?;
|
||||
let table_name = parser.parse_object_name(false)?;
|
||||
let period = parser.parse_trigger_period()?;
|
||||
let events = parser.parse_comma_separated(Parser::parse_trigger_event)?;
|
||||
|
||||
parser.expect_keyword_is(Keyword::AS)?;
|
||||
let statements = Some(parser.parse_conditional_statements(&[Keyword::END])?);
|
||||
|
||||
Ok(Statement::CreateTrigger {
|
||||
or_alter,
|
||||
or_replace: false,
|
||||
is_constraint: false,
|
||||
name,
|
||||
period,
|
||||
events,
|
||||
table_name,
|
||||
referenced_table_name: None,
|
||||
referencing: Vec::new(),
|
||||
trigger_object: TriggerObject::Statement,
|
||||
include_each: false,
|
||||
condition: None,
|
||||
exec_body: None,
|
||||
statements,
|
||||
characteristics: None,
|
||||
})
|
||||
}
|
||||
|
||||
/// Parse a sequence of statements, optionally separated by semicolon.
|
||||
///
|
||||
/// Stops parsing when reaching EOF or the given keyword.
|
||||
fn parse_statement_list(
|
||||
&self,
|
||||
parser: &mut Parser,
|
||||
terminal_keyword: Option<Keyword>,
|
||||
) -> Result<Vec<Statement>, ParserError> {
|
||||
let mut stmts = Vec::new();
|
||||
loop {
|
||||
if let Token::EOF = parser.peek_token_ref().token {
|
||||
break;
|
||||
}
|
||||
if let Some(term) = terminal_keyword {
|
||||
if parser.peek_keyword(term) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
stmts.push(parser.parse_statement()?);
|
||||
while let Token::SemiColon = parser.peek_token_ref().token {
|
||||
parser.advance_token();
|
||||
}
|
||||
}
|
||||
Ok(stmts)
|
||||
}
|
||||
}
|
||||
|
|
|
@ -27,7 +27,12 @@ use crate::{
|
|||
|
||||
use super::keywords;
|
||||
|
||||
const RESERVED_FOR_TABLE_ALIAS_MYSQL: &[Keyword] = &[Keyword::USE, Keyword::IGNORE, Keyword::FORCE];
|
||||
const RESERVED_FOR_TABLE_ALIAS_MYSQL: &[Keyword] = &[
|
||||
Keyword::USE,
|
||||
Keyword::IGNORE,
|
||||
Keyword::FORCE,
|
||||
Keyword::STRAIGHT_JOIN,
|
||||
];
|
||||
|
||||
/// A [`Dialect`] for [MySQL](https://www.mysql.com/)
|
||||
#[derive(Debug)]
|
||||
|
@ -62,6 +67,10 @@ impl Dialect for MySqlDialect {
|
|||
true
|
||||
}
|
||||
|
||||
fn ignores_wildcard_escapes(&self) -> bool {
|
||||
true
|
||||
}
|
||||
|
||||
fn supports_numeric_prefix(&self) -> bool {
|
||||
true
|
||||
}
|
||||
|
@ -133,6 +142,14 @@ impl Dialect for MySqlDialect {
|
|||
fn supports_match_against(&self) -> bool {
|
||||
true
|
||||
}
|
||||
|
||||
fn supports_set_names(&self) -> bool {
|
||||
true
|
||||
}
|
||||
|
||||
fn supports_comma_separated_set_assignments(&self) -> bool {
|
||||
true
|
||||
}
|
||||
}
|
||||
|
||||
/// `LOCK TABLES`
|
||||
|
|
|
@ -104,7 +104,7 @@ impl Dialect for PostgreSqlDialect {
|
|||
|
||||
fn get_next_precedence(&self, parser: &Parser) -> Option<Result<u8, ParserError>> {
|
||||
let token = parser.peek_token();
|
||||
debug!("get_next_precedence() {:?}", token);
|
||||
debug!("get_next_precedence() {token:?}");
|
||||
|
||||
// we only return some custom value here when the behaviour (not merely the numeric value) differs
|
||||
// from the default implementation
|
||||
|
@ -254,4 +254,12 @@ impl Dialect for PostgreSqlDialect {
|
|||
fn supports_geometric_types(&self) -> bool {
|
||||
true
|
||||
}
|
||||
|
||||
fn supports_set_names(&self) -> bool {
|
||||
true
|
||||
}
|
||||
|
||||
fn supports_alter_column_type_using(&self) -> bool {
|
||||
true
|
||||
}
|
||||
}
|
||||
|
|
|
@ -80,13 +80,15 @@ impl Dialect for RedshiftSqlDialect {
|
|||
}
|
||||
|
||||
fn is_identifier_start(&self, ch: char) -> bool {
|
||||
// Extends Postgres dialect with sharp
|
||||
PostgreSqlDialect {}.is_identifier_start(ch) || ch == '#'
|
||||
// Extends Postgres dialect with sharp and UTF-8 multibyte chars
|
||||
// https://docs.aws.amazon.com/redshift/latest/dg/r_names.html
|
||||
PostgreSqlDialect {}.is_identifier_start(ch) || ch == '#' || !ch.is_ascii()
|
||||
}
|
||||
|
||||
fn is_identifier_part(&self, ch: char) -> bool {
|
||||
// Extends Postgres dialect with sharp
|
||||
PostgreSqlDialect {}.is_identifier_part(ch) || ch == '#'
|
||||
// Extends Postgres dialect with sharp and UTF-8 multibyte chars
|
||||
// https://docs.aws.amazon.com/redshift/latest/dg/r_names.html
|
||||
PostgreSqlDialect {}.is_identifier_part(ch) || ch == '#' || !ch.is_ascii()
|
||||
}
|
||||
|
||||
/// redshift has `CONVERT(type, value)` instead of `CONVERT(value, type)`
|
||||
|
@ -121,4 +123,12 @@ impl Dialect for RedshiftSqlDialect {
|
|||
fn supports_array_typedef_with_brackets(&self) -> bool {
|
||||
true
|
||||
}
|
||||
|
||||
fn allow_extract_single_quotes(&self) -> bool {
|
||||
true
|
||||
}
|
||||
|
||||
fn supports_string_literal_backslash_escape(&self) -> bool {
|
||||
true
|
||||
}
|
||||
}
|
||||
|
|
|
@ -20,17 +20,17 @@ use crate::alloc::string::ToString;
|
|||
use crate::ast::helpers::key_value_options::{KeyValueOption, KeyValueOptionType, KeyValueOptions};
|
||||
use crate::ast::helpers::stmt_create_table::CreateTableBuilder;
|
||||
use crate::ast::helpers::stmt_data_loading::{
|
||||
FileStagingCommand, StageLoadSelectItem, StageParamsObject,
|
||||
FileStagingCommand, StageLoadSelectItem, StageLoadSelectItemKind, StageParamsObject,
|
||||
};
|
||||
use crate::ast::{
|
||||
ColumnOption, ColumnPolicy, ColumnPolicyProperty, CopyIntoSnowflakeKind, Ident,
|
||||
IdentityParameters, IdentityProperty, IdentityPropertyFormatKind, IdentityPropertyKind,
|
||||
IdentityPropertyOrder, ObjectName, RowAccessPolicy, ShowObjects, Statement, TagsColumnOption,
|
||||
WrappedCollection,
|
||||
IdentityPropertyOrder, ObjectName, RowAccessPolicy, ShowObjects, SqlOption, Statement,
|
||||
TagsColumnOption, WrappedCollection,
|
||||
};
|
||||
use crate::dialect::{Dialect, Precedence};
|
||||
use crate::keywords::Keyword;
|
||||
use crate::parser::{Parser, ParserError};
|
||||
use crate::parser::{IsOptional, Parser, ParserError};
|
||||
use crate::tokenizer::{Token, Word};
|
||||
#[cfg(not(feature = "std"))]
|
||||
use alloc::boxed::Box;
|
||||
|
@ -44,6 +44,7 @@ use alloc::{format, vec};
|
|||
use super::keywords::RESERVED_FOR_IDENTIFIER;
|
||||
use sqlparser::ast::StorageSerializationPolicy;
|
||||
|
||||
const RESERVED_KEYWORDS_FOR_SELECT_ITEM_OPERATOR: [Keyword; 1] = [Keyword::CONNECT_BY_ROOT];
|
||||
/// A [`Dialect`] for [Snowflake](https://www.snowflake.com/)
|
||||
#[derive(Debug, Default)]
|
||||
pub struct SnowflakeDialect;
|
||||
|
@ -130,6 +131,10 @@ impl Dialect for SnowflakeDialect {
|
|||
}
|
||||
|
||||
fn parse_statement(&self, parser: &mut Parser) -> Option<Result<Statement, ParserError>> {
|
||||
if parser.parse_keyword(Keyword::BEGIN) {
|
||||
return Some(parser.parse_begin_exception_end());
|
||||
}
|
||||
|
||||
if parser.parse_keywords(&[Keyword::ALTER, Keyword::SESSION]) {
|
||||
// ALTER SESSION
|
||||
let set = match parser.parse_one_of_keywords(&[Keyword::SET, Keyword::UNSET]) {
|
||||
|
@ -278,6 +283,10 @@ impl Dialect for SnowflakeDialect {
|
|||
true
|
||||
}
|
||||
|
||||
fn supports_left_associative_joins_without_parens(&self) -> bool {
|
||||
false
|
||||
}
|
||||
|
||||
fn is_reserved_for_identifier(&self, kw: Keyword) -> bool {
|
||||
// Unreserve some keywords that Snowflake accepts as identifiers
|
||||
// See: https://docs.snowflake.com/en/sql-reference/reserved-keywords
|
||||
|
@ -292,9 +301,8 @@ impl Dialect for SnowflakeDialect {
|
|||
true
|
||||
}
|
||||
|
||||
fn is_select_item_alias(&self, explicit: bool, kw: &Keyword, parser: &mut Parser) -> bool {
|
||||
explicit
|
||||
|| match kw {
|
||||
fn is_column_alias(&self, kw: &Keyword, parser: &mut Parser) -> bool {
|
||||
match kw {
|
||||
// The following keywords can be considered an alias as long as
|
||||
// they are not followed by other tokens that may change their meaning
|
||||
// e.g. `SELECT * EXCEPT (col1) FROM tbl`
|
||||
|
@ -310,7 +318,7 @@ impl Dialect for SnowflakeDialect {
|
|||
}
|
||||
|
||||
// `FETCH` can be considered an alias as long as it's not followed by `FIRST`` or `NEXT`
|
||||
// which would give it a different meanins, for example: `SELECT 1 FETCH FIRST 10 ROWS` - not an alias
|
||||
// which would give it a different meanings, for example: `SELECT 1 FETCH FIRST 10 ROWS` - not an alias
|
||||
Keyword::FETCH
|
||||
if parser.peek_keyword(Keyword::FIRST) || parser.peek_keyword(Keyword::NEXT) =>
|
||||
{
|
||||
|
@ -346,6 +354,19 @@ impl Dialect for SnowflakeDialect {
|
|||
fn supports_group_by_expr(&self) -> bool {
|
||||
true
|
||||
}
|
||||
|
||||
/// See: <https://docs.snowflake.com/en/sql-reference/constructs/connect-by>
|
||||
fn get_reserved_keywords_for_select_item_operator(&self) -> &[Keyword] {
|
||||
&RESERVED_KEYWORDS_FOR_SELECT_ITEM_OPERATOR
|
||||
}
|
||||
|
||||
fn supports_space_separated_column_options(&self) -> bool {
|
||||
true
|
||||
}
|
||||
|
||||
fn supports_comma_separated_drop_column_list(&self) -> bool {
|
||||
true
|
||||
}
|
||||
}
|
||||
|
||||
fn parse_file_staging_command(kw: Keyword, parser: &mut Parser) -> Result<Statement, ParserError> {
|
||||
|
@ -411,6 +432,8 @@ pub fn parse_create_table(
|
|||
// "CREATE TABLE x COPY GRANTS (c INT)" and "CREATE TABLE x (c INT) COPY GRANTS" are both
|
||||
// accepted by Snowflake
|
||||
|
||||
let mut plain_options = vec![];
|
||||
|
||||
loop {
|
||||
let next_token = parser.next_token();
|
||||
match &next_token.token {
|
||||
|
@ -422,7 +445,9 @@ pub fn parse_create_table(
|
|||
Keyword::COMMENT => {
|
||||
// Rewind the COMMENT keyword
|
||||
parser.prev_token();
|
||||
builder = builder.comment(parser.parse_optional_inline_comment()?);
|
||||
if let Some(comment_def) = parser.parse_optional_inline_comment()? {
|
||||
plain_options.push(SqlOption::Comment(comment_def))
|
||||
}
|
||||
}
|
||||
Keyword::AS => {
|
||||
let query = parser.parse_query()?;
|
||||
|
@ -443,7 +468,7 @@ pub fn parse_create_table(
|
|||
parser.expect_keyword_is(Keyword::BY)?;
|
||||
parser.expect_token(&Token::LParen)?;
|
||||
let cluster_by = Some(WrappedCollection::Parentheses(
|
||||
parser.parse_comma_separated(|p| p.parse_identifier())?,
|
||||
parser.parse_comma_separated(|p| p.parse_expr())?,
|
||||
));
|
||||
parser.expect_token(&Token::RParen)?;
|
||||
|
||||
|
@ -550,6 +575,9 @@ pub fn parse_create_table(
|
|||
builder.storage_serialization_policy =
|
||||
Some(parse_storage_serialization_policy(parser)?);
|
||||
}
|
||||
Keyword::IF if parser.parse_keywords(&[Keyword::NOT, Keyword::EXISTS]) => {
|
||||
builder = builder.if_not_exists(true);
|
||||
}
|
||||
_ => {
|
||||
return parser.expected("end of statement", next_token);
|
||||
}
|
||||
|
@ -583,6 +611,13 @@ pub fn parse_create_table(
|
|||
}
|
||||
}
|
||||
}
|
||||
let table_options = if !plain_options.is_empty() {
|
||||
crate::ast::CreateTableOptions::Plain(plain_options)
|
||||
} else {
|
||||
crate::ast::CreateTableOptions::None
|
||||
};
|
||||
|
||||
builder = builder.table_options(table_options);
|
||||
|
||||
if iceberg && builder.base_location.is_none() {
|
||||
return Err(ParserError::ParserError(
|
||||
|
@ -644,10 +679,7 @@ pub fn parse_create_stage(
|
|||
// [ comment ]
|
||||
if parser.parse_keyword(Keyword::COMMENT) {
|
||||
parser.expect_token(&Token::Eq)?;
|
||||
comment = Some(match parser.next_token().token {
|
||||
Token::SingleQuotedString(word) => Ok(word),
|
||||
_ => parser.expected("a comment statement", parser.peek_token()),
|
||||
}?)
|
||||
comment = Some(parser.parse_comment_value()?);
|
||||
}
|
||||
|
||||
Ok(Statement::CreateStage {
|
||||
|
@ -725,7 +757,7 @@ pub fn parse_copy_into(parser: &mut Parser) -> Result<Statement, ParserError> {
|
|||
};
|
||||
|
||||
let mut files: Vec<String> = vec![];
|
||||
let mut from_transformations: Option<Vec<StageLoadSelectItem>> = None;
|
||||
let mut from_transformations: Option<Vec<StageLoadSelectItemKind>> = None;
|
||||
let mut from_stage_alias = None;
|
||||
let mut from_stage = None;
|
||||
let mut stage_params = StageParamsObject {
|
||||
|
@ -747,6 +779,11 @@ pub fn parse_copy_into(parser: &mut Parser) -> Result<Statement, ParserError> {
|
|||
stage_params = parse_stage_params(parser)?;
|
||||
}
|
||||
|
||||
let into_columns = match &parser.peek_token().token {
|
||||
Token::LParen => Some(parser.parse_parenthesized_column_list(IsOptional::Optional, true)?),
|
||||
_ => None,
|
||||
};
|
||||
|
||||
parser.expect_keyword_is(Keyword::FROM)?;
|
||||
match parser.next_token().token {
|
||||
Token::LParen if kind == CopyIntoSnowflakeKind::Table => {
|
||||
|
@ -758,15 +795,10 @@ pub fn parse_copy_into(parser: &mut Parser) -> Result<Statement, ParserError> {
|
|||
from_stage = Some(parse_snowflake_stage_name(parser)?);
|
||||
stage_params = parse_stage_params(parser)?;
|
||||
|
||||
// as
|
||||
from_stage_alias = if parser.parse_keyword(Keyword::AS) {
|
||||
Some(match parser.next_token().token {
|
||||
Token::Word(w) => Ok(Ident::new(w.value)),
|
||||
_ => parser.expected("stage alias", parser.peek_token()),
|
||||
}?)
|
||||
} else {
|
||||
None
|
||||
};
|
||||
// Parse an optional alias
|
||||
from_stage_alias = parser
|
||||
.maybe_parse_table_alias()?
|
||||
.map(|table_alias| table_alias.name);
|
||||
parser.expect_token(&Token::RParen)?;
|
||||
}
|
||||
Token::LParen if kind == CopyIntoSnowflakeKind::Location => {
|
||||
|
@ -849,6 +881,7 @@ pub fn parse_copy_into(parser: &mut Parser) -> Result<Statement, ParserError> {
|
|||
Ok(Statement::CopyIntoSnowflake {
|
||||
kind,
|
||||
into,
|
||||
into_columns,
|
||||
from_obj: from_stage,
|
||||
from_obj_alias: from_stage_alias,
|
||||
stage_params,
|
||||
|
@ -869,10 +902,29 @@ pub fn parse_copy_into(parser: &mut Parser) -> Result<Statement, ParserError> {
|
|||
|
||||
fn parse_select_items_for_data_load(
|
||||
parser: &mut Parser,
|
||||
) -> Result<Option<Vec<StageLoadSelectItem>>, ParserError> {
|
||||
// [<alias>.]$<file_col_num>[.<element>] [ , [<alias>.]$<file_col_num>[.<element>] ... ]
|
||||
let mut select_items: Vec<StageLoadSelectItem> = vec![];
|
||||
) -> Result<Option<Vec<StageLoadSelectItemKind>>, ParserError> {
|
||||
let mut select_items: Vec<StageLoadSelectItemKind> = vec![];
|
||||
loop {
|
||||
match parser.maybe_parse(parse_select_item_for_data_load)? {
|
||||
// [<alias>.]$<file_col_num>[.<element>] [ , [<alias>.]$<file_col_num>[.<element>] ... ]
|
||||
Some(item) => select_items.push(StageLoadSelectItemKind::StageLoadSelectItem(item)),
|
||||
// Fallback, try to parse a standard SQL select item
|
||||
None => select_items.push(StageLoadSelectItemKind::SelectItem(
|
||||
parser.parse_select_item()?,
|
||||
)),
|
||||
}
|
||||
if matches!(parser.peek_token_ref().token, Token::Comma) {
|
||||
parser.advance_token();
|
||||
} else {
|
||||
break;
|
||||
}
|
||||
}
|
||||
Ok(Some(select_items))
|
||||
}
|
||||
|
||||
fn parse_select_item_for_data_load(
|
||||
parser: &mut Parser,
|
||||
) -> Result<StageLoadSelectItem, ParserError> {
|
||||
let mut alias: Option<Ident> = None;
|
||||
let mut file_col_num: i32 = 0;
|
||||
let mut element: Option<Ident> = None;
|
||||
|
@ -931,24 +983,12 @@ fn parse_select_items_for_data_load(
|
|||
}?);
|
||||
}
|
||||
|
||||
select_items.push(StageLoadSelectItem {
|
||||
Ok(StageLoadSelectItem {
|
||||
alias,
|
||||
file_col_num,
|
||||
element,
|
||||
item_as,
|
||||
});
|
||||
|
||||
match parser.next_token().token {
|
||||
Token::Comma => {
|
||||
// continue
|
||||
}
|
||||
_ => {
|
||||
parser.prev_token(); // need to move back
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
Ok(Some(select_items))
|
||||
})
|
||||
}
|
||||
|
||||
fn parse_stage_params(parser: &mut Parser) -> Result<StageParamsObject, ParserError> {
|
||||
|
@ -1016,9 +1056,15 @@ fn parse_session_options(
|
|||
let mut options: Vec<KeyValueOption> = Vec::new();
|
||||
let empty = String::new;
|
||||
loop {
|
||||
match parser.next_token().token {
|
||||
Token::Comma => continue,
|
||||
let next_token = parser.peek_token();
|
||||
match next_token.token {
|
||||
Token::SemiColon | Token::EOF => break,
|
||||
Token::Comma => {
|
||||
parser.advance_token();
|
||||
continue;
|
||||
}
|
||||
Token::Word(key) => {
|
||||
parser.advance_token();
|
||||
if set {
|
||||
let option = parse_option(parser, key)?;
|
||||
options.push(option);
|
||||
|
@ -1031,21 +1077,17 @@ fn parse_session_options(
|
|||
}
|
||||
}
|
||||
_ => {
|
||||
if parser.peek_token().token == Token::EOF {
|
||||
break;
|
||||
}
|
||||
return parser.expected("another option", parser.peek_token());
|
||||
return parser.expected("another option or end of statement", next_token);
|
||||
}
|
||||
}
|
||||
}
|
||||
options
|
||||
.is_empty()
|
||||
.then(|| {
|
||||
if options.is_empty() {
|
||||
Err(ParserError::ParserError(
|
||||
"expected at least one option".to_string(),
|
||||
))
|
||||
})
|
||||
.unwrap_or(Ok(options))
|
||||
} else {
|
||||
Ok(options)
|
||||
}
|
||||
}
|
||||
|
||||
/// Parses options provided within parentheses like:
|
||||
|
@ -1150,7 +1192,7 @@ fn parse_column_policy_property(
|
|||
parser: &mut Parser,
|
||||
with: bool,
|
||||
) -> Result<ColumnPolicyProperty, ParserError> {
|
||||
let policy_name = parser.parse_identifier()?;
|
||||
let policy_name = parser.parse_object_name(false)?;
|
||||
let using_columns = if parser.parse_keyword(Keyword::USING) {
|
||||
parser.expect_token(&Token::LParen)?;
|
||||
let columns = parser.parse_comma_separated(|p| p.parse_identifier())?;
|
||||
|
|
|
@ -15,7 +15,11 @@
|
|||
// specific language governing permissions and limitations
|
||||
// under the License.
|
||||
|
||||
use crate::ast::Statement;
|
||||
#[cfg(not(feature = "std"))]
|
||||
use alloc::boxed::Box;
|
||||
|
||||
use crate::ast::BinaryOperator;
|
||||
use crate::ast::{Expr, Statement};
|
||||
use crate::dialect::Dialect;
|
||||
use crate::keywords::Keyword;
|
||||
use crate::parser::{Parser, ParserError};
|
||||
|
@ -70,6 +74,27 @@ impl Dialect for SQLiteDialect {
|
|||
}
|
||||
}
|
||||
|
||||
fn parse_infix(
|
||||
&self,
|
||||
parser: &mut crate::parser::Parser,
|
||||
expr: &crate::ast::Expr,
|
||||
_precedence: u8,
|
||||
) -> Option<Result<crate::ast::Expr, ParserError>> {
|
||||
// Parse MATCH and REGEXP as operators
|
||||
// See <https://www.sqlite.org/lang_expr.html#the_like_glob_regexp_match_and_extract_operators>
|
||||
for (keyword, op) in [
|
||||
(Keyword::REGEXP, BinaryOperator::Regexp),
|
||||
(Keyword::MATCH, BinaryOperator::Match),
|
||||
] {
|
||||
if parser.parse_keyword(keyword) {
|
||||
let left = Box::new(expr.clone());
|
||||
let right = Box::new(parser.parse_expr().unwrap());
|
||||
return Some(Ok(Expr::BinaryOp { left, op, right }));
|
||||
}
|
||||
}
|
||||
None
|
||||
}
|
||||
|
||||
fn supports_in_empty_list(&self) -> bool {
|
||||
true
|
||||
}
|
||||
|
|
135
src/display_utils.rs
Normal file
135
src/display_utils.rs
Normal file
|
@ -0,0 +1,135 @@
|
|||
// Licensed to the Apache Software Foundation (ASF) under one
|
||||
// or more contributor license agreements. See the NOTICE file
|
||||
// distributed with this work for additional information
|
||||
// regarding copyright ownership. The ASF licenses this file
|
||||
// to you under the Apache License, Version 2.0 (the
|
||||
// "License"); you may not use this file except in compliance
|
||||
// with the License. You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing,
|
||||
// software distributed under the License is distributed on an
|
||||
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
||||
// KIND, either express or implied. See the License for the
|
||||
// specific language governing permissions and limitations
|
||||
// under the License.
|
||||
|
||||
//! Utilities for formatting SQL AST nodes with pretty printing support.
|
||||
//!
|
||||
//! The module provides formatters that implement the `Display` trait with support
|
||||
//! for both regular (`{}`) and pretty (`{:#}`) formatting modes. Pretty printing
|
||||
//! adds proper indentation and line breaks to make SQL statements more readable.
|
||||
|
||||
use core::fmt::{self, Display, Write};
|
||||
|
||||
/// A wrapper around a value that adds an indent to the value when displayed with {:#}.
|
||||
pub(crate) struct Indent<T>(pub T);
|
||||
|
||||
const INDENT: &str = " ";
|
||||
|
||||
impl<T> Display for Indent<T>
|
||||
where
|
||||
T: Display,
|
||||
{
|
||||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||
if f.alternate() {
|
||||
f.write_str(INDENT)?;
|
||||
write!(Indent(f), "{:#}", self.0)
|
||||
} else {
|
||||
self.0.fmt(f)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Adds an indent to the inner writer
|
||||
impl<T> Write for Indent<T>
|
||||
where
|
||||
T: Write,
|
||||
{
|
||||
fn write_str(&mut self, s: &str) -> fmt::Result {
|
||||
self.0.write_str(s)?;
|
||||
// Our NewLine and SpaceOrNewline utils always print individual newlines as a single-character string.
|
||||
if s == "\n" {
|
||||
self.0.write_str(INDENT)?;
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
/// A value that inserts a newline when displayed with {:#}, but not when displayed with {}.
|
||||
pub(crate) struct NewLine;
|
||||
|
||||
impl Display for NewLine {
|
||||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||
if f.alternate() {
|
||||
f.write_char('\n')
|
||||
} else {
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// A value that inserts a space when displayed with {}, but a newline when displayed with {:#}.
|
||||
pub(crate) struct SpaceOrNewline;
|
||||
|
||||
impl Display for SpaceOrNewline {
|
||||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||
if f.alternate() {
|
||||
f.write_char('\n')
|
||||
} else {
|
||||
f.write_char(' ')
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// A value that displays a comma-separated list of values.
|
||||
/// When pretty-printed (using {:#}), it displays each value on a new line.
|
||||
pub(crate) struct DisplayCommaSeparated<'a, T: fmt::Display>(pub(crate) &'a [T]);
|
||||
|
||||
impl<T: fmt::Display> fmt::Display for DisplayCommaSeparated<'_, T> {
|
||||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
||||
let mut first = true;
|
||||
for t in self.0 {
|
||||
if !first {
|
||||
f.write_char(',')?;
|
||||
SpaceOrNewline.fmt(f)?;
|
||||
}
|
||||
first = false;
|
||||
t.fmt(f)?;
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
/// Displays a whitespace, followed by a comma-separated list that is indented when pretty-printed.
|
||||
pub(crate) fn indented_list<T: fmt::Display>(f: &mut fmt::Formatter, items: &[T]) -> fmt::Result {
|
||||
SpaceOrNewline.fmt(f)?;
|
||||
Indent(DisplayCommaSeparated(items)).fmt(f)
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn test_indent() {
|
||||
struct TwoLines;
|
||||
|
||||
impl Display for TwoLines {
|
||||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||
f.write_str("line 1")?;
|
||||
SpaceOrNewline.fmt(f)?;
|
||||
f.write_str("line 2")
|
||||
}
|
||||
}
|
||||
|
||||
let indent = Indent(TwoLines);
|
||||
assert_eq!(
|
||||
indent.to_string(),
|
||||
TwoLines.to_string(),
|
||||
"Only the alternate form should be indented"
|
||||
);
|
||||
assert_eq!(format!("{:#}", indent), " line 1\n line 2");
|
||||
}
|
||||
}
|
|
@ -83,6 +83,7 @@ define_keywords!(
|
|||
ADMIN,
|
||||
AFTER,
|
||||
AGAINST,
|
||||
AGGREGATE,
|
||||
AGGREGATION,
|
||||
ALERT,
|
||||
ALGORITHM,
|
||||
|
@ -115,9 +116,11 @@ define_keywords!(
|
|||
AUTHENTICATION,
|
||||
AUTHORIZATION,
|
||||
AUTO,
|
||||
AUTOEXTEND_SIZE,
|
||||
AUTOINCREMENT,
|
||||
AUTO_INCREMENT,
|
||||
AVG,
|
||||
AVG_ROW_LENGTH,
|
||||
AVRO,
|
||||
BACKWARD,
|
||||
BASE64,
|
||||
|
@ -137,11 +140,13 @@ define_keywords!(
|
|||
BIT,
|
||||
BLOB,
|
||||
BLOCK,
|
||||
BLOOM,
|
||||
BLOOMFILTER,
|
||||
BOOL,
|
||||
BOOLEAN,
|
||||
BOTH,
|
||||
BOX,
|
||||
BRIN,
|
||||
BROWSE,
|
||||
BTREE,
|
||||
BUCKET,
|
||||
|
@ -171,11 +176,13 @@ define_keywords!(
|
|||
CHANNEL,
|
||||
CHAR,
|
||||
CHARACTER,
|
||||
CHARACTERISTICS,
|
||||
CHARACTERS,
|
||||
CHARACTER_LENGTH,
|
||||
CHARSET,
|
||||
CHAR_LENGTH,
|
||||
CHECK,
|
||||
CHECKSUM,
|
||||
CIRCLE,
|
||||
CLEAR,
|
||||
CLOB,
|
||||
|
@ -204,6 +211,7 @@ define_keywords!(
|
|||
CONNECT,
|
||||
CONNECTION,
|
||||
CONNECTOR,
|
||||
CONNECT_BY_ROOT,
|
||||
CONSTRAINT,
|
||||
CONTAINS,
|
||||
CONTINUE,
|
||||
|
@ -264,11 +272,13 @@ define_keywords!(
|
|||
DEFINED,
|
||||
DEFINER,
|
||||
DELAYED,
|
||||
DELAY_KEY_WRITE,
|
||||
DELETE,
|
||||
DELIMITED,
|
||||
DELIMITER,
|
||||
DELTA,
|
||||
DENSE_RANK,
|
||||
DENY,
|
||||
DEREF,
|
||||
DESC,
|
||||
DESCRIBE,
|
||||
|
@ -283,6 +293,7 @@ define_keywords!(
|
|||
DISTRIBUTE,
|
||||
DIV,
|
||||
DO,
|
||||
DOMAIN,
|
||||
DOUBLE,
|
||||
DOW,
|
||||
DOY,
|
||||
|
@ -294,6 +305,7 @@ define_keywords!(
|
|||
ELEMENT,
|
||||
ELEMENTS,
|
||||
ELSE,
|
||||
ELSEIF,
|
||||
EMPTY,
|
||||
ENABLE,
|
||||
ENABLE_SCHEMA_EVOLUTION,
|
||||
|
@ -306,6 +318,7 @@ define_keywords!(
|
|||
END_PARTITION,
|
||||
ENFORCED,
|
||||
ENGINE,
|
||||
ENGINE_ATTRIBUTE,
|
||||
ENUM,
|
||||
ENUM16,
|
||||
ENUM8,
|
||||
|
@ -333,6 +346,7 @@ define_keywords!(
|
|||
EXPLAIN,
|
||||
EXPLICIT,
|
||||
EXPORT,
|
||||
EXTEND,
|
||||
EXTENDED,
|
||||
EXTENSION,
|
||||
EXTERNAL,
|
||||
|
@ -381,11 +395,14 @@ define_keywords!(
|
|||
FUNCTION,
|
||||
FUNCTIONS,
|
||||
FUSION,
|
||||
FUTURE,
|
||||
GENERAL,
|
||||
GENERATE,
|
||||
GENERATED,
|
||||
GEOGRAPHY,
|
||||
GET,
|
||||
GIN,
|
||||
GIST,
|
||||
GLOBAL,
|
||||
GRANT,
|
||||
GRANTED,
|
||||
|
@ -405,6 +422,7 @@ define_keywords!(
|
|||
HOSTS,
|
||||
HOUR,
|
||||
HOURS,
|
||||
HUGEINT,
|
||||
ICEBERG,
|
||||
ID,
|
||||
IDENTITY,
|
||||
|
@ -423,6 +441,7 @@ define_keywords!(
|
|||
INDEX,
|
||||
INDICATOR,
|
||||
INHERIT,
|
||||
INHERITS,
|
||||
INITIALLY,
|
||||
INNER,
|
||||
INOUT,
|
||||
|
@ -432,6 +451,7 @@ define_keywords!(
|
|||
INPUTFORMAT,
|
||||
INSENSITIVE,
|
||||
INSERT,
|
||||
INSERT_METHOD,
|
||||
INSTALL,
|
||||
INSTANT,
|
||||
INSTEAD,
|
||||
|
@ -468,6 +488,7 @@ define_keywords!(
|
|||
JULIAN,
|
||||
KEY,
|
||||
KEYS,
|
||||
KEY_BLOCK_SIZE,
|
||||
KILL,
|
||||
LAG,
|
||||
LANGUAGE,
|
||||
|
@ -521,12 +542,14 @@ define_keywords!(
|
|||
MAX,
|
||||
MAXVALUE,
|
||||
MAX_DATA_EXTENSION_TIME_IN_DAYS,
|
||||
MAX_ROWS,
|
||||
MEASURES,
|
||||
MEDIUMBLOB,
|
||||
MEDIUMINT,
|
||||
MEDIUMTEXT,
|
||||
MEMBER,
|
||||
MERGE,
|
||||
MESSAGE,
|
||||
METADATA,
|
||||
METHOD,
|
||||
METRIC,
|
||||
|
@ -541,6 +564,7 @@ define_keywords!(
|
|||
MINUTE,
|
||||
MINUTES,
|
||||
MINVALUE,
|
||||
MIN_ROWS,
|
||||
MOD,
|
||||
MODE,
|
||||
MODIFIES,
|
||||
|
@ -553,6 +577,7 @@ define_keywords!(
|
|||
MULTISET,
|
||||
MUTATION,
|
||||
NAME,
|
||||
NAMES,
|
||||
NANOSECOND,
|
||||
NANOSECONDS,
|
||||
NATIONAL,
|
||||
|
@ -622,8 +647,10 @@ define_keywords!(
|
|||
ORDER,
|
||||
ORDINALITY,
|
||||
ORGANIZATION,
|
||||
OTHER,
|
||||
OUT,
|
||||
OUTER,
|
||||
OUTPUT,
|
||||
OUTPUTFORMAT,
|
||||
OVER,
|
||||
OVERFLOW,
|
||||
|
@ -636,6 +663,7 @@ define_keywords!(
|
|||
OWNERSHIP,
|
||||
PACKAGE,
|
||||
PACKAGES,
|
||||
PACK_KEYS,
|
||||
PARALLEL,
|
||||
PARAMETER,
|
||||
PARQUET,
|
||||
|
@ -643,6 +671,7 @@ define_keywords!(
|
|||
PARTITION,
|
||||
PARTITIONED,
|
||||
PARTITIONS,
|
||||
PASSING,
|
||||
PASSWORD,
|
||||
PAST,
|
||||
PATH,
|
||||
|
@ -675,6 +704,7 @@ define_keywords!(
|
|||
PRESERVE,
|
||||
PREWHERE,
|
||||
PRIMARY,
|
||||
PRINT,
|
||||
PRIOR,
|
||||
PRIVILEGES,
|
||||
PROCEDURE,
|
||||
|
@ -688,6 +718,7 @@ define_keywords!(
|
|||
QUARTER,
|
||||
QUERY,
|
||||
QUOTE,
|
||||
RAISE,
|
||||
RAISERROR,
|
||||
RANGE,
|
||||
RANK,
|
||||
|
@ -729,6 +760,7 @@ define_keywords!(
|
|||
REPLICATION,
|
||||
RESET,
|
||||
RESOLVE,
|
||||
RESOURCE,
|
||||
RESPECT,
|
||||
RESTART,
|
||||
RESTRICT,
|
||||
|
@ -754,6 +786,7 @@ define_keywords!(
|
|||
ROW,
|
||||
ROWID,
|
||||
ROWS,
|
||||
ROW_FORMAT,
|
||||
ROW_NUMBER,
|
||||
RULE,
|
||||
RUN,
|
||||
|
@ -768,6 +801,7 @@ define_keywords!(
|
|||
SEARCH,
|
||||
SECOND,
|
||||
SECONDARY,
|
||||
SECONDARY_ENGINE_ATTRIBUTE,
|
||||
SECONDS,
|
||||
SECRET,
|
||||
SECURITY,
|
||||
|
@ -782,6 +816,7 @@ define_keywords!(
|
|||
SERDE,
|
||||
SERDEPROPERTIES,
|
||||
SERIALIZABLE,
|
||||
SERVER,
|
||||
SERVICE,
|
||||
SESSION,
|
||||
SESSION_USER,
|
||||
|
@ -790,6 +825,7 @@ define_keywords!(
|
|||
SETS,
|
||||
SETTINGS,
|
||||
SHARE,
|
||||
SHARED,
|
||||
SHARING,
|
||||
SHOW,
|
||||
SIGNED,
|
||||
|
@ -805,11 +841,13 @@ define_keywords!(
|
|||
SPATIAL,
|
||||
SPECIFIC,
|
||||
SPECIFICTYPE,
|
||||
SPGIST,
|
||||
SQL,
|
||||
SQLEXCEPTION,
|
||||
SQLSTATE,
|
||||
SQLWARNING,
|
||||
SQRT,
|
||||
SRID,
|
||||
STABLE,
|
||||
STAGE,
|
||||
START,
|
||||
|
@ -817,19 +855,25 @@ define_keywords!(
|
|||
STATEMENT,
|
||||
STATIC,
|
||||
STATISTICS,
|
||||
STATS_AUTO_RECALC,
|
||||
STATS_PERSISTENT,
|
||||
STATS_SAMPLE_PAGES,
|
||||
STATUS,
|
||||
STDDEV_POP,
|
||||
STDDEV_SAMP,
|
||||
STDIN,
|
||||
STDOUT,
|
||||
STEP,
|
||||
STORAGE,
|
||||
STORAGE_INTEGRATION,
|
||||
STORAGE_SERIALIZATION_POLICY,
|
||||
STORED,
|
||||
STRAIGHT_JOIN,
|
||||
STRICT,
|
||||
STRING,
|
||||
STRUCT,
|
||||
SUBMULTISET,
|
||||
SUBSTR,
|
||||
SUBSTRING,
|
||||
SUBSTRING_REGEX,
|
||||
SUCCEEDS,
|
||||
|
@ -847,6 +891,7 @@ define_keywords!(
|
|||
TABLE,
|
||||
TABLES,
|
||||
TABLESAMPLE,
|
||||
TABLESPACE,
|
||||
TAG,
|
||||
TARGET,
|
||||
TASK,
|
||||
|
@ -863,6 +908,7 @@ define_keywords!(
|
|||
TIME,
|
||||
TIMESTAMP,
|
||||
TIMESTAMPTZ,
|
||||
TIMESTAMP_NTZ,
|
||||
TIMETZ,
|
||||
TIMEZONE,
|
||||
TIMEZONE_ABBR,
|
||||
|
@ -891,9 +937,13 @@ define_keywords!(
|
|||
TRY,
|
||||
TRY_CAST,
|
||||
TRY_CONVERT,
|
||||
TSQUERY,
|
||||
TSVECTOR,
|
||||
TUPLE,
|
||||
TYPE,
|
||||
UBIGINT,
|
||||
UESCAPE,
|
||||
UHUGEINT,
|
||||
UINT128,
|
||||
UINT16,
|
||||
UINT256,
|
||||
|
@ -927,9 +977,12 @@ define_keywords!(
|
|||
USER,
|
||||
USER_RESOURCES,
|
||||
USING,
|
||||
USMALLINT,
|
||||
UTINYINT,
|
||||
UUID,
|
||||
VACUUM,
|
||||
VALID,
|
||||
VALIDATE,
|
||||
VALIDATION_MODE,
|
||||
VALUE,
|
||||
VALUES,
|
||||
|
@ -957,6 +1010,7 @@ define_keywords!(
|
|||
WHEN,
|
||||
WHENEVER,
|
||||
WHERE,
|
||||
WHILE,
|
||||
WIDTH_BUCKET,
|
||||
WINDOW,
|
||||
WITH,
|
||||
|
@ -964,8 +1018,11 @@ define_keywords!(
|
|||
WITHOUT,
|
||||
WITHOUT_ARRAY_WRAPPER,
|
||||
WORK,
|
||||
WRAPPER,
|
||||
WRITE,
|
||||
XML,
|
||||
XMLNAMESPACES,
|
||||
XMLTABLE,
|
||||
XOR,
|
||||
YEAR,
|
||||
YEARS,
|
||||
|
@ -1038,6 +1095,7 @@ pub const RESERVED_FOR_TABLE_ALIAS: &[Keyword] = &[
|
|||
Keyword::SAMPLE,
|
||||
Keyword::TABLESAMPLE,
|
||||
Keyword::FROM,
|
||||
Keyword::OPEN,
|
||||
];
|
||||
|
||||
/// Can't be used as a column alias, so that `SELECT <expr> alias`
|
||||
|
@ -1072,7 +1130,7 @@ pub const RESERVED_FOR_COLUMN_ALIAS: &[Keyword] = &[
|
|||
Keyword::END,
|
||||
];
|
||||
|
||||
// Global list of reserved keywords alloweed after FROM.
|
||||
// Global list of reserved keywords allowed after FROM.
|
||||
// Parser should call Dialect::get_reserved_keyword_after_from
|
||||
// to allow for each dialect to customize the list.
|
||||
pub const RESERVED_FOR_TABLE_FACTOR: &[Keyword] = &[
|
||||
|
|
26
src/lib.rs
26
src/lib.rs
|
@ -64,6 +64,27 @@
|
|||
//! // The original SQL text can be generated from the AST
|
||||
//! assert_eq!(ast[0].to_string(), sql);
|
||||
//! ```
|
||||
//!
|
||||
//! # Pretty Printing
|
||||
//!
|
||||
//! SQL statements can be pretty-printed with proper indentation and line breaks using the alternate flag (`{:#}`):
|
||||
//!
|
||||
//! ```
|
||||
//! # use sqlparser::dialect::GenericDialect;
|
||||
//! # use sqlparser::parser::Parser;
|
||||
//! let sql = "SELECT a, b FROM table_1";
|
||||
//! let ast = Parser::parse_sql(&GenericDialect, sql).unwrap();
|
||||
//!
|
||||
//! // Pretty print with indentation and line breaks
|
||||
//! let pretty_sql = format!("{:#}", ast[0]);
|
||||
//! assert_eq!(pretty_sql, r#"
|
||||
//! SELECT
|
||||
//! a,
|
||||
//! b
|
||||
//! FROM
|
||||
//! table_1
|
||||
//! "#.trim());
|
||||
//! ```
|
||||
//! [sqlparser crates.io page]: https://crates.io/crates/sqlparser
|
||||
//! [`Parser::parse_sql`]: crate::parser::Parser::parse_sql
|
||||
//! [`Parser::new`]: crate::parser::Parser::new
|
||||
|
@ -128,6 +149,10 @@
|
|||
|
||||
#![cfg_attr(not(feature = "std"), no_std)]
|
||||
#![allow(clippy::upper_case_acronyms)]
|
||||
// Permit large enum variants to keep a unified, expressive AST.
|
||||
// Splitting complex nodes (expressions, statements, types) into separate types
|
||||
// would bloat the API and hide intent. Extra memory is a worthwhile tradeoff.
|
||||
#![allow(clippy::large_enum_variant)]
|
||||
|
||||
// Allow proc-macros to find this crate
|
||||
extern crate self as sqlparser;
|
||||
|
@ -142,6 +167,7 @@ extern crate pretty_assertions;
|
|||
pub mod ast;
|
||||
#[macro_use]
|
||||
pub mod dialect;
|
||||
mod display_utils;
|
||||
pub mod keywords;
|
||||
pub mod parser;
|
||||
pub mod tokenizer;
|
||||
|
|
2784
src/parser/mod.rs
2784
src/parser/mod.rs
File diff suppressed because it is too large
Load diff
|
@ -151,6 +151,8 @@ impl TestedDialects {
|
|||
///
|
||||
/// 2. re-serializing the result of parsing `sql` produces the same
|
||||
/// `canonical` sql string
|
||||
///
|
||||
/// For multiple statements, use [`statements_parse_to`].
|
||||
pub fn one_statement_parses_to(&self, sql: &str, canonical: &str) -> Statement {
|
||||
let mut statements = self.parse_sql_statements(sql).expect(sql);
|
||||
assert_eq!(statements.len(), 1);
|
||||
|
@ -166,6 +168,24 @@ impl TestedDialects {
|
|||
only_statement
|
||||
}
|
||||
|
||||
/// The same as [`one_statement_parses_to`] but it works for a multiple statements
|
||||
pub fn statements_parse_to(&self, sql: &str, canonical: &str) -> Vec<Statement> {
|
||||
let statements = self.parse_sql_statements(sql).expect(sql);
|
||||
if !canonical.is_empty() && sql != canonical {
|
||||
assert_eq!(self.parse_sql_statements(canonical).unwrap(), statements);
|
||||
} else {
|
||||
assert_eq!(
|
||||
sql,
|
||||
statements
|
||||
.iter()
|
||||
.map(|s| s.to_string())
|
||||
.collect::<Vec<_>>()
|
||||
.join("; ")
|
||||
);
|
||||
}
|
||||
statements
|
||||
}
|
||||
|
||||
/// Ensures that `sql` parses as an [`Expr`], and that
|
||||
/// re-serializing the parse result produces canonical
|
||||
pub fn expr_parses_to(&self, sql: &str, canonical: &str) -> Expr {
|
||||
|
@ -250,7 +270,7 @@ impl TestedDialects {
|
|||
tokenizer = tokenizer.with_unescape(options.unescape);
|
||||
}
|
||||
let tokens = tokenizer.tokenize().unwrap();
|
||||
assert_eq!(expected, tokens, "Tokenized differently for {:?}", dialect);
|
||||
assert_eq!(expected, tokens, "Tokenized differently for {dialect:?}");
|
||||
});
|
||||
}
|
||||
}
|
||||
|
@ -325,10 +345,12 @@ pub fn alter_table_op_with_name(stmt: Statement, expected_name: &str) -> AlterTa
|
|||
operations,
|
||||
on_cluster: _,
|
||||
location: _,
|
||||
iceberg,
|
||||
} => {
|
||||
assert_eq!(name.to_string(), expected_name);
|
||||
assert!(!if_exists);
|
||||
assert!(!is_only);
|
||||
assert!(!iceberg);
|
||||
only(operations)
|
||||
}
|
||||
_ => panic!("Expected ALTER TABLE statement"),
|
||||
|
@ -344,6 +366,11 @@ pub fn number(n: &str) -> Value {
|
|||
Value::Number(n.parse().unwrap(), false)
|
||||
}
|
||||
|
||||
/// Creates a [Value::SingleQuotedString]
|
||||
pub fn single_quoted_string(s: impl Into<String>) -> Value {
|
||||
Value::SingleQuotedString(s.into())
|
||||
}
|
||||
|
||||
pub fn table_alias(name: impl Into<String>) -> Option<TableAlias> {
|
||||
Some(TableAlias {
|
||||
name: Ident::new(name),
|
||||
|
@ -426,3 +453,52 @@ pub fn call(function: &str, args: impl IntoIterator<Item = Expr>) -> Expr {
|
|||
within_group: vec![],
|
||||
})
|
||||
}
|
||||
|
||||
/// Gets the first index column (mysql calls it a key part) of the first index found in a
|
||||
/// [`Statement::CreateIndex`], [`Statement::CreateTable`], or [`Statement::AlterTable`].
|
||||
pub fn index_column(stmt: Statement) -> Expr {
|
||||
match stmt {
|
||||
Statement::CreateIndex(CreateIndex { columns, .. }) => {
|
||||
columns.first().unwrap().column.expr.clone()
|
||||
}
|
||||
Statement::CreateTable(CreateTable { constraints, .. }) => {
|
||||
match constraints.first().unwrap() {
|
||||
TableConstraint::Index { columns, .. } => {
|
||||
columns.first().unwrap().column.expr.clone()
|
||||
}
|
||||
TableConstraint::Unique { columns, .. } => {
|
||||
columns.first().unwrap().column.expr.clone()
|
||||
}
|
||||
TableConstraint::PrimaryKey { columns, .. } => {
|
||||
columns.first().unwrap().column.expr.clone()
|
||||
}
|
||||
TableConstraint::FulltextOrSpatial { columns, .. } => {
|
||||
columns.first().unwrap().column.expr.clone()
|
||||
}
|
||||
_ => panic!("Expected an index, unique, primary, full text, or spatial constraint (foreign key does not support general key part expressions)"),
|
||||
}
|
||||
}
|
||||
Statement::AlterTable { operations, .. } => match operations.first().unwrap() {
|
||||
AlterTableOperation::AddConstraint { constraint, .. } => {
|
||||
match constraint {
|
||||
TableConstraint::Index { columns, .. } => {
|
||||
columns.first().unwrap().column.expr.clone()
|
||||
}
|
||||
TableConstraint::Unique { columns, .. } => {
|
||||
columns.first().unwrap().column.expr.clone()
|
||||
}
|
||||
TableConstraint::PrimaryKey { columns, .. } => {
|
||||
columns.first().unwrap().column.expr.clone()
|
||||
}
|
||||
TableConstraint::FulltextOrSpatial {
|
||||
columns,
|
||||
..
|
||||
} => columns.first().unwrap().column.expr.clone(),
|
||||
_ => panic!("Expected an index, unique, primary, full text, or spatial constraint (foreign key does not support general key part expressions)"),
|
||||
}
|
||||
}
|
||||
_ => panic!("Expected a constraint"),
|
||||
},
|
||||
_ => panic!("Expected CREATE INDEX, ALTER TABLE, or CREATE TABLE, got: {stmt:?}"),
|
||||
}
|
||||
}
|
||||
|
|
156
src/tokenizer.rs
156
src/tokenizer.rs
|
@ -246,6 +246,8 @@ pub enum Token {
|
|||
ShiftLeftVerticalBar,
|
||||
/// `|>> PostgreSQL/Redshift geometrical binary operator (Is strictly above?)
|
||||
VerticalBarShiftRight,
|
||||
/// `|> BigQuery pipe operator
|
||||
VerticalBarRightAngleBracket,
|
||||
/// `#>>`, extracts JSON sub-object at the specified path as text
|
||||
HashLongArrow,
|
||||
/// jsonb @> jsonb -> boolean: Test whether left json contains the right json
|
||||
|
@ -359,6 +361,7 @@ impl fmt::Display for Token {
|
|||
Token::AmpersandRightAngleBracket => f.write_str("&>"),
|
||||
Token::AmpersandLeftAngleBracketVerticalBar => f.write_str("&<|"),
|
||||
Token::VerticalBarAmpersandRightAngleBracket => f.write_str("|&>"),
|
||||
Token::VerticalBarRightAngleBracket => f.write_str("|>"),
|
||||
Token::TwoWayArrow => f.write_str("<->"),
|
||||
Token::LeftAngleBracketCaret => f.write_str("<^"),
|
||||
Token::RightAngleBracketCaret => f.write_str(">^"),
|
||||
|
@ -895,7 +898,7 @@ impl<'a> Tokenizer<'a> {
|
|||
};
|
||||
|
||||
let mut location = state.location();
|
||||
while let Some(token) = self.next_token(&mut state)? {
|
||||
while let Some(token) = self.next_token(&mut state, buf.last().map(|t| &t.token))? {
|
||||
let span = location.span_to(state.location());
|
||||
|
||||
buf.push(TokenWithSpan { token, span });
|
||||
|
@ -932,7 +935,11 @@ impl<'a> Tokenizer<'a> {
|
|||
}
|
||||
|
||||
/// Get the next token or return None
|
||||
fn next_token(&self, chars: &mut State) -> Result<Option<Token>, TokenizerError> {
|
||||
fn next_token(
|
||||
&self,
|
||||
chars: &mut State,
|
||||
prev_token: Option<&Token>,
|
||||
) -> Result<Option<Token>, TokenizerError> {
|
||||
match chars.peek() {
|
||||
Some(&ch) => match ch {
|
||||
' ' => self.consume_and_return(chars, Token::Whitespace(Whitespace::Space)),
|
||||
|
@ -1184,6 +1191,22 @@ impl<'a> Tokenizer<'a> {
|
|||
}
|
||||
// numbers and period
|
||||
'0'..='9' | '.' => {
|
||||
// special case where if ._ is encountered after a word then that word
|
||||
// is a table and the _ is the start of the col name.
|
||||
// if the prev token is not a word, then this is not a valid sql
|
||||
// word or number.
|
||||
if ch == '.' && chars.peekable.clone().nth(1) == Some('_') {
|
||||
if let Some(Token::Word(_)) = prev_token {
|
||||
chars.next();
|
||||
return Ok(Some(Token::Period));
|
||||
}
|
||||
|
||||
return self.tokenizer_error(
|
||||
chars.location(),
|
||||
"Unexpected character '_'".to_string(),
|
||||
);
|
||||
}
|
||||
|
||||
// Some dialects support underscore as number separator
|
||||
// There can only be one at a time and it must be followed by another digit
|
||||
let is_number_separator = |ch: char, next_char: Option<char>| {
|
||||
|
@ -1211,17 +1234,29 @@ impl<'a> Tokenizer<'a> {
|
|||
chars.next();
|
||||
}
|
||||
|
||||
// If the dialect supports identifiers that start with a numeric prefix
|
||||
// and we have now consumed a dot, check if the previous token was a Word.
|
||||
// If so, what follows is definitely not part of a decimal number and
|
||||
// we should yield the dot as a dedicated token so compound identifiers
|
||||
// starting with digits can be parsed correctly.
|
||||
if s == "." && self.dialect.supports_numeric_prefix() {
|
||||
if let Some(Token::Word(_)) = prev_token {
|
||||
return Ok(Some(Token::Period));
|
||||
}
|
||||
}
|
||||
|
||||
// Consume fractional digits.
|
||||
s += &peeking_next_take_while(chars, |ch, next_ch| {
|
||||
ch.is_ascii_digit() || is_number_separator(ch, next_ch)
|
||||
});
|
||||
|
||||
// No number -> Token::Period
|
||||
// No fraction -> Token::Period
|
||||
if s == "." {
|
||||
return Ok(Some(Token::Period));
|
||||
}
|
||||
|
||||
let mut exponent_part = String::new();
|
||||
// Parse exponent as number
|
||||
let mut exponent_part = String::new();
|
||||
if chars.peek() == Some(&'e') || chars.peek() == Some(&'E') {
|
||||
let mut char_clone = chars.peekable.clone();
|
||||
exponent_part.push(char_clone.next().unwrap());
|
||||
|
@ -1250,9 +1285,13 @@ impl<'a> Tokenizer<'a> {
|
|||
}
|
||||
}
|
||||
|
||||
// mysql dialect supports identifiers that start with a numeric prefix,
|
||||
// as long as they aren't an exponent number.
|
||||
if self.dialect.supports_numeric_prefix() && exponent_part.is_empty() {
|
||||
// If the dialect supports identifiers that start with a numeric prefix,
|
||||
// we need to check if the value is in fact an identifier and must thus
|
||||
// be tokenized as a word.
|
||||
if self.dialect.supports_numeric_prefix() {
|
||||
if exponent_part.is_empty() {
|
||||
// If it is not a number with an exponent, it may be
|
||||
// an identifier starting with digits.
|
||||
let word =
|
||||
peeking_take_while(chars, |ch| self.dialect.is_identifier_part(ch));
|
||||
|
||||
|
@ -1260,6 +1299,11 @@ impl<'a> Tokenizer<'a> {
|
|||
s += word.as_str();
|
||||
return Ok(Some(Token::make_word(s.as_str(), None)));
|
||||
}
|
||||
} else if prev_token == Some(&Token::Period) {
|
||||
// If the previous token was a period, thus not belonging to a number,
|
||||
// the value we have is part of an identifier.
|
||||
return Ok(Some(Token::make_word(s.as_str(), None)));
|
||||
}
|
||||
}
|
||||
|
||||
let long = if chars.peek() == Some(&'L') {
|
||||
|
@ -1378,6 +1422,9 @@ impl<'a> Tokenizer<'a> {
|
|||
_ => self.start_binop_opt(chars, "|>", None),
|
||||
}
|
||||
}
|
||||
Some('>') if self.dialect.supports_pipe_operator() => {
|
||||
self.consume_for_binop(chars, "|>", Token::VerticalBarRightAngleBracket)
|
||||
}
|
||||
// Bitshift '|' operator
|
||||
_ => self.start_binop(chars, "|", Token::Pipe),
|
||||
}
|
||||
|
@ -1704,7 +1751,7 @@ impl<'a> Tokenizer<'a> {
|
|||
(None, Some(tok)) => Ok(Some(tok)),
|
||||
(None, None) => self.tokenizer_error(
|
||||
chars.location(),
|
||||
format!("Expected a valid binary operator after '{}'", prefix),
|
||||
format!("Expected a valid binary operator after '{prefix}'"),
|
||||
),
|
||||
}
|
||||
}
|
||||
|
@ -1762,7 +1809,7 @@ impl<'a> Tokenizer<'a> {
|
|||
chars.next();
|
||||
|
||||
let mut temp = String::new();
|
||||
let end_delimiter = format!("${}$", value);
|
||||
let end_delimiter = format!("${value}$");
|
||||
|
||||
loop {
|
||||
match chars.next() {
|
||||
|
@ -2011,8 +2058,13 @@ impl<'a> Tokenizer<'a> {
|
|||
num_consecutive_quotes = 0;
|
||||
|
||||
if let Some(next) = chars.peek() {
|
||||
if !self.unescape {
|
||||
// In no-escape mode, the given query has to be saved completely including backslashes.
|
||||
if !self.unescape
|
||||
|| (self.dialect.ignores_wildcard_escapes()
|
||||
&& (*next == '%' || *next == '_'))
|
||||
{
|
||||
// In no-escape mode, the given query has to be saved completely
|
||||
// including backslashes. Similarly, with ignore_like_wildcard_escapes,
|
||||
// the backslash is not stripped.
|
||||
s.push(ch);
|
||||
s.push(*next);
|
||||
chars.next(); // consume next
|
||||
|
@ -2350,13 +2402,13 @@ fn take_char_from_hex_digits(
|
|||
location: chars.location(),
|
||||
})?;
|
||||
let digit = next_char.to_digit(16).ok_or_else(|| TokenizerError {
|
||||
message: format!("Invalid hex digit in escaped unicode string: {}", next_char),
|
||||
message: format!("Invalid hex digit in escaped unicode string: {next_char}"),
|
||||
location: chars.location(),
|
||||
})?;
|
||||
result = result * 16 + digit;
|
||||
}
|
||||
char::from_u32(result).ok_or_else(|| TokenizerError {
|
||||
message: format!("Invalid unicode character: {:x}", result),
|
||||
message: format!("Invalid unicode character: {result:x}"),
|
||||
location: chars.location(),
|
||||
})
|
||||
}
|
||||
|
@ -3452,7 +3504,7 @@ mod tests {
|
|||
}
|
||||
|
||||
fn check_unescape(s: &str, expected: Option<&str>) {
|
||||
let s = format!("'{}'", s);
|
||||
let s = format!("'{s}'");
|
||||
let mut state = State {
|
||||
peekable: s.chars().peekable(),
|
||||
line: 0,
|
||||
|
@ -3585,6 +3637,9 @@ mod tests {
|
|||
(r#"'\\a\\b\'c'"#, r#"\\a\\b\'c"#, r#"\a\b'c"#),
|
||||
(r#"'\'abcd'"#, r#"\'abcd"#, r#"'abcd"#),
|
||||
(r#"'''a''b'"#, r#"''a''b"#, r#"'a'b"#),
|
||||
(r#"'\q'"#, r#"\q"#, r#"q"#),
|
||||
(r#"'\%\_'"#, r#"\%\_"#, r#"%_"#),
|
||||
(r#"'\\%\\_'"#, r#"\\%\\_"#, r#"\%\_"#),
|
||||
] {
|
||||
let tokens = Tokenizer::new(&dialect, sql)
|
||||
.with_unescape(false)
|
||||
|
@ -3618,6 +3673,16 @@ mod tests {
|
|||
|
||||
compare(expected, tokens);
|
||||
}
|
||||
|
||||
// MySQL special case for LIKE escapes
|
||||
for (sql, expected) in [(r#"'\%'"#, r#"\%"#), (r#"'\_'"#, r#"\_"#)] {
|
||||
let dialect = MySqlDialect {};
|
||||
let tokens = Tokenizer::new(&dialect, sql).tokenize().unwrap();
|
||||
|
||||
let expected = vec![Token::SingleQuotedString(expected.to_string())];
|
||||
|
||||
compare(expected, tokens);
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
|
@ -3942,4 +4007,67 @@ mod tests {
|
|||
],
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_tokenize_identifiers_numeric_prefix() {
|
||||
all_dialects_where(|dialect| dialect.supports_numeric_prefix())
|
||||
.tokenizes_to("123abc", vec![Token::make_word("123abc", None)]);
|
||||
|
||||
all_dialects_where(|dialect| dialect.supports_numeric_prefix())
|
||||
.tokenizes_to("12e34", vec![Token::Number("12e34".to_string(), false)]);
|
||||
|
||||
all_dialects_where(|dialect| dialect.supports_numeric_prefix()).tokenizes_to(
|
||||
"t.12e34",
|
||||
vec![
|
||||
Token::make_word("t", None),
|
||||
Token::Period,
|
||||
Token::make_word("12e34", None),
|
||||
],
|
||||
);
|
||||
|
||||
all_dialects_where(|dialect| dialect.supports_numeric_prefix()).tokenizes_to(
|
||||
"t.1two3",
|
||||
vec![
|
||||
Token::make_word("t", None),
|
||||
Token::Period,
|
||||
Token::make_word("1two3", None),
|
||||
],
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn tokenize_period_underscore() {
|
||||
let sql = String::from("SELECT table._col");
|
||||
// a dialect that supports underscores in numeric literals
|
||||
let dialect = PostgreSqlDialect {};
|
||||
let tokens = Tokenizer::new(&dialect, &sql).tokenize().unwrap();
|
||||
|
||||
let expected = vec![
|
||||
Token::make_keyword("SELECT"),
|
||||
Token::Whitespace(Whitespace::Space),
|
||||
Token::Word(Word {
|
||||
value: "table".to_string(),
|
||||
quote_style: None,
|
||||
keyword: Keyword::TABLE,
|
||||
}),
|
||||
Token::Period,
|
||||
Token::Word(Word {
|
||||
value: "_col".to_string(),
|
||||
quote_style: None,
|
||||
keyword: Keyword::NoKeyword,
|
||||
}),
|
||||
];
|
||||
|
||||
compare(expected, tokens);
|
||||
|
||||
let sql = String::from("SELECT ._123");
|
||||
if let Ok(tokens) = Tokenizer::new(&dialect, &sql).tokenize() {
|
||||
panic!("Tokenizer should have failed on {sql}, but it succeeded with {tokens:?}");
|
||||
}
|
||||
|
||||
let sql = String::from("SELECT ._abc");
|
||||
if let Ok(tokens) = Tokenizer::new(&dialect, &sql).tokenize() {
|
||||
panic!("Tokenizer should have failed on {sql}, but it succeeded with {tokens:?}");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
414
tests/pretty_print.rs
Normal file
414
tests/pretty_print.rs
Normal file
|
@ -0,0 +1,414 @@
|
|||
// Licensed to the Apache Software Foundation (ASF) under one
|
||||
// or more contributor license agreements. See the NOTICE file
|
||||
// distributed with this work for additional information
|
||||
// regarding copyright ownership. The ASF licenses this file
|
||||
// to you under the Apache License, Version 2.0 (the
|
||||
// "License"); you may not use this file except in compliance
|
||||
// with the License. You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing,
|
||||
// software distributed under the License is distributed on an
|
||||
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
||||
// KIND, either express or implied. See the License for the
|
||||
// specific language governing permissions and limitations
|
||||
// under the License.
|
||||
|
||||
use sqlparser::dialect::GenericDialect;
|
||||
use sqlparser::parser::Parser;
|
||||
|
||||
fn prettify(sql: &str) -> String {
|
||||
let ast = Parser::parse_sql(&GenericDialect {}, sql).unwrap();
|
||||
format!("{:#}", ast[0])
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_pretty_print_select() {
|
||||
assert_eq!(
|
||||
prettify("SELECT a, b, c FROM my_table WHERE x = 1 AND y = 2"),
|
||||
r#"
|
||||
SELECT
|
||||
a,
|
||||
b,
|
||||
c
|
||||
FROM
|
||||
my_table
|
||||
WHERE
|
||||
x = 1 AND y = 2
|
||||
"#
|
||||
.trim()
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_pretty_print_join() {
|
||||
assert_eq!(
|
||||
prettify("SELECT a FROM table1 JOIN table2 ON table1.id = table2.id"),
|
||||
r#"
|
||||
SELECT
|
||||
a
|
||||
FROM
|
||||
table1
|
||||
JOIN table2 ON table1.id = table2.id
|
||||
"#
|
||||
.trim()
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_pretty_print_subquery() {
|
||||
assert_eq!(
|
||||
prettify("SELECT * FROM (SELECT a, b FROM my_table) AS subquery"),
|
||||
r#"
|
||||
SELECT
|
||||
*
|
||||
FROM
|
||||
(
|
||||
SELECT
|
||||
a,
|
||||
b
|
||||
FROM
|
||||
my_table
|
||||
) AS subquery
|
||||
"#
|
||||
.trim()
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_pretty_print_union() {
|
||||
assert_eq!(
|
||||
prettify("SELECT a FROM table1 UNION SELECT b FROM table2"),
|
||||
r#"
|
||||
SELECT
|
||||
a
|
||||
FROM
|
||||
table1
|
||||
UNION
|
||||
SELECT
|
||||
b
|
||||
FROM
|
||||
table2
|
||||
"#
|
||||
.trim()
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_pretty_print_group_by() {
|
||||
assert_eq!(
|
||||
prettify("SELECT a, COUNT(*) FROM my_table GROUP BY a HAVING COUNT(*) > 1"),
|
||||
r#"
|
||||
SELECT
|
||||
a,
|
||||
COUNT(*)
|
||||
FROM
|
||||
my_table
|
||||
GROUP BY
|
||||
a
|
||||
HAVING
|
||||
COUNT(*) > 1
|
||||
"#
|
||||
.trim()
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_pretty_print_cte() {
|
||||
assert_eq!(
|
||||
prettify("WITH cte AS (SELECT a, b FROM my_table) SELECT * FROM cte"),
|
||||
r#"
|
||||
WITH cte AS (
|
||||
SELECT
|
||||
a,
|
||||
b
|
||||
FROM
|
||||
my_table
|
||||
)
|
||||
SELECT
|
||||
*
|
||||
FROM
|
||||
cte
|
||||
"#
|
||||
.trim()
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_pretty_print_case_when() {
|
||||
assert_eq!(
|
||||
prettify("SELECT CASE WHEN x > 0 THEN 'positive' WHEN x < 0 THEN 'negative' ELSE 'zero' END FROM my_table"),
|
||||
r#"
|
||||
SELECT
|
||||
CASE
|
||||
WHEN x > 0 THEN
|
||||
'positive'
|
||||
WHEN x < 0 THEN
|
||||
'negative'
|
||||
ELSE
|
||||
'zero'
|
||||
END
|
||||
FROM
|
||||
my_table
|
||||
"#.trim()
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_pretty_print_window_function() {
|
||||
assert_eq!(
|
||||
prettify("SELECT id, value, ROW_NUMBER() OVER (PARTITION BY category ORDER BY value DESC) as rank FROM my_table"),
|
||||
r#"
|
||||
SELECT
|
||||
id,
|
||||
value,
|
||||
ROW_NUMBER() OVER (
|
||||
PARTITION BY category
|
||||
ORDER BY value DESC
|
||||
) AS rank
|
||||
FROM
|
||||
my_table
|
||||
"#.trim()
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_pretty_print_multiline_string() {
|
||||
assert_eq!(
|
||||
prettify("SELECT 'multiline\nstring' AS str"),
|
||||
r#"
|
||||
SELECT
|
||||
'multiline
|
||||
string' AS str
|
||||
"#
|
||||
.trim(),
|
||||
"A literal string with a newline should be kept as is. The contents of the string should not be indented."
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_pretty_print_insert_values() {
|
||||
assert_eq!(
|
||||
prettify("INSERT INTO my_table (a, b, c) VALUES (1, 2, 3), (4, 5, 6)"),
|
||||
r#"
|
||||
INSERT INTO my_table (a, b, c)
|
||||
VALUES
|
||||
(1, 2, 3),
|
||||
(4, 5, 6)
|
||||
"#
|
||||
.trim()
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_pretty_print_insert_select() {
|
||||
assert_eq!(
|
||||
prettify("INSERT INTO my_table (a, b) SELECT x, y FROM source_table RETURNING a AS id"),
|
||||
r#"
|
||||
INSERT INTO my_table (a, b)
|
||||
SELECT
|
||||
x,
|
||||
y
|
||||
FROM
|
||||
source_table
|
||||
RETURNING
|
||||
a AS id
|
||||
"#
|
||||
.trim()
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_pretty_print_update() {
|
||||
assert_eq!(
|
||||
prettify("UPDATE my_table SET a = 1, b = 2 WHERE x > 0 RETURNING id, name"),
|
||||
r#"
|
||||
UPDATE my_table
|
||||
SET
|
||||
a = 1,
|
||||
b = 2
|
||||
WHERE
|
||||
x > 0
|
||||
RETURNING
|
||||
id,
|
||||
name
|
||||
"#
|
||||
.trim()
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_pretty_print_delete() {
|
||||
assert_eq!(
|
||||
prettify("DELETE FROM my_table WHERE x > 0 RETURNING id, name"),
|
||||
r#"
|
||||
DELETE FROM
|
||||
my_table
|
||||
WHERE
|
||||
x > 0
|
||||
RETURNING
|
||||
id,
|
||||
name
|
||||
"#
|
||||
.trim()
|
||||
);
|
||||
|
||||
assert_eq!(
|
||||
prettify("DELETE table1, table2"),
|
||||
r#"
|
||||
DELETE
|
||||
table1,
|
||||
table2
|
||||
"#
|
||||
.trim()
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_pretty_print_create_table() {
|
||||
assert_eq!(
|
||||
prettify("CREATE TABLE my_table (id INT PRIMARY KEY, name VARCHAR(255) NOT NULL, CONSTRAINT fk_other FOREIGN KEY (id) REFERENCES other_table(id))"),
|
||||
r#"
|
||||
CREATE TABLE my_table (
|
||||
id INT PRIMARY KEY,
|
||||
name VARCHAR(255) NOT NULL,
|
||||
CONSTRAINT fk_other FOREIGN KEY (id) REFERENCES other_table(id)
|
||||
)
|
||||
"#
|
||||
.trim()
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_pretty_print_create_view() {
|
||||
assert_eq!(
|
||||
prettify("CREATE VIEW my_view AS SELECT a, b FROM my_table WHERE x > 0"),
|
||||
r#"
|
||||
CREATE VIEW my_view AS
|
||||
SELECT
|
||||
a,
|
||||
b
|
||||
FROM
|
||||
my_table
|
||||
WHERE
|
||||
x > 0
|
||||
"#
|
||||
.trim()
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
#[ignore = "https://github.com/apache/datafusion-sqlparser-rs/issues/1850"]
|
||||
fn test_pretty_print_create_function() {
|
||||
assert_eq!(
|
||||
prettify("CREATE FUNCTION my_func() RETURNS INT BEGIN SELECT COUNT(*) INTO @count FROM my_table; RETURN @count; END"),
|
||||
r#"
|
||||
CREATE FUNCTION my_func() RETURNS INT
|
||||
BEGIN
|
||||
SELECT COUNT(*) INTO @count FROM my_table;
|
||||
RETURN @count;
|
||||
END
|
||||
"#
|
||||
.trim()
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
#[ignore = "https://github.com/apache/datafusion-sqlparser-rs/issues/1850"]
|
||||
fn test_pretty_print_json_table() {
|
||||
assert_eq!(
|
||||
prettify("SELECT * FROM JSON_TABLE(@json, '$[*]' COLUMNS (id INT PATH '$.id', name VARCHAR(255) PATH '$.name')) AS jt"),
|
||||
r#"
|
||||
SELECT
|
||||
*
|
||||
FROM
|
||||
JSON_TABLE(
|
||||
@json,
|
||||
'$[*]' COLUMNS (
|
||||
id INT PATH '$.id',
|
||||
name VARCHAR(255) PATH '$.name'
|
||||
)
|
||||
) AS jt
|
||||
"#
|
||||
.trim()
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
#[ignore = "https://github.com/apache/datafusion-sqlparser-rs/issues/1850"]
|
||||
fn test_pretty_print_transaction_blocks() {
|
||||
assert_eq!(
|
||||
prettify("BEGIN; UPDATE my_table SET x = 1; COMMIT;"),
|
||||
r#"
|
||||
BEGIN;
|
||||
UPDATE my_table SET x = 1;
|
||||
COMMIT;
|
||||
"#
|
||||
.trim()
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
#[ignore = "https://github.com/apache/datafusion-sqlparser-rs/issues/1850"]
|
||||
fn test_pretty_print_control_flow() {
|
||||
assert_eq!(
|
||||
prettify("IF x > 0 THEN SELECT 'positive'; ELSE SELECT 'negative'; END IF;"),
|
||||
r#"
|
||||
IF x > 0 THEN
|
||||
SELECT 'positive';
|
||||
ELSE
|
||||
SELECT 'negative';
|
||||
END IF;
|
||||
"#
|
||||
.trim()
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
#[ignore = "https://github.com/apache/datafusion-sqlparser-rs/issues/1850"]
|
||||
fn test_pretty_print_merge() {
|
||||
assert_eq!(
|
||||
prettify("MERGE INTO target_table t USING source_table s ON t.id = s.id WHEN MATCHED THEN UPDATE SET t.value = s.value WHEN NOT MATCHED THEN INSERT (id, value) VALUES (s.id, s.value)"),
|
||||
r#"
|
||||
MERGE INTO target_table t
|
||||
USING source_table s ON t.id = s.id
|
||||
WHEN MATCHED THEN
|
||||
UPDATE SET t.value = s.value
|
||||
WHEN NOT MATCHED THEN
|
||||
INSERT (id, value) VALUES (s.id, s.value)
|
||||
"#
|
||||
.trim()
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
#[ignore = "https://github.com/apache/datafusion-sqlparser-rs/issues/1850"]
|
||||
fn test_pretty_print_create_index() {
|
||||
assert_eq!(
|
||||
prettify("CREATE INDEX idx_name ON my_table (column1, column2)"),
|
||||
r#"
|
||||
CREATE INDEX idx_name
|
||||
ON my_table (column1, column2)
|
||||
"#
|
||||
.trim()
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
#[ignore = "https://github.com/apache/datafusion-sqlparser-rs/issues/1850"]
|
||||
fn test_pretty_print_explain() {
|
||||
assert_eq!(
|
||||
prettify("EXPLAIN ANALYZE SELECT * FROM my_table WHERE x > 0"),
|
||||
r#"
|
||||
EXPLAIN ANALYZE
|
||||
SELECT
|
||||
*
|
||||
FROM
|
||||
my_table
|
||||
WHERE
|
||||
x > 0
|
||||
"#
|
||||
.trim()
|
||||
);
|
||||
}
|
|
@ -261,10 +261,10 @@ fn parse_at_at_identifier() {
|
|||
|
||||
#[test]
|
||||
fn parse_begin() {
|
||||
let sql = r#"BEGIN SELECT 1; EXCEPTION WHEN ERROR THEN SELECT 2; END"#;
|
||||
let sql = r#"BEGIN SELECT 1; EXCEPTION WHEN ERROR THEN SELECT 2; RAISE USING MESSAGE = FORMAT('ERR: %s', 'Bad'); END"#;
|
||||
let Statement::StartTransaction {
|
||||
statements,
|
||||
exception_statements,
|
||||
exception,
|
||||
has_end_keyword,
|
||||
..
|
||||
} = bigquery().verified_stmt(sql)
|
||||
|
@ -272,7 +272,10 @@ fn parse_begin() {
|
|||
unreachable!();
|
||||
};
|
||||
assert_eq!(1, statements.len());
|
||||
assert_eq!(1, exception_statements.unwrap().len());
|
||||
assert!(exception.is_some());
|
||||
|
||||
let exception = exception.unwrap();
|
||||
assert_eq!(1, exception.len());
|
||||
assert!(has_end_keyword);
|
||||
|
||||
bigquery().verified_stmt(
|
||||
|
@ -352,14 +355,16 @@ fn parse_create_view_with_options() {
|
|||
ViewColumnDef {
|
||||
name: Ident::new("age"),
|
||||
data_type: None,
|
||||
options: Some(vec![ColumnOption::Options(vec![SqlOption::KeyValue {
|
||||
options: Some(ColumnOptions::CommaSeparated(vec![ColumnOption::Options(
|
||||
vec![SqlOption::KeyValue {
|
||||
key: Ident::new("description"),
|
||||
value: Expr::Value(
|
||||
Value::DoubleQuotedString("field age".to_string()).with_span(
|
||||
Span::new(Location::new(1, 42), Location::new(1, 52))
|
||||
)
|
||||
),
|
||||
}])]),
|
||||
}]
|
||||
)])),
|
||||
},
|
||||
],
|
||||
columns
|
||||
|
@ -484,7 +489,7 @@ fn parse_create_table_with_options() {
|
|||
columns,
|
||||
partition_by,
|
||||
cluster_by,
|
||||
options,
|
||||
table_options,
|
||||
..
|
||||
}) => {
|
||||
assert_eq!(
|
||||
|
@ -536,10 +541,10 @@ fn parse_create_table_with_options() {
|
|||
(
|
||||
Some(Box::new(Expr::Identifier(Ident::new("_PARTITIONDATE")))),
|
||||
Some(WrappedCollection::NoWrapping(vec![
|
||||
Ident::new("userid"),
|
||||
Ident::new("age"),
|
||||
Expr::Identifier(Ident::new("userid")),
|
||||
Expr::Identifier(Ident::new("age")),
|
||||
])),
|
||||
Some(vec![
|
||||
CreateTableOptions::Options(vec![
|
||||
SqlOption::KeyValue {
|
||||
key: Ident::new("partition_expiration_days"),
|
||||
value: Expr::Value(
|
||||
|
@ -561,7 +566,7 @@ fn parse_create_table_with_options() {
|
|||
},
|
||||
])
|
||||
),
|
||||
(partition_by, cluster_by, options)
|
||||
(partition_by, cluster_by, table_options)
|
||||
)
|
||||
}
|
||||
_ => unreachable!(),
|
||||
|
@ -601,11 +606,13 @@ fn parse_nested_data_types() {
|
|||
field_name: Some("a".into()),
|
||||
field_type: DataType::Array(ArrayElemTypeDef::AngleBracket(
|
||||
Box::new(DataType::Int64,)
|
||||
))
|
||||
)),
|
||||
options: None,
|
||||
},
|
||||
StructField {
|
||||
field_name: Some("b".into()),
|
||||
field_type: DataType::Bytes(Some(42))
|
||||
field_type: DataType::Bytes(Some(42)),
|
||||
options: None,
|
||||
},
|
||||
],
|
||||
StructBracketKind::AngleBrackets
|
||||
|
@ -619,6 +626,7 @@ fn parse_nested_data_types() {
|
|||
vec![StructField {
|
||||
field_name: None,
|
||||
field_type: DataType::Int64,
|
||||
options: None,
|
||||
}],
|
||||
StructBracketKind::AngleBrackets
|
||||
),
|
||||
|
@ -632,35 +640,6 @@ fn parse_nested_data_types() {
|
|||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn parse_invalid_brackets() {
|
||||
let sql = "SELECT STRUCT<INT64>>(NULL)";
|
||||
assert_eq!(
|
||||
bigquery_and_generic()
|
||||
.parse_sql_statements(sql)
|
||||
.unwrap_err(),
|
||||
ParserError::ParserError("unmatched > in STRUCT literal".to_string())
|
||||
);
|
||||
|
||||
let sql = "SELECT STRUCT<STRUCT<INT64>>>(NULL)";
|
||||
assert_eq!(
|
||||
bigquery_and_generic()
|
||||
.parse_sql_statements(sql)
|
||||
.unwrap_err(),
|
||||
ParserError::ParserError("Expected: (, found: >".to_string())
|
||||
);
|
||||
|
||||
let sql = "CREATE TABLE table (x STRUCT<STRUCT<INT64>>>)";
|
||||
assert_eq!(
|
||||
bigquery_and_generic()
|
||||
.parse_sql_statements(sql)
|
||||
.unwrap_err(),
|
||||
ParserError::ParserError(
|
||||
"Expected: ',' or ')' after column definition, found: >".to_string()
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn parse_tuple_struct_literal() {
|
||||
// tuple syntax: https://cloud.google.com/bigquery/docs/reference/standard-sql/data-types#tuple_syntax
|
||||
|
@ -771,6 +750,7 @@ fn parse_typed_struct_syntax_bigquery() {
|
|||
fields: vec![StructField {
|
||||
field_name: None,
|
||||
field_type: DataType::Int64,
|
||||
options: None,
|
||||
}]
|
||||
},
|
||||
expr_from_projection(&select.projection[0])
|
||||
|
@ -799,7 +779,8 @@ fn parse_typed_struct_syntax_bigquery() {
|
|||
quote_style: None,
|
||||
span: Span::empty(),
|
||||
}),
|
||||
field_type: DataType::Int64
|
||||
field_type: DataType::Int64,
|
||||
options: None,
|
||||
},
|
||||
StructField {
|
||||
field_name: Some(Ident {
|
||||
|
@ -807,7 +788,8 @@ fn parse_typed_struct_syntax_bigquery() {
|
|||
quote_style: None,
|
||||
span: Span::empty(),
|
||||
}),
|
||||
field_type: DataType::String(None)
|
||||
field_type: DataType::String(None),
|
||||
options: None,
|
||||
},
|
||||
]
|
||||
},
|
||||
|
@ -825,17 +807,20 @@ fn parse_typed_struct_syntax_bigquery() {
|
|||
field_name: Some("arr".into()),
|
||||
field_type: DataType::Array(ArrayElemTypeDef::AngleBracket(Box::new(
|
||||
DataType::Float64
|
||||
)))
|
||||
))),
|
||||
options: None,
|
||||
},
|
||||
StructField {
|
||||
field_name: Some("str".into()),
|
||||
field_type: DataType::Struct(
|
||||
vec![StructField {
|
||||
field_name: None,
|
||||
field_type: DataType::Bool
|
||||
field_type: DataType::Bool,
|
||||
options: None,
|
||||
}],
|
||||
StructBracketKind::AngleBrackets
|
||||
)
|
||||
),
|
||||
options: None,
|
||||
},
|
||||
]
|
||||
},
|
||||
|
@ -858,13 +843,15 @@ fn parse_typed_struct_syntax_bigquery() {
|
|||
field_type: DataType::Struct(
|
||||
Default::default(),
|
||||
StructBracketKind::AngleBrackets
|
||||
)
|
||||
),
|
||||
options: None,
|
||||
},
|
||||
StructField {
|
||||
field_name: Some("y".into()),
|
||||
field_type: DataType::Array(ArrayElemTypeDef::AngleBracket(Box::new(
|
||||
DataType::Struct(Default::default(), StructBracketKind::AngleBrackets)
|
||||
)))
|
||||
))),
|
||||
options: None,
|
||||
},
|
||||
]
|
||||
},
|
||||
|
@ -879,7 +866,8 @@ fn parse_typed_struct_syntax_bigquery() {
|
|||
values: vec![Expr::Value(Value::Boolean(true).with_empty_span())],
|
||||
fields: vec![StructField {
|
||||
field_name: None,
|
||||
field_type: DataType::Bool
|
||||
field_type: DataType::Bool,
|
||||
options: None,
|
||||
}]
|
||||
},
|
||||
expr_from_projection(&select.projection[0])
|
||||
|
@ -891,7 +879,8 @@ fn parse_typed_struct_syntax_bigquery() {
|
|||
)],
|
||||
fields: vec![StructField {
|
||||
field_name: None,
|
||||
field_type: DataType::Bytes(Some(42))
|
||||
field_type: DataType::Bytes(Some(42)),
|
||||
options: None,
|
||||
}]
|
||||
},
|
||||
expr_from_projection(&select.projection[1])
|
||||
|
@ -907,7 +896,8 @@ fn parse_typed_struct_syntax_bigquery() {
|
|||
)],
|
||||
fields: vec![StructField {
|
||||
field_name: None,
|
||||
field_type: DataType::Date
|
||||
field_type: DataType::Date,
|
||||
options: None,
|
||||
}]
|
||||
},
|
||||
expr_from_projection(&select.projection[0])
|
||||
|
@ -916,11 +906,15 @@ fn parse_typed_struct_syntax_bigquery() {
|
|||
&Expr::Struct {
|
||||
values: vec![Expr::TypedString {
|
||||
data_type: DataType::Datetime(None),
|
||||
value: Value::SingleQuotedString("1999-01-01 01:23:34.45".into())
|
||||
value: ValueWithSpan {
|
||||
value: Value::SingleQuotedString("1999-01-01 01:23:34.45".into()),
|
||||
span: Span::empty(),
|
||||
},
|
||||
}],
|
||||
fields: vec![StructField {
|
||||
field_name: None,
|
||||
field_type: DataType::Datetime(None)
|
||||
field_type: DataType::Datetime(None),
|
||||
options: None,
|
||||
}]
|
||||
},
|
||||
expr_from_projection(&select.projection[1])
|
||||
|
@ -930,7 +924,8 @@ fn parse_typed_struct_syntax_bigquery() {
|
|||
values: vec![Expr::value(number("5.0"))],
|
||||
fields: vec![StructField {
|
||||
field_name: None,
|
||||
field_type: DataType::Float64
|
||||
field_type: DataType::Float64,
|
||||
options: None,
|
||||
}]
|
||||
},
|
||||
expr_from_projection(&select.projection[2])
|
||||
|
@ -940,7 +935,8 @@ fn parse_typed_struct_syntax_bigquery() {
|
|||
values: vec![Expr::value(number("1"))],
|
||||
fields: vec![StructField {
|
||||
field_name: None,
|
||||
field_type: DataType::Int64
|
||||
field_type: DataType::Int64,
|
||||
options: None,
|
||||
}]
|
||||
},
|
||||
expr_from_projection(&select.projection[3])
|
||||
|
@ -962,7 +958,8 @@ fn parse_typed_struct_syntax_bigquery() {
|
|||
})],
|
||||
fields: vec![StructField {
|
||||
field_name: None,
|
||||
field_type: DataType::Interval
|
||||
field_type: DataType::Interval,
|
||||
options: None,
|
||||
}]
|
||||
},
|
||||
expr_from_projection(&select.projection[0])
|
||||
|
@ -971,13 +968,17 @@ fn parse_typed_struct_syntax_bigquery() {
|
|||
&Expr::Struct {
|
||||
values: vec![Expr::TypedString {
|
||||
data_type: DataType::JSON,
|
||||
value: ValueWithSpan {
|
||||
value: Value::SingleQuotedString(
|
||||
r#"{"class" : {"students" : [{"name" : "Jane"}]}}"#.into()
|
||||
)
|
||||
),
|
||||
span: Span::empty(),
|
||||
}
|
||||
}],
|
||||
fields: vec![StructField {
|
||||
field_name: None,
|
||||
field_type: DataType::JSON
|
||||
field_type: DataType::JSON,
|
||||
options: None,
|
||||
}]
|
||||
},
|
||||
expr_from_projection(&select.projection[1])
|
||||
|
@ -993,7 +994,8 @@ fn parse_typed_struct_syntax_bigquery() {
|
|||
)],
|
||||
fields: vec![StructField {
|
||||
field_name: None,
|
||||
field_type: DataType::String(Some(42))
|
||||
field_type: DataType::String(Some(42)),
|
||||
options: None,
|
||||
}]
|
||||
},
|
||||
expr_from_projection(&select.projection[0])
|
||||
|
@ -1002,11 +1004,17 @@ fn parse_typed_struct_syntax_bigquery() {
|
|||
&Expr::Struct {
|
||||
values: vec![Expr::TypedString {
|
||||
data_type: DataType::Timestamp(None, TimezoneInfo::None),
|
||||
value: Value::SingleQuotedString("2008-12-25 15:30:00 America/Los_Angeles".into())
|
||||
value: ValueWithSpan {
|
||||
value: Value::SingleQuotedString(
|
||||
"2008-12-25 15:30:00 America/Los_Angeles".into()
|
||||
),
|
||||
span: Span::empty(),
|
||||
},
|
||||
}],
|
||||
fields: vec![StructField {
|
||||
field_name: None,
|
||||
field_type: DataType::Timestamp(None, TimezoneInfo::None)
|
||||
field_type: DataType::Timestamp(None, TimezoneInfo::None),
|
||||
options: None,
|
||||
}]
|
||||
},
|
||||
expr_from_projection(&select.projection[1])
|
||||
|
@ -1016,11 +1024,15 @@ fn parse_typed_struct_syntax_bigquery() {
|
|||
&Expr::Struct {
|
||||
values: vec![Expr::TypedString {
|
||||
data_type: DataType::Time(None, TimezoneInfo::None),
|
||||
value: Value::SingleQuotedString("15:30:00".into())
|
||||
value: ValueWithSpan {
|
||||
value: Value::SingleQuotedString("15:30:00".into()),
|
||||
span: Span::empty(),
|
||||
}
|
||||
}],
|
||||
fields: vec![StructField {
|
||||
field_name: None,
|
||||
field_type: DataType::Time(None, TimezoneInfo::None)
|
||||
field_type: DataType::Time(None, TimezoneInfo::None),
|
||||
options: None,
|
||||
}]
|
||||
},
|
||||
expr_from_projection(&select.projection[2])
|
||||
|
@ -1033,11 +1045,15 @@ fn parse_typed_struct_syntax_bigquery() {
|
|||
&Expr::Struct {
|
||||
values: vec![Expr::TypedString {
|
||||
data_type: DataType::Numeric(ExactNumberInfo::None),
|
||||
value: Value::SingleQuotedString("1".into())
|
||||
value: ValueWithSpan {
|
||||
value: Value::SingleQuotedString("1".into()),
|
||||
span: Span::empty(),
|
||||
}
|
||||
}],
|
||||
fields: vec![StructField {
|
||||
field_name: None,
|
||||
field_type: DataType::Numeric(ExactNumberInfo::None)
|
||||
field_type: DataType::Numeric(ExactNumberInfo::None),
|
||||
options: None,
|
||||
}]
|
||||
},
|
||||
expr_from_projection(&select.projection[0])
|
||||
|
@ -1046,11 +1062,15 @@ fn parse_typed_struct_syntax_bigquery() {
|
|||
&Expr::Struct {
|
||||
values: vec![Expr::TypedString {
|
||||
data_type: DataType::BigNumeric(ExactNumberInfo::None),
|
||||
value: Value::SingleQuotedString("1".into())
|
||||
value: ValueWithSpan {
|
||||
value: Value::SingleQuotedString("1".into()),
|
||||
span: Span::empty(),
|
||||
}
|
||||
}],
|
||||
fields: vec![StructField {
|
||||
field_name: None,
|
||||
field_type: DataType::BigNumeric(ExactNumberInfo::None)
|
||||
field_type: DataType::BigNumeric(ExactNumberInfo::None),
|
||||
options: None,
|
||||
}]
|
||||
},
|
||||
expr_from_projection(&select.projection[1])
|
||||
|
@ -1067,10 +1087,12 @@ fn parse_typed_struct_syntax_bigquery() {
|
|||
StructField {
|
||||
field_name: Some("key".into()),
|
||||
field_type: DataType::Int64,
|
||||
options: None,
|
||||
},
|
||||
StructField {
|
||||
field_name: Some("value".into()),
|
||||
field_type: DataType::Int64,
|
||||
options: None,
|
||||
},
|
||||
]
|
||||
},
|
||||
|
@ -1092,6 +1114,7 @@ fn parse_typed_struct_syntax_bigquery_and_generic() {
|
|||
fields: vec![StructField {
|
||||
field_name: None,
|
||||
field_type: DataType::Int64,
|
||||
options: None,
|
||||
}]
|
||||
},
|
||||
expr_from_projection(&select.projection[0])
|
||||
|
@ -1120,7 +1143,8 @@ fn parse_typed_struct_syntax_bigquery_and_generic() {
|
|||
quote_style: None,
|
||||
span: Span::empty(),
|
||||
}),
|
||||
field_type: DataType::Int64
|
||||
field_type: DataType::Int64,
|
||||
options: None,
|
||||
},
|
||||
StructField {
|
||||
field_name: Some(Ident {
|
||||
|
@ -1128,7 +1152,8 @@ fn parse_typed_struct_syntax_bigquery_and_generic() {
|
|||
quote_style: None,
|
||||
span: Span::empty(),
|
||||
}),
|
||||
field_type: DataType::String(None)
|
||||
field_type: DataType::String(None),
|
||||
options: None,
|
||||
},
|
||||
]
|
||||
},
|
||||
|
@ -1151,13 +1176,15 @@ fn parse_typed_struct_syntax_bigquery_and_generic() {
|
|||
field_type: DataType::Struct(
|
||||
Default::default(),
|
||||
StructBracketKind::AngleBrackets
|
||||
)
|
||||
),
|
||||
options: None,
|
||||
},
|
||||
StructField {
|
||||
field_name: Some("y".into()),
|
||||
field_type: DataType::Array(ArrayElemTypeDef::AngleBracket(Box::new(
|
||||
DataType::Struct(Default::default(), StructBracketKind::AngleBrackets)
|
||||
)))
|
||||
))),
|
||||
options: None,
|
||||
},
|
||||
]
|
||||
},
|
||||
|
@ -1172,7 +1199,8 @@ fn parse_typed_struct_syntax_bigquery_and_generic() {
|
|||
values: vec![Expr::Value(Value::Boolean(true).with_empty_span())],
|
||||
fields: vec![StructField {
|
||||
field_name: None,
|
||||
field_type: DataType::Bool
|
||||
field_type: DataType::Bool,
|
||||
options: None,
|
||||
}]
|
||||
},
|
||||
expr_from_projection(&select.projection[0])
|
||||
|
@ -1184,7 +1212,8 @@ fn parse_typed_struct_syntax_bigquery_and_generic() {
|
|||
)],
|
||||
fields: vec![StructField {
|
||||
field_name: None,
|
||||
field_type: DataType::Bytes(Some(42))
|
||||
field_type: DataType::Bytes(Some(42)),
|
||||
options: None,
|
||||
}]
|
||||
},
|
||||
expr_from_projection(&select.projection[1])
|
||||
|
@ -1200,7 +1229,8 @@ fn parse_typed_struct_syntax_bigquery_and_generic() {
|
|||
)],
|
||||
fields: vec![StructField {
|
||||
field_name: None,
|
||||
field_type: DataType::Date
|
||||
field_type: DataType::Date,
|
||||
options: None,
|
||||
}]
|
||||
},
|
||||
expr_from_projection(&select.projection[0])
|
||||
|
@ -1209,11 +1239,15 @@ fn parse_typed_struct_syntax_bigquery_and_generic() {
|
|||
&Expr::Struct {
|
||||
values: vec![Expr::TypedString {
|
||||
data_type: DataType::Datetime(None),
|
||||
value: Value::SingleQuotedString("1999-01-01 01:23:34.45".into())
|
||||
value: ValueWithSpan {
|
||||
value: Value::SingleQuotedString("1999-01-01 01:23:34.45".into()),
|
||||
span: Span::empty(),
|
||||
}
|
||||
}],
|
||||
fields: vec![StructField {
|
||||
field_name: None,
|
||||
field_type: DataType::Datetime(None)
|
||||
field_type: DataType::Datetime(None),
|
||||
options: None,
|
||||
}]
|
||||
},
|
||||
expr_from_projection(&select.projection[1])
|
||||
|
@ -1223,7 +1257,8 @@ fn parse_typed_struct_syntax_bigquery_and_generic() {
|
|||
values: vec![Expr::value(number("5.0"))],
|
||||
fields: vec![StructField {
|
||||
field_name: None,
|
||||
field_type: DataType::Float64
|
||||
field_type: DataType::Float64,
|
||||
options: None,
|
||||
}]
|
||||
},
|
||||
expr_from_projection(&select.projection[2])
|
||||
|
@ -1233,7 +1268,8 @@ fn parse_typed_struct_syntax_bigquery_and_generic() {
|
|||
values: vec![Expr::value(number("1"))],
|
||||
fields: vec![StructField {
|
||||
field_name: None,
|
||||
field_type: DataType::Int64
|
||||
field_type: DataType::Int64,
|
||||
options: None,
|
||||
}]
|
||||
},
|
||||
expr_from_projection(&select.projection[3])
|
||||
|
@ -1255,7 +1291,8 @@ fn parse_typed_struct_syntax_bigquery_and_generic() {
|
|||
})],
|
||||
fields: vec![StructField {
|
||||
field_name: None,
|
||||
field_type: DataType::Interval
|
||||
field_type: DataType::Interval,
|
||||
options: None,
|
||||
}]
|
||||
},
|
||||
expr_from_projection(&select.projection[0])
|
||||
|
@ -1264,13 +1301,17 @@ fn parse_typed_struct_syntax_bigquery_and_generic() {
|
|||
&Expr::Struct {
|
||||
values: vec![Expr::TypedString {
|
||||
data_type: DataType::JSON,
|
||||
value: ValueWithSpan {
|
||||
value: Value::SingleQuotedString(
|
||||
r#"{"class" : {"students" : [{"name" : "Jane"}]}}"#.into()
|
||||
)
|
||||
),
|
||||
span: Span::empty(),
|
||||
}
|
||||
}],
|
||||
fields: vec![StructField {
|
||||
field_name: None,
|
||||
field_type: DataType::JSON
|
||||
field_type: DataType::JSON,
|
||||
options: None,
|
||||
}]
|
||||
},
|
||||
expr_from_projection(&select.projection[1])
|
||||
|
@ -1286,7 +1327,8 @@ fn parse_typed_struct_syntax_bigquery_and_generic() {
|
|||
)],
|
||||
fields: vec![StructField {
|
||||
field_name: None,
|
||||
field_type: DataType::String(Some(42))
|
||||
field_type: DataType::String(Some(42)),
|
||||
options: None,
|
||||
}]
|
||||
},
|
||||
expr_from_projection(&select.projection[0])
|
||||
|
@ -1295,11 +1337,17 @@ fn parse_typed_struct_syntax_bigquery_and_generic() {
|
|||
&Expr::Struct {
|
||||
values: vec![Expr::TypedString {
|
||||
data_type: DataType::Timestamp(None, TimezoneInfo::None),
|
||||
value: Value::SingleQuotedString("2008-12-25 15:30:00 America/Los_Angeles".into())
|
||||
value: ValueWithSpan {
|
||||
value: Value::SingleQuotedString(
|
||||
"2008-12-25 15:30:00 America/Los_Angeles".into()
|
||||
),
|
||||
span: Span::empty(),
|
||||
}
|
||||
}],
|
||||
fields: vec![StructField {
|
||||
field_name: None,
|
||||
field_type: DataType::Timestamp(None, TimezoneInfo::None)
|
||||
field_type: DataType::Timestamp(None, TimezoneInfo::None),
|
||||
options: None,
|
||||
}]
|
||||
},
|
||||
expr_from_projection(&select.projection[1])
|
||||
|
@ -1309,11 +1357,15 @@ fn parse_typed_struct_syntax_bigquery_and_generic() {
|
|||
&Expr::Struct {
|
||||
values: vec![Expr::TypedString {
|
||||
data_type: DataType::Time(None, TimezoneInfo::None),
|
||||
value: Value::SingleQuotedString("15:30:00".into())
|
||||
value: ValueWithSpan {
|
||||
value: Value::SingleQuotedString("15:30:00".into()),
|
||||
span: Span::empty(),
|
||||
}
|
||||
}],
|
||||
fields: vec![StructField {
|
||||
field_name: None,
|
||||
field_type: DataType::Time(None, TimezoneInfo::None)
|
||||
field_type: DataType::Time(None, TimezoneInfo::None),
|
||||
options: None,
|
||||
}]
|
||||
},
|
||||
expr_from_projection(&select.projection[2])
|
||||
|
@ -1326,11 +1378,15 @@ fn parse_typed_struct_syntax_bigquery_and_generic() {
|
|||
&Expr::Struct {
|
||||
values: vec![Expr::TypedString {
|
||||
data_type: DataType::Numeric(ExactNumberInfo::None),
|
||||
value: Value::SingleQuotedString("1".into())
|
||||
value: ValueWithSpan {
|
||||
value: Value::SingleQuotedString("1".into()),
|
||||
span: Span::empty(),
|
||||
}
|
||||
}],
|
||||
fields: vec![StructField {
|
||||
field_name: None,
|
||||
field_type: DataType::Numeric(ExactNumberInfo::None)
|
||||
field_type: DataType::Numeric(ExactNumberInfo::None),
|
||||
options: None,
|
||||
}]
|
||||
},
|
||||
expr_from_projection(&select.projection[0])
|
||||
|
@ -1339,11 +1395,15 @@ fn parse_typed_struct_syntax_bigquery_and_generic() {
|
|||
&Expr::Struct {
|
||||
values: vec![Expr::TypedString {
|
||||
data_type: DataType::BigNumeric(ExactNumberInfo::None),
|
||||
value: Value::SingleQuotedString("1".into())
|
||||
value: ValueWithSpan {
|
||||
value: Value::SingleQuotedString("1".into()),
|
||||
span: Span::empty(),
|
||||
}
|
||||
}],
|
||||
fields: vec![StructField {
|
||||
field_name: None,
|
||||
field_type: DataType::BigNumeric(ExactNumberInfo::None)
|
||||
field_type: DataType::BigNumeric(ExactNumberInfo::None),
|
||||
options: None,
|
||||
}]
|
||||
},
|
||||
expr_from_projection(&select.projection[1])
|
||||
|
@ -1360,7 +1420,8 @@ fn parse_typed_struct_with_field_name_bigquery() {
|
|||
values: vec![Expr::value(number("5"))],
|
||||
fields: vec![StructField {
|
||||
field_name: Some(Ident::from("x")),
|
||||
field_type: DataType::Int64
|
||||
field_type: DataType::Int64,
|
||||
options: None,
|
||||
}]
|
||||
},
|
||||
expr_from_projection(&select.projection[0])
|
||||
|
@ -1372,7 +1433,8 @@ fn parse_typed_struct_with_field_name_bigquery() {
|
|||
)],
|
||||
fields: vec![StructField {
|
||||
field_name: Some(Ident::from("y")),
|
||||
field_type: DataType::String(None)
|
||||
field_type: DataType::String(None),
|
||||
options: None,
|
||||
}]
|
||||
},
|
||||
expr_from_projection(&select.projection[1])
|
||||
|
@ -1387,11 +1449,13 @@ fn parse_typed_struct_with_field_name_bigquery() {
|
|||
fields: vec![
|
||||
StructField {
|
||||
field_name: Some(Ident::from("x")),
|
||||
field_type: DataType::Int64
|
||||
field_type: DataType::Int64,
|
||||
options: None,
|
||||
},
|
||||
StructField {
|
||||
field_name: Some(Ident::from("y")),
|
||||
field_type: DataType::Int64
|
||||
field_type: DataType::Int64,
|
||||
options: None,
|
||||
}
|
||||
]
|
||||
},
|
||||
|
@ -1409,7 +1473,8 @@ fn parse_typed_struct_with_field_name_bigquery_and_generic() {
|
|||
values: vec![Expr::value(number("5"))],
|
||||
fields: vec![StructField {
|
||||
field_name: Some(Ident::from("x")),
|
||||
field_type: DataType::Int64
|
||||
field_type: DataType::Int64,
|
||||
options: None,
|
||||
}]
|
||||
},
|
||||
expr_from_projection(&select.projection[0])
|
||||
|
@ -1421,7 +1486,8 @@ fn parse_typed_struct_with_field_name_bigquery_and_generic() {
|
|||
)],
|
||||
fields: vec![StructField {
|
||||
field_name: Some(Ident::from("y")),
|
||||
field_type: DataType::String(None)
|
||||
field_type: DataType::String(None),
|
||||
options: None,
|
||||
}]
|
||||
},
|
||||
expr_from_projection(&select.projection[1])
|
||||
|
@ -1436,11 +1502,13 @@ fn parse_typed_struct_with_field_name_bigquery_and_generic() {
|
|||
fields: vec![
|
||||
StructField {
|
||||
field_name: Some(Ident::from("x")),
|
||||
field_type: DataType::Int64
|
||||
field_type: DataType::Int64,
|
||||
options: None,
|
||||
},
|
||||
StructField {
|
||||
field_name: Some(Ident::from("y")),
|
||||
field_type: DataType::Int64
|
||||
field_type: DataType::Int64,
|
||||
options: None,
|
||||
}
|
||||
]
|
||||
},
|
||||
|
@ -1735,6 +1803,7 @@ fn parse_merge() {
|
|||
},
|
||||
],
|
||||
};
|
||||
|
||||
match bigquery_and_generic().verified_stmt(sql) {
|
||||
Statement::Merge {
|
||||
into,
|
||||
|
@ -1742,6 +1811,7 @@ fn parse_merge() {
|
|||
source,
|
||||
on,
|
||||
clauses,
|
||||
..
|
||||
} => {
|
||||
assert!(!into);
|
||||
assert_eq!(
|
||||
|
@ -2132,6 +2202,7 @@ fn test_bigquery_create_function() {
|
|||
assert_eq!(
|
||||
stmt,
|
||||
Statement::CreateFunction(CreateFunction {
|
||||
or_alter: false,
|
||||
or_replace: true,
|
||||
temporary: true,
|
||||
if_not_exists: false,
|
||||
|
@ -2310,16 +2381,46 @@ fn bigquery_select_expr_star() {
|
|||
|
||||
#[test]
|
||||
fn test_select_as_struct() {
|
||||
bigquery().verified_only_select("SELECT * FROM (SELECT AS VALUE STRUCT(123 AS a, false AS b))");
|
||||
for (sql, parse_to) in [
|
||||
(
|
||||
"SELECT * FROM (SELECT AS STRUCT STRUCT(123 AS a, false AS b))",
|
||||
"SELECT * FROM (SELECT AS STRUCT STRUCT(123 AS a, false AS b))",
|
||||
),
|
||||
(
|
||||
"SELECT * FROM (SELECT DISTINCT AS STRUCT STRUCT(123 AS a, false AS b))",
|
||||
"SELECT * FROM (SELECT DISTINCT AS STRUCT STRUCT(123 AS a, false AS b))",
|
||||
),
|
||||
(
|
||||
"SELECT * FROM (SELECT ALL AS STRUCT STRUCT(123 AS a, false AS b))",
|
||||
"SELECT * FROM (SELECT AS STRUCT STRUCT(123 AS a, false AS b))",
|
||||
),
|
||||
] {
|
||||
bigquery().one_statement_parses_to(sql, parse_to);
|
||||
}
|
||||
|
||||
let select = bigquery().verified_only_select("SELECT AS STRUCT 1 AS a, 2 AS b");
|
||||
assert_eq!(Some(ValueTableMode::AsStruct), select.value_table_mode);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_select_as_value() {
|
||||
bigquery().verified_only_select(
|
||||
for (sql, parse_to) in [
|
||||
(
|
||||
"SELECT * FROM (SELECT AS VALUE STRUCT(5 AS star_rating, false AS up_down_rating))",
|
||||
);
|
||||
"SELECT * FROM (SELECT AS VALUE STRUCT(5 AS star_rating, false AS up_down_rating))",
|
||||
),
|
||||
(
|
||||
"SELECT * FROM (SELECT DISTINCT AS VALUE STRUCT(5 AS star_rating, false AS up_down_rating))",
|
||||
"SELECT * FROM (SELECT DISTINCT AS VALUE STRUCT(5 AS star_rating, false AS up_down_rating))",
|
||||
),
|
||||
(
|
||||
"SELECT * FROM (SELECT ALL AS VALUE STRUCT(5 AS star_rating, false AS up_down_rating))",
|
||||
"SELECT * FROM (SELECT AS VALUE STRUCT(5 AS star_rating, false AS up_down_rating))",
|
||||
),
|
||||
] {
|
||||
bigquery().one_statement_parses_to(sql, parse_to);
|
||||
}
|
||||
|
||||
let select = bigquery().verified_only_select("SELECT AS VALUE STRUCT(1 AS a, 2 AS b) AS xyz");
|
||||
assert_eq!(Some(ValueTableMode::AsValue), select.value_table_mode);
|
||||
}
|
||||
|
@ -2332,7 +2433,10 @@ fn test_triple_quote_typed_strings() {
|
|||
assert_eq!(
|
||||
Expr::TypedString {
|
||||
data_type: DataType::JSON,
|
||||
value: Value::TripleDoubleQuotedString(r#"{"foo":"bar's"}"#.into())
|
||||
value: ValueWithSpan {
|
||||
value: Value::TripleDoubleQuotedString(r#"{"foo":"bar's"}"#.into()),
|
||||
span: Span::empty(),
|
||||
}
|
||||
},
|
||||
expr
|
||||
);
|
||||
|
@ -2374,3 +2478,91 @@ fn test_any_type() {
|
|||
fn test_any_type_dont_break_custom_type() {
|
||||
bigquery_and_generic().verified_stmt("CREATE TABLE foo (x ANY)");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_struct_field_options() {
|
||||
bigquery().verified_stmt(concat!(
|
||||
"CREATE TABLE my_table (",
|
||||
"f0 STRUCT<a STRING, b INT64>, ",
|
||||
"f1 STRUCT<",
|
||||
"a STRING OPTIONS(description = 'This is a string', type = 'string'), ",
|
||||
"b INT64",
|
||||
"> OPTIONS(description = 'This is a struct field')",
|
||||
")",
|
||||
));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_struct_trailing_and_nested_bracket() {
|
||||
bigquery().verified_stmt(concat!(
|
||||
"CREATE TABLE my_table (",
|
||||
"f0 STRING, ",
|
||||
"f1 STRUCT<a STRING, b STRUCT<c INT64, d STRING>>, ",
|
||||
"f2 STRING",
|
||||
")",
|
||||
));
|
||||
|
||||
// More complex nested structs
|
||||
bigquery().verified_stmt(concat!(
|
||||
"CREATE TABLE my_table (",
|
||||
"f0 STRING, ",
|
||||
"f1 STRUCT<a STRING, b STRUCT<c INT64, d STRUCT<e STRING>>>, ",
|
||||
"f2 STRUCT<h STRING, i STRUCT<j INT64, k STRUCT<l STRUCT<m STRING>>>>, ",
|
||||
"f3 STRUCT<e STRING, f STRUCT<c INT64>>",
|
||||
")",
|
||||
));
|
||||
|
||||
// Bad case with missing closing bracket
|
||||
assert_eq!(
|
||||
ParserError::ParserError("Expected: >, found: )".to_owned()),
|
||||
bigquery()
|
||||
.parse_sql_statements("CREATE TABLE my_table(f1 STRUCT<a STRING, b INT64)")
|
||||
.unwrap_err()
|
||||
);
|
||||
|
||||
// Bad case with redundant closing bracket
|
||||
assert_eq!(
|
||||
ParserError::ParserError(
|
||||
"unmatched > after parsing data type STRUCT<a STRING, b INT64>)".to_owned()
|
||||
),
|
||||
bigquery()
|
||||
.parse_sql_statements("CREATE TABLE my_table(f1 STRUCT<a STRING, b INT64>>)")
|
||||
.unwrap_err()
|
||||
);
|
||||
|
||||
// Base case with redundant closing bracket in nested struct
|
||||
assert_eq!(
|
||||
ParserError::ParserError(
|
||||
"Expected: ',' or ')' after column definition, found: >".to_owned()
|
||||
),
|
||||
bigquery()
|
||||
.parse_sql_statements("CREATE TABLE my_table(f1 STRUCT<a STRUCT<b INT>>>, c INT64)")
|
||||
.unwrap_err()
|
||||
);
|
||||
|
||||
let sql = "SELECT STRUCT<INT64>>(NULL)";
|
||||
assert_eq!(
|
||||
bigquery_and_generic()
|
||||
.parse_sql_statements(sql)
|
||||
.unwrap_err(),
|
||||
ParserError::ParserError("unmatched > in STRUCT literal".to_string())
|
||||
);
|
||||
|
||||
let sql = "SELECT STRUCT<STRUCT<INT64>>>(NULL)";
|
||||
assert_eq!(
|
||||
bigquery_and_generic()
|
||||
.parse_sql_statements(sql)
|
||||
.unwrap_err(),
|
||||
ParserError::ParserError("Expected: (, found: >".to_string())
|
||||
);
|
||||
|
||||
let sql = "CREATE TABLE table (x STRUCT<STRUCT<INT64>>>)";
|
||||
assert_eq!(
|
||||
bigquery_and_generic()
|
||||
.parse_sql_statements(sql)
|
||||
.unwrap_err(),
|
||||
ParserError::ParserError(
|
||||
"Expected: ',' or ')' after column definition, found: >".to_string()
|
||||
)
|
||||
);
|
||||
}
|
||||
|
|
|
@ -28,7 +28,7 @@ use test_utils::*;
|
|||
use sqlparser::ast::Expr::{BinaryOp, Identifier};
|
||||
use sqlparser::ast::SelectItem::UnnamedExpr;
|
||||
use sqlparser::ast::TableFactor::Table;
|
||||
use sqlparser::ast::Value::Number;
|
||||
use sqlparser::ast::Value::Boolean;
|
||||
use sqlparser::ast::*;
|
||||
use sqlparser::dialect::ClickHouseDialect;
|
||||
use sqlparser::dialect::GenericDialect;
|
||||
|
@ -224,6 +224,10 @@ fn parse_create_table() {
|
|||
clickhouse().verified_stmt(
|
||||
r#"CREATE TABLE "x" ("a" "int") ENGINE = MergeTree ORDER BY "x" AS SELECT * FROM "t" WHERE true"#,
|
||||
);
|
||||
clickhouse().one_statement_parses_to(
|
||||
"CREATE TABLE x (a int) ENGINE = MergeTree() ORDER BY a",
|
||||
"CREATE TABLE x (a INT) ENGINE = MergeTree ORDER BY a",
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
|
@ -669,11 +673,13 @@ fn parse_create_table_with_nested_data_types() {
|
|||
DataType::Tuple(vec![
|
||||
StructField {
|
||||
field_name: None,
|
||||
field_type: DataType::FixedString(128)
|
||||
field_type: DataType::FixedString(128),
|
||||
options: None,
|
||||
},
|
||||
StructField {
|
||||
field_name: None,
|
||||
field_type: DataType::Int128
|
||||
field_type: DataType::Int128,
|
||||
options: None,
|
||||
}
|
||||
])
|
||||
))),
|
||||
|
@ -685,12 +691,14 @@ fn parse_create_table_with_nested_data_types() {
|
|||
StructField {
|
||||
field_name: Some("a".into()),
|
||||
field_type: DataType::Datetime64(9, None),
|
||||
options: None,
|
||||
},
|
||||
StructField {
|
||||
field_name: Some("b".into()),
|
||||
field_type: DataType::Array(ArrayElemTypeDef::Parenthesis(
|
||||
Box::new(DataType::Uuid)
|
||||
))
|
||||
)),
|
||||
options: None,
|
||||
},
|
||||
]),
|
||||
options: vec![],
|
||||
|
@ -721,7 +729,7 @@ fn parse_create_table_with_primary_key() {
|
|||
Statement::CreateTable(CreateTable {
|
||||
name,
|
||||
columns,
|
||||
engine,
|
||||
table_options,
|
||||
primary_key,
|
||||
order_by,
|
||||
..
|
||||
|
@ -742,16 +750,23 @@ fn parse_create_table_with_primary_key() {
|
|||
],
|
||||
columns
|
||||
);
|
||||
assert_eq!(
|
||||
engine,
|
||||
Some(TableEngine {
|
||||
name: "SharedMergeTree".to_string(),
|
||||
parameters: Some(vec![
|
||||
|
||||
let plain_options = match table_options {
|
||||
CreateTableOptions::Plain(options) => options,
|
||||
_ => unreachable!(),
|
||||
};
|
||||
|
||||
assert!(plain_options.contains(&SqlOption::NamedParenthesizedList(
|
||||
NamedParenthesizedList {
|
||||
key: Ident::new("ENGINE"),
|
||||
name: Some(Ident::new("SharedMergeTree")),
|
||||
values: vec![
|
||||
Ident::with_quote('\'', "/clickhouse/tables/{uuid}/{shard}"),
|
||||
Ident::with_quote('\'', "{replica}"),
|
||||
]),
|
||||
})
|
||||
);
|
||||
]
|
||||
}
|
||||
)));
|
||||
|
||||
fn assert_function(actual: &Function, name: &str, arg: &str) -> bool {
|
||||
assert_eq!(actual.name, ObjectName::from(vec![Ident::new(name)]));
|
||||
assert_eq!(
|
||||
|
@ -903,7 +918,7 @@ fn parse_create_view_with_fields_data_types() {
|
|||
}]),
|
||||
vec![]
|
||||
)),
|
||||
options: None
|
||||
options: None,
|
||||
},
|
||||
ViewColumnDef {
|
||||
name: "f".into(),
|
||||
|
@ -915,7 +930,7 @@ fn parse_create_view_with_fields_data_types() {
|
|||
}]),
|
||||
vec![]
|
||||
)),
|
||||
options: None
|
||||
options: None,
|
||||
},
|
||||
]
|
||||
);
|
||||
|
@ -944,42 +959,113 @@ fn parse_limit_by() {
|
|||
clickhouse_and_generic().verified_stmt(
|
||||
r#"SELECT * FROM default.last_asset_runs_mv ORDER BY created_at DESC LIMIT 1 BY asset, toStartOfDay(created_at)"#,
|
||||
);
|
||||
clickhouse_and_generic().parse_sql_statements(
|
||||
r#"SELECT * FROM default.last_asset_runs_mv ORDER BY created_at DESC BY asset, toStartOfDay(created_at)"#,
|
||||
).expect_err("BY without LIMIT");
|
||||
clickhouse_and_generic()
|
||||
.parse_sql_statements("SELECT * FROM T OFFSET 5 BY foo")
|
||||
.expect_err("BY with OFFSET but without LIMIT");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn parse_settings_in_query() {
|
||||
match clickhouse_and_generic()
|
||||
.verified_stmt(r#"SELECT * FROM t SETTINGS max_threads = 1, max_block_size = 10000"#)
|
||||
{
|
||||
Statement::Query(query) => {
|
||||
assert_eq!(
|
||||
query.settings,
|
||||
Some(vec![
|
||||
Setting {
|
||||
key: Ident::new("max_threads"),
|
||||
value: Number("1".parse().unwrap(), false)
|
||||
},
|
||||
Setting {
|
||||
key: Ident::new("max_block_size"),
|
||||
value: Number("10000".parse().unwrap(), false)
|
||||
},
|
||||
])
|
||||
);
|
||||
fn check_settings(sql: &str, expected: Vec<Setting>) {
|
||||
match clickhouse_and_generic().verified_stmt(sql) {
|
||||
Statement::Query(q) => {
|
||||
assert_eq!(q.settings, Some(expected));
|
||||
}
|
||||
_ => unreachable!(),
|
||||
}
|
||||
}
|
||||
|
||||
for (sql, expected_settings) in [
|
||||
(
|
||||
r#"SELECT * FROM t SETTINGS max_threads = 1, max_block_size = 10000"#,
|
||||
vec![
|
||||
Setting {
|
||||
key: Ident::new("max_threads"),
|
||||
value: Expr::value(number("1")),
|
||||
},
|
||||
Setting {
|
||||
key: Ident::new("max_block_size"),
|
||||
value: Expr::value(number("10000")),
|
||||
},
|
||||
],
|
||||
),
|
||||
(
|
||||
r#"SELECT * FROM t SETTINGS additional_table_filters = {'table_1': 'x != 2'}"#,
|
||||
vec![Setting {
|
||||
key: Ident::new("additional_table_filters"),
|
||||
value: Expr::Dictionary(vec![DictionaryField {
|
||||
key: Ident::with_quote('\'', "table_1"),
|
||||
value: Expr::value(single_quoted_string("x != 2")).into(),
|
||||
}]),
|
||||
}],
|
||||
),
|
||||
(
|
||||
r#"SELECT * FROM t SETTINGS additional_result_filter = 'x != 2', query_plan_optimize_lazy_materialization = false"#,
|
||||
vec![
|
||||
Setting {
|
||||
key: Ident::new("additional_result_filter"),
|
||||
value: Expr::value(single_quoted_string("x != 2")),
|
||||
},
|
||||
Setting {
|
||||
key: Ident::new("query_plan_optimize_lazy_materialization"),
|
||||
value: Expr::value(Boolean(false)),
|
||||
},
|
||||
],
|
||||
),
|
||||
] {
|
||||
check_settings(sql, expected_settings);
|
||||
}
|
||||
|
||||
let invalid_cases = vec![
|
||||
"SELECT * FROM t SETTINGS a",
|
||||
("SELECT * FROM t SETTINGS a", "Expected: =, found: EOF"),
|
||||
(
|
||||
"SELECT * FROM t SETTINGS a=",
|
||||
"SELECT * FROM t SETTINGS a=1, b",
|
||||
"Expected: an expression, found: EOF",
|
||||
),
|
||||
("SELECT * FROM t SETTINGS a=1, b", "Expected: =, found: EOF"),
|
||||
(
|
||||
"SELECT * FROM t SETTINGS a=1, b=",
|
||||
"SELECT * FROM t SETTINGS a=1, b=c",
|
||||
"Expected: an expression, found: EOF",
|
||||
),
|
||||
(
|
||||
"SELECT * FROM t SETTINGS a = {",
|
||||
"Expected: identifier, found: EOF",
|
||||
),
|
||||
(
|
||||
"SELECT * FROM t SETTINGS a = {'b'",
|
||||
"Expected: :, found: EOF",
|
||||
),
|
||||
(
|
||||
"SELECT * FROM t SETTINGS a = {'b': ",
|
||||
"Expected: an expression, found: EOF",
|
||||
),
|
||||
(
|
||||
"SELECT * FROM t SETTINGS a = {'b': 'c',}",
|
||||
"Expected: identifier, found: }",
|
||||
),
|
||||
(
|
||||
"SELECT * FROM t SETTINGS a = {'b': 'c', 'd'}",
|
||||
"Expected: :, found: }",
|
||||
),
|
||||
(
|
||||
"SELECT * FROM t SETTINGS a = {'b': 'c', 'd': }",
|
||||
"Expected: an expression, found: }",
|
||||
),
|
||||
(
|
||||
"SELECT * FROM t SETTINGS a = {ANY(b)}",
|
||||
"Expected: :, found: (",
|
||||
),
|
||||
];
|
||||
for sql in invalid_cases {
|
||||
for (sql, error_msg) in invalid_cases {
|
||||
assert_eq!(
|
||||
clickhouse_and_generic()
|
||||
.parse_sql_statements(sql)
|
||||
.expect_err("Expected: SETTINGS key = value, found: ");
|
||||
.unwrap_err(),
|
||||
ParserError(error_msg.to_string())
|
||||
);
|
||||
}
|
||||
}
|
||||
#[test]
|
||||
|
@ -1107,7 +1193,14 @@ fn parse_select_order_by_with_fill_interpolate() {
|
|||
},
|
||||
select.order_by.expect("ORDER BY expected")
|
||||
);
|
||||
assert_eq!(Some(Expr::value(number("2"))), select.limit);
|
||||
assert_eq!(
|
||||
select.limit_clause,
|
||||
Some(LimitClause::LimitOffset {
|
||||
limit: Some(Expr::value(number("2"))),
|
||||
offset: None,
|
||||
limit_by: vec![]
|
||||
})
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
|
@ -1321,7 +1414,7 @@ fn parse_use() {
|
|||
for object_name in &valid_object_names {
|
||||
// Test single identifier without quotes
|
||||
assert_eq!(
|
||||
clickhouse().verified_stmt(&format!("USE {}", object_name)),
|
||||
clickhouse().verified_stmt(&format!("USE {object_name}")),
|
||||
Statement::Use(Use::Object(ObjectName::from(vec![Ident::new(
|
||||
object_name.to_string()
|
||||
)])))
|
||||
|
@ -1329,7 +1422,7 @@ fn parse_use() {
|
|||
for "e in "e_styles {
|
||||
// Test single identifier with different type of quotes
|
||||
assert_eq!(
|
||||
clickhouse().verified_stmt(&format!("USE {0}{1}{0}", quote, object_name)),
|
||||
clickhouse().verified_stmt(&format!("USE {quote}{object_name}{quote}")),
|
||||
Statement::Use(Use::Object(ObjectName::from(vec![Ident::with_quote(
|
||||
quote,
|
||||
object_name.to_string(),
|
||||
|
@ -1343,7 +1436,7 @@ fn parse_use() {
|
|||
fn test_query_with_format_clause() {
|
||||
let format_options = vec!["TabSeparated", "JSONCompact", "NULL"];
|
||||
for format in &format_options {
|
||||
let sql = format!("SELECT * FROM t FORMAT {}", format);
|
||||
let sql = format!("SELECT * FROM t FORMAT {format}");
|
||||
match clickhouse_and_generic().verified_stmt(&sql) {
|
||||
Statement::Query(query) => {
|
||||
if *format == "NULL" {
|
||||
|
@ -1526,11 +1619,11 @@ fn parse_select_table_function_settings() {
|
|||
settings: Some(vec![
|
||||
Setting {
|
||||
key: "s0".into(),
|
||||
value: Value::Number("3".parse().unwrap(), false),
|
||||
value: Expr::value(number("3")),
|
||||
},
|
||||
Setting {
|
||||
key: "s1".into(),
|
||||
value: Value::SingleQuotedString("s".into()),
|
||||
value: Expr::value(single_quoted_string("s")),
|
||||
},
|
||||
]),
|
||||
},
|
||||
|
@ -1551,11 +1644,11 @@ fn parse_select_table_function_settings() {
|
|||
settings: Some(vec![
|
||||
Setting {
|
||||
key: "s0".into(),
|
||||
value: Value::Number("3".parse().unwrap(), false),
|
||||
value: Expr::value(number("3")),
|
||||
},
|
||||
Setting {
|
||||
key: "s1".into(),
|
||||
value: Value::SingleQuotedString("s".into()),
|
||||
value: Expr::value(single_quoted_string("s")),
|
||||
},
|
||||
]),
|
||||
},
|
||||
|
@ -1565,7 +1658,6 @@ fn parse_select_table_function_settings() {
|
|||
"SELECT * FROM t(SETTINGS a=)",
|
||||
"SELECT * FROM t(SETTINGS a=1, b)",
|
||||
"SELECT * FROM t(SETTINGS a=1, b=)",
|
||||
"SELECT * FROM t(SETTINGS a=1, b=c)",
|
||||
];
|
||||
for sql in invalid_cases {
|
||||
clickhouse_and_generic()
|
||||
|
|
File diff suppressed because it is too large
Load diff
|
@ -15,9 +15,11 @@
|
|||
// specific language governing permissions and limitations
|
||||
// under the License.
|
||||
|
||||
use sqlparser::ast::helpers::attached_token::AttachedToken;
|
||||
use sqlparser::ast::*;
|
||||
use sqlparser::dialect::{DatabricksDialect, GenericDialect};
|
||||
use sqlparser::parser::ParserError;
|
||||
use sqlparser::tokenizer::Span;
|
||||
use test_utils::*;
|
||||
|
||||
#[macro_use]
|
||||
|
@ -108,6 +110,8 @@ fn test_databricks_lambdas() {
|
|||
Expr::Lambda(LambdaFunction {
|
||||
params: OneOrManyWithParens::Many(vec![Ident::new("p1"), Ident::new("p2")]),
|
||||
body: Box::new(Expr::Case {
|
||||
case_token: AttachedToken::empty(),
|
||||
end_token: AttachedToken::empty(),
|
||||
operand: None,
|
||||
conditions: vec![
|
||||
CaseWhen {
|
||||
|
@ -210,7 +214,7 @@ fn parse_use() {
|
|||
for object_name in &valid_object_names {
|
||||
// Test single identifier without quotes
|
||||
assert_eq!(
|
||||
databricks().verified_stmt(&format!("USE {}", object_name)),
|
||||
databricks().verified_stmt(&format!("USE {object_name}")),
|
||||
Statement::Use(Use::Object(ObjectName::from(vec![Ident::new(
|
||||
object_name.to_string()
|
||||
)])))
|
||||
|
@ -218,7 +222,7 @@ fn parse_use() {
|
|||
for "e in "e_styles {
|
||||
// Test single identifier with different type of quotes
|
||||
assert_eq!(
|
||||
databricks().verified_stmt(&format!("USE {0}{1}{0}", quote, object_name)),
|
||||
databricks().verified_stmt(&format!("USE {quote}{object_name}{quote}")),
|
||||
Statement::Use(Use::Object(ObjectName::from(vec![Ident::with_quote(
|
||||
quote,
|
||||
object_name.to_string(),
|
||||
|
@ -230,21 +234,21 @@ fn parse_use() {
|
|||
for "e in "e_styles {
|
||||
// Test single identifier with keyword and different type of quotes
|
||||
assert_eq!(
|
||||
databricks().verified_stmt(&format!("USE CATALOG {0}my_catalog{0}", quote)),
|
||||
databricks().verified_stmt(&format!("USE CATALOG {quote}my_catalog{quote}")),
|
||||
Statement::Use(Use::Catalog(ObjectName::from(vec![Ident::with_quote(
|
||||
quote,
|
||||
"my_catalog".to_string(),
|
||||
)])))
|
||||
);
|
||||
assert_eq!(
|
||||
databricks().verified_stmt(&format!("USE DATABASE {0}my_database{0}", quote)),
|
||||
databricks().verified_stmt(&format!("USE DATABASE {quote}my_database{quote}")),
|
||||
Statement::Use(Use::Database(ObjectName::from(vec![Ident::with_quote(
|
||||
quote,
|
||||
"my_database".to_string(),
|
||||
)])))
|
||||
);
|
||||
assert_eq!(
|
||||
databricks().verified_stmt(&format!("USE SCHEMA {0}my_schema{0}", quote)),
|
||||
databricks().verified_stmt(&format!("USE SCHEMA {quote}my_schema{quote}")),
|
||||
Statement::Use(Use::Schema(ObjectName::from(vec![Ident::with_quote(
|
||||
quote,
|
||||
"my_schema".to_string(),
|
||||
|
@ -317,3 +321,46 @@ fn parse_databricks_struct_function() {
|
|||
})
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn data_type_timestamp_ntz() {
|
||||
// Literal
|
||||
assert_eq!(
|
||||
databricks().verified_expr("TIMESTAMP_NTZ '2025-03-29T18:52:00'"),
|
||||
Expr::TypedString {
|
||||
data_type: DataType::TimestampNtz,
|
||||
value: ValueWithSpan {
|
||||
value: Value::SingleQuotedString("2025-03-29T18:52:00".to_owned()),
|
||||
span: Span::empty(),
|
||||
}
|
||||
}
|
||||
);
|
||||
|
||||
// Cast
|
||||
assert_eq!(
|
||||
databricks().verified_expr("(created_at)::TIMESTAMP_NTZ"),
|
||||
Expr::Cast {
|
||||
kind: CastKind::DoubleColon,
|
||||
expr: Box::new(Expr::Nested(Box::new(Expr::Identifier(
|
||||
"created_at".into()
|
||||
)))),
|
||||
data_type: DataType::TimestampNtz,
|
||||
format: None
|
||||
}
|
||||
);
|
||||
|
||||
// Column definition
|
||||
match databricks().verified_stmt("CREATE TABLE foo (x TIMESTAMP_NTZ)") {
|
||||
Statement::CreateTable(CreateTable { columns, .. }) => {
|
||||
assert_eq!(
|
||||
columns,
|
||||
vec![ColumnDef {
|
||||
name: "x".into(),
|
||||
data_type: DataType::TimestampNtz,
|
||||
options: vec![],
|
||||
}]
|
||||
);
|
||||
}
|
||||
s => panic!("Unexpected statement: {s:?}"),
|
||||
}
|
||||
}
|
||||
|
|
|
@ -24,6 +24,7 @@ use test_utils::*;
|
|||
|
||||
use sqlparser::ast::*;
|
||||
use sqlparser::dialect::{DuckDbDialect, GenericDialect};
|
||||
use sqlparser::parser::ParserError;
|
||||
|
||||
fn duckdb() -> TestedDialects {
|
||||
TestedDialects::new(vec![Box::new(DuckDbDialect {})])
|
||||
|
@ -44,10 +45,12 @@ fn test_struct() {
|
|||
StructField {
|
||||
field_name: Some(Ident::new("v")),
|
||||
field_type: DataType::Varchar(None),
|
||||
options: None,
|
||||
},
|
||||
StructField {
|
||||
field_name: Some(Ident::new("i")),
|
||||
field_type: DataType::Integer(None),
|
||||
options: None,
|
||||
},
|
||||
],
|
||||
StructBracketKind::Parentheses,
|
||||
|
@ -84,6 +87,7 @@ fn test_struct() {
|
|||
StructField {
|
||||
field_name: Some(Ident::new("v")),
|
||||
field_type: DataType::Varchar(None),
|
||||
options: None,
|
||||
},
|
||||
StructField {
|
||||
field_name: Some(Ident::new("s")),
|
||||
|
@ -92,14 +96,17 @@ fn test_struct() {
|
|||
StructField {
|
||||
field_name: Some(Ident::new("a1")),
|
||||
field_type: DataType::Integer(None),
|
||||
options: None,
|
||||
},
|
||||
StructField {
|
||||
field_name: Some(Ident::new("a2")),
|
||||
field_type: DataType::Varchar(None),
|
||||
options: None,
|
||||
},
|
||||
],
|
||||
StructBracketKind::Parentheses,
|
||||
),
|
||||
options: None,
|
||||
},
|
||||
],
|
||||
StructBracketKind::Parentheses,
|
||||
|
@ -352,6 +359,32 @@ fn test_duckdb_load_extension() {
|
|||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_duckdb_specific_int_types() {
|
||||
let duckdb_dtypes = vec![
|
||||
("UTINYINT", DataType::UTinyInt),
|
||||
("USMALLINT", DataType::USmallInt),
|
||||
("UBIGINT", DataType::UBigInt),
|
||||
("UHUGEINT", DataType::UHugeInt),
|
||||
("HUGEINT", DataType::HugeInt),
|
||||
];
|
||||
for (dtype_string, data_type) in duckdb_dtypes {
|
||||
let sql = format!("SELECT 123::{dtype_string}");
|
||||
let select = duckdb().verified_only_select(&sql);
|
||||
assert_eq!(
|
||||
&Expr::Cast {
|
||||
kind: CastKind::DoubleColon,
|
||||
expr: Box::new(Expr::Value(
|
||||
Value::Number("123".parse().unwrap(), false).with_empty_span()
|
||||
)),
|
||||
data_type: data_type.clone(),
|
||||
format: None,
|
||||
},
|
||||
expr_from_projection(&select.projection[0])
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_duckdb_struct_literal() {
|
||||
//struct literal syntax https://duckdb.org/docs/sql/data_types/struct#creating-structs
|
||||
|
@ -709,19 +742,13 @@ fn test_duckdb_union_datatype() {
|
|||
storage: Default::default(),
|
||||
location: Default::default()
|
||||
}),
|
||||
table_properties: Default::default(),
|
||||
with_options: Default::default(),
|
||||
file_format: Default::default(),
|
||||
location: Default::default(),
|
||||
query: Default::default(),
|
||||
without_rowid: Default::default(),
|
||||
like: Default::default(),
|
||||
clone: Default::default(),
|
||||
engine: Default::default(),
|
||||
comment: Default::default(),
|
||||
auto_increment_offset: Default::default(),
|
||||
default_charset: Default::default(),
|
||||
collation: Default::default(),
|
||||
on_commit: Default::default(),
|
||||
on_cluster: Default::default(),
|
||||
primary_key: Default::default(),
|
||||
|
@ -729,7 +756,7 @@ fn test_duckdb_union_datatype() {
|
|||
partition_by: Default::default(),
|
||||
cluster_by: Default::default(),
|
||||
clustered_by: Default::default(),
|
||||
options: Default::default(),
|
||||
inherits: Default::default(),
|
||||
strict: Default::default(),
|
||||
copy_grants: Default::default(),
|
||||
enable_schema_evolution: Default::default(),
|
||||
|
@ -745,6 +772,7 @@ fn test_duckdb_union_datatype() {
|
|||
catalog: Default::default(),
|
||||
catalog_sync: Default::default(),
|
||||
storage_serialization_policy: Default::default(),
|
||||
table_options: CreateTableOptions::None
|
||||
}),
|
||||
stmt
|
||||
);
|
||||
|
@ -765,7 +793,7 @@ fn parse_use() {
|
|||
for object_name in &valid_object_names {
|
||||
// Test single identifier without quotes
|
||||
assert_eq!(
|
||||
duckdb().verified_stmt(&format!("USE {}", object_name)),
|
||||
duckdb().verified_stmt(&format!("USE {object_name}")),
|
||||
Statement::Use(Use::Object(ObjectName::from(vec![Ident::new(
|
||||
object_name.to_string()
|
||||
)])))
|
||||
|
@ -773,7 +801,7 @@ fn parse_use() {
|
|||
for "e in "e_styles {
|
||||
// Test single identifier with different type of quotes
|
||||
assert_eq!(
|
||||
duckdb().verified_stmt(&format!("USE {0}{1}{0}", quote, object_name)),
|
||||
duckdb().verified_stmt(&format!("USE {quote}{object_name}{quote}")),
|
||||
Statement::Use(Use::Object(ObjectName::from(vec![Ident::with_quote(
|
||||
quote,
|
||||
object_name.to_string(),
|
||||
|
@ -785,7 +813,9 @@ fn parse_use() {
|
|||
for "e in "e_styles {
|
||||
// Test double identifier with different type of quotes
|
||||
assert_eq!(
|
||||
duckdb().verified_stmt(&format!("USE {0}CATALOG{0}.{0}my_schema{0}", quote)),
|
||||
duckdb().verified_stmt(&format!(
|
||||
"USE {quote}CATALOG{quote}.{quote}my_schema{quote}"
|
||||
)),
|
||||
Statement::Use(Use::Object(ObjectName::from(vec![
|
||||
Ident::with_quote(quote, "CATALOG"),
|
||||
Ident::with_quote(quote, "my_schema")
|
||||
|
@ -801,3 +831,32 @@ fn parse_use() {
|
|||
])))
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_duckdb_trim() {
|
||||
let real_sql = r#"SELECT customer_id, TRIM(item_price_id, '"', "a") AS item_price_id FROM models_staging.subscriptions"#;
|
||||
assert_eq!(duckdb().verified_stmt(real_sql).to_string(), real_sql);
|
||||
|
||||
let sql_only_select = "SELECT TRIM('xyz', 'a')";
|
||||
let select = duckdb().verified_only_select(sql_only_select);
|
||||
assert_eq!(
|
||||
&Expr::Trim {
|
||||
expr: Box::new(Expr::Value(
|
||||
Value::SingleQuotedString("xyz".to_owned()).with_empty_span()
|
||||
)),
|
||||
trim_where: None,
|
||||
trim_what: None,
|
||||
trim_characters: Some(vec![Expr::Value(
|
||||
Value::SingleQuotedString("a".to_owned()).with_empty_span()
|
||||
)]),
|
||||
},
|
||||
expr_from_projection(only(&select.projection))
|
||||
);
|
||||
|
||||
// missing comma separation
|
||||
let error_sql = "SELECT TRIM('xyz' 'a')";
|
||||
assert_eq!(
|
||||
ParserError::ParserError("Expected: ), found: 'a'".to_owned()),
|
||||
duckdb().parse_sql_statements(error_sql).unwrap_err()
|
||||
);
|
||||
}
|
||||
|
|
|
@ -22,11 +22,10 @@
|
|||
|
||||
use sqlparser::ast::{
|
||||
ClusteredBy, CommentDef, CreateFunction, CreateFunctionBody, CreateFunctionUsing, CreateTable,
|
||||
Expr, Function, FunctionArgumentList, FunctionArguments, Ident, ObjectName,
|
||||
OneOrManyWithParens, OrderByExpr, OrderByOptions, SelectItem, Statement, TableFactor,
|
||||
UnaryOperator, Use, Value,
|
||||
Expr, Function, FunctionArgumentList, FunctionArguments, Ident, ObjectName, OrderByExpr,
|
||||
OrderByOptions, SelectItem, Set, Statement, TableFactor, UnaryOperator, Use, Value,
|
||||
};
|
||||
use sqlparser::dialect::{GenericDialect, HiveDialect, MsSqlDialect};
|
||||
use sqlparser::dialect::{AnsiDialect, GenericDialect, HiveDialect};
|
||||
use sqlparser::parser::ParserError;
|
||||
use sqlparser::test_utils::*;
|
||||
|
||||
|
@ -92,7 +91,7 @@ fn parse_msck() {
|
|||
}
|
||||
|
||||
#[test]
|
||||
fn parse_set() {
|
||||
fn parse_set_hivevar() {
|
||||
let set = "SET HIVEVAR:name = a, b, c_d";
|
||||
hive().verified_stmt(set);
|
||||
}
|
||||
|
@ -134,9 +133,7 @@ fn create_table_with_comment() {
|
|||
Statement::CreateTable(CreateTable { comment, .. }) => {
|
||||
assert_eq!(
|
||||
comment,
|
||||
Some(CommentDef::AfterColumnDefsWithoutEq(
|
||||
"table comment".to_string()
|
||||
))
|
||||
Some(CommentDef::WithoutEq("table comment".to_string()))
|
||||
)
|
||||
}
|
||||
_ => unreachable!(),
|
||||
|
@ -344,6 +341,9 @@ fn lateral_view() {
|
|||
fn sort_by() {
|
||||
let sort_by = "SELECT * FROM db.table SORT BY a";
|
||||
hive().verified_stmt(sort_by);
|
||||
|
||||
let sort_by_with_direction = "SELECT * FROM db.table SORT BY a, b DESC";
|
||||
hive().verified_stmt(sort_by_with_direction);
|
||||
}
|
||||
|
||||
#[test]
|
||||
|
@ -369,20 +369,20 @@ fn from_cte() {
|
|||
fn set_statement_with_minus() {
|
||||
assert_eq!(
|
||||
hive().verified_stmt("SET hive.tez.java.opts = -Xmx4g"),
|
||||
Statement::SetVariable {
|
||||
local: false,
|
||||
Statement::Set(Set::SingleAssignment {
|
||||
scope: None,
|
||||
hivevar: false,
|
||||
variables: OneOrManyWithParens::One(ObjectName::from(vec![
|
||||
variable: ObjectName::from(vec![
|
||||
Ident::new("hive"),
|
||||
Ident::new("tez"),
|
||||
Ident::new("java"),
|
||||
Ident::new("opts")
|
||||
])),
|
||||
value: vec![Expr::UnaryOp {
|
||||
]),
|
||||
values: vec![Expr::UnaryOp {
|
||||
op: UnaryOperator::Minus,
|
||||
expr: Box::new(Expr::Identifier(Ident::new("Xmx4g")))
|
||||
}],
|
||||
}
|
||||
})
|
||||
);
|
||||
|
||||
assert_eq!(
|
||||
|
@ -424,7 +424,7 @@ fn parse_create_function() {
|
|||
}
|
||||
|
||||
// Test error in dialect that doesn't support parsing CREATE FUNCTION
|
||||
let unsupported_dialects = TestedDialects::new(vec![Box::new(MsSqlDialect {})]);
|
||||
let unsupported_dialects = TestedDialects::new(vec![Box::new(AnsiDialect {})]);
|
||||
|
||||
assert_eq!(
|
||||
unsupported_dialects.parse_sql_statements(sql).unwrap_err(),
|
||||
|
@ -524,7 +524,7 @@ fn parse_use() {
|
|||
for object_name in &valid_object_names {
|
||||
// Test single identifier without quotes
|
||||
assert_eq!(
|
||||
hive().verified_stmt(&format!("USE {}", object_name)),
|
||||
hive().verified_stmt(&format!("USE {object_name}")),
|
||||
Statement::Use(Use::Object(ObjectName::from(vec![Ident::new(
|
||||
object_name.to_string()
|
||||
)])))
|
||||
|
@ -532,7 +532,7 @@ fn parse_use() {
|
|||
for "e in "e_styles {
|
||||
// Test single identifier with different type of quotes
|
||||
assert_eq!(
|
||||
hive().verified_stmt(&format!("USE {}{}{}", quote, object_name, quote)),
|
||||
hive().verified_stmt(&format!("USE {quote}{object_name}{quote}")),
|
||||
Statement::Use(Use::Object(ObjectName::from(vec![Ident::with_quote(
|
||||
quote,
|
||||
object_name.to_string(),
|
||||
|
|
|
@ -23,7 +23,8 @@
|
|||
mod test_utils;
|
||||
|
||||
use helpers::attached_token::AttachedToken;
|
||||
use sqlparser::tokenizer::Span;
|
||||
use sqlparser::keywords::Keyword;
|
||||
use sqlparser::tokenizer::{Location, Span, Token, TokenWithSpan, Word};
|
||||
use test_utils::*;
|
||||
|
||||
use sqlparser::ast::DataType::{Int, Text, Varbinary};
|
||||
|
@ -31,7 +32,7 @@ use sqlparser::ast::DeclareAssignment::MsSqlAssignment;
|
|||
use sqlparser::ast::Value::SingleQuotedString;
|
||||
use sqlparser::ast::*;
|
||||
use sqlparser::dialect::{GenericDialect, MsSqlDialect};
|
||||
use sqlparser::parser::ParserError;
|
||||
use sqlparser::parser::{Parser, ParserError};
|
||||
|
||||
#[test]
|
||||
fn parse_mssql_identifiers() {
|
||||
|
@ -99,23 +100,24 @@ fn parse_mssql_delimited_identifiers() {
|
|||
|
||||
#[test]
|
||||
fn parse_create_procedure() {
|
||||
let sql = "CREATE OR ALTER PROCEDURE test (@foo INT, @bar VARCHAR(256)) AS BEGIN SELECT 1 END";
|
||||
let sql = "CREATE OR ALTER PROCEDURE test (@foo INT, @bar VARCHAR(256)) AS BEGIN SELECT 1; END";
|
||||
|
||||
assert_eq!(
|
||||
ms().verified_stmt(sql),
|
||||
Statement::CreateProcedure {
|
||||
or_alter: true,
|
||||
body: vec![Statement::Query(Box::new(Query {
|
||||
body: ConditionalStatements::BeginEnd(BeginEndStatements {
|
||||
begin_token: AttachedToken::empty(),
|
||||
statements: vec![Statement::Query(Box::new(Query {
|
||||
with: None,
|
||||
limit: None,
|
||||
limit_by: vec![],
|
||||
offset: None,
|
||||
limit_clause: None,
|
||||
fetch: None,
|
||||
locks: vec![],
|
||||
for_clause: None,
|
||||
order_by: None,
|
||||
settings: None,
|
||||
format_clause: None,
|
||||
pipe_operators: vec![],
|
||||
body: Box::new(SetExpr::Select(Box::new(Select {
|
||||
select_token: AttachedToken::empty(),
|
||||
distinct: None,
|
||||
|
@ -142,6 +144,8 @@ fn parse_create_procedure() {
|
|||
flavor: SelectFlavor::Standard,
|
||||
})))
|
||||
}))],
|
||||
end_token: AttachedToken::empty(),
|
||||
}),
|
||||
params: Some(vec![
|
||||
ProcedureParam {
|
||||
name: Ident {
|
||||
|
@ -149,7 +153,8 @@ fn parse_create_procedure() {
|
|||
quote_style: None,
|
||||
span: Span::empty(),
|
||||
},
|
||||
data_type: DataType::Int(None)
|
||||
data_type: DataType::Int(None),
|
||||
mode: None,
|
||||
},
|
||||
ProcedureParam {
|
||||
name: Ident {
|
||||
|
@ -160,33 +165,259 @@ fn parse_create_procedure() {
|
|||
data_type: DataType::Varchar(Some(CharacterLength::IntegerLength {
|
||||
length: 256,
|
||||
unit: None
|
||||
}))
|
||||
})),
|
||||
mode: None,
|
||||
}
|
||||
]),
|
||||
name: ObjectName::from(vec![Ident {
|
||||
value: "test".into(),
|
||||
quote_style: None,
|
||||
span: Span::empty(),
|
||||
}])
|
||||
}]),
|
||||
language: None,
|
||||
}
|
||||
)
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn parse_mssql_create_procedure() {
|
||||
let _ = ms_and_generic().verified_stmt("CREATE OR ALTER PROCEDURE foo AS BEGIN SELECT 1 END");
|
||||
let _ = ms_and_generic().verified_stmt("CREATE PROCEDURE foo AS BEGIN SELECT 1 END");
|
||||
let _ = ms_and_generic().verified_stmt("CREATE OR ALTER PROCEDURE foo AS SELECT 1;");
|
||||
let _ = ms_and_generic().verified_stmt("CREATE OR ALTER PROCEDURE foo AS BEGIN SELECT 1; END");
|
||||
let _ = ms_and_generic().verified_stmt("CREATE PROCEDURE foo AS BEGIN SELECT 1; END");
|
||||
let _ = ms().verified_stmt(
|
||||
"CREATE PROCEDURE foo AS BEGIN SELECT [myColumn] FROM [myschema].[mytable] END",
|
||||
"CREATE PROCEDURE foo AS BEGIN SELECT [myColumn] FROM [myschema].[mytable]; END",
|
||||
);
|
||||
let _ = ms_and_generic().verified_stmt(
|
||||
"CREATE PROCEDURE foo (@CustomerName NVARCHAR(50)) AS BEGIN SELECT * FROM DEV END",
|
||||
"CREATE PROCEDURE foo (@CustomerName NVARCHAR(50)) AS BEGIN SELECT * FROM DEV; END",
|
||||
);
|
||||
let _ = ms().verified_stmt("CREATE PROCEDURE [foo] AS BEGIN UPDATE bar SET col = 'test' END");
|
||||
let _ = ms().verified_stmt("CREATE PROCEDURE [foo] AS BEGIN UPDATE bar SET col = 'test'; END");
|
||||
// Test a statement with END in it
|
||||
let _ = ms().verified_stmt("CREATE PROCEDURE [foo] AS BEGIN SELECT [foo], CASE WHEN [foo] IS NULL THEN 'empty' ELSE 'notempty' END AS [foo] END");
|
||||
let _ = ms().verified_stmt("CREATE PROCEDURE [foo] AS BEGIN SELECT [foo], CASE WHEN [foo] IS NULL THEN 'empty' ELSE 'notempty' END AS [foo]; END");
|
||||
// Multiple statements
|
||||
let _ = ms().verified_stmt("CREATE PROCEDURE [foo] AS BEGIN UPDATE bar SET col = 'test'; SELECT [foo] FROM BAR WHERE [FOO] > 10 END");
|
||||
let _ = ms().verified_stmt("CREATE PROCEDURE [foo] AS BEGIN UPDATE bar SET col = 'test'; SELECT [foo] FROM BAR WHERE [FOO] > 10; END");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn parse_create_function() {
|
||||
let return_expression_function = "CREATE FUNCTION some_scalar_udf(@foo INT, @bar VARCHAR(256)) RETURNS INT AS BEGIN RETURN 1; END";
|
||||
assert_eq!(
|
||||
ms().verified_stmt(return_expression_function),
|
||||
sqlparser::ast::Statement::CreateFunction(CreateFunction {
|
||||
or_alter: false,
|
||||
or_replace: false,
|
||||
temporary: false,
|
||||
if_not_exists: false,
|
||||
name: ObjectName::from(vec![Ident::new("some_scalar_udf")]),
|
||||
args: Some(vec![
|
||||
OperateFunctionArg {
|
||||
mode: None,
|
||||
name: Some(Ident::new("@foo")),
|
||||
data_type: DataType::Int(None),
|
||||
default_expr: None,
|
||||
},
|
||||
OperateFunctionArg {
|
||||
mode: None,
|
||||
name: Some(Ident::new("@bar")),
|
||||
data_type: DataType::Varchar(Some(CharacterLength::IntegerLength {
|
||||
length: 256,
|
||||
unit: None
|
||||
})),
|
||||
default_expr: None,
|
||||
},
|
||||
]),
|
||||
return_type: Some(DataType::Int(None)),
|
||||
function_body: Some(CreateFunctionBody::AsBeginEnd(BeginEndStatements {
|
||||
begin_token: AttachedToken::empty(),
|
||||
statements: vec![Statement::Return(ReturnStatement {
|
||||
value: Some(ReturnStatementValue::Expr(Expr::Value(
|
||||
(number("1")).with_empty_span()
|
||||
))),
|
||||
})],
|
||||
end_token: AttachedToken::empty(),
|
||||
})),
|
||||
behavior: None,
|
||||
called_on_null: None,
|
||||
parallel: None,
|
||||
using: None,
|
||||
language: None,
|
||||
determinism_specifier: None,
|
||||
options: None,
|
||||
remote_connection: None,
|
||||
}),
|
||||
);
|
||||
|
||||
let multi_statement_function = "\
|
||||
CREATE FUNCTION some_scalar_udf(@foo INT, @bar VARCHAR(256)) \
|
||||
RETURNS INT \
|
||||
AS \
|
||||
BEGIN \
|
||||
SET @foo = @foo + 1; \
|
||||
RETURN @foo; \
|
||||
END\
|
||||
";
|
||||
let _ = ms().verified_stmt(multi_statement_function);
|
||||
|
||||
let multi_statement_function_without_as = multi_statement_function.replace(" AS", "");
|
||||
let _ = ms().one_statement_parses_to(
|
||||
&multi_statement_function_without_as,
|
||||
multi_statement_function,
|
||||
);
|
||||
|
||||
let create_function_with_conditional = "\
|
||||
CREATE FUNCTION some_scalar_udf() \
|
||||
RETURNS INT \
|
||||
AS \
|
||||
BEGIN \
|
||||
IF 1 = 2 \
|
||||
BEGIN \
|
||||
RETURN 1; \
|
||||
END; \
|
||||
RETURN 0; \
|
||||
END\
|
||||
";
|
||||
let _ = ms().verified_stmt(create_function_with_conditional);
|
||||
|
||||
let create_or_alter_function = "\
|
||||
CREATE OR ALTER FUNCTION some_scalar_udf(@foo INT, @bar VARCHAR(256)) \
|
||||
RETURNS INT \
|
||||
AS \
|
||||
BEGIN \
|
||||
SET @foo = @foo + 1; \
|
||||
RETURN @foo; \
|
||||
END\
|
||||
";
|
||||
let _ = ms().verified_stmt(create_or_alter_function);
|
||||
|
||||
let create_function_with_return_expression = "\
|
||||
CREATE FUNCTION some_scalar_udf(@foo INT, @bar VARCHAR(256)) \
|
||||
RETURNS INT \
|
||||
AS \
|
||||
BEGIN \
|
||||
RETURN CONVERT(INT, 1) + 2; \
|
||||
END\
|
||||
";
|
||||
let _ = ms().verified_stmt(create_function_with_return_expression);
|
||||
|
||||
let create_inline_table_value_function = "\
|
||||
CREATE FUNCTION some_inline_tvf(@foo INT, @bar VARCHAR(256)) \
|
||||
RETURNS TABLE \
|
||||
AS \
|
||||
RETURN (SELECT 1 AS col_1)\
|
||||
";
|
||||
let _ = ms().verified_stmt(create_inline_table_value_function);
|
||||
|
||||
let create_inline_table_value_function_without_parentheses = "\
|
||||
CREATE FUNCTION some_inline_tvf(@foo INT, @bar VARCHAR(256)) \
|
||||
RETURNS TABLE \
|
||||
AS \
|
||||
RETURN SELECT 1 AS col_1\
|
||||
";
|
||||
let _ = ms().verified_stmt(create_inline_table_value_function_without_parentheses);
|
||||
|
||||
let create_inline_table_value_function_without_as =
|
||||
create_inline_table_value_function.replace(" AS", "");
|
||||
let _ = ms().one_statement_parses_to(
|
||||
&create_inline_table_value_function_without_as,
|
||||
create_inline_table_value_function,
|
||||
);
|
||||
|
||||
let create_multi_statement_table_value_function = "\
|
||||
CREATE FUNCTION some_multi_statement_tvf(@foo INT, @bar VARCHAR(256)) \
|
||||
RETURNS @t TABLE (col_1 INT) \
|
||||
AS \
|
||||
BEGIN \
|
||||
INSERT INTO @t SELECT 1; \
|
||||
RETURN; \
|
||||
END\
|
||||
";
|
||||
let _ = ms().verified_stmt(create_multi_statement_table_value_function);
|
||||
|
||||
let create_multi_statement_table_value_function_without_as =
|
||||
create_multi_statement_table_value_function.replace(" AS", "");
|
||||
let _ = ms().one_statement_parses_to(
|
||||
&create_multi_statement_table_value_function_without_as,
|
||||
create_multi_statement_table_value_function,
|
||||
);
|
||||
|
||||
let create_multi_statement_table_value_function_with_constraints = "\
|
||||
CREATE FUNCTION some_multi_statement_tvf(@foo INT, @bar VARCHAR(256)) \
|
||||
RETURNS @t TABLE (col_1 INT NOT NULL) \
|
||||
AS \
|
||||
BEGIN \
|
||||
INSERT INTO @t SELECT 1; \
|
||||
RETURN @t; \
|
||||
END\
|
||||
";
|
||||
let _ = ms().verified_stmt(create_multi_statement_table_value_function_with_constraints);
|
||||
|
||||
let create_multi_statement_tvf_without_table_definition = "\
|
||||
CREATE FUNCTION incorrect_tvf(@foo INT, @bar VARCHAR(256)) \
|
||||
RETURNS @t TABLE ()
|
||||
AS \
|
||||
BEGIN \
|
||||
INSERT INTO @t SELECT 1; \
|
||||
RETURN @t; \
|
||||
END\
|
||||
";
|
||||
assert_eq!(
|
||||
ParserError::ParserError("Unparsable function body".to_owned()),
|
||||
ms().parse_sql_statements(create_multi_statement_tvf_without_table_definition)
|
||||
.unwrap_err()
|
||||
);
|
||||
|
||||
let create_inline_tvf_without_subquery_or_bare_select = "\
|
||||
CREATE FUNCTION incorrect_tvf(@foo INT, @bar VARCHAR(256)) \
|
||||
RETURNS TABLE
|
||||
AS \
|
||||
RETURN 'hi'\
|
||||
";
|
||||
assert_eq!(
|
||||
ParserError::ParserError(
|
||||
"Expected a subquery (or bare SELECT statement) after RETURN".to_owned()
|
||||
),
|
||||
ms().parse_sql_statements(create_inline_tvf_without_subquery_or_bare_select)
|
||||
.unwrap_err()
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn parse_create_function_parameter_default_values() {
|
||||
let single_default_sql =
|
||||
"CREATE FUNCTION test_func(@param1 INT = 42) RETURNS INT AS BEGIN RETURN @param1; END";
|
||||
assert_eq!(
|
||||
ms().verified_stmt(single_default_sql),
|
||||
Statement::CreateFunction(CreateFunction {
|
||||
or_alter: false,
|
||||
or_replace: false,
|
||||
temporary: false,
|
||||
if_not_exists: false,
|
||||
name: ObjectName::from(vec![Ident::new("test_func")]),
|
||||
args: Some(vec![OperateFunctionArg {
|
||||
mode: None,
|
||||
name: Some(Ident::new("@param1")),
|
||||
data_type: DataType::Int(None),
|
||||
default_expr: Some(Expr::Value((number("42")).with_empty_span())),
|
||||
},]),
|
||||
return_type: Some(DataType::Int(None)),
|
||||
function_body: Some(CreateFunctionBody::AsBeginEnd(BeginEndStatements {
|
||||
begin_token: AttachedToken::empty(),
|
||||
statements: vec![Statement::Return(ReturnStatement {
|
||||
value: Some(ReturnStatementValue::Expr(Expr::Identifier(Ident::new(
|
||||
"@param1"
|
||||
)))),
|
||||
})],
|
||||
end_token: AttachedToken::empty(),
|
||||
})),
|
||||
behavior: None,
|
||||
called_on_null: None,
|
||||
parallel: None,
|
||||
using: None,
|
||||
language: None,
|
||||
determinism_specifier: None,
|
||||
options: None,
|
||||
remote_connection: None,
|
||||
}),
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
|
@ -1135,6 +1366,7 @@ fn parse_substring_in_select() {
|
|||
(number("1")).with_empty_span()
|
||||
))),
|
||||
special: true,
|
||||
shorthand: false,
|
||||
})],
|
||||
into: None,
|
||||
from: vec![TableWithJoins {
|
||||
|
@ -1161,14 +1393,13 @@ fn parse_substring_in_select() {
|
|||
flavor: SelectFlavor::Standard,
|
||||
}))),
|
||||
order_by: None,
|
||||
limit: None,
|
||||
limit_by: vec![],
|
||||
offset: None,
|
||||
limit_clause: None,
|
||||
fetch: None,
|
||||
locks: vec![],
|
||||
for_clause: None,
|
||||
settings: None,
|
||||
format_clause: None,
|
||||
pipe_operators: vec![],
|
||||
}),
|
||||
query
|
||||
);
|
||||
|
@ -1254,25 +1485,25 @@ fn parse_mssql_declare() {
|
|||
for_query: None
|
||||
}]
|
||||
},
|
||||
Statement::SetVariable {
|
||||
local: false,
|
||||
Statement::Set(Set::SingleAssignment {
|
||||
scope: None,
|
||||
hivevar: false,
|
||||
variables: OneOrManyWithParens::One(ObjectName::from(vec![Ident::new("@bar")])),
|
||||
value: vec![Expr::Value(
|
||||
variable: ObjectName::from(vec![Ident::new("@bar")]),
|
||||
values: vec![Expr::Value(
|
||||
(Value::Number("2".parse().unwrap(), false)).with_empty_span()
|
||||
)],
|
||||
},
|
||||
}),
|
||||
Statement::Query(Box::new(Query {
|
||||
with: None,
|
||||
limit: None,
|
||||
limit_by: vec![],
|
||||
offset: None,
|
||||
limit_clause: None,
|
||||
fetch: None,
|
||||
locks: vec![],
|
||||
for_clause: None,
|
||||
order_by: None,
|
||||
settings: None,
|
||||
format_clause: None,
|
||||
pipe_operators: vec![],
|
||||
|
||||
body: Box::new(SetExpr::Select(Box::new(Select {
|
||||
select_token: AttachedToken::empty(),
|
||||
distinct: None,
|
||||
|
@ -1306,6 +1537,89 @@ fn parse_mssql_declare() {
|
|||
],
|
||||
ast
|
||||
);
|
||||
|
||||
let declare_cursor_for_select =
|
||||
"DECLARE vend_cursor CURSOR FOR SELECT * FROM Purchasing.Vendor";
|
||||
let _ = ms().verified_stmt(declare_cursor_for_select);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_mssql_cursor() {
|
||||
let full_cursor_usage = "\
|
||||
DECLARE Employee_Cursor CURSOR FOR \
|
||||
SELECT LastName, FirstName \
|
||||
FROM AdventureWorks2022.HumanResources.vEmployee \
|
||||
WHERE LastName LIKE 'B%'; \
|
||||
\
|
||||
OPEN Employee_Cursor; \
|
||||
\
|
||||
FETCH NEXT FROM Employee_Cursor; \
|
||||
\
|
||||
WHILE @@FETCH_STATUS = 0 \
|
||||
BEGIN \
|
||||
FETCH NEXT FROM Employee_Cursor; \
|
||||
END; \
|
||||
\
|
||||
CLOSE Employee_Cursor; \
|
||||
DEALLOCATE Employee_Cursor\
|
||||
";
|
||||
let _ = ms().statements_parse_to(full_cursor_usage, "");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_mssql_while_statement() {
|
||||
let while_single_statement = "WHILE 1 = 0 PRINT 'Hello World';";
|
||||
let stmt = ms().verified_stmt(while_single_statement);
|
||||
assert_eq!(
|
||||
stmt,
|
||||
Statement::While(sqlparser::ast::WhileStatement {
|
||||
while_block: ConditionalStatementBlock {
|
||||
start_token: AttachedToken(TokenWithSpan {
|
||||
token: Token::Word(Word {
|
||||
value: "WHILE".to_string(),
|
||||
quote_style: None,
|
||||
keyword: Keyword::WHILE
|
||||
}),
|
||||
span: Span::empty()
|
||||
}),
|
||||
condition: Some(Expr::BinaryOp {
|
||||
left: Box::new(Expr::Value(
|
||||
(Value::Number("1".parse().unwrap(), false)).with_empty_span()
|
||||
)),
|
||||
op: BinaryOperator::Eq,
|
||||
right: Box::new(Expr::Value(
|
||||
(Value::Number("0".parse().unwrap(), false)).with_empty_span()
|
||||
)),
|
||||
}),
|
||||
then_token: None,
|
||||
conditional_statements: ConditionalStatements::Sequence {
|
||||
statements: vec![Statement::Print(PrintStatement {
|
||||
message: Box::new(Expr::Value(
|
||||
(Value::SingleQuotedString("Hello World".to_string()))
|
||||
.with_empty_span()
|
||||
)),
|
||||
})],
|
||||
}
|
||||
}
|
||||
})
|
||||
);
|
||||
|
||||
let while_begin_end = "\
|
||||
WHILE @@FETCH_STATUS = 0 \
|
||||
BEGIN \
|
||||
FETCH NEXT FROM Employee_Cursor; \
|
||||
END\
|
||||
";
|
||||
let _ = ms().verified_stmt(while_begin_end);
|
||||
|
||||
let while_begin_end_multiple_statements = "\
|
||||
WHILE @@FETCH_STATUS = 0 \
|
||||
BEGIN \
|
||||
FETCH NEXT FROM Employee_Cursor; \
|
||||
PRINT 'Hello World'; \
|
||||
END\
|
||||
";
|
||||
let _ = ms().verified_stmt(while_begin_end_multiple_statements);
|
||||
}
|
||||
|
||||
#[test]
|
||||
|
@ -1359,7 +1673,7 @@ fn parse_use() {
|
|||
for object_name in &valid_object_names {
|
||||
// Test single identifier without quotes
|
||||
assert_eq!(
|
||||
ms().verified_stmt(&format!("USE {}", object_name)),
|
||||
ms().verified_stmt(&format!("USE {object_name}")),
|
||||
Statement::Use(Use::Object(ObjectName::from(vec![Ident::new(
|
||||
object_name.to_string()
|
||||
)])))
|
||||
|
@ -1367,7 +1681,7 @@ fn parse_use() {
|
|||
for "e in "e_styles {
|
||||
// Test single identifier with different type of quotes
|
||||
assert_eq!(
|
||||
ms().verified_stmt(&format!("USE {}{}{}", quote, object_name, quote)),
|
||||
ms().verified_stmt(&format!("USE {quote}{object_name}{quote}")),
|
||||
Statement::Use(Use::Object(ObjectName::from(vec![Ident::with_quote(
|
||||
quote,
|
||||
object_name.to_string(),
|
||||
|
@ -1556,7 +1870,6 @@ fn parse_create_table_with_valid_options() {
|
|||
span: Span::empty(),
|
||||
},
|
||||
data_type: Int(None,),
|
||||
|
||||
options: vec![],
|
||||
},
|
||||
ColumnDef {
|
||||
|
@ -1566,7 +1879,6 @@ fn parse_create_table_with_valid_options() {
|
|||
span: Span::empty(),
|
||||
},
|
||||
data_type: Int(None,),
|
||||
|
||||
options: vec![],
|
||||
},
|
||||
],
|
||||
|
@ -1578,19 +1890,13 @@ fn parse_create_table_with_valid_options() {
|
|||
storage: None,
|
||||
location: None,
|
||||
},),
|
||||
table_properties: vec![],
|
||||
with_options,
|
||||
file_format: None,
|
||||
location: None,
|
||||
query: None,
|
||||
without_rowid: false,
|
||||
like: None,
|
||||
clone: None,
|
||||
engine: None,
|
||||
comment: None,
|
||||
auto_increment_offset: None,
|
||||
default_charset: None,
|
||||
collation: None,
|
||||
on_commit: None,
|
||||
on_cluster: None,
|
||||
primary_key: None,
|
||||
|
@ -1598,7 +1904,7 @@ fn parse_create_table_with_valid_options() {
|
|||
partition_by: None,
|
||||
cluster_by: None,
|
||||
clustered_by: None,
|
||||
options: None,
|
||||
inherits: None,
|
||||
strict: false,
|
||||
iceberg: false,
|
||||
copy_grants: false,
|
||||
|
@ -1615,11 +1921,28 @@ fn parse_create_table_with_valid_options() {
|
|||
catalog: None,
|
||||
catalog_sync: None,
|
||||
storage_serialization_policy: None,
|
||||
table_options: CreateTableOptions::With(with_options)
|
||||
})
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn parse_nested_slash_star_comment() {
|
||||
let sql = r#"
|
||||
select
|
||||
/*
|
||||
comment level 1
|
||||
/*
|
||||
comment level 2
|
||||
*/
|
||||
*/
|
||||
1;
|
||||
"#;
|
||||
let canonical = "SELECT 1";
|
||||
ms().one_statement_parses_to(sql, canonical);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn parse_create_table_with_invalid_options() {
|
||||
let invalid_cases = vec![
|
||||
|
@ -1732,19 +2055,13 @@ fn parse_create_table_with_identity_column() {
|
|||
storage: None,
|
||||
location: None,
|
||||
},),
|
||||
table_properties: vec![],
|
||||
with_options: vec![],
|
||||
file_format: None,
|
||||
location: None,
|
||||
query: None,
|
||||
without_rowid: false,
|
||||
like: None,
|
||||
clone: None,
|
||||
engine: None,
|
||||
comment: None,
|
||||
auto_increment_offset: None,
|
||||
default_charset: None,
|
||||
collation: None,
|
||||
on_commit: None,
|
||||
on_cluster: None,
|
||||
primary_key: None,
|
||||
|
@ -1752,7 +2069,7 @@ fn parse_create_table_with_identity_column() {
|
|||
partition_by: None,
|
||||
cluster_by: None,
|
||||
clustered_by: None,
|
||||
options: None,
|
||||
inherits: None,
|
||||
strict: false,
|
||||
copy_grants: false,
|
||||
enable_schema_evolution: None,
|
||||
|
@ -1768,6 +2085,7 @@ fn parse_create_table_with_identity_column() {
|
|||
catalog: None,
|
||||
catalog_sync: None,
|
||||
storage_serialization_policy: None,
|
||||
table_options: CreateTableOptions::None
|
||||
}),
|
||||
);
|
||||
}
|
||||
|
@ -1846,6 +2164,104 @@ fn parse_mssql_set_session_value() {
|
|||
ms().verified_stmt("SET ANSI_NULLS, ANSI_PADDING ON");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn parse_mssql_if_else() {
|
||||
// Simple statements and blocks
|
||||
ms().verified_stmt("IF 1 = 1 SELECT '1'; ELSE SELECT '2';");
|
||||
ms().verified_stmt("IF 1 = 1 BEGIN SET @A = 1; END ELSE SET @A = 2;");
|
||||
ms().verified_stmt(
|
||||
"IF DATENAME(weekday, GETDATE()) IN (N'Saturday', N'Sunday') SELECT 'Weekend'; ELSE SELECT 'Weekday';"
|
||||
);
|
||||
ms().verified_stmt(
|
||||
"IF (SELECT COUNT(*) FROM a.b WHERE c LIKE 'x%') > 1 SELECT 'yes'; ELSE SELECT 'No';",
|
||||
);
|
||||
|
||||
// Multiple statements
|
||||
let stmts = ms()
|
||||
.parse_sql_statements("DECLARE @A INT; IF 1=1 BEGIN SET @A = 1 END ELSE SET @A = 2")
|
||||
.unwrap();
|
||||
match &stmts[..] {
|
||||
[Statement::Declare { .. }, Statement::If(stmt)] => {
|
||||
assert_eq!(
|
||||
stmt.to_string(),
|
||||
"IF 1 = 1 BEGIN SET @A = 1; END ELSE SET @A = 2;"
|
||||
);
|
||||
}
|
||||
_ => panic!("Unexpected statements: {stmts:?}"),
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_mssql_if_else_span() {
|
||||
let sql = "IF 1 = 1 SELECT '1' ELSE SELECT '2'";
|
||||
let mut parser = Parser::new(&MsSqlDialect {}).try_with_sql(sql).unwrap();
|
||||
assert_eq!(
|
||||
parser.parse_statement().unwrap().span(),
|
||||
Span::new(Location::new(1, 1), Location::new(1, sql.len() as u64 + 1))
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_mssql_if_else_multiline_span() {
|
||||
let sql_line1 = "IF 1 = 1";
|
||||
let sql_line2 = "SELECT '1'";
|
||||
let sql_line3 = "ELSE SELECT '2'";
|
||||
let sql = [sql_line1, sql_line2, sql_line3].join("\n");
|
||||
let mut parser = Parser::new(&MsSqlDialect {}).try_with_sql(&sql).unwrap();
|
||||
assert_eq!(
|
||||
parser.parse_statement().unwrap().span(),
|
||||
Span::new(
|
||||
Location::new(1, 1),
|
||||
Location::new(3, sql_line3.len() as u64 + 1)
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_mssql_if_statements_span() {
|
||||
// Simple statements
|
||||
let mut sql = "IF 1 = 1 SELECT '1' ELSE SELECT '2'";
|
||||
let mut parser = Parser::new(&MsSqlDialect {}).try_with_sql(sql).unwrap();
|
||||
match parser.parse_statement().unwrap() {
|
||||
Statement::If(IfStatement {
|
||||
if_block,
|
||||
else_block: Some(else_block),
|
||||
..
|
||||
}) => {
|
||||
assert_eq!(
|
||||
if_block.span(),
|
||||
Span::new(Location::new(1, 1), Location::new(1, 20))
|
||||
);
|
||||
assert_eq!(
|
||||
else_block.span(),
|
||||
Span::new(Location::new(1, 21), Location::new(1, 36))
|
||||
);
|
||||
}
|
||||
stmt => panic!("Unexpected statement: {stmt:?}"),
|
||||
}
|
||||
|
||||
// Blocks
|
||||
sql = "IF 1 = 1 BEGIN SET @A = 1; END ELSE BEGIN SET @A = 2 END";
|
||||
parser = Parser::new(&MsSqlDialect {}).try_with_sql(sql).unwrap();
|
||||
match parser.parse_statement().unwrap() {
|
||||
Statement::If(IfStatement {
|
||||
if_block,
|
||||
else_block: Some(else_block),
|
||||
..
|
||||
}) => {
|
||||
assert_eq!(
|
||||
if_block.span(),
|
||||
Span::new(Location::new(1, 1), Location::new(1, 31))
|
||||
);
|
||||
assert_eq!(
|
||||
else_block.span(),
|
||||
Span::new(Location::new(1, 32), Location::new(1, 57))
|
||||
);
|
||||
}
|
||||
stmt => panic!("Unexpected statement: {stmt:?}"),
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn parse_mssql_varbinary_max_length() {
|
||||
let sql = "CREATE TABLE example (var_binary_col VARBINARY(MAX))";
|
||||
|
@ -1899,9 +2315,168 @@ fn parse_mssql_varbinary_max_length() {
|
|||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn parse_mssql_table_identifier_with_default_schema() {
|
||||
ms().verified_stmt("SELECT * FROM mydatabase..MyTable");
|
||||
}
|
||||
|
||||
fn ms() -> TestedDialects {
|
||||
TestedDialects::new(vec![Box::new(MsSqlDialect {})])
|
||||
}
|
||||
|
||||
fn ms_and_generic() -> TestedDialects {
|
||||
TestedDialects::new(vec![Box::new(MsSqlDialect {}), Box::new(GenericDialect {})])
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn parse_mssql_merge_with_output() {
|
||||
let stmt = "MERGE dso.products AS t \
|
||||
USING dsi.products AS \
|
||||
s ON s.ProductID = t.ProductID \
|
||||
WHEN MATCHED AND \
|
||||
NOT (t.ProductName = s.ProductName OR (ISNULL(t.ProductName, s.ProductName) IS NULL)) \
|
||||
THEN UPDATE SET t.ProductName = s.ProductName \
|
||||
WHEN NOT MATCHED BY TARGET \
|
||||
THEN INSERT (ProductID, ProductName) \
|
||||
VALUES (s.ProductID, s.ProductName) \
|
||||
WHEN NOT MATCHED BY SOURCE THEN DELETE \
|
||||
OUTPUT $action, deleted.ProductID INTO dsi.temp_products";
|
||||
ms_and_generic().verified_stmt(stmt);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn parse_create_trigger() {
|
||||
let create_trigger = "\
|
||||
CREATE OR ALTER TRIGGER reminder1 \
|
||||
ON Sales.Customer \
|
||||
AFTER INSERT, UPDATE \
|
||||
AS RAISERROR('Notify Customer Relations', 16, 10);\
|
||||
";
|
||||
let create_stmt = ms().verified_stmt(create_trigger);
|
||||
assert_eq!(
|
||||
create_stmt,
|
||||
Statement::CreateTrigger {
|
||||
or_alter: true,
|
||||
or_replace: false,
|
||||
is_constraint: false,
|
||||
name: ObjectName::from(vec![Ident::new("reminder1")]),
|
||||
period: TriggerPeriod::After,
|
||||
events: vec![TriggerEvent::Insert, TriggerEvent::Update(vec![]),],
|
||||
table_name: ObjectName::from(vec![Ident::new("Sales"), Ident::new("Customer")]),
|
||||
referenced_table_name: None,
|
||||
referencing: vec![],
|
||||
trigger_object: TriggerObject::Statement,
|
||||
include_each: false,
|
||||
condition: None,
|
||||
exec_body: None,
|
||||
statements: Some(ConditionalStatements::Sequence {
|
||||
statements: vec![Statement::RaisError {
|
||||
message: Box::new(Expr::Value(
|
||||
(Value::SingleQuotedString("Notify Customer Relations".to_string()))
|
||||
.with_empty_span()
|
||||
)),
|
||||
severity: Box::new(Expr::Value(
|
||||
(Value::Number("16".parse().unwrap(), false)).with_empty_span()
|
||||
)),
|
||||
state: Box::new(Expr::Value(
|
||||
(Value::Number("10".parse().unwrap(), false)).with_empty_span()
|
||||
)),
|
||||
arguments: vec![],
|
||||
options: vec![],
|
||||
}],
|
||||
}),
|
||||
characteristics: None,
|
||||
}
|
||||
);
|
||||
|
||||
let multi_statement_as_trigger = "\
|
||||
CREATE TRIGGER some_trigger ON some_table FOR INSERT \
|
||||
AS \
|
||||
DECLARE @var INT; \
|
||||
RAISERROR('Trigger fired', 10, 1);\
|
||||
";
|
||||
let _ = ms().verified_stmt(multi_statement_as_trigger);
|
||||
|
||||
let multi_statement_trigger = "\
|
||||
CREATE TRIGGER some_trigger ON some_table FOR INSERT \
|
||||
AS \
|
||||
BEGIN \
|
||||
DECLARE @var INT; \
|
||||
RAISERROR('Trigger fired', 10, 1); \
|
||||
END\
|
||||
";
|
||||
let _ = ms().verified_stmt(multi_statement_trigger);
|
||||
|
||||
let create_trigger_with_return = "\
|
||||
CREATE TRIGGER some_trigger ON some_table FOR INSERT \
|
||||
AS \
|
||||
BEGIN \
|
||||
RETURN; \
|
||||
END\
|
||||
";
|
||||
let _ = ms().verified_stmt(create_trigger_with_return);
|
||||
|
||||
let create_trigger_with_return = "\
|
||||
CREATE TRIGGER some_trigger ON some_table FOR INSERT \
|
||||
AS \
|
||||
BEGIN \
|
||||
RETURN; \
|
||||
END\
|
||||
";
|
||||
let _ = ms().verified_stmt(create_trigger_with_return);
|
||||
|
||||
let create_trigger_with_conditional = "\
|
||||
CREATE TRIGGER some_trigger ON some_table FOR INSERT \
|
||||
AS \
|
||||
BEGIN \
|
||||
IF 1 = 2 \
|
||||
BEGIN \
|
||||
RAISERROR('Trigger fired', 10, 1); \
|
||||
END; \
|
||||
RETURN; \
|
||||
END\
|
||||
";
|
||||
let _ = ms().verified_stmt(create_trigger_with_conditional);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn parse_drop_trigger() {
|
||||
let sql_drop_trigger = "DROP TRIGGER emp_stamp;";
|
||||
let drop_stmt = ms().one_statement_parses_to(sql_drop_trigger, "");
|
||||
assert_eq!(
|
||||
drop_stmt,
|
||||
Statement::DropTrigger {
|
||||
if_exists: false,
|
||||
trigger_name: ObjectName::from(vec![Ident::new("emp_stamp")]),
|
||||
table_name: None,
|
||||
option: None,
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn parse_print() {
|
||||
let print_string_literal = "PRINT 'Hello, world!'";
|
||||
let print_stmt = ms().verified_stmt(print_string_literal);
|
||||
assert_eq!(
|
||||
print_stmt,
|
||||
Statement::Print(PrintStatement {
|
||||
message: Box::new(Expr::Value(
|
||||
(Value::SingleQuotedString("Hello, world!".to_string())).with_empty_span()
|
||||
)),
|
||||
})
|
||||
);
|
||||
|
||||
let _ = ms().verified_stmt("PRINT N'Hello, ⛄️!'");
|
||||
let _ = ms().verified_stmt("PRINT @my_variable");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn parse_mssql_grant() {
|
||||
ms().verified_stmt("GRANT SELECT ON my_table TO public, db_admin");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn parse_mssql_deny() {
|
||||
ms().verified_stmt("DENY SELECT ON my_table TO public, db_admin");
|
||||
}
|
||||
|
|
File diff suppressed because it is too large
Load diff
File diff suppressed because it is too large
Load diff
|
@ -391,3 +391,19 @@ fn test_parse_nested_quoted_identifier() {
|
|||
.parse_sql_statements(r#"SELECT 1 AS ["1]"#)
|
||||
.is_err());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn parse_extract_single_quotes() {
|
||||
let sql = "SELECT EXTRACT('month' FROM my_timestamp) FROM my_table";
|
||||
redshift().verified_stmt(sql);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn parse_string_literal_backslash_escape() {
|
||||
redshift().one_statement_parses_to(r#"SELECT 'l\'auto'"#, "SELECT 'l''auto'");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn parse_utf8_multibyte_idents() {
|
||||
redshift().verified_stmt("SELECT 🚀.city AS 🎸 FROM customers AS 🚀");
|
||||
}
|
||||
|
|
File diff suppressed because it is too large
Load diff
|
@ -324,7 +324,7 @@ fn parse_create_table_on_conflict_col() {
|
|||
Keyword::IGNORE,
|
||||
Keyword::REPLACE,
|
||||
] {
|
||||
let sql = format!("CREATE TABLE t1 (a INT, b INT ON CONFLICT {:?})", keyword);
|
||||
let sql = format!("CREATE TABLE t1 (a INT, b INT ON CONFLICT {keyword:?})");
|
||||
match sqlite_and_generic().verified_stmt(&sql) {
|
||||
Statement::CreateTable(CreateTable { columns, .. }) => {
|
||||
assert_eq!(
|
||||
|
@ -410,7 +410,7 @@ fn parse_window_function_with_filter() {
|
|||
"count",
|
||||
"user_defined_function",
|
||||
] {
|
||||
let sql = format!("SELECT {}(x) FILTER (WHERE y) OVER () FROM t", func_name);
|
||||
let sql = format!("SELECT {func_name}(x) FILTER (WHERE y) OVER () FROM t");
|
||||
let select = sqlite().verified_only_select(&sql);
|
||||
assert_eq!(select.to_string(), sql);
|
||||
assert_eq!(
|
||||
|
@ -444,7 +444,7 @@ fn parse_window_function_with_filter() {
|
|||
fn parse_attach_database() {
|
||||
let sql = "ATTACH DATABASE 'test.db' AS test";
|
||||
let verified_stmt = sqlite().verified_stmt(sql);
|
||||
assert_eq!(sql, format!("{}", verified_stmt));
|
||||
assert_eq!(sql, format!("{verified_stmt}"));
|
||||
match verified_stmt {
|
||||
Statement::AttachDatabase {
|
||||
schema_name,
|
||||
|
@ -562,6 +562,36 @@ fn test_dollar_identifier_as_placeholder() {
|
|||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_match_operator() {
|
||||
assert_eq!(
|
||||
sqlite().verified_expr("col MATCH 'pattern'"),
|
||||
Expr::BinaryOp {
|
||||
op: BinaryOperator::Match,
|
||||
left: Box::new(Expr::Identifier(Ident::new("col"))),
|
||||
right: Box::new(Expr::Value(
|
||||
(Value::SingleQuotedString("pattern".to_string())).with_empty_span()
|
||||
))
|
||||
}
|
||||
);
|
||||
sqlite().verified_only_select("SELECT * FROM email WHERE email MATCH 'fts5'");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_regexp_operator() {
|
||||
assert_eq!(
|
||||
sqlite().verified_expr("col REGEXP 'pattern'"),
|
||||
Expr::BinaryOp {
|
||||
op: BinaryOperator::Regexp,
|
||||
left: Box::new(Expr::Identifier(Ident::new("col"))),
|
||||
right: Box::new(Expr::Value(
|
||||
(Value::SingleQuotedString("pattern".to_string())).with_empty_span()
|
||||
))
|
||||
}
|
||||
);
|
||||
sqlite().verified_only_select(r#"SELECT count(*) FROM messages WHERE msg_text REGEXP '\d+'"#);
|
||||
}
|
||||
|
||||
fn sqlite() -> TestedDialects {
|
||||
TestedDialects::new(vec![Box::new(SQLiteDialect {})])
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue