Skip to content

Commit bdbf6a1

Browse files
Snowflake: parse SHOW [TERSE] PRIMARY/IMPORTED/EXPORTED KEYS
1 parent d9d5e1b commit bdbf6a1

4 files changed

Lines changed: 75 additions & 1 deletion

File tree

src/ast/mod.rs

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2502,6 +2502,29 @@ impl fmt::Display for ShowCreateObject {
25022502
}
25032503
}
25042504

2505+
#[derive(Debug, Copy, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
2506+
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
2507+
#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
2508+
/// Key-constraint catalog selected by a `SHOW ... KEYS` statement.
2509+
pub enum ShowKeysKind {
2510+
/// `SHOW [TERSE] PRIMARY KEYS`
2511+
Primary,
2512+
/// `SHOW IMPORTED KEYS`
2513+
Imported,
2514+
/// `SHOW EXPORTED KEYS`
2515+
Exported,
2516+
}
2517+
2518+
impl fmt::Display for ShowKeysKind {
2519+
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
2520+
match self {
2521+
ShowKeysKind::Primary => f.write_str("PRIMARY"),
2522+
ShowKeysKind::Imported => f.write_str("IMPORTED"),
2523+
ShowKeysKind::Exported => f.write_str("EXPORTED"),
2524+
}
2525+
}
2526+
}
2527+
25052528
#[derive(Debug, Copy, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
25062529
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
25072530
#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
@@ -5104,6 +5127,19 @@ pub enum Statement {
51045127
show_options: ShowStatementOptions,
51055128
},
51065129
/// ```sql
5130+
/// SHOW [TERSE] PRIMARY KEYS [ IN ... ]
5131+
/// SHOW IMPORTED KEYS [ IN ... ]
5132+
/// SHOW EXPORTED KEYS [ IN ... ]
5133+
/// ```
5134+
ShowKeys {
5135+
/// Which key-constraint catalog to show.
5136+
kind: ShowKeysKind,
5137+
/// Whether to show terse output (only meaningful for `PRIMARY KEYS`).
5138+
terse: bool,
5139+
/// Options controlling the SHOW output (`LIKE` / `IN` / `LIMIT` / ...).
5140+
show_options: ShowStatementOptions,
5141+
},
5142+
/// ```sql
51075143
/// CREATE [OR REPLACE] ROW ACCESS POLICY [IF NOT EXISTS] <name>
51085144
/// AS (<args>) RETURNS BOOLEAN -> <body>
51095145
/// ```
@@ -7323,6 +7359,17 @@ impl fmt::Display for Statement {
73237359
terse = if *terse { "TERSE " } else { "" },
73247360
)
73257361
}
7362+
Statement::ShowKeys {
7363+
kind,
7364+
terse,
7365+
show_options,
7366+
} => {
7367+
write!(
7368+
f,
7369+
"SHOW {terse}{kind} KEYS{show_options}",
7370+
terse = if *terse { "TERSE " } else { "" },
7371+
)
7372+
}
73267373
Statement::CreateRowAccessPolicy {
73277374
or_replace,
73287375
if_not_exists,

src/ast/spans.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -555,6 +555,7 @@ impl Spanned for Statement {
555555
Statement::ShowFileFormats { .. } => Span::empty(),
556556
Statement::ShowStages { .. } => Span::empty(),
557557
Statement::ShowSequences { .. } => Span::empty(),
558+
Statement::ShowKeys { .. } => Span::empty(),
558559
Statement::CreateRowAccessPolicy { .. } => Span::empty(),
559560
Statement::AlterRowAccessPolicy { .. } => Span::empty(),
560561
Statement::DropRowAccessPolicy { .. } => Span::empty(),

src/dialect/snowflake.rs

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,8 @@ use crate::ast::{
3737
IdentityPropertyOrder, InitializeKind, Insert, MultiTableInsertIntoClause,
3838
MultiTableInsertType, MultiTableInsertValue, MultiTableInsertValues,
3939
MultiTableInsertWhenClause, ObjectName, ObjectNamePart, RefreshModeKind, RowAccessPolicy,
40-
ShowObjects, SqlOption, Statement, StorageLifecyclePolicy, StorageSerializationPolicy,
40+
ShowKeysKind, ShowObjects, SqlOption, Statement, StorageLifecyclePolicy,
41+
StorageSerializationPolicy,
4142
TableObject, TagsColumnOption, Value, WrappedCollection,
4243
};
4344
use crate::dialect::{Dialect, Precedence};
@@ -498,6 +499,15 @@ impl Dialect for SnowflakeDialect {
498499
if parser.parse_keyword(Keyword::SEQUENCES) {
499500
return Some(parse_show_sequences(terse, parser));
500501
}
502+
if parser.parse_keyword(Keyword::PRIMARY) {
503+
return Some(parse_show_keys(ShowKeysKind::Primary, terse, parser));
504+
}
505+
if parser.parse_keyword(Keyword::IMPORTED) {
506+
return Some(parse_show_keys(ShowKeysKind::Imported, terse, parser));
507+
}
508+
if parser.parse_keyword(Keyword::EXPORTED) {
509+
return Some(parse_show_keys(ShowKeysKind::Exported, terse, parser));
510+
}
501511
if parser.parse_keywords(&[Keyword::ROW, Keyword::ACCESS, Keyword::POLICIES]) {
502512
return Some(parse_show_row_access_policies(parser));
503513
}
@@ -2578,6 +2588,21 @@ fn parse_show_sequences(terse: bool, parser: &mut Parser) -> Result<Statement, P
25782588
})
25792589
}
25802590

2591+
/// Parse `SHOW [TERSE] { PRIMARY | IMPORTED | EXPORTED } KEYS [ ... ]`
2592+
fn parse_show_keys(
2593+
kind: ShowKeysKind,
2594+
terse: bool,
2595+
parser: &mut Parser,
2596+
) -> Result<Statement, ParserError> {
2597+
parser.expect_keyword(Keyword::KEYS)?;
2598+
let show_options = parser.parse_show_stmt_options()?;
2599+
Ok(Statement::ShowKeys {
2600+
kind,
2601+
terse,
2602+
show_options,
2603+
})
2604+
}
2605+
25812606
/// Parse `DESC[RIBE] WAREHOUSE <name>`
25822607
fn parse_describe_warehouse(parser: &mut Parser) -> Result<Statement, ParserError> {
25832608
let name = parser.parse_object_name(false)?;

src/keywords.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -428,6 +428,7 @@ define_keywords!(
428428
EXPLAIN,
429429
EXPLICIT,
430430
EXPORT,
431+
EXPORTED,
431432
EXTEND,
432433
EXTENDED,
433434
EXTENSION,

0 commit comments

Comments
 (0)