Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 17 additions & 1 deletion src/ast/ddl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -457,6 +457,12 @@ pub enum AlterTableOperation {
},
/// Remove the clustering key from the table.
DropClusteringKey,
/// Redshift `ALTER SORTKEY (column_list)`
/// <https://docs.aws.amazon.com/redshift/latest/dg/r_ALTER_TABLE.html>
AlterSortKey {
/// Column references in the sort key.
columns: Vec<Expr>,
},
/// Suspend background reclustering operations.
SuspendRecluster,
/// Resume background reclustering operations.
Expand Down Expand Up @@ -993,6 +999,10 @@ impl fmt::Display for AlterTableOperation {
write!(f, "DROP CLUSTERING KEY")?;
Ok(())
}
AlterTableOperation::AlterSortKey { columns } => {
write!(f, "ALTER SORTKEY({})", display_comma_separated(columns))?;
Ok(())
}
AlterTableOperation::SuspendRecluster => {
write!(f, "SUSPEND RECLUSTER")?;
Ok(())
Expand Down Expand Up @@ -3037,7 +3047,10 @@ pub struct CreateTable {
pub diststyle: Option<DistStyle>,
/// Redshift `DISTKEY` option
/// <https://docs.aws.amazon.com/redshift/latest/dg/r_CREATE_TABLE_NEW.html>
pub distkey: Option<Ident>,
pub distkey: Option<Expr>,
/// Redshift `SORTKEY` option
/// <https://docs.aws.amazon.com/redshift/latest/dg/r_CREATE_TABLE_NEW.html>
pub sortkey: Option<Vec<Expr>>,
}

impl fmt::Display for CreateTable {
Expand Down Expand Up @@ -3342,6 +3355,9 @@ impl fmt::Display for CreateTable {
if let Some(distkey) = &self.distkey {
write!(f, " DISTKEY({distkey})")?;
}
if let Some(sortkey) = &self.sortkey {
write!(f, " SORTKEY({})", display_comma_separated(sortkey))?;
}
if let Some(query) = &self.query {
write!(f, " AS {query}")?;
}
Expand Down
14 changes: 12 additions & 2 deletions src/ast/helpers/stmt_create_table.rs
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,9 @@ pub struct CreateTableBuilder {
/// Redshift `DISTSTYLE` option.
pub diststyle: Option<DistStyle>,
/// Redshift `DISTKEY` option.
pub distkey: Option<Ident>,
pub distkey: Option<Expr>,
/// Redshift `SORTKEY` option.
pub sortkey: Option<Vec<Expr>>,
}

impl CreateTableBuilder {
Expand Down Expand Up @@ -236,6 +238,7 @@ impl CreateTableBuilder {
require_user: false,
diststyle: None,
distkey: None,
sortkey: None,
}
}
/// Set `OR REPLACE` for the CREATE TABLE statement.
Expand Down Expand Up @@ -517,10 +520,15 @@ impl CreateTableBuilder {
self
}
/// Set Redshift `DISTKEY` option.
pub fn distkey(mut self, distkey: Option<Ident>) -> Self {
pub fn distkey(mut self, distkey: Option<Expr>) -> Self {
self.distkey = distkey;
self
}
/// Set Redshift `SORTKEY` option.
pub fn sortkey(mut self, sortkey: Option<Vec<Expr>>) -> Self {
self.sortkey = sortkey;
self
}
/// Consume the builder and produce a `CreateTable`.
pub fn build(self) -> CreateTable {
CreateTable {
Expand Down Expand Up @@ -579,6 +587,7 @@ impl CreateTableBuilder {
require_user: self.require_user,
diststyle: self.diststyle,
distkey: self.distkey,
sortkey: self.sortkey,
}
}
}
Expand Down Expand Up @@ -656,6 +665,7 @@ impl From<CreateTable> for CreateTableBuilder {
require_user: table.require_user,
diststyle: table.diststyle,
distkey: table.distkey,
sortkey: table.sortkey,
}
}
}
Expand Down
6 changes: 4 additions & 2 deletions src/ast/spans.rs
Original file line number Diff line number Diff line change
Expand Up @@ -582,8 +582,9 @@ impl Spanned for CreateTable {
refresh_mode: _,
initialize: _,
require_user: _,
diststyle: _, // enum, no span
distkey: _, // Ident, todo
diststyle: _,
distkey: _,
sortkey: _,
} = self;

union_spans(
Expand Down Expand Up @@ -1193,6 +1194,7 @@ impl Spanned for AlterTableOperation {
AlterTableOperation::OwnerTo { .. } => Span::empty(),
AlterTableOperation::ClusterBy { exprs } => union_spans(exprs.iter().map(|e| e.span())),
AlterTableOperation::DropClusteringKey => Span::empty(),
AlterTableOperation::AlterSortKey { .. } => Span::empty(),
AlterTableOperation::SuspendRecluster => Span::empty(),
AlterTableOperation::ResumeRecluster => Span::empty(),
AlterTableOperation::Refresh { .. } => Span::empty(),
Expand Down
1 change: 1 addition & 0 deletions src/keywords.rs
Original file line number Diff line number Diff line change
Expand Up @@ -953,6 +953,7 @@ define_keywords!(
SOME,
SORT,
SORTED,
SORTKEY,
SOURCE,
SPATIAL,
SPECIFIC,
Expand Down
32 changes: 29 additions & 3 deletions src/parser/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8384,17 +8384,25 @@ impl<'a> Parser<'a> {

let strict = self.parse_keyword(Keyword::STRICT);

// Redshift: DISTSTYLE, DISTKEY
// Redshift: DISTSTYLE, DISTKEY, SORTKEY
let diststyle = if self.parse_keyword(Keyword::DISTSTYLE) {
Some(self.parse_dist_style()?)
} else {
None
};
let distkey = if self.parse_keyword(Keyword::DISTKEY) {
self.expect_token(&Token::LParen)?;
let column = self.parse_identifier()?;
let expr = self.parse_expr()?;
self.expect_token(&Token::RParen)?;
Some(column)
Some(expr)
} else {
None
};
let sortkey = if self.parse_keyword(Keyword::SORTKEY) {
self.expect_token(&Token::LParen)?;
let columns = self.parse_comma_separated(|p| p.parse_expr())?;
self.expect_token(&Token::RParen)?;
Some(columns)
} else {
None
};
Expand Down Expand Up @@ -8440,6 +8448,7 @@ impl<'a> Parser<'a> {
.strict(strict)
.diststyle(diststyle)
.distkey(distkey)
.sortkey(sortkey)
.build())
}

Expand Down Expand Up @@ -9979,6 +9988,18 @@ impl<'a> Parser<'a> {
})
}

/// Parse Redshift `ALTER SORTKEY (column_list)`.
///
/// See <https://docs.aws.amazon.com/redshift/latest/dg/r_ALTER_TABLE.html>
fn parse_alter_sort_key(&mut self) -> Result<AlterTableOperation, ParserError> {
self.expect_keyword_is(Keyword::ALTER)?;
self.expect_keyword_is(Keyword::SORTKEY)?;
self.expect_token(&Token::LParen)?;
let columns = self.parse_comma_separated(|p| p.parse_expr())?;
self.expect_token(&Token::RParen)?;
Ok(AlterTableOperation::AlterSortKey { columns })
}

/// Parse a single `ALTER TABLE` operation and return an `AlterTableOperation`.
pub fn parse_alter_table_operation(&mut self) -> Result<AlterTableOperation, ParserError> {
let operation = if self.parse_keyword(Keyword::ADD) {
Expand Down Expand Up @@ -10257,6 +10278,11 @@ impl<'a> Parser<'a> {
column_position,
}
} else if self.parse_keyword(Keyword::ALTER) {
if self.peek_keyword(Keyword::SORTKEY) {
self.prev_token();
return self.parse_alter_sort_key();
}

let _ = self.parse_keyword(Keyword::COLUMN); // [ COLUMN ]
let column_name = self.parse_identifier()?;
let is_postgresql = dialect_of!(self is PostgreSqlDialect);
Expand Down
1 change: 1 addition & 0 deletions tests/sqlparser_duckdb.rs
Original file line number Diff line number Diff line change
Expand Up @@ -790,6 +790,7 @@ fn test_duckdb_union_datatype() {
require_user: Default::default(),
diststyle: Default::default(),
distkey: Default::default(),
sortkey: Default::default(),
}),
stmt
);
Expand Down
2 changes: 2 additions & 0 deletions tests/sqlparser_mssql.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2008,6 +2008,7 @@ fn parse_create_table_with_valid_options() {
require_user: false,
diststyle: None,
distkey: None,
sortkey: None,
})
);
}
Expand Down Expand Up @@ -2178,6 +2179,7 @@ fn parse_create_table_with_identity_column() {
require_user: false,
diststyle: None,
distkey: None,
sortkey: None,
}),
);
}
Expand Down
1 change: 1 addition & 0 deletions tests/sqlparser_postgres.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6476,6 +6476,7 @@ fn parse_trigger_related_functions() {
require_user: false,
diststyle: None,
distkey: None,
sortkey: None,
}
);

