Skip to content

Commit 9cd94c5

Browse files
Merge branch 'main' into no-whitespace-parser
2 parents 2957be4 + 4f79997 commit 9cd94c5

19 files changed

+1452
-154
lines changed

src/ast/data_type.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -375,7 +375,7 @@ pub enum DataType {
375375
/// Databricks timestamp without time zone. See [1].
376376
///
377377
/// [1]: https://docs.databricks.com/aws/en/sql/language-manual/data-types/timestamp-ntz-type
378-
TimestampNtz,
378+
TimestampNtz(Option<u64>),
379379
/// Interval type.
380380
Interval {
381381
/// [PostgreSQL] fields specification like `INTERVAL YEAR TO MONTH`.
@@ -676,7 +676,9 @@ impl fmt::Display for DataType {
676676
DataType::Timestamp(precision, timezone_info) => {
677677
format_datetime_precision_and_tz(f, "TIMESTAMP", precision, timezone_info)
678678
}
679-
DataType::TimestampNtz => write!(f, "TIMESTAMP_NTZ"),
679+
DataType::TimestampNtz(precision) => {
680+
format_type_with_optional_length(f, "TIMESTAMP_NTZ", precision, false)
681+
}
680682
DataType::Datetime64(precision, timezone) => {
681683
format_clickhouse_datetime_precision_and_timezone(
682684
f,

src/ast/ddl.rs

Lines changed: 350 additions & 11 deletions
Large diffs are not rendered by default.

src/ast/dml.rs

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,17 +27,20 @@ use sqlparser_derive::{Visit, VisitMut};
2727
use crate::display_utils::{indented_list, Indent, SpaceOrNewline};
2828

2929
use super::{
30-
display_comma_separated, display_separated, query::InputFormatClause, Assignment,
31-
CopyLegacyCsvOption, CopyLegacyOption, CopyOption, CopySource, CopyTarget, Expr, FromTable,
32-
Ident, InsertAliases, MysqlInsertPriority, ObjectName, OnInsert, OrderByExpr, Query,
33-
SelectItem, Setting, SqliteOnConflict, TableObject, TableWithJoins, UpdateTableFromKind,
30+
display_comma_separated, display_separated, helpers::attached_token::AttachedToken,
31+
query::InputFormatClause, Assignment, CopyLegacyCsvOption, CopyLegacyOption, CopyOption,
32+
CopySource, CopyTarget, Expr, FromTable, Ident, InsertAliases, MysqlInsertPriority, ObjectName,
33+
OnInsert, OrderByExpr, Query, SelectItem, Setting, SqliteOnConflict, TableObject,
34+
TableWithJoins, UpdateTableFromKind,
3435
};
3536

3637
/// INSERT statement.
3738
#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
3839
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
3940
#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
4041
pub struct Insert {
42+
/// Token for the `INSERT` keyword (or its substitutes)
43+
pub insert_token: AttachedToken,
4144
/// Only for Sqlite
4245
pub or: Option<SqliteOnConflict>,
4346
/// Only for mysql
@@ -180,6 +183,8 @@ impl Display for Insert {
180183
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
181184
#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
182185
pub struct Delete {
186+
/// Token for the `DELETE` keyword
187+
pub delete_token: AttachedToken,
183188
/// Multi tables delete are supported in mysql
184189
pub tables: Vec<ObjectName>,
185190
/// FROM
@@ -247,6 +252,8 @@ impl Display for Delete {
247252
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
248253
#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
249254
pub struct Update {
255+
/// Token for the `UPDATE` keyword
256+
pub update_token: AttachedToken,
250257
/// TABLE
251258
pub table: TableWithJoins,
252259
/// Column assignments

src/ast/mod.rs

Lines changed: 115 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -59,19 +59,21 @@ pub use self::dcl::{
5959
AlterRoleOperation, CreateRole, ResetConfig, RoleOption, SecondaryRoles, SetConfigValue, Use,
6060
};
6161
pub use self::ddl::{
62-
AlterColumnOperation, AlterConnectorOwner, AlterIndexOperation, AlterPolicyOperation,
63-
AlterSchema, AlterSchemaOperation, AlterTable, AlterTableAlgorithm, AlterTableLock,
64-
AlterTableOperation, AlterType, AlterTypeAddValue, AlterTypeAddValuePosition,
65-
AlterTypeOperation, AlterTypeRename, AlterTypeRenameValue, ClusteredBy, ColumnDef,
66-
ColumnOption, ColumnOptionDef, ColumnOptions, ColumnPolicy, ColumnPolicyProperty,
67-
ConstraintCharacteristics, CreateConnector, CreateDomain, CreateExtension, CreateFunction,
68-
CreateIndex, CreateTable, CreateTrigger, CreateView, Deduplicate, DeferrableInitial,
69-
DropBehavior, DropExtension, DropFunction, DropTrigger, GeneratedAs, GeneratedExpressionMode,
70-
IdentityParameters, IdentityProperty, IdentityPropertyFormatKind, IdentityPropertyKind,
71-
IdentityPropertyOrder, IndexColumn, IndexOption, IndexType, KeyOrIndexDisplay, Msck,
72-
NullsDistinctOption, Owner, Partition, ProcedureParam, ReferentialAction, RenameTableNameKind,
73-
ReplicaIdentity, TagsColumnOption, TriggerObjectKind, Truncate,
74-
UserDefinedTypeCompositeAttributeDef, UserDefinedTypeRepresentation, ViewColumnDef,
62+
Alignment, AlterColumnOperation, AlterConnectorOwner, AlterIndexOperation,
63+
AlterPolicyOperation, AlterSchema, AlterSchemaOperation, AlterTable, AlterTableAlgorithm,
64+
AlterTableLock, AlterTableOperation, AlterTableType, AlterType, AlterTypeAddValue,
65+
AlterTypeAddValuePosition, AlterTypeOperation, AlterTypeRename, AlterTypeRenameValue,
66+
ClusteredBy, ColumnDef, ColumnOption, ColumnOptionDef, ColumnOptions, ColumnPolicy,
67+
ColumnPolicyProperty, ConstraintCharacteristics, CreateConnector, CreateDomain,
68+
CreateExtension, CreateFunction, CreateIndex, CreateTable, CreateTrigger, CreateView,
69+
Deduplicate, DeferrableInitial, DropBehavior, DropExtension, DropFunction, DropTrigger,
70+
GeneratedAs, GeneratedExpressionMode, IdentityParameters, IdentityProperty,
71+
IdentityPropertyFormatKind, IdentityPropertyKind, IdentityPropertyOrder, IndexColumn,
72+
IndexOption, IndexType, KeyOrIndexDisplay, Msck, NullsDistinctOption, Owner, Partition,
73+
ProcedureParam, ReferentialAction, RenameTableNameKind, ReplicaIdentity, TagsColumnOption,
74+
TriggerObjectKind, Truncate, UserDefinedTypeCompositeAttributeDef,
75+
UserDefinedTypeInternalLength, UserDefinedTypeRangeOption, UserDefinedTypeRepresentation,
76+
UserDefinedTypeSqlDefinitionOption, UserDefinedTypeStorage, ViewColumnDef,
7577
};
7678
pub use self::dml::{Copy, CsvFormatOptions, Delete, Insert, Update};
7779
pub use self::operator::{BinaryOperator, UnaryOperator};
@@ -2787,7 +2789,7 @@ impl fmt::Display for Declare {
27872789
}
27882790

27892791
/// Sql options of a `CREATE TABLE` statement.
2790-
#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash, Default)]
2792+
#[derive(Debug, Default, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
27912793
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
27922794
#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
27932795
pub enum CreateTableOptions {
@@ -2919,6 +2921,15 @@ pub enum Set {
29192921
/// MySQL-style
29202922
/// SET a = 1, b = 2, ..;
29212923
MultipleAssignments { assignments: Vec<SetAssignment> },
2924+
/// Session authorization for Postgres/Redshift
2925+
///
2926+
/// ```sql
2927+
/// SET SESSION AUTHORIZATION { user_name | DEFAULT }
2928+
/// ```
2929+
///
2930+
/// See <https://www.postgresql.org/docs/current/sql-set-session-authorization.html>
2931+
/// See <https://docs.aws.amazon.com/redshift/latest/dg/r_SET_SESSION_AUTHORIZATION.html>
2932+
SetSessionAuthorization(SetSessionAuthorizationParam),
29222933
/// MS-SQL session
29232934
///
29242935
/// See <https://learn.microsoft.com/en-us/sql/t-sql/statements/set-statements-transact-sql>
@@ -2993,6 +3004,7 @@ impl Display for Set {
29933004
modifier = context_modifier.map(|m| format!("{m}")).unwrap_or_default()
29943005
)
29953006
}
3007+
Self::SetSessionAuthorization(kind) => write!(f, "SET SESSION AUTHORIZATION {kind}"),
29963008
Self::SetSessionParam(kind) => write!(f, "SET {kind}"),
29973009
Self::SetTransaction {
29983010
modes,
@@ -4084,7 +4096,7 @@ pub enum Statement {
40844096
/// ```
40854097
CreateType {
40864098
name: ObjectName,
4087-
representation: UserDefinedTypeRepresentation,
4099+
representation: Option<UserDefinedTypeRepresentation>,
40884100
},
40894101
/// ```sql
40904102
/// PRAGMA <schema-name>.<pragma-name> = <pragma-value>
@@ -4245,6 +4257,14 @@ pub enum Statement {
42454257
/// ```
42464258
/// [Redshift](https://docs.aws.amazon.com/redshift/latest/dg/r_VACUUM_command.html)
42474259
Vacuum(VacuumStatement),
4260+
/// Restore the value of a run-time parameter to the default value.
4261+
///
4262+
/// ```sql
4263+
/// RESET configuration_parameter;
4264+
/// RESET ALL;
4265+
/// ```
4266+
/// [PostgreSQL](https://www.postgresql.org/docs/current/sql-reset.html)
4267+
Reset(ResetStatement),
42484268
}
42494269

42504270
impl From<Analyze> for Statement {
@@ -5589,7 +5609,11 @@ impl fmt::Display for Statement {
55895609
name,
55905610
representation,
55915611
} => {
5592-
write!(f, "CREATE TYPE {name} AS {representation}")
5612+
write!(f, "CREATE TYPE {name}")?;
5613+
if let Some(repr) = representation {
5614+
write!(f, " {repr}")?;
5615+
}
5616+
Ok(())
55935617
}
55945618
Statement::Pragma { name, value, is_eq } => {
55955619
write!(f, "PRAGMA {name}")?;
@@ -5702,6 +5726,7 @@ impl fmt::Display for Statement {
57025726
Statement::AlterSchema(s) => write!(f, "{s}"),
57035727
Statement::Vacuum(s) => write!(f, "{s}"),
57045728
Statement::AlterUser(s) => write!(f, "{s}"),
5729+
Statement::Reset(s) => write!(f, "{s}"),
57055730
}
57065731
}
57075732
}
@@ -9763,6 +9788,42 @@ impl fmt::Display for TableObject {
97639788
}
97649789
}
97659790

9791+
/// Represents a SET SESSION AUTHORIZATION statement
9792+
#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
9793+
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
9794+
#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
9795+
pub struct SetSessionAuthorizationParam {
9796+
pub scope: ContextModifier,
9797+
pub kind: SetSessionAuthorizationParamKind,
9798+
}
9799+
9800+
impl fmt::Display for SetSessionAuthorizationParam {
9801+
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
9802+
write!(f, "{}", self.kind)
9803+
}
9804+
}
9805+
9806+
/// Represents the parameter kind for SET SESSION AUTHORIZATION
9807+
#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
9808+
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
9809+
#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
9810+
pub enum SetSessionAuthorizationParamKind {
9811+
/// Default authorization
9812+
Default,
9813+
9814+
/// User name
9815+
User(Ident),
9816+
}
9817+
9818+
impl fmt::Display for SetSessionAuthorizationParamKind {
9819+
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
9820+
match self {
9821+
SetSessionAuthorizationParamKind::Default => write!(f, "DEFAULT"),
9822+
SetSessionAuthorizationParamKind::User(name) => write!(f, "{}", name),
9823+
}
9824+
}
9825+
}
9826+
97669827
#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
97679828
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
97689829
#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
@@ -10464,6 +10525,38 @@ impl fmt::Display for VacuumStatement {
1046410525
}
1046510526
}
1046610527

10528+
/// Variants of the RESET statement
10529+
#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
10530+
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
10531+
#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
10532+
pub enum Reset {
10533+
/// Resets all session parameters to their default values.
10534+
ALL,
10535+
10536+
/// Resets a specific session parameter to its default value.
10537+
ConfigurationParameter(ObjectName),
10538+
}
10539+
10540+
/// Resets a session parameter to its default value.
10541+
/// ```sql
10542+
/// RESET { ALL | <configuration_parameter> }
10543+
/// ```
10544+
#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
10545+
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
10546+
#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
10547+
pub struct ResetStatement {
10548+
pub reset: Reset,
10549+
}
10550+
10551+
impl fmt::Display for ResetStatement {
10552+
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
10553+
match &self.reset {
10554+
Reset::ALL => write!(f, "RESET ALL"),
10555+
Reset::ConfigurationParameter(param) => write!(f, "RESET {}", param),
10556+
}
10557+
}
10558+
}
10559+
1046710560
impl From<Set> for Statement {
1046810561
fn from(s: Set) -> Self {
1046910562
Self::Set(s)
@@ -10704,6 +10797,12 @@ impl From<VacuumStatement> for Statement {
1070410797
}
1070510798
}
1070610799

10800+
impl From<ResetStatement> for Statement {
10801+
fn from(r: ResetStatement) -> Self {
10802+
Self::Reset(r)
10803+
}
10804+
}
10805+
1070710806
#[cfg(test)]
1070810807
mod tests {
1070910808
use crate::tokenizer::Location;

src/ast/operator.rs

Lines changed: 25 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -33,35 +33,35 @@ use super::display_separated;
3333
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
3434
#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
3535
pub enum UnaryOperator {
36+
/// `@-@` Length or circumference (PostgreSQL/Redshift geometric operator)
37+
/// see <https://www.postgresql.org/docs/9.5/functions-geometry.html>
38+
AtDashAt,
39+
/// Unary logical not operator: e.g. `! false` (Hive-specific)
40+
BangNot,
41+
/// Bitwise Not, e.g. `~9`
42+
BitwiseNot,
43+
/// `@@` Center (PostgreSQL/Redshift geometric operator)
44+
/// see <https://www.postgresql.org/docs/9.5/functions-geometry.html>
45+
DoubleAt,
46+
/// `#` Number of points in path or polygon (PostgreSQL/Redshift geometric operator)
47+
/// see <https://www.postgresql.org/docs/9.5/functions-geometry.html>
48+
Hash,
3649
/// Plus, e.g. `+9`
3750
Plus,
3851
/// Minus, e.g. `-9`
3952
Minus,
4053
/// Not, e.g. `NOT(true)`
4154
Not,
42-
/// Bitwise Not, e.g. `~9` (PostgreSQL-specific)
43-
PGBitwiseNot,
44-
/// Square root, e.g. `|/9` (PostgreSQL-specific)
45-
PGSquareRoot,
55+
/// Absolute value, e.g. `@ -9` (PostgreSQL-specific)
56+
PGAbs,
4657
/// Cube root, e.g. `||/27` (PostgreSQL-specific)
4758
PGCubeRoot,
4859
/// Factorial, e.g. `9!` (PostgreSQL-specific)
4960
PGPostfixFactorial,
5061
/// Factorial, e.g. `!!9` (PostgreSQL-specific)
5162
PGPrefixFactorial,
52-
/// Absolute value, e.g. `@ -9` (PostgreSQL-specific)
53-
PGAbs,
54-
/// Unary logical not operator: e.g. `! false` (Hive-specific)
55-
BangNot,
56-
/// `#` Number of points in path or polygon (PostgreSQL/Redshift geometric operator)
57-
/// see <https://www.postgresql.org/docs/9.5/functions-geometry.html>
58-
Hash,
59-
/// `@-@` Length or circumference (PostgreSQL/Redshift geometric operator)
60-
/// see <https://www.postgresql.org/docs/9.5/functions-geometry.html>
61-
AtDashAt,
62-
/// `@@` Center (PostgreSQL/Redshift geometric operator)
63-
/// see <https://www.postgresql.org/docs/9.5/functions-geometry.html>
64-
DoubleAt,
63+
/// Square root, e.g. `|/9` (PostgreSQL-specific)
64+
PGSquareRoot,
6565
/// `?-` Is horizontal? (PostgreSQL/Redshift geometric operator)
6666
/// see <https://www.postgresql.org/docs/9.5/functions-geometry.html>
6767
QuestionDash,
@@ -73,19 +73,19 @@ pub enum UnaryOperator {
7373
impl fmt::Display for UnaryOperator {
7474
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
7575
f.write_str(match self {
76-
UnaryOperator::Plus => "+",
76+
UnaryOperator::AtDashAt => "@-@",
77+
UnaryOperator::BangNot => "!",
78+
UnaryOperator::BitwiseNot => "~",
79+
UnaryOperator::DoubleAt => "@@",
80+
UnaryOperator::Hash => "#",
7781
UnaryOperator::Minus => "-",
7882
UnaryOperator::Not => "NOT",
79-
UnaryOperator::PGBitwiseNot => "~",
80-
UnaryOperator::PGSquareRoot => "|/",
83+
UnaryOperator::PGAbs => "@",
8184
UnaryOperator::PGCubeRoot => "||/",
8285
UnaryOperator::PGPostfixFactorial => "!",
8386
UnaryOperator::PGPrefixFactorial => "!!",
84-
UnaryOperator::PGAbs => "@",
85-
UnaryOperator::BangNot => "!",
86-
UnaryOperator::Hash => "#",
87-
UnaryOperator::AtDashAt => "@-@",
88-
UnaryOperator::DoubleAt => "@@",
87+
UnaryOperator::PGSquareRoot => "|/",
88+
UnaryOperator::Plus => "+",
8989
UnaryOperator::QuestionDash => "?-",
9090
UnaryOperator::QuestionPipe => "?|",
9191
})

src/ast/query.rs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3135,12 +3135,18 @@ pub struct Values {
31353135
/// Was there an explicit ROWs keyword (MySQL)?
31363136
/// <https://dev.mysql.com/doc/refman/8.0/en/values.html>
31373137
pub explicit_row: bool,
3138+
// MySql supports both VALUES and VALUE keywords.
3139+
// <https://dev.mysql.com/doc/refman/9.2/en/insert.html>
3140+
pub value_keyword: bool,
31383141
pub rows: Vec<Vec<Expr>>,
31393142
}
31403143

31413144
impl fmt::Display for Values {
31423145
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
3143-
f.write_str("VALUES")?;
3146+
match self.value_keyword {
3147+
true => f.write_str("VALUE")?,
3148+
false => f.write_str("VALUES")?,
3149+
};
31443150
let prefix = if self.explicit_row { "ROW" } else { "" };
31453151
let mut delim = "";
31463152
for row in &self.rows {

0 commit comments

Comments
 (0)