From aac003259cda5c32f9810bd9ca7ec7bf509ec1a9 Mon Sep 17 00:00:00 2001 From: psteinroe Date: Fri, 23 Oct 2026 13:52:12 +0000 Subject: [PATCH 1/7] fix(pretty_print): no stray spaces around commas and inside parentheses --- .../pgls_pretty_print/src/nodes/constraint.rs | 4 +- .../src/nodes/create_foreign_table_stmt.rs | 4 +- .../src/nodes/create_stmt.rs | 9 +- .../pgls_pretty_print/src/nodes/join_expr.rs | 33 ++-- .../pgls_pretty_print/src/nodes/json_table.rs | 8 +- .../pgls_pretty_print/src/nodes/node_list.rs | 9 +- .../src/nodes/publication_table.rs | 4 +- .../src/nodes/range_table_func.rs | 5 +- .../src/nodes/range_tbl_function.rs | 2 +- .../pgls_pretty_print/src/nodes/window_def.rs | 11 +- crates/pgls_pretty_print/src/renderer.rs | 35 +++-- crates/pgls_pretty_print/tests/common/mod.rs | 96 ++++++++++++ .../data/multi/leading_commas_short_lists.sql | 18 +++ .../tests/postgres_regress.rs | 12 +- ...ests__compact_select_list_leading_100.snap | 2 +- ...tests__compact_select_list_leading_80.snap | 2 +- .../snapshots/multi/tests__copy_100.snap | 2 +- .../tests/snapshots/multi/tests__copy_80.snap | 2 +- .../multi/tests__create_function_sql_100.snap | 4 +- .../multi/tests__create_function_sql_80.snap | 4 +- .../multi/tests__create_table_like_100.snap | 5 +- .../multi/tests__create_table_like_80.snap | 3 +- .../multi/tests__create_view_100.snap | 6 +- .../multi/tests__create_view_80.snap | 6 +- .../multi/tests__fast_default_100.snap | 2 +- .../multi/tests__fast_default_80.snap | 2 +- .../multi/tests__foreign_data_100.snap | 10 +- .../multi/tests__foreign_data_80.snap | 10 +- .../multi/tests__generated_stored_100.snap | 2 +- .../multi/tests__groupingsets_100.snap | 2 +- .../multi/tests__groupingsets_80.snap | 2 +- ...tests__leading_commas_short_lists_100.snap | 26 ++++ .../tests__leading_commas_short_lists_80.snap | 26 ++++ .../multi/tests__object_address_100.snap | 2 +- .../multi/tests__object_address_80.snap | 2 +- .../multi/tests__partition_join_100.snap | 36 ++--- .../multi/tests__partition_join_80.snap | 36 ++--- .../multi/tests__publication_100.snap | 89 ++++++----- .../multi/tests__publication_80.snap | 88 +++++------ .../tests__reserved_keyword_idents_100.snap | 2 +- .../tests__reserved_keyword_idents_80.snap | 2 +- .../multi/tests__sqljson_jsontable_100.snap | 144 ++++++++--------- .../multi/tests__sqljson_jsontable_80.snap | 118 +++++--------- .../snapshots/multi/tests__stats_ext_100.snap | 6 +- .../snapshots/multi/tests__stats_ext_80.snap | 4 +- .../multi/tests__typed_table_100.snap | 12 +- .../multi/tests__typed_table_80.snap | 2 +- .../snapshots/multi/tests__window_100.snap | 44 +++--- .../snapshots/multi/tests__window_80.snap | 41 +++-- .../tests/snapshots/multi/tests__xml_100.snap | 41 +---- .../tests/snapshots/multi/tests__xml_80.snap | 39 +---- .../single/tests__complex_select_0_100.snap | 3 +- .../single/tests__complex_select_0_80.snap | 3 +- .../tests__complex_select_part_7_100.snap | 3 +- .../tests__complex_select_part_7_80.snap | 3 +- ...ests__create_foreign_table_stmt_0_100.snap | 3 +- ...tests__create_foreign_table_stmt_0_80.snap | 3 +- .../single/tests__range_table_func_0_100.snap | 3 +- .../single/tests__range_table_func_0_80.snap | 2 - crates/pgls_pretty_print/tests/tests.rs | 4 + .../tests/whitespace_layout.rs | 145 ++++++++++++++++++ 61 files changed, 705 insertions(+), 543 deletions(-) create mode 100644 crates/pgls_pretty_print/tests/common/mod.rs create mode 100644 crates/pgls_pretty_print/tests/data/multi/leading_commas_short_lists.sql create mode 100644 crates/pgls_pretty_print/tests/snapshots/multi/tests__leading_commas_short_lists_100.snap create mode 100644 crates/pgls_pretty_print/tests/snapshots/multi/tests__leading_commas_short_lists_80.snap create mode 100644 crates/pgls_pretty_print/tests/whitespace_layout.rs diff --git a/crates/pgls_pretty_print/src/nodes/constraint.rs b/crates/pgls_pretty_print/src/nodes/constraint.rs index 1d39cba95..55b3aa6ca 100644 --- a/crates/pgls_pretty_print/src/nodes/constraint.rs +++ b/crates/pgls_pretty_print/src/nodes/constraint.rs @@ -312,9 +312,7 @@ pub(super) fn emit_constraint(e: &mut EventEmitter, n: &Constraint) { e.space(); e.token(TokenKind::WHERE_KW); e.space(); - e.token(TokenKind::L_PAREN); - super::emit_clause_condition(e, where_clause); - e.token(TokenKind::R_PAREN); + super::emit_parenthesized_condition(e, GroupKind::Constraint, where_clause); } emit_deferrable(e, n, &mut continued); diff --git a/crates/pgls_pretty_print/src/nodes/create_foreign_table_stmt.rs b/crates/pgls_pretty_print/src/nodes/create_foreign_table_stmt.rs index 6518f32d8..0548f663b 100644 --- a/crates/pgls_pretty_print/src/nodes/create_foreign_table_stmt.rs +++ b/crates/pgls_pretty_print/src/nodes/create_foreign_table_stmt.rs @@ -48,10 +48,10 @@ pub(super) fn emit_create_foreign_table_stmt(e: &mut EventEmitter, n: &CreateFor e.token(TokenKind::L_PAREN); if !base.table_elts.is_empty() { e.indent_start(); - e.line(crate::emitter::LineType::SoftOrSpace); + e.line(crate::emitter::LineType::Soft); emit_comma_separated_list(e, &base.table_elts, super::emit_node); e.indent_end(); - e.line(crate::emitter::LineType::SoftOrSpace); + e.line(crate::emitter::LineType::Soft); } e.token(TokenKind::R_PAREN); } diff --git a/crates/pgls_pretty_print/src/nodes/create_stmt.rs b/crates/pgls_pretty_print/src/nodes/create_stmt.rs index 2ffe49672..8d631844f 100644 --- a/crates/pgls_pretty_print/src/nodes/create_stmt.rs +++ b/crates/pgls_pretty_print/src/nodes/create_stmt.rs @@ -171,7 +171,7 @@ fn emit_create_stmt_impl(e: &mut EventEmitter, n: &CreateStmt, with_semicolon: b } e.indent_end(); - e.line(LineType::SoftOrSpace); + e.line(LineType::Soft); e.token(TokenKind::R_PAREN); } } else { @@ -182,10 +182,11 @@ fn emit_create_stmt_impl(e: &mut EventEmitter, n: &CreateStmt, with_semicolon: b e.space(); e.group_start(GroupKind::List); e.token(TokenKind::L_PAREN); - // Force break for tables with multiple columns for readability + // Force break for tables with multiple columns for readability. A table without columns + // keeps `()` together. if multiple_items { e.line(LineType::Hard); - } else { + } else if has_content { e.line(LineType::Soft); } @@ -223,7 +224,7 @@ fn emit_create_stmt_impl(e: &mut EventEmitter, n: &CreateStmt, with_semicolon: b if multiple_items { e.line(LineType::Hard); - } else { + } else if has_content { e.line(LineType::Soft); } e.token(TokenKind::R_PAREN); diff --git a/crates/pgls_pretty_print/src/nodes/join_expr.rs b/crates/pgls_pretty_print/src/nodes/join_expr.rs index d2d8c110b..7492ee28c 100644 --- a/crates/pgls_pretty_print/src/nodes/join_expr.rs +++ b/crates/pgls_pretty_print/src/nodes/join_expr.rs @@ -3,7 +3,7 @@ use pgls_query::protobuf::{JoinExpr, JoinType}; use crate::TokenKind; use crate::emitter::{EventEmitter, GroupKind, LineType}; -use super::node_list::{emit_comma_separated_list, emit_parenthesized_list}; +use super::node_list::emit_parenthesized_list; use super::string::emit_identifier_maybe_quoted; pub(super) fn emit_join_expr(e: &mut EventEmitter, n: &JoinExpr) { @@ -102,29 +102,14 @@ pub(super) fn emit_join_expr(e: &mut EventEmitter, n: &JoinExpr) { e.line(LineType::SoftOrSpace); e.token(TokenKind::USING_KW); e.space(); - e.token(TokenKind::L_PAREN); - if n.using_clause.len() > 1 { - e.indent_start(); - e.line(LineType::SoftOrSpace); - emit_comma_separated_list(e, &n.using_clause, |node, e| { - // For USING clause, String nodes should be identifiers - if let Some(pgls_query::NodeEnum::String(s)) = node.node.as_ref() { - emit_identifier_maybe_quoted(e, &s.sval); - } else { - super::emit_node(node, e); - } - }); - e.indent_end(); - } else { - emit_comma_separated_list(e, &n.using_clause, |node, e| { - if let Some(pgls_query::NodeEnum::String(s)) = node.node.as_ref() { - emit_identifier_maybe_quoted(e, &s.sval); - } else { - super::emit_node(node, e); - } - }); - } - e.token(TokenKind::R_PAREN); + emit_parenthesized_list(e, &n.using_clause, |node, e| { + // For USING clause, String nodes should be identifiers + if let Some(pgls_query::NodeEnum::String(s)) = node.node.as_ref() { + emit_identifier_maybe_quoted(e, &s.sval); + } else { + super::emit_node(node, e); + } + }); // Emit USING alias if present (PostgreSQL 14+) // Format: USING (col1, col2) AS alias diff --git a/crates/pgls_pretty_print/src/nodes/json_table.rs b/crates/pgls_pretty_print/src/nodes/json_table.rs index e39d11d8e..4a504c008 100644 --- a/crates/pgls_pretty_print/src/nodes/json_table.rs +++ b/crates/pgls_pretty_print/src/nodes/json_table.rs @@ -25,7 +25,7 @@ pub(super) fn emit_json_table(e: &mut EventEmitter, n: &JsonTable) { e.indent_start(); if let Some(context) = n.context_item.as_ref() { - e.line(LineType::SoftOrSpace); + e.line(LineType::Soft); super::emit_json_value_expr(e, context); e.token(TokenKind::COMMA); } @@ -60,7 +60,7 @@ pub(super) fn emit_json_table(e: &mut EventEmitter, n: &JsonTable) { } }); e.indent_end(); - e.line(LineType::SoftOrSpace); + e.line(LineType::Soft); e.token(TokenKind::R_PAREN); } @@ -70,7 +70,7 @@ pub(super) fn emit_json_table(e: &mut EventEmitter, n: &JsonTable) { } e.indent_end(); - e.line(LineType::SoftOrSpace); + e.line(LineType::Soft); e.token(TokenKind::R_PAREN); if let Some(alias) = n.alias.as_ref() { @@ -125,7 +125,7 @@ pub(super) fn emit_json_table_column(e: &mut EventEmitter, col: &JsonTableColumn } }); e.indent_end(); - e.line(LineType::SoftOrSpace); + e.line(LineType::Soft); e.token(TokenKind::R_PAREN); } diff --git a/crates/pgls_pretty_print/src/nodes/node_list.rs b/crates/pgls_pretty_print/src/nodes/node_list.rs index eab09a3c9..1587bdcb1 100644 --- a/crates/pgls_pretty_print/src/nodes/node_list.rs +++ b/crates/pgls_pretty_print/src/nodes/node_list.rs @@ -1,7 +1,7 @@ use pgls_query::Node; use crate::emitter::{EventEmitter, GroupKind, LineType}; -use crate::{CommaStyle, TokenKind}; +use crate::{CommaStyle, Layout, TokenKind}; /// Controls the spacing behavior after separators in list helpers #[derive(Clone, Copy, Default)] @@ -130,7 +130,12 @@ pub(super) fn emit_comma_separated_list_with_layout_break( e.take_own_line_leading_comments_at(location); } - super::emit_layout_break(e); + // As in `emit_comma_separated_list_with_spacing`, the break before a leading + // comma disappears when the list fits, so a single line reads "a, b". + match e.config().layout { + Layout::Expanded => e.line(LineType::Hard), + Layout::Fit => e.line(LineType::Soft), + } e.token(TokenKind::COMMA); e.space(); } else { diff --git a/crates/pgls_pretty_print/src/nodes/publication_table.rs b/crates/pgls_pretty_print/src/nodes/publication_table.rs index 2def8944a..b1c2b3fb0 100644 --- a/crates/pgls_pretty_print/src/nodes/publication_table.rs +++ b/crates/pgls_pretty_print/src/nodes/publication_table.rs @@ -20,9 +20,7 @@ pub(super) fn emit_publication_table(e: &mut EventEmitter, n: &PublicationTable) e.line(LineType::SoftOrSpace); e.token(TokenKind::WHERE_KW); e.space(); - e.token(TokenKind::L_PAREN); - super::emit_clause_condition(e, where_clause); - e.token(TokenKind::R_PAREN); + super::emit_parenthesized_condition(e, GroupKind::PublicationTable, where_clause); } e.group_end(); diff --git a/crates/pgls_pretty_print/src/nodes/range_table_func.rs b/crates/pgls_pretty_print/src/nodes/range_table_func.rs index 05baf1861..e597e6bd8 100644 --- a/crates/pgls_pretty_print/src/nodes/range_table_func.rs +++ b/crates/pgls_pretty_print/src/nodes/range_table_func.rs @@ -21,7 +21,6 @@ pub(super) fn emit_range_table_func(e: &mut EventEmitter, n: &RangeTableFunc) { // XMLNAMESPACES clause - emitted before row expression // Namespaces are stored as ResTarget nodes with name=prefix, val=URI if !n.namespaces.is_empty() { - e.line(LineType::SoftOrSpace); e.token(TokenKind::XMLNAMESPACES_KW); emit_parenthesized_list(e, &n.namespaces, |node, emitter| { // Each namespace is a ResTarget with val=URI, name=prefix @@ -54,7 +53,9 @@ pub(super) fn emit_range_table_func(e: &mut EventEmitter, n: &RangeTableFunc) { // Row expression (XPath for rows) // Wrap complex expressions in parentheses to keep them together if let Some(ref rowexpr) = n.rowexpr { - e.line(LineType::SoftOrSpace); + if !n.namespaces.is_empty() { + e.line(LineType::SoftOrSpace); + } // Check if rowexpr is a complex expression that needs wrapping let needs_parens = matches!( rowexpr.node.as_ref(), diff --git a/crates/pgls_pretty_print/src/nodes/range_tbl_function.rs b/crates/pgls_pretty_print/src/nodes/range_tbl_function.rs index 0d8f1912f..e441b6cb2 100644 --- a/crates/pgls_pretty_print/src/nodes/range_tbl_function.rs +++ b/crates/pgls_pretty_print/src/nodes/range_tbl_function.rs @@ -33,7 +33,7 @@ pub(super) fn emit_range_tbl_function(e: &mut EventEmitter, n: &RangeTblFunction } e.indent_end(); - e.line(LineType::SoftOrSpace); + e.line(LineType::Soft); e.token(TokenKind::R_PAREN); } diff --git a/crates/pgls_pretty_print/src/nodes/window_def.rs b/crates/pgls_pretty_print/src/nodes/window_def.rs index 42d494650..592e0337c 100644 --- a/crates/pgls_pretty_print/src/nodes/window_def.rs +++ b/crates/pgls_pretty_print/src/nodes/window_def.rs @@ -114,23 +114,27 @@ fn emit_window_spec(e: &mut EventEmitter, n: &WindowDef) { emit_comma_separated_list(e, &n.order_clause, |node, emitter| { super::emit_node(node, emitter) }); + first = false; } - emit_frame_clause(e, n); + emit_frame_clause(e, n, first); e.indent_end(); e.line(LineType::Soft); e.token(TokenKind::R_PAREN); } -fn emit_frame_clause(e: &mut EventEmitter, n: &WindowDef) -> bool { +/// Emits the frame clause, preceded by a break unless it is the first thing in the window spec. +fn emit_frame_clause(e: &mut EventEmitter, n: &WindowDef, first: bool) -> bool { let options = n.frame_options; if options & FRAMEOPTION_NONDEFAULT == 0 { return false; } - e.line(LineType::SoftOrSpace); + if !first { + e.line(LineType::SoftOrSpace); + } emit_frame_mode(e, options); e.space(); @@ -144,7 +148,6 @@ fn emit_frame_clause(e: &mut EventEmitter, n: &WindowDef) -> bool { e.line(LineType::SoftOrSpace); emit_frame_bound(e, options, n.end_offset.as_deref(), FrameBoundSide::End); } else { - e.line(LineType::SoftOrSpace); emit_frame_bound(e, options, n.start_offset.as_deref(), FrameBoundSide::Start); } diff --git a/crates/pgls_pretty_print/src/renderer.rs b/crates/pgls_pretty_print/src/renderer.rs index a6eb16ffa..c7c1dbd9c 100644 --- a/crates/pgls_pretty_print/src/renderer.rs +++ b/crates/pgls_pretty_print/src/renderer.rs @@ -51,7 +51,9 @@ pub struct Renderer { indent_level: usize, alignment_offsets: Vec, at_line_start: bool, - after_line_comment: bool, + /// A space written after the last text. It is only written out once more text follows on the + /// same line, so a line never ends in whitespace. + pending_space: bool, } impl Renderer { @@ -63,7 +65,7 @@ impl Renderer { indent_level: 0, alignment_offsets: Vec::new(), at_line_start: true, - after_line_comment: false, + pending_space: false, } } @@ -88,9 +90,8 @@ impl Renderer { self.handle_line(line_type)?; i += 1; } - LayoutEvent::Comment { text, line_comment } => { + LayoutEvent::Comment { text, .. } => { self.write_text(text)?; - self.after_line_comment = *line_comment; i += 1; } LayoutEvent::GroupStart { .. } => { @@ -131,7 +132,14 @@ impl Renderer { trailing_width: usize, ) -> Result<(), std::fmt::Error> { if let Some(single_line) = self.single_line_if_fits(group_events, trailing_width) { - self.write_text(&single_line)?; + // A space that ends the group waits for the next text, like any other space. + let text = single_line.trim_end_matches(' '); + if !text.is_empty() { + self.write_text(text)?; + } + if text.len() < single_line.len() { + self.write_space()?; + } return Ok(()); } @@ -227,9 +235,8 @@ impl Renderer { } i += 1; } - LayoutEvent::Comment { text, line_comment } => { + LayoutEvent::Comment { text, .. } => { self.write_text(text)?; - self.after_line_comment = *line_comment; i += 1; } LayoutEvent::GroupStart { .. } => { @@ -411,10 +418,11 @@ impl Renderer { } fn write_text(&mut self, text: &str) -> Result<(), std::fmt::Error> { - self.after_line_comment = false; if self.at_line_start { self.write_indentation()?; self.at_line_start = false; + } else if std::mem::take(&mut self.pending_space) { + write!(self.writer, " ")?; } write!(self.writer, "{text}")?; @@ -427,16 +435,21 @@ impl Renderer { Ok(()) } + /// Two spaces in a row, from a space next to a line that stayed flat for instance, are + /// written as one. fn write_space(&mut self) -> Result<(), std::fmt::Error> { - if !self.at_line_start { - write!(self.writer, " ")?; + if !self.at_line_start && !self.pending_space { + self.pending_space = true; self.current_line_length += 1; } Ok(()) } + /// Ends the current line. A line that has no text yet is not ended again: two breaks in a + /// row, around an empty list for instance, would otherwise print a blank line. fn write_line_break(&mut self) -> Result<(), std::fmt::Error> { - if self.at_line_start && self.after_line_comment { + self.pending_space = false; + if self.at_line_start { return Ok(()); } writeln!(self.writer)?; diff --git a/crates/pgls_pretty_print/tests/common/mod.rs b/crates/pgls_pretty_print/tests/common/mod.rs new file mode 100644 index 000000000..70331f765 --- /dev/null +++ b/crates/pgls_pretty_print/tests/common/mod.rs @@ -0,0 +1,96 @@ +use pgls_query::protobuf::Token; + +/// Checks the whitespace the formatter wrote between tokens: no space before a comma or `)`, none +/// after `(`, no double space between two tokens on a line, no trailing whitespace and no blank +/// line. Whitespace inside string literals, quoted identifiers and comments is left alone, and so +/// is the space that separates a comment from a parenthesis or comma. +pub fn whitespace_problems(output: &str) -> Vec { + let Ok(scan) = pgls_query::scan(output) else { + // The callers check that the output parses. + return Vec::new(); + }; + let tokens: Vec<_> = scan + .tokens + .iter() + .map(|token| { + ( + Token::try_from(token.token).ok(), + token.start as usize, + token.end as usize, + ) + }) + .collect(); + let line_at = |offset: usize| { + let number = output[..offset].matches('\n').count() + 1; + let start = output[..offset].rfind('\n').map_or(0, |i| i + 1); + let end = output[offset..] + .find('\n') + .map_or(output.len(), |i| offset + i); + format!("line {number} `{}`", &output[start..end]) + }; + let is_comment = + |token: Option| matches!(token, Some(Token::SqlComment | Token::CComment)); + let mut problems = Vec::new(); + + for pair in tokens.windows(2) { + let [(left, _, left_end), (right, right_start, _)] = pair else { + unreachable!() + }; + let gap = &output[*left_end..*right_start]; + if gap.is_empty() || gap.contains('\n') { + continue; + } + let problem = if gap.len() > 1 { + "double space" + } else if is_comment(*left) || is_comment(*right) { + continue; + } else if matches!(left, Some(Token::Ascii40)) { + "space after `(`" + } else if matches!(right, Some(Token::Ascii41)) { + "space before `)`" + } else if matches!(right, Some(Token::Ascii44)) { + "space before `,`" + } else { + continue; + }; + problems.push(format!("{problem} in {}", line_at(*left_end))); + } + + // Whitespace at the end of a line, or a whole line of it, unless it is inside a token such as + // a multi-line string. + let inside_token = |offset: usize| { + tokens + .iter() + .any(|(_, start, end)| *start <= offset && offset < *end) + }; + let lines: Vec<_> = output.split('\n').collect(); + let mut line_start = 0; + for (index, line) in lines.iter().enumerate() { + let trimmed = line.trim_end_matches([' ', '\t']); + if trimmed.len() < line.len() && !inside_token(line_start + trimmed.len()) { + problems.push(format!( + "trailing whitespace in line {} `{line}`", + index + 1 + )); + } else if line.is_empty() + && index > 0 + && index + 1 < lines.len() + && !inside_token(line_start - 1) + { + problems.push(format!("blank line at line {}", index + 1)); + } + line_start += line.len() + 1; + } + + problems +} + +#[allow(dead_code)] // Not every test crate asserts directly. +pub fn assert_clean_whitespace(output: &str) { + let problems = whitespace_problems(output); + assert!( + problems.is_empty(), + "stray whitespace in the output:\n{}\n\n{output}", + problems.join("\n") + ); +} diff --git a/crates/pgls_pretty_print/tests/data/multi/leading_commas_short_lists.sql b/crates/pgls_pretty_print/tests/data/multi/leading_commas_short_lists.sql new file mode 100644 index 000000000..310291bb1 --- /dev/null +++ b/crates/pgls_pretty_print/tests/data/multi/leading_commas_short_lists.sql @@ -0,0 +1,18 @@ +-- pgls-format: commaStyle=leading +select a, b from t; + +select a, b, c from t order by a, b; + +insert into t (a, b) values (1, 2); + +insert into t (a, b) values (1, 2), (3, 4); + +select count(*), max(a) from t group by b, c; + +select * from a inner join b using (x, y); + +update t set a = 1, b = 2 where c = 3; + +create index on t (a, b); + +select first_column_with_a_long_name, second_column_with_a_long_name, third_column_with_a_long_name from t; diff --git a/crates/pgls_pretty_print/tests/postgres_regress.rs b/crates/pgls_pretty_print/tests/postgres_regress.rs index 83edc806a..095ae9687 100644 --- a/crates/pgls_pretty_print/tests/postgres_regress.rs +++ b/crates/pgls_pretty_print/tests/postgres_regress.rs @@ -1,5 +1,8 @@ //! Formats every statement Postgres accepted in its own regression suite and checks that the output -//! parses back to the same AST, and that formatting the output again changes nothing. +//! parses back to the same AST, that formatting the output again changes nothing, and that it has +//! no stray whitespace. + +mod common; use std::panic::{AssertUnwindSafe, catch_unwind}; @@ -77,6 +80,13 @@ fn round_trip(sql: &str) -> Result<(), String> { .map_err(|e| format!("output doesn't parse at width {max_line_length}: {e}\n{output}"))? .into_root() .ok_or("output has no statement")?; + let whitespace = common::whitespace_problems(&output); + if !whitespace.is_empty() { + return Err(format!( + "stray whitespace at width {max_line_length}: {}\n{output}", + whitespace.join(", ") + )); + } let mut formatted = reparsed.clone(); normalize_ast(&mut formatted); if formatted != expected { diff --git a/crates/pgls_pretty_print/tests/snapshots/multi/tests__compact_select_list_leading_100.snap b/crates/pgls_pretty_print/tests/snapshots/multi/tests__compact_select_list_leading_100.snap index 7f7f9e286..278afe35f 100644 --- a/crates/pgls_pretty_print/tests/snapshots/multi/tests__compact_select_list_leading_100.snap +++ b/crates/pgls_pretty_print/tests/snapshots/multi/tests__compact_select_list_leading_100.snap @@ -19,7 +19,7 @@ from orders order by customer_id , created_at desc; -select id , name from users; +select id, name from users; select id into temporary table active_user_ids from users where active; diff --git a/crates/pgls_pretty_print/tests/snapshots/multi/tests__compact_select_list_leading_80.snap b/crates/pgls_pretty_print/tests/snapshots/multi/tests__compact_select_list_leading_80.snap index 509c74cea..43f922d77 100644 --- a/crates/pgls_pretty_print/tests/snapshots/multi/tests__compact_select_list_leading_80.snap +++ b/crates/pgls_pretty_print/tests/snapshots/multi/tests__compact_select_list_leading_80.snap @@ -22,7 +22,7 @@ from orders order by customer_id , created_at desc; -select id , name from users; +select id, name from users; select id into temporary table active_user_ids from users where active; diff --git a/crates/pgls_pretty_print/tests/snapshots/multi/tests__copy_100.snap b/crates/pgls_pretty_print/tests/snapshots/multi/tests__copy_100.snap index 4006c75ab..0cbb200ca 100644 --- a/crates/pgls_pretty_print/tests/snapshots/multi/tests__copy_100.snap +++ b/crates/pgls_pretty_print/tests/snapshots/multi/tests__copy_100.snap @@ -310,7 +310,7 @@ create foreign data wrapper copytest_wrapper; create server copytest_server foreign data wrapper copytest_wrapper; -create foreign table copytest_foreign_table ( a int ) server copytest_server; +create foreign table copytest_foreign_table (a int) server copytest_server; copy copytest_foreign_table from stdout with (freeze); diff --git a/crates/pgls_pretty_print/tests/snapshots/multi/tests__copy_80.snap b/crates/pgls_pretty_print/tests/snapshots/multi/tests__copy_80.snap index cbdd77dcb..5a72f0a8d 100644 --- a/crates/pgls_pretty_print/tests/snapshots/multi/tests__copy_80.snap +++ b/crates/pgls_pretty_print/tests/snapshots/multi/tests__copy_80.snap @@ -313,7 +313,7 @@ create foreign data wrapper copytest_wrapper; create server copytest_server foreign data wrapper copytest_wrapper; -create foreign table copytest_foreign_table ( a int ) server copytest_server; +create foreign table copytest_foreign_table (a int) server copytest_server; copy copytest_foreign_table from stdout with (freeze); diff --git a/crates/pgls_pretty_print/tests/snapshots/multi/tests__create_function_sql_100.snap b/crates/pgls_pretty_print/tests/snapshots/multi/tests__create_function_sql_100.snap index 89c89edfc..04c40bd29 100644 --- a/crates/pgls_pretty_print/tests/snapshots/multi/tests__create_function_sql_100.snap +++ b/crates/pgls_pretty_print/tests/snapshots/multi/tests__create_function_sql_100.snap @@ -392,9 +392,7 @@ from information_schema.parameters inner join information_schema.routines - using ( - specific_schema, - specific_name) + using (specific_schema, specific_name) where routine_schema = 'temp_func_test' and routine_name ~ '^functest_is_' order by 1, diff --git a/crates/pgls_pretty_print/tests/snapshots/multi/tests__create_function_sql_80.snap b/crates/pgls_pretty_print/tests/snapshots/multi/tests__create_function_sql_80.snap index 1bf574f82..7e0316b1d 100644 --- a/crates/pgls_pretty_print/tests/snapshots/multi/tests__create_function_sql_80.snap +++ b/crates/pgls_pretty_print/tests/snapshots/multi/tests__create_function_sql_80.snap @@ -392,9 +392,7 @@ from information_schema.parameters inner join information_schema.routines - using ( - specific_schema, - specific_name) + using (specific_schema, specific_name) where routine_schema = 'temp_func_test' and routine_name ~ '^functest_is_' order by 1, diff --git a/crates/pgls_pretty_print/tests/snapshots/multi/tests__create_table_like_100.snap b/crates/pgls_pretty_print/tests/snapshots/multi/tests__create_table_like_100.snap index 51fde4e72..9891b93b5 100644 --- a/crates/pgls_pretty_print/tests/snapshots/multi/tests__create_table_like_100.snap +++ b/crates/pgls_pretty_print/tests/snapshots/multi/tests__create_table_like_100.snap @@ -1,7 +1,6 @@ --- source: crates/pgls_pretty_print/tests/tests.rs input_file: crates/pgls_pretty_print/tests/data/multi/create_table_like.sql -snapshot_kind: text --- create table inhx (xx text default 'text'); @@ -475,7 +474,7 @@ alter table ctl_table alter table ctl_table alter column b set storage main; -create foreign table ctl_foreign_table1 ( like ctl_table ) server ctl_s0; +create foreign table ctl_foreign_table1 (like ctl_table) server ctl_s0; select attname, @@ -486,7 +485,7 @@ where attrelid = cast('ctl_foreign_table1' as regclass) and attnum > 0 order by attnum; -create foreign table ctl_foreign_table2 ( like ctl_table including all ) server ctl_s0; +create foreign table ctl_foreign_table2 (like ctl_table including all) server ctl_s0; select attname, diff --git a/crates/pgls_pretty_print/tests/snapshots/multi/tests__create_table_like_80.snap b/crates/pgls_pretty_print/tests/snapshots/multi/tests__create_table_like_80.snap index e6aaa8d71..8406b7a37 100644 --- a/crates/pgls_pretty_print/tests/snapshots/multi/tests__create_table_like_80.snap +++ b/crates/pgls_pretty_print/tests/snapshots/multi/tests__create_table_like_80.snap @@ -1,7 +1,6 @@ --- source: crates/pgls_pretty_print/tests/tests.rs input_file: crates/pgls_pretty_print/tests/data/multi/create_table_like.sql -snapshot_kind: text --- create table inhx (xx text default 'text'); @@ -485,7 +484,7 @@ alter table ctl_table alter table ctl_table alter column b set storage main; -create foreign table ctl_foreign_table1 ( like ctl_table ) server ctl_s0; +create foreign table ctl_foreign_table1 (like ctl_table) server ctl_s0; select attname, diff --git a/crates/pgls_pretty_print/tests/snapshots/multi/tests__create_view_100.snap b/crates/pgls_pretty_print/tests/snapshots/multi/tests__create_view_100.snap index 814101fe4..e464fc98f 100644 --- a/crates/pgls_pretty_print/tests/snapshots/multi/tests__create_view_100.snap +++ b/crates/pgls_pretty_print/tests/snapshots/multi/tests__create_view_100.snap @@ -579,13 +579,13 @@ create view v1a as select * from (tt2 natural join tt3) as j; create view v2 -as select * from tt2 inner join tt3 using ( b, c) inner join tt4 using (b); +as select * from tt2 inner join tt3 using (b, c) inner join tt4 using (b); create view v2a -as select * from (tt2 inner join tt3 using ( b, c) inner join tt4 using (b)) as j; +as select * from (tt2 inner join tt3 using (b, c) inner join tt4 using (b)) as j; create view v3 -as select * from tt2 inner join tt3 using ( b, c) full outer join tt4 using (b); +as select * from tt2 inner join tt3 using (b, c) full outer join tt4 using (b); select pg_get_viewdef('v1', true); diff --git a/crates/pgls_pretty_print/tests/snapshots/multi/tests__create_view_80.snap b/crates/pgls_pretty_print/tests/snapshots/multi/tests__create_view_80.snap index c7944c7b3..a72770f8e 100644 --- a/crates/pgls_pretty_print/tests/snapshots/multi/tests__create_view_80.snap +++ b/crates/pgls_pretty_print/tests/snapshots/multi/tests__create_view_80.snap @@ -622,16 +622,16 @@ create view v1a as select * from (tt2 natural join tt3) as j; create view v2 -as select * from tt2 inner join tt3 using ( b, c) inner join tt4 using (b); +as select * from tt2 inner join tt3 using (b, c) inner join tt4 using (b); create view v2a as select * from - (tt2 inner join tt3 using ( b, c) inner join tt4 using (b)) as j; + (tt2 inner join tt3 using (b, c) inner join tt4 using (b)) as j; create view v3 -as select * from tt2 inner join tt3 using ( b, c) full outer join tt4 using (b); +as select * from tt2 inner join tt3 using (b, c) full outer join tt4 using (b); select pg_get_viewdef('v1', true); diff --git a/crates/pgls_pretty_print/tests/snapshots/multi/tests__fast_default_100.snap b/crates/pgls_pretty_print/tests/snapshots/multi/tests__fast_default_100.snap index 40e854d4f..e8b9aa4fd 100644 --- a/crates/pgls_pretty_print/tests/snapshots/multi/tests__fast_default_100.snap +++ b/crates/pgls_pretty_print/tests/snapshots/multi/tests__fast_default_100.snap @@ -798,7 +798,7 @@ create foreign data wrapper dummy; create server s0 foreign data wrapper dummy; -create foreign table ft1 ( c1 int not null ) server s0; +create foreign table ft1 (c1 int not null) server s0; alter foreign table ft1 add column c8 int default 0; diff --git a/crates/pgls_pretty_print/tests/snapshots/multi/tests__fast_default_80.snap b/crates/pgls_pretty_print/tests/snapshots/multi/tests__fast_default_80.snap index 592c27539..b8b0b4b26 100644 --- a/crates/pgls_pretty_print/tests/snapshots/multi/tests__fast_default_80.snap +++ b/crates/pgls_pretty_print/tests/snapshots/multi/tests__fast_default_80.snap @@ -848,7 +848,7 @@ create foreign data wrapper dummy; create server s0 foreign data wrapper dummy; -create foreign table ft1 ( c1 int not null ) server s0; +create foreign table ft1 (c1 int not null) server s0; alter foreign table ft1 add column c8 int default 0; diff --git a/crates/pgls_pretty_print/tests/snapshots/multi/tests__foreign_data_100.snap b/crates/pgls_pretty_print/tests/snapshots/multi/tests__foreign_data_100.snap index 4a48273e8..556f1e821 100644 --- a/crates/pgls_pretty_print/tests/snapshots/multi/tests__foreign_data_100.snap +++ b/crates/pgls_pretty_print/tests/snapshots/multi/tests__foreign_data_100.snap @@ -484,7 +484,7 @@ create index on lt1 using btree (a); create foreign table ft_part1 partition of lt1 for values from (0) to (1000) server s0; -create foreign table ft_part2 ( a int ) server s0; +create foreign table ft_part2 (a int) server s0; alter table lt1 attach partition @@ -499,7 +499,7 @@ alter table lt1 create foreign table ft_part1 partition of lt1 for values from (0) to (1000) server s0; -create foreign table ft_part2 ( a int not null ) server s0; +create foreign table ft_part2 (a int not null) server s0; alter table lt1 attach partition @@ -518,7 +518,7 @@ create table lt1_part1 partition of lt1 for values from (0) to (1000) partition create foreign table ft_part_1_1 partition of lt1_part1 for values from (0) to (100) server s0; -create foreign table ft_part_1_2 ( a int ) server s0; +create foreign table ft_part_1_2 (a int) server s0; alter table lt1_part1 attach partition @@ -538,7 +538,7 @@ alter table lt1 create foreign table ft_part_1_1 partition of lt1_part1 for values from (0) to (100) server s0; -create foreign table ft_part_1_2 ( a int not null ) server s0; +create foreign table ft_part_1_2 (a int not null) server s0; alter table lt1_part1 attach partition @@ -1343,7 +1343,7 @@ partition by list(a); create foreign table foreign_part partition of temp_parted default server s0; -create foreign table foreign_part ( a int ) server s0; +create foreign table foreign_part (a int) server s0; alter table temp_parted attach partition diff --git a/crates/pgls_pretty_print/tests/snapshots/multi/tests__foreign_data_80.snap b/crates/pgls_pretty_print/tests/snapshots/multi/tests__foreign_data_80.snap index 8f41f4d25..cb8ad9657 100644 --- a/crates/pgls_pretty_print/tests/snapshots/multi/tests__foreign_data_80.snap +++ b/crates/pgls_pretty_print/tests/snapshots/multi/tests__foreign_data_80.snap @@ -505,7 +505,7 @@ create foreign table ft_part1 partition of lt1 for values from (0) to (1000) server s0; -create foreign table ft_part2 ( a int ) server s0; +create foreign table ft_part2 (a int) server s0; alter table lt1 attach partition @@ -522,7 +522,7 @@ create foreign table ft_part1 partition of lt1 for values from (0) to (1000) server s0; -create foreign table ft_part2 ( a int not null ) server s0; +create foreign table ft_part2 (a int not null) server s0; alter table lt1 attach partition @@ -546,7 +546,7 @@ create foreign table ft_part_1_1 partition of lt1_part1 for values from (0) to (100) server s0; -create foreign table ft_part_1_2 ( a int ) server s0; +create foreign table ft_part_1_2 (a int) server s0; alter table lt1_part1 attach partition @@ -568,7 +568,7 @@ create foreign table ft_part_1_1 partition of lt1_part1 for values from (0) to (100) server s0; -create foreign table ft_part_1_2 ( a int not null ) server s0; +create foreign table ft_part_1_2 (a int not null) server s0; alter table lt1_part1 attach partition @@ -1395,7 +1395,7 @@ partition by list(a); create foreign table foreign_part partition of temp_parted default server s0; -create foreign table foreign_part ( a int ) server s0; +create foreign table foreign_part (a int) server s0; alter table temp_parted attach partition diff --git a/crates/pgls_pretty_print/tests/snapshots/multi/tests__generated_stored_100.snap b/crates/pgls_pretty_print/tests/snapshots/multi/tests__generated_stored_100.snap index 37f37b713..7d1fd68df 100644 --- a/crates/pgls_pretty_print/tests/snapshots/multi/tests__generated_stored_100.snap +++ b/crates/pgls_pretty_print/tests/snapshots/multi/tests__generated_stored_100.snap @@ -776,7 +776,7 @@ insert into gtest24nn (a) values (null); create type gtest_type as (f1 int, f2 text, f3 bigint); -create table gtest28 of gtest_type (f1 with options generated always as (f2 * 2) stored ); +create table gtest28 of gtest_type (f1 with options generated always as (f2 * 2) stored); drop type gtest_type cascade; diff --git a/crates/pgls_pretty_print/tests/snapshots/multi/tests__groupingsets_100.snap b/crates/pgls_pretty_print/tests/snapshots/multi/tests__groupingsets_100.snap index 584f30ef2..b37cee34f 100644 --- a/crates/pgls_pretty_print/tests/snapshots/multi/tests__groupingsets_100.snap +++ b/crates/pgls_pretty_print/tests/snapshots/multi/tests__groupingsets_100.snap @@ -253,7 +253,7 @@ select sum(t1.v), max(t2.c) from - gstest1 as t1 inner join gstest2 as t2 using ( a, b) + gstest1 as t1 inner join gstest2 as t2 using (a, b) group by grouping sets ((a, b), ()); select a, d, grouping(a, b, c) from gstest3 group by grouping sets ((a, b), (a, c)); diff --git a/crates/pgls_pretty_print/tests/snapshots/multi/tests__groupingsets_80.snap b/crates/pgls_pretty_print/tests/snapshots/multi/tests__groupingsets_80.snap index aaa801af7..c2e485219 100644 --- a/crates/pgls_pretty_print/tests/snapshots/multi/tests__groupingsets_80.snap +++ b/crates/pgls_pretty_print/tests/snapshots/multi/tests__groupingsets_80.snap @@ -302,7 +302,7 @@ select sum(t1.v), max(t2.c) from - gstest1 as t1 inner join gstest2 as t2 using ( a, b) + gstest1 as t1 inner join gstest2 as t2 using (a, b) group by grouping sets ((a, b), ()); select diff --git a/crates/pgls_pretty_print/tests/snapshots/multi/tests__leading_commas_short_lists_100.snap b/crates/pgls_pretty_print/tests/snapshots/multi/tests__leading_commas_short_lists_100.snap new file mode 100644 index 000000000..bc550d200 --- /dev/null +++ b/crates/pgls_pretty_print/tests/snapshots/multi/tests__leading_commas_short_lists_100.snap @@ -0,0 +1,26 @@ +--- +source: crates/pgls_pretty_print/tests/tests.rs +input_file: crates/pgls_pretty_print/tests/data/multi/leading_commas_short_lists.sql +--- +select a, b from t; + +select a, b, c from t order by a, b; + +insert into t (a, b) values (1, 2); + +insert into t (a, b) values (1, 2), (3, 4); + +select count(*), max(a) from t group by b, c; + +select * from a inner join b using (x, y); + +update t set a = 1, b = 2 where c = 3; + +create index on t using btree (a, b); + +select + first_column_with_a_long_name + , second_column_with_a_long_name + , third_column_with_a_long_name +from + t; diff --git a/crates/pgls_pretty_print/tests/snapshots/multi/tests__leading_commas_short_lists_80.snap b/crates/pgls_pretty_print/tests/snapshots/multi/tests__leading_commas_short_lists_80.snap new file mode 100644 index 000000000..bc550d200 --- /dev/null +++ b/crates/pgls_pretty_print/tests/snapshots/multi/tests__leading_commas_short_lists_80.snap @@ -0,0 +1,26 @@ +--- +source: crates/pgls_pretty_print/tests/tests.rs +input_file: crates/pgls_pretty_print/tests/data/multi/leading_commas_short_lists.sql +--- +select a, b from t; + +select a, b, c from t order by a, b; + +insert into t (a, b) values (1, 2); + +insert into t (a, b) values (1, 2), (3, 4); + +select count(*), max(a) from t group by b, c; + +select * from a inner join b using (x, y); + +update t set a = 1, b = 2 where c = 3; + +create index on t using btree (a, b); + +select + first_column_with_a_long_name + , second_column_with_a_long_name + , third_column_with_a_long_name +from + t; diff --git a/crates/pgls_pretty_print/tests/snapshots/multi/tests__object_address_100.snap b/crates/pgls_pretty_print/tests/snapshots/multi/tests__object_address_100.snap index d1c46d1ad..c62eec731 100644 --- a/crates/pgls_pretty_print/tests/snapshots/multi/tests__object_address_100.snap +++ b/crates/pgls_pretty_print/tests/snapshots/multi/tests__object_address_100.snap @@ -48,7 +48,7 @@ create type addr_nsp.gencomptype as (a int); create type addr_nsp.genenum as enum ('one', 'two'); -create foreign table addr_nsp.genftable ( a int ) server addr_fserv; +create foreign table addr_nsp.genftable (a int) server addr_fserv; create aggregate addr_nsp.genaggr (int4) (sfunc = int4pl, stype = int4); diff --git a/crates/pgls_pretty_print/tests/snapshots/multi/tests__object_address_80.snap b/crates/pgls_pretty_print/tests/snapshots/multi/tests__object_address_80.snap index db7155aa8..6dbcae256 100644 --- a/crates/pgls_pretty_print/tests/snapshots/multi/tests__object_address_80.snap +++ b/crates/pgls_pretty_print/tests/snapshots/multi/tests__object_address_80.snap @@ -48,7 +48,7 @@ create type addr_nsp.gencomptype as (a int); create type addr_nsp.genenum as enum ('one', 'two'); -create foreign table addr_nsp.genftable ( a int ) server addr_fserv; +create foreign table addr_nsp.genftable (a int) server addr_fserv; create aggregate addr_nsp.genaggr (int4) (sfunc = int4pl, stype = int4); diff --git a/crates/pgls_pretty_print/tests/snapshots/multi/tests__partition_join_100.snap b/crates/pgls_pretty_print/tests/snapshots/multi/tests__partition_join_100.snap index 1c3890eb2..cb3858194 100644 --- a/crates/pgls_pretty_print/tests/snapshots/multi/tests__partition_join_100.snap +++ b/crates/pgls_pretty_print/tests/snapshots/multi/tests__partition_join_100.snap @@ -438,7 +438,7 @@ select a, b from - prt1 full outer join prt2 as p2 (b, a, c) using ( a, b) + prt1 full outer join prt2 as p2 (b, a, c) using (a, b) where a between 490 and 510 group by 1, @@ -450,7 +450,7 @@ select a, b from - prt1 full outer join prt2 as p2 (b, a, c) using ( a, b) + prt1 full outer join prt2 as p2 (b, a, c) using (a, b) where a between 490 and 510 group by 1, @@ -642,58 +642,46 @@ order by t1.a, select count(*) from - prt1 full outer join prt2 as p2 (b, a, c) using ( a, b) + prt1 full outer join prt2 as p2 (b, a, c) using (a, b) full outer join prt2 as p3 (b, a, c) - using ( - a, - b) + using (a, b) where a between 490 and 510; select count(*) from - prt1 full outer join prt2 as p2 (b, a, c) using ( a, b) + prt1 full outer join prt2 as p2 (b, a, c) using (a, b) full outer join prt2 as p3 (b, a, c) - using ( - a, - b) + using (a, b) where a between 490 and 510; select count(*) from - prt1 full outer join prt2 as p2 (b, a, c) using ( a, b) + prt1 full outer join prt2 as p2 (b, a, c) using (a, b) full outer join prt2 as p3 (b, a, c) - using ( - a, - b) + using (a, b) full outer join prt1 as p4 (a, b, c) - using ( - a, - b) + using (a, b) where a between 490 and 510; select count(*) from - prt1 full outer join prt2 as p2 (b, a, c) using ( a, b) + prt1 full outer join prt2 as p2 (b, a, c) using (a, b) full outer join prt2 as p3 (b, a, c) - using ( - a, - b) + using (a, b) full outer join prt1 as p4 (a, b, c) - using ( - a, - b) + using (a, b) where a between 490 and 510; diff --git a/crates/pgls_pretty_print/tests/snapshots/multi/tests__partition_join_80.snap b/crates/pgls_pretty_print/tests/snapshots/multi/tests__partition_join_80.snap index 34928e10a..8cc130670 100644 --- a/crates/pgls_pretty_print/tests/snapshots/multi/tests__partition_join_80.snap +++ b/crates/pgls_pretty_print/tests/snapshots/multi/tests__partition_join_80.snap @@ -473,7 +473,7 @@ select a, b from - prt1 full outer join prt2 as p2 (b, a, c) using ( a, b) + prt1 full outer join prt2 as p2 (b, a, c) using (a, b) where a between 490 and 510 group by 1, @@ -485,7 +485,7 @@ select a, b from - prt1 full outer join prt2 as p2 (b, a, c) using ( a, b) + prt1 full outer join prt2 as p2 (b, a, c) using (a, b) where a between 490 and 510 group by 1, @@ -677,58 +677,46 @@ order by t1.a, select count(*) from - prt1 full outer join prt2 as p2 (b, a, c) using ( a, b) + prt1 full outer join prt2 as p2 (b, a, c) using (a, b) full outer join prt2 as p3 (b, a, c) - using ( - a, - b) + using (a, b) where a between 490 and 510; select count(*) from - prt1 full outer join prt2 as p2 (b, a, c) using ( a, b) + prt1 full outer join prt2 as p2 (b, a, c) using (a, b) full outer join prt2 as p3 (b, a, c) - using ( - a, - b) + using (a, b) where a between 490 and 510; select count(*) from - prt1 full outer join prt2 as p2 (b, a, c) using ( a, b) + prt1 full outer join prt2 as p2 (b, a, c) using (a, b) full outer join prt2 as p3 (b, a, c) - using ( - a, - b) + using (a, b) full outer join prt1 as p4 (a, b, c) - using ( - a, - b) + using (a, b) where a between 490 and 510; select count(*) from - prt1 full outer join prt2 as p2 (b, a, c) using ( a, b) + prt1 full outer join prt2 as p2 (b, a, c) using (a, b) full outer join prt2 as p3 (b, a, c) - using ( - a, - b) + using (a, b) full outer join prt1 as p4 (a, b, c) - using ( - a, - b) + using (a, b) where a between 490 and 510; diff --git a/crates/pgls_pretty_print/tests/snapshots/multi/tests__publication_100.snap b/crates/pgls_pretty_print/tests/snapshots/multi/tests__publication_100.snap index edf4b451a..8989d3044 100644 --- a/crates/pgls_pretty_print/tests/snapshots/multi/tests__publication_100.snap +++ b/crates/pgls_pretty_print/tests/snapshots/multi/tests__publication_100.snap @@ -221,11 +221,11 @@ with (publish = 'insert'); reset client_min_messages; -alter publication testpub5 add table testpub_rf_tbl3 where ( e > 1000 and e < 2000); +alter publication testpub5 add table testpub_rf_tbl3 where (e > 1000 and e < 2000); alter publication testpub5 drop table testpub_rf_tbl2; -alter publication testpub5 set table testpub_rf_tbl3 where ( e > 300 and e < 500); +alter publication testpub5 set table testpub_rf_tbl3 where (e > 300 and e < 500); set client_min_messages = 'ERROR'; @@ -277,9 +277,9 @@ with (publish = 'insert'); reset client_min_messages; -alter publication testpub5 set table testpub_rf_tbl3 where ( 1234); +alter publication testpub5 set table testpub_rf_tbl3 where (1234); -alter publication testpub5 set table testpub_rf_tbl3 where ( e < avg(e)); +alter publication testpub5 set table testpub_rf_tbl3 where (e < avg(e)); create function testpub_rf_func1(int, int) returns boolean @@ -296,27 +296,27 @@ language plpgsql immutable as $$ BEGIN RETURN 123; END; $$; -alter publication testpub5 add table testpub_rf_tbl1 where ( a >= testpub_rf_func2()); +alter publication testpub5 add table testpub_rf_tbl1 where (a >= testpub_rf_func2()); -alter publication testpub5 add table testpub_rf_tbl1 where ( a < random()); +alter publication testpub5 add table testpub_rf_tbl1 where (a < random()); create collation user_collation from "C"; -alter publication testpub5 add table testpub_rf_tbl1 where ( b < '2' collate "user_collation"); +alter publication testpub5 add table testpub_rf_tbl1 where (b < '2' collate "user_collation"); -alter publication testpub5 set table testpub_rf_tbl1 where ( nullif(1, 2) = a); +alter publication testpub5 set table testpub_rf_tbl1 where (nullif(1, 2) = a); -alter publication testpub5 set table testpub_rf_tbl1 where ( a is null); +alter publication testpub5 set table testpub_rf_tbl1 where (a is null); -alter publication testpub5 set table testpub_rf_tbl1 where ( a > 5 is false); +alter publication testpub5 set table testpub_rf_tbl1 where (a > 5 is false); -alter publication testpub5 set table testpub_rf_tbl1 where ( a is distinct from 5); +alter publication testpub5 set table testpub_rf_tbl1 where (a is distinct from 5); -alter publication testpub5 set table testpub_rf_tbl1 where ( (a, a + 1) < (2, 3)); +alter publication testpub5 set table testpub_rf_tbl1 where ((a, a + 1) < (2, 3)); -alter publication testpub5 set table testpub_rf_tbl1 where ( cast(b as varchar) < '2'); +alter publication testpub5 set table testpub_rf_tbl1 where (cast(b as varchar) < '2'); -alter publication testpub5 set table testpub_rf_tbl4 where ( length(g) < 6); +alter publication testpub5 set table testpub_rf_tbl4 where (length(g) < 6); create type rf_bug_status as enum ('new', 'open', 'closed'); @@ -336,27 +336,26 @@ create publication testpub6 for table testpub_rf_tbl1 where (a in (select genera create publication testpub6 for table testpub_rf_tbl1 where (cast('(0,1)' as tid) = ctid); -alter publication testpub5 set table testpub_rf_tbl5 where ( a is document); +alter publication testpub5 set table testpub_rf_tbl5 where (a is document); alter publication testpub5 set -table testpub_rf_tbl5 where ( xmlexists('//foo[text() = ''bar'']' passing a)); +table testpub_rf_tbl5 where (xmlexists('//foo[text() = ''bar'']' passing a)); -alter publication testpub5 set table testpub_rf_tbl1 where ( nullif(1, 2) = a); +alter publication testpub5 set table testpub_rf_tbl1 where (nullif(1, 2) = a); -alter publication testpub5 set -table testpub_rf_tbl1 where ( case a when 5 then true else false end); +alter publication testpub5 set table testpub_rf_tbl1 where (case a when 5 then true else false end); -alter publication testpub5 set table testpub_rf_tbl1 where ( coalesce(b, 'foo') = 'foo'); +alter publication testpub5 set table testpub_rf_tbl1 where (coalesce(b, 'foo') = 'foo'); -alter publication testpub5 set table testpub_rf_tbl1 where ( greatest(a, 10) > 10); +alter publication testpub5 set table testpub_rf_tbl1 where (greatest(a, 10) > 10); -alter publication testpub5 set table testpub_rf_tbl1 where ( a in (2, 4, 6)); +alter publication testpub5 set table testpub_rf_tbl1 where (a in (2, 4, 6)); -alter publication testpub5 set table testpub_rf_tbl1 where ( array[a] <@ array[2, 4, 6]); +alter publication testpub5 set table testpub_rf_tbl1 where (array[a] <@ array[2, 4, 6]); -alter publication testpub5 set table testpub_rf_tbl1 where ( row(a, 2) is null); +alter publication testpub5 set table testpub_rf_tbl1 where (row(a, 2) is null); -alter publication testpub5 drop table testpub_rf_tbl1 where ( e < 27); +alter publication testpub5 drop table testpub_rf_tbl1 where (e < 27); set client_min_messages = 'ERROR'; @@ -364,7 +363,7 @@ create publication testpub6 for tables in schema testpub_rf_schema2; alter publication testpub6 set tables in schema testpub_rf_schema2, -table testpub_rf_schema2.testpub_rf_tbl6 where ( i < 99); +table testpub_rf_schema2.testpub_rf_tbl6 where (i < 99); reset client_min_messages; @@ -452,19 +451,19 @@ reset client_min_messages; update rf_tbl_abcd_pk set a = 1; -alter publication testpub6 set table rf_tbl_abcd_pk where ( b > 99); +alter publication testpub6 set table rf_tbl_abcd_pk where (b > 99); update rf_tbl_abcd_pk set a = 1; -alter publication testpub6 set table rf_tbl_abcd_pk where ( c > 99); +alter publication testpub6 set table rf_tbl_abcd_pk where (c > 99); update rf_tbl_abcd_pk set a = 1; -alter publication testpub6 set table rf_tbl_abcd_pk where ( d > 99); +alter publication testpub6 set table rf_tbl_abcd_pk where (d > 99); update rf_tbl_abcd_pk set a = 1; -alter publication testpub6 set table rf_tbl_abcd_nopk where ( a > 99); +alter publication testpub6 set table rf_tbl_abcd_nopk where (a > 99); update rf_tbl_abcd_nopk set a = 1; @@ -474,11 +473,11 @@ alter table rf_tbl_abcd_pk alter table rf_tbl_abcd_nopk replica identity full; -alter publication testpub6 set table rf_tbl_abcd_pk where ( c > 99); +alter publication testpub6 set table rf_tbl_abcd_pk where (c > 99); update rf_tbl_abcd_pk set a = 1; -alter publication testpub6 set table rf_tbl_abcd_nopk where ( a > 99); +alter publication testpub6 set table rf_tbl_abcd_nopk where (a > 99); update rf_tbl_abcd_nopk set a = 1; @@ -488,15 +487,15 @@ alter table rf_tbl_abcd_pk alter table rf_tbl_abcd_nopk replica identity nothing; -alter publication testpub6 set table rf_tbl_abcd_pk where ( a > 99); +alter publication testpub6 set table rf_tbl_abcd_pk where (a > 99); update rf_tbl_abcd_pk set a = 1; -alter publication testpub6 set table rf_tbl_abcd_pk where ( c > 99); +alter publication testpub6 set table rf_tbl_abcd_pk where (c > 99); update rf_tbl_abcd_pk set a = 1; -alter publication testpub6 set table rf_tbl_abcd_nopk where ( a > 99); +alter publication testpub6 set table rf_tbl_abcd_nopk where (a > 99); update rf_tbl_abcd_nopk set a = 1; @@ -516,33 +515,33 @@ create unique index idx_abcd_nopk_c on rf_tbl_abcd_nopk using btree (c); alter table rf_tbl_abcd_nopk replica identity using index idx_abcd_nopk_c; -alter publication testpub6 set table rf_tbl_abcd_pk where ( a > 99); +alter publication testpub6 set table rf_tbl_abcd_pk where (a > 99); update rf_tbl_abcd_pk set a = 1; -alter publication testpub6 set table rf_tbl_abcd_pk where ( c > 99); +alter publication testpub6 set table rf_tbl_abcd_pk where (c > 99); update rf_tbl_abcd_pk set a = 1; -alter publication testpub6 set table rf_tbl_abcd_nopk where ( a > 99); +alter publication testpub6 set table rf_tbl_abcd_nopk where (a > 99); update rf_tbl_abcd_nopk set a = 1; -alter publication testpub6 set table rf_tbl_abcd_nopk where ( c > 99); +alter publication testpub6 set table rf_tbl_abcd_nopk where (c > 99); update rf_tbl_abcd_nopk set a = 1; alter publication testpub6 set (publish_via_partition_root = 0); -alter publication testpub6 set table rf_tbl_abcd_part_pk where ( a > 99); +alter publication testpub6 set table rf_tbl_abcd_part_pk where (a > 99); -alter publication testpub6 set table rf_tbl_abcd_part_pk_1 where ( a > 99); +alter publication testpub6 set table rf_tbl_abcd_part_pk_1 where (a > 99); update rf_tbl_abcd_part_pk set a = 1; alter publication testpub6 set (publish_via_partition_root = 1); -alter publication testpub6 set table rf_tbl_abcd_part_pk where ( a > 99); +alter publication testpub6 set table rf_tbl_abcd_part_pk where (a > 99); update rf_tbl_abcd_part_pk set a = 1; @@ -552,7 +551,7 @@ alter publication testpub6 set table rf_tbl_abcd_part_pk; alter publication testpub6 set (publish_via_partition_root = 0); -alter publication testpub6 set table rf_tbl_abcd_part_pk_1 where ( b > 99); +alter publication testpub6 set table rf_tbl_abcd_part_pk_1 where (b > 99); alter publication testpub6 set (publish_via_partition_root = 0); @@ -560,7 +559,7 @@ update rf_tbl_abcd_part_pk set a = 1; alter publication testpub6 set (publish_via_partition_root = 1); -alter publication testpub6 set table rf_tbl_abcd_part_pk where ( b > 99); +alter publication testpub6 set table rf_tbl_abcd_part_pk where (b > 99); update rf_tbl_abcd_part_pk set a = 1; @@ -907,7 +906,7 @@ create table testpub_tbl_both_filters ( alter table testpub_tbl_both_filters replica identity using index testpub_tbl_both_filters_pkey; -alter publication testpub_both_filters add table testpub_tbl_both_filters (a, c) where ( c <> 1); +alter publication testpub_both_filters add table testpub_tbl_both_filters (a, c) where (c <> 1); drop table testpub_tbl_both_filters; diff --git a/crates/pgls_pretty_print/tests/snapshots/multi/tests__publication_80.snap b/crates/pgls_pretty_print/tests/snapshots/multi/tests__publication_80.snap index b4eeefa47..4fc73a321 100644 --- a/crates/pgls_pretty_print/tests/snapshots/multi/tests__publication_80.snap +++ b/crates/pgls_pretty_print/tests/snapshots/multi/tests__publication_80.snap @@ -233,12 +233,12 @@ with (publish = 'insert'); reset client_min_messages; alter publication testpub5 add -table testpub_rf_tbl3 where ( e > 1000 and e < 2000); +table testpub_rf_tbl3 where (e > 1000 and e < 2000); alter publication testpub5 drop table testpub_rf_tbl2; alter publication testpub5 set -table testpub_rf_tbl3 where ( e > 300 and e < 500); +table testpub_rf_tbl3 where (e > 300 and e < 500); set client_min_messages = 'ERROR'; @@ -292,9 +292,9 @@ with (publish = 'insert'); reset client_min_messages; -alter publication testpub5 set table testpub_rf_tbl3 where ( 1234); +alter publication testpub5 set table testpub_rf_tbl3 where (1234); -alter publication testpub5 set table testpub_rf_tbl3 where ( e < avg(e)); +alter publication testpub5 set table testpub_rf_tbl3 where (e < avg(e)); create function testpub_rf_func1(int, int) returns boolean @@ -316,31 +316,31 @@ immutable as $$ BEGIN RETURN 123; END; $$; alter publication testpub5 add -table testpub_rf_tbl1 where ( a >= testpub_rf_func2()); +table testpub_rf_tbl1 where (a >= testpub_rf_func2()); -alter publication testpub5 add table testpub_rf_tbl1 where ( a < random()); +alter publication testpub5 add table testpub_rf_tbl1 where (a < random()); create collation user_collation from "C"; alter publication testpub5 add -table testpub_rf_tbl1 where ( b < '2' collate "user_collation"); +table testpub_rf_tbl1 where (b < '2' collate "user_collation"); -alter publication testpub5 set table testpub_rf_tbl1 where ( nullif(1, 2) = a); +alter publication testpub5 set table testpub_rf_tbl1 where (nullif(1, 2) = a); -alter publication testpub5 set table testpub_rf_tbl1 where ( a is null); +alter publication testpub5 set table testpub_rf_tbl1 where (a is null); -alter publication testpub5 set table testpub_rf_tbl1 where ( a > 5 is false); +alter publication testpub5 set table testpub_rf_tbl1 where (a > 5 is false); alter publication testpub5 set -table testpub_rf_tbl1 where ( a is distinct from 5); +table testpub_rf_tbl1 where (a is distinct from 5); alter publication testpub5 set -table testpub_rf_tbl1 where ( (a, a + 1) < (2, 3)); +table testpub_rf_tbl1 where ((a, a + 1) < (2, 3)); alter publication testpub5 set -table testpub_rf_tbl1 where ( cast(b as varchar) < '2'); +table testpub_rf_tbl1 where (cast(b as varchar) < '2'); -alter publication testpub5 set table testpub_rf_tbl4 where ( length(g) < 6); +alter publication testpub5 set table testpub_rf_tbl4 where (length(g) < 6); create type rf_bug_status as enum ('new', 'open', 'closed'); @@ -364,30 +364,30 @@ for table testpub_rf_tbl1 where (a in (select generate_series(1, 5))); create publication testpub6 for table testpub_rf_tbl1 where (cast('(0,1)' as tid) = ctid); -alter publication testpub5 set table testpub_rf_tbl5 where ( a is document); +alter publication testpub5 set table testpub_rf_tbl5 where (a is document); alter publication testpub5 set -table testpub_rf_tbl5 where ( xmlexists('//foo[text() = ''bar'']' passing a)); +table testpub_rf_tbl5 where (xmlexists('//foo[text() = ''bar'']' passing a)); -alter publication testpub5 set table testpub_rf_tbl1 where ( nullif(1, 2) = a); +alter publication testpub5 set table testpub_rf_tbl1 where (nullif(1, 2) = a); alter publication testpub5 set -table testpub_rf_tbl1 where ( case a when 5 then true else false end); +table testpub_rf_tbl1 where (case a when 5 then true else false end); alter publication testpub5 set -table testpub_rf_tbl1 where ( coalesce(b, 'foo') = 'foo'); +table testpub_rf_tbl1 where (coalesce(b, 'foo') = 'foo'); alter publication testpub5 set -table testpub_rf_tbl1 where ( greatest(a, 10) > 10); +table testpub_rf_tbl1 where (greatest(a, 10) > 10); -alter publication testpub5 set table testpub_rf_tbl1 where ( a in (2, 4, 6)); +alter publication testpub5 set table testpub_rf_tbl1 where (a in (2, 4, 6)); alter publication testpub5 set -table testpub_rf_tbl1 where ( array[a] <@ array[2, 4, 6]); +table testpub_rf_tbl1 where (array[a] <@ array[2, 4, 6]); -alter publication testpub5 set table testpub_rf_tbl1 where ( row(a, 2) is null); +alter publication testpub5 set table testpub_rf_tbl1 where (row(a, 2) is null); -alter publication testpub5 drop table testpub_rf_tbl1 where ( e < 27); +alter publication testpub5 drop table testpub_rf_tbl1 where (e < 27); set client_min_messages = 'ERROR'; @@ -395,7 +395,7 @@ create publication testpub6 for tables in schema testpub_rf_schema2; alter publication testpub6 set tables in schema testpub_rf_schema2, -table testpub_rf_schema2.testpub_rf_tbl6 where ( i < 99); +table testpub_rf_schema2.testpub_rf_tbl6 where (i < 99); reset client_min_messages; @@ -483,19 +483,19 @@ reset client_min_messages; update rf_tbl_abcd_pk set a = 1; -alter publication testpub6 set table rf_tbl_abcd_pk where ( b > 99); +alter publication testpub6 set table rf_tbl_abcd_pk where (b > 99); update rf_tbl_abcd_pk set a = 1; -alter publication testpub6 set table rf_tbl_abcd_pk where ( c > 99); +alter publication testpub6 set table rf_tbl_abcd_pk where (c > 99); update rf_tbl_abcd_pk set a = 1; -alter publication testpub6 set table rf_tbl_abcd_pk where ( d > 99); +alter publication testpub6 set table rf_tbl_abcd_pk where (d > 99); update rf_tbl_abcd_pk set a = 1; -alter publication testpub6 set table rf_tbl_abcd_nopk where ( a > 99); +alter publication testpub6 set table rf_tbl_abcd_nopk where (a > 99); update rf_tbl_abcd_nopk set a = 1; @@ -505,11 +505,11 @@ alter table rf_tbl_abcd_pk alter table rf_tbl_abcd_nopk replica identity full; -alter publication testpub6 set table rf_tbl_abcd_pk where ( c > 99); +alter publication testpub6 set table rf_tbl_abcd_pk where (c > 99); update rf_tbl_abcd_pk set a = 1; -alter publication testpub6 set table rf_tbl_abcd_nopk where ( a > 99); +alter publication testpub6 set table rf_tbl_abcd_nopk where (a > 99); update rf_tbl_abcd_nopk set a = 1; @@ -519,15 +519,15 @@ alter table rf_tbl_abcd_pk alter table rf_tbl_abcd_nopk replica identity nothing; -alter publication testpub6 set table rf_tbl_abcd_pk where ( a > 99); +alter publication testpub6 set table rf_tbl_abcd_pk where (a > 99); update rf_tbl_abcd_pk set a = 1; -alter publication testpub6 set table rf_tbl_abcd_pk where ( c > 99); +alter publication testpub6 set table rf_tbl_abcd_pk where (c > 99); update rf_tbl_abcd_pk set a = 1; -alter publication testpub6 set table rf_tbl_abcd_nopk where ( a > 99); +alter publication testpub6 set table rf_tbl_abcd_nopk where (a > 99); update rf_tbl_abcd_nopk set a = 1; @@ -547,33 +547,33 @@ create unique index idx_abcd_nopk_c on rf_tbl_abcd_nopk using btree (c); alter table rf_tbl_abcd_nopk replica identity using index idx_abcd_nopk_c; -alter publication testpub6 set table rf_tbl_abcd_pk where ( a > 99); +alter publication testpub6 set table rf_tbl_abcd_pk where (a > 99); update rf_tbl_abcd_pk set a = 1; -alter publication testpub6 set table rf_tbl_abcd_pk where ( c > 99); +alter publication testpub6 set table rf_tbl_abcd_pk where (c > 99); update rf_tbl_abcd_pk set a = 1; -alter publication testpub6 set table rf_tbl_abcd_nopk where ( a > 99); +alter publication testpub6 set table rf_tbl_abcd_nopk where (a > 99); update rf_tbl_abcd_nopk set a = 1; -alter publication testpub6 set table rf_tbl_abcd_nopk where ( c > 99); +alter publication testpub6 set table rf_tbl_abcd_nopk where (c > 99); update rf_tbl_abcd_nopk set a = 1; alter publication testpub6 set (publish_via_partition_root = 0); -alter publication testpub6 set table rf_tbl_abcd_part_pk where ( a > 99); +alter publication testpub6 set table rf_tbl_abcd_part_pk where (a > 99); -alter publication testpub6 set table rf_tbl_abcd_part_pk_1 where ( a > 99); +alter publication testpub6 set table rf_tbl_abcd_part_pk_1 where (a > 99); update rf_tbl_abcd_part_pk set a = 1; alter publication testpub6 set (publish_via_partition_root = 1); -alter publication testpub6 set table rf_tbl_abcd_part_pk where ( a > 99); +alter publication testpub6 set table rf_tbl_abcd_part_pk where (a > 99); update rf_tbl_abcd_part_pk set a = 1; @@ -583,7 +583,7 @@ alter publication testpub6 set table rf_tbl_abcd_part_pk; alter publication testpub6 set (publish_via_partition_root = 0); -alter publication testpub6 set table rf_tbl_abcd_part_pk_1 where ( b > 99); +alter publication testpub6 set table rf_tbl_abcd_part_pk_1 where (b > 99); alter publication testpub6 set (publish_via_partition_root = 0); @@ -591,7 +591,7 @@ update rf_tbl_abcd_part_pk set a = 1; alter publication testpub6 set (publish_via_partition_root = 1); -alter publication testpub6 set table rf_tbl_abcd_part_pk where ( b > 99); +alter publication testpub6 set table rf_tbl_abcd_part_pk where (b > 99); update rf_tbl_abcd_part_pk set a = 1; @@ -958,7 +958,7 @@ alter table testpub_tbl_both_filters replica identity using index testpub_tbl_both_filters_pkey; alter publication testpub_both_filters add -table testpub_tbl_both_filters (a, c) where ( c <> 1); +table testpub_tbl_both_filters (a, c) where (c <> 1); drop table testpub_tbl_both_filters; diff --git a/crates/pgls_pretty_print/tests/snapshots/multi/tests__reserved_keyword_idents_100.snap b/crates/pgls_pretty_print/tests/snapshots/multi/tests__reserved_keyword_idents_100.snap index 3698be4be..12e5081cc 100644 --- a/crates/pgls_pretty_print/tests/snapshots/multi/tests__reserved_keyword_idents_100.snap +++ b/crates/pgls_pretty_print/tests/snapshots/multi/tests__reserved_keyword_idents_100.snap @@ -74,7 +74,7 @@ create user mapping for u server "primary"; drop user mapping for u server "primary"; -create foreign table ft1 ( x int ) server "primary"; +create foreign table ft1 (x int) server "primary"; import foreign schema "primary" from server s into "select"; diff --git a/crates/pgls_pretty_print/tests/snapshots/multi/tests__reserved_keyword_idents_80.snap b/crates/pgls_pretty_print/tests/snapshots/multi/tests__reserved_keyword_idents_80.snap index d7999b2b4..2c026d2dd 100644 --- a/crates/pgls_pretty_print/tests/snapshots/multi/tests__reserved_keyword_idents_80.snap +++ b/crates/pgls_pretty_print/tests/snapshots/multi/tests__reserved_keyword_idents_80.snap @@ -76,7 +76,7 @@ create user mapping for u server "primary"; drop user mapping for u server "primary"; -create foreign table ft1 ( x int ) server "primary"; +create foreign table ft1 (x int) server "primary"; import foreign schema "primary" from server s into "select"; diff --git a/crates/pgls_pretty_print/tests/snapshots/multi/tests__sqljson_jsontable_100.snap b/crates/pgls_pretty_print/tests/snapshots/multi/tests__sqljson_jsontable_100.snap index 2ed6b6175..0b6a6c778 100644 --- a/crates/pgls_pretty_print/tests/snapshots/multi/tests__sqljson_jsontable_100.snap +++ b/crates/pgls_pretty_print/tests/snapshots/multi/tests__sqljson_jsontable_100.snap @@ -1,24 +1,23 @@ --- source: crates/pgls_pretty_print/tests/tests.rs input_file: crates/pgls_pretty_print/tests/data/multi/sqljson_jsontable.sql -snapshot_kind: text --- -select * from json_table( '[]', 'strict $.a' columns (js2 int path '$' ) default 1 on error ); +select * from json_table('[]', 'strict $.a' columns (js2 int path '$') default 1 on error); -select * from json_table( '[]', 'strict $.a' columns (js2 int path '$' ) null on error ); +select * from json_table('[]', 'strict $.a' columns (js2 int path '$') null on error); -select * from json_table( '[]', 'strict $.a' columns (js2 int path '$' ) empty array on error ); +select * from json_table('[]', 'strict $.a' columns (js2 int path '$') empty array on error); -select * from json_table( '[]', 'strict $.a' columns (js2 int path '$' ) error on error ); +select * from json_table('[]', 'strict $.a' columns (js2 int path '$') error on error); -select * from json_table( cast('"1.23"' as jsonb), '$.a' as js2 columns (js2 int path '$' ) ); +select * from json_table(cast('"1.23"' as jsonb), '$.a' as js2 columns (js2 int path '$')); -select * from json_table( cast(null as jsonb), '$' columns (v1 timestamp ) ) as f (v1, v2); +select * from json_table(cast(null as jsonb), '$' columns (v1 timestamp)) as f (v1, v2); select * from - json_table( cast('"1.23"' as jsonb), '$.a' columns (js2 int path '$', js2 int path '$' ) ); + json_table(cast('"1.23"' as jsonb), '$.a' columns (js2 int path '$', js2 int path '$')); create type comp as (a int, b int); @@ -36,11 +35,11 @@ from drop type comp; -select * from json_table( cast(null as jsonb), '$' columns (foo int ) ) as bar; +select * from json_table(cast(null as jsonb), '$' columns (foo int)) as bar; -select * from json_table( cast('"1.23"' as jsonb), 'strict $.a' columns (js2 int path '$' ) ); +select * from json_table(cast('"1.23"' as jsonb), 'strict $.a' columns (js2 int path '$')); -select * from json_table( cast('123' as jsonb), '$' columns (item int path '$', foo int ) ) as bar; +select * from json_table(cast('123' as jsonb), '$' columns (item int path '$', foo int)) as bar; create domain jsonb_test_domain as text check (value <> 'foo'); @@ -358,14 +357,14 @@ select * from (values ('1'), ('"err"')) as vals (js), - json_table( cast(vals.js as jsonb), '$' columns (a int path '$' ) ) as jt; + json_table(cast(vals.js as jsonb), '$' columns (a int path '$')) as jt; select * from (values ('1'), ('"err"')) as vals (js) left outer join - json_table( cast(vals.js as jsonb), '$' columns (a int path '$' error on error ) ) as jt + json_table(cast(vals.js as jsonb), '$' columns (a int path '$' error on error)) as jt on true; select @@ -373,13 +372,10 @@ select from (values ('1'), ('"err"')) as vals (js) left outer join - json_table( cast(vals.js as jsonb), '$' columns (a int path '$' error on error ) ) as jt + json_table(cast(vals.js as jsonb), '$' columns (a int path '$' error on error)) as jt on true; -select - * -from - json_table( cast('1' as jsonb), '$' columns (a int path '$.a' error on empty ) ) as jt; +select * from json_table(cast('1' as jsonb), '$' columns (a int path '$.a' error on empty)) as jt; select * @@ -441,34 +437,31 @@ from select * from - json_table( cast('"a"' as jsonb), '$' columns (a int4 exists path '$.a' error on error ) ); + json_table(cast('"a"' as jsonb), '$' columns (a int4 exists path '$.a' error on error)); -select - * -from - json_table( cast('"a"' as jsonb), '$' columns (a int4 exists path '$' error on error ) ); +select * from json_table(cast('"a"' as jsonb), '$' columns (a int4 exists path '$' error on error)); -select * from json_table( cast('"a"' as jsonb), '$' columns (a int2 exists path '$.a' ) ); +select * from json_table(cast('"a"' as jsonb), '$' columns (a int2 exists path '$.a')); -select * from json_table( cast('"a"' as jsonb), '$' columns (a int8 exists path '$.a' ) ); +select * from json_table(cast('"a"' as jsonb), '$' columns (a int8 exists path '$.a')); -select * from json_table( cast('"a"' as jsonb), '$' columns (a float4 exists path '$.a' ) ); +select * from json_table(cast('"a"' as jsonb), '$' columns (a float4 exists path '$.a')); -select * from json_table( cast('"a"' as jsonb), '$' columns (a char(3) exists path '$.a' ) ); +select * from json_table(cast('"a"' as jsonb), '$' columns (a char(3) exists path '$.a')); select * from - json_table( cast('"a"' as jsonb), '$' columns (a char(3) exists path '$.a' error on error ) ); + json_table(cast('"a"' as jsonb), '$' columns (a char(3) exists path '$.a' error on error)); select * from - json_table( cast('"a"' as jsonb), '$' columns (a char(5) exists path '$.a' error on error ) ); + json_table(cast('"a"' as jsonb), '$' columns (a char(5) exists path '$.a' error on error)); -select * from json_table( cast('"a"' as jsonb), '$' columns (a json exists path '$.a' ) ); +select * from json_table(cast('"a"' as jsonb), '$' columns (a json exists path '$.a')); -select * from json_table( cast('"a"' as jsonb), '$' columns (a jsonb exists path '$.a' ) ); +select * from json_table(cast('"a"' as jsonb), '$' columns (a jsonb exists path '$.a')); create domain dint4 as int; @@ -478,47 +471,47 @@ select a, cast(a as bool) from - json_table( cast('"a"' as jsonb), '$' columns (a dint4 exists path '$.a' ) ); + json_table(cast('"a"' as jsonb), '$' columns (a dint4 exists path '$.a')); select a, cast(a as bool) from - json_table( cast('{"a":1}' as jsonb), '$' columns (a dint4_0 exists path '$.b' ) ); + json_table(cast('{"a":1}' as jsonb), '$' columns (a dint4_0 exists path '$.b')); select a, cast(a as bool) from - json_table( cast('{"a":1}' as jsonb), '$' columns (a dint4_0 exists path '$.b' error on error ) ); + json_table(cast('{"a":1}' as jsonb), '$' columns (a dint4_0 exists path '$.b' error on error)); select a, cast(a as bool) from - json_table( cast('{"a":1}' as jsonb), '$' columns (a dint4_0 exists path '$.b' false on error ) ); + json_table(cast('{"a":1}' as jsonb), '$' columns (a dint4_0 exists path '$.b' false on error)); select a, cast(a as bool) from - json_table( cast('{"a":1}' as jsonb), '$' columns (a dint4_0 exists path '$.b' true on error ) ); + json_table(cast('{"a":1}' as jsonb), '$' columns (a dint4_0 exists path '$.b' true on error)); drop domain dint4, dint4_0; -select * from json_table( cast('"world"' as jsonb), '$' columns (item text path '$' keep quotes ) ); +select * from json_table(cast('"world"' as jsonb), '$' columns (item text path '$' keep quotes)); -select * from json_table( cast('"world"' as jsonb), '$' columns (item text path '$' omit quotes ) ); +select * from json_table(cast('"world"' as jsonb), '$' columns (item text path '$' omit quotes)); select * from - json_table( cast('"world"' as jsonb), '$' columns (item text format json path '$' keep quotes ) ); + json_table(cast('"world"' as jsonb), '$' columns (item text format json path '$' keep quotes)); select * from - json_table( cast('"world"' as jsonb), '$' columns (item text format json path '$' omit quotes ) ); + json_table(cast('"world"' as jsonb), '$' columns (item text format json path '$' omit quotes)); select * @@ -619,7 +612,7 @@ from '$' as a columns ( b int, - nested path '$' as a columns (c int ) + nested path '$' as a columns (c int) ) ) as jt; @@ -631,21 +624,14 @@ from '$' as a columns ( b int, - nested path '$' as n_a columns (c int ) + nested path '$' as n_a columns (c int) ) ) as jt; select * from - json_table( - cast('[]' as jsonb), - '$' - columns ( - b int, - nested path '$' as b columns (c int ) - ) - ) as jt; + json_table(cast('[]' as jsonb), '$' columns (b int, nested path '$' as b columns (c int))) as jt; select * @@ -654,8 +640,8 @@ from cast('[]' as jsonb), '$' columns ( - nested path '$' as a columns (b int ), - nested path '$' columns (nested path '$' as a columns (c int ) ) + nested path '$' as a columns (b int), + nested path '$' columns (nested path '$' as a columns (c int)) ) ) as jt; @@ -678,8 +664,8 @@ from columns ( n for ordinality, a int path 'lax $.a' default -1 on empty, - nested path 'strict $.b[*]' as pb columns (b_id for ordinality, b int path '$' ), - nested path 'strict $.c[*]' as pc columns (c_id for ordinality, c int path '$' ) + nested path 'strict $.b[*]' as pb columns (b_id for ordinality, b int path '$'), + nested path 'strict $.c[*]' as pc columns (c_id for ordinality, c int path '$') ) ) as jt; @@ -695,7 +681,7 @@ from y as y columns ( y text format json path '$', - nested path 'strict $[*] ? (@ == $y)' columns (z int path '$' ) + nested path 'strict $[*] ? (@ == $y)' columns (z int path '$') ) ) as jt; @@ -713,13 +699,13 @@ from nested path '$[1]' as p1 columns ( a1 int, - nested path '$[*]' as "p1 1" columns (a11 text ), + nested path '$[*]' as "p1 1" columns (a11 text), b1 text ), nested path '$[2]' as p2 columns ( - nested path '$[*]' as "p2:1" columns (a21 text ), - nested path '$[*]' as p22 columns (a22 text ) + nested path '$[*]' as "p2:1" columns (a21 text), + nested path '$[*]' as p22 columns (a22 text) ) ) ); @@ -746,7 +732,7 @@ from xx int path '$.c', nested path '$.a.za[1]' columns ( - nested path '$.z21[*]' columns (z21 int path '$?(@ >= $"x")' error on error ) + nested path '$.z21[*]' columns (z21 int path '$?(@ >= $"x")' error on error) ) ) ) as sub; @@ -763,11 +749,11 @@ from passing x as x, y as y columns ( - nested path '$.a.za[2]' columns (nested path '$.z22[*]' as z22 columns (c int path '$' ) ), - nested path '$.a.za[1]' columns (d int[] path '$.z21' ), - nested path '$.a.za[0]' columns (nested path '$.z1[*]' as z1 columns (a int path '$' ) ), + nested path '$.a.za[2]' columns (nested path '$.z22[*]' as z22 columns (c int path '$')), + nested path '$.a.za[1]' columns (d int[] path '$.z21'), + nested path '$.a.za[0]' columns (nested path '$.z1[*]' as z1 columns (a int path '$')), xx1 int path '$.c', - nested path '$.a.za[1]' columns (nested path '$.z21[*]' as z21 columns (b int path '$' ) ), + nested path '$.a.za[1]' columns (nested path '$.z21[*]' as z21 columns (b int path '$')), xx int path '$.c' ) ) as sub; @@ -787,11 +773,11 @@ from xx1 int path '$.c', nested path '$.a.za[0].z1[*]' columns ( - nested path '$ ?(@ >= ($"x" -2))' columns (a int path '$' ) + nested path '$ ?(@ >= ($"x" -2))' columns (a int path '$') ), nested path '$.a.za[0]' columns ( - nested path '$.z1[*] ? (@ >= ($"x" -2))' columns (b int path '$' ) + nested path '$.z1[*] ? (@ >= ($"x" -2))' columns (b int path '$') ) ) ) as sub; @@ -809,7 +795,7 @@ from y as y columns ( xx1 int path '$.c', - nested path '$.a.za[1]' columns (nested path '$.z21[*]' columns (b int path '$' ) ), + nested path '$.a.za[1]' columns (nested path '$.z21[*]' columns (b int path '$')), nested path '$.a.za[1] ? (@.z21[*] >= ($"x"-1))' columns ( nested path '$.z21[*] ? (@ >= ($"y" + 3))' as z22 @@ -840,11 +826,11 @@ from y as y columns ( xx1 int path '$.c', - nested path '$.a.za[2]' columns (nested path '$.z22[*]' as z22 columns (c int path '$' ) ), - nested path '$.a.za[1]' columns (d json path '$ ? (@.z21[*] == ($"x" -1))' ), + nested path '$.a.za[2]' columns (nested path '$.z22[*]' as z22 columns (c int path '$')), + nested path '$.a.za[1]' columns (d json path '$ ? (@.z21[*] == ($"x" -1))'), nested path '$.a.za[0]' columns ( - nested path '$.z1[*] ? (@ >= ($"x" -2))' as z1 columns (a int path '$' ) + nested path '$.z1[*] ? (@ >= ($"x" -2))' as z1 columns (a int path '$') ), nested path '$.a.za[1]' columns ( @@ -872,7 +858,7 @@ from xx1 int path '$.c', nested path '$.a.za[2]' columns ( - nested path '$.z22[*]' as z22 columns (c int path '$' without wrapper omit quotes ) + nested path '$.z22[*]' as z22 columns (c int path '$' without wrapper omit quotes) ), nested path '$.a.za[1]' columns ( @@ -880,7 +866,7 @@ from ), nested path '$.a.za[0]' columns ( - nested path '$.z1[*] ? (@ >= ($"x" -2))' as z1 columns (a int path '$' keep quotes ) + nested path '$.z1[*] ? (@ >= ($"x" -2))' as z1 columns (a int path '$' keep quotes) ), nested path '$.a.za[1]' columns ( @@ -896,26 +882,26 @@ drop view jsonb_table_view7; drop table s; -select * from json_table( cast('1' as jsonb), '$' columns (a int ) null on error ); +select * from json_table(cast('1' as jsonb), '$' columns (a int) null on error); -select * from json_table( cast('1' as jsonb), '$' columns (a int true on empty ) ); +select * from json_table(cast('1' as jsonb), '$' columns (a int true on empty)); -select * from json_table( cast('1' as jsonb), '$' columns (a int omit quotes true on error ) ); +select * from json_table(cast('1' as jsonb), '$' columns (a int omit quotes true on error)); -select * from json_table( cast('1' as jsonb), '$' columns (a int exists empty object on error ) ); +select * from json_table(cast('1' as jsonb), '$' columns (a int exists empty object on error)); create view json_table_view8 -as select * from json_table( '"a"', '$' columns (a text path '$' ) ); +as select * from json_table('"a"', '$' columns (a text path '$')); create view json_table_view9 -as select * from json_table( '"a"', '$' columns (a text path '$' ) error on error ); +as select * from json_table('"a"', '$' columns (a text path '$') error on error); drop view json_table_view8, json_table_view9; create view json_table_view8 -as select * from json_table( '"a"', '$' columns (a text path '$' ) empty array on error ); +as select * from json_table('"a"', '$' columns (a text path '$') empty array on error); create view json_table_view9 -as select * from json_table( '"a"', '$' columns (a text path '$' ) empty array on error ); +as select * from json_table('"a"', '$' columns (a text path '$') empty array on error); drop view json_table_view8, json_table_view9; diff --git a/crates/pgls_pretty_print/tests/snapshots/multi/tests__sqljson_jsontable_80.snap b/crates/pgls_pretty_print/tests/snapshots/multi/tests__sqljson_jsontable_80.snap index f0b4ca731..c72bbdc7c 100644 --- a/crates/pgls_pretty_print/tests/snapshots/multi/tests__sqljson_jsontable_80.snap +++ b/crates/pgls_pretty_print/tests/snapshots/multi/tests__sqljson_jsontable_80.snap @@ -1,24 +1,16 @@ --- source: crates/pgls_pretty_print/tests/tests.rs input_file: crates/pgls_pretty_print/tests/data/multi/sqljson_jsontable.sql -snapshot_kind: text --- select * from - json_table( - '[]', - 'strict $.a' - columns ( - js2 int path '$' - ) - default 1 on error - ); + json_table('[]', 'strict $.a' columns (js2 int path '$') default 1 on error); select * from - json_table( '[]', 'strict $.a' columns (js2 int path '$' ) null on error ); + json_table('[]', 'strict $.a' columns (js2 int path '$') null on error); select * @@ -35,23 +27,17 @@ from select * from - json_table( '[]', 'strict $.a' columns (js2 int path '$' ) error on error ); + json_table('[]', 'strict $.a' columns (js2 int path '$') error on error); select * from - json_table( - cast('"1.23"' as jsonb), - '$.a' as js2 - columns ( - js2 int path '$' - ) - ); + json_table(cast('"1.23"' as jsonb), '$.a' as js2 columns (js2 int path '$')); select * from - json_table( cast(null as jsonb), '$' columns (v1 timestamp ) ) as f (v1, v2); + json_table(cast(null as jsonb), '$' columns (v1 timestamp)) as f (v1, v2); select * @@ -81,18 +67,12 @@ from drop type comp; -select * from json_table( cast(null as jsonb), '$' columns (foo int ) ) as bar; +select * from json_table(cast(null as jsonb), '$' columns (foo int)) as bar; select * from - json_table( - cast('"1.23"' as jsonb), - 'strict $.a' - columns ( - js2 int path '$' - ) - ); + json_table(cast('"1.23"' as jsonb), 'strict $.a' columns (js2 int path '$')); select * @@ -428,7 +408,7 @@ select * from (values ('1'), ('"err"')) as vals (js), - json_table( cast(vals.js as jsonb), '$' columns (a int path '$' ) ) as jt; + json_table(cast(vals.js as jsonb), '$' columns (a int path '$')) as jt; select * @@ -551,28 +531,22 @@ from select * from - json_table( cast('"a"' as jsonb), '$' columns (a int2 exists path '$.a' ) ); + json_table(cast('"a"' as jsonb), '$' columns (a int2 exists path '$.a')); select * from - json_table( cast('"a"' as jsonb), '$' columns (a int8 exists path '$.a' ) ); + json_table(cast('"a"' as jsonb), '$' columns (a int8 exists path '$.a')); select * from - json_table( cast('"a"' as jsonb), '$' columns (a float4 exists path '$.a' ) ); + json_table(cast('"a"' as jsonb), '$' columns (a float4 exists path '$.a')); select * from - json_table( - cast('"a"' as jsonb), - '$' - columns ( - a char(3) exists path '$.a' - ) - ); + json_table(cast('"a"' as jsonb), '$' columns (a char(3) exists path '$.a')); select * @@ -599,12 +573,12 @@ from select * from - json_table( cast('"a"' as jsonb), '$' columns (a json exists path '$.a' ) ); + json_table(cast('"a"' as jsonb), '$' columns (a json exists path '$.a')); select * from - json_table( cast('"a"' as jsonb), '$' columns (a jsonb exists path '$.a' ) ); + json_table(cast('"a"' as jsonb), '$' columns (a jsonb exists path '$.a')); create domain dint4 as int; @@ -614,7 +588,7 @@ select a, cast(a as bool) from - json_table( cast('"a"' as jsonb), '$' columns (a dint4 exists path '$.a' ) ); + json_table(cast('"a"' as jsonb), '$' columns (a dint4 exists path '$.a')); select a, @@ -809,7 +783,7 @@ from '$' as a columns ( b int, - nested path '$' as a columns (c int ) + nested path '$' as a columns (c int) ) ) as jt; @@ -821,7 +795,7 @@ from '$' as a columns ( b int, - nested path '$' as n_a columns (c int ) + nested path '$' as n_a columns (c int) ) ) as jt; @@ -833,7 +807,7 @@ from '$' columns ( b int, - nested path '$' as b columns (c int ) + nested path '$' as b columns (c int) ) ) as jt; @@ -844,8 +818,8 @@ from cast('[]' as jsonb), '$' columns ( - nested path '$' as a columns (b int ), - nested path '$' columns (nested path '$' as a columns (c int ) ) + nested path '$' as a columns (b int), + nested path '$' columns (nested path '$' as a columns (c int)) ) ) as jt; @@ -893,7 +867,7 @@ from y as y columns ( y text format json path '$', - nested path 'strict $[*] ? (@ == $y)' columns (z int path '$' ) + nested path 'strict $[*] ? (@ == $y)' columns (z int path '$') ) ) as jt; @@ -911,13 +885,13 @@ from nested path '$[1]' as p1 columns ( a1 int, - nested path '$[*]' as "p1 1" columns (a11 text ), + nested path '$[*]' as "p1 1" columns (a11 text), b1 text ), nested path '$[2]' as p2 columns ( - nested path '$[*]' as "p2:1" columns (a21 text ), - nested path '$[*]' as p22 columns (a22 text ) + nested path '$[*]' as "p2:1" columns (a21 text), + nested path '$[*]' as p22 columns (a22 text) ) ) ); @@ -968,17 +942,17 @@ from columns ( nested path '$.a.za[2]' columns ( - nested path '$.z22[*]' as z22 columns (c int path '$' ) + nested path '$.z22[*]' as z22 columns (c int path '$') ), - nested path '$.a.za[1]' columns (d int[] path '$.z21' ), + nested path '$.a.za[1]' columns (d int[] path '$.z21'), nested path '$.a.za[0]' columns ( - nested path '$.z1[*]' as z1 columns (a int path '$' ) + nested path '$.z1[*]' as z1 columns (a int path '$') ), xx1 int path '$.c', nested path '$.a.za[1]' columns ( - nested path '$.z21[*]' as z21 columns (b int path '$' ) + nested path '$.z21[*]' as z21 columns (b int path '$') ), xx int path '$.c' ) @@ -999,11 +973,11 @@ from xx1 int path '$.c', nested path '$.a.za[0].z1[*]' columns ( - nested path '$ ?(@ >= ($"x" -2))' columns (a int path '$' ) + nested path '$ ?(@ >= ($"x" -2))' columns (a int path '$') ), nested path '$.a.za[0]' columns ( - nested path '$.z1[*] ? (@ >= ($"x" -2))' columns (b int path '$' ) + nested path '$.z1[*] ? (@ >= ($"x" -2))' columns (b int path '$') ) ) ) as sub; @@ -1023,7 +997,7 @@ from xx1 int path '$.c', nested path '$.a.za[1]' columns ( - nested path '$.z21[*]' columns (b int path '$' ) + nested path '$.z21[*]' columns (b int path '$') ), nested path '$.a.za[1] ? (@.z21[*] >= ($"x"-1))' columns ( @@ -1057,7 +1031,7 @@ from xx1 int path '$.c', nested path '$.a.za[2]' columns ( - nested path '$.z22[*]' as z22 columns (c int path '$' ) + nested path '$.z22[*]' as z22 columns (c int path '$') ), nested path '$.a.za[1]' columns ( @@ -1065,7 +1039,7 @@ from ), nested path '$.a.za[0]' columns ( - nested path '$.z1[*] ? (@ >= ($"x" -2))' as z1 columns (a int path '$' ) + nested path '$.z1[*] ? (@ >= ($"x" -2))' as z1 columns (a int path '$') ), nested path '$.a.za[1]' columns ( @@ -1123,26 +1097,14 @@ drop view jsonb_table_view7; drop table s; -select - * -from - json_table( cast('1' as jsonb), '$' columns (a int ) null on error ); +select * from json_table(cast('1' as jsonb), '$' columns (a int) null on error); -select - * -from - json_table( cast('1' as jsonb), '$' columns (a int true on empty ) ); +select * from json_table(cast('1' as jsonb), '$' columns (a int true on empty)); select * from - json_table( - cast('1' as jsonb), - '$' - columns ( - a int omit quotes true on error - ) - ); + json_table(cast('1' as jsonb), '$' columns (a int omit quotes true on error)); select * @@ -1156,13 +1118,13 @@ from ); create view json_table_view8 -as select * from json_table( '"a"', '$' columns (a text path '$' ) ); +as select * from json_table('"a"', '$' columns (a text path '$')); create view json_table_view9 as select * from - json_table( '"a"', '$' columns (a text path '$' ) error on error ); + json_table('"a"', '$' columns (a text path '$') error on error); drop view json_table_view8, json_table_view9; @@ -1170,12 +1132,12 @@ create view json_table_view8 as select * from - json_table( '"a"', '$' columns (a text path '$' ) empty array on error ); + json_table('"a"', '$' columns (a text path '$') empty array on error); create view json_table_view9 as select * from - json_table( '"a"', '$' columns (a text path '$' ) empty array on error ); + json_table('"a"', '$' columns (a text path '$') empty array on error); drop view json_table_view8, json_table_view9; diff --git a/crates/pgls_pretty_print/tests/snapshots/multi/tests__stats_ext_100.snap b/crates/pgls_pretty_print/tests/snapshots/multi/tests__stats_ext_100.snap index 97d94d05c..7df2f7ed9 100644 --- a/crates/pgls_pretty_print/tests/snapshots/multi/tests__stats_ext_100.snap +++ b/crates/pgls_pretty_print/tests/snapshots/multi/tests__stats_ext_100.snap @@ -77,9 +77,9 @@ create statistics tst on a from (select * from ext_stats_test) as foo; create statistics tst on a from ext_stats_test as s tablesample system (x); -create statistics tst on a from xmltable( 'foo' passing 'bar' columns a text); +create statistics tst on a from xmltable('foo' passing 'bar' columns a text); -create statistics tst on a from json_table( cast('123' as jsonb), '$' columns (item int ) ); +create statistics tst on a from json_table(cast('123' as jsonb), '$' columns (item int)); create function tftest(int) returns table ( @@ -356,7 +356,7 @@ create foreign data wrapper extstats_dummy_fdw; create server extstats_dummy_srv foreign data wrapper extstats_dummy_fdw; -create foreign table tststats.f ( a int, b int, c text ) server extstats_dummy_srv; +create foreign table tststats.f (a int, b int, c text) server extstats_dummy_srv; create table tststats.pt ( a int, diff --git a/crates/pgls_pretty_print/tests/snapshots/multi/tests__stats_ext_80.snap b/crates/pgls_pretty_print/tests/snapshots/multi/tests__stats_ext_80.snap index 455a35ebb..f5b4ca747 100644 --- a/crates/pgls_pretty_print/tests/snapshots/multi/tests__stats_ext_80.snap +++ b/crates/pgls_pretty_print/tests/snapshots/multi/tests__stats_ext_80.snap @@ -77,11 +77,11 @@ create statistics tst on a from (select * from ext_stats_test) as foo; create statistics tst on a from ext_stats_test as s tablesample system (x); -create statistics tst on a from xmltable( 'foo' passing 'bar' columns a text); +create statistics tst on a from xmltable('foo' passing 'bar' columns a text); create statistics tst on a -from json_table( cast('123' as jsonb), '$' columns (item int ) ); +from json_table(cast('123' as jsonb), '$' columns (item int)); create function tftest(int) returns table ( diff --git a/crates/pgls_pretty_print/tests/snapshots/multi/tests__typed_table_100.snap b/crates/pgls_pretty_print/tests/snapshots/multi/tests__typed_table_100.snap index 2a3af5abd..f79615b26 100644 --- a/crates/pgls_pretty_print/tests/snapshots/multi/tests__typed_table_100.snap +++ b/crates/pgls_pretty_print/tests/snapshots/multi/tests__typed_table_100.snap @@ -37,13 +37,13 @@ create table stuff (id int); alter table persons inherit stuff; -create table personsx of person_type (myname with options not null ); +create table personsx of person_type (myname with options not null); -create table persons2 of person_type (id with options primary key, unique (name) ); +create table persons2 of person_type (id with options primary key, unique (name)); -create table persons3 of person_type (primary key (id), name with options default '' ); +create table persons3 of person_type (primary key (id), name with options default ''); -create table persons4 of person_type (name with options not null, name with options default '' ); +create table persons4 of person_type (name with options not null, name with options default ''); drop type person_type; @@ -72,6 +72,6 @@ as $$ SELECT length($1.name) $$; select id, namelen(persons) from persons; -create table persons2 of person_type (id with options primary key, unique (name) ); +create table persons2 of person_type (id with options primary key, unique (name)); -create table persons3 of person_type (primary key (id), name with options not null default '' ); +create table persons3 of person_type (primary key (id), name with options not null default ''); diff --git a/crates/pgls_pretty_print/tests/snapshots/multi/tests__typed_table_80.snap b/crates/pgls_pretty_print/tests/snapshots/multi/tests__typed_table_80.snap index f494637a7..ce8dda654 100644 --- a/crates/pgls_pretty_print/tests/snapshots/multi/tests__typed_table_80.snap +++ b/crates/pgls_pretty_print/tests/snapshots/multi/tests__typed_table_80.snap @@ -37,7 +37,7 @@ create table stuff (id int); alter table persons inherit stuff; -create table personsx of person_type (myname with options not null ); +create table personsx of person_type (myname with options not null); create table persons2 of person_type ( diff --git a/crates/pgls_pretty_print/tests/snapshots/multi/tests__window_100.snap b/crates/pgls_pretty_print/tests/snapshots/multi/tests__window_100.snap index 95ec44041..0ccd74208 100644 --- a/crates/pgls_pretty_print/tests/snapshots/multi/tests__window_100.snap +++ b/crates/pgls_pretty_print/tests/snapshots/multi/tests__window_100.snap @@ -440,7 +440,7 @@ where unique1 < 10; select - sum(unique1) over ( rows between current row and unbounded following), + sum(unique1) over (rows between current row and unbounded following), unique1, four from @@ -449,7 +449,7 @@ where unique1 < 10; select - sum(unique1) over ( rows between 2 preceding and 2 following), + sum(unique1) over (rows between 2 preceding and 2 following), unique1, four from @@ -458,7 +458,7 @@ where unique1 < 10; select - sum(unique1) over ( rows between 2 preceding and 2 following), + sum(unique1) over (rows between 2 preceding and 2 following), unique1, four from @@ -467,7 +467,7 @@ where unique1 < 10; select - sum(unique1) over ( rows between 2 preceding and 2 following exclude current row), + sum(unique1) over (rows between 2 preceding and 2 following exclude current row), unique1, four from @@ -476,7 +476,7 @@ where unique1 < 10; select - sum(unique1) over ( rows between 2 preceding and 2 following exclude group), + sum(unique1) over (rows between 2 preceding and 2 following exclude group), unique1, four from @@ -485,7 +485,7 @@ where unique1 < 10; select - sum(unique1) over ( rows between 2 preceding and 2 following exclude ties), + sum(unique1) over (rows between 2 preceding and 2 following exclude ties), unique1, four from @@ -562,7 +562,7 @@ where unique1 < 10; select - sum(unique1) over ( rows between 2 preceding and 1 preceding), + sum(unique1) over (rows between 2 preceding and 1 preceding), unique1, four from @@ -571,7 +571,7 @@ where unique1 < 10; select - sum(unique1) over ( rows between 1 following and 3 following), + sum(unique1) over (rows between 1 following and 3 following), unique1, four from @@ -580,7 +580,7 @@ where unique1 < 10; select - sum(unique1) over ( rows between unbounded preceding and 1 following), + sum(unique1) over (rows between unbounded preceding and 1 following), unique1, four from @@ -662,8 +662,7 @@ window select sum(unique1) over ( order by unique1 - rows - (select unique1 from tenk1 order by unique1 limit 1) + 1 preceding + rows (select unique1 from tenk1 order by unique1 limit 1) + 1 preceding ), unique1 from @@ -1210,7 +1209,7 @@ window ); select - sum(unique1) over ( rows between x preceding and x following), + sum(unique1) over (rows between x preceding and x following), unique1, four from @@ -1238,7 +1237,7 @@ select * from unbounded_syntax_test1a(2); select * from unbounded_syntax_test1b(2); select - sum(unique1) over ( rows between unbounded preceding and unbounded following), + sum(unique1) over (rows between unbounded preceding and unbounded following), unique1, four from @@ -1278,7 +1277,7 @@ immutable return x; select - sum(unique1) over ( rows between 1 preceding and 1 following), + sum(unique1) over (rows between 1 preceding and 1 following), unique1, four from @@ -1287,7 +1286,7 @@ where unique1 < 10; select - sum(unique1) over ( rows between unbounded(1) preceding and unbounded(1) following), + sum(unique1) over (rows between unbounded(1) preceding and unbounded(1) following), unique1, four from @@ -1296,7 +1295,7 @@ where unique1 < 10; select - sum(unique1) over ( rows between unbounded.x preceding and unbounded.x following), + sum(unique1) over (rows between unbounded.x preceding and unbounded.x following), unique1, four from @@ -2280,7 +2279,6 @@ from select sum(salary) over ( - range between cast('1 year' as interval) preceding and @@ -3650,10 +3648,10 @@ from select x, - avg(x) over ( rows between current row and 1 following) as curr_next_avg, - avg(x) over ( rows between 1 preceding and current row) as prev_curr_avg, - sum(x) over ( rows between current row and 1 following) as curr_next_sum, - sum(x) over ( rows between 1 preceding and current row) as prev_curr_sum + avg(x) over (rows between current row and 1 following) as curr_next_avg, + avg(x) over (rows between 1 preceding and current row) as prev_curr_avg, + sum(x) over (rows between current row and 1 following) as curr_next_sum, + sum(x) over (rows between 1 preceding and current row) as prev_curr_sum from ( values @@ -3672,7 +3670,7 @@ from select x, - avg(x) over ( rows between current row and 2 following) + avg(x) over (rows between current row and 2 following) from ( values @@ -3686,7 +3684,7 @@ from select x, - sum(x) over ( rows between current row and 2 following) + sum(x) over (rows between current row and 2 following) from ( values diff --git a/crates/pgls_pretty_print/tests/snapshots/multi/tests__window_80.snap b/crates/pgls_pretty_print/tests/snapshots/multi/tests__window_80.snap index 99eb788d0..55633a45e 100644 --- a/crates/pgls_pretty_print/tests/snapshots/multi/tests__window_80.snap +++ b/crates/pgls_pretty_print/tests/snapshots/multi/tests__window_80.snap @@ -527,7 +527,7 @@ where unique1 < 10; select - sum(unique1) over ( rows between current row and unbounded following), + sum(unique1) over (rows between current row and unbounded following), unique1, four from @@ -536,7 +536,7 @@ where unique1 < 10; select - sum(unique1) over ( rows between 2 preceding and 2 following), + sum(unique1) over (rows between 2 preceding and 2 following), unique1, four from @@ -545,7 +545,7 @@ where unique1 < 10; select - sum(unique1) over ( rows between 2 preceding and 2 following), + sum(unique1) over (rows between 2 preceding and 2 following), unique1, four from @@ -555,7 +555,6 @@ where select sum(unique1) over ( - rows between 2 preceding and @@ -570,7 +569,7 @@ where unique1 < 10; select - sum(unique1) over ( rows between 2 preceding and 2 following exclude group), + sum(unique1) over (rows between 2 preceding and 2 following exclude group), unique1, four from @@ -579,7 +578,7 @@ where unique1 < 10; select - sum(unique1) over ( rows between 2 preceding and 2 following exclude ties), + sum(unique1) over (rows between 2 preceding and 2 following exclude ties), unique1, four from @@ -684,7 +683,7 @@ where unique1 < 10; select - sum(unique1) over ( rows between 2 preceding and 1 preceding), + sum(unique1) over (rows between 2 preceding and 1 preceding), unique1, four from @@ -693,7 +692,7 @@ where unique1 < 10; select - sum(unique1) over ( rows between 1 following and 3 following), + sum(unique1) over (rows between 1 following and 3 following), unique1, four from @@ -702,7 +701,7 @@ where unique1 < 10; select - sum(unique1) over ( rows between unbounded preceding and 1 following), + sum(unique1) over (rows between unbounded preceding and 1 following), unique1, four from @@ -805,8 +804,7 @@ window select sum(unique1) over ( order by unique1 - rows - (select unique1 from tenk1 order by unique1 limit 1) + 1 preceding + rows (select unique1 from tenk1 order by unique1 limit 1) + 1 preceding ), unique1 from @@ -1419,7 +1417,7 @@ window ); select - sum(unique1) over ( rows between x preceding and x following), + sum(unique1) over (rows between x preceding and x following), unique1, four from @@ -1447,7 +1445,7 @@ select * from unbounded_syntax_test1a(2); select * from unbounded_syntax_test1b(2); select - sum(unique1) over ( rows between unbounded preceding and unbounded following), + sum(unique1) over (rows between unbounded preceding and unbounded following), unique1, four from @@ -1487,7 +1485,7 @@ immutable return x; select - sum(unique1) over ( rows between 1 preceding and 1 following), + sum(unique1) over (rows between 1 preceding and 1 following), unique1, four from @@ -1497,7 +1495,6 @@ where select sum(unique1) over ( - rows between unbounded(1) preceding and @@ -1512,7 +1509,6 @@ where select sum(unique1) over ( - rows between unbounded.x preceding and @@ -2571,7 +2567,6 @@ from select sum(salary) over ( - range between cast('1 year' as interval) preceding and @@ -4286,10 +4281,10 @@ from select x, - avg(x) over ( rows between current row and 1 following) as curr_next_avg, - avg(x) over ( rows between 1 preceding and current row) as prev_curr_avg, - sum(x) over ( rows between current row and 1 following) as curr_next_sum, - sum(x) over ( rows between 1 preceding and current row) as prev_curr_sum + avg(x) over (rows between current row and 1 following) as curr_next_avg, + avg(x) over (rows between 1 preceding and current row) as prev_curr_avg, + sum(x) over (rows between current row and 1 following) as curr_next_sum, + sum(x) over (rows between 1 preceding and current row) as prev_curr_sum from ( values @@ -4308,7 +4303,7 @@ from select x, - avg(x) over ( rows between current row and 2 following) + avg(x) over (rows between current row and 2 following) from ( values @@ -4322,7 +4317,7 @@ from select x, - sum(x) over ( rows between current row and 2 following) + sum(x) over (rows between current row and 2 following) from ( values diff --git a/crates/pgls_pretty_print/tests/snapshots/multi/tests__xml_100.snap b/crates/pgls_pretty_print/tests/snapshots/multi/tests__xml_100.snap index fcb80e8ae..ed945bb18 100644 --- a/crates/pgls_pretty_print/tests/snapshots/multi/tests__xml_100.snap +++ b/crates/pgls_pretty_print/tests/snapshots/multi/tests__xml_100.snap @@ -1,7 +1,6 @@ --- source: crates/pgls_pretty_print/tests/tests.rs input_file: crates/pgls_pretty_print/tests/data/multi/xml.sql -snapshot_kind: text --- create table xmltest ( id int, @@ -617,7 +616,6 @@ select from (select data from xmldata) as x, lateral xmltable( - '/ROWS/ROW' passing data columns @@ -637,7 +635,6 @@ as select from (select data from xmldata) as x, lateral xmltable( - '/ROWS/ROW' passing data columns @@ -657,13 +654,12 @@ select * from xmltableview1; select * from xmltableview1; -select * from xmltable( row() passing null columns v1 timestamp) as f (v1, v2); +select * from xmltable(row() passing null columns v1 timestamp) as f (v1, v2); select * from xmltable( - xmlnamespaces('http://x.y' as zz), '/zz:rows/zz:row' passing '10' @@ -676,7 +672,6 @@ as select * from xmltable( - xmlnamespaces('http://x.y' as "Zz"), '/Zz:rows/Zz:row' passing '10' @@ -690,7 +685,6 @@ select * from xmltable( - xmlnamespaces(default 'http://x.y'), '/rows/row' passing '10' @@ -698,7 +692,7 @@ from a int path 'a' ); -select * from xmltable( '.' passing '' columns a text path 'foo/namespace::node()'); +select * from xmltable('.' passing '' columns a text path 'foo/namespace::node()'); prepare pp as select @@ -706,7 +700,6 @@ as select from (select data from xmldata) as x, lateral xmltable( - '/ROWS/ROW' passing data columns @@ -727,7 +720,6 @@ select from xmldata, lateral xmltable( - '/ROWS/ROW[COUNTRY_NAME="Japan" or COUNTRY_NAME="India"]' passing data columns @@ -740,7 +732,6 @@ select from xmldata, lateral xmltable( - '/ROWS/ROW[COUNTRY_NAME="Japan" or COUNTRY_NAME="India"]' passing data columns @@ -754,7 +745,6 @@ select from xmldata, lateral xmltable( - '/ROWS/ROW[COUNTRY_NAME="Japan" or COUNTRY_NAME="India"]' passing data columns @@ -768,7 +758,6 @@ select from xmldata, lateral xmltable( - '/ROWS/ROW[COUNTRY_NAME="Japan" or COUNTRY_NAME="India"]' passing data columns @@ -780,7 +769,6 @@ select from xmldata, lateral xmltable( - '/ROWS/ROW[COUNTRY_NAME="Japan" or COUNTRY_NAME="India"]' passing data columns @@ -792,7 +780,6 @@ select from xmldata, lateral xmltable( - '/ROWS/ROW[COUNTRY_NAME="Japan" or COUNTRY_NAME="India"]' passing data columns @@ -807,7 +794,6 @@ select from xmldata, lateral xmltable( - '/ROWS/ROW[COUNTRY_NAME="Japan" or COUNTRY_NAME="India"]' passing data columns @@ -821,7 +807,6 @@ select * from xmltable( - '/root' passing 'a1aa2a bbbbxxxcccc' columns @@ -832,7 +817,6 @@ select * from xmltable( - '/root' passing 'a1aa2a bbbbxxxcccc' columns @@ -843,7 +827,6 @@ select * from xmltable( - 'd/r' passing ' &"<>!foo]]>2' columns @@ -854,7 +837,6 @@ select * from xmltable( - '/x/a' passing ''"&<>' columns @@ -865,7 +847,6 @@ select * from xmltable( - '/x/a' passing ''"&<>' columns @@ -877,7 +858,6 @@ select from (select data from xmldata) as x, lateral xmltable( - '/ROWS/ROW' passing data columns @@ -896,7 +876,6 @@ select from xmldata, lateral xmltable( - '/ROWS/ROW[COUNTRY_NAME="Japan" or COUNTRY_NAME="India"]' passing data columns @@ -911,7 +890,6 @@ select from xmldata, lateral xmltable( - '/ROWS/ROW[COUNTRY_NAME="Japan" or COUNTRY_NAME="India"]' passing data columns @@ -927,7 +905,6 @@ select from xmldata, lateral xmltable( - '/ROWS/ROW[COUNTRY_NAME="Japan" or COUNTRY_NAME="India"]' passing data columns @@ -974,7 +951,6 @@ select from (select data from xmldata) as x, lateral xmltable( - '/ROWS/ROW' passing data columns @@ -993,7 +969,6 @@ select from (select data from xmldata) as x, lateral xmltable( - '/ROWS/ROW' passing data columns @@ -1014,7 +989,6 @@ select from (select data from xmldata) as x, lateral xmltable( - '/ROWS/ROW' passing data columns @@ -1035,7 +1009,6 @@ select from (select data from xmldata) as x, lateral xmltable( - '/ROWS/ROW' passing data columns @@ -1080,7 +1053,6 @@ as ( from y, lateral xmltable( - '/proc' passing proc columns @@ -1134,7 +1106,6 @@ as ( from y, lateral xmltable( - '/data/proc' passing doc columns @@ -1167,20 +1138,19 @@ select xmltable.* from xmltest2, - lateral xmltable( '/d/r' passing x columns a int path '' || lower(_path) || 'c'); + lateral xmltable('/d/r' passing x columns a int path '' || lower(_path) || 'c'); select xmltable.* from xmltest2, - lateral xmltable( ('/d/r/' || lower(_path) || 'c') passing x columns a int path '.'); + lateral xmltable(('/d/r/' || lower(_path) || 'c') passing x columns a int path '.'); select xmltable.* from xmltest2, lateral xmltable( - ('/d/r/' || lower(_path) || 'c') passing x columns @@ -1191,7 +1161,6 @@ select * from xmltable( - '*' passing 'a' columns @@ -1206,7 +1175,6 @@ select * from xmltable( - '*' passing 'pre&deeppost' columns @@ -1218,7 +1186,6 @@ select * from xmltable( - '.' passing xmlelement(name a) columns diff --git a/crates/pgls_pretty_print/tests/snapshots/multi/tests__xml_80.snap b/crates/pgls_pretty_print/tests/snapshots/multi/tests__xml_80.snap index 374d36fb3..45000320f 100644 --- a/crates/pgls_pretty_print/tests/snapshots/multi/tests__xml_80.snap +++ b/crates/pgls_pretty_print/tests/snapshots/multi/tests__xml_80.snap @@ -1,7 +1,6 @@ --- source: crates/pgls_pretty_print/tests/tests.rs input_file: crates/pgls_pretty_print/tests/data/multi/xml.sql -snapshot_kind: text --- create table xmltest ( id int, @@ -686,7 +685,6 @@ select from (select data from xmldata) as x, lateral xmltable( - '/ROWS/ROW' passing data columns @@ -706,7 +704,6 @@ as select from (select data from xmldata) as x, lateral xmltable( - '/ROWS/ROW' passing data columns @@ -726,13 +723,12 @@ select * from xmltableview1; select * from xmltableview1; -select * from xmltable( row() passing null columns v1 timestamp) as f (v1, v2); +select * from xmltable(row() passing null columns v1 timestamp) as f (v1, v2); select * from xmltable( - xmlnamespaces('http://x.y' as zz), '/zz:rows/zz:row' passing '10' @@ -745,7 +741,6 @@ as select * from xmltable( - xmlnamespaces('http://x.y' as "Zz"), '/Zz:rows/Zz:row' passing '10' @@ -759,7 +754,6 @@ select * from xmltable( - xmlnamespaces(default 'http://x.y'), '/rows/row' passing '10' @@ -770,7 +764,7 @@ from select * from - xmltable( '.' passing '' columns a text path 'foo/namespace::node()'); + xmltable('.' passing '' columns a text path 'foo/namespace::node()'); prepare pp as select @@ -778,7 +772,6 @@ as select from (select data from xmldata) as x, lateral xmltable( - '/ROWS/ROW' passing data columns @@ -799,7 +792,6 @@ select from xmldata, lateral xmltable( - '/ROWS/ROW[COUNTRY_NAME="Japan" or COUNTRY_NAME="India"]' passing data columns @@ -812,7 +804,6 @@ select from xmldata, lateral xmltable( - '/ROWS/ROW[COUNTRY_NAME="Japan" or COUNTRY_NAME="India"]' passing data columns @@ -826,7 +817,6 @@ select from xmldata, lateral xmltable( - '/ROWS/ROW[COUNTRY_NAME="Japan" or COUNTRY_NAME="India"]' passing data columns @@ -840,7 +830,6 @@ select from xmldata, lateral xmltable( - '/ROWS/ROW[COUNTRY_NAME="Japan" or COUNTRY_NAME="India"]' passing data columns @@ -852,7 +841,6 @@ select from xmldata, lateral xmltable( - '/ROWS/ROW[COUNTRY_NAME="Japan" or COUNTRY_NAME="India"]' passing data columns @@ -864,7 +852,6 @@ select from xmldata, lateral xmltable( - '/ROWS/ROW[COUNTRY_NAME="Japan" or COUNTRY_NAME="India"]' passing data columns @@ -879,7 +866,6 @@ select from xmldata, lateral xmltable( - '/ROWS/ROW[COUNTRY_NAME="Japan" or COUNTRY_NAME="India"]' passing data columns @@ -893,7 +879,6 @@ select * from xmltable( - '/root' passing 'a1aa2a bbbbxxxcccc' columns @@ -904,7 +889,6 @@ select * from xmltable( - '/root' passing 'a1aa2a bbbbxxxcccc' columns @@ -915,7 +899,6 @@ select * from xmltable( - 'd/r' passing ' &"<>!foo]]>2' columns @@ -926,7 +909,6 @@ select * from xmltable( - '/x/a' passing ''"&<>' columns @@ -937,7 +919,6 @@ select * from xmltable( - '/x/a' passing ''"&<>' columns @@ -949,7 +930,6 @@ select from (select data from xmldata) as x, lateral xmltable( - '/ROWS/ROW' passing data columns @@ -968,7 +948,6 @@ select from xmldata, lateral xmltable( - '/ROWS/ROW[COUNTRY_NAME="Japan" or COUNTRY_NAME="India"]' passing data columns @@ -983,7 +962,6 @@ select from xmldata, lateral xmltable( - '/ROWS/ROW[COUNTRY_NAME="Japan" or COUNTRY_NAME="India"]' passing data columns @@ -999,7 +977,6 @@ select from xmldata, lateral xmltable( - '/ROWS/ROW[COUNTRY_NAME="Japan" or COUNTRY_NAME="India"]' passing data columns @@ -1046,7 +1023,6 @@ select from (select data from xmldata) as x, lateral xmltable( - '/ROWS/ROW' passing data columns @@ -1065,7 +1041,6 @@ select from (select data from xmldata) as x, lateral xmltable( - '/ROWS/ROW' passing data columns @@ -1086,7 +1061,6 @@ select from (select data from xmldata) as x, lateral xmltable( - '/ROWS/ROW' passing data columns @@ -1107,7 +1081,6 @@ select from (select data from xmldata) as x, lateral xmltable( - '/ROWS/ROW' passing data columns @@ -1162,7 +1135,6 @@ as ( from y, lateral xmltable( - '/proc' passing proc columns @@ -1219,7 +1191,6 @@ as ( from y, lateral xmltable( - '/data/proc' passing doc columns @@ -1253,7 +1224,6 @@ select from xmltest2, lateral xmltable( - '/d/r' passing x columns @@ -1265,7 +1235,6 @@ select from xmltest2, lateral xmltable( - ('/d/r/' || lower(_path) || 'c') passing x columns @@ -1277,7 +1246,6 @@ select from xmltest2, lateral xmltable( - ('/d/r/' || lower(_path) || 'c') passing x columns @@ -1288,7 +1256,6 @@ select * from xmltable( - '*' passing 'a' columns @@ -1303,7 +1270,6 @@ select * from xmltable( - '*' passing 'pre&deeppost' columns @@ -1315,7 +1281,6 @@ select * from xmltable( - '.' passing xmlelement(name a) columns diff --git a/crates/pgls_pretty_print/tests/snapshots/single/tests__complex_select_0_100.snap b/crates/pgls_pretty_print/tests/snapshots/single/tests__complex_select_0_100.snap index e17cb4e41..065ba1502 100644 --- a/crates/pgls_pretty_print/tests/snapshots/single/tests__complex_select_0_100.snap +++ b/crates/pgls_pretty_print/tests/snapshots/single/tests__complex_select_0_100.snap @@ -1,7 +1,6 @@ --- source: crates/pgls_pretty_print/tests/tests.rs input_file: crates/pgls_pretty_print/tests/data/single/complex_select_0.sql -snapshot_kind: text --- with recursive pks_fks @@ -208,7 +207,7 @@ select ) as column_dependencies from - repeated_references as rep inner join pks_fks using ( resorigtbl, resorigcol) + repeated_references as rep inner join pks_fks using (resorigtbl, resorigcol) inner join pg_class as tbl on tbl.oid = rep.resorigtbl diff --git a/crates/pgls_pretty_print/tests/snapshots/single/tests__complex_select_0_80.snap b/crates/pgls_pretty_print/tests/snapshots/single/tests__complex_select_0_80.snap index 4bce7a692..a057000c5 100644 --- a/crates/pgls_pretty_print/tests/snapshots/single/tests__complex_select_0_80.snap +++ b/crates/pgls_pretty_print/tests/snapshots/single/tests__complex_select_0_80.snap @@ -1,7 +1,6 @@ --- source: crates/pgls_pretty_print/tests/tests.rs input_file: crates/pgls_pretty_print/tests/data/single/complex_select_0.sql -snapshot_kind: text --- with recursive pks_fks @@ -227,7 +226,7 @@ select ) as column_dependencies from - repeated_references as rep inner join pks_fks using ( resorigtbl, resorigcol) + repeated_references as rep inner join pks_fks using (resorigtbl, resorigcol) inner join pg_class as tbl on tbl.oid = rep.resorigtbl diff --git a/crates/pgls_pretty_print/tests/snapshots/single/tests__complex_select_part_7_100.snap b/crates/pgls_pretty_print/tests/snapshots/single/tests__complex_select_part_7_100.snap index 283facf8f..fe2ed1be6 100644 --- a/crates/pgls_pretty_print/tests/snapshots/single/tests__complex_select_part_7_100.snap +++ b/crates/pgls_pretty_print/tests/snapshots/single/tests__complex_select_part_7_100.snap @@ -1,7 +1,6 @@ --- source: crates/pgls_pretty_print/tests/tests.rs input_file: crates/pgls_pretty_print/tests/data/single/complex_select_part_7.sql -snapshot_kind: text --- select sch.nspname as table_schema, @@ -18,7 +17,7 @@ select ) as column_dependencies from - repeated_references as rep inner join pks_fks using ( resorigtbl, resorigcol) + repeated_references as rep inner join pks_fks using (resorigtbl, resorigcol) inner join pg_class as tbl on tbl.oid = rep.resorigtbl diff --git a/crates/pgls_pretty_print/tests/snapshots/single/tests__complex_select_part_7_80.snap b/crates/pgls_pretty_print/tests/snapshots/single/tests__complex_select_part_7_80.snap index 494cb00e9..985c63bf9 100644 --- a/crates/pgls_pretty_print/tests/snapshots/single/tests__complex_select_part_7_80.snap +++ b/crates/pgls_pretty_print/tests/snapshots/single/tests__complex_select_part_7_80.snap @@ -1,7 +1,6 @@ --- source: crates/pgls_pretty_print/tests/tests.rs input_file: crates/pgls_pretty_print/tests/data/single/complex_select_part_7.sql -snapshot_kind: text --- select sch.nspname as table_schema, @@ -18,7 +17,7 @@ select ) as column_dependencies from - repeated_references as rep inner join pks_fks using ( resorigtbl, resorigcol) + repeated_references as rep inner join pks_fks using (resorigtbl, resorigcol) inner join pg_class as tbl on tbl.oid = rep.resorigtbl diff --git a/crates/pgls_pretty_print/tests/snapshots/single/tests__create_foreign_table_stmt_0_100.snap b/crates/pgls_pretty_print/tests/snapshots/single/tests__create_foreign_table_stmt_0_100.snap index 6e4120341..cba18392d 100644 --- a/crates/pgls_pretty_print/tests/snapshots/single/tests__create_foreign_table_stmt_0_100.snap +++ b/crates/pgls_pretty_print/tests/snapshots/single/tests__create_foreign_table_stmt_0_100.snap @@ -1,6 +1,5 @@ --- source: crates/pgls_pretty_print/tests/tests.rs input_file: crates/pgls_pretty_print/tests/data/single/create_foreign_table_stmt_0.sql -snapshot_kind: text --- -create foreign table foreign_users ( id int ) server myserver; +create foreign table foreign_users (id int) server myserver; diff --git a/crates/pgls_pretty_print/tests/snapshots/single/tests__create_foreign_table_stmt_0_80.snap b/crates/pgls_pretty_print/tests/snapshots/single/tests__create_foreign_table_stmt_0_80.snap index 6e4120341..cba18392d 100644 --- a/crates/pgls_pretty_print/tests/snapshots/single/tests__create_foreign_table_stmt_0_80.snap +++ b/crates/pgls_pretty_print/tests/snapshots/single/tests__create_foreign_table_stmt_0_80.snap @@ -1,6 +1,5 @@ --- source: crates/pgls_pretty_print/tests/tests.rs input_file: crates/pgls_pretty_print/tests/data/single/create_foreign_table_stmt_0.sql -snapshot_kind: text --- -create foreign table foreign_users ( id int ) server myserver; +create foreign table foreign_users (id int) server myserver; diff --git a/crates/pgls_pretty_print/tests/snapshots/single/tests__range_table_func_0_100.snap b/crates/pgls_pretty_print/tests/snapshots/single/tests__range_table_func_0_100.snap index 85796a1f3..b969895ed 100644 --- a/crates/pgls_pretty_print/tests/snapshots/single/tests__range_table_func_0_100.snap +++ b/crates/pgls_pretty_print/tests/snapshots/single/tests__range_table_func_0_100.snap @@ -1,9 +1,8 @@ --- source: crates/pgls_pretty_print/tests/tests.rs input_file: crates/pgls_pretty_print/tests/data/single/range_table_func_0.sql -snapshot_kind: text --- select * from - xmltable( '/root' passing 'value' columns item text path 'item'); + xmltable('/root' passing 'value' columns item text path 'item'); diff --git a/crates/pgls_pretty_print/tests/snapshots/single/tests__range_table_func_0_80.snap b/crates/pgls_pretty_print/tests/snapshots/single/tests__range_table_func_0_80.snap index 332bb7f3c..ce3439fe4 100644 --- a/crates/pgls_pretty_print/tests/snapshots/single/tests__range_table_func_0_80.snap +++ b/crates/pgls_pretty_print/tests/snapshots/single/tests__range_table_func_0_80.snap @@ -1,13 +1,11 @@ --- source: crates/pgls_pretty_print/tests/tests.rs input_file: crates/pgls_pretty_print/tests/data/single/range_table_func_0.sql -snapshot_kind: text --- select * from xmltable( - '/root' passing 'value' columns diff --git a/crates/pgls_pretty_print/tests/tests.rs b/crates/pgls_pretty_print/tests/tests.rs index c2cd8105d..c81960382 100644 --- a/crates/pgls_pretty_print/tests/tests.rs +++ b/crates/pgls_pretty_print/tests/tests.rs @@ -1,3 +1,5 @@ +mod common; + use camino::Utf8Path; use dir_test::{Fixture, dir_test}; use insta::{assert_snapshot, with_settings}; @@ -167,6 +169,7 @@ fn test_single(fixture: Fixture<&str>) { println!("Formatted content (width={max_line_length}):\n{output}"); assert_line_lengths(&output, max_line_length); + common::assert_clean_whitespace(&output); let parsed_output = pgls_query::parse(&output).expect("Failed to parse SQL"); let mut parsed_ast = parsed_output.into_root().expect("No root node found"); @@ -255,6 +258,7 @@ fn test_multi(fixture: Fixture<&str>) { // Verify line length assert_line_lengths(&output, max_line_length); + common::assert_clean_whitespace(&output); // Verify AST equality let parsed_output = pgls_query::parse(&output).unwrap_or_else(|e| { diff --git a/crates/pgls_pretty_print/tests/whitespace_layout.rs b/crates/pgls_pretty_print/tests/whitespace_layout.rs new file mode 100644 index 000000000..061ed0cf2 --- /dev/null +++ b/crates/pgls_pretty_print/tests/whitespace_layout.rs @@ -0,0 +1,145 @@ +mod common; + +use pgls_pretty_print::{CommaStyle, FormatConfig, Layout, format_statement}; + +fn format_idempotently(sql: &str, config: &FormatConfig) -> String { + let ast = pgls_query::parse(sql).unwrap().into_root().unwrap(); + let first = format_statement(&ast, sql, config).unwrap().formatted; + + let ast = pgls_query::parse(&first).unwrap().into_root().unwrap(); + let second = format_statement(&ast, &first, config).unwrap().formatted; + assert_eq!(second, first); + common::assert_clean_whitespace(&first); + first +} + +#[test] +fn leading_commas_do_not_add_a_space_before_the_comma() { + let config = FormatConfig { + comma_style: CommaStyle::Leading, + ..Default::default() + }; + assert_eq!( + format_idempotently("select a, b from t", &config), + "select a, b from t;" + ); + assert_eq!( + format_idempotently("insert into t (a, b) values (1, 2)", &config), + "insert into t (a, b) values (1, 2);" + ); + + // A list that breaks still puts the comma in front of the item. + let config = FormatConfig { + line_width: 40, + ..config + }; + assert_eq!( + format_idempotently( + "select first_column, second_column, third_column from t", + &config + ), + "select\n first_column\n , second_column\n , third_column\nfrom\n t;" + ); +} + +#[test] +fn leading_commas_in_expanded_layout_break_before_every_comma() { + let config = FormatConfig { + comma_style: CommaStyle::Leading, + layout: Layout::Expanded, + ..Default::default() + }; + assert_eq!( + format_idempotently("select a, b from t", &config), + "select\n a\n , b\nfrom\n t;" + ); +} + +#[test] +fn no_trailing_whitespace_after_a_block_comment_in_a_policy() { + let output = format_idempotently( + "create policy p on t using (\n a = 1 -- first\n and b = 2 /* second */\n);", + &FormatConfig::default(), + ); + assert!(output.contains("b = 2 /* second */\n);"), "{output}"); +} + +#[test] +fn no_trailing_whitespace_before_an_isolated_semicolon() { + let config = FormatConfig { + line_width: 40, + isolate_semicolon: true, + ..Default::default() + }; + let output = format_idempotently( + "create foreign data wrapper foo_wrapper handler foo_handler validator bar_validator", + &config, + ); + assert!(output.ends_with("validator bar_validator\n;"), "{output}"); +} + +#[test] +fn no_space_inside_parentheses() { + let config = FormatConfig::default(); + assert_eq!( + format_idempotently("select * from a join b using (x, y)", &config), + "select * from a inner join b using (x, y);" + ); + assert_eq!( + format_idempotently("create foreign table ft (a int) server s", &config), + "create foreign table ft (a int) server s;" + ); + assert_eq!( + format_idempotently( + "select * from json_table('{}'::jsonb, '$' as a columns (c int))", + &config + ), + "select * from json_table(cast('{}' as jsonb), '$' as a columns (c int));" + ); + assert_eq!( + format_idempotently( + "select * from xmltable('/root' passing '' columns item text)", + &config + ), + "select * from xmltable('/root' passing '' columns item text);" + ); + assert_eq!( + format_idempotently( + "select sum(x) over (rows between 1 preceding and current row) from t", + &config + ), + "select sum(x) over (rows between 1 preceding and current row) from t;" + ); + assert_eq!( + format_idempotently("alter publication p add table t where (a > 1)", &config), + "alter publication p add table t where (a > 1);" + ); + assert_eq!( + format_idempotently( + "create table t of person_type (id with options not null)", + &config + ), + "create table t of person_type (id with options not null);" + ); +} + +#[test] +fn no_blank_line_in_empty_parentheses() { + let config = FormatConfig { + line_width: 40, + ..Default::default() + }; + assert_eq!( + format_idempotently( + "create table i_am_a_failure () using i_do_not_exist_am", + &config + ), + "create table i_am_a_failure () using i_do_not_exist_am;" + ); + + let output = format_idempotently( + "select sum(salary) over (range between unbounded preceding and current row) from empsalary", + &config, + ); + assert!(output.contains("over (\n"), "{output}"); +} From 06192358120fa0dcc5d6cdbe4eafaacbdd8eeeef Mon Sep 17 00:00:00 2001 From: psteinroe Date: Fri, 23 Oct 2026 13:57:07 +0000 Subject: [PATCH 2/7] fix(pretty_print): VALUES keeps comments, ORDER BY/LIMIT and the CTE header together --- crates/pgls_pretty_print/src/comments.rs | 9 + crates/pgls_pretty_print/src/emitter.rs | 21 ++- .../src/nodes/common_table_expr.rs | 7 +- .../src/nodes/select_stmt.rs | 161 +++++++++++------- .../src/nodes/with_clause.rs | 9 +- .../data/multi/values_order_by_limit.sql | 13 ++ .../snapshots/multi/tests__brin_100.snap | 9 +- .../tests/snapshots/multi/tests__brin_80.snap | 9 +- .../multi/tests__collate.icu.utf8_100.snap | 4 +- .../multi/tests__collate.icu.utf8_80.snap | 4 +- .../multi/tests__collate.linux.utf8_100.snap | 4 +- .../multi/tests__collate.linux.utf8_80.snap | 4 +- .../snapshots/multi/tests__collate_100.snap | 4 +- .../snapshots/multi/tests__collate_80.snap | 4 +- .../multi/tests__conversion_100.snap | 28 +-- .../snapshots/multi/tests__conversion_80.snap | 28 +-- .../snapshots/multi/tests__float4_100.snap | 8 +- .../snapshots/multi/tests__float4_80.snap | 8 +- .../snapshots/multi/tests__float8_100.snap | 8 +- .../snapshots/multi/tests__float8_80.snap | 8 +- .../snapshots/multi/tests__inherit_100.snap | 4 +- .../snapshots/multi/tests__inherit_80.snap | 4 +- .../snapshots/multi/tests__insert_100.snap | 4 +- .../snapshots/multi/tests__insert_80.snap | 4 +- .../multi/tests__jsonb_jsonpath_100.snap | 5 +- .../multi/tests__jsonb_jsonpath_80.snap | 5 +- .../snapshots/multi/tests__merge_100.snap | 7 +- .../snapshots/multi/tests__merge_80.snap | 7 +- .../snapshots/multi/tests__numeric_100.snap | 29 +--- .../snapshots/multi/tests__numeric_80.snap | 41 ++--- .../multi/tests__numeric_big_100.snap | 65 ++----- .../multi/tests__numeric_big_80.snap | 65 ++----- .../multi/tests__object_address_100.snap | 8 +- .../multi/tests__object_address_80.snap | 8 +- .../multi/tests__opr_sanity_100.snap | 8 +- .../snapshots/multi/tests__opr_sanity_80.snap | 8 +- .../multi/tests__partition_prune_100.snap | 8 +- .../multi/tests__partition_prune_80.snap | 8 +- .../snapshots/multi/tests__rowtypes_100.snap | 8 +- .../snapshots/multi/tests__rowtypes_80.snap | 8 +- .../snapshots/multi/tests__subselect_80.snap | 10 +- .../multi/tests__timestamptz_100.snap | 5 +- .../multi/tests__timestamptz_80.snap | 5 +- .../snapshots/multi/tests__uuid_100.snap | 5 +- .../tests/snapshots/multi/tests__uuid_80.snap | 5 +- .../tests__values_order_by_limit_100.snap | 23 +++ .../tests__values_order_by_limit_80.snap | 23 +++ .../snapshots/multi/tests__window_100.snap | 18 +- .../snapshots/multi/tests__window_80.snap | 19 +-- .../snapshots/multi/tests__with_100.snap | 96 ++++------- .../tests/snapshots/multi/tests__with_80.snap | 123 +++++-------- .../tests/snapshots/multi/tests__xml_100.snap | 20 +-- .../tests/snapshots/multi/tests__xml_80.snap | 20 +-- .../single/tests__complex_select_0_100.snap | 31 ++-- .../single/tests__complex_select_0_80.snap | 22 +-- ...tests__delete_with_cte_returning_0_80.snap | 5 +- .../pgls_pretty_print/tests/values_layout.rs | 86 ++++++++++ 57 files changed, 509 insertions(+), 661 deletions(-) create mode 100644 crates/pgls_pretty_print/tests/data/multi/values_order_by_limit.sql create mode 100644 crates/pgls_pretty_print/tests/snapshots/multi/tests__values_order_by_limit_100.snap create mode 100644 crates/pgls_pretty_print/tests/snapshots/multi/tests__values_order_by_limit_80.snap diff --git a/crates/pgls_pretty_print/src/comments.rs b/crates/pgls_pretty_print/src/comments.rs index c7e996e90..bc792bb44 100644 --- a/crates/pgls_pretty_print/src/comments.rs +++ b/crates/pgls_pretty_print/src/comments.rs @@ -243,6 +243,15 @@ pub(crate) fn first_node_location(ast: &NodeEnum) -> Option { .min() } +/// The location of the node of `ast` that starts last in the source. A trailing comment written +/// after `ast` is attached there. +pub(crate) fn last_node_location(ast: &NodeEnum) -> Option { + ast.iter() + .filter_map(|node| node_location(&node)) + .filter(|location| *location >= 0) + .max() +} + #[cfg(test)] mod tests { use super::*; diff --git a/crates/pgls_pretty_print/src/emitter.rs b/crates/pgls_pretty_print/src/emitter.rs index b758c1355..05d293c35 100644 --- a/crates/pgls_pretty_print/src/emitter.rs +++ b/crates/pgls_pretty_print/src/emitter.rs @@ -203,10 +203,25 @@ impl EventEmitter { /// Emits and consumes comments that follow `location`, if any. pub fn take_trailing_comments_at(&mut self, location: i32) { - let Some(comments) = self.trailing_comments.remove(&location) else { - return; - }; + let comments = self.remove_trailing_comments_at(location); + self.emit_trailing_comments(comments); + } + + /// Consumes the comments that follow `location` without emitting them, so that the caller + /// can emit them somewhere else with `emit_trailing_comments`. + pub fn remove_trailing_comments_at(&mut self, location: i32) -> Vec { + self.trailing_comments.remove(&location).unwrap_or_default() + } + + /// Puts back comments taken with `remove_trailing_comments_at`. + pub fn restore_trailing_comments_at(&mut self, location: i32, comments: Vec) { + if !comments.is_empty() { + self.trailing_comments.insert(location, comments); + } + } + /// Emits comments that follow the node just emitted. + pub fn emit_trailing_comments(&mut self, comments: Vec) { for comment in comments { if comment.after_separator { self.comments_after_separator.push(comment); diff --git a/crates/pgls_pretty_print/src/nodes/common_table_expr.rs b/crates/pgls_pretty_print/src/nodes/common_table_expr.rs index a2019d1cc..6f9f7595e 100644 --- a/crates/pgls_pretty_print/src/nodes/common_table_expr.rs +++ b/crates/pgls_pretty_print/src/nodes/common_table_expr.rs @@ -22,11 +22,8 @@ pub(super) fn emit_common_table_expr(e: &mut EventEmitter, n: &CommonTableExpr) emit_parenthesized_list(e, &n.aliascolnames, super::emit_node); } - if matches!(e.config().layout, crate::Layout::Expanded) { - e.space(); - } else { - e.line(LineType::SoftOrSpace); - } + // `name as (` stays on one line; a body that doesn't fit breaks after the parenthesis. + e.space(); e.token(TokenKind::AS_KW); e.space(); diff --git a/crates/pgls_pretty_print/src/nodes/select_stmt.rs b/crates/pgls_pretty_print/src/nodes/select_stmt.rs index 8424056c5..f4bb92ddf 100644 --- a/crates/pgls_pretty_print/src/nodes/select_stmt.rs +++ b/crates/pgls_pretty_print/src/nodes/select_stmt.rs @@ -4,7 +4,7 @@ use pgls_query::{ }; use crate::emitter::{EventEmitter, GroupKind, LineType}; -use crate::{ClauseBodyStyle, Layout, TokenKind}; +use crate::{ClauseBodyStyle, Comment, Layout, TokenKind}; use super::{ node_list::{ @@ -169,7 +169,14 @@ fn emit_select_stmt_impl(e: &mut EventEmitter, n: &SelectStmt, with_semicolon: b } }; + let last_row = n.values_lists.last(); emit_comma_separated_list_with_spacing(e, &n.values_lists, spacing, |row, e| { + // A line comment written after a row is attached to the row's last value. Emitted + // there, it would end up inside the parentheses and force the row to break, so it + // follows the closing parenthesis instead, and the comma when there is one. + let followed_by_comma = !last_row.is_some_and(|last| std::ptr::eq(last, row)); + let comments_after_row = take_comments_after_row(e, row, followed_by_comma); + // Wrap each row tuple in a group so it can break independently e.group_start(GroupKind::List); e.token(TokenKind::L_PAREN); @@ -180,13 +187,11 @@ fn emit_select_stmt_impl(e: &mut EventEmitter, n: &SelectStmt, with_semicolon: b e.line(LineType::Soft); e.token(TokenKind::R_PAREN); e.group_end(); + + e.emit_trailing_comments(comments_after_row); }); e.indent_end(); - - if with_semicolon { - e.token(TokenKind::SEMICOLON); - } } else { e.token(TokenKind::SELECT_KW); @@ -286,81 +291,109 @@ fn emit_select_stmt_impl(e: &mut EventEmitter, n: &SelectStmt, with_semicolon: b } e.indent_end(); } + } - // Emit ORDER BY clause if present - if !n.sort_clause.is_empty() { - super::emit_layout_break(e); - e.token(TokenKind::ORDER_KW); - e.space(); - e.token(TokenKind::BY_KW); - e.space(); - e.indent_start(); - emit_comma_separated_list(e, &n.sort_clause, super::emit_node); - e.indent_end(); - } - - match n.limit_option() { - LimitOption::WithTies => { - if let Some(ref limit_offset) = n.limit_offset { - super::emit_layout_break(e); - e.token(TokenKind::OFFSET_KW); - e.space(); - super::emit_node(limit_offset, e); - e.space(); - e.token(TokenKind::ROWS_KW); - } + // ORDER BY, LIMIT and locking follow a select list and a VALUES list alike. + if !n.sort_clause.is_empty() { + super::emit_layout_break(e); + e.token(TokenKind::ORDER_KW); + e.space(); + e.token(TokenKind::BY_KW); + e.space(); + e.indent_start(); + emit_comma_separated_list(e, &n.sort_clause, super::emit_node); + e.indent_end(); + } - if let Some(ref limit_count) = n.limit_count { - super::emit_layout_break(e); - e.token(TokenKind::FETCH_KW); - e.space(); - e.token(TokenKind::FIRST_KW); - e.space(); - super::emit_node(limit_count, e); - e.space(); - e.token(TokenKind::ROWS_KW); - e.space(); - e.token(TokenKind::WITH_KW); - e.space(); - emit_keyword(e, "TIES"); - } + match n.limit_option() { + LimitOption::WithTies => { + if let Some(ref limit_offset) = n.limit_offset { + super::emit_layout_break(e); + e.token(TokenKind::OFFSET_KW); + e.space(); + super::emit_node(limit_offset, e); + e.space(); + e.token(TokenKind::ROWS_KW); } - _ => { - if let Some(ref limit_count) = n.limit_count { - super::emit_layout_break(e); - e.token(TokenKind::LIMIT_KW); - e.space(); - super::emit_node(limit_count, e); - } - if let Some(ref limit_offset) = n.limit_offset { - super::emit_layout_break(e); - e.token(TokenKind::OFFSET_KW); - e.space(); - super::emit_node(limit_offset, e); - } + if let Some(ref limit_count) = n.limit_count { + super::emit_layout_break(e); + e.token(TokenKind::FETCH_KW); + e.space(); + e.token(TokenKind::FIRST_KW); + e.space(); + super::emit_node(limit_count, e); + e.space(); + e.token(TokenKind::ROWS_KW); + e.space(); + e.token(TokenKind::WITH_KW); + e.space(); + emit_keyword(e, "TIES"); } } + _ => { + if let Some(ref limit_count) = n.limit_count { + super::emit_layout_break(e); + e.token(TokenKind::LIMIT_KW); + e.space(); + super::emit_node(limit_count, e); + } - if !n.locking_clause.is_empty() { - for locking in &n.locking_clause { - if let Some(pgls_query::NodeEnum::LockingClause(locking_clause)) = - locking.node.as_ref() - { - super::emit_layout_break(e); - super::emit_locking_clause(e, locking_clause); - } + if let Some(ref limit_offset) = n.limit_offset { + super::emit_layout_break(e); + e.token(TokenKind::OFFSET_KW); + e.space(); + super::emit_node(limit_offset, e); } } + } - if with_semicolon { - e.token(TokenKind::SEMICOLON); + if !n.locking_clause.is_empty() { + for locking in &n.locking_clause { + if let Some(pgls_query::NodeEnum::LockingClause(locking_clause)) = locking.node.as_ref() + { + super::emit_layout_break(e); + super::emit_locking_clause(e, locking_clause); + } } } + if with_semicolon { + e.token(TokenKind::SEMICOLON); + } + e.group_end(); } +/// Removes the line comments written after `row`, which are attached to its last value, so that +/// they can be emitted after the row. Between two rows, they follow the comma. +fn take_comments_after_row( + e: &mut EventEmitter, + row: &Node, + followed_by_comma: bool, +) -> Vec { + let Some(location) = row + .node + .as_ref() + .and_then(crate::comments::last_node_location) + else { + return Vec::new(); + }; + + let (mut after_row, inside): (Vec<_>, Vec<_>) = e + .remove_trailing_comments_at(location) + .into_iter() + .partition(|comment| comment.line_comment && !comment.own_line); + e.restore_trailing_comments_at(location, inside); + + if followed_by_comma { + for comment in &mut after_row { + comment.after_separator = true; + } + } + after_row +} + fn emit_distinct_clause(e: &mut EventEmitter, clause: &[Node]) { e.space(); e.token(TokenKind::DISTINCT_KW); diff --git a/crates/pgls_pretty_print/src/nodes/with_clause.rs b/crates/pgls_pretty_print/src/nodes/with_clause.rs index b1684941d..1aed57bf5 100644 --- a/crates/pgls_pretty_print/src/nodes/with_clause.rs +++ b/crates/pgls_pretty_print/src/nodes/with_clause.rs @@ -1,7 +1,7 @@ use pgls_query::protobuf::WithClause; use crate::TokenKind; -use crate::emitter::{EventEmitter, GroupKind, LineType}; +use crate::emitter::{EventEmitter, GroupKind}; use super::node_list::emit_comma_separated_list; @@ -19,11 +19,8 @@ pub(super) fn emit_with_clause(e: &mut EventEmitter, n: &WithClause) { } if !n.ctes.is_empty() { - if matches!(e.config().layout, crate::Layout::Expanded) { - e.space(); - } else { - e.line(LineType::SoftOrSpace); - } + // The first CTE stays on the WITH line. Its body breaks inside its own parentheses. + e.space(); emit_comma_separated_list(e, &n.ctes, |node, e| { super::emit_node(node, e); }); diff --git a/crates/pgls_pretty_print/tests/data/multi/values_order_by_limit.sql b/crates/pgls_pretty_print/tests/data/multi/values_order_by_limit.sql new file mode 100644 index 000000000..271f37fe9 --- /dev/null +++ b/crates/pgls_pretty_print/tests/data/multi/values_order_by_limit.sql @@ -0,0 +1,13 @@ +values (1, 2), (3, 4) order by 1 limit 1; + +values (1, 'one'), (2, 'two'), (3, 'three') order by 2 desc offset 1; + +values (1), (2) order by 1 fetch first 1 rows only; + +select * from (values (1), (2), (3) order by 1 desc limit 2) as v (x); + +insert into t (a) values (1), (2) on conflict do nothing; + +with v (a, b) as (values (1, 2), (3, 4)) select * from v; + +with numbered_rows_with_a_long_name (id, label) as (values (1, 'first row label'), (2, 'second row label')) select * from numbered_rows_with_a_long_name; diff --git a/crates/pgls_pretty_print/tests/snapshots/multi/tests__brin_100.snap b/crates/pgls_pretty_print/tests/snapshots/multi/tests__brin_100.snap index b9bf41956..e8146b872 100644 --- a/crates/pgls_pretty_print/tests/snapshots/multi/tests__brin_100.snap +++ b/crates/pgls_pretty_print/tests/snapshots/multi/tests__brin_100.snap @@ -1,7 +1,6 @@ --- source: crates/pgls_pretty_print/tests/tests.rs input_file: crates/pgls_pretty_print/tests/data/multi/brin.sql -snapshot_kind: text --- create table brintest ( byteacol bytea, @@ -603,9 +602,7 @@ create table brintest_3 ( d text ); -with -rand_value -as ( +with rand_value as ( select string_agg(fipshash(cast(i as text)), '') as val from generate_series(1, 60) as s (i) ) insert into brintest_3 @@ -621,9 +618,7 @@ drop index brin_test_temp_idx; vacuum brintest_3; -with -rand_value -as ( +with rand_value as ( select string_agg(fipshash(cast(-i as text)), '') as val from generate_series(1, 60) as s (i) ) insert into brintest_3 diff --git a/crates/pgls_pretty_print/tests/snapshots/multi/tests__brin_80.snap b/crates/pgls_pretty_print/tests/snapshots/multi/tests__brin_80.snap index a591acc51..e8a8ff3b5 100644 --- a/crates/pgls_pretty_print/tests/snapshots/multi/tests__brin_80.snap +++ b/crates/pgls_pretty_print/tests/snapshots/multi/tests__brin_80.snap @@ -1,7 +1,6 @@ --- source: crates/pgls_pretty_print/tests/tests.rs input_file: crates/pgls_pretty_print/tests/data/multi/brin.sql -snapshot_kind: text --- create table brintest ( byteacol bytea, @@ -668,9 +667,7 @@ create table brintest_3 ( d text ); -with -rand_value -as ( +with rand_value as ( select string_agg(fipshash(cast(i as text)), '') as val from @@ -689,9 +686,7 @@ drop index brin_test_temp_idx; vacuum brintest_3; -with -rand_value -as ( +with rand_value as ( select string_agg(fipshash(cast(-i as text)), '') as val from diff --git a/crates/pgls_pretty_print/tests/snapshots/multi/tests__collate.icu.utf8_100.snap b/crates/pgls_pretty_print/tests/snapshots/multi/tests__collate.icu.utf8_100.snap index f4c10ccdf..804c90480 100644 --- a/crates/pgls_pretty_print/tests/snapshots/multi/tests__collate.icu.utf8_100.snap +++ b/crates/pgls_pretty_print/tests/snapshots/multi/tests__collate.icu.utf8_100.snap @@ -305,9 +305,7 @@ select x || y from collate_test10; select x, y from collate_test10 order by x || y; -with recursive -foo (x) -as ( +with recursive foo (x) as ( select x from (values ('a' collate "en-x-icu"), ('b')) as t (x) union all select (x || 'c') collate "de-x-icu" from foo where length(x) < 10 diff --git a/crates/pgls_pretty_print/tests/snapshots/multi/tests__collate.icu.utf8_80.snap b/crates/pgls_pretty_print/tests/snapshots/multi/tests__collate.icu.utf8_80.snap index aa9babe80..78189c717 100644 --- a/crates/pgls_pretty_print/tests/snapshots/multi/tests__collate.icu.utf8_80.snap +++ b/crates/pgls_pretty_print/tests/snapshots/multi/tests__collate.icu.utf8_80.snap @@ -381,9 +381,7 @@ select x || y from collate_test10; select x, y from collate_test10 order by x || y; -with recursive -foo (x) -as ( +with recursive foo (x) as ( select x from (values ('a' collate "en-x-icu"), ('b')) as t (x) union all select (x || 'c') collate "de-x-icu" from foo where length(x) < 10 diff --git a/crates/pgls_pretty_print/tests/snapshots/multi/tests__collate.linux.utf8_100.snap b/crates/pgls_pretty_print/tests/snapshots/multi/tests__collate.linux.utf8_100.snap index 84efe5712..4b0244293 100644 --- a/crates/pgls_pretty_print/tests/snapshots/multi/tests__collate.linux.utf8_100.snap +++ b/crates/pgls_pretty_print/tests/snapshots/multi/tests__collate.linux.utf8_100.snap @@ -323,9 +323,7 @@ select x || y from collate_test10; select x, y from collate_test10 order by x || y; -with recursive -foo (x) -as ( +with recursive foo (x) as ( select x from (values ('a' collate "en_US"), ('b')) as t (x) union all select (x || 'c') collate "de_DE" from foo where length(x) < 10 diff --git a/crates/pgls_pretty_print/tests/snapshots/multi/tests__collate.linux.utf8_80.snap b/crates/pgls_pretty_print/tests/snapshots/multi/tests__collate.linux.utf8_80.snap index 7f2010702..87b8279ba 100644 --- a/crates/pgls_pretty_print/tests/snapshots/multi/tests__collate.linux.utf8_80.snap +++ b/crates/pgls_pretty_print/tests/snapshots/multi/tests__collate.linux.utf8_80.snap @@ -379,9 +379,7 @@ select x || y from collate_test10; select x, y from collate_test10 order by x || y; -with recursive -foo (x) -as ( +with recursive foo (x) as ( select x from (values ('a' collate "en_US"), ('b')) as t (x) union all select (x || 'c') collate "de_DE" from foo where length(x) < 10 diff --git a/crates/pgls_pretty_print/tests/snapshots/multi/tests__collate_100.snap b/crates/pgls_pretty_print/tests/snapshots/multi/tests__collate_100.snap index 0ea412e2e..ebf4bd788 100644 --- a/crates/pgls_pretty_print/tests/snapshots/multi/tests__collate_100.snap +++ b/crates/pgls_pretty_print/tests/snapshots/multi/tests__collate_100.snap @@ -179,9 +179,7 @@ select x || y from collate_test10; select x, y from collate_test10 order by x || y; -with recursive -foo (x) -as ( +with recursive foo (x) as ( select x from (values ('a' collate "C"), ('b')) as t (x) union all select (x || 'c') collate "POSIX" from foo where length(x) < 10 diff --git a/crates/pgls_pretty_print/tests/snapshots/multi/tests__collate_80.snap b/crates/pgls_pretty_print/tests/snapshots/multi/tests__collate_80.snap index de63bcce1..fe14c96c3 100644 --- a/crates/pgls_pretty_print/tests/snapshots/multi/tests__collate_80.snap +++ b/crates/pgls_pretty_print/tests/snapshots/multi/tests__collate_80.snap @@ -232,9 +232,7 @@ select x || y from collate_test10; select x, y from collate_test10 order by x || y; -with recursive -foo (x) -as ( +with recursive foo (x) as ( select x from (values ('a' collate "C"), ('b')) as t (x) union all select (x || 'c') collate "POSIX" from foo where length(x) < 10 diff --git a/crates/pgls_pretty_print/tests/snapshots/multi/tests__conversion_100.snap b/crates/pgls_pretty_print/tests/snapshots/multi/tests__conversion_100.snap index 04614b40e..7233cbbe2 100644 --- a/crates/pgls_pretty_print/tests/snapshots/multi/tests__conversion_100.snap +++ b/crates/pgls_pretty_print/tests/snapshots/multi/tests__conversion_100.snap @@ -104,9 +104,7 @@ values select description, (test_conv(inbytes, 'utf8', 'utf8')).* from utf8_verification_inputs; -with -test_bytes -as ( +with test_bytes as ( select inbytes, description, @@ -114,8 +112,7 @@ as ( from utf8_verification_inputs ), -test_padded -as ( +test_padded as ( select description, (test_conv(inbytes || cast(repeat('.', 64) as bytea), 'utf8', 'utf8')).error @@ -132,9 +129,7 @@ where p.error is distinct from b.error order by description; -with -test_bytes -as ( +with test_bytes as ( select inbytes, description, @@ -142,8 +137,7 @@ as ( from utf8_verification_inputs ), -test_padded -as ( +test_padded as ( select description, (test_conv( @@ -164,9 +158,7 @@ where p.error is distinct from b.error order by description; -with -test_bytes -as ( +with test_bytes as ( select inbytes, description, @@ -174,8 +166,7 @@ as ( from utf8_verification_inputs ), -test_padded -as ( +test_padded as ( select description, (test_conv( @@ -196,9 +187,7 @@ where p.error is distinct from b.error order by description; -with -test_bytes -as ( +with test_bytes as ( select inbytes, description, @@ -206,8 +195,7 @@ as ( from utf8_verification_inputs ), -test_padded -as ( +test_padded as ( select description, (test_conv( diff --git a/crates/pgls_pretty_print/tests/snapshots/multi/tests__conversion_80.snap b/crates/pgls_pretty_print/tests/snapshots/multi/tests__conversion_80.snap index 82fc9ff66..0b38cf6ae 100644 --- a/crates/pgls_pretty_print/tests/snapshots/multi/tests__conversion_80.snap +++ b/crates/pgls_pretty_print/tests/snapshots/multi/tests__conversion_80.snap @@ -120,9 +120,7 @@ select from utf8_verification_inputs; -with -test_bytes -as ( +with test_bytes as ( select inbytes, description, @@ -130,8 +128,7 @@ as ( from utf8_verification_inputs ), -test_padded -as ( +test_padded as ( select description, (test_conv(inbytes || cast(repeat('.', 64) as bytea), 'utf8', 'utf8')).error @@ -148,9 +145,7 @@ where p.error is distinct from b.error order by description; -with -test_bytes -as ( +with test_bytes as ( select inbytes, description, @@ -158,8 +153,7 @@ as ( from utf8_verification_inputs ), -test_padded -as ( +test_padded as ( select description, (test_conv( @@ -181,9 +175,7 @@ where p.error is distinct from b.error order by description; -with -test_bytes -as ( +with test_bytes as ( select inbytes, description, @@ -191,8 +183,7 @@ as ( from utf8_verification_inputs ), -test_padded -as ( +test_padded as ( select description, (test_conv( @@ -214,9 +205,7 @@ where p.error is distinct from b.error order by description; -with -test_bytes -as ( +with test_bytes as ( select inbytes, description, @@ -224,8 +213,7 @@ as ( from utf8_verification_inputs ), -test_padded -as ( +test_padded as ( select description, (test_conv( diff --git a/crates/pgls_pretty_print/tests/snapshots/multi/tests__float4_100.snap b/crates/pgls_pretty_print/tests/snapshots/multi/tests__float4_100.snap index 373f9f534..dc3e2f786 100644 --- a/crates/pgls_pretty_print/tests/snapshots/multi/tests__float4_100.snap +++ b/crates/pgls_pretty_print/tests/snapshots/multi/tests__float4_100.snap @@ -206,9 +206,7 @@ create cast (xfloat4 as int) without function; create cast (int as xfloat4) without function; -with -testdata (bits) -as ( +with testdata (bits) as ( values (X'00000001'), (X'00000002'), @@ -238,9 +236,7 @@ select from (select cast(cast(cast(bits as int) as xfloat4) as float4) as flt from testdata offset 0) as s; -with -testdata (bits) -as ( +with testdata (bits) as ( values (X'00000000'), (X'00800000'), diff --git a/crates/pgls_pretty_print/tests/snapshots/multi/tests__float4_80.snap b/crates/pgls_pretty_print/tests/snapshots/multi/tests__float4_80.snap index 798050ab6..ac54a521b 100644 --- a/crates/pgls_pretty_print/tests/snapshots/multi/tests__float4_80.snap +++ b/crates/pgls_pretty_print/tests/snapshots/multi/tests__float4_80.snap @@ -206,9 +206,7 @@ create cast (xfloat4 as int) without function; create cast (int as xfloat4) without function; -with -testdata (bits) -as ( +with testdata (bits) as ( values (X'00000001'), (X'00000002'), @@ -245,9 +243,7 @@ from ) as s; -with -testdata (bits) -as ( +with testdata (bits) as ( values (X'00000000'), (X'00800000'), diff --git a/crates/pgls_pretty_print/tests/snapshots/multi/tests__float8_100.snap b/crates/pgls_pretty_print/tests/snapshots/multi/tests__float8_100.snap index 34fad7525..359895cf8 100644 --- a/crates/pgls_pretty_print/tests/snapshots/multi/tests__float8_100.snap +++ b/crates/pgls_pretty_print/tests/snapshots/multi/tests__float8_100.snap @@ -447,9 +447,7 @@ create cast (xfloat8 as bigint) without function; create cast (bigint as xfloat8) without function; -with -testdata (bits) -as ( +with testdata (bits) as ( values (X'0000000000000001'), (X'0000000000000002'), @@ -476,9 +474,7 @@ select from (select cast(cast(cast(bits as bigint) as xfloat8) as float8) as flt from testdata offset 0) as s; -with -testdata (bits) -as ( +with testdata (bits) as ( values (X'0000000000000000'), (X'0010000000000000'), diff --git a/crates/pgls_pretty_print/tests/snapshots/multi/tests__float8_80.snap b/crates/pgls_pretty_print/tests/snapshots/multi/tests__float8_80.snap index 1e8c0aa20..617241788 100644 --- a/crates/pgls_pretty_print/tests/snapshots/multi/tests__float8_80.snap +++ b/crates/pgls_pretty_print/tests/snapshots/multi/tests__float8_80.snap @@ -475,9 +475,7 @@ create cast (xfloat8 as bigint) without function; create cast (bigint as xfloat8) without function; -with -testdata (bits) -as ( +with testdata (bits) as ( values (X'0000000000000001'), (X'0000000000000002'), @@ -511,9 +509,7 @@ from ) as s; -with -testdata (bits) -as ( +with testdata (bits) as ( values (X'0000000000000000'), (X'0010000000000000'), diff --git a/crates/pgls_pretty_print/tests/snapshots/multi/tests__inherit_100.snap b/crates/pgls_pretty_print/tests/snapshots/multi/tests__inherit_100.snap index 8d316762e..4a8de0e45 100644 --- a/crates/pgls_pretty_print/tests/snapshots/multi/tests__inherit_100.snap +++ b/crates/pgls_pretty_print/tests/snapshots/multi/tests__inherit_100.snap @@ -828,9 +828,7 @@ alter table inht1 rename column aaa to aaaa; alter table inht1 rename column b to bb; -with recursive -r -as ( +with recursive r as ( select cast('inht1' as regclass) as inhrelid union all select c.inhrelid from pg_inherits as c, r where r.inhrelid = c.inhparent diff --git a/crates/pgls_pretty_print/tests/snapshots/multi/tests__inherit_80.snap b/crates/pgls_pretty_print/tests/snapshots/multi/tests__inherit_80.snap index ca57fc887..3e8a6daba 100644 --- a/crates/pgls_pretty_print/tests/snapshots/multi/tests__inherit_80.snap +++ b/crates/pgls_pretty_print/tests/snapshots/multi/tests__inherit_80.snap @@ -851,9 +851,7 @@ alter table inht1 rename column aaa to aaaa; alter table inht1 rename column b to bb; -with recursive -r -as ( +with recursive r as ( select cast('inht1' as regclass) as inhrelid union all select c.inhrelid from pg_inherits as c, r where r.inhrelid = c.inhparent diff --git a/crates/pgls_pretty_print/tests/snapshots/multi/tests__insert_100.snap b/crates/pgls_pretty_print/tests/snapshots/multi/tests__insert_100.snap index 155e49558..48180c511 100644 --- a/crates/pgls_pretty_print/tests/snapshots/multi/tests__insert_100.snap +++ b/crates/pgls_pretty_print/tests/snapshots/multi/tests__insert_100.snap @@ -594,9 +594,7 @@ alter table mlparted attach partition mlparted4 for values from (1, 30) to (1, 40); -with -ins (a, b, c) -as ( +with ins (a, b, c) as ( insert into mlparted (b, a) select s.a, 1 from generate_series(2, 39) as s (a) returning cast(tableoid as regclass), diff --git a/crates/pgls_pretty_print/tests/snapshots/multi/tests__insert_80.snap b/crates/pgls_pretty_print/tests/snapshots/multi/tests__insert_80.snap index 9f32c450c..9da548185 100644 --- a/crates/pgls_pretty_print/tests/snapshots/multi/tests__insert_80.snap +++ b/crates/pgls_pretty_print/tests/snapshots/multi/tests__insert_80.snap @@ -649,9 +649,7 @@ alter table mlparted attach partition mlparted4 for values from (1, 30) to (1, 40); -with -ins (a, b, c) -as ( +with ins (a, b, c) as ( insert into mlparted (b, a) select s.a, 1 from generate_series(2, 39) as s (a) returning cast(tableoid as regclass), diff --git a/crates/pgls_pretty_print/tests/snapshots/multi/tests__jsonb_jsonpath_100.snap b/crates/pgls_pretty_print/tests/snapshots/multi/tests__jsonb_jsonpath_100.snap index 6ea6aa884..8e941d0ed 100644 --- a/crates/pgls_pretty_print/tests/snapshots/multi/tests__jsonb_jsonpath_100.snap +++ b/crates/pgls_pretty_print/tests/snapshots/multi/tests__jsonb_jsonpath_100.snap @@ -1,7 +1,6 @@ --- source: crates/pgls_pretty_print/tests/tests.rs input_file: crates/pgls_pretty_print/tests/data/multi/jsonb_jsonpath.sql -snapshot_kind: text --- select cast('{"a": 12}' as jsonb) @? '$'; @@ -2101,9 +2100,7 @@ select jsonb_path_match('[{"a": 1}]', '$undefined_var'); select jsonb_path_match('[{"a": 1}]', 'false'); -with -str (j, num) -as ( +with str (j, num) as ( select jsonb_build_object('s', s), num diff --git a/crates/pgls_pretty_print/tests/snapshots/multi/tests__jsonb_jsonpath_80.snap b/crates/pgls_pretty_print/tests/snapshots/multi/tests__jsonb_jsonpath_80.snap index f1311ee40..a5df3e8df 100644 --- a/crates/pgls_pretty_print/tests/snapshots/multi/tests__jsonb_jsonpath_80.snap +++ b/crates/pgls_pretty_print/tests/snapshots/multi/tests__jsonb_jsonpath_80.snap @@ -1,7 +1,6 @@ --- source: crates/pgls_pretty_print/tests/tests.rs input_file: crates/pgls_pretty_print/tests/data/multi/jsonb_jsonpath.sql -snapshot_kind: text --- select cast('{"a": 12}' as jsonb) @? '$'; @@ -2376,9 +2375,7 @@ select jsonb_path_match('[{"a": 1}]', '$undefined_var'); select jsonb_path_match('[{"a": 1}]', 'false'); -with -str (j, num) -as ( +with str (j, num) as ( select jsonb_build_object('s', s), num diff --git a/crates/pgls_pretty_print/tests/snapshots/multi/tests__merge_100.snap b/crates/pgls_pretty_print/tests/snapshots/multi/tests__merge_100.snap index a704dbb87..d02db3777 100644 --- a/crates/pgls_pretty_print/tests/snapshots/multi/tests__merge_100.snap +++ b/crates/pgls_pretty_print/tests/snapshots/multi/tests__merge_100.snap @@ -647,9 +647,7 @@ insert into sq_target_merge_log values (1, 'Original value'); begin; -with -m -as ( +with m as ( merge into sq_target as t using sq_source as s on tid = sid @@ -667,8 +665,7 @@ as ( end as description ), -m2 -as ( +m2 as ( merge into sq_target_merge_log as l using m on l.tid = m.tid diff --git a/crates/pgls_pretty_print/tests/snapshots/multi/tests__merge_80.snap b/crates/pgls_pretty_print/tests/snapshots/multi/tests__merge_80.snap index 353144609..5440097f5 100644 --- a/crates/pgls_pretty_print/tests/snapshots/multi/tests__merge_80.snap +++ b/crates/pgls_pretty_print/tests/snapshots/multi/tests__merge_80.snap @@ -682,9 +682,7 @@ insert into sq_target_merge_log values (1, 'Original value'); begin; -with -m -as ( +with m as ( merge into sq_target as t using sq_source as s on tid = sid @@ -704,8 +702,7 @@ as ( end as description ), -m2 -as ( +m2 as ( merge into sq_target_merge_log as l using m on l.tid = m.tid diff --git a/crates/pgls_pretty_print/tests/snapshots/multi/tests__numeric_100.snap b/crates/pgls_pretty_print/tests/snapshots/multi/tests__numeric_100.snap index c6a0d7c2f..63659de9d 100644 --- a/crates/pgls_pretty_print/tests/snapshots/multi/tests__numeric_100.snap +++ b/crates/pgls_pretty_print/tests/snapshots/multi/tests__numeric_100.snap @@ -1,7 +1,6 @@ --- source: crates/pgls_pretty_print/tests/tests.rs input_file: crates/pgls_pretty_print/tests/data/multi/numeric.sql -snapshot_kind: text --- create table num_data ( id int4, @@ -1260,9 +1259,7 @@ select div(cast('nan' as numeric), '0'); select div(cast('0' as numeric), '0'); -with -v (x) -as ( +with v (x) as ( values (cast('0' as numeric)), ('1'), ('-1'), ('4.2'), ('-7.777'), ('inf'), ('-inf'), ('nan') ) select @@ -1276,9 +1273,7 @@ select from v; -with -v (x) -as ( +with v (x) as ( values (cast('0' as numeric)), ('1'), ('-1'), ('4.2'), ('-7.777'), ('inf'), ('-inf'), ('nan') ) select @@ -1290,9 +1285,7 @@ select from v; -with -v (x) -as ( +with v (x) as ( values (cast('0' as numeric)), ('1'), @@ -1758,9 +1751,7 @@ select to_char(val, '9.999EEEE') from num_data; select to_char(val, 'FMRN') from num_data; -with -v (val) -as ( +with v (val) as ( values (cast('0' as numeric)), ('-4.2'), ('4.2e9'), ('1.2e-5'), ('inf'), ('-inf'), ('nan') ) select @@ -1771,9 +1762,7 @@ select from v; -with -v (exp) -as ( +with v (exp) as ( values (-16379), (-16378), @@ -1804,9 +1793,7 @@ select from v; -with -v (val) -as ( +with v (val) as ( values (cast('0' as numeric)), ('-4.2'), ('4.2e9'), ('1.2e-5'), ('inf'), ('-inf'), ('nan') ) select @@ -1817,9 +1804,7 @@ select from v; -with -v (val) -as ( +with v (val) as ( values (cast('0' as numeric)), ('-4.2'), ('4.2e9'), ('1.2e-5'), ('inf'), ('-inf'), ('nan') ) select diff --git a/crates/pgls_pretty_print/tests/snapshots/multi/tests__numeric_80.snap b/crates/pgls_pretty_print/tests/snapshots/multi/tests__numeric_80.snap index 9d88e06a6..482108e72 100644 --- a/crates/pgls_pretty_print/tests/snapshots/multi/tests__numeric_80.snap +++ b/crates/pgls_pretty_print/tests/snapshots/multi/tests__numeric_80.snap @@ -1,7 +1,6 @@ --- source: crates/pgls_pretty_print/tests/tests.rs input_file: crates/pgls_pretty_print/tests/data/multi/numeric.sql -snapshot_kind: text --- create table num_data ( id int4, @@ -1255,9 +1254,7 @@ from where t1.id1 = t2.id and t1.result <> t2.expected; -with -v (x) -as ( +with v (x) as ( values (cast('0' as numeric)), ('1'), @@ -1277,9 +1274,7 @@ from v as v1 (x1), v as v2 (x2); -with -v (x) -as ( +with v (x) as ( values (cast('0' as numeric)), ('1'), @@ -1325,9 +1320,7 @@ select div(cast('nan' as numeric), '0'); select div(cast('0' as numeric), '0'); -with -v (x) -as ( +with v (x) as ( values (cast('0' as numeric)), ('1'), @@ -1349,9 +1342,7 @@ select from v; -with -v (x) -as ( +with v (x) as ( values (cast('0' as numeric)), ('1'), @@ -1371,9 +1362,7 @@ select from v; -with -v (x) -as ( +with v (x) as ( values (cast('0' as numeric)), ('1'), @@ -1444,9 +1433,7 @@ select log(cast('inf' as numeric), '-inf'); select log(cast('-inf' as numeric), 'inf'); -with -v (x) -as ( +with v (x) as ( values (cast('0' as numeric)), ('1'), ('2'), ('4.2'), ('inf'), ('nan') ) select @@ -1879,9 +1866,7 @@ select to_char(val, '9.999EEEE') from num_data; select to_char(val, 'FMRN') from num_data; -with -v (val) -as ( +with v (val) as ( values (cast('0' as numeric)), ('-4.2'), @@ -1899,9 +1884,7 @@ select from v; -with -v (exp) -as ( +with v (exp) as ( values (-16379), (-16378), @@ -1932,9 +1915,7 @@ select from v; -with -v (val) -as ( +with v (val) as ( values (cast('0' as numeric)), ('-4.2'), @@ -1952,9 +1933,7 @@ select from v; -with -v (val) -as ( +with v (val) as ( values (cast('0' as numeric)), ('-4.2'), diff --git a/crates/pgls_pretty_print/tests/snapshots/multi/tests__numeric_big_100.snap b/crates/pgls_pretty_print/tests/snapshots/multi/tests__numeric_big_100.snap index e28261cce..488354922 100644 --- a/crates/pgls_pretty_print/tests/snapshots/multi/tests__numeric_big_100.snap +++ b/crates/pgls_pretty_print/tests/snapshots/multi/tests__numeric_big_100.snap @@ -1,7 +1,6 @@ --- source: crates/pgls_pretty_print/tests/tests.rs input_file: crates/pgls_pretty_print/tests/data/multi/numeric_big.sql -snapshot_kind: text --- drop table num_data; @@ -3522,9 +3521,7 @@ from where t1.id1 = t2.id and t1.result <> t2.expected; -with -t (b, p, bc_result) -as ( +with t (b, p, bc_result) as ( values (0.084738, -20, 2744326694304960114888.785913), (0.084738, -19, 232548755422013710215.445941), @@ -3577,9 +3574,7 @@ select from t; -with -t (b, p, bc_result) -as ( +with t (b, p, bc_result) as ( values (37.821637, -20, .00000000000000000000000000000002787363175065101), (37.821637, -19, .000000000000000000000000000001054226381944797), @@ -3632,9 +3627,7 @@ select from t; -with -t (b, p, bc_result) -as ( +with t (b, p, bc_result) as ( values (0.06933247, -20.342987, 379149253615977128356318.39406340), (0.06933247, -19.342987, 26287354251852125772450.59436685), @@ -3687,9 +3680,7 @@ select from t; -with -t (b, p, bc_result) -as ( +with t (b, p, bc_result) as ( values (27.234987, -20.230957, .000000000000000000000000000009247064512095633), (27.234987, -19.230957, .0000000000000000000000000002518436817750859), @@ -3742,9 +3733,7 @@ select from t; -with -t (b, p, bc_result) -as ( +with t (b, p, bc_result) as ( values ( 0.12, @@ -3766,9 +3755,7 @@ select from t; -with -t (x, bc_result) -as ( +with t (x, bc_result) as ( values (-20.29837, .000000001529431101152222), (-19.29837, .000000004157424770142192), @@ -3820,9 +3807,7 @@ select from t; -with -t (x, bc_result) -as ( +with t (x, bc_result) as ( values ('1.0e-1', -2.3025850929940457), ('1.0e-2', -4.6051701859880914), @@ -3873,9 +3858,7 @@ select from t; -with -t (x, bc_result) -as ( +with t (x, bc_result) as ( values ('1.0e-1', -.10536051565782630), ('1.0e-2', -.010050335853501441), @@ -3926,9 +3909,7 @@ select from t; -with -t (x, bc_result) -as ( +with t (x, bc_result) as ( values ('1.0e-1', .09531017980432486), ('1.0e-2', .009950330853168083), @@ -3979,9 +3960,7 @@ select from t; -with -t (x, bc_result) -as ( +with t (x, bc_result) as ( values ('1.0e1', 2.3025850929940457), ('1.0e2', 4.6051701859880914), @@ -4032,9 +4011,7 @@ select from t; -with -t (x, bc_result) -as ( +with t (x, bc_result) as ( values ('1.0e100', 230.25850929940457), ('1.0e200', 460.51701859880914), @@ -4055,9 +4032,7 @@ select from t; -with -t (x, bc_result) -as ( +with t (x, bc_result) as ( values ( 484990182159328900690402236933516249572671683638747490717351807610531884491845416923860371219625151551889257298200816555016472471293780254009492949585031653913930735918829139712249577547959394351523545901788627247613322896296041868431769047433229466634098452564756860190085118463828382895145244362033728480588969626012192733802377468089120757046364393407262957242230928854711898925295251902007136232994524624903257456111389508582206404271734668422903183500589303866613158037169610592539145461637447957948521714058034772237111009429638870236361143304703683377693378577075353794118557951847394763531830696578809001981568860219578880229402696449243344235099860421846016326538272155937175661905904288335499593232232926636205909086901191153907183842087577811871344870731324067822883041265129394268082883745408414994.8967939438561591657171240282983703914075472645212002662497023142663831371447287624846942598424990784971781730103682951722370983277124599054059027055336437808366784501932987082321905202623642371063626378290734289114618092750984153422293450048717769065428713836637664433167768445609659527458911187829232316677137895259433038764404970599325009178297626038331436654541552998098529141205301472138026818453893127265938030066392881979113522757891639646670670272542401773230506961559808927249585675430838495658225557294666522469887436551840596777627408780618586500922973500018513068499587683746133637919751545157547095670767246977244726331271787622126889459658539988980096764323712767863722912919120929339399753431689512753214200090670880647731689804555417871258907716687575767185444541243606329768784843125926070743277339790277626515824924290352180761378846035233155198504033292692893297993698953705472933411199778880561376633444249703838589180474329586470353212010427945060694794274109764269805332803290229, @@ -4084,9 +4059,7 @@ select from t; -with -t (x, bc_result) -as ( +with t (x, bc_result) as ( values ('9.0e-1', -.04575749056067513), ('6.0e-1', -.2218487496163564), @@ -4121,9 +4094,7 @@ select from t; -with -t (x, bc_result) -as ( +with t (x, bc_result) as ( values ('9.0e-1', -1.0000000000000000), ('6.0e-1', -.3979400086720376), @@ -4152,9 +4123,7 @@ select from t; -with -t (x, bc_result) -as ( +with t (x, bc_result) as ( values ('9.0e-1', .2787536009528290), ('6.0e-1', .2041199826559248), @@ -4190,9 +4159,7 @@ select from t; -with -t (x, bc_result) -as ( +with t (x, bc_result) as ( values ('2.0e10', 10.301029995663981), ('5.0e10', 10.698970004336019), diff --git a/crates/pgls_pretty_print/tests/snapshots/multi/tests__numeric_big_80.snap b/crates/pgls_pretty_print/tests/snapshots/multi/tests__numeric_big_80.snap index a2f3e640f..a2ad2b30d 100644 --- a/crates/pgls_pretty_print/tests/snapshots/multi/tests__numeric_big_80.snap +++ b/crates/pgls_pretty_print/tests/snapshots/multi/tests__numeric_big_80.snap @@ -1,7 +1,6 @@ --- source: crates/pgls_pretty_print/tests/tests.rs input_file: crates/pgls_pretty_print/tests/data/multi/numeric_big.sql -snapshot_kind: text --- drop table num_data; @@ -3561,9 +3560,7 @@ from where t1.id1 = t2.id and t1.result <> t2.expected; -with -t (b, p, bc_result) -as ( +with t (b, p, bc_result) as ( values (0.084738, -20, 2744326694304960114888.785913), (0.084738, -19, 232548755422013710215.445941), @@ -3616,9 +3613,7 @@ select from t; -with -t (b, p, bc_result) -as ( +with t (b, p, bc_result) as ( values (37.821637, -20, .00000000000000000000000000000002787363175065101), (37.821637, -19, .000000000000000000000000000001054226381944797), @@ -3671,9 +3666,7 @@ select from t; -with -t (b, p, bc_result) -as ( +with t (b, p, bc_result) as ( values (0.06933247, -20.342987, 379149253615977128356318.39406340), (0.06933247, -19.342987, 26287354251852125772450.59436685), @@ -3726,9 +3719,7 @@ select from t; -with -t (b, p, bc_result) -as ( +with t (b, p, bc_result) as ( values (27.234987, -20.230957, .000000000000000000000000000009247064512095633), (27.234987, -19.230957, .0000000000000000000000000002518436817750859), @@ -3781,9 +3772,7 @@ select from t; -with -t (b, p, bc_result) -as ( +with t (b, p, bc_result) as ( values ( 0.12, @@ -3805,9 +3794,7 @@ select from t; -with -t (x, bc_result) -as ( +with t (x, bc_result) as ( values (-20.29837, .000000001529431101152222), (-19.29837, .000000004157424770142192), @@ -3859,9 +3846,7 @@ select from t; -with -t (x, bc_result) -as ( +with t (x, bc_result) as ( values ('1.0e-1', -2.3025850929940457), ('1.0e-2', -4.6051701859880914), @@ -3912,9 +3897,7 @@ select from t; -with -t (x, bc_result) -as ( +with t (x, bc_result) as ( values ('1.0e-1', -.10536051565782630), ('1.0e-2', -.010050335853501441), @@ -3965,9 +3948,7 @@ select from t; -with -t (x, bc_result) -as ( +with t (x, bc_result) as ( values ('1.0e-1', .09531017980432486), ('1.0e-2', .009950330853168083), @@ -4018,9 +3999,7 @@ select from t; -with -t (x, bc_result) -as ( +with t (x, bc_result) as ( values ('1.0e1', 2.3025850929940457), ('1.0e2', 4.6051701859880914), @@ -4071,9 +4050,7 @@ select from t; -with -t (x, bc_result) -as ( +with t (x, bc_result) as ( values ('1.0e100', 230.25850929940457), ('1.0e200', 460.51701859880914), @@ -4094,9 +4071,7 @@ select from t; -with -t (x, bc_result) -as ( +with t (x, bc_result) as ( values ( 484990182159328900690402236933516249572671683638747490717351807610531884491845416923860371219625151551889257298200816555016472471293780254009492949585031653913930735918829139712249577547959394351523545901788627247613322896296041868431769047433229466634098452564756860190085118463828382895145244362033728480588969626012192733802377468089120757046364393407262957242230928854711898925295251902007136232994524624903257456111389508582206404271734668422903183500589303866613158037169610592539145461637447957948521714058034772237111009429638870236361143304703683377693378577075353794118557951847394763531830696578809001981568860219578880229402696449243344235099860421846016326538272155937175661905904288335499593232232926636205909086901191153907183842087577811871344870731324067822883041265129394268082883745408414994.8967939438561591657171240282983703914075472645212002662497023142663831371447287624846942598424990784971781730103682951722370983277124599054059027055336437808366784501932987082321905202623642371063626378290734289114618092750984153422293450048717769065428713836637664433167768445609659527458911187829232316677137895259433038764404970599325009178297626038331436654541552998098529141205301472138026818453893127265938030066392881979113522757891639646670670272542401773230506961559808927249585675430838495658225557294666522469887436551840596777627408780618586500922973500018513068499587683746133637919751545157547095670767246977244726331271787622126889459658539988980096764323712767863722912919120929339399753431689512753214200090670880647731689804555417871258907716687575767185444541243606329768784843125926070743277339790277626515824924290352180761378846035233155198504033292692893297993698953705472933411199778880561376633444249703838589180474329586470353212010427945060694794274109764269805332803290229, @@ -4123,9 +4098,7 @@ select from t; -with -t (x, bc_result) -as ( +with t (x, bc_result) as ( values ('9.0e-1', -.04575749056067513), ('6.0e-1', -.2218487496163564), @@ -4160,9 +4133,7 @@ select from t; -with -t (x, bc_result) -as ( +with t (x, bc_result) as ( values ('9.0e-1', -1.0000000000000000), ('6.0e-1', -.3979400086720376), @@ -4191,9 +4162,7 @@ select from t; -with -t (x, bc_result) -as ( +with t (x, bc_result) as ( values ('9.0e-1', .2787536009528290), ('6.0e-1', .2041199826559248), @@ -4229,9 +4198,7 @@ select from t; -with -t (x, bc_result) -as ( +with t (x, bc_result) as ( values ('2.0e10', 10.301029995663981), ('5.0e10', 10.698970004336019), diff --git a/crates/pgls_pretty_print/tests/snapshots/multi/tests__object_address_100.snap b/crates/pgls_pretty_print/tests/snapshots/multi/tests__object_address_100.snap index c62eec731..efea02597 100644 --- a/crates/pgls_pretty_print/tests/snapshots/multi/tests__object_address_100.snap +++ b/crates/pgls_pretty_print/tests/snapshots/multi/tests__object_address_100.snap @@ -229,9 +229,7 @@ select pg_get_object_address('subscription', '{one}', '{}'); select pg_get_object_address('subscription', '{one,two}', '{}'); -with -objects (type, name, args) -as ( +with objects (type, name, args) as ( values ('table', cast('{addr_nsp, gentable}' as text[]), cast('{}' as text[])), ('table', cast('{addr_nsp, parttable}' as text[]), cast('{}' as text[])), @@ -312,9 +310,7 @@ drop owned by regress_addr_user; drop role regress_addr_user; -with -objects (classid, objid, objsubid) -as ( +with objects (classid, objid, objsubid) as ( values (cast('pg_class' as regclass), 0, 0), (cast('pg_class' as regclass), cast('pg_class' as regclass), 100), diff --git a/crates/pgls_pretty_print/tests/snapshots/multi/tests__object_address_80.snap b/crates/pgls_pretty_print/tests/snapshots/multi/tests__object_address_80.snap index 6dbcae256..76d223476 100644 --- a/crates/pgls_pretty_print/tests/snapshots/multi/tests__object_address_80.snap +++ b/crates/pgls_pretty_print/tests/snapshots/multi/tests__object_address_80.snap @@ -251,9 +251,7 @@ select pg_get_object_address('subscription', '{one}', '{}'); select pg_get_object_address('subscription', '{one,two}', '{}'); -with -objects (type, name, args) -as ( +with objects (type, name, args) as ( values ('table', cast('{addr_nsp, gentable}' as text[]), cast('{}' as text[])), ('table', cast('{addr_nsp, parttable}' as text[]), cast('{}' as text[])), @@ -339,9 +337,7 @@ drop owned by regress_addr_user; drop role regress_addr_user; -with -objects (classid, objid, objsubid) -as ( +with objects (classid, objid, objsubid) as ( values (cast('pg_class' as regclass), 0, 0), (cast('pg_class' as regclass), cast('pg_class' as regclass), 100), diff --git a/crates/pgls_pretty_print/tests/snapshots/multi/tests__opr_sanity_100.snap b/crates/pgls_pretty_print/tests/snapshots/multi/tests__opr_sanity_100.snap index c847522e5..deecb9b84 100644 --- a/crates/pgls_pretty_print/tests/snapshots/multi/tests__opr_sanity_100.snap +++ b/crates/pgls_pretty_print/tests/snapshots/multi/tests__opr_sanity_100.snap @@ -916,9 +916,7 @@ from where d.classoid is null and o1.oid <= 9999; -with -funcdescs -as ( +with funcdescs as ( select p.oid as p_oid, proname, @@ -946,9 +944,7 @@ where oprdesc not like 'deprecated%' and prodesc is distinct from oprdesc; -with -funcdescs -as ( +with funcdescs as ( select p.oid as p_oid, proname, diff --git a/crates/pgls_pretty_print/tests/snapshots/multi/tests__opr_sanity_80.snap b/crates/pgls_pretty_print/tests/snapshots/multi/tests__opr_sanity_80.snap index f97e31f22..069693bf3 100644 --- a/crates/pgls_pretty_print/tests/snapshots/multi/tests__opr_sanity_80.snap +++ b/crates/pgls_pretty_print/tests/snapshots/multi/tests__opr_sanity_80.snap @@ -953,9 +953,7 @@ from where d.classoid is null and o1.oid <= 9999; -with -funcdescs -as ( +with funcdescs as ( select p.oid as p_oid, proname, @@ -983,9 +981,7 @@ where oprdesc not like 'deprecated%' and prodesc is distinct from oprdesc; -with -funcdescs -as ( +with funcdescs as ( select p.oid as p_oid, proname, diff --git a/crates/pgls_pretty_print/tests/snapshots/multi/tests__partition_prune_100.snap b/crates/pgls_pretty_print/tests/snapshots/multi/tests__partition_prune_100.snap index 5316ea204..cd69ce3f1 100644 --- a/crates/pgls_pretty_print/tests/snapshots/multi/tests__partition_prune_100.snap +++ b/crates/pgls_pretty_print/tests/snapshots/multi/tests__partition_prune_100.snap @@ -1966,9 +1966,7 @@ create table part_abc_log ( c bool ); -with -t -as ( +with t as ( merge into part_abc_view as pt using (select stable_one() + 1 as pid) as q inner join part_abc_2 as pt2 on q.pid = pt2.a on pt.a = stable_one() + 2 @@ -1980,9 +1978,7 @@ insert into part_abc_log select * from t returning *; -with -t -as ( +with t as ( merge into part_abc_view as pt using (select stable_one() + 1 as pid) as q inner join part_abc_2 as pt2 on q.pid = pt2.a on pt.a = stable_one() + 2 diff --git a/crates/pgls_pretty_print/tests/snapshots/multi/tests__partition_prune_80.snap b/crates/pgls_pretty_print/tests/snapshots/multi/tests__partition_prune_80.snap index 8495d8189..c4b9845c7 100644 --- a/crates/pgls_pretty_print/tests/snapshots/multi/tests__partition_prune_80.snap +++ b/crates/pgls_pretty_print/tests/snapshots/multi/tests__partition_prune_80.snap @@ -2153,9 +2153,7 @@ create table part_abc_log ( c bool ); -with -t -as ( +with t as ( merge into part_abc_view as pt using (select stable_one() + 1 as pid) as q inner join @@ -2170,9 +2168,7 @@ insert into part_abc_log select * from t returning *; -with -t -as ( +with t as ( merge into part_abc_view as pt using (select stable_one() + 1 as pid) as q inner join diff --git a/crates/pgls_pretty_print/tests/snapshots/multi/tests__rowtypes_100.snap b/crates/pgls_pretty_print/tests/snapshots/multi/tests__rowtypes_100.snap index 57b2e9073..4ca1a9911 100644 --- a/crates/pgls_pretty_print/tests/snapshots/multi/tests__rowtypes_100.snap +++ b/crates/pgls_pretty_print/tests/snapshots/multi/tests__rowtypes_100.snap @@ -637,9 +637,7 @@ from ) as r (a, b); -with -r (a, b) -as materialized ( +with r (a, b) as materialized ( values (1, row(1, 2)), (1, row(null, null)), @@ -655,9 +653,7 @@ select from r; -with -r (a, b) -as materialized ( +with r (a, b) as materialized ( values (1, row(1, 2)), (1, row(null, null)), diff --git a/crates/pgls_pretty_print/tests/snapshots/multi/tests__rowtypes_80.snap b/crates/pgls_pretty_print/tests/snapshots/multi/tests__rowtypes_80.snap index cd056585f..22d2bfc74 100644 --- a/crates/pgls_pretty_print/tests/snapshots/multi/tests__rowtypes_80.snap +++ b/crates/pgls_pretty_print/tests/snapshots/multi/tests__rowtypes_80.snap @@ -687,9 +687,7 @@ from ) as r (a, b); -with -r (a, b) -as materialized ( +with r (a, b) as materialized ( values (1, row(1, 2)), (1, row(null, null)), @@ -705,9 +703,7 @@ select from r; -with -r (a, b) -as materialized ( +with r (a, b) as materialized ( values (1, row(1, 2)), (1, row(null, null)), diff --git a/crates/pgls_pretty_print/tests/snapshots/multi/tests__subselect_80.snap b/crates/pgls_pretty_print/tests/snapshots/multi/tests__subselect_80.snap index 6398ad6d3..dd3e47e68 100644 --- a/crates/pgls_pretty_print/tests/snapshots/multi/tests__subselect_80.snap +++ b/crates/pgls_pretty_print/tests/snapshots/multi/tests__subselect_80.snap @@ -1075,8 +1075,9 @@ from onek as o inner join lateral ( - with recursive - x (a) as (select o.four as a union select a + 1 from x where a < 10) + with recursive x (a) as ( + select o.four as a union select a + 1 from x where a < 10 + ) select * from @@ -1094,8 +1095,9 @@ from onek as o inner join lateral ( - with recursive - x (a) as (select o.four as a union select a + 1 from x where a < 10) + with recursive x (a) as ( + select o.four as a union select a + 1 from x where a < 10 + ) select * from diff --git a/crates/pgls_pretty_print/tests/snapshots/multi/tests__timestamptz_100.snap b/crates/pgls_pretty_print/tests/snapshots/multi/tests__timestamptz_100.snap index d0d35b136..ac88c85dd 100644 --- a/crates/pgls_pretty_print/tests/snapshots/multi/tests__timestamptz_100.snap +++ b/crates/pgls_pretty_print/tests/snapshots/multi/tests__timestamptz_100.snap @@ -1,7 +1,6 @@ --- source: crates/pgls_pretty_print/tests/tests.rs input_file: crates/pgls_pretty_print/tests/data/multi/timestamptz.sql -snapshot_kind: text --- create table timestamptz_tbl (d1 timestamp(2) with time zone); @@ -670,9 +669,7 @@ select make_timestamptz(1973, 7, 15, 8, 15, 55.33, '+2'); select make_timestamptz(1973, 7, 15, 8, 15, 55.33, '-2'); -with -tzs (tz) -as ( +with tzs (tz) as ( values ('+1'), ('+1:'), diff --git a/crates/pgls_pretty_print/tests/snapshots/multi/tests__timestamptz_80.snap b/crates/pgls_pretty_print/tests/snapshots/multi/tests__timestamptz_80.snap index eecf685aa..5edea6dab 100644 --- a/crates/pgls_pretty_print/tests/snapshots/multi/tests__timestamptz_80.snap +++ b/crates/pgls_pretty_print/tests/snapshots/multi/tests__timestamptz_80.snap @@ -1,7 +1,6 @@ --- source: crates/pgls_pretty_print/tests/tests.rs input_file: crates/pgls_pretty_print/tests/data/multi/timestamptz.sql -snapshot_kind: text --- create table timestamptz_tbl (d1 timestamp(2) with time zone); @@ -774,9 +773,7 @@ select make_timestamptz(1973, 7, 15, 8, 15, 55.33, '+2'); select make_timestamptz(1973, 7, 15, 8, 15, 55.33, '-2'); -with -tzs (tz) -as ( +with tzs (tz) as ( values ('+1'), ('+1:'), diff --git a/crates/pgls_pretty_print/tests/snapshots/multi/tests__uuid_100.snap b/crates/pgls_pretty_print/tests/snapshots/multi/tests__uuid_100.snap index ad5b8996e..345165a67 100644 --- a/crates/pgls_pretty_print/tests/snapshots/multi/tests__uuid_100.snap +++ b/crates/pgls_pretty_print/tests/snapshots/multi/tests__uuid_100.snap @@ -1,7 +1,6 @@ --- source: crates/pgls_pretty_print/tests/tests.rs input_file: crates/pgls_pretty_print/tests/data/multi/uuid.sql -snapshot_kind: text --- create table guid1 ( guid_field uuid, @@ -140,9 +139,7 @@ insert into guid3 (guid_field) select uuidv7() from generate_series(1, 10); select array_agg(id order by guid_field) from guid3; -with -uuidts -as ( +with uuidts as ( select y, ts as ts, diff --git a/crates/pgls_pretty_print/tests/snapshots/multi/tests__uuid_80.snap b/crates/pgls_pretty_print/tests/snapshots/multi/tests__uuid_80.snap index 5a0fcd08a..547668456 100644 --- a/crates/pgls_pretty_print/tests/snapshots/multi/tests__uuid_80.snap +++ b/crates/pgls_pretty_print/tests/snapshots/multi/tests__uuid_80.snap @@ -1,7 +1,6 @@ --- source: crates/pgls_pretty_print/tests/tests.rs input_file: crates/pgls_pretty_print/tests/data/multi/uuid.sql -snapshot_kind: text --- create table guid1 ( guid_field uuid, @@ -176,9 +175,7 @@ insert into guid3 (guid_field) select uuidv7() from generate_series(1, 10); select array_agg(id order by guid_field) from guid3; -with -uuidts -as ( +with uuidts as ( select y, ts as ts, diff --git a/crates/pgls_pretty_print/tests/snapshots/multi/tests__values_order_by_limit_100.snap b/crates/pgls_pretty_print/tests/snapshots/multi/tests__values_order_by_limit_100.snap new file mode 100644 index 000000000..6ac0f2bd5 --- /dev/null +++ b/crates/pgls_pretty_print/tests/snapshots/multi/tests__values_order_by_limit_100.snap @@ -0,0 +1,23 @@ +--- +source: crates/pgls_pretty_print/tests/tests.rs +input_file: crates/pgls_pretty_print/tests/data/multi/values_order_by_limit.sql +--- +values (1, 2), (3, 4) order by 1 limit 1; + +values (1, 'one'), (2, 'two'), (3, 'three') order by 2 desc offset 1; + +values (1), (2) order by 1 limit 1; + +select * from (values (1), (2), (3) order by 1 desc limit 2) as v (x); + +insert into t (a) values (1), (2) on conflict do nothing; + +with v (a, b) as (values (1, 2), (3, 4)) select * from v; + +with numbered_rows_with_a_long_name (id, label) as ( + values (1, 'first row label'), (2, 'second row label') +) +select + * +from + numbered_rows_with_a_long_name; diff --git a/crates/pgls_pretty_print/tests/snapshots/multi/tests__values_order_by_limit_80.snap b/crates/pgls_pretty_print/tests/snapshots/multi/tests__values_order_by_limit_80.snap new file mode 100644 index 000000000..6ac0f2bd5 --- /dev/null +++ b/crates/pgls_pretty_print/tests/snapshots/multi/tests__values_order_by_limit_80.snap @@ -0,0 +1,23 @@ +--- +source: crates/pgls_pretty_print/tests/tests.rs +input_file: crates/pgls_pretty_print/tests/data/multi/values_order_by_limit.sql +--- +values (1, 2), (3, 4) order by 1 limit 1; + +values (1, 'one'), (2, 'two'), (3, 'three') order by 2 desc offset 1; + +values (1), (2) order by 1 limit 1; + +select * from (values (1), (2), (3) order by 1 desc limit 2) as v (x); + +insert into t (a) values (1), (2) on conflict do nothing; + +with v (a, b) as (values (1, 2), (3, 4)) select * from v; + +with numbered_rows_with_a_long_name (id, label) as ( + values (1, 'first row label'), (2, 'second row label') +) +select + * +from + numbered_rows_with_a_long_name; diff --git a/crates/pgls_pretty_print/tests/snapshots/multi/tests__window_100.snap b/crates/pgls_pretty_print/tests/snapshots/multi/tests__window_100.snap index 0ccd74208..21f4e84ec 100644 --- a/crates/pgls_pretty_print/tests/snapshots/multi/tests__window_100.snap +++ b/crates/pgls_pretty_print/tests/snapshots/multi/tests__window_100.snap @@ -2632,9 +2632,7 @@ window 1 following ); -with -cte (x) -as ( +with cte (x) as ( select 1 union all select 1 union all select 1 union all select * from generate_series(5, 49, 2) ) select @@ -2651,9 +2649,7 @@ window 1 following ); -with -cte (x) -as ( +with cte (x) as ( select 1 union all select 1 union all select 1 union all select * from generate_series(5, 49, 2) ) select @@ -2670,9 +2666,7 @@ window 1 following ); -with -cte (x) -as ( +with cte (x) as ( select 1 union all select 1 union all select 1 union all select * from generate_series(5, 49, 2) ) select @@ -3591,10 +3585,8 @@ create aggregate sum_int_randomrestart ( minvfunc = sum_int_randrestart_minvfunc ); -with -vs as (select i, cast(random() * 100 as int4) as v from generate_series(1, 100) as i), -sum_following -as ( +with vs as (select i, cast(random() * 100 as int4) as v from generate_series(1, 100) as i), +sum_following as ( select i, sum(v) over (order by i desc rows between unbounded preceding and current row) as s diff --git a/crates/pgls_pretty_print/tests/snapshots/multi/tests__window_80.snap b/crates/pgls_pretty_print/tests/snapshots/multi/tests__window_80.snap index 55633a45e..864a22175 100644 --- a/crates/pgls_pretty_print/tests/snapshots/multi/tests__window_80.snap +++ b/crates/pgls_pretty_print/tests/snapshots/multi/tests__window_80.snap @@ -3042,9 +3042,7 @@ window 1 following ); -with -cte (x) -as ( +with cte (x) as ( select 1 union all select 1 union all select 1 union all select * from generate_series(5, 49, 2) @@ -3063,9 +3061,7 @@ window 1 following ); -with -cte (x) -as ( +with cte (x) as ( select 1 union all select 1 union all select 1 union all select * from generate_series(5, 49, 2) @@ -3084,9 +3080,7 @@ window 1 following ); -with -cte (x) -as ( +with cte (x) as ( select 1 union all select 1 union all select 1 union all select * from generate_series(5, 49, 2) @@ -4183,13 +4177,10 @@ create aggregate sum_int_randomrestart ( minvfunc = sum_int_randrestart_minvfunc ); -with -vs -as ( +with vs as ( select i, cast(random() * 100 as int4) as v from generate_series(1, 100) as i ), -sum_following -as ( +sum_following as ( select i, sum(v) over ( diff --git a/crates/pgls_pretty_print/tests/snapshots/multi/tests__with_100.snap b/crates/pgls_pretty_print/tests/snapshots/multi/tests__with_100.snap index bc08a1045..b4c0fd52d 100644 --- a/crates/pgls_pretty_print/tests/snapshots/multi/tests__with_100.snap +++ b/crates/pgls_pretty_print/tests/snapshots/multi/tests__with_100.snap @@ -1,7 +1,6 @@ --- source: crates/pgls_pretty_print/tests/tests.rs input_file: crates/pgls_pretty_print/tests/data/multi/with.sql -snapshot_kind: text --- with q1 (x, y) as (select 1, 2) select * from q1, q1 as q2; @@ -28,9 +27,7 @@ select from t; -with recursive -t (n) -as ( +with recursive t (n) as ( values (cast('01' as varbit)) union select n || cast('10' as varbit) from t where n < cast('100' as varbit) @@ -86,24 +83,12 @@ select from t; -with recursive -w1 (c1) -as ( - with - w2 (c2) - as ( - with - w3 (c3) - as ( - with - w4 (c4) - as ( - with - w5 (c5) - as ( - with recursive - w6 (c6) - as ( +with recursive w1 (c1) as ( + with w2 (c2) as ( + with w3 (c3) as ( + with w4 (c4) as ( + with w5 (c5) as ( + with recursive w6 (c6) as ( with w6 (c6) as (with w8 (c8) as (select 1) select * from w8) select * from w6 ) select @@ -136,33 +121,24 @@ select from w1; -with recursive -outermost (x) -as ( +with recursive outermost (x) as ( select 1 union - (with - innermost1 - as ( + (with innermost1 as ( select 2 union - (with - innermost2 - as ( + (with innermost2 as ( select 3 union - (with - innermost3 - as ( + (with innermost3 as ( select 4 union - (with - innermost4 - as ( + (with innermost4 as ( select 5 union - (with - innermost5 as (select 6 union (with innermost6 as (select 7) select * from innermost6)) + (with innermost5 as ( + select 6 union (with innermost6 as (select 7) select * from innermost6) + ) select * from @@ -215,9 +191,7 @@ insert into department values (6, 4, 'F'); insert into department values (7, 5, 'G'); -with recursive -subdepartment -as ( +with recursive subdepartment as ( select name as root_name, * from department where name = 'A' union all select @@ -235,9 +209,7 @@ from subdepartment order by name; -with recursive -subdepartment (level, id, parent_department, name) -as ( +with recursive subdepartment (level, id, parent_department, name) as ( select 1, * from department where name = 'A' union all select @@ -255,9 +227,7 @@ from subdepartment order by name; -with recursive -subdepartment (level, id, parent_department, name) -as ( +with recursive subdepartment (level, id, parent_department, name) as ( select 1, * from department where name = 'A' union all select @@ -284,9 +254,7 @@ from subdepartment order by name; -with recursive -subdepartment -as ( +with recursive subdepartment as ( select id, parent_department, name from department where name = 'A' union select @@ -347,21 +315,21 @@ select pg_get_viewdef(cast('vsubdepartment' as regclass), true); select sum(n) from t; -with recursive -q as (select * from department union all (with x as (select * from q) select * from x)) +with recursive q as ( + select * from department union all (with x as (select * from q) select * from x) +) select * from q limit 24; -with recursive -q -as ( +with recursive q as ( select * from department union all - (with recursive - x as (select * from department union all (select * from q union all select * from x)) + (with recursive x as ( + select * from department union all (select * from q union all select * from x) + ) select * from @@ -399,9 +367,7 @@ values (15, 11), (16, 11); -with recursive -t (id, path) -as ( +with recursive t (id, path) as ( values (1, cast(array[] as int[])) union all select tree.id, t.path || tree.id from tree inner join t on tree.parent_id = t.id @@ -417,9 +383,7 @@ from order by t1.id, t2.id; -with recursive -t (id, path) -as ( +with recursive t (id, path) as ( values (1, cast(array[] as int[])) union all select tree.id, t.path || tree.id from tree inner join t on tree.parent_id = t.id @@ -435,9 +399,7 @@ from group by t1.id order by t1.id; -with recursive -t (id, path) -as ( +with recursive t (id, path) as ( values (1, cast(array[] as int[])) union all select tree.id, t.path || tree.id from tree inner join t on tree.parent_id = t.id diff --git a/crates/pgls_pretty_print/tests/snapshots/multi/tests__with_80.snap b/crates/pgls_pretty_print/tests/snapshots/multi/tests__with_80.snap index c9397abd3..5c091c125 100644 --- a/crates/pgls_pretty_print/tests/snapshots/multi/tests__with_80.snap +++ b/crates/pgls_pretty_print/tests/snapshots/multi/tests__with_80.snap @@ -1,7 +1,6 @@ --- source: crates/pgls_pretty_print/tests/tests.rs input_file: crates/pgls_pretty_print/tests/data/multi/with.sql -snapshot_kind: text --- with q1 (x, y) as (select 1, 2) select * from q1, q1 as q2; @@ -22,16 +21,15 @@ select from t; -with recursive -t (n) as (select (values (1)) union all select n + 1 from t where n < 5) +with recursive t (n) as ( + select (values (1)) union all select n + 1 from t where n < 5 +) select * from t; -with recursive -t (n) -as ( +with recursive t (n) as ( values (cast('01' as varbit)) union select n || cast('10' as varbit) from t where n < cast('100' as varbit) @@ -45,8 +43,9 @@ create view nums ( n ) -as with recursive -nums (n) as (values (1) union all select n + 1 from nums where n < 5) +as with recursive nums (n) as ( + values (1) union all select n + 1 from nums where n < 5 +) select n from @@ -58,8 +57,9 @@ create or replace view nums ( n ) -as with recursive -nums (n) as (values (1) union all select n + 1 from nums where n < 6) +as with recursive nums (n) as ( + values (1) union all select n + 1 from nums where n < 6 +) select n from @@ -85,8 +85,9 @@ limit 10; with q as (select 'foo' as x) select x, pg_typeof(x) from q; -with recursive -t (n) as (select 'foo' union all select n || ' bar' from t where length(n) < 20) +with recursive t (n) as ( + select 'foo' union all select n || ' bar' from t where length(n) < 20 +) select n, pg_typeof(n) @@ -100,24 +101,12 @@ select from t; -with recursive -w1 (c1) -as ( - with - w2 (c2) - as ( - with - w3 (c3) - as ( - with - w4 (c4) - as ( - with - w5 (c5) - as ( - with recursive - w6 (c6) - as ( +with recursive w1 (c1) as ( + with w2 (c2) as ( + with w3 (c3) as ( + with w4 (c4) as ( + with w5 (c5) as ( + with recursive w6 (c6) as ( with w6 (c6) as (with w8 (c8) as (select 1) select * from w8) select * @@ -154,34 +143,22 @@ select from w1; -with recursive -outermost (x) -as ( +with recursive outermost (x) as ( select 1 union - (with - innermost1 - as ( + (with innermost1 as ( select 2 union - (with - innermost2 - as ( + (with innermost2 as ( select 3 union - (with - innermost3 - as ( + (with innermost3 as ( select 4 union - (with - innermost4 - as ( + (with innermost4 as ( select 5 union - (with - innermost5 - as ( + (with innermost5 as ( select 6 union (with innermost6 as (select 7) select * from innermost6) @@ -238,9 +215,7 @@ insert into department values (6, 4, 'F'); insert into department values (7, 5, 'G'); -with recursive -subdepartment -as ( +with recursive subdepartment as ( select name as root_name, * from department where name = 'A' union all select @@ -258,9 +233,7 @@ from subdepartment order by name; -with recursive -subdepartment (level, id, parent_department, name) -as ( +with recursive subdepartment (level, id, parent_department, name) as ( select 1, * from department where name = 'A' union all select @@ -278,9 +251,7 @@ from subdepartment order by name; -with recursive -subdepartment (level, id, parent_department, name) -as ( +with recursive subdepartment (level, id, parent_department, name) as ( select 1, * from department where name = 'A' union all select @@ -307,9 +278,7 @@ from subdepartment order by name; -with recursive -subdepartment -as ( +with recursive subdepartment as ( select id, parent_department, name from department where name = 'A' union select @@ -332,8 +301,9 @@ select count(*) from ( - with recursive - t (n) as (select 1 union all select n + 1 from t where n < 500) + with recursive t (n) as ( + select 1 union all select n + 1 from t where n < 500 + ) select * from @@ -347,8 +317,9 @@ where count(*) from ( - with recursive - t (n) as (select 1 union all select n + 1 from t where n < 100) + with recursive t (n) as ( + select 1 union all select n + 1 from t where n < 100 + ) select * from @@ -379,9 +350,7 @@ select pg_get_viewdef(cast('vsubdepartment' as regclass), true); select sum(n) from t; -with recursive -q -as ( +with recursive q as ( select * from department union all (with x as (select * from q) select * from x) @@ -392,14 +361,10 @@ from q limit 24; -with recursive -q -as ( +with recursive q as ( select * from department union all - (with recursive - x - as ( + (with recursive x as ( select * from department union all (select * from q union all select * from x) @@ -441,9 +406,7 @@ values (15, 11), (16, 11); -with recursive -t (id, path) -as ( +with recursive t (id, path) as ( values (1, cast(array[] as int[])) union all select @@ -465,9 +428,7 @@ from order by t1.id, t2.id; -with recursive -t (id, path) -as ( +with recursive t (id, path) as ( values (1, cast(array[] as int[])) union all select @@ -489,9 +450,7 @@ from group by t1.id order by t1.id; -with recursive -t (id, path) -as ( +with recursive t (id, path) as ( values (1, cast(array[] as int[])) union all select diff --git a/crates/pgls_pretty_print/tests/snapshots/multi/tests__xml_100.snap b/crates/pgls_pretty_print/tests/snapshots/multi/tests__xml_100.snap index ed945bb18..f7226749f 100644 --- a/crates/pgls_pretty_print/tests/snapshots/multi/tests__xml_100.snap +++ b/crates/pgls_pretty_print/tests/snapshots/multi/tests__xml_100.snap @@ -1022,9 +1022,7 @@ from premier_name text path 'PREMIER_NAME' default 'not specified' ); -with -x -as ( +with x as ( select proname, proowner, @@ -1038,16 +1036,14 @@ as ( where proname = 'f_leak' ), -y -as ( +y as ( select xmlelement(name proc, xmlforest(proname, proowner, procost, pronargs, proargnames, proargtypes)) as proc from x ), -z -as ( +z as ( select xmltable.* from @@ -1068,9 +1064,7 @@ select * from z except select * from x; -with -x -as ( +with x as ( select proname, proowner, @@ -1082,8 +1076,7 @@ as ( from pg_proc ), -y -as ( +y as ( select xmlelement(name data, xmlagg( xmlelement(name proc, xmlforest( @@ -1099,8 +1092,7 @@ as ( from x ), -z -as ( +z as ( select xmltable.* from diff --git a/crates/pgls_pretty_print/tests/snapshots/multi/tests__xml_80.snap b/crates/pgls_pretty_print/tests/snapshots/multi/tests__xml_80.snap index 45000320f..dd0a7553a 100644 --- a/crates/pgls_pretty_print/tests/snapshots/multi/tests__xml_80.snap +++ b/crates/pgls_pretty_print/tests/snapshots/multi/tests__xml_80.snap @@ -1094,9 +1094,7 @@ from premier_name text path 'PREMIER_NAME' default 'not specified' ); -with -x -as ( +with x as ( select proname, proowner, @@ -1113,8 +1111,7 @@ as ( where proname = 'f_leak' ), -y -as ( +y as ( select xmlelement(name proc, xmlforest( proname, @@ -1128,8 +1125,7 @@ as ( from x ), -z -as ( +z as ( select xmltable.* from @@ -1150,9 +1146,7 @@ select * from z except select * from x; -with -x -as ( +with x as ( select proname, proowner, @@ -1167,8 +1161,7 @@ as ( from pg_proc ), -y -as ( +y as ( select xmlelement(name data, xmlagg( xmlelement(name proc, xmlforest( @@ -1184,8 +1177,7 @@ as ( from x ), -z -as ( +z as ( select xmltable.* from diff --git a/crates/pgls_pretty_print/tests/snapshots/single/tests__complex_select_0_100.snap b/crates/pgls_pretty_print/tests/snapshots/single/tests__complex_select_0_100.snap index 065ba1502..9ae5a2441 100644 --- a/crates/pgls_pretty_print/tests/snapshots/single/tests__complex_select_0_100.snap +++ b/crates/pgls_pretty_print/tests/snapshots/single/tests__complex_select_0_100.snap @@ -2,9 +2,7 @@ source: crates/pgls_pretty_print/tests/tests.rs input_file: crates/pgls_pretty_print/tests/data/single/complex_select_0.sql --- -with recursive -pks_fks -as ( +with recursive pks_fks as ( select cast(contype as text) as contype, conname, @@ -29,8 +27,7 @@ as ( where contype = 'f' ), -views -as ( +views as ( select c.oid as view_id, n.nspname as view_schema, @@ -44,8 +41,7 @@ as ( where c.relkind in ('v', 'm') ), -transform_json -as ( +transform_json as ( select view_id, view_schema, @@ -129,8 +125,7 @@ as ( from views ), -target_entries -as ( +target_entries as ( select view_id, view_schema, @@ -139,8 +134,7 @@ as ( from transform_json ), -results -as ( +results as ( select view_id, view_schema, @@ -151,8 +145,16 @@ as ( from target_entries ), -recursion (view_id, view_schema, view_name, view_column, resorigtbl, resorigcol, is_cycle, path) -as ( +recursion ( + view_id, + view_schema, + view_name, + view_column, + resorigtbl, + resorigcol, + is_cycle, + path +) as ( select r.*, false, array[resorigtbl] from results as r where true union all select @@ -172,8 +174,7 @@ as ( where not is_cycle ), -repeated_references -as ( +repeated_references as ( select view_id, view_schema, diff --git a/crates/pgls_pretty_print/tests/snapshots/single/tests__complex_select_0_80.snap b/crates/pgls_pretty_print/tests/snapshots/single/tests__complex_select_0_80.snap index a057000c5..f0e216728 100644 --- a/crates/pgls_pretty_print/tests/snapshots/single/tests__complex_select_0_80.snap +++ b/crates/pgls_pretty_print/tests/snapshots/single/tests__complex_select_0_80.snap @@ -2,9 +2,7 @@ source: crates/pgls_pretty_print/tests/tests.rs input_file: crates/pgls_pretty_print/tests/data/single/complex_select_0.sql --- -with recursive -pks_fks -as ( +with recursive pks_fks as ( select cast(contype as text) as contype, conname, @@ -35,8 +33,7 @@ as ( where contype = 'f' ), -views -as ( +views as ( select c.oid as view_id, n.nspname as view_schema, @@ -50,8 +47,7 @@ as ( where c.relkind in ('v', 'm') ), -transform_json -as ( +transform_json as ( select view_id, view_schema, @@ -139,8 +135,7 @@ as ( from views ), -target_entries -as ( +target_entries as ( select view_id, view_schema, @@ -149,8 +144,7 @@ as ( from transform_json ), -results -as ( +results as ( select view_id, view_schema, @@ -170,8 +164,7 @@ recursion ( resorigcol, is_cycle, path -) -as ( +) as ( select r.*, false, array[resorigtbl] from results as r where true union all select @@ -191,8 +184,7 @@ as ( where not is_cycle ), -repeated_references -as ( +repeated_references as ( select view_id, view_schema, diff --git a/crates/pgls_pretty_print/tests/snapshots/single/tests__delete_with_cte_returning_0_80.snap b/crates/pgls_pretty_print/tests/snapshots/single/tests__delete_with_cte_returning_0_80.snap index 9a5b550b4..0bfd8a9e4 100644 --- a/crates/pgls_pretty_print/tests/snapshots/single/tests__delete_with_cte_returning_0_80.snap +++ b/crates/pgls_pretty_print/tests/snapshots/single/tests__delete_with_cte_returning_0_80.snap @@ -1,11 +1,8 @@ --- source: crates/pgls_pretty_print/tests/tests.rs input_file: crates/pgls_pretty_print/tests/data/single/delete_with_cte_returning_0.sql -snapshot_kind: text --- -with -stale -as ( +with stale as ( select id from sessions where last_seen < now() - cast('30 days' as interval) ) delete from sessions diff --git a/crates/pgls_pretty_print/tests/values_layout.rs b/crates/pgls_pretty_print/tests/values_layout.rs index a464a169d..2e90e26bb 100644 --- a/crates/pgls_pretty_print/tests/values_layout.rs +++ b/crates/pgls_pretty_print/tests/values_layout.rs @@ -151,3 +151,89 @@ fn expanded_values_rows_preserve_comments() { assert_eq!(output.matches(comment).count(), 1, "{output}"); } } + +#[test] +fn a_comment_after_a_values_row_stays_after_the_row() { + for (comma_style, expected) in [ + ( + CommaStyle::Trailing, + "insert into t\nvalues\n (1, 'a'), -- first row\n (2, 'b');", + ), + ( + CommaStyle::Leading, + "insert into t\nvalues\n (1, 'a') -- first row\n , (2, 'b');", + ), + ] { + let config = FormatConfig { + values_row_layout: Layout::Expanded, + comma_style, + ..Default::default() + }; + for sql in [ + "insert into t values (1, 'a'), -- first row\n(2, 'b');", + "insert into t values (1, 'a') -- first row\n, (2, 'b');", + ] { + assert_eq!(format_idempotently(sql, &config), expected); + } + } +} + +#[test] +fn a_comment_after_the_last_values_row_stays_after_it() { + let config = FormatConfig { + values_row_layout: Layout::Expanded, + ..Default::default() + }; + assert_eq!( + format_idempotently( + "insert into t values (1, 'a'), (2, 'b') -- last row\non conflict do nothing;", + &config + ), + "insert into t\nvalues\n (1, 'a'),\n (2, 'b') -- last row\non conflict do nothing;" + ); +} + +#[test] +fn values_keeps_order_by_and_limit() { + for config in [ + FormatConfig::default(), + FormatConfig { + values_row_layout: Layout::Expanded, + ..Default::default() + }, + ] { + for sql in [ + "values (1, 2), (3, 4) order by 1 limit 1", + "select * from (values (1), (2) limit 1 offset 1) as v", + "values (1), (2) order by 1 fetch first 1 rows with ties", + ] { + let ast = pgls_query::parse(sql).unwrap().into_root().unwrap(); + let output = format_statement(&ast, sql, &config); + assert!(output.is_ok(), "{sql}: {output:?}"); + format_idempotently(sql, &config); + } + } + + assert_eq!( + format_idempotently( + "values (1, 2), (3, 4) order by 1 limit 1", + &FormatConfig::default() + ), + "values (1, 2), (3, 4) order by 1 limit 1;" + ); +} + +#[test] +fn cte_header_stays_on_one_line_with_expanded_values() { + let config = FormatConfig { + values_row_layout: Layout::Expanded, + ..Default::default() + }; + assert_eq!( + format_idempotently( + "with v (a, b) as (values (1, 2), (3, 4)) select * from v", + &config + ), + "with v (a, b) as (\n values\n (1, 2),\n (3, 4)\n)\nselect\n *\nfrom\n v;" + ); +} From a48075df8c8514343dcea5886b8226ad4dfcfca1 Mon Sep 17 00:00:00 2001 From: psteinroe Date: Fri, 23 Oct 2026 14:00:30 +0000 Subject: [PATCH 3/7] fix(pretty_print): align expanded CASE from where it starts --- .../pgls_pretty_print/src/nodes/case_when.rs | 4 + crates/pgls_pretty_print/src/renderer.rs | 51 +++++-- crates/pgls_pretty_print/tests/case_layout.rs | 128 +++++++++++++++++- .../pgls_pretty_print/tests/comment_layout.rs | 6 +- .../single/tests__expanded_case_expr_200.snap | 12 +- docs/features/formatting.md | 24 ++++ 6 files changed, 201 insertions(+), 24 deletions(-) diff --git a/crates/pgls_pretty_print/src/nodes/case_when.rs b/crates/pgls_pretty_print/src/nodes/case_when.rs index 85bd5308e..2d2a45736 100644 --- a/crates/pgls_pretty_print/src/nodes/case_when.rs +++ b/crates/pgls_pretty_print/src/nodes/case_when.rs @@ -10,9 +10,13 @@ pub(super) fn emit_case_when(e: &mut EventEmitter, n: &CaseWhen) { e.token(TokenKind::WHEN_KW); + // A condition that breaks, an AND chain for instance, continues one level deeper than WHEN, so + // that its operators don't read as siblings of WHEN. if let Some(ref expr) = n.expr { e.space(); + e.indent_start(); super::emit_node(expr, e); + e.indent_end(); } let expanded = matches!(e.config().layout, crate::Layout::Expanded); diff --git a/crates/pgls_pretty_print/src/renderer.rs b/crates/pgls_pretty_print/src/renderer.rs index c7c1dbd9c..907dc5755 100644 --- a/crates/pgls_pretty_print/src/renderer.rs +++ b/crates/pgls_pretty_print/src/renderer.rs @@ -44,12 +44,20 @@ impl Default for RenderConfig { } } +/// The column continuation lines align with, from `AlignStart`. +struct Alignment { + column: usize, + /// The indent level when the alignment started. + indent_level: usize, +} + pub struct Renderer { config: RenderConfig, writer: W, current_line_length: usize, indent_level: usize, - alignment_offsets: Vec, + /// Open alignments, innermost last. + alignments: Vec, at_line_start: bool, /// A space written after the last text. It is only written out once more text follows on the /// same line, so a line never ends in whitespace. @@ -63,7 +71,7 @@ impl Renderer { writer, current_line_length: 0, indent_level: 0, - alignment_offsets: Vec::new(), + alignments: Vec::new(), at_line_start: true, pending_space: false, } @@ -116,7 +124,7 @@ impl Renderer { i += 1; } LayoutEvent::AlignEnd => { - self.alignment_offsets.pop(); + self.alignments.pop(); i += 1; } } @@ -185,16 +193,25 @@ impl Renderer { } } + /// Continuation lines start at the column of the innermost alignment, plus one indent for + /// every level opened since. Without an alignment they start at the indent level. fn indentation_width(&self) -> usize { - self.indent_level * self.config.indent_size - + self.alignment_offsets.last().copied().unwrap_or(0) + let indent = self.indent_level * self.config.indent_size; + match self.alignments.last() { + Some(alignment) => (alignment.column + indent) + .saturating_sub(alignment.indent_level * self.config.indent_size), + None => indent, + } } + /// The column is kept with the indent level it was taken at, not as an offset from that + /// level's indent: the text on the line can start left of it, `select case` with the select + /// list already one level in for instance. fn start_alignment(&mut self) { - let offset = self - .effective_line_length() - .saturating_sub(self.indent_level * self.config.indent_size); - self.alignment_offsets.push(offset); + self.alignments.push(Alignment { + column: self.effective_line_length(), + indent_level: self.indent_level, + }); } fn render_events_with_breaks( @@ -262,7 +279,7 @@ impl Renderer { i += 1; } LayoutEvent::AlignEnd => { - self.alignment_offsets.pop(); + self.alignments.pop(); i += 1; } } @@ -465,11 +482,17 @@ impl Renderer { self.current_line_length += spaces.len(); spaces } + // One tab per indent level and spaces for the rest of an alignment. The columns only + // line up when the editor's tab width equals `indent_size`. IndentStyle::Tabs => { - let tabs = "\t".repeat(self.indent_level) - + &" ".repeat(self.alignment_offsets.last().copied().unwrap_or(0)); - self.current_line_length += self.indentation_width(); - tabs + let width = self.indentation_width(); + let tabs = match self.config.indent_size { + 0 => self.indent_level, + size => self.indent_level.min(width / size), + }; + let spaces = width - tabs * self.config.indent_size; + self.current_line_length += width; + "\t".repeat(tabs) + &" ".repeat(spaces) } }; write!(self.writer, "{indent_str}")?; diff --git a/crates/pgls_pretty_print/tests/case_layout.rs b/crates/pgls_pretty_print/tests/case_layout.rs index 942fd3b45..6c8489e4f 100644 --- a/crates/pgls_pretty_print/tests/case_layout.rs +++ b/crates/pgls_pretty_print/tests/case_layout.rs @@ -1,5 +1,5 @@ use pgls_pretty_print::{ - CommaStyle, FormatConfig, Layout, LogicalOperatorPlacement, format_statement, + ClauseBodyStyle, CommaStyle, FormatConfig, Layout, LogicalOperatorPlacement, format_statement, renderer::{IndentStyle, KeywordCase}, }; @@ -123,3 +123,129 @@ fn fit_case_stays_compact_when_it_fits() { assert_eq!(format_idempotently(sql, &FormatConfig::default()), expected); } } + +/// Column of the first non-blank character of the line that starts with `needle`. +fn column_of(output: &str, needle: &str) -> usize { + let line = output + .lines() + .find(|line| line.trim_start().starts_with(needle)) + .unwrap_or_else(|| panic!("no line starting with {needle:?} in\n{output}")); + line.len() - line.trim_start().len() +} + +fn column_of_case(output: &str) -> usize { + output + .lines() + .find_map(|line| line.find("case")) + .expect("a line with case") +} + +#[test] +fn expanded_case_aligns_from_where_it_starts_after_on() { + let config = FormatConfig { + layout: Layout::Expanded, + clause_body_style: ClauseBodyStyle::Compact, + indent_size: 4, + ..Default::default() + }; + let output = format_idempotently( + "select * from t join u on case when t.a then t.b = u.b else t.c = u.c end", + &config, + ); + let case = column_of_case(&output); + assert_eq!(column_of(&output, "when"), case + 4, "{output}"); + assert_eq!(column_of(&output, "then"), case + 8, "{output}"); + assert_eq!(column_of(&output, "else"), case + 4, "{output}"); + assert_eq!(column_of(&output, "end"), case, "{output}"); +} + +#[test] +fn expanded_case_aligns_from_where_it_starts_with_a_wide_indent() { + for indent_style in [IndentStyle::Spaces, IndentStyle::Tabs] { + let config = FormatConfig { + layout: Layout::Expanded, + clause_body_style: ClauseBodyStyle::Compact, + indent_size: 8, + indent_style: indent_style.clone(), + ..Default::default() + }; + let output = format_idempotently("select case when a then 1 else 2 end from t", &config); + let expected = "select case\n when a\n then 1\n else 2\n end\nfrom t;"; + let expected = match indent_style { + IndentStyle::Spaces => expected.to_string(), + // A tab per indent level that fits in the column, spaces for the rest. + IndentStyle::Tabs => { + "select case\n\t when a\n\t\t then 1\n\t else 2\n end\nfrom t;" + .to_string() + } + }; + assert_eq!(output, expected, "{indent_style:?}"); + } +} + +#[test] +fn expanded_case_with_tabs_aligns_with_spaces_after_tabs() { + let config = FormatConfig { + layout: Layout::Expanded, + indent_style: IndentStyle::Tabs, + indent_size: 4, + comma_style: CommaStyle::Leading, + ..Default::default() + }; + let output = format_idempotently("select a, case when b then 1 else 2 end from t", &config); + assert_eq!( + output, + "select\n\ta\n\t, case\n\t\t when b\n\t\t\t then 1\n\t\t else 2\n\t end\nfrom\n\tt;" + ); +} + +#[test] +fn when_condition_continuation_is_indented() { + let config = FormatConfig { + layout: Layout::Expanded, + logical_operator_placement: LogicalOperatorPlacement::Leading, + indent_size: 4, + ..Default::default() + }; + let output = format_idempotently( + "select case when aaaa = 1 and bbbb = 2 then 'x' else 'y' end from t", + &config, + ); + assert_eq!( + column_of(&output, "and"), + column_of(&output, "when") + 4, + "{output}" + ); + + // Fit layout indents the continuation the same way once the condition breaks. + let config = FormatConfig { + line_width: 40, + ..Default::default() + }; + let output = format_idempotently( + "select case when first_condition_column = 1 and second_condition_column = 2 then 'x' else 'y' end from t", + &config, + ); + assert!( + column_of(&output, "second_condition_column") > column_of(&output, "when"), + "{output}" + ); +} + +/// The example in `docs/features/formatting.md`. +#[test] +fn the_documented_expanded_case_layout() { + let config = FormatConfig { + layout: Layout::Expanded, + comma_style: CommaStyle::Leading, + logical_operator_placement: LogicalOperatorPlacement::Leading, + ..Default::default() + }; + assert_eq!( + format_idempotently( + "select id, case when status = 'active' and deleted_at is null then 'live' else 'archived' end as state from accounts", + &config + ), + "select\n id\n , case\n when status = 'active'\n and deleted_at is null\n then 'live'\n else 'archived'\n end as state\nfrom\n accounts;" + ); +} diff --git a/crates/pgls_pretty_print/tests/comment_layout.rs b/crates/pgls_pretty_print/tests/comment_layout.rs index 0de4c411d..a7e6cc334 100644 --- a/crates/pgls_pretty_print/tests/comment_layout.rs +++ b/crates/pgls_pretty_print/tests/comment_layout.rs @@ -105,7 +105,7 @@ fn trailing_comment_keeps_the_case_condition_together() { "SELECT\n\ \tCASE\n\ \t\tWHEN ecrictbimmsynd.codtyprept = 'B0'\n\ - \t\tAND planctb_tab.codplanctb < 700000 -- test\n\ + \t\t\tAND planctb_tab.codplanctb < 700000 -- test\n\ \t\t\tTHEN 'CURRENT EXPENSES'\n\ \t\tWHEN ecrictbimmsynd.codtyprept IS NOT NULL\n\ \t\t\tTHEN 'REPAIR TO DO'\n\ @@ -130,8 +130,8 @@ fn standalone_comment_stays_before_the_conjunction_it_documents() { \t\tWHEN ecrictbimmsynd.codnatopr = 'SLD'\n\ \t\t\tTHEN 'SLD'\n\ \t\tWHEN ecrictbimmsynd.codtyprept = 'B0'\n\ - \t\t-- 700000 is a big number\n\ - \t\t{keyword} planctb_tab.codplanctb < 700000\n\ + \t\t\t-- 700000 is a big number\n\ + \t\t\t{keyword} planctb_tab.codplanctb < 700000\n\ \t\t\tTHEN 'CURRENT EXPENSES'\n\ \tEND\n\ ;" diff --git a/crates/pgls_pretty_print/tests/snapshots/single/tests__expanded_case_expr_200.snap b/crates/pgls_pretty_print/tests/snapshots/single/tests__expanded_case_expr_200.snap index bde0d6a92..e0bdb350e 100644 --- a/crates/pgls_pretty_print/tests/snapshots/single/tests__expanded_case_expr_200.snap +++ b/crates/pgls_pretty_print/tests/snapshots/single/tests__expanded_case_expr_200.snap @@ -5,7 +5,7 @@ input_file: crates/pgls_pretty_print/tests/data/single/expanded_case_expr.sql SELECT CASE WHEN CAST(accounting_lines.accounting_class AS VARCHAR) SIMILAR TO '(6|7)%' - AND CAST(accounting_lines.accounting_class AS VARCHAR) NOT SIMILAR TO '70(1|2|4)%' + AND CAST(accounting_lines.accounting_class AS VARCHAR) NOT SIMILAR TO '70(1|2|4)%' THEN COALESCE(accounting_lines.allocation_key_fk, allocation_keys.id) ELSE accounting_lines.allocation_key_fk END AS allocation_key_fk @@ -18,11 +18,11 @@ SELECT END AS source , CASE WHEN bank_informations.account_type = 'SEPARATE' - AND account_nature IN ('COMPTE_COURANT', 'COMPTE_COURANT_OTHER') - AND accounting_class = '5121' - AND accounting_account_sub_account = '000000003' - AND is_main_bank_account = TRUE - AND buildings.id IS NOT NULL + AND account_nature IN ('COMPTE_COURANT', 'COMPTE_COURANT_OTHER') + AND accounting_class = '5121' + AND accounting_account_sub_account = '000000003' + AND is_main_bank_account = TRUE + AND buildings.id IS NOT NULL THEN concat('SDC ', buildings.name) WHEN kind = 'CO_OWNER' THEN COALESCE(bank_informations.name, clean_labels(co_owner_account_customers.fullname)) diff --git a/docs/features/formatting.md b/docs/features/formatting.md index 7b81359dc..f00308f3d 100644 --- a/docs/features/formatting.md +++ b/docs/features/formatting.md @@ -74,6 +74,30 @@ The expressions inside each tuple stay on one line when they fit. This option applies to `INSERT`, standalone `VALUES`, and `VALUES` in subqueries and CTEs, independently of `layout` and `clauseBodyStyle`. +With `layout: "expanded"`, a `CASE` expression puts every `WHEN` and `ELSE` +on its own line, one indent level in from where `CASE` starts, `THEN` one +level further in, and `END` back under `CASE`. This holds wherever the `CASE` +starts, after a leading comma or `THEN` for instance. A `WHEN` condition that +breaks continues one level in from `WHEN`: + +```sql +select + id + , case + when status = 'active' + and deleted_at is null + then 'live' + else 'archived' + end as state +from + accounts; +``` + +With `indentStyle: "tabs"`, the indent levels are written as tabs and the rest +of an alignment, such as the two columns of `, ` before `case` above, as +spaces. The columns only line up when the editor's tab width equals +`indentSize`. + With `clauseBodyStyle: "compact"`, the first relation stays on the `FROM` line and the condition on the `WHERE`, `HAVING`, or `ON` line. A select list of a single item stays on the `SELECT` line, while a list of several items still From 63ba24fbae60abf5c980ec80bf143fef71675e64 Mon Sep 17 00:00:00 2001 From: psteinroe Date: Fri, 23 Oct 2026 14:04:06 +0000 Subject: [PATCH 4/7] fix(pretty_print): keep `= (` together for scalar subqueries --- crates/pgls_pretty_print/src/emitter.rs | 9 +++ crates/pgls_pretty_print/src/nodes/a_expr.rs | 20 ++++- .../pgls_pretty_print/src/nodes/sub_link.rs | 13 ++-- .../tests/data/multi/scalar_subquery.sql | 9 +++ .../data/multi/scalar_subquery_expanded.sql | 10 +++ .../multi/tests__create_cast_100.snap | 3 +- .../multi/tests__create_cast_80.snap | 3 +- .../multi/tests__create_index_80.snap | 16 ++-- .../multi/tests__create_view_100.snap | 6 +- .../multi/tests__create_view_80.snap | 6 +- .../snapshots/multi/tests__namespace_80.snap | 15 ++-- .../multi/tests__opr_sanity_100.snap | 3 +- .../snapshots/multi/tests__opr_sanity_80.snap | 8 +- .../multi/tests__rangefuncs_100.snap | 6 +- .../snapshots/multi/tests__rangefuncs_80.snap | 6 +- .../snapshots/multi/tests__reloptions_80.snap | 7 +- .../multi/tests__scalar_subquery_100.snap | 39 ++++++++++ .../multi/tests__scalar_subquery_80.snap | 66 ++++++++++++++++ .../tests__scalar_subquery_expanded_100.snap | 52 +++++++++++++ .../tests__scalar_subquery_expanded_80.snap | 52 +++++++++++++ .../multi/tests__tablespace_100.snap | 7 +- .../snapshots/multi/tests__tablespace_80.snap | 7 +- .../multi/tests__type_sanity_100.snap | 5 +- .../multi/tests__type_sanity_80.snap | 3 +- .../snapshots/multi/tests__with_100.snap | 3 +- .../tests/snapshots/multi/tests__with_80.snap | 3 +- .../tests/subquery_layout.rs | 76 +++++++++++++++++++ 27 files changed, 384 insertions(+), 69 deletions(-) create mode 100644 crates/pgls_pretty_print/tests/data/multi/scalar_subquery.sql create mode 100644 crates/pgls_pretty_print/tests/data/multi/scalar_subquery_expanded.sql create mode 100644 crates/pgls_pretty_print/tests/snapshots/multi/tests__scalar_subquery_100.snap create mode 100644 crates/pgls_pretty_print/tests/snapshots/multi/tests__scalar_subquery_80.snap create mode 100644 crates/pgls_pretty_print/tests/snapshots/multi/tests__scalar_subquery_expanded_100.snap create mode 100644 crates/pgls_pretty_print/tests/snapshots/multi/tests__scalar_subquery_expanded_80.snap create mode 100644 crates/pgls_pretty_print/tests/subquery_layout.rs diff --git a/crates/pgls_pretty_print/src/emitter.rs b/crates/pgls_pretty_print/src/emitter.rs index 05d293c35..018ecf090 100644 --- a/crates/pgls_pretty_print/src/emitter.rs +++ b/crates/pgls_pretty_print/src/emitter.rs @@ -59,12 +59,15 @@ pub struct EventEmitter { leading_line_comments_require_break: bool, /// Line comments written after a list comma, waiting for the emitter to reach that comma. comments_after_separator: Vec, + /// `config.layout` as configured, while `with_layout` overrides it for part of a statement. + configured_layout: crate::Layout, } impl EventEmitter { pub fn new(config: FormatConfig) -> Self { Self { events: Vec::new(), + configured_layout: config.layout.clone(), config, leading_comments: HashMap::new(), trailing_comments: HashMap::new(), @@ -79,6 +82,11 @@ impl EventEmitter { &self.config } + /// The layout from the configuration, which `with_layout` does not change. + pub fn configured_layout(&self) -> crate::Layout { + self.configured_layout.clone() + } + /// Emits `body` with `layout` in place of the configured one. pub fn with_layout(&mut self, layout: crate::Layout, body: impl FnOnce(&mut Self)) { let previous = std::mem::replace(&mut self.config.layout, layout); @@ -93,6 +101,7 @@ impl EventEmitter { ) -> Self { Self { events: Vec::new(), + configured_layout: config.layout.clone(), config, leading_comments, trailing_comments, diff --git a/crates/pgls_pretty_print/src/nodes/a_expr.rs b/crates/pgls_pretty_print/src/nodes/a_expr.rs index 6aa4dcd4b..0fffb7817 100644 --- a/crates/pgls_pretty_print/src/nodes/a_expr.rs +++ b/crates/pgls_pretty_print/src/nodes/a_expr.rs @@ -72,7 +72,13 @@ fn emit_aexpr_op(e: &mut EventEmitter, n: &AExpr) { } e.space(); emit_operator(e, &n.name); - e.line(LineType::SoftOrSpace); + // A subquery breaks inside its parentheses, so `= (` stays on the operator line, + // like `in (` and `exists (`. + if starts_with_subquery_parenthesis(rexpr) { + e.space(); + } else { + e.line(LineType::SoftOrSpace); + } emit_operand_with_parens(e, rexpr, parent_info, OperandSide::Right); if grouped_suffix { e.group_end(); @@ -598,6 +604,18 @@ fn operator_needs_space(op: &str) -> bool { op.chars().any(|c| c.is_alphabetic()) } +/// Whether `node` is printed as `(subquery)` or `array(subquery)`. +fn starts_with_subquery_parenthesis(node: &Node) -> bool { + use pgls_query::protobuf::SubLinkType; + + matches!( + node.node.as_ref(), + Some(NodeEnum::SubLink(sub_link)) + if sub_link.sub_link_type == SubLinkType::ExprSublink as i32 + || sub_link.sub_link_type == SubLinkType::ArraySublink as i32 + ) +} + fn operator_prefers_line_break(name: &[Node]) -> bool { match extract_simple_operator(name) { // Comparison operators diff --git a/crates/pgls_pretty_print/src/nodes/sub_link.rs b/crates/pgls_pretty_print/src/nodes/sub_link.rs index 918bf38b6..8ce0ded2f 100644 --- a/crates/pgls_pretty_print/src/nodes/sub_link.rs +++ b/crates/pgls_pretty_print/src/nodes/sub_link.rs @@ -136,13 +136,16 @@ fn emit_subquery(e: &mut EventEmitter, node: &pgls_query::protobuf::Node) { if let Some(pgls_query::NodeEnum::SelectStmt(select_stmt)) = node.node.as_ref() { // Expanded layout breaks between clauses. A subquery made of its select list alone, such // as `(select auth.uid())`, has none to break between, so it only breaks past the width. - if has_select_list_only(select_stmt) { - e.with_layout(Layout::Fit, |e| { - super::emit_select_stmt_no_semicolon(e, select_stmt); - }); + // A subquery with clauses nested in such a subquery, `(select coalesce((select ... from + // ...), 0))` for instance, uses the configured layout again. + let layout = if has_select_list_only(select_stmt) { + Layout::Fit } else { + e.configured_layout() + }; + e.with_layout(layout, |e| { super::emit_select_stmt_no_semicolon(e, select_stmt); - } + }); } else { // For other node types (e.g., VALUES), emit normally super::emit_node(node, e); diff --git a/crates/pgls_pretty_print/tests/data/multi/scalar_subquery.sql b/crates/pgls_pretty_print/tests/data/multi/scalar_subquery.sql new file mode 100644 index 000000000..2422f842f --- /dev/null +++ b/crates/pgls_pretty_print/tests/data/multi/scalar_subquery.sql @@ -0,0 +1,9 @@ +create policy members_only on documents using (organization_id = (select organization_id from memberships where user_id = auth.uid())); + +select * from documents as d where d.owner_id = (select m.user_id from memberships as m where m.organization_id = d.organization_id limit 1); + +select * from documents where tags = array(select tag from document_tags where document_tags.document_id = documents.id order by tag); + +select * from t where a = (select coalesce((select max(x) from y where y.id = t.id and y.kind = 'some kind of value'), 0)); + +select * from t where a = (select 1); diff --git a/crates/pgls_pretty_print/tests/data/multi/scalar_subquery_expanded.sql b/crates/pgls_pretty_print/tests/data/multi/scalar_subquery_expanded.sql new file mode 100644 index 000000000..ea996d5b3 --- /dev/null +++ b/crates/pgls_pretty_print/tests/data/multi/scalar_subquery_expanded.sql @@ -0,0 +1,10 @@ +-- pgls-format: layout=expanded, clauseBodyStyle=compact, logicalOperatorPlacement=leading +create policy members_only on documents using (organization_id = (select organization_id from memberships where user_id = auth.uid())); + +select * from documents as d where d.owner_id = (select m.user_id from memberships as m where m.organization_id = d.organization_id limit 1); + +select * from documents where tags = array(select tag from document_tags where document_tags.document_id = documents.id order by tag); + +select * from t where a = (select coalesce((select max(x) from y where y.id = t.id and y.kind = 'some kind of value'), 0)); + +select * from t where a = (select 1); diff --git a/crates/pgls_pretty_print/tests/snapshots/multi/tests__create_cast_100.snap b/crates/pgls_pretty_print/tests/snapshots/multi/tests__create_cast_100.snap index a7d96a590..1b9354cda 100644 --- a/crates/pgls_pretty_print/tests/snapshots/multi/tests__create_cast_100.snap +++ b/crates/pgls_pretty_print/tests/snapshots/multi/tests__create_cast_100.snap @@ -80,8 +80,7 @@ from pg_depend where classid = cast('pg_cast' as regclass) and - objid = - ( + objid = ( select oid from diff --git a/crates/pgls_pretty_print/tests/snapshots/multi/tests__create_cast_80.snap b/crates/pgls_pretty_print/tests/snapshots/multi/tests__create_cast_80.snap index 7575e7034..81a417c80 100644 --- a/crates/pgls_pretty_print/tests/snapshots/multi/tests__create_cast_80.snap +++ b/crates/pgls_pretty_print/tests/snapshots/multi/tests__create_cast_80.snap @@ -84,8 +84,7 @@ from pg_depend where classid = cast('pg_cast' as regclass) and - objid = - ( + objid = ( select oid from diff --git a/crates/pgls_pretty_print/tests/snapshots/multi/tests__create_index_80.snap b/crates/pgls_pretty_print/tests/snapshots/multi/tests__create_index_80.snap index 1d704513e..6586db097 100644 --- a/crates/pgls_pretty_print/tests/snapshots/multi/tests__create_index_80.snap +++ b/crates/pgls_pretty_print/tests/snapshots/multi/tests__create_index_80.snap @@ -1196,8 +1196,7 @@ from tenk1 as t1 where t1.thousand = 42 or - t1.thousand = - ( + t1.thousand = ( select t2.tenthous from @@ -1213,8 +1212,7 @@ from tenk1 as t1 where t1.thousand = 42 or - t1.thousand = - ( + t1.thousand = ( select t2.tenthous from @@ -2326,8 +2324,9 @@ as from pg_class where - relnamespace = - (select oid from pg_namespace where nspname = 'schema_to_reindex'); + relnamespace = ( + select oid from pg_namespace where nspname = 'schema_to_reindex' + ); insert into reindex_before select @@ -2374,8 +2373,9 @@ as from pg_class where - relnamespace = - (select oid from pg_namespace where nspname = 'schema_to_reindex'); + relnamespace = ( + select oid from pg_namespace where nspname = 'schema_to_reindex' + ); select b.relname, diff --git a/crates/pgls_pretty_print/tests/snapshots/multi/tests__create_view_100.snap b/crates/pgls_pretty_print/tests/snapshots/multi/tests__create_view_100.snap index e464fc98f..f754f6809 100644 --- a/crates/pgls_pretty_print/tests/snapshots/multi/tests__create_view_100.snap +++ b/crates/pgls_pretty_print/tests/snapshots/multi/tests__create_view_100.snap @@ -857,8 +857,7 @@ begin; delete from pg_depend where - objid = - ( + objid = ( select oid from pg_rewrite where ev_class = cast('tt14v' as regclass) and rulename = '_RETURN' ) and refobjsubid = 3 @@ -886,8 +885,7 @@ begin; delete from pg_depend where - objid = - ( + objid = ( select oid from pg_rewrite where ev_class = cast('tt14v' as regclass) and rulename = '_RETURN' ) and refobjsubid = 4 diff --git a/crates/pgls_pretty_print/tests/snapshots/multi/tests__create_view_80.snap b/crates/pgls_pretty_print/tests/snapshots/multi/tests__create_view_80.snap index a72770f8e..a38b8d1c7 100644 --- a/crates/pgls_pretty_print/tests/snapshots/multi/tests__create_view_80.snap +++ b/crates/pgls_pretty_print/tests/snapshots/multi/tests__create_view_80.snap @@ -916,8 +916,7 @@ begin; delete from pg_depend where - objid = - ( + objid = ( select oid from @@ -950,8 +949,7 @@ begin; delete from pg_depend where - objid = - ( + objid = ( select oid from diff --git a/crates/pgls_pretty_print/tests/snapshots/multi/tests__namespace_80.snap b/crates/pgls_pretty_print/tests/snapshots/multi/tests__namespace_80.snap index 2e668aa7d..3ca6dbc12 100644 --- a/crates/pgls_pretty_print/tests/snapshots/multi/tests__namespace_80.snap +++ b/crates/pgls_pretty_print/tests/snapshots/multi/tests__namespace_80.snap @@ -53,8 +53,9 @@ select from pg_class where - relnamespace = - (select oid from pg_namespace where nspname = 'test_ns_schema_1'); + relnamespace = ( + select oid from pg_namespace where nspname = 'test_ns_schema_1' + ); insert into test_ns_schema_1.abc default values; @@ -73,8 +74,9 @@ select from pg_class where - relnamespace = - (select oid from pg_namespace where nspname = 'test_ns_schema_1'); + relnamespace = ( + select oid from pg_namespace where nspname = 'test_ns_schema_1' + ); create schema test_ns_schema_renamed; @@ -94,8 +96,9 @@ select from pg_class where - relnamespace = - (select oid from pg_namespace where nspname = 'test_ns_schema_renamed'); + relnamespace = ( + select oid from pg_namespace where nspname = 'test_ns_schema_renamed' + ); create schema test_maint_search_path; diff --git a/crates/pgls_pretty_print/tests/snapshots/multi/tests__opr_sanity_100.snap b/crates/pgls_pretty_print/tests/snapshots/multi/tests__opr_sanity_100.snap index deecb9b84..cf6f219f0 100644 --- a/crates/pgls_pretty_print/tests/snapshots/multi/tests__opr_sanity_100.snap +++ b/crates/pgls_pretty_print/tests/snapshots/multi/tests__opr_sanity_100.snap @@ -418,8 +418,7 @@ from pg_proc as p1 where proallargtypes is not null and - array(select unnest(proargtypes)) <> - array( + array(select unnest(proargtypes)) <> array( select proallargtypes[i] from diff --git a/crates/pgls_pretty_print/tests/snapshots/multi/tests__opr_sanity_80.snap b/crates/pgls_pretty_print/tests/snapshots/multi/tests__opr_sanity_80.snap index 069693bf3..f0a6328ca 100644 --- a/crates/pgls_pretty_print/tests/snapshots/multi/tests__opr_sanity_80.snap +++ b/crates/pgls_pretty_print/tests/snapshots/multi/tests__opr_sanity_80.snap @@ -430,8 +430,7 @@ from pg_proc as p1 where proallargtypes is not null and - array(select unnest(proargtypes)) <> - array( + array(select unnest(proargtypes)) <> array( select proallargtypes[i] from @@ -530,8 +529,9 @@ where 'lo_lseek64', 'lo_tell', 'lo_tell64', 'lo_truncate', 'lo_truncate64', 'loread', 'lowrite' ) and - pronamespace = - (select oid from pg_catalog.pg_namespace where nspname = 'pg_catalog') + pronamespace = ( + select oid from pg_catalog.pg_namespace where nspname = 'pg_catalog' + ) order by 1; select diff --git a/crates/pgls_pretty_print/tests/snapshots/multi/tests__rangefuncs_100.snap b/crates/pgls_pretty_print/tests/snapshots/multi/tests__rangefuncs_100.snap index e31ac2150..d173808a2 100644 --- a/crates/pgls_pretty_print/tests/snapshots/multi/tests__rangefuncs_100.snap +++ b/crates/pgls_pretty_print/tests/snapshots/multi/tests__rangefuncs_100.snap @@ -1272,8 +1272,7 @@ begin; delete from pg_depend where - objid = - ( + objid = ( select oid from @@ -1300,8 +1299,7 @@ begin; delete from pg_depend where - objid = - ( + objid = ( select oid from diff --git a/crates/pgls_pretty_print/tests/snapshots/multi/tests__rangefuncs_80.snap b/crates/pgls_pretty_print/tests/snapshots/multi/tests__rangefuncs_80.snap index 01d4cc896..a6dc46dbb 100644 --- a/crates/pgls_pretty_print/tests/snapshots/multi/tests__rangefuncs_80.snap +++ b/crates/pgls_pretty_print/tests/snapshots/multi/tests__rangefuncs_80.snap @@ -1445,8 +1445,7 @@ begin; delete from pg_depend where - objid = - ( + objid = ( select oid from @@ -1473,8 +1472,7 @@ begin; delete from pg_depend where - objid = - ( + objid = ( select oid from diff --git a/crates/pgls_pretty_print/tests/snapshots/multi/tests__reloptions_80.snap b/crates/pgls_pretty_print/tests/snapshots/multi/tests__reloptions_80.snap index bf48435b8..f8205f892 100644 --- a/crates/pgls_pretty_print/tests/snapshots/multi/tests__reloptions_80.snap +++ b/crates/pgls_pretty_print/tests/snapshots/multi/tests__reloptions_80.snap @@ -1,7 +1,6 @@ --- source: crates/pgls_pretty_print/tests/tests.rs input_file: crates/pgls_pretty_print/tests/data/multi/reloptions.sql -snapshot_kind: text --- create table reloptions_test (i int) with ( @@ -112,8 +111,7 @@ select from pg_class where - oid = - ( + oid = ( select reltoastrelid from @@ -175,8 +173,7 @@ select from pg_class where - oid = - ( + oid = ( select reltoastrelid from diff --git a/crates/pgls_pretty_print/tests/snapshots/multi/tests__scalar_subquery_100.snap b/crates/pgls_pretty_print/tests/snapshots/multi/tests__scalar_subquery_100.snap new file mode 100644 index 000000000..b8ca9a475 --- /dev/null +++ b/crates/pgls_pretty_print/tests/snapshots/multi/tests__scalar_subquery_100.snap @@ -0,0 +1,39 @@ +--- +source: crates/pgls_pretty_print/tests/tests.rs +input_file: crates/pgls_pretty_print/tests/data/multi/scalar_subquery.sql +--- +create policy members_only +on documents +as permissive +for all +to public +using (organization_id = (select organization_id from memberships where user_id = auth.uid())); + +select + * +from + documents as d +where + d.owner_id = ( + select m.user_id from memberships as m where m.organization_id = d.organization_id limit 1 + ); + +select + * +from + documents +where + tags = array( + select tag from document_tags where document_tags.document_id = documents.id order by tag + ); + +select + * +from + t +where + a = ( + select coalesce((select max(x) from y where y.id = t.id and y.kind = 'some kind of value'), 0) + ); + +select * from t where a = (select 1); diff --git a/crates/pgls_pretty_print/tests/snapshots/multi/tests__scalar_subquery_80.snap b/crates/pgls_pretty_print/tests/snapshots/multi/tests__scalar_subquery_80.snap new file mode 100644 index 000000000..278fbf584 --- /dev/null +++ b/crates/pgls_pretty_print/tests/snapshots/multi/tests__scalar_subquery_80.snap @@ -0,0 +1,66 @@ +--- +source: crates/pgls_pretty_print/tests/tests.rs +input_file: crates/pgls_pretty_print/tests/data/multi/scalar_subquery.sql +--- +create policy members_only +on documents +as permissive +for all +to public +using ( + organization_id = ( + select organization_id from memberships where user_id = auth.uid() + ) +); + +select + * +from + documents as d +where + d.owner_id = ( + select + m.user_id + from + memberships as m + where + m.organization_id = d.organization_id + limit 1 + ); + +select + * +from + documents +where + tags = array( + select + tag + from + document_tags + where + document_tags.document_id = documents.id + order by tag + ); + +select + * +from + t +where + a = ( + select + coalesce( + ( + select + max(x) + from + y + where + y.id = t.id and y.kind = 'some kind of value' + ), + 0 + ) + ); + +select * from t where a = (select 1); diff --git a/crates/pgls_pretty_print/tests/snapshots/multi/tests__scalar_subquery_expanded_100.snap b/crates/pgls_pretty_print/tests/snapshots/multi/tests__scalar_subquery_expanded_100.snap new file mode 100644 index 000000000..c7853c85c --- /dev/null +++ b/crates/pgls_pretty_print/tests/snapshots/multi/tests__scalar_subquery_expanded_100.snap @@ -0,0 +1,52 @@ +--- +source: crates/pgls_pretty_print/tests/tests.rs +input_file: crates/pgls_pretty_print/tests/data/multi/scalar_subquery_expanded.sql +--- +create policy members_only +on documents +as permissive +for all +to public +using ( + organization_id = ( + select organization_id + from memberships + where user_id = auth.uid() + ) +); + +select * +from documents as d +where d.owner_id = ( + select m.user_id + from memberships as m + where m.organization_id = d.organization_id + limit 1 + ); + +select * +from documents +where tags = array( + select tag + from document_tags + where document_tags.document_id = documents.id + order by tag + ); + +select * +from t +where a = ( + select coalesce( + ( + select max(x) + from y + where y.id = t.id + and y.kind = 'some kind of value' + ), + 0 + ) + ); + +select * +from t +where a = (select 1); diff --git a/crates/pgls_pretty_print/tests/snapshots/multi/tests__scalar_subquery_expanded_80.snap b/crates/pgls_pretty_print/tests/snapshots/multi/tests__scalar_subquery_expanded_80.snap new file mode 100644 index 000000000..c7853c85c --- /dev/null +++ b/crates/pgls_pretty_print/tests/snapshots/multi/tests__scalar_subquery_expanded_80.snap @@ -0,0 +1,52 @@ +--- +source: crates/pgls_pretty_print/tests/tests.rs +input_file: crates/pgls_pretty_print/tests/data/multi/scalar_subquery_expanded.sql +--- +create policy members_only +on documents +as permissive +for all +to public +using ( + organization_id = ( + select organization_id + from memberships + where user_id = auth.uid() + ) +); + +select * +from documents as d +where d.owner_id = ( + select m.user_id + from memberships as m + where m.organization_id = d.organization_id + limit 1 + ); + +select * +from documents +where tags = array( + select tag + from document_tags + where document_tags.document_id = documents.id + order by tag + ); + +select * +from t +where a = ( + select coalesce( + ( + select max(x) + from y + where y.id = t.id + and y.kind = 'some kind of value' + ), + 0 + ) + ); + +select * +from t +where a = (select 1); diff --git a/crates/pgls_pretty_print/tests/snapshots/multi/tests__tablespace_100.snap b/crates/pgls_pretty_print/tests/snapshots/multi/tests__tablespace_100.snap index f62c3573b..ca034f9ad 100644 --- a/crates/pgls_pretty_print/tests/snapshots/multi/tests__tablespace_100.snap +++ b/crates/pgls_pretty_print/tests/snapshots/multi/tests__tablespace_100.snap @@ -1,7 +1,6 @@ --- source: crates/pgls_pretty_print/tests/tests.rs input_file: crates/pgls_pretty_print/tests/data/multi/tablespace.sql -snapshot_kind: text --- create tablespace regress_tblspace location 'relative'; @@ -90,8 +89,7 @@ select from pg_class where - oid = - ( + oid = ( select i.indexrelid from @@ -162,8 +160,7 @@ select from pg_class where - oid = - ( + oid = ( select i.indexrelid from diff --git a/crates/pgls_pretty_print/tests/snapshots/multi/tests__tablespace_80.snap b/crates/pgls_pretty_print/tests/snapshots/multi/tests__tablespace_80.snap index 49c10a362..5824a02b4 100644 --- a/crates/pgls_pretty_print/tests/snapshots/multi/tests__tablespace_80.snap +++ b/crates/pgls_pretty_print/tests/snapshots/multi/tests__tablespace_80.snap @@ -1,7 +1,6 @@ --- source: crates/pgls_pretty_print/tests/tests.rs input_file: crates/pgls_pretty_print/tests/data/multi/tablespace.sql -snapshot_kind: text --- create tablespace regress_tblspace location 'relative'; @@ -111,8 +110,7 @@ select from pg_class where - oid = - ( + oid = ( select i.indexrelid from @@ -184,8 +182,7 @@ select from pg_class where - oid = - ( + oid = ( select i.indexrelid from diff --git a/crates/pgls_pretty_print/tests/snapshots/multi/tests__type_sanity_100.snap b/crates/pgls_pretty_print/tests/snapshots/multi/tests__type_sanity_100.snap index 2aad3dc76..f790609ad 100644 --- a/crates/pgls_pretty_print/tests/snapshots/multi/tests__type_sanity_100.snap +++ b/crates/pgls_pretty_print/tests/snapshots/multi/tests__type_sanity_100.snap @@ -630,8 +630,9 @@ select from pg_class as c1 where - c1.relnatts <> - (select count(*) from pg_attribute as a1 where a1.attrelid = c1.oid and a1.attnum > 0); + c1.relnatts <> ( + select count(*) from pg_attribute as a1 where a1.attrelid = c1.oid and a1.attnum > 0 + ); select a1.attrelid, diff --git a/crates/pgls_pretty_print/tests/snapshots/multi/tests__type_sanity_80.snap b/crates/pgls_pretty_print/tests/snapshots/multi/tests__type_sanity_80.snap index 53a3aa47f..2ba360831 100644 --- a/crates/pgls_pretty_print/tests/snapshots/multi/tests__type_sanity_80.snap +++ b/crates/pgls_pretty_print/tests/snapshots/multi/tests__type_sanity_80.snap @@ -671,8 +671,7 @@ select from pg_class as c1 where - c1.relnatts <> - ( + c1.relnatts <> ( select count(*) from diff --git a/crates/pgls_pretty_print/tests/snapshots/multi/tests__with_100.snap b/crates/pgls_pretty_print/tests/snapshots/multi/tests__with_100.snap index b4c0fd52d..c5b695c75 100644 --- a/crates/pgls_pretty_print/tests/snapshots/multi/tests__with_100.snap +++ b/crates/pgls_pretty_print/tests/snapshots/multi/tests__with_100.snap @@ -278,8 +278,7 @@ from ) as t where - n < - ( + n < ( select count(*) from diff --git a/crates/pgls_pretty_print/tests/snapshots/multi/tests__with_80.snap b/crates/pgls_pretty_print/tests/snapshots/multi/tests__with_80.snap index 5c091c125..71b516b81 100644 --- a/crates/pgls_pretty_print/tests/snapshots/multi/tests__with_80.snap +++ b/crates/pgls_pretty_print/tests/snapshots/multi/tests__with_80.snap @@ -311,8 +311,7 @@ from ) as t where - n < - ( + n < ( select count(*) from diff --git a/crates/pgls_pretty_print/tests/subquery_layout.rs b/crates/pgls_pretty_print/tests/subquery_layout.rs new file mode 100644 index 000000000..9cf960aff --- /dev/null +++ b/crates/pgls_pretty_print/tests/subquery_layout.rs @@ -0,0 +1,76 @@ +use pgls_pretty_print::{ + ClauseBodyStyle, FormatConfig, Layout, LogicalOperatorPlacement, format_statement, +}; + +fn format_idempotently(sql: &str, config: &FormatConfig) -> String { + let ast = pgls_query::parse(sql).unwrap().into_root().unwrap(); + let first = format_statement(&ast, sql, config).unwrap().formatted; + let ast = pgls_query::parse(&first).unwrap().into_root().unwrap(); + let second = format_statement(&ast, &first, config).unwrap().formatted; + assert_eq!(first, second); + first +} + +fn expanded_compact_leading() -> FormatConfig { + FormatConfig { + layout: Layout::Expanded, + clause_body_style: ClauseBodyStyle::Compact, + logical_operator_placement: LogicalOperatorPlacement::Leading, + ..Default::default() + } +} + +#[test] +fn scalar_subquery_keeps_operator_and_parenthesis_together() { + assert_eq!( + format_idempotently( + "create policy p on t using (org_id = (select org_id from members where user_id = auth.uid()))", + &expanded_compact_leading(), + ), + "create policy p\non t\nas permissive\nfor all\nto public\nusing (\n org_id = (\n select org_id\n from members\n where user_id = auth.uid()\n )\n);" + ); + + let config = FormatConfig { + line_width: 60, + ..Default::default() + }; + assert_eq!( + format_idempotently( + "select * from t where t.owner_id = (select m.user_id from memberships as m where m.id = 1)", + &config, + ), + "select\n *\nfrom\n t\nwhere\n t.owner_id = (\n select m.user_id from memberships as m where m.id = 1\n );" + ); +} + +#[test] +fn array_subquery_keeps_operator_and_array_together() { + let config = FormatConfig { + line_width: 60, + ..Default::default() + }; + let output = format_idempotently( + "select * from t where t.tags = array(select tag from tags where tags.id = t.id order by tag)", + &config, + ); + assert!(output.contains("t.tags = array(\n"), "{output}"); +} + +#[test] +fn nested_subquery_inside_select_list_only_subquery_expands() { + let config = FormatConfig { + layout: Layout::Expanded, + clause_body_style: ClauseBodyStyle::Compact, + ..Default::default() + }; + let output = format_idempotently( + "select * from t where a = (select coalesce((select max(x) from y where y.id = t.id), 0))", + &config, + ); + assert!( + output + .lines() + .any(|line| line.trim_start().starts_with("from y")), + "{output}" + ); +} From c7c7ee9e671f404290b9c89d51ae4be5375d9148 Mon Sep 17 00:00:00 2001 From: psteinroe Date: Fri, 23 Oct 2026 14:07:51 +0000 Subject: [PATCH 5/7] fix(pretty_print): group function calls so count(*), arrays and JSON functions break sensibly --- .../src/nodes/a_array_expr.rs | 14 +- .../pgls_pretty_print/src/nodes/func_call.rs | 12 +- .../src/nodes/json_func_expr.rs | 284 ++--- .../tests/function_call_layout.rs | 104 ++ ...s__20230405163940_download_metrics_80.snap | 24 +- .../multi/tests__aggregates_100.snap | 20 +- .../snapshots/multi/tests__aggregates_80.snap | 50 +- .../snapshots/multi/tests__amutils_100.snap | 63 +- .../snapshots/multi/tests__amutils_80.snap | 84 +- .../snapshots/multi/tests__arrays_100.snap | 12 +- .../snapshots/multi/tests__arrays_80.snap | 12 +- .../multi/tests__json_returning_100.snap | 22 +- .../multi/tests__json_returning_80.snap | 23 +- .../snapshots/multi/tests__jsonpath_100.snap | 10 +- .../snapshots/multi/tests__jsonpath_80.snap | 10 +- .../multi/tests__multirangetypes_80.snap | 5 +- .../multi/tests__ordered_set_filter_80.snap | 11 +- .../snapshots/multi/tests__rangefuncs_80.snap | 7 +- .../snapshots/multi/tests__rowtypes_80.snap | 6 +- .../multi/tests__sqljson_queryfuncs_100.snap | 409 +++++--- .../multi/tests__sqljson_queryfuncs_80.snap | 977 ++++++++++++------ .../snapshots/multi/tests__stats_ext_100.snap | 14 +- .../snapshots/multi/tests__stats_ext_80.snap | 14 +- .../snapshots/multi/tests__tuplesort_100.snap | 16 +- .../snapshots/multi/tests__tuplesort_80.snap | 16 +- .../multi/tests__type_sanity_100.snap | 24 +- .../multi/tests__type_sanity_80.snap | 25 +- .../snapshots/multi/tests__window_100.snap | 4 +- .../snapshots/multi/tests__window_80.snap | 4 +- ...s__func_call_within_group_filter_0_80.snap | 11 +- 30 files changed, 1327 insertions(+), 960 deletions(-) create mode 100644 crates/pgls_pretty_print/tests/function_call_layout.rs diff --git a/crates/pgls_pretty_print/src/nodes/a_array_expr.rs b/crates/pgls_pretty_print/src/nodes/a_array_expr.rs index c886ea719..f15d07777 100644 --- a/crates/pgls_pretty_print/src/nodes/a_array_expr.rs +++ b/crates/pgls_pretty_print/src/nodes/a_array_expr.rs @@ -2,10 +2,10 @@ use pgls_query::protobuf::AArrayExpr; use crate::{ TokenKind, - emitter::{EventEmitter, GroupKind}, + emitter::{EventEmitter, GroupKind, LineType}, }; -use super::node_list::emit_comma_separated_list; +use super::node_list::emit_fill_comma_separated_list; pub(super) fn emit_a_array_expr(e: &mut EventEmitter, n: &AArrayExpr) { e.group_start(GroupKind::AArrayExpr); @@ -13,8 +13,16 @@ pub(super) fn emit_a_array_expr(e: &mut EventEmitter, n: &AArrayExpr) { e.token(TokenKind::ARRAY_KW); e.token(TokenKind::L_BRACK); + // Laid out like an `IN (...)` list: on one line when it fits, otherwise the elements fill + // indented lines and `]` goes back to the line the array started on. if !n.elements.is_empty() { - emit_comma_separated_list(e, &n.elements, super::emit_node); + e.indent_start(); + e.line(LineType::Soft); + e.group_start(GroupKind::List); + emit_fill_comma_separated_list(e, &n.elements, super::emit_node); + e.group_end(); + e.indent_end(); + e.line(LineType::Soft); } e.token(TokenKind::R_BRACK); diff --git a/crates/pgls_pretty_print/src/nodes/func_call.rs b/crates/pgls_pretty_print/src/nodes/func_call.rs index 6c211e9fc..def8c6bf1 100644 --- a/crates/pgls_pretty_print/src/nodes/func_call.rs +++ b/crates/pgls_pretty_print/src/nodes/func_call.rs @@ -102,7 +102,10 @@ fn emit_grouped_comma( pub(super) fn emit_func_call(e: &mut EventEmitter, n: &FuncCall) { e.group_start(GroupKind::FuncCall); - if n.over.is_some() { + // The name and arguments have a group of their own when a clause follows them, so that + // `count(*)` stays together and the line breaks before FILTER, WITHIN GROUP or OVER first. + let has_suffix = n.over.is_some() || n.agg_filter.is_some() || n.agg_within_group; + if has_suffix { e.group_start(GroupKind::List); } @@ -115,7 +118,7 @@ pub(super) fn emit_func_call(e: &mut EventEmitter, n: &FuncCall) { emit_standard_function(e, n); } - if n.over.is_some() { + if has_suffix { e.group_end(); } @@ -125,6 +128,8 @@ pub(super) fn emit_func_call(e: &mut EventEmitter, n: &FuncCall) { "ordered-set aggregate is missing ORDER BY list" ); + // A clause that breaks onto its own line continues the call, one level in. + e.indent_start(); e.line(LineType::SoftOrSpace); e.token(TokenKind::WITHIN_KW); e.space(); @@ -144,9 +149,11 @@ pub(super) fn emit_func_call(e: &mut EventEmitter, n: &FuncCall) { e.line(LineType::Soft); e.token(TokenKind::R_PAREN); e.group_end(); + e.indent_end(); } if let Some(ref filter) = n.agg_filter { + e.indent_start(); e.line(LineType::SoftOrSpace); e.token(TokenKind::FILTER_KW); e.space(); @@ -154,6 +161,7 @@ pub(super) fn emit_func_call(e: &mut EventEmitter, n: &FuncCall) { e.token(TokenKind::WHERE_KW); super::emit_clause_condition(e, filter); e.token(TokenKind::R_PAREN); + e.indent_end(); } // Handle OVER clause (window functions) diff --git a/crates/pgls_pretty_print/src/nodes/json_func_expr.rs b/crates/pgls_pretty_print/src/nodes/json_func_expr.rs index 13aa7ed87..b74b1f3c7 100644 --- a/crates/pgls_pretty_print/src/nodes/json_func_expr.rs +++ b/crates/pgls_pretty_print/src/nodes/json_func_expr.rs @@ -50,218 +50,136 @@ pub(super) fn emit_json_func_expr(e: &mut EventEmitter, n: &JsonFuncExpr) { // Map JSON function operation types // 0=Undefined, 1=JsonExistsOp, 2=JsonQueryOp, 3=JsonValueOp, 4=JsonTableOp - match n.op { - 1 => { - // JSON_EXISTS - e.token(TokenKind::JSON_EXISTS_KW); + let keyword = match n.op { + 1 => TokenKind::JSON_EXISTS_KW, + 2 => TokenKind::JSON_QUERY_KW, + 3 => TokenKind::JSON_VALUE_KW, + _ => { + // Unknown JSON function - emit placeholder + e.token(TokenKind::IDENT("json_func".to_string())); e.token(TokenKind::L_PAREN); - - if let Some(ref context) = n.context_item { - if let Some(ref raw_expr) = context.raw_expr { - super::emit_node(raw_expr, e); - } - if let Some(ref format) = context.format { - super::json_value_expr::emit_json_format(e, format); - } - } - - e.token(TokenKind::COMMA); - e.space(); - - if let Some(ref pathspec) = n.pathspec { - super::emit_node(pathspec, e); - } - - // PASSING clause - if !n.passing.is_empty() { - e.line(LineType::SoftOrSpace); - e.token(TokenKind::PASSING_KW); - e.space(); - super::node_list::emit_comma_separated_list(e, &n.passing, super::emit_node); - } - - // ON ERROR clause - if let Some(ref on_error) = n.on_error { - e.line(LineType::SoftOrSpace); - emit_json_behavior(e, on_error); - e.space(); - e.token(TokenKind::ON_KW); - e.space(); - e.token(TokenKind::ERROR_KW); - } - e.token(TokenKind::R_PAREN); + e.group_end(); + return; } - 2 => { - // JSON_QUERY - e.token(TokenKind::JSON_QUERY_KW); - e.token(TokenKind::L_PAREN); + }; + let is_exists = n.op == 1; + let is_query = n.op == 2; + + // Laid out like a function call: on one line when it fits, otherwise the context item, the + // path and every clause on their own line, one level in, and `)` back on the call's line. + e.token(keyword); + e.token(TokenKind::L_PAREN); + e.indent_start(); + e.line(LineType::Soft); + + if let Some(ref context) = n.context_item { + if let Some(ref raw_expr) = context.raw_expr { + super::emit_node(raw_expr, e); + } + if let Some(ref format) = context.format { + super::json_value_expr::emit_json_format(e, format); + } + } - if let Some(ref context) = n.context_item { - if let Some(ref raw_expr) = context.raw_expr { - super::emit_node(raw_expr, e); - } - if let Some(ref format) = context.format { - super::json_value_expr::emit_json_format(e, format); - } - } + e.token(TokenKind::COMMA); + e.line(LineType::SoftOrSpace); - e.token(TokenKind::COMMA); - e.space(); + if let Some(ref pathspec) = n.pathspec { + super::emit_node(pathspec, e); + } - if let Some(ref pathspec) = n.pathspec { - super::emit_node(pathspec, e); - } + // PASSING clause + if !n.passing.is_empty() { + e.line(LineType::SoftOrSpace); + e.token(TokenKind::PASSING_KW); + e.space(); + // The arguments break on their own only when they don't fit on the PASSING line. + e.group_start(GroupKind::List); + super::node_list::emit_comma_separated_list(e, &n.passing, super::emit_node); + e.group_end(); + } - // PASSING clause - if !n.passing.is_empty() { - e.line(LineType::SoftOrSpace); - e.token(TokenKind::PASSING_KW); - e.space(); - super::node_list::emit_comma_separated_list(e, &n.passing, super::emit_node); - } + // RETURNING clause; JSON_EXISTS has none. + if !is_exists && let Some(ref output) = n.output { + e.line(LineType::SoftOrSpace); + super::json_value_expr::emit_json_output(e, output, &mut false); + } - // RETURNING clause - if let Some(ref output) = n.output { + if is_query { + // Wrapper handling (WITHOUT WRAPPER, WITH WRAPPER, etc.) + // wrapper: 0=Undefined, 1=JswUnspec, 2=JswNone (WITHOUT), 3=JswConditional, 4=JswUnconditional + match n.wrapper { + 2 => { + // JswNone = WITHOUT WRAPPER e.line(LineType::SoftOrSpace); - super::json_value_expr::emit_json_output(e, output, &mut false); - } - - // Wrapper handling (WITHOUT WRAPPER, WITH WRAPPER, etc.) - // wrapper: 0=Undefined, 1=JswUnspec, 2=JswNone (WITHOUT), 3=JswConditional, 4=JswUnconditional - match n.wrapper { - 2 => { - // JswNone = WITHOUT WRAPPER - e.line(LineType::SoftOrSpace); - e.token(TokenKind::WITHOUT_KW); - e.space(); - e.token(TokenKind::WRAPPER_KW); - } - 3 => { - // JswConditional = WITH CONDITIONAL WRAPPER - e.line(LineType::SoftOrSpace); - e.token(TokenKind::WITH_KW); - e.space(); - e.token(TokenKind::CONDITIONAL_KW); - e.space(); - e.token(TokenKind::WRAPPER_KW); - } - 4 => { - // JswUnconditional = WITH UNCONDITIONAL WRAPPER / WITH WRAPPER - e.line(LineType::SoftOrSpace); - e.token(TokenKind::WITH_KW); - e.space(); - e.token(TokenKind::UNCONDITIONAL_KW); - e.space(); - e.token(TokenKind::WRAPPER_KW); - } - _ => {} - } - - // Quote behavior (KEEP QUOTES, OMIT QUOTES) - // quotes: 0=undefined, 1=unspec, 2=keep, 3=omit - match n.quotes { - 2 => { - e.line(LineType::SoftOrSpace); - e.token(TokenKind::KEEP_KW); - e.space(); - e.token(TokenKind::QUOTES_KW); - } - 3 => { - e.line(LineType::SoftOrSpace); - e.token(TokenKind::OMIT_KW); - e.space(); - e.token(TokenKind::QUOTES_KW); - } - _ => {} + e.token(TokenKind::WITHOUT_KW); + e.space(); + e.token(TokenKind::WRAPPER_KW); } - - // ON EMPTY clause - if let Some(ref on_empty) = n.on_empty { + 3 => { + // JswConditional = WITH CONDITIONAL WRAPPER e.line(LineType::SoftOrSpace); - emit_json_behavior(e, on_empty); + e.token(TokenKind::WITH_KW); e.space(); - e.token(TokenKind::ON_KW); + e.token(TokenKind::CONDITIONAL_KW); e.space(); - e.token(TokenKind::EMPTY_KW); + e.token(TokenKind::WRAPPER_KW); } - - // ON ERROR clause - if let Some(ref on_error) = n.on_error { + 4 => { + // JswUnconditional = WITH UNCONDITIONAL WRAPPER / WITH WRAPPER e.line(LineType::SoftOrSpace); - emit_json_behavior(e, on_error); + e.token(TokenKind::WITH_KW); e.space(); - e.token(TokenKind::ON_KW); + e.token(TokenKind::UNCONDITIONAL_KW); e.space(); - e.token(TokenKind::ERROR_KW); + e.token(TokenKind::WRAPPER_KW); } - - e.token(TokenKind::R_PAREN); + _ => {} } - 3 => { - // JSON_VALUE - e.token(TokenKind::JSON_VALUE_KW); - e.token(TokenKind::L_PAREN); - - if let Some(ref context) = n.context_item { - if let Some(ref raw_expr) = context.raw_expr { - super::emit_node(raw_expr, e); - } - if let Some(ref format) = context.format { - super::json_value_expr::emit_json_format(e, format); - } - } - e.token(TokenKind::COMMA); - e.space(); - - if let Some(ref pathspec) = n.pathspec { - super::emit_node(pathspec, e); - } - - // PASSING clause - if !n.passing.is_empty() { + // Quote behavior (KEEP QUOTES, OMIT QUOTES) + // quotes: 0=undefined, 1=unspec, 2=keep, 3=omit + match n.quotes { + 2 => { e.line(LineType::SoftOrSpace); - e.token(TokenKind::PASSING_KW); + e.token(TokenKind::KEEP_KW); e.space(); - super::node_list::emit_comma_separated_list(e, &n.passing, super::emit_node); + e.token(TokenKind::QUOTES_KW); } - - // RETURNING clause - if let Some(ref output) = n.output { - e.line(LineType::SoftOrSpace); - super::json_value_expr::emit_json_output(e, output, &mut false); - } - - // ON EMPTY clause - if let Some(ref on_empty) = n.on_empty { + 3 => { e.line(LineType::SoftOrSpace); - emit_json_behavior(e, on_empty); + e.token(TokenKind::OMIT_KW); e.space(); - e.token(TokenKind::ON_KW); - e.space(); - e.token(TokenKind::EMPTY_KW); + e.token(TokenKind::QUOTES_KW); } + _ => {} + } + } - // ON ERROR clause - if let Some(ref on_error) = n.on_error { - e.line(LineType::SoftOrSpace); - emit_json_behavior(e, on_error); - e.space(); - e.token(TokenKind::ON_KW); - e.space(); - e.token(TokenKind::ERROR_KW); - } + // ON EMPTY clause; JSON_EXISTS has none. + if !is_exists && let Some(ref on_empty) = n.on_empty { + e.line(LineType::SoftOrSpace); + emit_json_behavior(e, on_empty); + e.space(); + e.token(TokenKind::ON_KW); + e.space(); + e.token(TokenKind::EMPTY_KW); + } - e.token(TokenKind::R_PAREN); - } - _ => { - // Unknown JSON function - emit placeholder - e.token(TokenKind::IDENT("json_func".to_string())); - e.token(TokenKind::L_PAREN); - e.token(TokenKind::R_PAREN); - } + // ON ERROR clause + if let Some(ref on_error) = n.on_error { + e.line(LineType::SoftOrSpace); + emit_json_behavior(e, on_error); + e.space(); + e.token(TokenKind::ON_KW); + e.space(); + e.token(TokenKind::ERROR_KW); } + e.indent_end(); + e.line(LineType::Soft); + e.token(TokenKind::R_PAREN); + e.group_end(); } diff --git a/crates/pgls_pretty_print/tests/function_call_layout.rs b/crates/pgls_pretty_print/tests/function_call_layout.rs new file mode 100644 index 000000000..ac70c26df --- /dev/null +++ b/crates/pgls_pretty_print/tests/function_call_layout.rs @@ -0,0 +1,104 @@ +use pgls_pretty_print::{FormatConfig, format_statement}; + +fn format_idempotently(sql: &str, config: &FormatConfig) -> String { + let ast = pgls_query::parse(sql).unwrap().into_root().unwrap(); + let first = format_statement(&ast, sql, config).unwrap().formatted; + let ast = pgls_query::parse(&first).unwrap().into_root().unwrap(); + let second = format_statement(&ast, &first, config).unwrap().formatted; + assert_eq!(first, second); + first +} + +#[test] +fn count_star_stays_together_when_the_filter_breaks() { + let output = format_idempotently( + "select count(*) filter (where status = 'active' and created_at > now() - interval '30 days' and deleted_at is null) from users", + &FormatConfig::default(), + ); + assert!(output.contains(" count(*)\n filter (where"), "{output}"); +} + +#[test] +fn within_group_continues_one_level_in() { + let config = FormatConfig { + line_width: 60, + ..Default::default() + }; + let output = format_idempotently( + "select percentile_cont(0.5) within group (order by some_long_column_name, another_long_column) from t", + &config, + ); + assert!( + output.contains(" percentile_cont(0.5)\n within group ("), + "{output}" + ); +} + +#[test] +fn long_array_literal_fills_indented_lines() { + let output = format_idempotently( + "select * from t where t.a = any (array[1111111111, 2222222222, 3333333333, 4444444444, 5555555555, 6666666666, 7777777777, 8888888888])", + &FormatConfig { + line_width: 60, + ..Default::default() + }, + ); + assert_eq!( + output, + "select\n *\nfrom\n t\nwhere\n t.a = any (array[\n 1111111111, 2222222222, 3333333333, 4444444444,\n 5555555555, 6666666666, 7777777777, 8888888888\n ]);" + ); +} + +#[test] +fn short_array_literal_stays_on_one_line() { + assert_eq!( + format_idempotently("select array[1, 2, 3]", &FormatConfig::default()), + "select array[1, 2, 3];" + ); +} + +#[test] +fn json_value_respects_the_line_width() { + let config = FormatConfig { + line_width: 60, + ..Default::default() + }; + let output = format_idempotently( + "select json_value(jsonb_column_with_long_name, '$.some.very.long.path.expression.here' returning integer default 0 on empty error on error) from t", + &config, + ); + assert_eq!( + output, + "select\n json_value(\n jsonb_column_with_long_name,\n '$.some.very.long.path.expression.here'\n returning int\n default 0 on empty\n error on error\n )\nfrom\n t;" + ); +} + +#[test] +fn json_query_and_json_exists_respect_the_line_width() { + let config = FormatConfig { + line_width: 60, + ..Default::default() + }; + let output = format_idempotently( + "select json_query(jsonb_column_with_long_name, '$.some.path' returning jsonb with conditional wrapper keep quotes) from t", + &config, + ); + assert!(output.lines().all(|line| line.len() <= 60), "{output}"); + assert!( + output.contains("\n with conditional wrapper\n"), + "{output}" + ); + + let output = format_idempotently( + "select json_exists(jsonb_column_with_long_name, '$.some.very.long.path.expression' error on error) from t", + &config, + ); + assert!(output.lines().all(|line| line.len() <= 60), "{output}"); + assert!(output.contains("\n error on error\n )"), "{output}"); + + // A call that fits stays on one line. + assert_eq!( + format_idempotently("select json_value(j, '$.a' returning int)", &config), + "select json_value(j, '$.a' returning int);" + ); +} diff --git a/crates/pgls_pretty_print/tests/snapshots/multi/tests__20230405163940_download_metrics_80.snap b/crates/pgls_pretty_print/tests/snapshots/multi/tests__20230405163940_download_metrics_80.snap index 49925ea93..b2e519ddb 100644 --- a/crates/pgls_pretty_print/tests/snapshots/multi/tests__20230405163940_download_metrics_80.snap +++ b/crates/pgls_pretty_print/tests/snapshots/multi/tests__20230405163940_download_metrics_80.snap @@ -62,23 +62,17 @@ as select dl.package_id, count(dl.id) as downloads_all_time, - count( - dl.id - ) - filter (where - dl.created_at > now() - cast('180 days' as interval)) + count(dl.id) + filter (where + dl.created_at > now() - cast('180 days' as interval)) as downloads_180_days, - count( - dl.id - ) - filter (where - dl.created_at > now() - cast('90 days' as interval)) + count(dl.id) + filter (where + dl.created_at > now() - cast('90 days' as interval)) as downloads_90_days, - count( - dl.id - ) - filter (where - dl.created_at > now() - cast('30 days' as interval)) + count(dl.id) + filter (where + dl.created_at > now() - cast('30 days' as interval)) as downloads_30_day from app.downloads as dl diff --git a/crates/pgls_pretty_print/tests/snapshots/multi/tests__aggregates_100.snap b/crates/pgls_pretty_print/tests/snapshots/multi/tests__aggregates_100.snap index a0216c3bb..e09aef9d3 100644 --- a/crates/pgls_pretty_print/tests/snapshots/multi/tests__aggregates_100.snap +++ b/crates/pgls_pretty_print/tests/snapshots/multi/tests__aggregates_100.snap @@ -1170,11 +1170,9 @@ from select ( select - max( - (select i.unique2 from tenk1 as i where i.unique1 = o.unique1) - ) - filter (where - o.unique1 < 10) + max((select i.unique2 from tenk1 as i where i.unique1 = o.unique1)) + filter (where + o.unique1 < 10) ) from tenk1 as o; @@ -1262,18 +1260,14 @@ from select percentile_cont(array[0, 0.25, 0.5, 0.75, 1]) within group (order by thousand) from tenk1; select - percentile_disc( - array[array[null, 1, 0.5], array[0.75, 0.25, null]] - ) - within group (order by thousand) + percentile_disc(array[array[null, 1, 0.5], array[0.75, 0.25, null]]) + within group (order by thousand) from tenk1; select - percentile_cont( - array[0, 1, 0.25, 0.75, 0.5, 1, 0.3, 0.32, 0.35, 0.38, 0.4] - ) - within group (order by x) + percentile_cont(array[0, 1, 0.25, 0.75, 0.5, 1, 0.3, 0.32, 0.35, 0.38, 0.4]) + within group (order by x) from generate_series(1, 6) as x; diff --git a/crates/pgls_pretty_print/tests/snapshots/multi/tests__aggregates_80.snap b/crates/pgls_pretty_print/tests/snapshots/multi/tests__aggregates_80.snap index 924d0b070..67db79d62 100644 --- a/crates/pgls_pretty_print/tests/snapshots/multi/tests__aggregates_80.snap +++ b/crates/pgls_pretty_print/tests/snapshots/multi/tests__aggregates_80.snap @@ -1340,21 +1340,17 @@ from select ( select - max( - (select i.unique2 from tenk1 as i where i.unique1 = o.unique1) - ) - filter (where - o.unique1 < 10) + max((select i.unique2 from tenk1 as i where i.unique1 = o.unique1)) + filter (where + o.unique1 < 10) ) from tenk1 as o; select - sum( - unique1 - ) - filter (where - unique1 in (select unique1 from onek where unique1 < 100)) + sum(unique1) + filter (where + unique1 in (select unique1 from onek where unique1 < 100)) from tenk1; @@ -1477,10 +1473,8 @@ from (values (1), (1), (2), (2), (3), (3), (4)) as v (x); select - percentile_disc( - array[0, 0.1, 0.25, 0.5, 0.75, 0.9, 1] - ) - within group (order by thousand) + percentile_disc(array[0, 0.1, 0.25, 0.5, 0.75, 0.9, 1]) + within group (order by thousand) from tenk1; @@ -1490,18 +1484,14 @@ from tenk1; select - percentile_disc( - array[array[null, 1, 0.5], array[0.75, 0.25, null]] - ) - within group (order by thousand) + percentile_disc(array[array[null, 1, 0.5], array[0.75, 0.25, null]]) + within group (order by thousand) from tenk1; select - percentile_cont( - array[0, 1, 0.25, 0.75, 0.5, 1, 0.3, 0.32, 0.35, 0.38, 0.4] - ) - within group (order by x) + percentile_cont(array[0, 1, 0.25, 0.75, 0.5, 1, 0.3, 0.32, 0.35, 0.38, 0.4]) + within group (order by x) from generate_series(1, 6) as x; @@ -1553,10 +1543,8 @@ select rank(3) within group (order by stringu1, stringu2) from tenk1; select rank('fred') within group (order by x) from generate_series(1, 5) as x; select - rank( - cast('adam' as text) collate "C" - ) - within group (order by x collate "POSIX") + rank(cast('adam' as text) collate "C") + within group (order by x collate "POSIX") from (values ('fred'), ('jim')) as v (x); @@ -1576,12 +1564,10 @@ create view aggordview1 as select ten, percentile_disc(0.5) within group (order by thousand) as p50, - percentile_disc( - 0.5 - ) - within group (order by thousand) - filter (where - hundred = 1) + percentile_disc(0.5) + within group (order by thousand) + filter (where + hundred = 1) as px, rank(5, 'AZZZZ', 50) within group (order by hundred, string4 desc, hundred) from diff --git a/crates/pgls_pretty_print/tests/snapshots/multi/tests__amutils_100.snap b/crates/pgls_pretty_print/tests/snapshots/multi/tests__amutils_100.snap index 974df6362..93e1f5a91 100644 --- a/crates/pgls_pretty_print/tests/snapshots/multi/tests__amutils_100.snap +++ b/crates/pgls_pretty_print/tests/snapshots/multi/tests__amutils_100.snap @@ -1,7 +1,6 @@ --- source: crates/pgls_pretty_print/tests/tests.rs input_file: crates/pgls_pretty_print/tests/data/multi/amutils.sql -snapshot_kind: text --- select prop, @@ -11,25 +10,11 @@ select from pg_am as a, unnest( - cast(array['asc', - 'desc', - 'nulls_first', - 'nulls_last', - 'orderable', - 'distance_orderable', - 'returnable', - 'search_array', - 'search_nulls', - 'clusterable', - 'index_scan', - 'bitmap_scan', - 'backward_scan', - 'can_order', - 'can_unique', - 'can_multi_col', - 'can_exclude', - 'can_include', - 'bogus'] + cast(array[ + 'asc', 'desc', 'nulls_first', 'nulls_last', 'orderable', 'distance_orderable', 'returnable', + 'search_array', 'search_nulls', 'clusterable', 'index_scan', 'bitmap_scan', 'backward_scan', + 'can_order', 'can_unique', 'can_multi_col', 'can_exclude', 'can_include', 'bogus' + ] as text[]) ) with ordinality @@ -46,25 +31,11 @@ select from pg_am as a, unnest( - cast(array['asc', - 'desc', - 'nulls_first', - 'nulls_last', - 'orderable', - 'distance_orderable', - 'returnable', - 'search_array', - 'search_nulls', - 'clusterable', - 'index_scan', - 'bitmap_scan', - 'backward_scan', - 'can_order', - 'can_unique', - 'can_multi_col', - 'can_exclude', - 'can_include', - 'bogus'] + cast(array[ + 'asc', 'desc', 'nulls_first', 'nulls_last', 'orderable', 'distance_orderable', 'returnable', + 'search_array', 'search_nulls', 'clusterable', 'index_scan', 'bitmap_scan', 'backward_scan', + 'can_order', 'can_unique', 'can_multi_col', 'can_exclude', 'can_include', 'bogus' + ] as text[]) ) with ordinality @@ -84,16 +55,10 @@ select pg_index_column_has_property(cast('brinidx' as regclass), 1, prop) as brin from unnest( - cast(array['asc', - 'desc', - 'nulls_first', - 'nulls_last', - 'orderable', - 'distance_orderable', - 'returnable', - 'search_array', - 'search_nulls', - 'bogus'] + cast(array[ + 'asc', 'desc', 'nulls_first', 'nulls_last', 'orderable', 'distance_orderable', 'returnable', + 'search_array', 'search_nulls', 'bogus' + ] as text[]) ) with ordinality diff --git a/crates/pgls_pretty_print/tests/snapshots/multi/tests__amutils_80.snap b/crates/pgls_pretty_print/tests/snapshots/multi/tests__amutils_80.snap index 0e3e21aff..abb9a19fa 100644 --- a/crates/pgls_pretty_print/tests/snapshots/multi/tests__amutils_80.snap +++ b/crates/pgls_pretty_print/tests/snapshots/multi/tests__amutils_80.snap @@ -1,7 +1,6 @@ --- source: crates/pgls_pretty_print/tests/tests.rs input_file: crates/pgls_pretty_print/tests/data/multi/amutils.sql -snapshot_kind: text --- select prop, @@ -12,25 +11,12 @@ select from pg_am as a, unnest( - cast(array['asc', - 'desc', - 'nulls_first', - 'nulls_last', - 'orderable', - 'distance_orderable', - 'returnable', - 'search_array', - 'search_nulls', - 'clusterable', - 'index_scan', - 'bitmap_scan', - 'backward_scan', - 'can_order', - 'can_unique', - 'can_multi_col', - 'can_exclude', - 'can_include', - 'bogus'] + cast(array[ + 'asc', 'desc', 'nulls_first', 'nulls_last', 'orderable', + 'distance_orderable', 'returnable', 'search_array', 'search_nulls', + 'clusterable', 'index_scan', 'bitmap_scan', 'backward_scan', 'can_order', + 'can_unique', 'can_multi_col', 'can_exclude', 'can_include', 'bogus' + ] as text[]) ) with ordinality @@ -48,25 +34,12 @@ select from pg_am as a, unnest( - cast(array['asc', - 'desc', - 'nulls_first', - 'nulls_last', - 'orderable', - 'distance_orderable', - 'returnable', - 'search_array', - 'search_nulls', - 'clusterable', - 'index_scan', - 'bitmap_scan', - 'backward_scan', - 'can_order', - 'can_unique', - 'can_multi_col', - 'can_exclude', - 'can_include', - 'bogus'] + cast(array[ + 'asc', 'desc', 'nulls_first', 'nulls_last', 'orderable', + 'distance_orderable', 'returnable', 'search_array', 'search_nulls', + 'clusterable', 'index_scan', 'bitmap_scan', 'backward_scan', 'can_order', + 'can_unique', 'can_multi_col', 'can_exclude', 'can_include', 'bogus' + ] as text[]) ) with ordinality @@ -91,16 +64,11 @@ select pg_index_column_has_property(cast('brinidx' as regclass), 1, prop) as brin from unnest( - cast(array['asc', - 'desc', - 'nulls_first', - 'nulls_last', - 'orderable', - 'distance_orderable', - 'returnable', - 'search_array', - 'search_nulls', - 'bogus'] + cast(array[ + 'asc', 'desc', 'nulls_first', 'nulls_last', 'orderable', + 'distance_orderable', 'returnable', 'search_array', 'search_nulls', + 'bogus' + ] as text[]) ) with ordinality @@ -117,11 +85,9 @@ select pg_index_has_property(cast('brinidx' as regclass), prop) as brin from unnest( - cast(array['clusterable', - 'index_scan', - 'bitmap_scan', - 'backward_scan', - 'bogus'] + cast(array[ + 'clusterable', 'index_scan', 'bitmap_scan', 'backward_scan', 'bogus' + ] as text[]) ) with ordinality @@ -135,12 +101,10 @@ select from pg_am as a, unnest( - cast(array['can_order', - 'can_unique', - 'can_multi_col', - 'can_exclude', - 'can_include', - 'bogus'] + cast(array[ + 'can_order', 'can_unique', 'can_multi_col', 'can_exclude', 'can_include', + 'bogus' + ] as text[]) ) with ordinality diff --git a/crates/pgls_pretty_print/tests/snapshots/multi/tests__arrays_100.snap b/crates/pgls_pretty_print/tests/snapshots/multi/tests__arrays_100.snap index 3ba6aec40..6dfa63a03 100644 --- a/crates/pgls_pretty_print/tests/snapshots/multi/tests__arrays_100.snap +++ b/crates/pgls_pretty_print/tests/snapshots/multi/tests__arrays_100.snap @@ -280,8 +280,10 @@ values array[array[array[113, 142], array[1, 147]]], cast(array[1.1, 1.2, 1.3] as float8[]), array[1.1, 1.2, 1.3], - array[array[array['aaa', 'aab'], array['aba', 'abb'], array['aca', 'acb']], - array[array['baa', 'bab'], array['bba', 'bbb'], array['bca', 'bcb']]], + array[ + array[array['aaa', 'aab'], array['aba', 'abb'], array['aca', 'acb']], + array[array['baa', 'bab'], array['bba', 'bbb'], array['bca', 'bcb']] + ], cast(array['19620326', '19931223', '19970117'] as timestamp[]) ); @@ -339,8 +341,10 @@ select from ( select - array[array[array[111, 112], array[121, 122], array[131, 132]], - array[array[211, 212], array[221, 122], array[231, 232]]] + array[ + array[array[111, 112], array[121, 122], array[131, 132]], + array[array[211, 212], array[221, 122], array[231, 232]] + ] as f ) as t; diff --git a/crates/pgls_pretty_print/tests/snapshots/multi/tests__arrays_80.snap b/crates/pgls_pretty_print/tests/snapshots/multi/tests__arrays_80.snap index 9823316ce..9d7277628 100644 --- a/crates/pgls_pretty_print/tests/snapshots/multi/tests__arrays_80.snap +++ b/crates/pgls_pretty_print/tests/snapshots/multi/tests__arrays_80.snap @@ -313,8 +313,10 @@ values array[array[array[113, 142], array[1, 147]]], cast(array[1.1, 1.2, 1.3] as float8[]), array[1.1, 1.2, 1.3], - array[array[array['aaa', 'aab'], array['aba', 'abb'], array['aca', 'acb']], - array[array['baa', 'bab'], array['bba', 'bbb'], array['bca', 'bcb']]], + array[ + array[array['aaa', 'aab'], array['aba', 'abb'], array['aca', 'acb']], + array[array['baa', 'bab'], array['bba', 'bbb'], array['bca', 'bcb']] + ], cast(array['19620326', '19931223', '19970117'] as timestamp[]) ); @@ -372,8 +374,10 @@ select from ( select - array[array[array[111, 112], array[121, 122], array[131, 132]], - array[array[211, 212], array[221, 122], array[231, 232]]] + array[ + array[array[111, 112], array[121, 122], array[131, 132]], + array[array[211, 212], array[221, 122], array[231, 232]] + ] as f ) as t; diff --git a/crates/pgls_pretty_print/tests/snapshots/multi/tests__json_returning_100.snap b/crates/pgls_pretty_print/tests/snapshots/multi/tests__json_returning_100.snap index 43ebb1ebb..c6ae5eeb4 100644 --- a/crates/pgls_pretty_print/tests/snapshots/multi/tests__json_returning_100.snap +++ b/crates/pgls_pretty_print/tests/snapshots/multi/tests__json_returning_100.snap @@ -5,18 +5,24 @@ input_file: crates/pgls_pretty_print/tests/data/multi/json_returning.sql select json_value(cast('123' as jsonb), '$' returning int); select - json_value(cast('{"a": {"b": "a rather long string value"}}' as jsonb), '$.a.b' - returning text - default 'fallback' on empty - error on error); + json_value( + cast('{"a": {"b": "a rather long string value"}}' as jsonb), + '$.a.b' + returning text + default 'fallback' on empty + error on error + ); select json_query(cast('[1, 2]' as jsonb), '$' returning text); select - json_query(cast('{"a": [1, 2, 3, 4, 5, 6, 7, 8]}' as jsonb), '$.a' - returning jsonb - with conditional wrapper - keep quotes); + json_query( + cast('{"a": [1, 2, 3, 4, 5, 6, 7, 8]}' as jsonb), + '$.a' + returning jsonb + with conditional wrapper + keep quotes + ); select json_arrayagg(i absent on null returning jsonb) from generate_series(1, 3) as i; diff --git a/crates/pgls_pretty_print/tests/snapshots/multi/tests__json_returning_80.snap b/crates/pgls_pretty_print/tests/snapshots/multi/tests__json_returning_80.snap index 818092789..64e46ed30 100644 --- a/crates/pgls_pretty_print/tests/snapshots/multi/tests__json_returning_80.snap +++ b/crates/pgls_pretty_print/tests/snapshots/multi/tests__json_returning_80.snap @@ -5,19 +5,24 @@ input_file: crates/pgls_pretty_print/tests/data/multi/json_returning.sql select json_value(cast('123' as jsonb), '$' returning int); select - json_value(cast('{"a": {"b": "a rather long string value"}}' - as jsonb), '$.a.b' - returning text - default 'fallback' on empty - error on error); + json_value( + cast('{"a": {"b": "a rather long string value"}}' as jsonb), + '$.a.b' + returning text + default 'fallback' on empty + error on error + ); select json_query(cast('[1, 2]' as jsonb), '$' returning text); select - json_query(cast('{"a": [1, 2, 3, 4, 5, 6, 7, 8]}' as jsonb), '$.a' - returning jsonb - with conditional wrapper - keep quotes); + json_query( + cast('{"a": [1, 2, 3, 4, 5, 6, 7, 8]}' as jsonb), + '$.a' + returning jsonb + with conditional wrapper + keep quotes + ); select json_arrayagg(i absent on null returning jsonb) diff --git a/crates/pgls_pretty_print/tests/snapshots/multi/tests__jsonpath_100.snap b/crates/pgls_pretty_print/tests/snapshots/multi/tests__jsonpath_100.snap index e1661d3dd..b60d06e59 100644 --- a/crates/pgls_pretty_print/tests/snapshots/multi/tests__jsonpath_100.snap +++ b/crates/pgls_pretty_print/tests/snapshots/multi/tests__jsonpath_100.snap @@ -1,7 +1,6 @@ --- source: crates/pgls_pretty_print/tests/tests.rs input_file: crates/pgls_pretty_print/tests/data/multi/jsonpath.sql -snapshot_kind: text --- select cast('' as jsonpath); @@ -458,11 +457,10 @@ select errinfo.hint from unnest( - array[cast('$ ? (@ like_regex "pattern" flag "smixq")' as text), - '$ ? (@ like_regex "pattern" flag "a")', - '@ + 1', - '00', - '1a'] + array[ + cast('$ ? (@ like_regex "pattern" flag "smixq")' as text), + '$ ? (@ like_regex "pattern" flag "a")', '@ + 1', '00', '1a' + ] ) as str, lateral pg_input_error_info(str, 'jsonpath') as errinfo; diff --git a/crates/pgls_pretty_print/tests/snapshots/multi/tests__jsonpath_80.snap b/crates/pgls_pretty_print/tests/snapshots/multi/tests__jsonpath_80.snap index 3c9bce573..08da6ffba 100644 --- a/crates/pgls_pretty_print/tests/snapshots/multi/tests__jsonpath_80.snap +++ b/crates/pgls_pretty_print/tests/snapshots/multi/tests__jsonpath_80.snap @@ -1,7 +1,6 @@ --- source: crates/pgls_pretty_print/tests/tests.rs input_file: crates/pgls_pretty_print/tests/data/multi/jsonpath.sql -snapshot_kind: text --- select cast('' as jsonpath); @@ -462,11 +461,10 @@ select errinfo.hint from unnest( - array[cast('$ ? (@ like_regex "pattern" flag "smixq")' as text), - '$ ? (@ like_regex "pattern" flag "a")', - '@ + 1', - '00', - '1a'] + array[ + cast('$ ? (@ like_regex "pattern" flag "smixq")' as text), + '$ ? (@ like_regex "pattern" flag "a")', '@ + 1', '00', '1a' + ] ) as str, lateral pg_input_error_info(str, 'jsonpath') as errinfo; diff --git a/crates/pgls_pretty_print/tests/snapshots/multi/tests__multirangetypes_80.snap b/crates/pgls_pretty_print/tests/snapshots/multi/tests__multirangetypes_80.snap index c88898c0a..92e3b3a73 100644 --- a/crates/pgls_pretty_print/tests/snapshots/multi/tests__multirangetypes_80.snap +++ b/crates/pgls_pretty_print/tests/snapshots/multi/tests__multirangetypes_80.snap @@ -1771,8 +1771,9 @@ language sql as $$select int4range(1,10)$$; select - array[nummultirange(numrange(1.1, 1.2)), - nummultirange(numrange(12.3, 155.5))]; + array[ + nummultirange(numrange(1.1, 1.2)), nummultirange(numrange(12.3, 155.5)) + ]; create table i8mr_array ( f1 int, diff --git a/crates/pgls_pretty_print/tests/snapshots/multi/tests__ordered_set_filter_80.snap b/crates/pgls_pretty_print/tests/snapshots/multi/tests__ordered_set_filter_80.snap index aa0bfb3ce..80665a2d5 100644 --- a/crates/pgls_pretty_print/tests/snapshots/multi/tests__ordered_set_filter_80.snap +++ b/crates/pgls_pretty_print/tests/snapshots/multi/tests__ordered_set_filter_80.snap @@ -1,7 +1,6 @@ --- source: crates/pgls_pretty_print/tests/tests.rs input_file: crates/pgls_pretty_print/tests/data/multi/ordered_set_filter.sql -snapshot_kind: text --- select percentile_disc(0.5) within group (order by score) filter (where score > 0) @@ -9,12 +8,10 @@ from (values (1), (2), (3)) as scores (score); select - percentile_cont( - 0.9 - ) - within group (order by duration) - filter (where - duration is not null) + percentile_cont(0.9) + within group (order by duration) + filter (where + duration is not null) from ( values (cast('1 hour' as interval)), (cast('2 hours' as interval)) diff --git a/crates/pgls_pretty_print/tests/snapshots/multi/tests__rangefuncs_80.snap b/crates/pgls_pretty_print/tests/snapshots/multi/tests__rangefuncs_80.snap index a6dc46dbb..50d7a29e0 100644 --- a/crates/pgls_pretty_print/tests/snapshots/multi/tests__rangefuncs_80.snap +++ b/crates/pgls_pretty_print/tests/snapshots/multi/tests__rangefuncs_80.snap @@ -1593,9 +1593,10 @@ select row_to_json(u) from unnest( - array[cast(null as rngfunc2), - cast((1, 'foo') as rngfunc2), - cast(null as rngfunc2)] + array[ + cast(null as rngfunc2), cast((1, 'foo') as rngfunc2), + cast(null as rngfunc2) + ] ) as u; diff --git a/crates/pgls_pretty_print/tests/snapshots/multi/tests__rowtypes_80.snap b/crates/pgls_pretty_print/tests/snapshots/multi/tests__rowtypes_80.snap index 22d2bfc74..a3e3dc88d 100644 --- a/crates/pgls_pretty_print/tests/snapshots/multi/tests__rowtypes_80.snap +++ b/crates/pgls_pretty_print/tests/snapshots/multi/tests__rowtypes_80.snap @@ -427,8 +427,10 @@ select q.d = row(row(4)) from unnest( - array[row(1, row(2), array[row(3)], row(row(4))), - row(2, row(3), array[row(4)], row(row(5)))] + array[ + row(1, row(2), array[row(3)], row(row(4))), + row(2, row(3), array[row(4)], row(row(5))) + ] ) as q (a int, b record, c record[], d record); diff --git a/crates/pgls_pretty_print/tests/snapshots/multi/tests__sqljson_queryfuncs_100.snap b/crates/pgls_pretty_print/tests/snapshots/multi/tests__sqljson_queryfuncs_100.snap index e9865ea9e..c2f8abd76 100644 --- a/crates/pgls_pretty_print/tests/snapshots/multi/tests__sqljson_queryfuncs_100.snap +++ b/crates/pgls_pretty_print/tests/snapshots/multi/tests__sqljson_queryfuncs_100.snap @@ -119,22 +119,31 @@ select json_value(cast('null' as jsonb), '$' returning sqljsonb_int_not_null); select json_value(cast('null' as jsonb), '$' returning sqljsonb_int_not_null error on error); select - json_value(cast('null' as jsonb), '$' - returning sqljsonb_int_not_null - default 2 on empty - error on error); + json_value( + cast('null' as jsonb), + '$' + returning sqljsonb_int_not_null + default 2 on empty + error on error + ); select - json_value(cast('1' as jsonb), '$.a' - returning sqljsonb_int_not_null - default 2 on empty - error on error); + json_value( + cast('1' as jsonb), + '$.a' + returning sqljsonb_int_not_null + default 2 on empty + error on error + ); select - json_value(cast('1' as jsonb), '$.a' - returning sqljsonb_int_not_null - default null on empty - error on error); + json_value( + cast('1' as jsonb), + '$.a' + returning sqljsonb_int_not_null + default null on empty + error on error + ); create type rainbow as enum ('red', 'orange', 'yellow', 'green', 'blue', 'purple'); @@ -190,11 +199,14 @@ select json_value(cast('["1"]' as jsonb), '$[*]' returning record); select x, - json_value(cast('{"a": 1, "b": 2}' as jsonb), '$.* ? (@ > $x)' - passing x as x - returning int - default -1 on empty - default -2 on error) + json_value( + cast('{"a": 1, "b": 2}' as jsonb), + '$.* ? (@ > $x)' + passing x as x + returning int + default -1 on empty + default -2 on error + ) as y from generate_series(0, 2) as x; @@ -205,54 +217,84 @@ select json_value(cast('null' as jsonb), '$a' passing cast(' (1, 2 )' as point) as a returning point); select - json_value(cast('null' as jsonb), '$a' - passing cast(' (1, 2 )' as point) as a - returning point - error on error); + json_value( + cast('null' as jsonb), + '$a' + passing cast(' (1, 2 )' as point) as a + returning point + error on error + ); select - json_value(cast('null' as jsonb), '$ts' - passing cast('2018-02-21 12:34:56 +10' as timestamptz) as ts); + json_value( + cast('null' as jsonb), + '$ts' + passing cast('2018-02-21 12:34:56 +10' as timestamptz) as ts + ); select - json_value(cast('null' as jsonb), '$ts' - passing cast('2018-02-21 12:34:56 +10' as timestamptz) as ts - returning timestamptz); + json_value( + cast('null' as jsonb), + '$ts' + passing cast('2018-02-21 12:34:56 +10' as timestamptz) as ts + returning timestamptz + ); select - json_value(cast('null' as jsonb), '$ts' - passing cast('2018-02-21 12:34:56 +10' as timestamptz) as ts - returning timestamp); + json_value( + cast('null' as jsonb), + '$ts' + passing cast('2018-02-21 12:34:56 +10' as timestamptz) as ts + returning timestamp + ); select - json_value(cast('null' as jsonb), '$ts' - passing cast('2018-02-21 12:34:56 +10' as date) as ts - returning date); + json_value( + cast('null' as jsonb), + '$ts' + passing cast('2018-02-21 12:34:56 +10' as date) as ts + returning date + ); select - json_value(cast('null' as jsonb), '$ts' - passing cast('2018-02-21 12:34:56 +10' as time) as ts - returning time); + json_value( + cast('null' as jsonb), + '$ts' + passing cast('2018-02-21 12:34:56 +10' as time) as ts + returning time + ); select - json_value(cast('null' as jsonb), '$ts' - passing cast('2018-02-21 12:34:56 +10' as timetz) as ts - returning timetz); + json_value( + cast('null' as jsonb), + '$ts' + passing cast('2018-02-21 12:34:56 +10' as timetz) as ts + returning timetz + ); select - json_value(cast('null' as jsonb), '$ts' - passing cast('2018-02-21 12:34:56 +10' as timestamp) as ts - returning timestamp); + json_value( + cast('null' as jsonb), + '$ts' + passing cast('2018-02-21 12:34:56 +10' as timestamp) as ts + returning timestamp + ); select - json_value(cast('null' as jsonb), '$ts' - passing cast('2018-02-21 12:34:56 +10' as timestamptz) as ts - returning json); + json_value( + cast('null' as jsonb), + '$ts' + passing cast('2018-02-21 12:34:56 +10' as timestamptz) as ts + returning json + ); select - json_value(cast('null' as jsonb), '$ts' - passing cast('2018-02-21 12:34:56 +10' as timestamptz) as ts - returning jsonb); + json_value( + cast('null' as jsonb), + '$ts' + passing cast('2018-02-21 12:34:56 +10' as timestamptz) as ts + returning jsonb + ); select json_value('{"a": 1.234}', '$.a' returning int error on error); @@ -326,10 +368,13 @@ select json_query(cast('"aaa"' as jsonb), '$.a' returning char(2) omit quotes default 'bb' on empty); select - json_query(cast('"aaa"' as jsonb), '$.a' - returning char(2) - omit quotes - default cast('"bb"' as jsonb) on empty); + json_query( + cast('"aaa"' as jsonb), + '$.a' + returning char(2) + omit quotes + default cast('"bb"' as jsonb) on empty + ); select json_query(cast('[1]' as jsonb), '$' with unconditional wrapper omit quotes); @@ -350,20 +395,26 @@ select json_query(cast('{"rec": "{1,2,3}"}' as jsonb), '$.rec' returning int[] o select json_query(cast('{"rec": "{1,2,3}"}' as jsonb), '$.rec' returning int[] keep quotes); select - json_query(cast('{"rec": "{1,2,3}"}' as jsonb), '$.rec' - returning int[] - keep quotes - error on error); + json_query( + cast('{"rec": "{1,2,3}"}' as jsonb), + '$.rec' + returning int[] + keep quotes + error on error + ); select json_query(cast('{"rec": "[1,2]"}' as jsonb), '$.rec' returning int4range omit quotes); select json_query(cast('{"rec": "[1,2]"}' as jsonb), '$.rec' returning int4range keep quotes); select - json_query(cast('{"rec": "[1,2]"}' as jsonb), '$.rec' - returning int4range - keep quotes - error on error); + json_query( + cast('{"rec": "[1,2]"}' as jsonb), + '$.rec' + returning int4range + keep quotes + error on error + ); create domain qf_char_domain as char(1); @@ -456,11 +507,13 @@ select json_query(cast('[3,4]' as jsonb), '$[*]' returning anyarray empty object select x, y, - json_query(cast('[1,2,3,4,5,null]' as jsonb), '$[*] ? (@ >= $x && @ <= $y)' - passing x as x, - y as y - with conditional wrapper - empty array on empty) + json_query( + cast('[1,2,3,4,5,null]' as jsonb), + '$[*] ? (@ >= $x && @ <= $y)' + passing x as x, y as y + with conditional wrapper + empty array on empty + ) as list from generate_series(0, 4) as x, @@ -469,20 +522,29 @@ from create type comp_abc as (a text, b int, c timestamp); select - json_query(cast('{"rec": "(abc,42,01.02.2003)"}' as jsonb), '$.rec' - returning comp_abc - omit quotes); + json_query( + cast('{"rec": "(abc,42,01.02.2003)"}' as jsonb), + '$.rec' + returning comp_abc + omit quotes + ); select - json_query(cast('{"rec": "(abc,42,01.02.2003)"}' as jsonb), '$.rec' - returning comp_abc - keep quotes); + json_query( + cast('{"rec": "(abc,42,01.02.2003)"}' as jsonb), + '$.rec' + returning comp_abc + keep quotes + ); select - json_query(cast('{"rec": "(abc,42,01.02.2003)"}' as jsonb), '$.rec' - returning comp_abc - keep quotes - error on error); + json_query( + cast('{"rec": "(abc,42,01.02.2003)"}' as jsonb), + '$.rec' + returning comp_abc + keep quotes + error on error + ); drop type comp_abc; @@ -491,57 +553,80 @@ create type sqljsonb_rec as (a int, t text, js json, jb jsonb, jsa json[]); create type sqljsonb_reca as (reca sqljsonb_rec[]); select - json_query(cast('[{"a": 1, "b": "foo", "t": "aaa", "js": [1, "2", {}], "jb": {"x": [1, "2", {}]}}, {"a": 2}]' - as jsonb), '$[0]' - returning sqljsonb_rec); + json_query( + cast('[{"a": 1, "b": "foo", "t": "aaa", "js": [1, "2", {}], "jb": {"x": [1, "2", {}]}}, {"a": 2}]' + as jsonb), + '$[0]' + returning sqljsonb_rec + ); select - json_query(cast('[{"a": "a", "b": "foo", "t": "aaa", "js": [1, "2", {}], "jb": {"x": [1, "2", {}]}}, {"a": 2}]' - as jsonb), '$[0]' - returning sqljsonb_rec - error on error); + json_query( + cast('[{"a": "a", "b": "foo", "t": "aaa", "js": [1, "2", {}], "jb": {"x": [1, "2", {}]}}, {"a": 2}]' + as jsonb), + '$[0]' + returning sqljsonb_rec + error on error + ); select - json_query(cast('[{"a": "a", "b": "foo", "t": "aaa", "js": [1, "2", {}], "jb": {"x": [1, "2", {}]}}, {"a": 2}]' - as jsonb), '$[0]' - returning sqljsonb_rec); + json_query( + cast('[{"a": "a", "b": "foo", "t": "aaa", "js": [1, "2", {}], "jb": {"x": [1, "2", {}]}}, {"a": 2}]' + as jsonb), + '$[0]' + returning sqljsonb_rec + ); select * from unnest( - (json_query(cast('{"jsa": [{"a": 1, "b": ["foo"]}, {"a": 2, "c": {}}, 123]}' as jsonb), '$' - returning sqljsonb_rec)).jsa + (json_query( + cast('{"jsa": [{"a": 1, "b": ["foo"]}, {"a": 2, "c": {}}, 123]}' as jsonb), + '$' + returning sqljsonb_rec + )).jsa ); select * from unnest( - (json_query(cast('{"reca": [{"a": 1, "t": ["foo", []]}, {"a": 2, "jb": [{}, true]}]}' - as jsonb), '$' - returning sqljsonb_reca)).reca + (json_query( + cast('{"reca": [{"a": 1, "t": ["foo", []]}, {"a": 2, "jb": [{}, true]}]}' as jsonb), + '$' + returning sqljsonb_reca + )).reca ); select - json_query(cast('[{"a": 1, "b": "foo", "t": "aaa", "js": [1, "2", {}], "jb": {"x": [1, "2", {}]}}, {"a": 2}]' - as jsonb), '$[0]' - returning jsonpath); + json_query( + cast('[{"a": 1, "b": "foo", "t": "aaa", "js": [1, "2", {}], "jb": {"x": [1, "2", {}]}}, {"a": 2}]' + as jsonb), + '$[0]' + returning jsonpath + ); select - json_query(cast('[{"a": 1, "b": "foo", "t": "aaa", "js": [1, "2", {}], "jb": {"x": [1, "2", {}]}}, {"a": 2}]' - as jsonb), '$[0]' - returning jsonpath - error on error); + json_query( + cast('[{"a": 1, "b": "foo", "t": "aaa", "js": [1, "2", {}], "jb": {"x": [1, "2", {}]}}, {"a": 2}]' + as jsonb), + '$[0]' + returning jsonpath + error on error + ); select json_query(cast('[1,2,null,"3"]' as jsonb), '$[*]' returning int[] with unconditional wrapper); select - json_query(cast('[1,2,null,"a"]' as jsonb), '$[*]' - returning int[] - with unconditional wrapper - error on error); + json_query( + cast('[1,2,null,"a"]' as jsonb), + '$[*]' + returning int[] + with unconditional wrapper + error on error + ); select json_query(cast('[1,2,null,"a"]' as jsonb), '$[*]' returning int[] with unconditional wrapper); @@ -550,8 +635,11 @@ select * from unnest( - json_query(cast('[{"a": 1, "t": ["foo", []]}, {"a": 2, "jb": [{}, true]}]' as jsonb), '$' - returning sqljsonb_rec[]) + json_query( + cast('[{"a": 1, "t": ["foo", []]}, {"a": 2, "jb": [{}, true]}]' as jsonb), + '$' + returning sqljsonb_rec[] + ) ); select json_query(cast('{"a": 1}' as jsonb), '$.a' returning sqljsonb_int_not_null); @@ -559,24 +647,36 @@ select json_query(cast('{"a": 1}' as jsonb), '$.a' returning sqljsonb_int_not_nu select json_query(cast('{"a": 1}' as jsonb), '$.b' returning sqljsonb_int_not_null); select - json_query(cast('{"a": 1}' as jsonb), '$.b' - returning sqljsonb_int_not_null - error on empty - error on error); + json_query( + cast('{"a": 1}' as jsonb), + '$.b' + returning sqljsonb_int_not_null + error on empty + error on error + ); select - json_query(cast('null' as jsonb), '$ts' - passing cast('2018-02-21 12:34:56 +10' as timestamptz) as ts); + json_query( + cast('null' as jsonb), + '$ts' + passing cast('2018-02-21 12:34:56 +10' as timestamptz) as ts + ); select - json_query(cast('null' as jsonb), '$ts' - passing cast('2018-02-21 12:34:56 +10' as timestamptz) as ts - returning json); + json_query( + cast('null' as jsonb), + '$ts' + passing cast('2018-02-21 12:34:56 +10' as timestamptz) as ts + returning json + ); select - json_query(cast('null' as jsonb), '$ts' - passing cast('2018-02-21 12:34:56 +10' as timestamptz) as ts - returning jsonb); + json_query( + cast('null' as jsonb), + '$ts' + passing cast('2018-02-21 12:34:56 +10' as timestamptz) as ts + returning jsonb + ); create table test_jsonb_constraints ( js text, @@ -585,10 +685,11 @@ create table test_jsonb_constraints ( default json_query(cast('[1,2]' as jsonb), '$[*]' with unconditional wrapper) constraint test_jsonb_constraint1 check (js is json) constraint test_jsonb_constraint2 check ( - json_exists(cast(js as jsonb), '$.a' - passing i + 5 as int, - cast(i as text) as "TXT", - array[1, 2, 3] as arr) + json_exists( + cast(js as jsonb), + '$.a' + passing i + 5 as int, cast(i as text) as "TXT", array[1, 2, 3] as arr + ) ) constraint test_jsonb_constraint3 check ( json_value(cast(js as jsonb), '$.a' returning int default '12' on empty error on error) > i @@ -797,43 +898,64 @@ drop function ret_setint; create domain queryfuncs_test_domain as text check (value <> 'foo'); select - json_value(cast('{"d1": "H"}' as jsonb), '$.a2' - returning queryfuncs_test_domain - default cast('foo' as queryfuncs_test_domain) on empty); + json_value( + cast('{"d1": "H"}' as jsonb), + '$.a2' + returning queryfuncs_test_domain + default cast('foo' as queryfuncs_test_domain) on empty + ); select - json_value(cast('{"d1": "H"}' as jsonb), '$.a2' - returning queryfuncs_test_domain - default cast('foo1' as queryfuncs_test_domain) on empty); + json_value( + cast('{"d1": "H"}' as jsonb), + '$.a2' + returning queryfuncs_test_domain + default cast('foo1' as queryfuncs_test_domain) on empty + ); select - json_value(cast('{"d1": "H"}' as jsonb), '$.a2' - returning queryfuncs_test_domain - default cast(cast('"foo1"' as jsonb) as text) on empty); + json_value( + cast('{"d1": "H"}' as jsonb), + '$.a2' + returning queryfuncs_test_domain + default cast(cast('"foo1"' as jsonb) as text) on empty + ); select - json_value(cast('{"d1": "foo"}' as jsonb), '$.a2' - returning queryfuncs_test_domain - default cast('foo1' as queryfuncs_test_domain) on empty); + json_value( + cast('{"d1": "foo"}' as jsonb), + '$.a2' + returning queryfuncs_test_domain + default cast('foo1' as queryfuncs_test_domain) on empty + ); select json_query('"a"', '$.a' returning int default cast((select '"1"') as jsonb) on error); select - json_query('"a"', '$.a' - returning queryfuncs_test_domain - default cast((select '"1"') as queryfuncs_test_domain) on error); + json_query( + '"a"', + '$.a' + returning queryfuncs_test_domain + default cast((select '"1"') as queryfuncs_test_domain) on error + ); select json_query('"a"', '$.a' returning int default cast(cast((select 1) as oid) as int) on error); select - json_query('"a"', '$.a' - returning int[] - default cast(cast((select '{1}') as oid[]) as int[]) on error); + json_query( + '"a"', + '$.a' + returning int[] + default cast(cast((select '{1}') as oid[]) as int[]) on error + ); select - json_query('"a"', '$.a' - returning int[] - default cast((select '{1}') as text) collate "C" on error); + json_query( + '"a"', + '$.a' + returning int[] + default cast((select '{1}') as text) collate "C" on error + ); create table someparent (a int); @@ -841,9 +963,12 @@ create table somechild () inherits (someparent); select - json_query('"a"', '$.a' - returning someparent - default cast(cast((select '(1)') as somechild) as someparent) on error); + json_query( + '"a"', + '$.a' + returning someparent + default cast(cast((select '(1)') as somechild) as someparent) on error + ); drop domain queryfuncs_test_domain; diff --git a/crates/pgls_pretty_print/tests/snapshots/multi/tests__sqljson_queryfuncs_80.snap b/crates/pgls_pretty_print/tests/snapshots/multi/tests__sqljson_queryfuncs_80.snap index 3d5bdb14c..4fd55c3b2 100644 --- a/crates/pgls_pretty_print/tests/snapshots/multi/tests__sqljson_queryfuncs_80.snap +++ b/crates/pgls_pretty_print/tests/snapshots/multi/tests__sqljson_queryfuncs_80.snap @@ -39,22 +39,32 @@ select json_exists(cast('{"a": {"b": 1}}' as jsonb), '$.a.b'); select json_exists(cast('{"a": 1, "b": 2}' as jsonb), '$.a.b'); select - json_exists(cast('{"a": 1, "b": 2}' as jsonb), '$.* ? (@ > $x)' - passing 1 as x); + json_exists( + cast('{"a": 1, "b": 2}' as jsonb), + '$.* ? (@ > $x)' + passing 1 as x + ); select - json_exists(cast('{"a": 1, "b": 2}' as jsonb), '$.* ? (@ > $x)' - passing '1' as x); + json_exists( + cast('{"a": 1, "b": 2}' as jsonb), + '$.* ? (@ > $x)' + passing '1' as x + ); select - json_exists(cast('{"a": 1, "b": 2}' as jsonb), '$.* ? (@ > $x && @ < $y)' - passing 0 as x, - 2 as y); + json_exists( + cast('{"a": 1, "b": 2}' as jsonb), + '$.* ? (@ > $x && @ < $y)' + passing 0 as x, 2 as y + ); select - json_exists(cast('{"a": 1, "b": 2}' as jsonb), '$.* ? (@ > $x && @ < $y)' - passing 0 as x, - 1 as y); + json_exists( + cast('{"a": 1, "b": 2}' as jsonb), + '$.* ? (@ > $x && @ < $y)' + passing 0 as x, 1 as y + ); select json_exists(cast('1' as jsonb), '$ > 2'); @@ -126,27 +136,39 @@ create domain sqljsonb_int_not_null as int not null; select json_value(cast('null' as jsonb), '$' returning sqljsonb_int_not_null); select - json_value(cast('null' as jsonb), '$' - returning sqljsonb_int_not_null - error on error); + json_value( + cast('null' as jsonb), + '$' + returning sqljsonb_int_not_null + error on error + ); select - json_value(cast('null' as jsonb), '$' - returning sqljsonb_int_not_null - default 2 on empty - error on error); + json_value( + cast('null' as jsonb), + '$' + returning sqljsonb_int_not_null + default 2 on empty + error on error + ); select - json_value(cast('1' as jsonb), '$.a' - returning sqljsonb_int_not_null - default 2 on empty - error on error); + json_value( + cast('1' as jsonb), + '$.a' + returning sqljsonb_int_not_null + default 2 on empty + error on error + ); select - json_value(cast('1' as jsonb), '$.a' - returning sqljsonb_int_not_null - default null on empty - error on error); + json_value( + cast('1' as jsonb), + '$.a' + returning sqljsonb_int_not_null + default null on empty + error on error + ); create type rainbow as enum ( 'red', @@ -162,9 +184,12 @@ create domain rgb as rainbow check (value in ('red', 'green', 'blue')); select json_value(cast('"purple"' as jsonb), 'lax $[*]' returning rgb); select - json_value(cast('"purple"' as jsonb), 'lax $[*]' - returning rgb - error on error); + json_value( + cast('"purple"' as jsonb), + 'lax $[*]' + returning rgb + error on error + ); select json_value(cast('[]' as jsonb), '$'); @@ -194,9 +219,12 @@ select json_value(cast('1' as jsonb), 'lax $.a' null on empty default '2' on error); select - json_value(cast('1' as jsonb), 'lax $.a' - default '2' on empty - default '3' on error); + json_value( + cast('1' as jsonb), + 'lax $.a' + default '2' on empty + default '3' on error + ); select json_value(cast('1' as jsonb), 'lax $.a' error on empty default '3' on error); @@ -208,14 +236,20 @@ select json_value(cast('[1,2]' as jsonb), '$[*]' default '0' on error); select json_value(cast('[" "]' as jsonb), '$[*]' returning int error on error); select - json_value(cast('[" "]' as jsonb), '$[*]' - returning int - default 2 + 3 on error); + json_value( + cast('[" "]' as jsonb), + '$[*]' + returning int + default 2 + 3 on error + ); select - json_value(cast('["1"]' as jsonb), '$[*]' - returning int - default 2 + 3 on error); + json_value( + cast('["1"]' as jsonb), + '$[*]' + returning int + default 2 + 3 on error + ); select json_value(cast('["1"]' as jsonb), '$[*]' returning int format json); @@ -223,73 +257,112 @@ select json_value(cast('["1"]' as jsonb), '$[*]' returning record); select x, - json_value(cast('{"a": 1, "b": 2}' as jsonb), '$.* ? (@ > $x)' - passing x as x - returning int - default -1 on empty - default -2 on error) + json_value( + cast('{"a": 1, "b": 2}' as jsonb), + '$.* ? (@ > $x)' + passing x as x + returning int + default -1 on empty + default -2 on error + ) as y from generate_series(0, 2) as x; select - json_value(cast('null' as jsonb), '$a' - passing cast(' (1, 2 )' as point) as a); + json_value( + cast('null' as jsonb), + '$a' + passing cast(' (1, 2 )' as point) as a + ); select - json_value(cast('null' as jsonb), '$a' - passing cast(' (1, 2 )' as point) as a - returning point); + json_value( + cast('null' as jsonb), + '$a' + passing cast(' (1, 2 )' as point) as a + returning point + ); select - json_value(cast('null' as jsonb), '$a' - passing cast(' (1, 2 )' as point) as a - returning point - error on error); + json_value( + cast('null' as jsonb), + '$a' + passing cast(' (1, 2 )' as point) as a + returning point + error on error + ); select - json_value(cast('null' as jsonb), '$ts' - passing cast('2018-02-21 12:34:56 +10' as timestamptz) as ts); + json_value( + cast('null' as jsonb), + '$ts' + passing cast('2018-02-21 12:34:56 +10' as timestamptz) as ts + ); select - json_value(cast('null' as jsonb), '$ts' - passing cast('2018-02-21 12:34:56 +10' as timestamptz) as ts - returning timestamptz); + json_value( + cast('null' as jsonb), + '$ts' + passing cast('2018-02-21 12:34:56 +10' as timestamptz) as ts + returning timestamptz + ); select - json_value(cast('null' as jsonb), '$ts' - passing cast('2018-02-21 12:34:56 +10' as timestamptz) as ts - returning timestamp); + json_value( + cast('null' as jsonb), + '$ts' + passing cast('2018-02-21 12:34:56 +10' as timestamptz) as ts + returning timestamp + ); select - json_value(cast('null' as jsonb), '$ts' - passing cast('2018-02-21 12:34:56 +10' as date) as ts - returning date); + json_value( + cast('null' as jsonb), + '$ts' + passing cast('2018-02-21 12:34:56 +10' as date) as ts + returning date + ); select - json_value(cast('null' as jsonb), '$ts' - passing cast('2018-02-21 12:34:56 +10' as time) as ts - returning time); + json_value( + cast('null' as jsonb), + '$ts' + passing cast('2018-02-21 12:34:56 +10' as time) as ts + returning time + ); select - json_value(cast('null' as jsonb), '$ts' - passing cast('2018-02-21 12:34:56 +10' as timetz) as ts - returning timetz); + json_value( + cast('null' as jsonb), + '$ts' + passing cast('2018-02-21 12:34:56 +10' as timetz) as ts + returning timetz + ); select - json_value(cast('null' as jsonb), '$ts' - passing cast('2018-02-21 12:34:56 +10' as timestamp) as ts - returning timestamp); + json_value( + cast('null' as jsonb), + '$ts' + passing cast('2018-02-21 12:34:56 +10' as timestamp) as ts + returning timestamp + ); select - json_value(cast('null' as jsonb), '$ts' - passing cast('2018-02-21 12:34:56 +10' as timestamptz) as ts - returning json); + json_value( + cast('null' as jsonb), + '$ts' + passing cast('2018-02-21 12:34:56 +10' as timestamptz) as ts + returning json + ); select - json_value(cast('null' as jsonb), '$ts' - passing cast('2018-02-21 12:34:56 +10' as timestamptz) as ts - returning jsonb); + json_value( + cast('null' as jsonb), + '$ts' + passing cast('2018-02-21 12:34:56 +10' as timestamptz) as ts + returning jsonb + ); select json_value('{"a": 1.234}', '$.a' returning int error on error); @@ -349,38 +422,53 @@ select json_query(cast('"aaa"' as jsonb), '$' returning text omit quotes); select json_query(cast('"aaa"' as jsonb), '$' omit quotes error on error); select - json_query(cast('"aaa"' as jsonb), '$' - returning json - omit quotes - error on error); + json_query( + cast('"aaa"' as jsonb), + '$' + returning json + omit quotes + error on error + ); select - json_query(cast('"aaa"' as jsonb), '$' - returning bytea format json - omit quotes - error on error); + json_query( + cast('"aaa"' as jsonb), + '$' + returning bytea format json + omit quotes + error on error + ); select json_query(cast('"aaa"' as jsonb), '$' returning char(3) error on error); select json_query(cast('"aaa"' as jsonb), '$' returning char(3)); select - json_query(cast('"aaa"' as jsonb), '$' - returning char(3) - omit quotes - error on error); + json_query( + cast('"aaa"' as jsonb), + '$' + returning char(3) + omit quotes + error on error + ); select - json_query(cast('"aaa"' as jsonb), '$.a' - returning char(2) - omit quotes - default 'bb' on empty); + json_query( + cast('"aaa"' as jsonb), + '$.a' + returning char(2) + omit quotes + default 'bb' on empty + ); select - json_query(cast('"aaa"' as jsonb), '$.a' - returning char(2) - omit quotes - default cast('"bb"' as jsonb) on empty); + json_query( + cast('"aaa"' as jsonb), + '$.a' + returning char(2) + omit quotes + default cast('"bb"' as jsonb) on empty + ); select json_query(cast('[1]' as jsonb), '$' with unconditional wrapper omit quotes); @@ -389,71 +477,104 @@ select json_query(cast('[1]' as jsonb), '$' with conditional wrapper omit quotes); select - json_query(cast('["1"]' as jsonb), '$[*]' - with conditional wrapper - keep quotes); + json_query( + cast('["1"]' as jsonb), + '$[*]' + with conditional wrapper + keep quotes + ); select - json_query(cast('["1"]' as jsonb), '$[*]' - with unconditional wrapper - keep quotes); + json_query( + cast('["1"]' as jsonb), + '$[*]' + with unconditional wrapper + keep quotes + ); select - json_query(cast('["1"]' as jsonb), '$[*]' - with unconditional wrapper - keep quotes); + json_query( + cast('["1"]' as jsonb), + '$[*]' + with unconditional wrapper + keep quotes + ); select json_query(cast('["1"]' as jsonb), '$[*]' without wrapper omit quotes); select json_query(cast('["1"]' as jsonb), '$[*]' without wrapper keep quotes); select - json_query(cast('{"rec": "{1,2,3}"}' as jsonb), '$.rec' - returning int[] - omit quotes); + json_query( + cast('{"rec": "{1,2,3}"}' as jsonb), + '$.rec' + returning int[] + omit quotes + ); select - json_query(cast('{"rec": "{1,2,3}"}' as jsonb), '$.rec' - returning int[] - keep quotes); + json_query( + cast('{"rec": "{1,2,3}"}' as jsonb), + '$.rec' + returning int[] + keep quotes + ); select - json_query(cast('{"rec": "{1,2,3}"}' as jsonb), '$.rec' - returning int[] - keep quotes - error on error); + json_query( + cast('{"rec": "{1,2,3}"}' as jsonb), + '$.rec' + returning int[] + keep quotes + error on error + ); select - json_query(cast('{"rec": "[1,2]"}' as jsonb), '$.rec' - returning int4range - omit quotes); + json_query( + cast('{"rec": "[1,2]"}' as jsonb), + '$.rec' + returning int4range + omit quotes + ); select - json_query(cast('{"rec": "[1,2]"}' as jsonb), '$.rec' - returning int4range - keep quotes); + json_query( + cast('{"rec": "[1,2]"}' as jsonb), + '$.rec' + returning int4range + keep quotes + ); select - json_query(cast('{"rec": "[1,2]"}' as jsonb), '$.rec' - returning int4range - keep quotes - error on error); + json_query( + cast('{"rec": "[1,2]"}' as jsonb), + '$.rec' + returning int4range + keep quotes + error on error + ); create domain qf_char_domain as char(1); create domain qf_jsonb_domain as jsonb; select - json_query(cast('"1"' as jsonb), '$' - returning qf_char_domain - omit quotes - error on error); + json_query( + cast('"1"' as jsonb), + '$' + returning qf_char_domain + omit quotes + error on error + ); select - json_query(cast('"1"' as jsonb), '$' - returning qf_jsonb_domain - omit quotes - error on error); + json_query( + cast('"1"' as jsonb), + '$' + returning qf_jsonb_domain + omit quotes + error on error + ); drop domain qf_char_domain, qf_jsonb_domain; @@ -506,34 +627,52 @@ select json_query(cast('[1,2]' as jsonb), '$' returning bytea); select json_query(cast('[1,2]' as jsonb), '$' returning bytea format json); select - json_query(cast('[1,2]' as jsonb), '$[*]' - returning bytea - empty object on error); + json_query( + cast('[1,2]' as jsonb), + '$[*]' + returning bytea + empty object on error + ); select - json_query(cast('[1,2]' as jsonb), '$[*]' - returning bytea format json - empty object on error); + json_query( + cast('[1,2]' as jsonb), + '$[*]' + returning bytea format json + empty object on error + ); select - json_query(cast('[1,2]' as jsonb), '$[*]' - returning json - empty object on error); + json_query( + cast('[1,2]' as jsonb), + '$[*]' + returning json + empty object on error + ); select - json_query(cast('[1,2]' as jsonb), '$[*]' - returning jsonb - empty object on error); + json_query( + cast('[1,2]' as jsonb), + '$[*]' + returning jsonb + empty object on error + ); select - json_query(cast('[3,4]' as jsonb), '$[*]' - returning bigint[] - empty object on error); + json_query( + cast('[3,4]' as jsonb), + '$[*]' + returning bigint[] + empty object on error + ); select - json_query(cast('"[3,4]"' as jsonb), '$[*]' - returning bigint[] - empty object on error); + json_query( + cast('"[3,4]"' as jsonb), + '$[*]' + returning bigint[] + empty object on error + ); select json_query(cast('"123.1"' as jsonb), '$' returning int2 error on error); @@ -552,30 +691,41 @@ select json_query(cast('"123.1"' as jsonb), '$' returning float8 error on error); select - json_query(cast('"123.1"' as jsonb), '$' - returning int2 - omit quotes - error on error); + json_query( + cast('"123.1"' as jsonb), + '$' + returning int2 + omit quotes + error on error + ); select - json_query(cast('"123.1"' as jsonb), '$' - returning float8 - omit quotes - error on error); + json_query( + cast('"123.1"' as jsonb), + '$' + returning float8 + omit quotes + error on error + ); select - json_query(cast('[3,4]' as jsonb), '$[*]' - returning anyarray - empty object on error); + json_query( + cast('[3,4]' as jsonb), + '$[*]' + returning anyarray + empty object on error + ); select x, y, - json_query(cast('[1,2,3,4,5,null]' as jsonb), '$[*] ? (@ >= $x && @ <= $y)' - passing x as x, - y as y - with conditional wrapper - empty array on empty) + json_query( + cast('[1,2,3,4,5,null]' as jsonb), + '$[*] ? (@ >= $x && @ <= $y)' + passing x as x, y as y + with conditional wrapper + empty array on empty + ) as list from generate_series(0, 4) as x, @@ -584,20 +734,29 @@ from create type comp_abc as (a text, b int, c timestamp); select - json_query(cast('{"rec": "(abc,42,01.02.2003)"}' as jsonb), '$.rec' - returning comp_abc - omit quotes); + json_query( + cast('{"rec": "(abc,42,01.02.2003)"}' as jsonb), + '$.rec' + returning comp_abc + omit quotes + ); select - json_query(cast('{"rec": "(abc,42,01.02.2003)"}' as jsonb), '$.rec' - returning comp_abc - keep quotes); + json_query( + cast('{"rec": "(abc,42,01.02.2003)"}' as jsonb), + '$.rec' + returning comp_abc + keep quotes + ); select - json_query(cast('{"rec": "(abc,42,01.02.2003)"}' as jsonb), '$.rec' - returning comp_abc - keep quotes - error on error); + json_query( + cast('{"rec": "(abc,42,01.02.2003)"}' as jsonb), + '$.rec' + returning comp_abc + keep quotes + error on error + ); drop type comp_abc; @@ -606,73 +765,105 @@ create type sqljsonb_rec as (a int, t text, js json, jb jsonb, jsa json[]); create type sqljsonb_reca as (reca sqljsonb_rec[]); select - json_query(cast('[{"a": 1, "b": "foo", "t": "aaa", "js": [1, "2", {}], "jb": {"x": [1, "2", {}]}}, {"a": 2}]' - as jsonb), '$[0]' - returning sqljsonb_rec); + json_query( + cast('[{"a": 1, "b": "foo", "t": "aaa", "js": [1, "2", {}], "jb": {"x": [1, "2", {}]}}, {"a": 2}]' + as jsonb), + '$[0]' + returning sqljsonb_rec + ); select - json_query(cast('[{"a": "a", "b": "foo", "t": "aaa", "js": [1, "2", {}], "jb": {"x": [1, "2", {}]}}, {"a": 2}]' - as jsonb), '$[0]' - returning sqljsonb_rec - error on error); + json_query( + cast('[{"a": "a", "b": "foo", "t": "aaa", "js": [1, "2", {}], "jb": {"x": [1, "2", {}]}}, {"a": 2}]' + as jsonb), + '$[0]' + returning sqljsonb_rec + error on error + ); select - json_query(cast('[{"a": "a", "b": "foo", "t": "aaa", "js": [1, "2", {}], "jb": {"x": [1, "2", {}]}}, {"a": 2}]' - as jsonb), '$[0]' - returning sqljsonb_rec); + json_query( + cast('[{"a": "a", "b": "foo", "t": "aaa", "js": [1, "2", {}], "jb": {"x": [1, "2", {}]}}, {"a": 2}]' + as jsonb), + '$[0]' + returning sqljsonb_rec + ); select * from unnest( - (json_query(cast('{"jsa": [{"a": 1, "b": ["foo"]}, {"a": 2, "c": {}}, 123]}' - as jsonb), '$' - returning sqljsonb_rec)).jsa + (json_query( + cast('{"jsa": [{"a": 1, "b": ["foo"]}, {"a": 2, "c": {}}, 123]}' + as jsonb), + '$' + returning sqljsonb_rec + )).jsa ); select * from unnest( - (json_query(cast('{"reca": [{"a": 1, "t": ["foo", []]}, {"a": 2, "jb": [{}, true]}]}' - as jsonb), '$' - returning sqljsonb_reca)).reca + (json_query( + cast('{"reca": [{"a": 1, "t": ["foo", []]}, {"a": 2, "jb": [{}, true]}]}' + as jsonb), + '$' + returning sqljsonb_reca + )).reca ); select - json_query(cast('[{"a": 1, "b": "foo", "t": "aaa", "js": [1, "2", {}], "jb": {"x": [1, "2", {}]}}, {"a": 2}]' - as jsonb), '$[0]' - returning jsonpath); + json_query( + cast('[{"a": 1, "b": "foo", "t": "aaa", "js": [1, "2", {}], "jb": {"x": [1, "2", {}]}}, {"a": 2}]' + as jsonb), + '$[0]' + returning jsonpath + ); select - json_query(cast('[{"a": 1, "b": "foo", "t": "aaa", "js": [1, "2", {}], "jb": {"x": [1, "2", {}]}}, {"a": 2}]' - as jsonb), '$[0]' - returning jsonpath - error on error); + json_query( + cast('[{"a": 1, "b": "foo", "t": "aaa", "js": [1, "2", {}], "jb": {"x": [1, "2", {}]}}, {"a": 2}]' + as jsonb), + '$[0]' + returning jsonpath + error on error + ); select - json_query(cast('[1,2,null,"3"]' as jsonb), '$[*]' - returning int[] - with unconditional wrapper); + json_query( + cast('[1,2,null,"3"]' as jsonb), + '$[*]' + returning int[] + with unconditional wrapper + ); select - json_query(cast('[1,2,null,"a"]' as jsonb), '$[*]' - returning int[] - with unconditional wrapper - error on error); + json_query( + cast('[1,2,null,"a"]' as jsonb), + '$[*]' + returning int[] + with unconditional wrapper + error on error + ); select - json_query(cast('[1,2,null,"a"]' as jsonb), '$[*]' - returning int[] - with unconditional wrapper); + json_query( + cast('[1,2,null,"a"]' as jsonb), + '$[*]' + returning int[] + with unconditional wrapper + ); select * from unnest( - json_query(cast('[{"a": 1, "t": ["foo", []]}, {"a": 2, "jb": [{}, true]}]' - as jsonb), '$' - returning sqljsonb_rec[]) + json_query( + cast('[{"a": 1, "t": ["foo", []]}, {"a": 2, "jb": [{}, true]}]' as jsonb), + '$' + returning sqljsonb_rec[] + ) ); select @@ -682,56 +873,81 @@ select json_query(cast('{"a": 1}' as jsonb), '$.b' returning sqljsonb_int_not_null); select - json_query(cast('{"a": 1}' as jsonb), '$.b' - returning sqljsonb_int_not_null - error on empty - error on error); + json_query( + cast('{"a": 1}' as jsonb), + '$.b' + returning sqljsonb_int_not_null + error on empty + error on error + ); select - json_query(cast('null' as jsonb), '$ts' - passing cast('2018-02-21 12:34:56 +10' as timestamptz) as ts); + json_query( + cast('null' as jsonb), + '$ts' + passing cast('2018-02-21 12:34:56 +10' as timestamptz) as ts + ); select - json_query(cast('null' as jsonb), '$ts' - passing cast('2018-02-21 12:34:56 +10' as timestamptz) as ts - returning json); + json_query( + cast('null' as jsonb), + '$ts' + passing cast('2018-02-21 12:34:56 +10' as timestamptz) as ts + returning json + ); select - json_query(cast('null' as jsonb), '$ts' - passing cast('2018-02-21 12:34:56 +10' as timestamptz) as ts - returning jsonb); + json_query( + cast('null' as jsonb), + '$ts' + passing cast('2018-02-21 12:34:56 +10' as timestamptz) as ts + returning jsonb + ); create table test_jsonb_constraints ( js text, i int, x jsonb - default json_query(cast('[1,2]' as jsonb), '$[*]' - with unconditional wrapper) + default json_query( + cast('[1,2]' as jsonb), + '$[*]' + with unconditional wrapper + ) constraint test_jsonb_constraint1 check (js is json) constraint test_jsonb_constraint2 check ( - json_exists(cast(js as jsonb), '$.a' - passing i + 5 as int, - cast(i as text) as "TXT", - array[1, 2, 3] as arr) + json_exists( + cast(js as jsonb), + '$.a' + passing i + 5 as int, cast(i as text) as "TXT", array[1, 2, 3] as arr + ) ) constraint test_jsonb_constraint3 check ( - json_value(cast(js as jsonb), '$.a' - returning int - default '12' on empty - error on error) > + json_value( + cast(js as jsonb), + '$.a' + returning int + default '12' on empty + error on error + ) > i ) constraint test_jsonb_constraint4 check ( - json_query(cast(js as jsonb), '$.a' - with conditional wrapper - empty object on error) = + json_query( + cast(js as jsonb), + '$.a' + with conditional wrapper + empty object on error + ) = cast('[10]' as jsonb) ) constraint test_jsonb_constraint5 check ( - json_query(cast(js as jsonb), '$.a' - returning char(5) - omit quotes - empty array on empty) > + json_query( + cast(js as jsonb), + '$.a' + returning char(5) + omit quotes + empty array on empty + ) > 'a' collate "C" ) ); @@ -880,17 +1096,26 @@ create index on test_jsonb_mutability using btree ( ); create index on test_jsonb_mutability using btree ( - (json_query(js, '$.a ? (@.datetime("HH:MI TZH") < $.datetime("YY-MM-DD HH:MI"))')) + (json_query( + js, + '$.a ? (@.datetime("HH:MI TZH") < $.datetime("YY-MM-DD HH:MI"))' + )) ); create index on test_jsonb_mutability using btree ( - (json_query(js, '$.datetime("HH:MI TZH") < $x' - passing cast('12:34' as timetz) as x)) + (json_query( + js, + '$.datetime("HH:MI TZH") < $x' + passing cast('12:34' as timetz) as x + )) ); create index on test_jsonb_mutability using btree ( - (json_query(js, '$.datetime("HH:MI TZH") < $y' - passing cast('12:34' as timetz) as x)) + (json_query( + js, + '$.datetime("HH:MI TZH") < $y' + passing cast('12:34' as timetz) as x + )) ); create index on test_jsonb_mutability using btree ( @@ -902,28 +1127,43 @@ create index on test_jsonb_mutability using btree ( ); create index on test_jsonb_mutability using btree ( - (json_query(js, '$.datetime() ? (@ == $x)' - passing cast('12:34' as time) as x)) + (json_query( + js, + '$.datetime() ? (@ == $x)' + passing cast('12:34' as time) as x + )) ); create index on test_jsonb_mutability using btree ( - (json_query(js, '$.datetime("YY-MM-DD") ? (@ == $x)' - passing cast('2020-07-14' as date) as x)) + (json_query( + js, + '$.datetime("YY-MM-DD") ? (@ == $x)' + passing cast('2020-07-14' as date) as x + )) ); create index on test_jsonb_mutability using btree ( - (json_query(js, '$[1, $.a ? (@.datetime() == $x)]' - passing cast('12:34' as time) as x)) + (json_query( + js, + '$[1, $.a ? (@.datetime() == $x)]' + passing cast('12:34' as time) as x + )) ); create index on test_jsonb_mutability using btree ( - (json_query(js, '$[1, 0 to $.a ? (@.datetime() == $x)]' - passing cast('12:34' as time) as x)) + (json_query( + js, + '$[1, 0 to $.a ? (@.datetime() == $x)]' + passing cast('12:34' as time) as x + )) ); create index on test_jsonb_mutability using btree ( - (json_query(js, '$[1, $.a ? (@.datetime("HH:MI") == $x)]' - passing cast('12:34' as time) as x)) + (json_query( + js, + '$[1, $.a ? (@.datetime("HH:MI") == $x)]' + passing cast('12:34' as time) as x + )) ); create index on test_jsonb_mutability using btree ( @@ -967,49 +1207,76 @@ drop function ret_setint; create domain queryfuncs_test_domain as text check (value <> 'foo'); select - json_value(cast('{"d1": "H"}' as jsonb), '$.a2' - returning queryfuncs_test_domain - default cast('foo' as queryfuncs_test_domain) on empty); + json_value( + cast('{"d1": "H"}' as jsonb), + '$.a2' + returning queryfuncs_test_domain + default cast('foo' as queryfuncs_test_domain) on empty + ); select - json_value(cast('{"d1": "H"}' as jsonb), '$.a2' - returning queryfuncs_test_domain - default cast('foo1' as queryfuncs_test_domain) on empty); + json_value( + cast('{"d1": "H"}' as jsonb), + '$.a2' + returning queryfuncs_test_domain + default cast('foo1' as queryfuncs_test_domain) on empty + ); select - json_value(cast('{"d1": "H"}' as jsonb), '$.a2' - returning queryfuncs_test_domain - default cast(cast('"foo1"' as jsonb) as text) on empty); + json_value( + cast('{"d1": "H"}' as jsonb), + '$.a2' + returning queryfuncs_test_domain + default cast(cast('"foo1"' as jsonb) as text) on empty + ); select - json_value(cast('{"d1": "foo"}' as jsonb), '$.a2' - returning queryfuncs_test_domain - default cast('foo1' as queryfuncs_test_domain) on empty); + json_value( + cast('{"d1": "foo"}' as jsonb), + '$.a2' + returning queryfuncs_test_domain + default cast('foo1' as queryfuncs_test_domain) on empty + ); select - json_query('"a"', '$.a' - returning int - default cast((select '"1"') as jsonb) on error); + json_query( + '"a"', + '$.a' + returning int + default cast((select '"1"') as jsonb) on error + ); select - json_query('"a"', '$.a' - returning queryfuncs_test_domain - default cast((select '"1"') as queryfuncs_test_domain) on error); + json_query( + '"a"', + '$.a' + returning queryfuncs_test_domain + default cast((select '"1"') as queryfuncs_test_domain) on error + ); select - json_query('"a"', '$.a' - returning int - default cast(cast((select 1) as oid) as int) on error); + json_query( + '"a"', + '$.a' + returning int + default cast(cast((select 1) as oid) as int) on error + ); select - json_query('"a"', '$.a' - returning int[] - default cast(cast((select '{1}') as oid[]) as int[]) on error); + json_query( + '"a"', + '$.a' + returning int[] + default cast(cast((select '{1}') as oid[]) as int[]) on error + ); select - json_query('"a"', '$.a' - returning int[] - default cast((select '{1}') as text) collate "C" on error); + json_query( + '"a"', + '$.a' + returning int[] + default cast((select '{1}') as text) collate "C" on error + ); create table someparent (a int); @@ -1017,9 +1284,12 @@ create table somechild () inherits (someparent); select - json_query('"a"', '$.a' - returning someparent - default cast(cast((select '(1)') as somechild) as someparent) on error); + json_query( + '"a"', + '$.a' + returning someparent + default cast(cast((select '(1)') as somechild) as someparent) on error + ); drop domain queryfuncs_test_domain; @@ -1030,14 +1300,20 @@ select json_exists(cast('{"a": 123}' as jsonb), '$' || '.' || 'a'); select json_value(cast('{"a": 123}' as jsonb), '$' || '.' || 'a'); select - json_value(cast('{"a": 123}' as jsonb), '$' || '.' || 'b' - default 'foo' on empty); + json_value( + cast('{"a": 123}' as jsonb), + '$' || '.' || 'b' + default 'foo' on empty + ); select json_query(cast('{"a": 123}' as jsonb), '$' || '.' || 'a'); select - json_query(cast('{"a": 123}' as jsonb), '$' || '.' || 'a' - with unconditional wrapper); + json_query( + cast('{"a": 123}' as jsonb), + '$' || '.' || 'a' + with unconditional wrapper + ); select json_query(cast('{"a": 123}' as jsonb), 'error' || ' ' || 'error'); @@ -1072,63 +1348,96 @@ create domain queryfuncs_char2 as char(2); create domain queryfuncs_char2_chk as char(2) check (value not in ('12')); select - json_query(cast('123' as jsonb), '$' - returning queryfuncs_char2 - error on error); + json_query( + cast('123' as jsonb), + '$' + returning queryfuncs_char2 + error on error + ); select - json_query(cast('123' as jsonb), '$' - returning queryfuncs_char2 - default '1' on error); + json_query( + cast('123' as jsonb), + '$' + returning queryfuncs_char2 + default '1' on error + ); select - json_query(cast('123' as jsonb), '$' - returning queryfuncs_char2_chk - error on error); + json_query( + cast('123' as jsonb), + '$' + returning queryfuncs_char2_chk + error on error + ); select - json_query(cast('123' as jsonb), '$' - returning queryfuncs_char2_chk - default '1' on error); + json_query( + cast('123' as jsonb), + '$' + returning queryfuncs_char2_chk + default '1' on error + ); select - json_value(cast('123' as jsonb), '$' - returning queryfuncs_char2 - error on error); + json_value( + cast('123' as jsonb), + '$' + returning queryfuncs_char2 + error on error + ); select - json_value(cast('123' as jsonb), '$' - returning queryfuncs_char2 - default 1 on error); + json_value( + cast('123' as jsonb), + '$' + returning queryfuncs_char2 + default 1 on error + ); select - json_value(cast('123' as jsonb), '$' - returning queryfuncs_char2_chk - error on error); + json_value( + cast('123' as jsonb), + '$' + returning queryfuncs_char2_chk + error on error + ); select - json_value(cast('123' as jsonb), '$' - returning queryfuncs_char2_chk - default 1 on error); + json_value( + cast('123' as jsonb), + '$' + returning queryfuncs_char2_chk + default 1 on error + ); drop domain queryfuncs_char2, queryfuncs_char2_chk; create domain queryfuncs_d_varbit3 as varbit(3) check (value <> '01'); select - json_value(cast('1234' as jsonb), '$' - returning queryfuncs_d_varbit3 - default '111111' on error); + json_value( + cast('1234' as jsonb), + '$' + returning queryfuncs_d_varbit3 + default '111111' on error + ); select - json_value(cast('1234' as jsonb), '$' - returning queryfuncs_d_varbit3 - default '010' on error); + json_value( + cast('1234' as jsonb), + '$' + returning queryfuncs_d_varbit3 + default '010' on error + ); select - json_value(cast('1234' as jsonb), '$' - returning queryfuncs_d_varbit3 - default '01' on error); + json_value( + cast('1234' as jsonb), + '$' + returning queryfuncs_d_varbit3 + default '01' on error + ); select json_value(cast('"111"' as jsonb), '$' returning bit(2) error on error); @@ -1136,13 +1445,19 @@ select json_value(cast('1234' as jsonb), '$' returning bit(3) default 1 on error); select - json_value(cast('1234' as jsonb), '$' - returning bit(3) - default cast(1 as bit(3)) on error); + json_value( + cast('1234' as jsonb), + '$' + returning bit(3) + default cast(1 as bit(3)) on error + ); select - json_value(cast('"111"' as jsonb), '$.a' - returning bit(3) - default '1111' on empty); + json_value( + cast('"111"' as jsonb), + '$.a' + returning bit(3) + default '1111' on empty + ); drop domain queryfuncs_d_varbit3; diff --git a/crates/pgls_pretty_print/tests/snapshots/multi/tests__stats_ext_100.snap b/crates/pgls_pretty_print/tests/snapshots/multi/tests__stats_ext_100.snap index 7df2f7ed9..6292559b8 100644 --- a/crates/pgls_pretty_print/tests/snapshots/multi/tests__stats_ext_100.snap +++ b/crates/pgls_pretty_print/tests/snapshots/multi/tests__stats_ext_100.snap @@ -2379,12 +2379,14 @@ with (autovacuum_enabled = off); insert into mcv_lists_arrays (a, b, c) select - array[fipshash(cast(i / 100 as text)), - fipshash(cast(i / 100 - 1 as text)), - fipshash(cast(i / 100 + 1 as text))], - array[cast(i / 100 - 1 as numeric) / 1000, - cast(i / 100 as numeric) / 1000, - cast(i / 100 + 1 as numeric) / 1000], + array[ + fipshash(cast(i / 100 as text)), fipshash(cast(i / 100 - 1 as text)), + fipshash(cast(i / 100 + 1 as text)) + ], + array[ + cast(i / 100 - 1 as numeric) / 1000, cast(i / 100 as numeric) / 1000, + cast(i / 100 + 1 as numeric) / 1000 + ], array[i / 100 - 1, i / 100, i / 100 + 1] from generate_series(1, 5000) as s (i); diff --git a/crates/pgls_pretty_print/tests/snapshots/multi/tests__stats_ext_80.snap b/crates/pgls_pretty_print/tests/snapshots/multi/tests__stats_ext_80.snap index f5b4ca747..f760b6161 100644 --- a/crates/pgls_pretty_print/tests/snapshots/multi/tests__stats_ext_80.snap +++ b/crates/pgls_pretty_print/tests/snapshots/multi/tests__stats_ext_80.snap @@ -3013,12 +3013,14 @@ with (autovacuum_enabled = off); insert into mcv_lists_arrays (a, b, c) select - array[fipshash(cast(i / 100 as text)), - fipshash(cast(i / 100 - 1 as text)), - fipshash(cast(i / 100 + 1 as text))], - array[cast(i / 100 - 1 as numeric) / 1000, - cast(i / 100 as numeric) / 1000, - cast(i / 100 + 1 as numeric) / 1000], + array[ + fipshash(cast(i / 100 as text)), fipshash(cast(i / 100 - 1 as text)), + fipshash(cast(i / 100 + 1 as text)) + ], + array[ + cast(i / 100 - 1 as numeric) / 1000, cast(i / 100 as numeric) / 1000, + cast(i / 100 + 1 as numeric) / 1000 + ], array[i / 100 - 1, i / 100, i / 100 + 1] from generate_series(1, 5000) as s (i); diff --git a/crates/pgls_pretty_print/tests/snapshots/multi/tests__tuplesort_100.snap b/crates/pgls_pretty_print/tests/snapshots/multi/tests__tuplesort_100.snap index ee4a3f85c..8e069b964 100644 --- a/crates/pgls_pretty_print/tests/snapshots/multi/tests__tuplesort_100.snap +++ b/crates/pgls_pretty_print/tests/snapshots/multi/tests__tuplesort_100.snap @@ -415,12 +415,8 @@ select percentile_disc(0.01) within group (order by id), percentile_disc(0.8) within group (order by abort_increasing), percentile_disc(0.2) within group (order by cast(id as text)), - rank( - '00000000-0000-0000-0000-000000000000', - '2', - '2' - ) - within group (order by noabort_increasing, id, cast(id as text)) + rank('00000000-0000-0000-0000-000000000000', '2', '2') + within group (order by noabort_increasing, id, cast(id as text)) from (select * from abbrev_abort_uuids union all select null, null, null, null, null) as s; @@ -436,12 +432,8 @@ select percentile_disc(0.01) within group (order by id), percentile_disc(0.8) within group (order by abort_increasing), percentile_disc(0.2) within group (order by cast(id as text)), - rank( - '00000000-0000-0000-0000-000000000000', - '2', - '2' - ) - within group (order by noabort_increasing, id, cast(id as text)) + rank('00000000-0000-0000-0000-000000000000', '2', '2') + within group (order by noabort_increasing, id, cast(id as text)) from (select * from abbrev_abort_uuids union all select null, null, null, null, null) as s; diff --git a/crates/pgls_pretty_print/tests/snapshots/multi/tests__tuplesort_80.snap b/crates/pgls_pretty_print/tests/snapshots/multi/tests__tuplesort_80.snap index 6b85c8b44..0a235da45 100644 --- a/crates/pgls_pretty_print/tests/snapshots/multi/tests__tuplesort_80.snap +++ b/crates/pgls_pretty_print/tests/snapshots/multi/tests__tuplesort_80.snap @@ -437,12 +437,8 @@ select percentile_disc(0.01) within group (order by id), percentile_disc(0.8) within group (order by abort_increasing), percentile_disc(0.2) within group (order by cast(id as text)), - rank( - '00000000-0000-0000-0000-000000000000', - '2', - '2' - ) - within group (order by noabort_increasing, id, cast(id as text)) + rank('00000000-0000-0000-0000-000000000000', '2', '2') + within group (order by noabort_increasing, id, cast(id as text)) from ( select * from abbrev_abort_uuids @@ -463,12 +459,8 @@ select percentile_disc(0.01) within group (order by id), percentile_disc(0.8) within group (order by abort_increasing), percentile_disc(0.2) within group (order by cast(id as text)), - rank( - '00000000-0000-0000-0000-000000000000', - '2', - '2' - ) - within group (order by noabort_increasing, id, cast(id as text)) + rank('00000000-0000-0000-0000-000000000000', '2', '2') + within group (order by noabort_increasing, id, cast(id as text)) from ( select * from abbrev_abort_uuids diff --git a/crates/pgls_pretty_print/tests/snapshots/multi/tests__type_sanity_100.snap b/crates/pgls_pretty_print/tests/snapshots/multi/tests__type_sanity_100.snap index f790609ad..e3e49b00f 100644 --- a/crates/pgls_pretty_print/tests/snapshots/multi/tests__type_sanity_100.snap +++ b/crates/pgls_pretty_print/tests/snapshots/multi/tests__type_sanity_100.snap @@ -831,23 +831,15 @@ from where oid < 16384 and typtype not in ('p', 'c') and - oid <> all (cast(array['regproc', - 'regprocedure', - 'regoper', - 'regoperator', - 'regconfig', - 'regdictionary', - 'regnamespace', - 'regcollation'] + oid <> all (cast(array[ + 'regproc', 'regprocedure', 'regoper', 'regoperator', 'regconfig', 'regdictionary', + 'regnamespace', 'regcollation' + ] as regtype[])) and - oid <> all (cast(array['gtsvector', - 'pg_node_tree', - 'pg_ndistinct', - 'pg_dependencies', - 'pg_mcv_list', - 'pg_brin_bloom_summary', - 'pg_brin_minmax_multi_summary', - 'xml'] + oid <> all (cast(array[ + 'gtsvector', 'pg_node_tree', 'pg_ndistinct', 'pg_dependencies', 'pg_mcv_list', + 'pg_brin_bloom_summary', 'pg_brin_minmax_multi_summary', 'xml' + ] as regtype[])) and not exists (select 1 from pg_type as u where u.typarray = t.oid) and not exists ( diff --git a/crates/pgls_pretty_print/tests/snapshots/multi/tests__type_sanity_80.snap b/crates/pgls_pretty_print/tests/snapshots/multi/tests__type_sanity_80.snap index 2ba360831..289493abb 100644 --- a/crates/pgls_pretty_print/tests/snapshots/multi/tests__type_sanity_80.snap +++ b/crates/pgls_pretty_print/tests/snapshots/multi/tests__type_sanity_80.snap @@ -882,23 +882,16 @@ from where oid < 16384 and typtype not in ('p', 'c') and - oid <> all (cast(array['regproc', - 'regprocedure', - 'regoper', - 'regoperator', - 'regconfig', - 'regdictionary', - 'regnamespace', - 'regcollation'] + oid <> all (cast(array[ + 'regproc', 'regprocedure', 'regoper', 'regoperator', 'regconfig', + 'regdictionary', 'regnamespace', 'regcollation' + ] as regtype[])) and - oid <> all (cast(array['gtsvector', - 'pg_node_tree', - 'pg_ndistinct', - 'pg_dependencies', - 'pg_mcv_list', - 'pg_brin_bloom_summary', - 'pg_brin_minmax_multi_summary', - 'xml'] + oid <> all (cast(array[ + 'gtsvector', 'pg_node_tree', 'pg_ndistinct', 'pg_dependencies', + 'pg_mcv_list', 'pg_brin_bloom_summary', 'pg_brin_minmax_multi_summary', + 'xml' + ] as regtype[])) and not exists (select 1 from pg_type as u where u.typarray = t.oid) and not exists ( diff --git a/crates/pgls_pretty_print/tests/snapshots/multi/tests__window_100.snap b/crates/pgls_pretty_print/tests/snapshots/multi/tests__window_100.snap index 21f4e84ec..1c9dfdfc4 100644 --- a/crates/pgls_pretty_print/tests/snapshots/multi/tests__window_100.snap +++ b/crates/pgls_pretty_print/tests/snapshots/multi/tests__window_100.snap @@ -2804,8 +2804,8 @@ select sum(salary), row_number() over (order by depname), sum(sum(salary) filter (where enroll_date > '2007-01-01')) - filter (where - depname <> 'sales') over (order by depname desc) as filtered_sum, + filter (where + depname <> 'sales') over (order by depname desc) as filtered_sum, depname from empsalary diff --git a/crates/pgls_pretty_print/tests/snapshots/multi/tests__window_80.snap b/crates/pgls_pretty_print/tests/snapshots/multi/tests__window_80.snap index 864a22175..da002b9bc 100644 --- a/crates/pgls_pretty_print/tests/snapshots/multi/tests__window_80.snap +++ b/crates/pgls_pretty_print/tests/snapshots/multi/tests__window_80.snap @@ -3293,8 +3293,8 @@ select sum(salary), row_number() over (order by depname), sum(sum(salary) filter (where enroll_date > '2007-01-01')) - filter (where - depname <> 'sales') over (order by depname desc) as filtered_sum, + filter (where + depname <> 'sales') over (order by depname desc) as filtered_sum, depname from empsalary diff --git a/crates/pgls_pretty_print/tests/snapshots/single/tests__func_call_within_group_filter_0_80.snap b/crates/pgls_pretty_print/tests/snapshots/single/tests__func_call_within_group_filter_0_80.snap index b5cf1ea0b..2bbda06bd 100644 --- a/crates/pgls_pretty_print/tests/snapshots/single/tests__func_call_within_group_filter_0_80.snap +++ b/crates/pgls_pretty_print/tests/snapshots/single/tests__func_call_within_group_filter_0_80.snap @@ -1,14 +1,11 @@ --- source: crates/pgls_pretty_print/tests/tests.rs input_file: crates/pgls_pretty_print/tests/data/single/func_call_within_group_filter_0.sql -snapshot_kind: text --- select - percentile_cont( - 0.75 - ) - within group (order by salary desc) - filter (where - salary is not null) + percentile_cont(0.75) + within group (order by salary desc) + filter (where + salary is not null) from employees; From ead236b9bdb397b9ae103de48b7995dd711a20bd Mon Sep 17 00:00:00 2001 From: psteinroe Date: Fri, 23 Oct 2026 14:09:51 +0000 Subject: [PATCH 6/7] fix(pretty_print): keep `with admin option` portable --- .../pgls_pretty_print/src/nodes/def_elem.rs | 8 ++++-- .../multi/tests__privileges_100.snap | 26 +++++++++---------- .../snapshots/multi/tests__privileges_80.snap | 26 +++++++++---------- .../tests/source_fidelity.rs | 18 +++++++++++++ 4 files changed, 50 insertions(+), 28 deletions(-) diff --git a/crates/pgls_pretty_print/src/nodes/def_elem.rs b/crates/pgls_pretty_print/src/nodes/def_elem.rs index fddc2c1c1..b44f7359a 100644 --- a/crates/pgls_pretty_print/src/nodes/def_elem.rs +++ b/crates/pgls_pretty_print/src/nodes/def_elem.rs @@ -105,7 +105,7 @@ pub(super) fn emit_options_def_elem(e: &mut EventEmitter, n: &DefElem) { } /// Emit role options for GRANT...WITH syntax -/// Uses `NAME TRUE` / `NAME FALSE` syntax for INHERIT, SET, ADMIN +/// Uses `NAME TRUE` / `NAME FALSE` syntax for INHERIT, SET, ADMIN, except `ADMIN OPTION`. pub(super) fn emit_grant_role_option(e: &mut EventEmitter, n: &DefElem) { e.group_start(GroupKind::DefElem); @@ -128,7 +128,11 @@ pub(super) fn emit_grant_role_option(e: &mut EventEmitter, n: &DefElem) { // Emit TRUE or FALSE if let Some(val) = is_true { e.space(); - if val { + // `admin option` and `admin true` parse the same, but Postgres 15 and older only accept + // `with admin option`. + if val && defname.eq_ignore_ascii_case("admin") { + e.token(TokenKind::OPTION_KW); + } else if val { e.token(TokenKind::TRUE_KW); } else { e.token(TokenKind::FALSE_KW); diff --git a/crates/pgls_pretty_print/tests/snapshots/multi/tests__privileges_100.snap b/crates/pgls_pretty_print/tests/snapshots/multi/tests__privileges_100.snap index 77ffebd0c..aa6276883 100644 --- a/crates/pgls_pretty_print/tests/snapshots/multi/tests__privileges_100.snap +++ b/crates/pgls_pretty_print/tests/snapshots/multi/tests__privileges_100.snap @@ -50,11 +50,11 @@ create user regress_priv_user10; create role regress_priv_role; -grant regress_priv_user1 to regress_priv_user2 with admin true; +grant regress_priv_user1 to regress_priv_user2 with admin option; -grant regress_priv_user1 to regress_priv_user3 with admin true granted by regress_priv_user2; +grant regress_priv_user1 to regress_priv_user3 with admin option granted by regress_priv_user2; -grant regress_priv_user1 to regress_priv_user2 with admin true granted by regress_priv_user3; +grant regress_priv_user1 to regress_priv_user2 with admin option granted by regress_priv_user3; revoke admin option for regress_priv_user1 from regress_priv_user2 restrict; @@ -92,7 +92,7 @@ from where roleid = cast('regress_priv_user1' as regrole); -grant regress_priv_user1 to regress_priv_user2 with admin true; +grant regress_priv_user1 to regress_priv_user2 with admin option; grant regress_priv_user2 to regress_priv_user3; @@ -113,7 +113,7 @@ revoke regress_priv_user2 from regress_priv_user3 restrict; revoke regress_priv_user1 from regress_priv_user2 cascade; -grant regress_priv_user1 to regress_priv_user2 with admin true; +grant regress_priv_user1 to regress_priv_user2 with admin option; grant regress_priv_user1 to regress_priv_user3 granted by regress_priv_user2; @@ -127,7 +127,7 @@ drop owned by regress_priv_user2; drop role regress_priv_user2; -grant regress_priv_user1 to regress_priv_user3 with admin true; +grant regress_priv_user1 to regress_priv_user3 with admin option; grant regress_priv_user1 to regress_priv_user4 granted by regress_priv_user3; @@ -137,7 +137,7 @@ drop role regress_priv_user4; drop role regress_priv_user3; -grant regress_priv_user1 to regress_priv_user5 with admin true; +grant regress_priv_user1 to regress_priv_user5 with admin option; grant regress_priv_user1 to regress_priv_user6 granted by regress_priv_user5; @@ -159,13 +159,13 @@ grant pg_read_all_data to regress_priv_user6; grant pg_write_all_data to regress_priv_user7; -grant pg_read_all_settings to regress_priv_user8 with admin true; +grant pg_read_all_settings to regress_priv_user8 with admin option; grant regress_priv_user9 to regress_priv_user8; set session authorization regress_priv_user8; -grant pg_read_all_settings to regress_priv_user9 with admin true; +grant pg_read_all_settings to regress_priv_user9 with admin option; set session authorization regress_priv_user9; @@ -299,7 +299,7 @@ alter group regress_priv_group2 drop user regress_priv_user2; revoke admin option for regress_priv_group2 from regress_priv_user1 restrict; -grant regress_priv_group2 to regress_priv_user4 with admin true; +grant regress_priv_group2 to regress_priv_user4 with admin option; create function leak(int, int) returns boolean @@ -310,9 +310,9 @@ as $$int4lt$$; alter function leak(int, int) owner to regress_priv_user1; -grant regress_priv_role to regress_priv_user1 with admin true granted by regress_priv_role; +grant regress_priv_role to regress_priv_user1 with admin option granted by regress_priv_role; -grant regress_priv_role to regress_priv_user1 with admin true granted by current_role; +grant regress_priv_role to regress_priv_user1 with admin option granted by current_role; revoke admin option for regress_priv_role from regress_priv_user1 granted by foo restrict; @@ -2853,7 +2853,7 @@ create role regress_group_indirect_manager; create role regress_group_member; -grant regress_group to regress_group_direct_manager with inherit false, admin true; +grant regress_group to regress_group_direct_manager with inherit false, admin option; grant regress_group_direct_manager to regress_group_indirect_manager; diff --git a/crates/pgls_pretty_print/tests/snapshots/multi/tests__privileges_80.snap b/crates/pgls_pretty_print/tests/snapshots/multi/tests__privileges_80.snap index 790eb8606..1063c46ee 100644 --- a/crates/pgls_pretty_print/tests/snapshots/multi/tests__privileges_80.snap +++ b/crates/pgls_pretty_print/tests/snapshots/multi/tests__privileges_80.snap @@ -56,16 +56,16 @@ create user regress_priv_user10; create role regress_priv_role; -grant regress_priv_user1 to regress_priv_user2 with admin true; +grant regress_priv_user1 to regress_priv_user2 with admin option; grant regress_priv_user1 to regress_priv_user3 - with admin true + with admin option granted by regress_priv_user2; grant regress_priv_user1 to regress_priv_user2 - with admin true + with admin option granted by regress_priv_user3; revoke admin option for regress_priv_user1 from regress_priv_user2 restrict; @@ -104,7 +104,7 @@ from where roleid = cast('regress_priv_user1' as regrole); -grant regress_priv_user1 to regress_priv_user2 with admin true; +grant regress_priv_user1 to regress_priv_user2 with admin option; grant regress_priv_user2 to regress_priv_user3; @@ -126,7 +126,7 @@ revoke regress_priv_user2 from regress_priv_user3 restrict; revoke regress_priv_user1 from regress_priv_user2 cascade; -grant regress_priv_user1 to regress_priv_user2 with admin true; +grant regress_priv_user1 to regress_priv_user2 with admin option; grant regress_priv_user1 to regress_priv_user3 granted by regress_priv_user2; @@ -140,7 +140,7 @@ drop owned by regress_priv_user2; drop role regress_priv_user2; -grant regress_priv_user1 to regress_priv_user3 with admin true; +grant regress_priv_user1 to regress_priv_user3 with admin option; grant regress_priv_user1 to regress_priv_user4 granted by regress_priv_user3; @@ -150,7 +150,7 @@ drop role regress_priv_user4; drop role regress_priv_user3; -grant regress_priv_user1 to regress_priv_user5 with admin true; +grant regress_priv_user1 to regress_priv_user5 with admin option; grant regress_priv_user1 to regress_priv_user6 granted by regress_priv_user5; @@ -172,13 +172,13 @@ grant pg_read_all_data to regress_priv_user6; grant pg_write_all_data to regress_priv_user7; -grant pg_read_all_settings to regress_priv_user8 with admin true; +grant pg_read_all_settings to regress_priv_user8 with admin option; grant regress_priv_user9 to regress_priv_user8; set session authorization regress_priv_user8; -grant pg_read_all_settings to regress_priv_user9 with admin true; +grant pg_read_all_settings to regress_priv_user9 with admin option; set session authorization regress_priv_user9; @@ -341,7 +341,7 @@ alter group regress_priv_group2 drop user regress_priv_user2; revoke admin option for regress_priv_group2 from regress_priv_user1 restrict; -grant regress_priv_group2 to regress_priv_user4 with admin true; +grant regress_priv_group2 to regress_priv_user4 with admin option; create function leak(int, int) returns boolean @@ -354,12 +354,12 @@ alter function leak(int, int) owner to regress_priv_user1; grant regress_priv_role to regress_priv_user1 - with admin true + with admin option granted by regress_priv_role; grant regress_priv_role to regress_priv_user1 - with admin true + with admin option granted by current_role; revoke admin option for regress_priv_role @@ -3179,7 +3179,7 @@ create role regress_group_member; grant regress_group to regress_group_direct_manager - with inherit false, admin true; + with inherit false, admin option; grant regress_group_direct_manager to regress_group_indirect_manager; diff --git a/crates/pgls_pretty_print/tests/source_fidelity.rs b/crates/pgls_pretty_print/tests/source_fidelity.rs index 32c79c414..d34a58d13 100644 --- a/crates/pgls_pretty_print/tests/source_fidelity.rs +++ b/crates/pgls_pretty_print/tests/source_fidelity.rs @@ -307,3 +307,21 @@ fn set_values_keep_their_case() { "set datestyle = 'German';" ); } + +/// `with admin true` parses to the same AST, but only Postgres 16 and newer accept it. +#[test] +fn grant_role_with_admin_option_keeps_the_portable_spelling() { + assert_eq!( + format("GRANT r1 TO u1 WITH ADMIN OPTION"), + "grant r1 to u1 with admin option;" + ); + assert_eq!( + format("GRANT r1 TO u1 WITH ADMIN TRUE"), + "grant r1 to u1 with admin option;" + ); + // Only Postgres 16 and newer have the other options, so they keep the boolean spelling. + assert_eq!( + format("GRANT r1 TO u1 WITH INHERIT FALSE, ADMIN FALSE, SET TRUE"), + "grant r1 to u1 with inherit false, admin false, set true;" + ); +} From a808ad4391ac2e26eb1b412f06e2f4d4b2afbcd0 Mon Sep 17 00:00:00 2001 From: psteinroe Date: Fri, 23 Oct 2026 14:25:57 +0000 Subject: [PATCH 7/7] fix(pretty_print): keep a FILTER clause that fits on one line --- crates/pgls_pretty_print/src/nodes/func_call.rs | 13 ++++++++++++- .../tests/function_call_layout.rs | 15 ++++++++++++++- ...tests__20230405163940_download_metrics_80.snap | 9 +++------ .../snapshots/multi/tests__aggregates_100.snap | 3 +-- .../snapshots/multi/tests__aggregates_80.snap | 9 +++------ .../multi/tests__ordered_set_filter_80.snap | 3 +-- .../tests/snapshots/multi/tests__window_100.snap | 3 +-- .../tests/snapshots/multi/tests__window_80.snap | 4 ++-- ...tests__func_call_within_group_filter_0_80.snap | 3 +-- 9 files changed, 38 insertions(+), 24 deletions(-) diff --git a/crates/pgls_pretty_print/src/nodes/func_call.rs b/crates/pgls_pretty_print/src/nodes/func_call.rs index def8c6bf1..0a6db171e 100644 --- a/crates/pgls_pretty_print/src/nodes/func_call.rs +++ b/crates/pgls_pretty_print/src/nodes/func_call.rs @@ -157,10 +157,21 @@ pub(super) fn emit_func_call(e: &mut EventEmitter, n: &FuncCall) { e.line(LineType::SoftOrSpace); e.token(TokenKind::FILTER_KW); e.space(); + // `filter (where ...)` stays on one line when it fits. Otherwise the condition goes on + // its own lines inside the parentheses, like a policy's `using (...)`. + e.group_start(GroupKind::List); e.token(TokenKind::L_PAREN); + e.indent_start(); + e.line(LineType::Soft); e.token(TokenKind::WHERE_KW); - super::emit_clause_condition(e, filter); + e.space(); + e.indent_start(); + super::emit_node(filter, e); + e.indent_end(); + e.indent_end(); + e.line(LineType::Soft); e.token(TokenKind::R_PAREN); + e.group_end(); e.indent_end(); } diff --git a/crates/pgls_pretty_print/tests/function_call_layout.rs b/crates/pgls_pretty_print/tests/function_call_layout.rs index ac70c26df..e699ed840 100644 --- a/crates/pgls_pretty_print/tests/function_call_layout.rs +++ b/crates/pgls_pretty_print/tests/function_call_layout.rs @@ -15,7 +15,20 @@ fn count_star_stays_together_when_the_filter_breaks() { "select count(*) filter (where status = 'active' and created_at > now() - interval '30 days' and deleted_at is null) from users", &FormatConfig::default(), ); - assert!(output.contains(" count(*)\n filter (where"), "{output}"); + assert_eq!( + output, + "select\n count(*)\n filter (\n where status = 'active' and\n created_at > now() - cast('30 days' as interval) and\n deleted_at is null\n )\nfrom\n users;" + ); + + // A FILTER clause that fits stays on one line, even when the call breaks before it. + let output = format_idempotently( + "select percentile_cont(0.5) within group (order by response_time_in_milliseconds) filter (where status_code < 500) from requests", + &FormatConfig::default(), + ); + assert!( + output.contains("\n filter (where status_code < 500)\n"), + "{output}" + ); } #[test] diff --git a/crates/pgls_pretty_print/tests/snapshots/multi/tests__20230405163940_download_metrics_80.snap b/crates/pgls_pretty_print/tests/snapshots/multi/tests__20230405163940_download_metrics_80.snap index b2e519ddb..63f4dc367 100644 --- a/crates/pgls_pretty_print/tests/snapshots/multi/tests__20230405163940_download_metrics_80.snap +++ b/crates/pgls_pretty_print/tests/snapshots/multi/tests__20230405163940_download_metrics_80.snap @@ -63,16 +63,13 @@ as dl.package_id, count(dl.id) as downloads_all_time, count(dl.id) - filter (where - dl.created_at > now() - cast('180 days' as interval)) + filter (where dl.created_at > now() - cast('180 days' as interval)) as downloads_180_days, count(dl.id) - filter (where - dl.created_at > now() - cast('90 days' as interval)) + filter (where dl.created_at > now() - cast('90 days' as interval)) as downloads_90_days, count(dl.id) - filter (where - dl.created_at > now() - cast('30 days' as interval)) + filter (where dl.created_at > now() - cast('30 days' as interval)) as downloads_30_day from app.downloads as dl diff --git a/crates/pgls_pretty_print/tests/snapshots/multi/tests__aggregates_100.snap b/crates/pgls_pretty_print/tests/snapshots/multi/tests__aggregates_100.snap index e09aef9d3..3d131f0e0 100644 --- a/crates/pgls_pretty_print/tests/snapshots/multi/tests__aggregates_100.snap +++ b/crates/pgls_pretty_print/tests/snapshots/multi/tests__aggregates_100.snap @@ -1171,8 +1171,7 @@ select ( select max((select i.unique2 from tenk1 as i where i.unique1 = o.unique1)) - filter (where - o.unique1 < 10) + filter (where o.unique1 < 10) ) from tenk1 as o; diff --git a/crates/pgls_pretty_print/tests/snapshots/multi/tests__aggregates_80.snap b/crates/pgls_pretty_print/tests/snapshots/multi/tests__aggregates_80.snap index 67db79d62..28c949d21 100644 --- a/crates/pgls_pretty_print/tests/snapshots/multi/tests__aggregates_80.snap +++ b/crates/pgls_pretty_print/tests/snapshots/multi/tests__aggregates_80.snap @@ -1341,16 +1341,14 @@ select ( select max((select i.unique2 from tenk1 as i where i.unique1 = o.unique1)) - filter (where - o.unique1 < 10) + filter (where o.unique1 < 10) ) from tenk1 as o; select sum(unique1) - filter (where - unique1 in (select unique1 from onek where unique1 < 100)) + filter (where unique1 in (select unique1 from onek where unique1 < 100)) from tenk1; @@ -1566,8 +1564,7 @@ as select percentile_disc(0.5) within group (order by thousand) as p50, percentile_disc(0.5) within group (order by thousand) - filter (where - hundred = 1) + filter (where hundred = 1) as px, rank(5, 'AZZZZ', 50) within group (order by hundred, string4 desc, hundred) from diff --git a/crates/pgls_pretty_print/tests/snapshots/multi/tests__ordered_set_filter_80.snap b/crates/pgls_pretty_print/tests/snapshots/multi/tests__ordered_set_filter_80.snap index 80665a2d5..f4304b714 100644 --- a/crates/pgls_pretty_print/tests/snapshots/multi/tests__ordered_set_filter_80.snap +++ b/crates/pgls_pretty_print/tests/snapshots/multi/tests__ordered_set_filter_80.snap @@ -10,8 +10,7 @@ from select percentile_cont(0.9) within group (order by duration) - filter (where - duration is not null) + filter (where duration is not null) from ( values (cast('1 hour' as interval)), (cast('2 hours' as interval)) diff --git a/crates/pgls_pretty_print/tests/snapshots/multi/tests__window_100.snap b/crates/pgls_pretty_print/tests/snapshots/multi/tests__window_100.snap index 1c9dfdfc4..346e7d079 100644 --- a/crates/pgls_pretty_print/tests/snapshots/multi/tests__window_100.snap +++ b/crates/pgls_pretty_print/tests/snapshots/multi/tests__window_100.snap @@ -2804,8 +2804,7 @@ select sum(salary), row_number() over (order by depname), sum(sum(salary) filter (where enroll_date > '2007-01-01')) - filter (where - depname <> 'sales') over (order by depname desc) as filtered_sum, + filter (where depname <> 'sales') over (order by depname desc) as filtered_sum, depname from empsalary diff --git a/crates/pgls_pretty_print/tests/snapshots/multi/tests__window_80.snap b/crates/pgls_pretty_print/tests/snapshots/multi/tests__window_80.snap index da002b9bc..cdc60a26b 100644 --- a/crates/pgls_pretty_print/tests/snapshots/multi/tests__window_80.snap +++ b/crates/pgls_pretty_print/tests/snapshots/multi/tests__window_80.snap @@ -3293,8 +3293,8 @@ select sum(salary), row_number() over (order by depname), sum(sum(salary) filter (where enroll_date > '2007-01-01')) - filter (where - depname <> 'sales') over (order by depname desc) as filtered_sum, + filter (where depname <> 'sales') over (order by depname desc) + as filtered_sum, depname from empsalary diff --git a/crates/pgls_pretty_print/tests/snapshots/single/tests__func_call_within_group_filter_0_80.snap b/crates/pgls_pretty_print/tests/snapshots/single/tests__func_call_within_group_filter_0_80.snap index 2bbda06bd..083994c75 100644 --- a/crates/pgls_pretty_print/tests/snapshots/single/tests__func_call_within_group_filter_0_80.snap +++ b/crates/pgls_pretty_print/tests/snapshots/single/tests__func_call_within_group_filter_0_80.snap @@ -5,7 +5,6 @@ input_file: crates/pgls_pretty_print/tests/data/single/func_call_within_group_fi select percentile_cont(0.75) within group (order by salary desc) - filter (where - salary is not null) + filter (where salary is not null) from employees;