Skip to content

Commit 607aad4

Browse files
authored
Merge branch 'main' into feature/snowflake-multi-table-insert-v2
2 parents 5150514 + ace189b commit 607aad4

File tree

12 files changed

+734
-35
lines changed

12 files changed

+734
-35
lines changed

src/ast/ddl.rs

Lines changed: 111 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -43,13 +43,14 @@ use crate::ast::{
4343
},
4444
ArgMode, AttachedToken, CommentDef, ConditionalStatements, CreateFunctionBody,
4545
CreateFunctionUsing, CreateTableLikeKind, CreateTableOptions, CreateViewParams, DataType, Expr,
46-
FileFormat, FunctionBehavior, FunctionCalledOnNull, FunctionDesc, FunctionDeterminismSpecifier,
47-
FunctionParallel, HiveDistributionStyle, HiveFormat, HiveIOFormat, HiveRowFormat,
48-
HiveSetLocation, Ident, InitializeKind, MySQLColumnPosition, ObjectName, OnCommit,
49-
OneOrManyWithParens, OperateFunctionArg, OrderByExpr, ProjectionSelect, Query, RefreshModeKind,
50-
RowAccessPolicy, SequenceOptions, Spanned, SqlOption, StorageSerializationPolicy, TableVersion,
51-
Tag, TriggerEvent, TriggerExecBody, TriggerObject, TriggerPeriod, TriggerReferencing, Value,
52-
ValueWithSpan, WrappedCollection,
46+
FileFormat, FunctionBehavior, FunctionCalledOnNull, FunctionDefinitionSetParam, FunctionDesc,
47+
FunctionDeterminismSpecifier, FunctionParallel, FunctionSecurity, HiveDistributionStyle,
48+
HiveFormat, HiveIOFormat, HiveRowFormat, HiveSetLocation, Ident, InitializeKind,
49+
MySQLColumnPosition, ObjectName, OnCommit, OneOrManyWithParens, OperateFunctionArg,
50+
OrderByExpr, ProjectionSelect, Query, RefreshModeKind, RowAccessPolicy, SequenceOptions,
51+
Spanned, SqlOption, StorageSerializationPolicy, TableVersion, Tag, TriggerEvent,
52+
TriggerExecBody, TriggerObject, TriggerPeriod, TriggerReferencing, Value, ValueWithSpan,
53+
WrappedCollection,
5354
};
5455
use crate::display_utils::{DisplayCommaSeparated, Indent, NewLine, SpaceOrNewline};
5556
use crate::keywords::Keyword;
@@ -2697,6 +2698,14 @@ pub struct CreateTable {
26972698
/// <https://www.postgresql.org/docs/current/ddl-inherit.html>
26982699
/// <https://www.postgresql.org/docs/current/sql-createtable.html#SQL-CREATETABLE-PARMS-INHERITS>
26992700
pub inherits: Option<Vec<ObjectName>>,
2701+
/// PostgreSQL `PARTITION OF` clause to create a partition of a parent table.
2702+
/// Contains the parent table name.
2703+
/// <https://www.postgresql.org/docs/current/sql-createtable.html>
2704+
#[cfg_attr(feature = "visitor", visit(with = "visit_relation"))]
2705+
pub partition_of: Option<ObjectName>,
2706+
/// PostgreSQL partition bound specification for PARTITION OF.
2707+
/// <https://www.postgresql.org/docs/current/sql-createtable.html>
2708+
pub for_values: Option<ForValues>,
27002709
/// SQLite "STRICT" clause.
27012710
/// if the "STRICT" table-option keyword is added to the end, after the closing ")",
27022711
/// then strict typing rules apply to that table.
@@ -2792,6 +2801,9 @@ impl fmt::Display for CreateTable {
27922801
dynamic = if self.dynamic { "DYNAMIC " } else { "" },
27932802
name = self.name,
27942803
)?;
2804+
if let Some(partition_of) = &self.partition_of {
2805+
write!(f, " PARTITION OF {partition_of}")?;
2806+
}
27952807
if let Some(on_cluster) = &self.on_cluster {
27962808
write!(f, " ON CLUSTER {on_cluster}")?;
27972809
}
@@ -2806,12 +2818,19 @@ impl fmt::Display for CreateTable {
28062818
Indent(DisplayCommaSeparated(&self.constraints)).fmt(f)?;
28072819
NewLine.fmt(f)?;
28082820
f.write_str(")")?;
2809-
} else if self.query.is_none() && self.like.is_none() && self.clone.is_none() {
2821+
} else if self.query.is_none()
2822+
&& self.like.is_none()
2823+
&& self.clone.is_none()
2824+
&& self.partition_of.is_none()
2825+
{
28102826
// PostgreSQL allows `CREATE TABLE t ();`, but requires empty parens
28112827
f.write_str(" ()")?;
28122828
} else if let Some(CreateTableLikeKind::Parenthesized(like_in_columns_list)) = &self.like {
28132829
write!(f, " ({like_in_columns_list})")?;
28142830
}
2831+
if let Some(for_values) = &self.for_values {
2832+
write!(f, " {for_values}")?;
2833+
}
28152834

28162835
// Hive table comment should be after column definitions, please refer to:
28172836
// [Hive](https://cwiki.apache.org/confluence/display/Hive/LanguageManual+DDL#LanguageManualDDL-CreateTable)
@@ -3053,6 +3072,76 @@ impl fmt::Display for CreateTable {
30533072
}
30543073
}
30553074

