Skip to content

Commit 4a05b6f

Browse files
authored
Merge branch 'apache:main' into Value_support_for_visitor
2 parents e5248ef + 68c41a9 commit 4a05b6f

File tree

9 files changed

+197
-83
lines changed

9 files changed

+197
-83
lines changed

src/ast/query.rs

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2077,20 +2077,34 @@ impl fmt::Display for Join {
20772077
self.relation,
20782078
suffix(constraint)
20792079
),
2080-
JoinOperator::LeftOuter(constraint) => write!(
2080+
JoinOperator::Left(constraint) => write!(
20812081
f,
20822082
" {}LEFT JOIN {}{}",
20832083
prefix(constraint),
20842084
self.relation,
20852085
suffix(constraint)
20862086
),
2087-
JoinOperator::RightOuter(constraint) => write!(
2087+
JoinOperator::LeftOuter(constraint) => write!(
2088+
f,
2089+
" {}LEFT OUTER JOIN {}{}",
2090+
prefix(constraint),
2091+
self.relation,
2092+
suffix(constraint)
2093+
),
2094+
JoinOperator::Right(constraint) => write!(
20882095
f,
20892096
" {}RIGHT JOIN {}{}",
20902097
prefix(constraint),
20912098
self.relation,
20922099
suffix(constraint)
20932100
),
2101+
JoinOperator::RightOuter(constraint) => write!(
2102+
f,
2103+
" {}RIGHT OUTER JOIN {}{}",
2104+
prefix(constraint),
2105+
self.relation,
2106+
suffix(constraint)
2107+
),
20942108
JoinOperator::FullOuter(constraint) => write!(
20952109
f,
20962110
" {}FULL JOIN {}{}",
@@ -2162,7 +2176,9 @@ impl fmt::Display for Join {
21622176
pub enum JoinOperator {
21632177
Join(JoinConstraint),
21642178
Inner(JoinConstraint),
2179+
Left(JoinConstraint),
21652180
LeftOuter(JoinConstraint),
2181+
Right(JoinConstraint),
21662182
RightOuter(JoinConstraint),
21672183
FullOuter(JoinConstraint),
21682184
CrossJoin,
@@ -2547,13 +2563,18 @@ impl fmt::Display for SelectInto {
25472563
/// e.g. GROUP BY year WITH ROLLUP WITH TOTALS
25482564
///
25492565
/// [ClickHouse]: <https://clickhouse.com/docs/en/sql-reference/statements/select/group-by#rollup-modifier>
2550-
#[derive(Debug, Copy, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
2566+
#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
25512567
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
25522568
#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
25532569
pub enum GroupByWithModifier {
25542570
Rollup,
25552571
Cube,
25562572
Totals,
2573+
/// Hive supports GROUP BY GROUPING SETS syntax.
2574+
/// e.g. GROUP BY year , month GROUPING SETS((year,month),(year),(month))
2575+
///
2576+
/// [Hive]: <https://cwiki.apache.org/confluence/pages/viewpage.action?pageId=30151323#EnhancedAggregation,Cube,GroupingandRollup-GROUPINGSETSclause>
2577+
GroupingSets(Expr),
25572578
}
25582579

25592580
impl fmt::Display for GroupByWithModifier {
@@ -2562,6 +2583,9 @@ impl fmt::Display for GroupByWithModifier {
25622583
GroupByWithModifier::Rollup => write!(f, "WITH ROLLUP"),
25632584
GroupByWithModifier::Cube => write!(f, "WITH CUBE"),
25642585
GroupByWithModifier::Totals => write!(f, "WITH TOTALS"),
2586+
GroupByWithModifier::GroupingSets(expr) => {
2587+
write!(f, "{expr}")
2588+
}
25652589
}
25662590
}
25672591
}

src/ast/spans.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2010,7 +2010,9 @@ impl Spanned for JoinOperator {
20102010
match self {
20112011
JoinOperator::Join(join_constraint) => join_constraint.span(),
20122012
JoinOperator::Inner(join_constraint) => join_constraint.span(),
2013+
JoinOperator::Left(join_constraint) => join_constraint.span(),
20132014
JoinOperator::LeftOuter(join_constraint) => join_constraint.span(),
2015+
JoinOperator::Right(join_constraint) => join_constraint.span(),
20142016
JoinOperator::RightOuter(join_constraint) => join_constraint.span(),
20152017
JoinOperator::FullOuter(join_constraint) => join_constraint.span(),
20162018
JoinOperator::CrossJoin => Span::empty(),

src/dialect/clickhouse.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,4 +79,14 @@ impl Dialect for ClickHouseDialect {
7979
fn supports_from_first_select(&self) -> bool {
8080
true
8181
}
82+
83+
// See <https://clickhouse.com/docs/en/sql-reference/aggregate-functions/grouping_function#grouping-sets>
84+
fn supports_group_by_expr(&self) -> bool {
85+
true
86+
}
87+
88+
/// See <https://clickhouse.com/docs/en/sql-reference/statements/select/group-by#rollup-modifier>
89+
fn supports_group_by_with_modifier(&self) -> bool {
90+
true
91+
}
8292
}

src/dialect/generic.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,10 @@ impl Dialect for GenericDialect {
4848
true
4949
}
5050

51+
fn supports_group_by_with_modifier(&self) -> bool {
52+
true
53+
}
54+
5155
fn supports_connect_by(&self) -> bool {
5256
true
5357
}

src/dialect/hive.rs

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -52,18 +52,23 @@ impl Dialect for HiveDialect {
5252
true
5353
}
5454

55-
/// See Hive <https://cwiki.apache.org/confluence/pages/viewpage.action?pageId=27362061#Tutorial-BuiltInOperators>
55+
/// See <https://cwiki.apache.org/confluence/pages/viewpage.action?pageId=27362061#Tutorial-BuiltInOperators>
5656
fn supports_bang_not_operator(&self) -> bool {
5757
true
5858
}
5959

60-
/// See Hive <https://cwiki.apache.org/confluence/pages/viewpage.action?pageId=27362036#LanguageManualDML-Loadingfilesintotables>
60+
/// See <https://cwiki.apache.org/confluence/pages/viewpage.action?pageId=27362036#LanguageManualDML-Loadingfilesintotables>
6161
fn supports_load_data(&self) -> bool {
6262
true
6363
}
6464

65-
/// See Hive <https://cwiki.apache.org/confluence/display/hive/languagemanual+sampling>
65+
/// See <https://cwiki.apache.org/confluence/display/hive/languagemanual+sampling>
6666
fn supports_table_sample_before_alias(&self) -> bool {
6767
true
6868
}
69+
70+
/// See <https://cwiki.apache.org/confluence/pages/viewpage.action?pageId=30151323#EnhancedAggregation,Cube,GroupingandRollup-CubesandRollupsr>
71+
fn supports_group_by_with_modifier(&self) -> bool {
72+
true
73+
}
6974
}

src/dialect/mod.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -245,6 +245,12 @@ pub trait Dialect: Debug + Any {
245245
false
246246
}
247247

248+
/// Returns true if the dialects supports `GROUP BY` modifiers prefixed by a `WITH` keyword.
249+
/// Example: `GROUP BY value WITH ROLLUP`.
250+
fn supports_group_by_with_modifier(&self) -> bool {
251+
false
252+
}
253+
248254
/// Returns true if the dialect supports CONNECT BY.
249255
fn supports_connect_by(&self) -> bool {
250256
false

src/parser/mod.rs

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5740,7 +5740,7 @@ impl<'a> Parser<'a> {
57405740
drop_behavior,
57415741
})
57425742
}
5743-
/// ```sql
5743+
/// ```sql
57445744
/// DROP CONNECTOR [IF EXISTS] name
57455745
/// ```
57465746
///
@@ -9148,7 +9148,7 @@ impl<'a> Parser<'a> {
91489148
};
91499149

91509150
let mut modifiers = vec![];
9151-
if dialect_of!(self is ClickHouseDialect | GenericDialect) {
9151+
if self.dialect.supports_group_by_with_modifier() {
91529152
loop {
91539153
if !self.parse_keyword(Keyword::WITH) {
91549154
break;
@@ -9171,6 +9171,14 @@ impl<'a> Parser<'a> {
91719171
});
91729172
}
91739173
}
9174+
if self.parse_keywords(&[Keyword::GROUPING, Keyword::SETS]) {
9175+
self.expect_token(&Token::LParen)?;
9176+
let result = self.parse_comma_separated(|p| p.parse_tuple(true, true))?;
9177+
self.expect_token(&Token::RParen)?;
9178+
modifiers.push(GroupByWithModifier::GroupingSets(Expr::GroupingSets(
9179+
result,
9180+
)));
9181+
};
91749182
let group_by = match expressions {
91759183
None => GroupByExpr::All(modifiers),
91769184
Some(exprs) => GroupByExpr::Expressions(exprs, modifiers),
@@ -11182,9 +11190,9 @@ impl<'a> Parser<'a> {
1118211190
}
1118311191
Some(Keyword::JOIN) => {
1118411192
if is_left {
11185-
JoinOperator::LeftOuter
11193+
JoinOperator::Left
1118611194
} else {
11187-
JoinOperator::RightOuter
11195+
JoinOperator::Right
1118811196
}
1118911197
}
1119011198
_ => {

tests/sqlparser_clickhouse.rs

Lines changed: 0 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -1069,61 +1069,6 @@ fn parse_create_materialized_view() {
10691069
clickhouse_and_generic().verified_stmt(sql);
10701070
}
10711071

1072-
#[test]
1073-
fn parse_group_by_with_modifier() {
1074-
let clauses = ["x", "a, b", "ALL"];
1075-
let modifiers = [
1076-
"WITH ROLLUP",
1077-
"WITH CUBE",
1078-
"WITH TOTALS",
1079-
"WITH ROLLUP WITH CUBE",
1080-
];
1081-
let expected_modifiers = [
1082-
vec![GroupByWithModifier::Rollup],
1083-
vec![GroupByWithModifier::Cube],
1084-
vec![GroupByWithModifier::Totals],
1085-
vec![GroupByWithModifier::Rollup, GroupByWithModifier::Cube],
1086-
];
1087-
for clause in &clauses {
1088-
for (modifier, expected_modifier) in modifiers.iter().zip(expected_modifiers.iter()) {
1089-
let sql = format!("SELECT * FROM t GROUP BY {clause} {modifier}");
1090-
match clickhouse_and_generic().verified_stmt(&sql) {
1091-
Statement::Query(query) => {
1092-
let group_by = &query.body.as_select().unwrap().group_by;
1093-
if clause == &"ALL" {
1094-
assert_eq!(group_by, &GroupByExpr::All(expected_modifier.to_vec()));
1095-
} else {
1096-
assert_eq!(
1097-
group_by,
1098-
&GroupByExpr::Expressions(
1099-
clause
1100-
.split(", ")
1101-
.map(|c| Identifier(Ident::new(c)))
1102-
.collect(),
1103-
expected_modifier.to_vec()
1104-
)
1105-
);
1106-
}
1107-
}
1108-
_ => unreachable!(),
1109-
}
1110-
}
1111-
}
1112-
1113-
// invalid cases
1114-
let invalid_cases = [
1115-
"SELECT * FROM t GROUP BY x WITH",
1116-
"SELECT * FROM t GROUP BY x WITH ROLLUP CUBE",
1117-
"SELECT * FROM t GROUP BY x WITH WITH ROLLUP",
1118-
"SELECT * FROM t GROUP BY WITH ROLLUP",
1119-
];
1120-
for sql in invalid_cases {
1121-
clickhouse_and_generic()
1122-
.parse_sql_statements(sql)
1123-
.expect_err("Expected: one of ROLLUP or CUBE or TOTALS, found: WITH");
1124-
}
1125-
}
1126-
11271072
#[test]
11281073
fn parse_select_order_by_with_fill_interpolate() {
11291074
let sql = "SELECT id, fname, lname FROM customer WHERE id < 5 \

0 commit comments

Comments
 (0)