Skip to content

Commit 5aa4d9a

Browse files
feat: implement wildcard select ilike
1 parent 16cdc92 commit 5aa4d9a

6 files changed

Lines changed: 76 additions & 3 deletions

File tree

src/ast/mod.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,8 @@ pub use self::ddl::{
4040
pub use self::operator::{BinaryOperator, UnaryOperator};
4141
pub use self::query::{
4242
ConnectBy, Cte, CteAsMaterialized, Distinct, ExceptSelectItem, ExcludeSelectItem, Fetch,
43-
ForClause, ForJson, ForXml, GroupByExpr, IdentWithAlias, Join, JoinConstraint, JoinOperator,
44-
JsonTableColumn, JsonTableColumnErrorHandling, LateralView, LockClause, LockType,
43+
ForClause, ForJson, ForXml, GroupByExpr, IdentWithAlias, IlikeSelectItem, Join, JoinConstraint,
44+
JoinOperator, JsonTableColumn, JsonTableColumnErrorHandling, LateralView, LockClause, LockType,
4545
NamedWindowDefinition, NonBlock, Offset, OffsetRows, OrderByExpr, Query, RenameSelectItem,
4646
ReplaceSelectElement, ReplaceSelectItem, Select, SelectInto, SelectItem, SetExpr, SetOperator,
4747
SetQuantifier, Table, TableAlias, TableFactor, TableVersion, TableWithJoins, Top, TopQuantity,

src/ast/query.rs

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -477,6 +477,9 @@ impl fmt::Display for IdentWithAlias {
477477
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
478478
#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
479479
pub struct WildcardAdditionalOptions {
480+
/// `[ILIKE...]`.
481+
/// Snowflake syntax: <https://docs.snowflake.com/en/sql-reference/sql/select>
482+
pub opt_ilike: Option<IlikeSelectItem>,
480483
/// `[EXCLUDE...]`.
481484
pub opt_exclude: Option<ExcludeSelectItem>,
482485
/// `[EXCEPT...]`.
@@ -492,6 +495,9 @@ pub struct WildcardAdditionalOptions {
492495

493496
impl fmt::Display for WildcardAdditionalOptions {
494497
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
498+
if let Some(ilike) = &self.opt_ilike {
499+
write!(f, " {ilike}")?;
500+
}
495501
if let Some(exclude) = &self.opt_exclude {
496502
write!(f, " {exclude}")?;
497503
}
@@ -508,6 +514,25 @@ impl fmt::Display for WildcardAdditionalOptions {
508514
}
509515
}
510516

517+
/// Snowflake `ILIKE` information.
518+
///
519+
/// # Syntax
520+
/// ```plaintext
521+
/// ILIKE <value>
522+
/// ```
523+
#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
524+
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
525+
#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
526+
pub struct IlikeSelectItem {
527+
pub pattern: Expr,
528+
}
529+
530+
impl fmt::Display for IlikeSelectItem {
531+
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
532+
write!(f, "ILIKE {}", self.pattern)?;
533+
Ok(())
534+
}
535+
}
511536
/// Snowflake `EXCLUDE` information.
512537
///
513538
/// # Syntax

src/parser/mod.rs

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8775,9 +8775,14 @@ impl<'a> Parser<'a> {
87758775
pub fn parse_wildcard_additional_options(
87768776
&mut self,
87778777
) -> Result<WildcardAdditionalOptions, ParserError> {
8778+
let opt_ilike = if dialect_of!(self is GenericDialect | SnowflakeDialect) {
8779+
self.parse_optional_select_item_ilike()?
8780+
} else {
8781+
None
8782+
};
87788783
let opt_exclude = if dialect_of!(self is GenericDialect | DuckDbDialect | SnowflakeDialect)
87798784
{
8780-
self.parse_optional_select_item_exclude()?
8785+
self.parse_optional_select_item_exclude(opt_ilike.is_some())?
87818786
} else {
87828787
None
87838788
};
@@ -8801,20 +8806,39 @@ impl<'a> Parser<'a> {
88018806
};
88028807

88038808
Ok(WildcardAdditionalOptions {
8809+
opt_ilike,
88048810
opt_exclude,
88058811
opt_except,
88068812
opt_rename,
88078813
opt_replace,
88088814
})
88098815
}
88108816

8817+
pub fn parse_optional_select_item_ilike(
8818+
&mut self,
8819+
) -> Result<Option<IlikeSelectItem>, ParserError> {
8820+
let opt_ilike = if self.parse_keyword(Keyword::ILIKE) {
8821+
let pattern = self.parse_value()?;
8822+
Some(IlikeSelectItem {
8823+
pattern: Expr::Value(pattern),
8824+
})
8825+
} else {
8826+
None
8827+
};
8828+
Ok(opt_ilike)
8829+
}
8830+
88118831
/// Parse an [`Exclude`](ExcludeSelectItem) information for wildcard select items.
88128832
///
88138833
/// If it is not possible to parse it, will return an option.
88148834
pub fn parse_optional_select_item_exclude(
88158835
&mut self,
8836+
opt_ilike: bool,
88168837
) -> Result<Option<ExcludeSelectItem>, ParserError> {
88178838
let opt_exclude = if self.parse_keyword(Keyword::EXCLUDE) {
8839+
if opt_ilike {
8840+
return Err(ParserError::ParserError("Unexpected EXCLUDE".to_string()));
8841+
}
88188842
if self.consume_token(&Token::LParen) {
88198843
let columns =
88208844
self.parse_comma_separated(|parser| parser.parse_identifier(false))?;

tests/sqlparser_common.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6560,6 +6560,7 @@ fn lateral_function() {
65606560
distinct: None,
65616561
top: None,
65626562
projection: vec![SelectItem::Wildcard(WildcardAdditionalOptions {
6563+
opt_ilike: None,
65636564
opt_exclude: None,
65646565
opt_except: None,
65656566
opt_rename: None,

tests/sqlparser_duckdb.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,7 @@ fn test_select_union_by_name() {
148148
distinct: None,
149149
top: None,
150150
projection: vec![SelectItem::Wildcard(WildcardAdditionalOptions {
151+
opt_ilike: None,
151152
opt_exclude: None,
152153
opt_except: None,
153154
opt_rename: None,
@@ -183,6 +184,7 @@ fn test_select_union_by_name() {
183184
distinct: None,
184185
top: None,
185186
projection: vec![SelectItem::Wildcard(WildcardAdditionalOptions {
187+
opt_ilike: None,
186188
opt_exclude: None,
187189
opt_except: None,
188190
opt_rename: None,

tests/sqlparser_snowflake.rs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1615,3 +1615,24 @@ fn test_select_wildcard_with_replace() {
16151615
});
16161616
assert_eq!(expected, select.projection[0]);
16171617
}
1618+
1619+
#[test]
1620+
fn test_select_wildcard_with_ilike() {
1621+
let select = snowflake_and_generic().verified_only_select(r#"SELECT * ILIKE '%id%' FROM tbl"#);
1622+
let expected = SelectItem::Wildcard(WildcardAdditionalOptions {
1623+
opt_ilike: Some(IlikeSelectItem {
1624+
pattern: Expr::Value(Value::SingleQuotedString("%id%".to_owned())),
1625+
}),
1626+
..Default::default()
1627+
});
1628+
assert_eq!(expected, select.projection[0]);
1629+
}
1630+
1631+
#[test]
1632+
fn test_select_wildcard_with_ilike_replace() {
1633+
let res = snowflake().parse_sql_statements(r#"SELECT * ILIKE '%id%' EXCLUDE col FROM tbl"#);
1634+
assert_eq!(
1635+
res.unwrap_err().to_string(),
1636+
"sql parser error: Unexpected EXCLUDE"
1637+
);
1638+
}

0 commit comments

Comments
 (0)