Expand Down
26 changes: 26 additions & 0 deletions tests/sqlparser_redshift.rs
Original file line number Diff line number Diff line change
Expand Up @@ -474,3 +474,29 @@ fn test_copy_credentials() {
"COPY t1 FROM 's3://bucket/file.csv' CREDENTIALS 'aws_access_key_id=AK;aws_secret_access_key=SK' CSV",
);
}

#[test]
fn test_create_table_sortkey() {
redshift().verified_stmt("CREATE TABLE t1 (c1 INT, c2 INT, c3 TIMESTAMP) SORTKEY(c3)");
redshift().verified_stmt("CREATE TABLE t1 (c1 INT, c2 INT) SORTKEY(c1, c2)");
}

#[test]
fn test_create_table_distkey_sortkey_with_ctas() {
redshift().verified_stmt(
"CREATE TABLE t1 DISTKEY(1) SORTKEY(1, 3) AS SELECT eventid, venueid, dateid, eventname FROM event",
);
}

#[test]
fn test_create_table_diststyle_distkey_sortkey() {
redshift().verified_stmt(
"CREATE TABLE t1 (c1 INT, c2 INT) DISTSTYLE KEY DISTKEY(c1) SORTKEY(c1, c2)",
);
}

#[test]
fn test_alter_table_alter_sortkey() {
redshift().verified_stmt("ALTER TABLE users ALTER SORTKEY(created_at)");
redshift().verified_stmt("ALTER TABLE users ALTER SORTKEY(c1, c2)");
}