Skip to content

Commit 073df28

Browse files
authored
Add SHOW CATALOGS syntax and tests (apache#2284)
Signed-off-by: Smith Cruise <chendingchao1@126.com>
1 parent 93bc80c commit 073df28

File tree

6 files changed

+104
-1
lines changed

6 files changed

+104
-1
lines changed

src/ast/mod.rs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4109,6 +4109,17 @@ pub enum Statement {
41094109
show_options: ShowStatementOptions,
41104110
},
41114111
/// ```sql
4112+
/// SHOW CATALOGS
4113+
/// ```
4114+
ShowCatalogs {
4115+
/// `true` when terse output format was requested.
4116+
terse: bool,
4117+
/// `true` when history information was requested.
4118+
history: bool,
4119+
/// Additional options for `SHOW CATALOGS`.
4120+
show_options: ShowStatementOptions,
4121+
},
4122+
/// ```sql
41124123
/// SHOW DATABASES
41134124
/// ```
41144125
ShowDatabases {
@@ -5744,6 +5755,19 @@ impl fmt::Display for Statement {
57445755
)?;
57455756
Ok(())
57465757
}
5758+
Statement::ShowCatalogs {
5759+
terse,
5760+
history,
5761+
show_options,
5762+
} => {
5763+
write!(
5764+
f,
5765+
"SHOW {terse}CATALOGS{history}{show_options}",
5766+
terse = if *terse { "TERSE " } else { "" },
5767+
history = if *history { " HISTORY" } else { "" },
5768+
)?;
5769+
Ok(())
5770+
}
57475771
Statement::ShowProcessList { full } => {
57485772
write!(
57495773
f,

src/ast/spans.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -478,6 +478,7 @@ impl Spanned for Statement {
478478
Statement::AlterConnector { .. } => Span::empty(),
479479
Statement::DropPolicy { .. } => Span::empty(),
480480
Statement::DropConnector { .. } => Span::empty(),
481+
Statement::ShowCatalogs { .. } => Span::empty(),
481482
Statement::ShowDatabases { .. } => Span::empty(),
482483
Statement::ShowProcessList { .. } => Span::empty(),
483484
Statement::ShowSchemas { .. } => Span::empty(),

src/keywords.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -193,6 +193,7 @@ define_keywords!(
193193
CASES,
194194
CAST,
195195
CATALOG,
196+
CATALOGS,
196197
CATALOG_SYNC,
197198
CATALOG_SYNC_NAMESPACE_FLATTEN_DELIMITER,
198199
CATALOG_SYNC_NAMESPACE_MODE,

src/parser/mod.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15465,6 +15465,8 @@ impl<'a> Parser<'a> {
1546515465
session,
1546615466
global,
1546715467
})
15468+
} else if self.parse_keyword(Keyword::CATALOGS) {
15469+
self.parse_show_catalogs(terse)
1546815470
} else if self.parse_keyword(Keyword::DATABASES) {
1546915471
self.parse_show_databases(terse)
1547015472
} else if self.parse_keyword(Keyword::SCHEMAS) {
@@ -15488,6 +15490,16 @@ impl<'a> Parser<'a> {
1548815490
}))
1548915491
}
1549015492

15493+
fn parse_show_catalogs(&mut self, terse: bool) -> Result<Statement, ParserError> {
15494+
let history = self.parse_keyword(Keyword::HISTORY);
15495+
let show_options = self.parse_show_stmt_options()?;
15496+
Ok(Statement::ShowCatalogs {
15497+
terse,
15498+
history,
15499+
show_options,
15500+
})
15501+
}
15502+
1549115503
fn parse_show_databases(&mut self, terse: bool) -> Result<Statement, ParserError> {
1549215504
let history = self.parse_keyword(Keyword::HISTORY);
1549315505
let show_options = self.parse_show_stmt_options()?;

tests/sqlparser_common.rs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14772,6 +14772,7 @@ fn parse_method_expr() {
1477214772
fn test_show_dbs_schemas_tables_views() {
1477314773
// These statements are parsed the same by all dialects
1477414774
let stmts = vec![
14775+
"SHOW CATALOGS",
1477514776
"SHOW DATABASES",
1477614777
"SHOW SCHEMAS",
1477714778
"SHOW TABLES",
@@ -14789,7 +14790,11 @@ fn test_show_dbs_schemas_tables_views() {
1478914790
// These statements are parsed the same by all dialects
1479014791
// except for how the parser interprets the location of
1479114792
// LIKE option (infix/suffix)
14792-
let stmts = vec!["SHOW DATABASES LIKE '%abc'", "SHOW SCHEMAS LIKE '%abc'"];
14793+
let stmts = vec![
14794+
"SHOW CATALOGS LIKE '%abc'",
14795+
"SHOW DATABASES LIKE '%abc'",
14796+
"SHOW SCHEMAS LIKE '%abc'",
14797+
];
1479314798
for stmt in stmts {
1479414799
all_dialects_where(|d| d.supports_show_like_before_in()).verified_stmt(stmt);
1479514800
all_dialects_where(|d| !d.supports_show_like_before_in()).verified_stmt(stmt);

tests/sqlparser_databricks.rs

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -299,6 +299,66 @@ fn parse_use() {
299299
}
300300
}
301301

302+
#[test]
303+
fn parse_show_catalogs() {
304+
databricks().verified_stmt("SHOW CATALOGS");
305+
databricks().verified_stmt("SHOW TERSE CATALOGS");
306+
databricks().verified_stmt("SHOW CATALOGS HISTORY");
307+
databricks().verified_stmt("SHOW CATALOGS LIKE 'pay*'");
308+
databricks().verified_stmt("SHOW CATALOGS 'pay*'");
309+
databricks().verified_stmt("SHOW CATALOGS STARTS WITH 'pay'");
310+
databricks().verified_stmt("SHOW CATALOGS LIMIT 10");
311+
databricks().verified_stmt("SHOW CATALOGS HISTORY STARTS WITH 'pay'");
312+
313+
match databricks().verified_stmt("SHOW CATALOGS LIKE 'pay*'") {
314+
Statement::ShowCatalogs {
315+
terse,
316+
history,
317+
show_options,
318+
} => {
319+
assert!(!terse);
320+
assert!(!history);
321+
assert_eq!(show_options.show_in, None);
322+
assert_eq!(show_options.starts_with, None);
323+
assert_eq!(show_options.limit, None);
324+
assert_eq!(show_options.limit_from, None);
325+
assert_eq!(
326+
show_options.filter_position,
327+
Some(ShowStatementFilterPosition::Suffix(
328+
ShowStatementFilter::Like("pay*".to_string())
329+
))
330+
);
331+
}
332+
_ => unreachable!(),
333+
}
334+
}
335+
336+
#[test]
337+
fn parse_show_catalogs_with_show_options() {
338+
databricks().verified_stmt("SHOW TERSE CATALOGS HISTORY IN ACCOUNT");
339+
340+
match databricks().verified_stmt("SHOW TERSE CATALOGS HISTORY IN ACCOUNT") {
341+
Statement::ShowCatalogs {
342+
terse,
343+
history,
344+
show_options,
345+
} => {
346+
assert!(terse);
347+
assert!(history);
348+
assert_eq!(show_options.filter_position, None);
349+
assert!(matches!(
350+
show_options.show_in,
351+
Some(ShowStatementIn {
352+
parent_type: Some(ShowStatementInParentType::Account),
353+
parent_name: None,
354+
..
355+
})
356+
));
357+
}
358+
_ => unreachable!(),
359+
}
360+
}
361+
302362
#[test]
303363
fn parse_databricks_struct_function() {
304364
assert_eq!(

0 commit comments

Comments
 (0)