mirror of
https://github.com/apache/datafusion-sqlparser-rs.git
synced 2025-07-08 01:15:00 +00:00
Add suppport for Show Objects statement for the Snowflake parser (#1702)
Co-authored-by: Denys Tsomenko <denys.tsomenko@caspiandb.com> Co-authored-by: Ifeanyi Ubah <ify1992@yahoo.com>
This commit is contained in:
parent
443f492b4b
commit
0b8ba91156
6 changed files with 97 additions and 2 deletions
|
@ -3010,6 +3010,12 @@ pub enum Statement {
|
||||||
show_options: ShowStatementOptions,
|
show_options: ShowStatementOptions,
|
||||||
},
|
},
|
||||||
/// ```sql
|
/// ```sql
|
||||||
|
/// SHOW OBJECTS LIKE 'line%' IN mydb.public
|
||||||
|
/// ```
|
||||||
|
/// Snowflake-specific statement
|
||||||
|
/// <https://docs.snowflake.com/en/sql-reference/sql/show-objects>
|
||||||
|
ShowObjects(ShowObjects),
|
||||||
|
/// ```sql
|
||||||
/// SHOW TABLES
|
/// SHOW TABLES
|
||||||
/// ```
|
/// ```
|
||||||
ShowTables {
|
ShowTables {
|
||||||
|
@ -4703,6 +4709,17 @@ impl fmt::Display for Statement {
|
||||||
)?;
|
)?;
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
Statement::ShowObjects(ShowObjects {
|
||||||
|
terse,
|
||||||
|
show_options,
|
||||||
|
}) => {
|
||||||
|
write!(
|
||||||
|
f,
|
||||||
|
"SHOW {terse}OBJECTS{show_options}",
|
||||||
|
terse = if *terse { "TERSE " } else { "" },
|
||||||
|
)?;
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
Statement::ShowTables {
|
Statement::ShowTables {
|
||||||
terse,
|
terse,
|
||||||
history,
|
history,
|
||||||
|
@ -8343,6 +8360,14 @@ impl fmt::Display for ShowStatementIn {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
|
||||||
|
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
|
||||||
|
#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
|
||||||
|
pub struct ShowObjects {
|
||||||
|
pub terse: bool,
|
||||||
|
pub show_options: ShowStatementOptions,
|
||||||
|
}
|
||||||
|
|
||||||
/// MSSQL's json null clause
|
/// MSSQL's json null clause
|
||||||
///
|
///
|
||||||
/// ```plaintext
|
/// ```plaintext
|
||||||
|
|
|
@ -496,6 +496,7 @@ impl Spanned for Statement {
|
||||||
Statement::DropConnector { .. } => Span::empty(),
|
Statement::DropConnector { .. } => Span::empty(),
|
||||||
Statement::ShowDatabases { .. } => Span::empty(),
|
Statement::ShowDatabases { .. } => Span::empty(),
|
||||||
Statement::ShowSchemas { .. } => Span::empty(),
|
Statement::ShowSchemas { .. } => Span::empty(),
|
||||||
|
Statement::ShowObjects { .. } => Span::empty(),
|
||||||
Statement::ShowViews { .. } => Span::empty(),
|
Statement::ShowViews { .. } => Span::empty(),
|
||||||
Statement::LISTEN { .. } => Span::empty(),
|
Statement::LISTEN { .. } => Span::empty(),
|
||||||
Statement::NOTIFY { .. } => Span::empty(),
|
Statement::NOTIFY { .. } => Span::empty(),
|
||||||
|
|
|
@ -25,7 +25,7 @@ use crate::ast::helpers::stmt_data_loading::{
|
||||||
use crate::ast::{
|
use crate::ast::{
|
||||||
ColumnOption, ColumnPolicy, ColumnPolicyProperty, CopyIntoSnowflakeKind, Ident,
|
ColumnOption, ColumnPolicy, ColumnPolicyProperty, CopyIntoSnowflakeKind, Ident,
|
||||||
IdentityParameters, IdentityProperty, IdentityPropertyFormatKind, IdentityPropertyKind,
|
IdentityParameters, IdentityProperty, IdentityPropertyFormatKind, IdentityPropertyKind,
|
||||||
IdentityPropertyOrder, ObjectName, RowAccessPolicy, Statement, TagsColumnOption,
|
IdentityPropertyOrder, ObjectName, RowAccessPolicy, ShowObjects, Statement, TagsColumnOption,
|
||||||
WrappedCollection,
|
WrappedCollection,
|
||||||
};
|
};
|
||||||
use crate::dialect::{Dialect, Precedence};
|
use crate::dialect::{Dialect, Precedence};
|
||||||
|
@ -185,6 +185,19 @@ impl Dialect for SnowflakeDialect {
|
||||||
return Some(parse_file_staging_command(kw, parser));
|
return Some(parse_file_staging_command(kw, parser));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if parser.parse_keyword(Keyword::SHOW) {
|
||||||
|
let terse = parser.parse_keyword(Keyword::TERSE);
|
||||||
|
if parser.parse_keyword(Keyword::OBJECTS) {
|
||||||
|
return Some(parse_show_objects(terse, parser));
|
||||||
|
}
|
||||||
|
//Give back Keyword::TERSE
|
||||||
|
if terse {
|
||||||
|
parser.prev_token();
|
||||||
|
}
|
||||||
|
//Give back Keyword::SHOW
|
||||||
|
parser.prev_token();
|
||||||
|
}
|
||||||
|
|
||||||
None
|
None
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1092,3 +1105,13 @@ fn parse_column_tags(parser: &mut Parser, with: bool) -> Result<TagsColumnOption
|
||||||
|
|
||||||
Ok(TagsColumnOption { with, tags })
|
Ok(TagsColumnOption { with, tags })
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Parse snowflake show objects.
|
||||||
|
/// <https://docs.snowflake.com/en/sql-reference/sql/show-objects>
|
||||||
|
fn parse_show_objects(terse: bool, parser: &mut Parser) -> Result<Statement, ParserError> {
|
||||||
|
let show_options = parser.parse_show_stmt_options()?;
|
||||||
|
Ok(Statement::ShowObjects(ShowObjects {
|
||||||
|
terse,
|
||||||
|
show_options,
|
||||||
|
}))
|
||||||
|
}
|
||||||
|
|
|
@ -588,6 +588,7 @@ define_keywords!(
|
||||||
NUMERIC,
|
NUMERIC,
|
||||||
NVARCHAR,
|
NVARCHAR,
|
||||||
OBJECT,
|
OBJECT,
|
||||||
|
OBJECTS,
|
||||||
OCCURRENCES_REGEX,
|
OCCURRENCES_REGEX,
|
||||||
OCTETS,
|
OCTETS,
|
||||||
OCTET_LENGTH,
|
OCTET_LENGTH,
|
||||||
|
|
|
@ -14231,7 +14231,7 @@ impl<'a> Parser<'a> {
|
||||||
false
|
false
|
||||||
}
|
}
|
||||||
|
|
||||||
fn parse_show_stmt_options(&mut self) -> Result<ShowStatementOptions, ParserError> {
|
pub(crate) fn parse_show_stmt_options(&mut self) -> Result<ShowStatementOptions, ParserError> {
|
||||||
let show_in;
|
let show_in;
|
||||||
let mut filter_position = None;
|
let mut filter_position = None;
|
||||||
if self.dialect.supports_show_like_before_in() {
|
if self.dialect.supports_show_like_before_in() {
|
||||||
|
|
|
@ -3083,6 +3083,7 @@ fn test_parentheses_overflow() {
|
||||||
#[test]
|
#[test]
|
||||||
fn test_show_databases() {
|
fn test_show_databases() {
|
||||||
snowflake().verified_stmt("SHOW DATABASES");
|
snowflake().verified_stmt("SHOW DATABASES");
|
||||||
|
snowflake().verified_stmt("SHOW TERSE DATABASES");
|
||||||
snowflake().verified_stmt("SHOW DATABASES HISTORY");
|
snowflake().verified_stmt("SHOW DATABASES HISTORY");
|
||||||
snowflake().verified_stmt("SHOW DATABASES LIKE '%abc%'");
|
snowflake().verified_stmt("SHOW DATABASES LIKE '%abc%'");
|
||||||
snowflake().verified_stmt("SHOW DATABASES STARTS WITH 'demo_db'");
|
snowflake().verified_stmt("SHOW DATABASES STARTS WITH 'demo_db'");
|
||||||
|
@ -3095,6 +3096,7 @@ fn test_show_databases() {
|
||||||
#[test]
|
#[test]
|
||||||
fn test_parse_show_schemas() {
|
fn test_parse_show_schemas() {
|
||||||
snowflake().verified_stmt("SHOW SCHEMAS");
|
snowflake().verified_stmt("SHOW SCHEMAS");
|
||||||
|
snowflake().verified_stmt("SHOW TERSE SCHEMAS");
|
||||||
snowflake().verified_stmt("SHOW SCHEMAS IN ACCOUNT");
|
snowflake().verified_stmt("SHOW SCHEMAS IN ACCOUNT");
|
||||||
snowflake().verified_stmt("SHOW SCHEMAS IN ACCOUNT abc");
|
snowflake().verified_stmt("SHOW SCHEMAS IN ACCOUNT abc");
|
||||||
snowflake().verified_stmt("SHOW SCHEMAS IN DATABASE");
|
snowflake().verified_stmt("SHOW SCHEMAS IN DATABASE");
|
||||||
|
@ -3104,9 +3106,51 @@ fn test_parse_show_schemas() {
|
||||||
snowflake().verified_stmt("SHOW SCHEMAS IN DATABASE STARTS WITH 'abc' LIMIT 20 FROM 'xyz'");
|
snowflake().verified_stmt("SHOW SCHEMAS IN DATABASE STARTS WITH 'abc' LIMIT 20 FROM 'xyz'");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_parse_show_objects() {
|
||||||
|
snowflake().verified_stmt("SHOW OBJECTS");
|
||||||
|
snowflake().verified_stmt("SHOW OBJECTS IN abc");
|
||||||
|
snowflake().verified_stmt("SHOW OBJECTS LIKE '%test%' IN abc");
|
||||||
|
snowflake().verified_stmt("SHOW OBJECTS IN ACCOUNT");
|
||||||
|
snowflake().verified_stmt("SHOW OBJECTS IN DATABASE");
|
||||||
|
snowflake().verified_stmt("SHOW OBJECTS IN DATABASE abc");
|
||||||
|
snowflake().verified_stmt("SHOW OBJECTS IN SCHEMA");
|
||||||
|
snowflake().verified_stmt("SHOW OBJECTS IN SCHEMA abc");
|
||||||
|
snowflake().verified_stmt("SHOW TERSE OBJECTS");
|
||||||
|
snowflake().verified_stmt("SHOW TERSE OBJECTS IN abc");
|
||||||
|
snowflake().verified_stmt("SHOW TERSE OBJECTS LIKE '%test%' IN abc");
|
||||||
|
snowflake().verified_stmt("SHOW TERSE OBJECTS LIKE '%test%' IN abc STARTS WITH 'b'");
|
||||||
|
snowflake().verified_stmt("SHOW TERSE OBJECTS LIKE '%test%' IN abc STARTS WITH 'b' LIMIT 10");
|
||||||
|
snowflake()
|
||||||
|
.verified_stmt("SHOW TERSE OBJECTS LIKE '%test%' IN abc STARTS WITH 'b' LIMIT 10 FROM 'x'");
|
||||||
|
match snowflake().verified_stmt("SHOW TERSE OBJECTS LIKE '%test%' IN abc") {
|
||||||
|
Statement::ShowObjects(ShowObjects {
|
||||||
|
terse,
|
||||||
|
show_options,
|
||||||
|
}) => {
|
||||||
|
assert!(terse);
|
||||||
|
let name = match show_options.show_in {
|
||||||
|
Some(ShowStatementIn {
|
||||||
|
parent_name: Some(val),
|
||||||
|
..
|
||||||
|
}) => val.to_string(),
|
||||||
|
_ => unreachable!(),
|
||||||
|
};
|
||||||
|
assert_eq!("abc", name);
|
||||||
|
let like = match show_options.filter_position {
|
||||||
|
Some(ShowStatementFilterPosition::Infix(ShowStatementFilter::Like(val))) => val,
|
||||||
|
_ => unreachable!(),
|
||||||
|
};
|
||||||
|
assert_eq!("%test%", like);
|
||||||
|
}
|
||||||
|
_ => unreachable!(),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_parse_show_tables() {
|
fn test_parse_show_tables() {
|
||||||
snowflake().verified_stmt("SHOW TABLES");
|
snowflake().verified_stmt("SHOW TABLES");
|
||||||
|
snowflake().verified_stmt("SHOW TERSE TABLES");
|
||||||
snowflake().verified_stmt("SHOW TABLES IN ACCOUNT");
|
snowflake().verified_stmt("SHOW TABLES IN ACCOUNT");
|
||||||
snowflake().verified_stmt("SHOW TABLES IN DATABASE");
|
snowflake().verified_stmt("SHOW TABLES IN DATABASE");
|
||||||
snowflake().verified_stmt("SHOW TABLES IN DATABASE xyz");
|
snowflake().verified_stmt("SHOW TABLES IN DATABASE xyz");
|
||||||
|
@ -3129,6 +3173,7 @@ fn test_parse_show_tables() {
|
||||||
#[test]
|
#[test]
|
||||||
fn test_show_views() {
|
fn test_show_views() {
|
||||||
snowflake().verified_stmt("SHOW VIEWS");
|
snowflake().verified_stmt("SHOW VIEWS");
|
||||||
|
snowflake().verified_stmt("SHOW TERSE VIEWS");
|
||||||
snowflake().verified_stmt("SHOW VIEWS IN ACCOUNT");
|
snowflake().verified_stmt("SHOW VIEWS IN ACCOUNT");
|
||||||
snowflake().verified_stmt("SHOW VIEWS IN DATABASE");
|
snowflake().verified_stmt("SHOW VIEWS IN DATABASE");
|
||||||
snowflake().verified_stmt("SHOW VIEWS IN DATABASE xyz");
|
snowflake().verified_stmt("SHOW VIEWS IN DATABASE xyz");
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue