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

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 28 additions & 0 deletions crates/pgls_pretty_print/src/nodes/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -660,6 +660,34 @@ pub(super) fn emit_condition_on_keyword_line(e: &mut EventEmitter, condition: &N
}
}

/// Whether a select item without an alias, which would continue on a line of its own, is written as
/// a name or keyword followed by
/// parentheses that hold everything that can break: a function call, `coalesce(...)`, `cast(...)`,
/// a subquery, ...
pub(super) fn breaks_only_inside_its_parentheses(e: &EventEmitter, item: &Node) -> bool {
let Some(NodeEnum::ResTarget(target)) = item.node.as_ref() else {
return false;
};
if !target.name.is_empty() || !target.indirection.is_empty() {
return false;
}
let Some(value) = target.val.as_deref() else {
return false;
};
match value.node.as_ref() {
// FILTER, WITHIN GROUP and OVER continue the call on lines of their own.
Some(NodeEnum::FuncCall(call)) => {
call.agg_filter.is_none() && call.over.is_none() && !call.agg_within_group
}
Some(NodeEnum::CoalesceExpr(_) | NodeEnum::MinMaxExpr(_) | NodeEnum::XmlSerialize(_)) => {
true
}
Some(NodeEnum::TypeCast(_)) => matches!(e.config().cast_style, crate::CastStyle::Cast),
Some(NodeEnum::SubLink(_)) => breaks_only_inside_a_subquery(value),
_ => false,
}
}

fn breaks_only_inside_a_subquery(node: &Node) -> bool {
use pgls_query::protobuf::{AExprKind, BoolExprType, SubLinkType};

Expand Down
26 changes: 19 additions & 7 deletions crates/pgls_pretty_print/src/nodes/select_stmt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -200,18 +200,30 @@ fn emit_select_stmt_impl(e: &mut EventEmitter, n: &SelectStmt, with_semicolon: b
}

if !n.target_list.is_empty() {
e.indent_start();

// Compact keeps a lone item on the SELECT line, `select 1`; a list of several still
// puts one item per line.
match e.config().clause_body_style {
ClauseBodyStyle::Compact if n.target_list.len() == 1 => e.space(),
_ => super::emit_layout_break(e),
// puts one item per line. The indent is for the item's continuation lines, which an
// item that only breaks inside its parentheses, `coalesce(...)`, doesn't have: indented,
// its arguments would sit two levels in and its `)` one level in.
let lone_compact_item =
matches!(e.config().clause_body_style, ClauseBodyStyle::Compact)
&& n.target_list.len() == 1;
let indent = !(lone_compact_item
&& super::breaks_only_inside_its_parentheses(e, &n.target_list[0]));

if indent {
e.indent_start();
}
if lone_compact_item {
e.space();
} else {
super::emit_layout_break(e);
}

emit_comma_separated_list_with_layout_break(e, &n.target_list, super::emit_node);

e.indent_end();
if indent {
e.indent_end();
}
}

// Emit INTO clause if present (SELECT ... INTO table_name)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,10 @@ select case
from users;

select json_build_object(
'identifier', users.id,
'display_name', users.name,
'email_address', users.email
)
'identifier', users.id,
'display_name', users.name,
'email_address', users.email
)
from users;

select first_really_long_column_name_here + second_really_long_column_name_here + third_long_column
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,10 @@ select case
from users;

select json_build_object(
'identifier', users.id,
'display_name', users.name,
'email_address', users.email
)
'identifier', users.id,
'display_name', users.name,
'email_address', users.email
)
from users;

select first_really_long_column_name_here +
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,10 +55,10 @@ select case
from users;

select json_build_object(
'identifier', users.id,
'display_name', users.name,
'email_address', users.email
)
'identifier', users.id,
'display_name', users.name,
'email_address', users.email
)
from users;

select first_really_long_column_name_here + second_really_long_column_name_here + third_long_column
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,10 +55,10 @@ select case
from users;

select json_build_object(
'identifier', users.id,
'display_name', users.name,
'email_address', users.email
)
'identifier', users.id,
'display_name', users.name,
'email_address', users.email
)
from users;

select first_really_long_column_name_here +
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,10 @@ select case
from users;

select json_build_object(
'identifier', users.id
, 'display_name', users.name
, 'email_address', users.email
)
'identifier', users.id
, 'display_name', users.name
, 'email_address', users.email
)
from users;

select first_really_long_column_name_here + second_really_long_column_name_here + third_long_column
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,10 @@ select case
from users;

select json_build_object(
'identifier', users.id
, 'display_name', users.name
, 'email_address', users.email
)
'identifier', users.id
, 'display_name', users.name
, 'email_address', users.email
)
from users;

select first_really_long_column_name_here +
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,14 +37,14 @@ 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 max(x)
from y
where y.id = t.id
and y.kind = 'some kind of value'
),
0
)
);

select *
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,14 +37,14 @@ 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 max(x)
from y
where y.id = t.id
and y.kind = 'some kind of value'
),
0
)
);

select *
Expand Down
26 changes: 26 additions & 0 deletions crates/pgls_pretty_print/tests/subquery_layout.rs
Original file line number Diff line number Diff line change
Expand Up @@ -119,3 +119,29 @@ fn a_subquery_opened_on_the_on_line_closes_under_on() {
assert!(output.contains("\n on u.id = (\n select\n"), "{output}");
assert!(output.ends_with("\n );"), "{output}");
}

#[test]
fn a_lone_compact_select_item_closes_under_select() {
let config = FormatConfig {
clause_body_style: ClauseBodyStyle::Compact,
line_width: 60,
..Default::default()
};
assert_eq!(
format_idempotently(
"select coalesce(some_long_column_name, another_long_column_name, 0) from t",
&config,
),
"select coalesce(\n some_long_column_name,\n another_long_column_name,\n 0\n)\nfrom t;"
);

// An item with continuation lines of its own keeps the indent for them.
let output = format_idempotently(
"select some_long_column_name + another_long_column_name + a_third_long_column from t",
&config,
);
assert_eq!(
output,
"select some_long_column_name + another_long_column_name +\n a_third_long_column\nfrom t;"
);
}
Loading