Skip to content

Commit bbf1790

Browse files
Merge branch 'main' into operator-extension
2 parents 994344d + 4f79997 commit bbf1790

17 files changed

+1378
-118
lines changed

src/ast/ddl.rs

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

src/ast/dml.rs

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,16 +27,19 @@ use sqlparser_derive::{Visit, VisitMut};
2727
use crate::display_utils::{indented_list, Indent, SpaceOrNewline};
2828

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

3536
/// INSERT statement.
3637
#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
3738
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
3839
#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
3940
pub struct Insert {
41+
/// Token for the `INSERT` keyword (or its substitutes)
42+
pub insert_token: AttachedToken,
4043
/// Only for Sqlite
4144
pub or: Option<SqliteOnConflict>,
4245
/// Only for mysql
@@ -179,6 +182,8 @@ impl Display for Insert {
179182
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
180183
#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
181184
pub struct Delete {
185+
/// Token for the `DELETE` keyword
186+
pub delete_token: AttachedToken,
182187
/// Multi tables delete are supported in mysql
183188
pub tables: Vec<ObjectName>,
184189
/// FROM
@@ -246,6 +251,8 @@ impl Display for Delete {
246251
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
247252
#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
248253
pub struct Update {
254+
/// Token for the `UPDATE` keyword
255+
pub update_token: AttachedToken,
249256
/// TABLE
250257
pub table: TableWithJoins,
251258
/// Column assignments

src/ast/mod.rs

Lines changed: 115 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -59,20 +59,22 @@ 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, CreateOperator, CreateOperatorClass, CreateOperatorFamily, CreateTable,
69-
CreateTrigger, CreateView, Deduplicate, DeferrableInitial, DropBehavior, DropExtension,
70-
DropFunction, DropTrigger, GeneratedAs, GeneratedExpressionMode, IdentityParameters,
71-
IdentityProperty, IdentityPropertyFormatKind, IdentityPropertyKind, IdentityPropertyOrder,
72-
IndexColumn, IndexOption, IndexType, KeyOrIndexDisplay, Msck, NullsDistinctOption,
73-
OperatorArgTypes, OperatorClassItem, OperatorPurpose, Owner, Partition, ProcedureParam,
74-
ReferentialAction, RenameTableNameKind, ReplicaIdentity, TagsColumnOption, TriggerObjectKind,
75-
Truncate, 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, CreateOperator, CreateOperatorClass,
69+
CreateOperatorFamily, CreateTable, CreateTrigger, CreateView, Deduplicate, DeferrableInitial,
70+
DropBehavior, DropExtension, DropFunction, DropTrigger, GeneratedAs, GeneratedExpressionMode,
71+
IdentityParameters, IdentityProperty, IdentityPropertyFormatKind, IdentityPropertyKind,
72+
IdentityPropertyOrder, IndexColumn, IndexOption, IndexType, KeyOrIndexDisplay, Msck,
73+
NullsDistinctOption, OperatorArgTypes, OperatorClassItem, OperatorPurpose, Owner, Partition,
74+
ProcedureParam, ReferentialAction, RenameTableNameKind, ReplicaIdentity, TagsColumnOption,
75+
TriggerObjectKind, Truncate, UserDefinedTypeCompositeAttributeDef,
76+
UserDefinedTypeInternalLength, UserDefinedTypeRangeOption, UserDefinedTypeRepresentation,
77+
UserDefinedTypeSqlDefinitionOption, UserDefinedTypeStorage, ViewColumnDef,
7678
};
7779
pub use self::dml::{Delete, Insert, Update};
7880
pub use self::operator::{BinaryOperator, UnaryOperator};
@@ -2920,6 +2922,15 @@ pub enum Set {
29202922
/// MySQL-style
29212923
/// SET a = 1, b = 2, ..;
29222924
MultipleAssignments { assignments: Vec<SetAssignment> },
2925+
/// Session authorization for Postgres/Redshift
2926+
///
2927+
/// ```sql
2928+
/// SET SESSION AUTHORIZATION { user_name | DEFAULT }
2929+
/// ```
2930+
///
2931+
/// See <https://www.postgresql.org/docs/current/sql-set-session-authorization.html>
2932+
/// See <https://docs.aws.amazon.com/redshift/latest/dg/r_SET_SESSION_AUTHORIZATION.html>
2933+
SetSessionAuthorization(SetSessionAuthorizationParam),
29232934
/// MS-SQL session
29242935
///
29252936
/// See <https://learn.microsoft.com/en-us/sql/t-sql/statements/set-statements-transact-sql>
@@ -2994,6 +3005,7 @@ impl Display for Set {
29943005
modifier = context_modifier.map(|m| format!("{m}")).unwrap_or_default()
29953006
)
29963007
}
3008+
Self::SetSessionAuthorization(kind) => write!(f, "SET SESSION AUTHORIZATION {kind}"),
29973009
Self::SetSessionParam(kind) => write!(f, "SET {kind}"),
29983010
Self::SetTransaction {
29993011
modes,
@@ -4113,7 +4125,7 @@ pub enum Statement {
41134125
/// ```
41144126
CreateType {
41154127
name: ObjectName,
4116-
representation: UserDefinedTypeRepresentation,
4128+
representation: Option<UserDefinedTypeRepresentation>,
41174129
},
41184130
/// ```sql
41194131
/// PRAGMA <schema-name>.<pragma-name> = <pragma-value>
@@ -4274,6 +4286,14 @@ pub enum Statement {
42744286
/// ```
42754287
/// [Redshift](https://docs.aws.amazon.com/redshift/latest/dg/r_VACUUM_command.html)
42764288
Vacuum(VacuumStatement),
4289+
/// Restore the value of a run-time parameter to the default value.
4290+
///
4291+
/// ```sql
4292+
/// RESET configuration_parameter;
4293+
/// RESET ALL;
4294+
/// ```
4295+
/// [PostgreSQL](https://www.postgresql.org/docs/current/sql-reset.html)
4296+
Reset(ResetStatement),
42774297
}
42784298

42794299
impl From<Analyze> for Statement {
@@ -5660,7 +5680,11 @@ impl fmt::Display for Statement {
56605680
name,
56615681
representation,
56625682
} => {
5663-
write!(f, "CREATE TYPE {name} AS {representation}")
5683+
write!(f, "CREATE TYPE {name}")?;
5684+
if let Some(repr) = representation {
5685+
write!(f, " {repr}")?;
5686+
}
5687+
Ok(())
56645688
}
56655689
Statement::Pragma { name, value, is_eq } => {
56665690
write!(f, "PRAGMA {name}")?;
@@ -5773,6 +5797,7 @@ impl fmt::Display for Statement {
57735797
Statement::AlterSchema(s) => write!(f, "{s}"),
57745798
Statement::Vacuum(s) => write!(f, "{s}"),
57755799
Statement::AlterUser(s) => write!(f, "{s}"),
5800+
Statement::Reset(s) => write!(f, "{s}"),
57765801
}
57775802
}
57785803
}
@@ -9834,6 +9859,42 @@ impl fmt::Display for TableObject {
98349859
}
98359860
}
98369861

9862+
/// Represents a SET SESSION AUTHORIZATION statement
9863+
#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
9864+
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
9865+
#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
9866+
pub struct SetSessionAuthorizationParam {
9867+
pub scope: ContextModifier,
9868+
pub kind: SetSessionAuthorizationParamKind,
9869+
}
9870+
9871+
impl fmt::Display for SetSessionAuthorizationParam {
9872+
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
9873+
write!(f, "{}", self.kind)
9874+
}
9875+
}
9876+
9877+
/// Represents the parameter kind for SET SESSION AUTHORIZATION
9878+
#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
9879+
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
9880+
#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
9881+
pub enum SetSessionAuthorizationParamKind {
9882+
/// Default authorization
9883+
Default,
9884+
9885+
/// User name
9886+
User(Ident),
9887+
}
9888+
9889+
impl fmt::Display for SetSessionAuthorizationParamKind {
9890+
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
9891+
match self {
9892+
SetSessionAuthorizationParamKind::Default => write!(f, "DEFAULT"),
9893+
SetSessionAuthorizationParamKind::User(name) => write!(f, "{}", name),
9894+
}
9895+
}
9896+
}
9897+
98379898
#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
98389899
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
98399900
#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
@@ -10535,6 +10596,38 @@ impl fmt::Display for VacuumStatement {
1053510596
}
1053610597
}
1053710598

10599+
/// Variants of the RESET statement
10600+
#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
10601+
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
10602+
#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
10603+
pub enum Reset {
10604+
/// Resets all session parameters to their default values.
10605+
ALL,
10606+
10607+
/// Resets a specific session parameter to its default value.
10608+
ConfigurationParameter(ObjectName),
10609+
}
10610+
10611+
/// Resets a session parameter to its default value.
10612+
/// ```sql
10613+
/// RESET { ALL | <configuration_parameter> }
10614+
/// ```
10615+
#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
10616+
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
10617+
#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
10618+
pub struct ResetStatement {
10619+
pub reset: Reset,
10620+
}
10621+
10622+
impl fmt::Display for ResetStatement {
10623+
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
10624+
match &self.reset {
10625+
Reset::ALL => write!(f, "RESET ALL"),
10626+
Reset::ConfigurationParameter(param) => write!(f, "RESET {}", param),
10627+
}
10628+
}
10629+
}
10630+
1053810631
impl From<Set> for Statement {
1053910632
fn from(s: Set) -> Self {
1054010633
Self::Set(s)
@@ -10775,6 +10868,12 @@ impl From<VacuumStatement> for Statement {
1077510868
}
1077610869
}
1077710870

10871+
impl From<ResetStatement> for Statement {
10872+
fn from(r: ResetStatement) -> Self {
10873+
Self::Reset(r)
10874+
}
10875+
}
10876+
1077810877
#[cfg(test)]
1077910878
mod tests {
1078010879
use crate::tokenizer::Location;

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)