3075+
/// PostgreSQL partition bound specification for `PARTITION OF`.
3076+
///
3077+
/// Specifies partition bounds for a child partition table.
3078+
///
3079+
/// See [PostgreSQL](https://www.postgresql.org/docs/current/sql-createtable.html)
3080+
#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
3081+
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
3082+
#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
3083+
pub enum ForValues {
3084+
/// `FOR VALUES IN (expr, ...)`
3085+
In(Vec<Expr>),
3086+
/// `FOR VALUES FROM (expr|MINVALUE|MAXVALUE, ...) TO (expr|MINVALUE|MAXVALUE, ...)`
3087+
From {
3088+
from: Vec<PartitionBoundValue>,
3089+
to: Vec<PartitionBoundValue>,
3090+
},
3091+
/// `FOR VALUES WITH (MODULUS n, REMAINDER r)`
3092+
With { modulus: u64, remainder: u64 },
3093+
/// `DEFAULT`
3094+
Default,
3095+
}
3096+
3097+
impl fmt::Display for ForValues {
3098+
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
3099+
match self {
3100+
ForValues::In(values) => {
3101+
write!(f, "FOR VALUES IN ({})", display_comma_separated(values))
3102+
}
3103+
ForValues::From { from, to } => {
3104+
write!(
3105+
f,
3106+
"FOR VALUES FROM ({}) TO ({})",
3107+
display_comma_separated(from),
3108+
display_comma_separated(to)
3109+
)
3110+
}
3111+
ForValues::With { modulus, remainder } => {
3112+
write!(
3113+
f,
3114+
"FOR VALUES WITH (MODULUS {modulus}, REMAINDER {remainder})"
3115+
)
3116+
}
3117+
ForValues::Default => write!(f, "DEFAULT"),
3118+
}
3119+
}
3120+
}
3121+
3122+
/// A value in a partition bound specification.
3123+
///
3124+
/// Used in RANGE partition bounds where values can be expressions,
3125+
/// MINVALUE (negative infinity), or MAXVALUE (positive infinity).
3126+
#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
3127+
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
3128+
#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
3129+
pub enum PartitionBoundValue {
3130+
Expr(Expr),
3131+
MinValue,
3132+
MaxValue,
3133+
}
3134+
3135+
impl fmt::Display for PartitionBoundValue {
3136+
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
3137+
match self {
3138+
PartitionBoundValue::Expr(expr) => write!(f, "{expr}"),
3139+
PartitionBoundValue::MinValue => write!(f, "MINVALUE"),
3140+
PartitionBoundValue::MaxValue => write!(f, "MAXVALUE"),
3141+
}
3142+
}
3143+
}
3144+
30563145
#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
30573146
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
30583147
#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
@@ -3138,6 +3227,14 @@ pub struct CreateFunction {
31383227
///
31393228
/// [PostgreSQL](https://www.postgresql.org/docs/current/sql-createfunction.html)
31403229
pub parallel: Option<FunctionParallel>,
3230+
/// SECURITY { DEFINER | INVOKER }
3231+
///
3232+
/// [PostgreSQL](https://www.postgresql.org/docs/current/sql-createfunction.html)
3233+
pub security: Option<FunctionSecurity>,
3234+
/// SET configuration_parameter clauses
3235+
///
3236+
/// [PostgreSQL](https://www.postgresql.org/docs/current/sql-createfunction.html)
3237+
pub set_params: Vec<FunctionDefinitionSetParam>,
31413238
/// USING ... (Hive only)
31423239
pub using: Option<CreateFunctionUsing>,
31433240
/// Language used in a UDF definition.
@@ -3204,6 +3301,12 @@ impl fmt::Display for CreateFunction {
32043301
if let Some(parallel) = &self.parallel {
32053302
write!(f, " {parallel}")?;
32063303
}
3304+
if let Some(security) = &self.security {
3305+
write!(f, " {security}")?;
3306+
}
3307+
for set_param in &self.set_params {
3308+
write!(f, " {set_param}")?;
3309+
}
32073310
if let Some(remote_connection) = &self.remote_connection {
32083311
write!(f, " REMOTE WITH CONNECTION {remote_connection}")?;
32093312
}

src/ast/helpers/stmt_create_table.rs

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,8 @@ use sqlparser_derive::{Visit, VisitMut};
2626

2727
use crate::ast::{
2828
ClusteredBy, ColumnDef, CommentDef, CreateTable, CreateTableLikeKind, CreateTableOptions, Expr,
29-
FileFormat, HiveDistributionStyle, HiveFormat, Ident, InitializeKind, ObjectName, OnCommit,
30-
OneOrManyWithParens, Query, RefreshModeKind, RowAccessPolicy, Statement,
29+
FileFormat, ForValues, HiveDistributionStyle, HiveFormat, Ident, InitializeKind, ObjectName,
30+
OnCommit, OneOrManyWithParens, Query, RefreshModeKind, RowAccessPolicy, Statement,
3131
StorageSerializationPolicy, TableConstraint, TableVersion, Tag, WrappedCollection,
3232
};
3333

@@ -94,6 +94,8 @@ pub struct CreateTableBuilder {
9494
pub cluster_by: Option<WrappedCollection<Vec<Expr>>>,
9595
pub clustered_by: Option<ClusteredBy>,
9696
pub inherits: Option<Vec<ObjectName>>,
97+
pub partition_of: Option<ObjectName>,
98+
pub for_values: Option<ForValues>,
9799
pub strict: bool,
98100
pub copy_grants: bool,
99101
pub enable_schema_evolution: Option<bool>,
@@ -150,6 +152,8 @@ impl CreateTableBuilder {
150152
cluster_by: None,
151153
clustered_by: None,
152154
inherits: None,
155+
partition_of: None,
156+
for_values: None,
153157
strict: false,
154158
copy_grants: false,
155159
enable_schema_evolution: None,
@@ -317,6 +321,16 @@ impl CreateTableBuilder {
317321
self
318322
}
319323

324+
pub fn partition_of(mut self, partition_of: Option<ObjectName>) -> Self {
325+
self.partition_of = partition_of;
326+
self
327+
}
328+
329+
pub fn for_values(mut self, for_values: Option<ForValues>) -> Self {
330+
self.for_values = for_values;
331+
self
332+
}
333+
320334
pub fn strict(mut self, strict: bool) -> Self {
321335
self.strict = strict;
322336
self
@@ -463,6 +477,8 @@ impl CreateTableBuilder {
463477
cluster_by: self.cluster_by,
464478
clustered_by: self.clustered_by,
465479
inherits: self.inherits,
480+
partition_of: self.partition_of,
481+
for_values: self.for_values,
466482
strict: self.strict,
467483
copy_grants: self.copy_grants,
468484
enable_schema_evolution: self.enable_schema_evolution,
@@ -527,6 +543,8 @@ impl TryFrom<Statement> for CreateTableBuilder {
527543
cluster_by,
528544
clustered_by,
529545
inherits,
546+
partition_of,
547+
for_values,
530548
strict,
531549
copy_grants,
532550
enable_schema_evolution,
@@ -577,6 +595,8 @@ impl TryFrom<Statement> for CreateTableBuilder {
577595
cluster_by,
578596
clustered_by,
579597
inherits,
598+
partition_of,
599+
for_values,
580600
strict,
581601
iceberg,
582602
copy_grants,

src/ast/mod.rs

Lines changed: 65 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -69,15 +69,15 @@ pub use self::ddl::{
6969
CreateExtension, CreateFunction, CreateIndex, CreateOperator, CreateOperatorClass,
7070
CreateOperatorFamily, CreateTable, CreateTrigger, CreateView, Deduplicate, DeferrableInitial,
7171
DropBehavior, DropExtension, DropFunction, DropOperator, DropOperatorClass, DropOperatorFamily,
72-
DropOperatorSignature, DropTrigger, GeneratedAs, GeneratedExpressionMode, IdentityParameters,
73-
IdentityProperty, IdentityPropertyFormatKind, IdentityPropertyKind, IdentityPropertyOrder,
74-
IndexColumn, IndexOption, IndexType, KeyOrIndexDisplay, Msck, NullsDistinctOption,
75-
OperatorArgTypes, OperatorClassItem, OperatorFamilyDropItem, OperatorFamilyItem,
76-
OperatorOption, OperatorPurpose, Owner, Partition, ProcedureParam, ReferentialAction,
77-
RenameTableNameKind, ReplicaIdentity, TagsColumnOption, TriggerObjectKind, Truncate,
78-
UserDefinedTypeCompositeAttributeDef, UserDefinedTypeInternalLength,
79-
UserDefinedTypeRangeOption, UserDefinedTypeRepresentation, UserDefinedTypeSqlDefinitionOption,
80-
UserDefinedTypeStorage, ViewColumnDef,
72+
DropOperatorSignature, DropTrigger, ForValues, GeneratedAs, GeneratedExpressionMode,
73+
IdentityParameters, IdentityProperty, IdentityPropertyFormatKind, IdentityPropertyKind,
74+
IdentityPropertyOrder, IndexColumn, IndexOption, IndexType, KeyOrIndexDisplay, Msck,
75+
NullsDistinctOption, OperatorArgTypes, OperatorClassItem, OperatorFamilyDropItem,
76+
OperatorFamilyItem, OperatorOption, OperatorPurpose, Owner, Partition, PartitionBoundValue,
77+
ProcedureParam, ReferentialAction, RenameTableNameKind, ReplicaIdentity, TagsColumnOption,
78+
TriggerObjectKind, Truncate, UserDefinedTypeCompositeAttributeDef,
79+
UserDefinedTypeInternalLength, UserDefinedTypeRangeOption, UserDefinedTypeRepresentation,
80+
UserDefinedTypeSqlDefinitionOption, UserDefinedTypeStorage, ViewColumnDef,
8181
};
8282
pub use self::dml::{
8383
Delete, Insert, Merge, MergeAction, MergeClause, MergeClauseKind, MergeInsertExpr,
@@ -8782,6 +8782,62 @@ impl fmt::Display for FunctionBehavior {
87828782
}
87838783
}
87848784

8785+
/// Security attribute for functions: SECURITY DEFINER or SECURITY INVOKER.
8786+
///
8787+
/// [PostgreSQL](https://www.postgresql.org/docs/current/sql-createfunction.html)
8788+
#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
8789+
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
8790+
#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
8791+
pub enum FunctionSecurity {
8792+
Definer,
8793+
Invoker,
8794+
}
8795+
8796+
impl fmt::Display for FunctionSecurity {
8797+
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
8798+
match self {
8799+
FunctionSecurity::Definer => write!(f, "SECURITY DEFINER"),
8800+
FunctionSecurity::Invoker => write!(f, "SECURITY INVOKER"),
8801+
}
8802+
}
8803+
}
8804+
8805+
/// Value for a SET configuration parameter in a CREATE FUNCTION statement.
8806+
///
8807+
/// [PostgreSQL](https://www.postgresql.org/docs/current/sql-createfunction.html)
8808+
#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
8809+
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
8810+
#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
8811+
pub enum FunctionSetValue {
8812+
/// SET param = value1, value2, ...
8813+
Values(Vec<Expr>),
8814+
/// SET param FROM CURRENT
8815+
FromCurrent,
8816+
}
8817+
8818+
/// A SET configuration_parameter clause in a CREATE FUNCTION statement.
8819+
///
8820+
/// [PostgreSQL](https://www.postgresql.org/docs/current/sql-createfunction.html)
8821+
#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
8822+
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
8823+
#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
8824+
pub struct FunctionDefinitionSetParam {
8825+
pub name: Ident,
8826+
pub value: FunctionSetValue,
8827+
}
8828+
8829+
impl fmt::Display for FunctionDefinitionSetParam {
8830+
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
8831+
write!(f, "SET {} ", self.name)?;
8832+
match &self.value {
8833+
FunctionSetValue::Values(values) => {
8834+
write!(f, "= {}", display_comma_separated(values))
8835+
}
8836+
FunctionSetValue::FromCurrent => write!(f, "FROM CURRENT"),
8837+
}
8838+
}
8839+
}
8840+
87858841
/// These attributes describe the behavior of the function when called with a null argument.
87868842
#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
87878843
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]

src/ast/query.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1242,6 +1242,8 @@ pub enum TableFactor {
12421242
lateral: bool,
12431243
subquery: Box<Query>,
12441244
alias: Option<TableAlias>,
1245+
/// Optional table sample modifier
1246+
sample: Option<TableSampleKind>,
12451247
},
12461248
/// `TABLE(<expr>)[ AS <alias> ]`
12471249
TableFunction {
@@ -1922,6 +1924,7 @@ impl fmt::Display for TableFactor {
19221924
lateral,
19231925
subquery,
19241926
alias,
1927+
sample,
19251928
} => {
19261929
if *lateral {
19271930
write!(f, "LATERAL ")?;
@@ -1934,6 +1937,9 @@ impl fmt::Display for TableFactor {
19341937
if let Some(alias) = alias {
19351938
write!(f, " {alias}")?;
19361939
}
1940+
if let Some(TableSampleKind::AfterTableAlias(sample)) = sample {
1941+
write!(f, " {sample}")?;
1942+
}
19371943
Ok(())
19381944
}
19391945
TableFactor::Function {

0 commit comments

Comments
 (0)