Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 25 additions & 8 deletions src/formatter/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -807,12 +807,31 @@ impl<'a> Formatter<'a> {
}

fn format_over_clause(&self, node: Node<'a>) -> String {
let mut parts = vec![self.kw("OVER")];
parts.push(" (".to_string());

// The window_specification contains the actual partition/order clauses.
let spec = node.find_child("window_specification").unwrap_or(node);
// `OVER (...)` with an inline window_specification holds the actual
// partition/order/frame clauses and stays parenthesized.
if let Some(spec) = node.find_child("window_specification") {
return format!(
"{} ({})",
self.kw("OVER"),
self.format_window_spec_body(spec)
);
}
// `OVER window_name` references a window defined in the WINDOW clause.
// This bare form is distinct from `OVER (window_name)` in PostgreSQL,
// so preserve it without adding parentheses.
if let Some(name) = node.find_child("ColId") {
return format!("{} {}", self.kw("OVER"), self.format_expr(name));
}
format!(
"{} ({})",
self.kw("OVER"),
self.format_window_spec_body(node)
)
}

/// Format the inner contents (PARTITION BY / ORDER BY / frame) of a
/// window specification, without the surrounding parentheses.
pub(crate) fn format_window_spec_body(&self, spec: Node<'a>) -> String {
let mut inner = Vec::new();
let mut cursor = spec.walk();
for child in spec.named_children(&mut cursor) {
Expand All @@ -827,9 +846,7 @@ impl<'a> Formatter<'a> {
_ => inner.push(self.format_expr(child)),
}
}
parts.push(inner.join(" "));
parts.push(")".to_string());
parts.join("")
inner.join(" ")
}

fn format_partition_clause(&self, node: Node<'a>) -> String {
Expand Down
Loading