Skip to content

Commit 5fe1b77

Browse files
committed
fixup
1 parent 4670853 commit 5fe1b77

File tree

3 files changed

+39
-40
lines changed

3 files changed

+39
-40
lines changed

src/ast/mod.rs

+5-21
Original file line numberDiff line numberDiff line change
@@ -2423,7 +2423,6 @@ pub struct ListAgg {
24232423
pub expr: Box<Expr>,
24242424
pub separator: Option<Box<Expr>>,
24252425
pub on_overflow: Option<ListAggOnOverflow>,
2426-
pub within_group: Vec<OrderByExpr>,
24272426
}
24282427

24292428
impl fmt::Display for ListAgg {
@@ -2441,13 +2440,6 @@ impl fmt::Display for ListAgg {
24412440
write!(f, "{}", on_overflow)?;
24422441
}
24432442
write!(f, ")")?;
2444-
if !self.within_group.is_empty() {
2445-
write!(
2446-
f,
2447-
" WITHIN GROUP (ORDER BY {})",
2448-
display_comma_separated(&self.within_group)
2449-
)?;
2450-
}
24512443
Ok(())
24522444
}
24532445
}
@@ -2497,7 +2489,6 @@ pub struct ArrayAgg {
24972489
pub expr: Box<Expr>,
24982490
pub order_by: Option<Box<OrderByExpr>>,
24992491
pub limit: Option<Box<Expr>>,
2500-
pub within_group: bool, // order by is used inside a within group or not
25012492
}
25022493

25032494
impl fmt::Display for ArrayAgg {
@@ -2508,20 +2499,13 @@ impl fmt::Display for ArrayAgg {
25082499
if self.distinct { "DISTINCT " } else { "" },
25092500
self.expr
25102501
)?;
2511-
if !self.within_group {
2512-
if let Some(order_by) = &self.order_by {
2513-
write!(f, " ORDER BY {}", order_by)?;
2514-
}
2515-
if let Some(limit) = &self.limit {
2516-
write!(f, " LIMIT {}", limit)?;
2517-
}
2502+
if let Some(order_by) = &self.order_by {
2503+
write!(f, " ORDER BY {}", order_by)?;
25182504
}
2519-
write!(f, ")")?;
2520-
if self.within_group {
2521-
if let Some(order_by) = &self.order_by {
2522-
write!(f, " WITHIN GROUP (ORDER BY {})", order_by)?;
2523-
}
2505+
if let Some(limit) = &self.limit {
2506+
write!(f, " LIMIT {}", limit)?;
25242507
}
2508+
write!(f, ")")?;
25252509
Ok(())
25262510
}
25272511
}

src/parser.rs

+24-11
Original file line numberDiff line numberDiff line change
@@ -1014,17 +1014,24 @@ impl<'a> Parser<'a> {
10141014
self.expect_keywords(&[Keyword::ORDER, Keyword::BY])?;
10151015
let order_by_expr = self.parse_comma_separated(Parser::parse_order_by_expr)?;
10161016
self.expect_token(&Token::RParen)?;
1017-
order_by_expr
1017+
Some(order_by_expr)
10181018
} else {
1019-
vec![]
1019+
None
10201020
};
1021-
Ok(Expr::ListAgg(ListAgg {
1021+
let list_agg = Expr::ListAgg(ListAgg {
10221022
distinct,
10231023
expr,
10241024
separator,
10251025
on_overflow,
1026-
within_group,
1027-
}))
1026+
});
1027+
Ok(if let Some(within_group) = within_group {
1028+
Expr::WithinGroup(WithinGroup {
1029+
expr: Box::new(list_agg),
1030+
order_by: within_group,
1031+
})
1032+
} else {
1033+
list_agg
1034+
})
10281035
}
10291036

10301037
pub fn parse_array_agg_expr(&mut self) -> Result<Expr, ParserError> {
@@ -1050,7 +1057,6 @@ impl<'a> Parser<'a> {
10501057
expr,
10511058
order_by,
10521059
limit,
1053-
within_group: false,
10541060
}));
10551061
}
10561062
// Snowflake defines ORDERY BY in within group instead of inside the function like
@@ -1061,18 +1067,25 @@ impl<'a> Parser<'a> {
10611067
self.expect_keywords(&[Keyword::ORDER, Keyword::BY])?;
10621068
let order_by_expr = self.parse_order_by_expr()?;
10631069
self.expect_token(&Token::RParen)?;
1064-
Some(Box::new(order_by_expr))
1070+
Some(order_by_expr)
10651071
} else {
10661072
None
10671073
};
10681074

1069-
Ok(Expr::ArrayAgg(ArrayAgg {
1075+
let array_agg = Expr::ArrayAgg(ArrayAgg {
10701076
distinct,
10711077
expr,
1072-
order_by: within_group,
1078+
order_by: None,
10731079
limit: None,
1074-
within_group: true,
1075-
}))
1080+
});
1081+
Ok(if let Some(within_group) = within_group {
1082+
Expr::WithinGroup(WithinGroup {
1083+
expr: Box::new(array_agg),
1084+
order_by: vec![within_group],
1085+
})
1086+
} else {
1087+
array_agg
1088+
})
10761089
}
10771090

10781091
// This function parses date/time fields for both the EXTRACT function-like

tests/sqlparser_common.rs

+10-8
Original file line numberDiff line numberDiff line change
@@ -1702,14 +1702,16 @@ fn parse_listagg() {
17021702
},
17031703
];
17041704
assert_eq!(
1705-
&Expr::ListAgg(ListAgg {
1706-
distinct: true,
1707-
expr,
1708-
separator: Some(Box::new(Expr::Value(Value::SingleQuotedString(
1709-
", ".to_string()
1710-
)))),
1711-
on_overflow,
1712-
within_group
1705+
&Expr::WithinGroup(WithinGroup {
1706+
expr: Box::new(Expr::ListAgg(ListAgg {
1707+
distinct: true,
1708+
expr,
1709+
separator: Some(Box::new(Expr::Value(Value::SingleQuotedString(
1710+
", ".to_string()
1711+
)))),
1712+
on_overflow,
1713+
})),
1714+
order_by: within_group
17131715
}),
17141716
expr_from_projection(only(&select.projection))
17151717
);

0 commit comments

Comments
 (0)