Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improvements to pretty-printing for AND/OR/CASE #482

Merged
merged 3 commits into from
Jul 31, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
133 changes: 73 additions & 60 deletions partiql-ast/src/pretty.rs
Original file line number Diff line number Diff line change
Expand Up @@ -570,10 +570,15 @@ impl PrettyDoc for BinOp {
let op = arena.text(sym);
let lhs = lhs.pretty_doc(arena).nest(nest);
let rhs = rhs.pretty_doc(arena).nest(nest);
let sep = arena.space();
let sep = if nest == 0 {
arena.space()
} else {
arena.softline()
};
let expr = arena.intersperse([lhs, op, rhs], sep).group();
let paren_expr = [arena.text("("), expr, arena.text(")")];
arena.concat(paren_expr).group()
//let paren_expr = [arena.text("("), expr, arena.text(")")];
//arena.concat(paren_expr).group()
jpschorr marked this conversation as resolved.
Show resolved Hide resolved
pretty_parenthesized_doc(expr, arena).group()
}
}

Expand Down Expand Up @@ -698,30 +703,26 @@ impl PrettyDoc for SimpleCase {
default,
} = self;

let kw_case = arena.text("CASE");
let search = expr.pretty_doc(arena);
let branches = cases.iter().map(|ExprPair { first, second }| {
let kw_when = arena.text("WHEN");
let test = first.pretty_doc(arena);
let kw_then = arena.text("THEN");
let then = second.pretty_doc(arena);
arena
.intersperse([kw_when, test, kw_then, then], arena.space())
.group()
});
let branches = arena
.intersperse(branches, arena.softline())
.group()
.nest(MINOR_NEST_INDENT);
let default = default
.as_ref()
.map(|d| arena.text("ELSE ").append(d.pretty_doc(arena)));

if let Some(default) = default {
arena.intersperse([kw_case, search, branches, default], arena.softline())
} else {
arena.intersperse([kw_case, search, branches], arena.softline())
}
let branches = cases
.iter()
.map(|ExprPair { first, second }| {
let kw_when = arena.text("WHEN");
let test = first.pretty_doc(arena);
let kw_then = arena.text("THEN");
let then = second.pretty_doc(arena);
arena
.intersperse([kw_when, test, kw_then, then], arena.space())
.group()
})
.chain(
default
.iter()
.map(|d| arena.text("ELSE ").append(d.pretty_doc(arena)).group()),
);

pretty_seq_doc(branches, "CASE", Some(search), "END", " ", arena)
}
}

Expand All @@ -734,29 +735,24 @@ impl PrettyDoc for SearchedCase {
{
let SearchedCase { cases, default } = self;

let kw_case = arena.text("CASE");
let branches = cases.iter().map(|ExprPair { first, second }| {
let kw_when = arena.text("WHEN");
let test = first.pretty_doc(arena);
let kw_then = arena.text("THEN");
let then = second.pretty_doc(arena);
arena
.intersperse([kw_when, test, kw_then, then], arena.space())
.group()
});
let branches = arena
.intersperse(branches, arena.softline())
.group()
.nest(MINOR_NEST_INDENT);
let default = default
.as_ref()
.map(|d| arena.text("ELSE ").append(d.pretty_doc(arena)));

if let Some(default) = default {
arena.intersperse([kw_case, branches, default], arena.softline())
} else {
arena.intersperse([kw_case, branches], arena.softline())
}
let branches = cases
.iter()
.map(|ExprPair { first, second }| {
let kw_when = arena.text("WHEN");
let test = first.pretty_doc(arena);
let kw_then = arena.text("THEN");
let then = second.pretty_doc(arena);
arena
.intersperse([kw_when, test, kw_then, then], arena.space())
.group()
})
.chain(
default
.iter()
.map(|d| arena.text("ELSE ").append(d.pretty_doc(arena)).group()),
);

pretty_seq_doc(branches, "CASE", None, "END", " ", arena)
}
}

Expand Down Expand Up @@ -1258,33 +1254,32 @@ where
D::Doc: Clone,
A: Clone,
{
arena
.text("(")
.append(arena.space())
.append(doc)
.append(arena.space())
.append(arena.text(")"))
.group()
arena.text("(").append(doc).append(arena.text(")")).group()
}

