Skip to content

Commit fd21fae

Browse files
authored
Fallback to identifier parsing if expression parsing fails (#1513)
1 parent 0fb2ef3 commit fd21fae

File tree

6 files changed

+277
-191
lines changed

6 files changed

+277
-191
lines changed

src/dialect/mod.rs

+6
Original file line numberDiff line numberDiff line change
@@ -681,6 +681,12 @@ pub trait Dialect: Debug + Any {
681681
fn supports_partiql(&self) -> bool {
682682
false
683683
}
684+
685+
/// Returns true if the specified keyword is reserved and cannot be
686+
/// used as an identifier without special handling like quoting.
687+
fn is_reserved_for_identifier(&self, kw: Keyword) -> bool {
688+
keywords::RESERVED_FOR_IDENTIFIER.contains(&kw)
689+
}
684690
}
685691

686692
/// This represents the operators for which precedence must be defined

src/dialect/snowflake.rs

+12
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,8 @@ use alloc::vec::Vec;
3838
#[cfg(not(feature = "std"))]
3939
use alloc::{format, vec};
4040

41+
use super::keywords::RESERVED_FOR_IDENTIFIER;
42+
4143
/// A [`Dialect`] for [Snowflake](https://www.snowflake.com/)
4244
#[derive(Debug, Default)]
4345
pub struct SnowflakeDialect;
@@ -214,6 +216,16 @@ impl Dialect for SnowflakeDialect {
214216
fn supports_show_like_before_in(&self) -> bool {
215217
true
216218
}
219+
220+
fn is_reserved_for_identifier(&self, kw: Keyword) -> bool {
221+
// Unreserve some keywords that Snowflake accepts as identifiers
222+
// See: https://docs.snowflake.com/en/sql-reference/reserved-keywords
223+
if matches!(kw, Keyword::INTERVAL) {
224+
false
225+
} else {
226+
RESERVED_FOR_IDENTIFIER.contains(&kw)
227+
}
228+
}
217229
}
218230

219231
/// Parse snowflake create table statement.

src/keywords.rs

+10
Original file line numberDiff line numberDiff line change
@@ -948,3 +948,13 @@ pub const RESERVED_FOR_COLUMN_ALIAS: &[Keyword] = &[
948948
Keyword::INTO,
949949
Keyword::END,
950950
];
951+
952+
/// Global list of reserved keywords that cannot be parsed as identifiers
953+
/// without special handling like quoting. Parser should call `Dialect::is_reserved_for_identifier`
954+
/// to allow for each dialect to customize the list.
955+
pub const RESERVED_FOR_IDENTIFIER: &[Keyword] = &[
956+
Keyword::EXISTS,
957+
Keyword::INTERVAL,
958+
Keyword::STRUCT,
959+
Keyword::TRIM,
960+
];

0 commit comments

Comments
 (0)