Skip to content

Commit

Permalink
sql: improve formatting of Remap nodes
Browse files Browse the repository at this point in the history
  • Loading branch information
erikgrinaker committed Jul 16, 2024
1 parent 6edb9e5 commit 5de0125
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 21 deletions.
15 changes: 13 additions & 2 deletions src/sql/planner/plan.rs
Original file line number Diff line number Diff line change
Expand Up @@ -513,13 +513,24 @@ impl Node {
Self::Remap { source, targets } => {
let remap = remap_sources(targets)
.into_iter()
.map(|from| from.map(|from| format!("#{from}")).unwrap_or("Null".to_string()))
.map(|from| {
from.map(|from| match source.column_label(from) {
Label::None => format!("#{from}"),
label => format!("{label}"),
})
.unwrap_or("Null".to_string())
})
.join(", ");
write!(f, "Remap: {remap}")?;
let dropped = targets
.iter()
.enumerate()
.filter_map(|(i, v)| v.is_none().then_some(format!("#{i}")))
.filter_map(|(i, v)| {
v.is_none().then_some(match source.column_label(i) {
Label::None => format!("#{i}"),
label => format!("{label}"),
})
})
.join(", ");
if !dropped.is_empty() {
write!(f, " (dropped: {dropped})")?;
Expand Down
12 changes: 6 additions & 6 deletions src/sql/testscripts/queries/having
Original file line number Diff line number Diff line change
Expand Up @@ -46,15 +46,15 @@ b, 42

[plan]> SELECT "group" FROM test GROUP BY "group" HAVING MAX("int") > 10
---
Remap: #0 (dropped: #1)
Remap: test.group (dropped: #1)
└─ Filter: #1 > 10
└─ Aggregate: test.group, max(test.int)
└─ Scan: test
b

[plan]> SELECT "group", MAX("int") FROM test GROUP BY "group" HAVING MAX("int") - MIN("int") > 10
---
Remap: #0, #1 (dropped: #2)
Remap: test.group, #1 (dropped: #2)
└─ Filter: #1 - #2 > 10
└─ Aggregate: test.group, max(test.int), min(test.int)
└─ Scan: test
Expand All @@ -72,7 +72,7 @@ b, 42
# Having works with an aggregate function not in the SELECT clause.
[plan]> SELECT "group", COUNT(*) FROM test GROUP BY "group" HAVING MAX("int") > 10
---
Remap: #0, #1 (dropped: #2)
Remap: test.group, #1 (dropped: #2)
└─ Filter: #2 > 10
└─ Aggregate: test.group, count(TRUE), max(test.int)
└─ Scan: test
Expand All @@ -81,7 +81,7 @@ b, 3
# Having works with compound expressions.
[plan]> SELECT "group", COUNT(*) FROM test GROUP BY "group" HAVING MAX("int") / COUNT(*) > 3
---
Remap: #0, #1 (dropped: #2)
Remap: test.group, #1 (dropped: #2)
└─ Filter: #2 / #1 > 3
└─ Aggregate: test.group, count(TRUE), max(test.int)
└─ Scan: test
Expand All @@ -102,7 +102,7 @@ Remap: #0 (dropped: #1)
# Having can use (un)qualified expressions for an (un)qualified GROUP BY.
[plan]> SELECT COUNT(*) FROM test GROUP BY "group" HAVING test."group" = 'a'
---
Remap: #0 (dropped: #1)
Remap: #0 (dropped: test.group)
└─ Filter: test.group = a
└─ Projection: #1, test.group
└─ Aggregate: test.group, count(TRUE)
Expand All @@ -111,7 +111,7 @@ Remap: #0 (dropped: #1)

[plan]> SELECT COUNT(*) FROM test GROUP BY test."group" HAVING "group" = 'a'
---
Remap: #0 (dropped: #1)
Remap: #0 (dropped: test.group)
└─ Filter: test.group = a
└─ Projection: #1, test.group
└─ Aggregate: test.group, count(TRUE)
Expand Down
2 changes: 1 addition & 1 deletion src/sql/testscripts/queries/join_inner
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,7 @@ Error: invalid input: unknown field movies.unknown_id
WHERE m.studio_id = s.id \
ORDER BY m.rating DESC, m.released ASC, m.id ASC
---
Remap: #0, #1, #2, #3, #4 (dropped: #5)
Remap: m.id, m.title, genre, studio, m.rating (dropped: m.released)
└─ Order: m.rating desc, m.released asc, m.id asc
└─ Projection: m.id, m.title, g.name as genre, s.name as studio, m.rating, m.released
└─ HashJoin: inner on m.studio_id = s.id
Expand Down
8 changes: 4 additions & 4 deletions src/sql/testscripts/queries/join_outer
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ HashJoin: outer on movies.id = genres.id
# Right join.
[plan]> SELECT * FROM genres RIGHT JOIN movies ON movies.id = genres.id
---
Remap: #7, #8, #0, #1, #2, #3, #4, #5, #6
Remap: genres.id, genres.name, movies.id, movies.title, movies.studio_id, movies.genre_id, movies.released, movies.rating, movies.ultrahd
└─ HashJoin: outer on movies.id = genres.id
├─ Scan: movies
└─ Scan: genres
Expand Down Expand Up @@ -104,7 +104,7 @@ HashJoin: outer on movies.id = genres.id

[plan]> SELECT * FROM genres RIGHT OUTER JOIN movies ON movies.id = genres.id
---
Remap: #7, #8, #0, #1, #2, #3, #4, #5, #6
Remap: genres.id, genres.name, movies.id, movies.title, movies.studio_id, movies.genre_id, movies.released, movies.rating, movies.ultrahd
└─ HashJoin: outer on movies.id = genres.id
├─ Scan: movies
└─ Scan: genres
Expand All @@ -131,7 +131,7 @@ HashJoin: outer on genres.id = movies.id

[plan]> SELECT * FROM movies RIGHT JOIN genres ON movies.id = genres.id
---
Remap: #2, #3, #4, #5, #6, #7, #8, #0, #1
Remap: movies.id, movies.title, movies.studio_id, movies.genre_id, movies.released, movies.rating, movies.ultrahd, genres.id, genres.name
└─ HashJoin: outer on genres.id = movies.id
├─ Scan: genres
└─ Scan: movies
Expand Down Expand Up @@ -164,7 +164,7 @@ NestedLoopJoin: outer on genres.id > movies.id OR genres.id = movies.id
LEFT JOIN genres ON studios.id = genres.id \
RIGHT JOIN movies ON movies.id = studios.id
---
Remap: #7, #8, #9, #10, #11, #0, #1, #2, #3, #4, #5, #6
Remap: studios.id, studios.name, studios.country_id, genres.id, genres.name, movies.id, movies.title, movies.studio_id, movies.genre_id, movies.released, movies.rating, movies.ultrahd
└─ HashJoin: outer on movies.id = studios.id
├─ Scan: movies
└─ HashJoin: outer on studios.id = genres.id
Expand Down
16 changes: 8 additions & 8 deletions src/sql/testscripts/queries/order
Original file line number Diff line number Diff line change
Expand Up @@ -258,7 +258,7 @@ Order: test.float ^ 2 asc
# only result in one hidden column.
[plan]> SELECT id, "int" FROM test ORDER BY "bool" DESC
---
Remap: #0, #1 (dropped: #2)
Remap: test.id, test.int (dropped: test.bool)
└─ Order: test.bool desc
└─ Projection: test.id, test.int, test.bool
└─ Scan: test
Expand All @@ -275,7 +275,7 @@ Remap: #0, #1 (dropped: #2)

[plan]> SELECT id, "int" FROM test ORDER BY "bool" DESC, "bool" ASC
---
Remap: #0, #1 (dropped: #2)
Remap: test.id, test.int (dropped: test.bool)
└─ Order: test.bool desc, test.bool asc
└─ Projection: test.id, test.int, test.bool
└─ Scan: test
Expand All @@ -293,7 +293,7 @@ Remap: #0, #1 (dropped: #2)
# Can order on expressions on columns not in the result.
[plan]> SELECT id FROM test ORDER BY "float" ^ 2 - "int" ^ 2 DESC
---
Remap: #0 (dropped: #1, #2)
Remap: test.id (dropped: test.float, test.int)
└─ Order: test.float ^ 2 - test.int ^ 2 desc
└─ Projection: test.id, test.float, test.int
└─ Scan: test
Expand Down Expand Up @@ -373,7 +373,7 @@ Order: int desc

[plan]> SELECT id AS "int" FROM test ORDER BY test."int" DESC
---
Remap: #0 (dropped: #1)
Remap: int (dropped: test.int)
└─ Order: test.int desc
└─ Projection: test.id as int, test.int
└─ Scan: test
Expand Down Expand Up @@ -481,7 +481,7 @@ FALSE, -1

[plan]> SELECT "bool" FROM test GROUP BY "bool" ORDER BY MAX("int") DESC
---
Remap: #0 (dropped: #1)
Remap: test.bool (dropped: #1)
└─ Order: #1 desc
└─ Aggregate: test.bool, max(test.int)
└─ Scan: test
Expand All @@ -491,7 +491,7 @@ FALSE

[plan]> SELECT "bool", MAX("int") FROM test GROUP BY "bool" ORDER BY MAX("int") - MIN("int") DESC
---
Remap: #0, #1 (dropped: #2)
Remap: test.bool, #1 (dropped: #2)
└─ Order: #1 - #2 desc
└─ Aggregate: test.bool, max(test.int), min(test.int)
└─ Scan: test
Expand All @@ -514,7 +514,7 @@ Remap: #0 (dropped: #1)
# ORDER BY can use (un)qualified expressions for an (un)qualified GROUP BY.
[plan]> SELECT COUNT(*) FROM test GROUP BY "bool" ORDER BY test."bool"
---
Remap: #0 (dropped: #1)
Remap: #0 (dropped: test.bool)
└─ Order: test.bool asc
└─ Projection: #1, test.bool
└─ Aggregate: test.bool, count(TRUE)
Expand All @@ -525,7 +525,7 @@ Remap: #0 (dropped: #1)

[plan]> SELECT COUNT(*) FROM test GROUP BY test."bool" ORDER BY "bool"
---
Remap: #0 (dropped: #1)
Remap: #0 (dropped: test.bool)
└─ Order: test.bool asc
└─ Projection: #1, test.bool
└─ Aggregate: test.bool, count(TRUE)
Expand Down

0 comments on commit 5de0125

Please sign in to comment.