fn pretty_seq<'i, 'b, I, P, D, A>(
list: I,
fn pretty_seq_doc<'i, 'b, I, E, D, A>(
seq: I,
start: &'static str,
qualifier: Option<E>,
end: &'static str,
sep: &'static str,
arena: &'b D,
) -> DocBuilder<'b, D, A>
where
I: IntoIterator<Item = &'b P>,
P: PrettyDoc + 'b,
E: Pretty<'b, D, A>,
I: IntoIterator<Item = E>,
D: DocAllocator<'b, A>,
D::Doc: Clone,
A: Clone,
{
let start = arena.text(start);
let end = arena.text(end);
let sep = arena.text(sep).append(arena.line());
let seq = list.into_iter().map(|l| l.pretty_doc(arena));
let start = if let Some(qual) = qualifier {
start.append(arena.space()).append(qual)
} else {
start
};
let body = arena.line().append(arena.intersperse(seq, sep)).group();
start
.append(body.nest(MINOR_NEST_INDENT))
Expand All @@ -1293,6 +1288,24 @@ where
.group()
}

fn pretty_seq<'i, 'b, I, P, D, A>(
list: I,
start: &'static str,
end: &'static str,
sep: &'static str,
arena: &'b D,
) -> DocBuilder<'b, D, A>
where
I: IntoIterator<Item = &'b P>,
P: PrettyDoc + 'b,
D: DocAllocator<'b, A>,
D::Doc: Clone,
A: Clone,
{
let seq = list.into_iter().map(|l| l.pretty_doc(arena));
pretty_seq_doc(seq, start, None, end, sep, arena)
}

fn pretty_list<'b, I, P, D, A>(list: I, nest: isize, arena: &'b D) -> DocBuilder<'b, D, A>
where
I: IntoIterator<Item = &'b P>,
Expand Down
12 changes: 12 additions & 0 deletions partiql/tests/pretty.rs
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,18 @@ fn pretty_pivot() {
);
}

#[test]
fn pretty_ands_and_ors() {
pretty_print_test(
"pretty_ands_and_ors",
"
SELECT *
FROM test_data AS test_data
WHERE ((((test_data.country_code <> 'Distinctio.') AND ((test_data.* < false) AND (NOT (test_data.description LIKE 'Esse solam.') AND NOT (test_data.transaction_id LIKE 'Esset accusata.')))) OR (test_data.test_address <> 'Potest. Sed.')) AND (test_data.* > -28.146858383543243))
",
);
}

