feat(list): support multiple dialect
@@ -23,6 +23,7 @@ use metrics::histogram;
use sqlparser::ast::Value;
use sqlparser::dialect::keywords::Keyword;
use sqlparser::dialect::Dialect;
+use sqlparser::dialect::GenericDialect;
use sqlparser::dialect::SnowflakeDialect;
use sqlparser::parser::Parser;
use sqlparser::parser::ParserError;
@@ -50,28 +51,30 @@ macro_rules! parser_err {
/// SQL Parser
pub struct DfParser<'a> {
pub(crate) parser: Parser<'a>,
+ pub(crate) sql: &'a str,
}
impl<'a> DfParser<'a> {
/// Parse the specified tokens
- pub fn new(sql: &str) -> Result<Self, ParserError> {
- let dialect = &SnowflakeDialect {};
+ pub fn new(sql: &'a str) -> Result<Self, ParserError> {
+ let dialect = &GenericDialect {};
DfParser::new_with_dialect(sql, dialect)
}
/// Parse the specified tokens with dialect
- pub fn new_with_dialect(sql: &str, dialect: &'a dyn Dialect) -> Result<Self, ParserError> {
+ pub fn new_with_dialect(sql: &'a str, dialect: &'a dyn Dialect) -> Result<Self, ParserError> {
let mut tokenizer = Tokenizer::new(dialect, sql);
let tokens = tokenizer.tokenize()?;
Ok(DfParser {
+ sql,
parser: Parser::new(tokens, dialect),
})
}
/// Parse a SQL statement and produce a set of statements with dialect
pub fn parse_sql(sql: &str) -> Result<(Vec<DfStatement>, Vec<DfHint>), ErrorCode> {
- let dialect = &SnowflakeDialect {};
+ let dialect = &GenericDialect {};
let start = Instant::now();
let result = DfParser::parse_sql_with_dialect(sql, dialect)?;
histogram!(super::metrics::METRIC_PARSER_USEDTIME, start.elapsed());
@@ -181,10 +184,13 @@ impl<'a> DfParser<'a> {
self.parse_call()
}
+ // Change to snowflake dialect for list cmd
Keyword::LIST => {
+ *self = Self::new_with_dialect(self.sql, &SnowflakeDialect {})?;
self.parser.next_token();
self.parse_list_cmd()
}
+
Keyword::NoKeyword => match w.value.to_uppercase().as_str() {
// Use database
"USE" => self.parse_use_database(),
