Skip to content

Commit 0e20b9e

Browse files
authored
Merge branch 'main' into ast-visitor
2 parents 4507be8 + f7f14df commit 0e20b9e

9 files changed

+391
-88
lines changed

CHANGELOG.md

+17
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,23 @@ Given that the parser produces a typed AST, any changes to the AST will technica
88
## [Unreleased]
99
Check https://github.com/sqlparser-rs/sqlparser-rs/commits/main for undocumented changes.
1010

11+
## [0.25.0] 2022-10-03
12+
13+
### Added
14+
15+
* Support `AUTHORIZATION` clause in `CREATE SCHEMA` statements (#646) - Thanks @AugustoFKL
16+
* Support optional precision for `CLOB` and `BLOB` (#639) - Thanks @AugustoFKL
17+
* Support optional precision in `VARBINARY` and `BINARY` (#637) - Thanks @AugustoFKL
18+
19+
20+
### Changed
21+
* `TIMESTAMP` and `TIME` parsing preserve zone information (#646) - Thanks @AugustoFKL
22+
23+
### Fixed
24+
25+
* Correct order of arguments when parsing `LIMIT x,y` , restrict to `MySql` and `Generic` dialects - Thanks @AugustoFKL
26+
27+
1128
## [0.24.0] 2022-09-29
1229

1330
### Added

Cargo.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
[package]
22
name = "sqlparser"
33
description = "Extensible SQL Lexer and Parser with support for ANSI SQL:2011"
4-
version = "0.24.0"
4+
version = "0.25.0"
55
authors = ["Andy Grove <[email protected]>"]
66
homepage = "https://github.com/sqlparser-rs/sqlparser-rs"
77
documentation = "https://docs.rs/sqlparser/"

src/ast/data_type.rs

+93-22
Original file line numberDiff line numberDiff line change
@@ -29,22 +29,40 @@ use super::value::escape_single_quote_string;
2929
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
3030
#[cfg_attr(feature = "derive-visitor", derive(Drive, DriveMut))]
3131
pub enum DataType {
32-
/// Fixed-length character type e.g. CHAR(10)
32+
/// Fixed-length character type e.g. CHARACTER(10)
33+
Character(#[cfg_attr(feature = "derive-visitor", drive(skip))] Option<u64>),
34+
/// Fixed-length char type e.g. CHAR(10)
3335
Char(#[cfg_attr(feature = "derive-visitor", drive(skip))] Option<u64>),
36+
/// Character varying type e.g. CHARACTER VARYING(10)
37+
CharacterVarying(#[cfg_attr(feature = "derive-visitor", drive(skip))] Option<u64>),
38+
/// Char varying type e.g. CHAR VARYING(10)
39+
CharVarying(#[cfg_attr(feature = "derive-visitor", drive(skip))] Option<u64>),
3440
/// Variable-length character type e.g. VARCHAR(10)
3541
Varchar(#[cfg_attr(feature = "derive-visitor", drive(skip))] Option<u64>),
3642
/// Variable-length character type e.g. NVARCHAR(10)
3743
Nvarchar(#[cfg_attr(feature = "derive-visitor", drive(skip))] Option<u64>),
3844
/// Uuid type
3945
Uuid,
40-
/// Large character object e.g. CLOB(1000)
41-
Clob(#[cfg_attr(feature = "derive-visitor", drive(skip))] u64),
42-
/// Fixed-length binary type e.g. BINARY(10)
43-
Binary(#[cfg_attr(feature = "derive-visitor", drive(skip))] u64),
44-
/// Variable-length binary type e.g. VARBINARY(10)
45-
Varbinary(#[cfg_attr(feature = "derive-visitor", drive(skip))] u64),
46-
/// Large binary object e.g. BLOB(1000)
47-
Blob(#[cfg_attr(feature = "derive-visitor", drive(skip))] u64),
46+
/// Large character object with optional length e.g. CLOB, CLOB(1000), [standard], [Oracle]
47+
///
48+
/// [standard]: https://jakewheat.github.io/sql-overview/sql-2016-foundation-grammar.html#character-large-object-type
49+
/// [Oracle]: https://docs.oracle.com/javadb/10.10.1.2/ref/rrefclob.html
50+
Clob(#[cfg_attr(feature = "derive-visitor", drive(skip))] Option<u64>),
51+
/// Fixed-length binary type with optional length e.g. [standard], [MS SQL Server]
52+
///
53+
/// [standard]: https://jakewheat.github.io/sql-overview/sql-2016-foundation-grammar.html#binary-string-type
54+
/// [MS SQL Server]: https://learn.microsoft.com/pt-br/sql/t-sql/data-types/binary-and-varbinary-transact-sql?view=sql-server-ver16
55+
Binary(#[cfg_attr(feature = "derive-visitor", drive(skip))] Option<u64>),
56+
/// Variable-length binary with optional length type e.g. [standard], [MS SQL Server]
57+
///
58+
/// [standard]: https://jakewheat.github.io/sql-overview/sql-2016-foundation-grammar.html#binary-string-type
59+
/// [MS SQL Server]: https://learn.microsoft.com/pt-br/sql/t-sql/data-types/binary-and-varbinary-transact-sql?view=sql-server-ver16
60+
Varbinary(#[cfg_attr(feature = "derive-visitor", drive(skip))] Option<u64>),
61+
/// Large binary object with optional length e.g. BLOB, BLOB(1000), [standard], [Oracle]
62+
///
63+
/// [standard]: https://jakewheat.github.io/sql-overview/sql-2016-foundation-grammar.html#binary-large-object-string-type
64+
/// [Oracle]: https://docs.oracle.com/javadb/10.8.3.0/ref/rrefblob.html
65+
Blob(#[cfg_attr(feature = "derive-visitor", drive(skip))] Option<u64>),
4866
/// Decimal type with optional precision and scale e.g. DECIMAL(10,2)
4967
Decimal(
5068
#[cfg_attr(feature = "derive-visitor", drive(skip))] Option<u64>,
@@ -94,13 +112,11 @@ pub enum DataType {
94112
/// Date
95113
Date,
96114
/// Time
97-
Time,
115+
Time(TimezoneInfo),
98116
/// Datetime
99117
Datetime,
100-
/// Timestamp [Without Time Zone]
101-
Timestamp,
102-
/// Timestamp With Time Zone
103-
TimestampTz,
118+
/// Timestamp
119+
Timestamp(TimezoneInfo),
104120
/// Interval
105121
Interval,
106122
/// Regclass used in postgresql serial
@@ -124,18 +140,27 @@ pub enum DataType {
124140
impl fmt::Display for DataType {
125141
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
126142
match self {
143+
DataType::Character(size) => {
144+
format_type_with_optional_length(f, "CHARACTER", size, false)
145+
}
127146
DataType::Char(size) => format_type_with_optional_length(f, "CHAR", size, false),
128-
DataType::Varchar(size) => {
147+
DataType::CharacterVarying(size) => {
129148
format_type_with_optional_length(f, "CHARACTER VARYING", size, false)
130149
}
150+
DataType::CharVarying(size) => {
151+
format_type_with_optional_length(f, "CHAR VARYING", size, false)
152+
}
153+
DataType::Varchar(size) => format_type_with_optional_length(f, "VARCHAR", size, false),
131154
DataType::Nvarchar(size) => {
132155
format_type_with_optional_length(f, "NVARCHAR", size, false)
133156
}
134157
DataType::Uuid => write!(f, "UUID"),
135-
DataType::Clob(size) => write!(f, "CLOB({})", size),
136-
DataType::Binary(size) => write!(f, "BINARY({})", size),
137-
DataType::Varbinary(size) => write!(f, "VARBINARY({})", size),
138-
DataType::Blob(size) => write!(f, "BLOB({})", size),
158+
DataType::Clob(size) => format_type_with_optional_length(f, "CLOB", size, false),
159+
DataType::Binary(size) => format_type_with_optional_length(f, "BINARY", size, false),
160+
DataType::Varbinary(size) => {
161+
format_type_with_optional_length(f, "VARBINARY", size, false)
162+
}
163+
DataType::Blob(size) => format_type_with_optional_length(f, "BLOB", size, false),
139164
DataType::Decimal(precision, scale) => {
140165
if let Some(scale) = scale {
141166
write!(f, "NUMERIC({},{})", precision.unwrap(), scale)
@@ -183,10 +208,9 @@ impl fmt::Display for DataType {
183208
DataType::DoublePrecision => write!(f, "DOUBLE PRECISION"),
184209
DataType::Boolean => write!(f, "BOOLEAN"),
185210
DataType::Date => write!(f, "DATE"),
186-
DataType::Time => write!(f, "TIME"),
211+
DataType::Time(timezone_info) => write!(f, "TIME{}", timezone_info),
187212
DataType::Datetime => write!(f, "DATETIME"),
188-
DataType::Timestamp => write!(f, "TIMESTAMP"),
189-
DataType::TimestampTz => write!(f, "TIMESTAMPTZ"),
213+
DataType::Timestamp(timezone_info) => write!(f, "TIMESTAMP{}", timezone_info),
190214
DataType::Interval => write!(f, "INTERVAL"),
191215
DataType::Regclass => write!(f, "REGCLASS"),
192216
DataType::Text => write!(f, "TEXT"),
@@ -233,3 +257,50 @@ fn format_type_with_optional_length(
233257
}
234258
Ok(())
235259
}
260+
261+
/// Timestamp and Time data types information about TimeZone formatting.
262+
///
263+
/// This is more related to a display information than real differences between each variant. To
264+
/// guarantee compatibility with the input query we must maintain its exact information.
265+
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
266+
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
267+
pub enum TimezoneInfo {
268+
/// No information about time zone. E.g., TIMESTAMP
269+
None,
270+
/// Temporal type 'WITH TIME ZONE'. E.g., TIMESTAMP WITH TIME ZONE, [standard], [Oracle]
271+
///
272+
/// [standard]: https://jakewheat.github.io/sql-overview/sql-2016-foundation-grammar.html#datetime-type
273+
/// [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
274+
WithTimeZone,
275+
/// Temporal type 'WITHOUT TIME ZONE'. E.g., TIME WITHOUT TIME ZONE, [standard], [Postgresql]
276+
///
277+
/// [standard]: https://jakewheat.github.io/sql-overview/sql-2016-foundation-grammar.html#datetime-type
278+
/// [Postgresql]: https://www.postgresql.org/docs/current/datatype-datetime.html
279+
WithoutTimeZone,
280+
/// Postgresql specific `WITH TIME ZONE` formatting, for both TIME and TIMESTAMP. E.g., TIMETZ, [Postgresql]
281+
///
282+
/// [Postgresql]: https://www.postgresql.org/docs/current/datatype-datetime.html
283+
Tz,
284+
}
285+
286+
impl fmt::Display for TimezoneInfo {
287+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
288+
match self {
289+
TimezoneInfo::None => {
290+
write!(f, "")
291+
}
292+
TimezoneInfo::WithTimeZone => {
293+
write!(f, " WITH TIME ZONE")
294+
}
295+
TimezoneInfo::WithoutTimeZone => {
296+
write!(f, " WITHOUT TIME ZONE")
297+
}
298+
TimezoneInfo::Tz => {
299+
// TZ is the only one that is displayed BEFORE the precision, so the datatype display
300+
// must be aware of that. Check <https://www.postgresql.org/docs/14/datatype-datetime.html>
301+
// for more information
302+
write!(f, "TZ")
303+
}
304+
}
305+
}
306+
}

src/ast/mod.rs

+33-1
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ pub use derive_visitor::{
3535
};
3636

3737
pub use self::data_type::DataType;
38+
pub use self::data_type::TimezoneInfo;
3839
pub use self::ddl::{
3940
AlterColumnOperation, AlterTableOperation, ColumnDef, ColumnOption, ColumnOptionDef,
4041
ReferentialAction, TableConstraint,
@@ -1406,7 +1407,8 @@ pub enum Statement {
14061407
},
14071408
/// CREATE SCHEMA
14081409
CreateSchema {
1409-
schema_name: ObjectName,
1410+
/// `<schema name> | AUTHORIZATION <schema authorization identifier> | <schema name> AUTHORIZATION <schema authorization identifier>`
1411+
schema_name: SchemaName,
14101412
#[cfg_attr(feature = "derive-visitor", drive(skip))]
14111413
if_not_exists: bool,
14121414
},
@@ -3446,6 +3448,36 @@ impl fmt::Display for CreateFunctionUsing {
34463448
}
34473449
}
34483450

3451+
/// Schema possible naming variants ([1]).
3452+
///
3453+
/// [1]: https://jakewheat.github.io/sql-overview/sql-2016-foundation-grammar.html#schema-definition
3454+
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
3455+
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
3456+
pub enum SchemaName {
3457+
/// Only schema name specified: `<schema name>`.
3458+
Simple(ObjectName),
3459+
/// Only authorization identifier specified: `AUTHORIZATION <schema authorization identifier>`.
3460+
UnnamedAuthorization(Ident),
3461+
/// Both schema name and authorization identifier specified: `<schema name> AUTHORIZATION <schema authorization identifier>`.
3462+
NamedAuthorization(ObjectName, Ident),
3463+
}
3464+
3465+
impl fmt::Display for SchemaName {
3466+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
3467+
match self {
3468+
SchemaName::Simple(name) => {
3469+
write!(f, "{name}")
3470+
}
3471+
SchemaName::UnnamedAuthorization(authorization) => {
3472+
write!(f, "AUTHORIZATION {authorization}")
3473+
}
3474+
SchemaName::NamedAuthorization(name, authorization) => {
3475+
write!(f, "{name} AUTHORIZATION {authorization}")
3476+
}
3477+
}
3478+
}
3479+
}
3480+
34493481
#[cfg(test)]
34503482
mod tests {
34513483
use super::*;

src/keywords.rs

+1
Original file line numberDiff line numberDiff line change
@@ -532,6 +532,7 @@ define_keywords!(
532532
TIME,
533533
TIMESTAMP,
534534
TIMESTAMPTZ,
535+
TIMETZ,
535536
TIMEZONE,
536537
TIMEZONE_HOUR,
537538
TIMEZONE_MINUTE,

0 commit comments

Comments
 (0)