Skip to content

Commit 47ba442

Browse files
committed
Simplify parsing, remove InterpolateArg::Empty
1 parent 7022e42 commit 47ba442

File tree

4 files changed

+45
-53
lines changed

4 files changed

+45
-53
lines changed

src/ast/mod.rs

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -43,15 +43,14 @@ pub use self::operator::{BinaryOperator, UnaryOperator};
4343
pub use self::query::{
4444
AfterMatchSkip, ConnectBy, Cte, CteAsMaterialized, Distinct, EmptyMatchesMode,
4545
ExceptSelectItem, ExcludeSelectItem, ExprWithAlias, Fetch, ForClause, ForJson, ForXml,
46-
GroupByExpr, GroupByWithModifier, IdentWithAlias, IlikeSelectItem, Interpolation,
47-
InterpolationArg, Join, JoinConstraint, JoinOperator, JsonTableColumn,
48-
JsonTableColumnErrorHandling, LateralView, LockClause, LockType, MatchRecognizePattern,
49-
MatchRecognizeSymbol, Measure, NamedWindowDefinition, NamedWindowExpr, NonBlock, Offset,
50-
OffsetRows, OrderByExpr, PivotValueSource, Query, RenameSelectItem, RepetitionQuantifier,
51-
ReplaceSelectElement, ReplaceSelectItem, RowsPerMatch, Select, SelectInto, SelectItem, SetExpr,
52-
SetOperator, SetQuantifier, SymbolDefinition, Table, TableAlias, TableFactor, TableVersion,
53-
TableWithJoins, Top, TopQuantity, ValueTableMode, Values, WildcardAdditionalOptions, With,
54-
WithFill,
46+
GroupByExpr, GroupByWithModifier, IdentWithAlias, IlikeSelectItem, Interpolate, InterpolateArg,
47+
Join, JoinConstraint, JoinOperator, JsonTableColumn, JsonTableColumnErrorHandling, LateralView,
48+
LockClause, LockType, MatchRecognizePattern, MatchRecognizeSymbol, Measure,
49+
NamedWindowDefinition, NamedWindowExpr, NonBlock, Offset, OffsetRows, OrderByExpr,
50+
PivotValueSource, Query, RenameSelectItem, RepetitionQuantifier, ReplaceSelectElement,
51+
ReplaceSelectItem, RowsPerMatch, Select, SelectInto, SelectItem, SetExpr, SetOperator,
52+
SetQuantifier, SymbolDefinition, Table, TableAlias, TableFactor, TableVersion, TableWithJoins,
53+
Top, TopQuantity, ValueTableMode, Values, WildcardAdditionalOptions, With, WithFill,
5554
};
5655
pub use self::value::{
5756
escape_double_quote_string, escape_quoted_string, DateTimeField, DollarQuotedString,

src/ast/query.rs

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1634,7 +1634,7 @@ pub struct OrderByExpr {
16341634
/// Supported by [ClickHouse syntax]
16351635
///
16361636
/// [ClickHouse syntax]: <https://clickhouse.com/docs/en/sql-reference/statements/select/order-by#order-by-expr-with-fill-modifier>
1637-
pub interpolate: Option<InterpolationArg>,
1637+
pub interpolate: Option<InterpolateArg>,
16381638
}
16391639

16401640
impl fmt::Display for OrderByExpr {
@@ -1655,10 +1655,13 @@ impl fmt::Display for OrderByExpr {
16551655
}
16561656
if let Some(ref interpolate) = self.interpolate {
16571657
match interpolate {
1658-
InterpolationArg::NoBody => write!(f, " INTERPOLATE")?,
1659-
InterpolationArg::EmptyBody => write!(f, " INTERPOLATE ()")?,
1660-
InterpolationArg::Columns(columns) => {
1661-
write!(f, " INTERPOLATE ({})", display_comma_separated(columns))?;
1658+
InterpolateArg::NoBody => write!(f, " INTERPOLATE")?,
1659+
InterpolateArg::Columns(columns) => {
1660+
if columns.is_empty() {
1661+
write!(f, " INTERPOLATE ()")?;
1662+
} else {
1663+
write!(f, " INTERPOLATE ({})", display_comma_separated(columns))?;
1664+
}
16621665
}
16631666
}
16641667
}
@@ -1702,21 +1705,20 @@ impl fmt::Display for WithFill {
17021705
#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
17031706
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
17041707
#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
1705-
pub struct Interpolation {
1706-
pub column: Expr,
1708+
pub struct Interpolate {
1709+
pub column: Ident,
17071710
pub formula: Option<Expr>,
17081711
}
17091712

17101713
#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
17111714
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
17121715
#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
1713-
pub enum InterpolationArg {
1716+
pub enum InterpolateArg {
17141717
NoBody,
1715-
EmptyBody,
1716-
Columns(Vec<Interpolation>),
1718+
Columns(Vec<Interpolate>),
17171719
}
17181720

1719-
impl fmt::Display for Interpolation {
1721+
impl fmt::Display for Interpolate {
17201722
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
17211723
write!(f, "{}", self.column)?;
17221724
if let Some(ref formula) = self.formula {

src/parser/mod.rs

Lines changed: 10 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -10420,19 +10420,13 @@ impl<'a> Parser<'a> {
1042010420
&& self.parse_keyword(Keyword::INTERPOLATE)
1042110421
{
1042210422
if self.consume_token(&Token::LParen) {
10423-
if self.peek_token().token == Token::RParen {
10424-
// INTERPOLATE ()
10425-
self.next_token();
10426-
Some(InterpolationArg::EmptyBody)
10427-
} else {
10428-
// INTERPOLATE ( ... )
10429-
let interpolations = self.parse_interpolations()?;
10430-
self.expect_token(&Token::RParen)?;
10431-
Some(InterpolationArg::Columns(interpolations))
10432-
}
10423+
let interpolations = self.parse_interpolations()?;
10424+
self.expect_token(&Token::RParen)?;
10425+
// INTERPOLATE () and INTERPOLATE ( ... ) variants
10426+
Some(InterpolateArg::Columns(interpolations))
1043310427
} else {
1043410428
// INTERPOLATE
10435-
Some(InterpolationArg::NoBody)
10429+
Some(InterpolateArg::NoBody)
1043610430
}
1043710431
} else {
1043810432
None
@@ -10473,19 +10467,19 @@ impl<'a> Parser<'a> {
1047310467

1047410468
// Parse a set of comma seperated INTERPOLATE expressions (ClickHouse dialect)
1047510469
// that follow the INTERPOLATE keyword in an ORDER BY clause with the WITH FILL modifier
10476-
pub fn parse_interpolations(&mut self) -> Result<Vec<Interpolation>, ParserError> {
10477-
self.parse_comma_separated(|p| p.parse_interpolation())
10470+
pub fn parse_interpolations(&mut self) -> Result<Vec<Interpolate>, ParserError> {
10471+
self.parse_comma_separated0(|p| p.parse_interpolation())
1047810472
}
1047910473

1048010474
// Parse a INTERPOLATE expression (ClickHouse dialect)
10481-
pub fn parse_interpolation(&mut self) -> Result<Interpolation, ParserError> {
10482-
let column = self.parse_expr()?;
10475+
pub fn parse_interpolation(&mut self) -> Result<Interpolate, ParserError> {
10476+
let column = self.parse_identifier(false)?;
1048310477
let formula = if self.parse_keyword(Keyword::AS) {
1048410478
Some(self.parse_expr()?)
1048510479
} else {
1048610480
None
1048710481
};
10488-
Ok(Interpolation { column, formula })
10482+
Ok(Interpolate { column, formula })
1048910483
}
1049010484

1049110485
/// Parse a TOP clause, MSSQL equivalent of LIMIT,

tests/sqlparser_clickhouse.rs

Lines changed: 14 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -712,8 +712,8 @@ fn parse_select_order_by_with_fill_interpolate() {
712712
to: Some(Expr::Value(number("40"))),
713713
step: Some(Expr::Value(number("3"))),
714714
}),
715-
interpolate: Some(InterpolationArg::Columns(vec![Interpolation {
716-
column: Expr::Identifier(Ident::new("col1")),
715+
interpolate: Some(InterpolateArg::Columns(vec![Interpolate {
716+
column: Ident::new("col1"),
717717
formula: Some(Expr::BinaryOp {
718718
left: Box::new(Expr::Identifier(Ident::new("col1"))),
719719
op: BinaryOperator::Plus,
@@ -743,26 +743,26 @@ fn parse_with_fill() {
743743
}
744744

745745
#[test]
746-
fn parse_interpolation_body_with_columns() {
746+
fn parse_interpolate_body_with_columns() {
747747
let sql = "SELECT fname FROM customer ORDER BY fname WITH FILL \
748748
INTERPOLATE (col1 AS col1 + 1, col2 AS col3, col4 AS col4 + 4)";
749749
let select = clickhouse().verified_query(sql);
750750
assert_eq!(
751-
Some(InterpolationArg::Columns(vec![
752-
Interpolation {
753-
column: Expr::Identifier(Ident::new("col1")),
751+
Some(InterpolateArg::Columns(vec![
752+
Interpolate {
753+
column: Ident::new("col1"),
754754
formula: Some(Expr::BinaryOp {
755755
left: Box::new(Expr::Identifier(Ident::new("col1"))),
756756
op: BinaryOperator::Plus,
757757
right: Box::new(Expr::Value(number("1"))),
758758
}),
759759
},
760-
Interpolation {
761-
column: Expr::Identifier(Ident::new("col2")),
760+
Interpolate {
761+
column: Ident::new("col2"),
762762
formula: Some(Expr::Identifier(Ident::new("col3"))),
763763
},
764-
Interpolation {
765-
column: Expr::Identifier(Ident::new("col4")),
764+
Interpolate {
765+
column: Ident::new("col4"),
766766
formula: Some(Expr::BinaryOp {
767767
left: Box::new(Expr::Identifier(Ident::new("col4"))),
768768
op: BinaryOperator::Plus,
@@ -775,21 +775,18 @@ fn parse_interpolation_body_with_columns() {
775775
}
776776

777777
#[test]
778-
fn parse_interpolation_without_body() {
778+
fn parse_interpolate_without_body() {
779779
let sql = "SELECT fname FROM customer ORDER BY fname WITH FILL INTERPOLATE";
780780
let select = clickhouse().verified_query(sql);
781-
assert_eq!(
782-
Some(InterpolationArg::NoBody),
783-
select.order_by[0].interpolate
784-
);
781+
assert_eq!(Some(InterpolateArg::NoBody), select.order_by[0].interpolate);
785782
}
786783

787784
#[test]
788-
fn parse_interpolation_with_empty_body() {
785+
fn parse_interpolate_with_empty_body() {
789786
let sql = "SELECT fname FROM customer ORDER BY fname WITH FILL INTERPOLATE ()";
790787
let select = clickhouse().verified_query(sql);
791788
assert_eq!(
792-
Some(InterpolationArg::EmptyBody),
789+
Some(InterpolateArg::Columns(vec![])),
793790
select.order_by[0].interpolate
794791
);
795792
}

0 commit comments

Comments
 (0)