#[test]
fn pretty_exclude() {
pretty_print_test(
Expand Down
33 changes: 23 additions & 10 deletions partiql/tests/snapshots/pretty__pretty2.snap
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
---
source: partiql-ast/tests/pretty.rs
source: partiql/tests/pretty.rs
expression: doc
---
========================================================================================================================================================================================================
Expand All @@ -15,26 +15,31 @@ SELECT foo, bar, baz, thud, grunt, count(1) AS n FROM (SELECT * FROM table1) WHE
ORDER BY n DESC

------------------------------------------------------------------------------------------------------------------------
SELECT foo, bar, baz, thud, grunt, count(1) AS n FROM (SELECT * FROM table1)
WHERE ((bar BETWEEN 3 AND 25 AND NOT (baz LIKE 'example%')) OR (foo.a.b[*] IS MISSING)) GROUP BY foo ORDER BY n DESC
SELECT foo, bar, baz, thud, grunt, count(1) AS n FROM (SELECT * FROM table1) WHERE ((bar BETWEEN 3 AND 25 AND
NOT (baz LIKE 'example%')) OR (foo.a.b[*] IS MISSING)) GROUP BY foo ORDER BY n DESC

--------------------------------------------------------------------------------
SELECT foo, bar, baz, thud, grunt, count(1) AS n FROM (SELECT * FROM table1)
WHERE ((bar BETWEEN 3 AND 25 AND NOT (baz LIKE 'example%')) OR (foo.a.b[*] IS MISSING))
GROUP BY foo ORDER BY n DESC
WHERE ((bar BETWEEN 3 AND 25 AND NOT (baz LIKE 'example%')) OR
(foo.a.b[*] IS MISSING)) GROUP BY foo ORDER BY n DESC

----------------------------------------
SELECT foo, bar, baz, thud, grunt,
count(1) AS n FROM (SELECT *
FROM table1)
WHERE ((bar BETWEEN 3 AND 25 AND NOT (baz LIKE 'example%')) OR (foo.a.b[*] IS MISSING))
GROUP BY foo ORDER BY n DESC
WHERE ((bar BETWEEN 3 AND 25 AND
NOT (baz LIKE 'example%')) OR
(foo.a.b[*] IS MISSING)) GROUP BY foo
ORDER BY n DESC

------------------------------
SELECT foo, bar, baz, thud,
grunt, count(1) AS n
FROM (SELECT * FROM table1)
WHERE ((bar BETWEEN 3 AND 25 AND NOT (baz LIKE 'example%')) OR (foo.a.b[*] IS MISSING))
WHERE ((bar BETWEEN 3 AND 25
AND
NOT (baz LIKE 'example%'))
OR (foo.a.b[*] IS MISSING))
GROUP BY foo ORDER BY n DESC

--------------------
Expand All @@ -43,7 +48,11 @@ SELECT foo, bar,
count(1) AS n
FROM (SELECT *
FROM table1)
WHERE ((bar BETWEEN 3 AND 25 AND NOT (baz LIKE 'example%')) OR (foo.a.b[*] IS MISSING))
WHERE ((bar BETWEEN 3 AND 25
AND
NOT (baz LIKE 'example%'))
OR
(foo.a.b[*] IS MISSING))
GROUP BY foo
ORDER BY n DESC

Expand All @@ -56,7 +65,11 @@ SELECT foo,
count(1) AS n
FROM (SELECT *
FROM table1)
WHERE ((bar BETWEEN 3 AND 25 AND NOT (baz LIKE 'example%')) OR (foo.a.b[*] IS MISSING))
WHERE ((bar BETWEEN 3 AND 25
AND
NOT (baz LIKE 'example%'))
OR
(foo.a.b[*] IS MISSING))
GROUP BY
foo
ORDER BY n DESC
85 changes: 85 additions & 0 deletions partiql/tests/snapshots/pretty__pretty_ands_and_ors.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
---
source: partiql/tests/pretty.rs
expression: doc
---
========================================================================================================================================================================================================

SELECT *
FROM test_data AS test_data
WHERE ((((test_data.country_code <> 'Distinctio.') AND ((test_data.* < false) AND (NOT (test_data.description LIKE 'Esse solam.') AND NOT (test_data.transaction_id LIKE 'Esset accusata.')))) OR (test_data.test_address <> 'Potest. Sed.')) AND (test_data.* > -28.146858383543243))

========================================================================================================================================================================================================

------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
SELECT * FROM test_data AS test_data WHERE ((((test_data.country_code <> 'Distinctio.') AND ((test_data.* < false) AND (NOT (test_data.description LIKE 'Esse solam.') AND
NOT (test_data.transaction_id LIKE 'Esset accusata.')))) OR (test_data.test_address <> 'Potest. Sed.')) AND (test_data.* > -28.146858383543243))

------------------------------------------------------------------------------------------------------------------------
SELECT * FROM test_data AS test_data WHERE ((((test_data.country_code <> 'Distinctio.') AND ((test_data.* < false) AND
(NOT (test_data.description LIKE 'Esse solam.') AND NOT (test_data.transaction_id LIKE 'Esset accusata.')))) OR
(test_data.test_address <> 'Potest. Sed.')) AND (test_data.* > -28.146858383543243))

--------------------------------------------------------------------------------
SELECT * FROM test_data AS test_data
WHERE ((((test_data.country_code <> 'Distinctio.') AND ((test_data.* < false)
AND (NOT (test_data.description LIKE 'Esse solam.') AND
NOT (test_data.transaction_id LIKE 'Esset accusata.')))) OR
(test_data.test_address <> 'Potest. Sed.')) AND
(test_data.* > -28.146858383543243))

----------------------------------------
SELECT * FROM test_data AS test_data
WHERE ((((test_data.country_code <> 'Distinctio.')
AND ((test_data.* < false) AND
(NOT (test_data.description LIKE 'Esse solam.')
AND
NOT (test_data.transaction_id LIKE 'Esset accusata.'))))
OR
(test_data.test_address <> 'Potest. Sed.'))
AND
(test_data.* > -28.146858383543243))

------------------------------
SELECT *
FROM test_data AS test_data
WHERE ((((test_data.country_code <> 'Distinctio.')
AND
((test_data.* < false)
AND
(NOT (test_data.description LIKE 'Esse solam.')
AND
NOT (test_data.transaction_id LIKE 'Esset accusata.'))))
OR
(test_data.test_address <> 'Potest. Sed.'))
AND
(test_data.* > -28.146858383543243))

--------------------
SELECT *
FROM test_data AS test_data
WHERE ((((test_data.country_code <> 'Distinctio.')
AND
((test_data.* < false)
AND
(NOT (test_data.description LIKE 'Esse solam.')
AND
NOT (test_data.transaction_id LIKE 'Esset accusata.'))))
OR
(test_data.test_address <> 'Potest. Sed.'))
AND
(test_data.* > -28.146858383543243))

----------
SELECT *
FROM test_data AS test_data
WHERE ((((test_data.country_code <> 'Distinctio.')
AND
((test_data.* < false)
AND
(NOT (test_data.description LIKE 'Esse solam.')
AND
NOT (test_data.transaction_id LIKE 'Esset accusata.'))))
OR
(test_data.test_address <> 'Potest. Sed.'))
AND
(test_data.* > -28.146858383543243))
Loading
Loading