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

Change HIR's and LIR's EXPLAIN CTE order #31132

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
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
12 changes: 6 additions & 6 deletions src/compute-types/src/explain/text.rs
Original file line number Diff line number Diff line change
Expand Up @@ -141,16 +141,16 @@ impl DisplayText<PlanRenderingContext<'_, Plan>> for Plan {
head = body.as_ref();
}

writeln!(f, "{}Return{}", ctx.indent, annotations)?;
ctx.indented(|ctx| head.fmt_text(f, ctx))?;
writeln!(f, "{}With", ctx.indent)?;
ctx.indented(|ctx| {
for (id, value) in bindings.iter().rev() {
for (id, value) in bindings.iter() {
writeln!(f, "{}cte {} =", ctx.indent, *id)?;
ctx.indented(|ctx| value.fmt_text(f, ctx))?;
}
Ok(())
})?;
writeln!(f, "{}Return{}", ctx.indent, annotations)?;
ctx.indented(|ctx| head.fmt_text(f, ctx))?;
}
LetRec {
ids,
Expand All @@ -161,11 +161,9 @@ impl DisplayText<PlanRenderingContext<'_, Plan>> for Plan {
let bindings = izip!(ids.iter(), values, limits).collect_vec();
let head = body.as_ref();

writeln!(f, "{}Return{}", ctx.indent, annotations)?;
ctx.indented(|ctx| head.fmt_text(f, ctx))?;
writeln!(f, "{}With Mutually Recursive", ctx.indent)?;
ctx.indented(|ctx| {
for (id, value, limit) in bindings.iter().rev() {
for (id, value, limit) in bindings.iter() {
if let Some(limit) = limit {
writeln!(f, "{}cte {} {} =", ctx.indent, limit, *id)?;
} else {
Expand All @@ -175,6 +173,8 @@ impl DisplayText<PlanRenderingContext<'_, Plan>> for Plan {
}
Ok(())
})?;
writeln!(f, "{}Return{}", ctx.indent, annotations)?;
ctx.indented(|ctx| head.fmt_text(f, ctx))?;
}
Mfp {
input,
Expand Down
24 changes: 12 additions & 12 deletions src/sql/src/plan/explain/text.rs
Original file line number Diff line number Diff line change
Expand Up @@ -95,59 +95,59 @@ impl HirRelationExpr {
}
}
Let {
name: _,
name,
id,
value,
body,
} => {
let mut bindings = vec![(id, value.as_ref())];
let mut bindings = vec![(id, name, value.as_ref())];
let mut head = body.as_ref();

// Render Let-blocks nested in the body an outer Let-block in one step
// with a flattened list of bindings
while let Let {
name: _,
name,
id,
value,
body,
} = head
{
bindings.push((id, value.as_ref()));
bindings.push((id, name, value.as_ref()));
head = body.as_ref();
}

writeln!(f, "{}Return", ctx.indent)?;
ctx.indented(|ctx| head.fmt_text(f, ctx))?;
writeln!(f, "{}With", ctx.indent)?;
ctx.indented(|ctx| {
for (id, value) in bindings.iter().rev() {
for (id, name, value) in bindings.iter() {
// TODO: print the name and not the id
writeln!(f, "{}cte {} =", ctx.indent, *id)?;
writeln!(f, "{}cte [{} as {}] =", ctx.indent, *id, *name)?;
ctx.indented(|ctx| value.fmt_text(f, ctx))?;
}
Ok(())
})?;
writeln!(f, "{}Return", ctx.indent)?;
ctx.indented(|ctx| head.fmt_text(f, ctx))?;
}
LetRec {
limit,
bindings,
body,
} => {
writeln!(f, "{}Return", ctx.indent)?;
ctx.indented(|ctx| body.fmt_text(f, ctx))?;
write!(f, "{}With Mutually Recursive", ctx.indent)?;
if let Some(limit) = limit {
write!(f, " {}", limit)?;
}
writeln!(f)?;
ctx.indented(|ctx| {
for (_name, id, value, _type) in bindings.iter().rev() {
for (name, id, value, _type) in bindings.iter() {
// TODO: print the name and not the id
writeln!(f, "{}cte {} =", ctx.indent, *id)?;
writeln!(f, "{}cte [{} as {}] =", ctx.indent, *id, *name)?;
ctx.indented(|ctx| value.fmt_text(f, ctx))?;
}
Ok(())
})?;
writeln!(f, "{}Return", ctx.indent)?;
ctx.indented(|ctx| body.fmt_text(f, ctx))?;
}
Get { id, .. } => match id {
Id::Local(id) => {
Expand Down
86 changes: 43 additions & 43 deletions test/sqllogictest/cte_lowering.slt
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,14 @@ WITH t AS (SELECT * FROM y WHERE a < 3)
SELECT * FROM t NATURAL JOIN t a;
----
Project (#0)
With
cte [l0 as t] =
Filter (#0 < 3)
Get materialize.public.y
Return
InnerJoin (#0 = #1)
Get l0
Get l0
With
cte l0 =
Filter (#0 < 3)
Get materialize.public.y

Target cluster: quickstart

Expand All @@ -49,13 +49,13 @@ EXPLAIN RAW PLAN FOR
WITH t AS (SELECT * FROM y WHERE a < 3)
SELECT * FROM y WHERE (select a from t) < a;
----
Return
Filter (select(Get l0) < #0)
Get materialize.public.y
With
cte l0 =
cte [l0 as t] =
Filter (#0 < 3)
Get materialize.public.y
Return
Filter (select(Get l0) < #0)
Get materialize.public.y

Target cluster: quickstart

Expand Down Expand Up @@ -527,45 +527,45 @@ FROM (WITH e(e) AS (SELECT b FROM b)
< (SELECT max(x) FROM x))
----
Project (#1)
Return
Map (select(Get l8))
Return
Filter (select(Get l6) < select(Get l7))
Get l3
With
cte [l0 as a] =
Get materialize.public.y
cte [l1 as b] =
Get materialize.public.y
cte [l2 as x] =
Get l1
cte [l8 as subquery-8] =
With
cte l6 =
Return
Reduce aggregates=[min(#0)]
Get l4
cte [l3 as c] =
Get materialize.public.y
cte [l5 as subquery-5] =
With
cte l4 =
cte [l4 as d] =
Get l3
cte l7 =
Reduce aggregates=[max(#0)]
Get l2
cte l3 =
Get l1
With
cte l8 =
Return
Reduce aggregates=[max(#0)]
Get l4
Return
Filter (select(Get l5) > 1)
Get l3
Return
Map (select(Get l8))
With
cte l5 =
Return
Reduce aggregates=[max(#0)]
Get l4
cte [l3 as e] =
Get l1
cte [l7 as subquery-7] =
Reduce aggregates=[max(#0)]
Get l2
cte [l6 as subquery-6] =
With
cte l4 =
cte [l4 as f] =
Get l3
cte l3 =
Get materialize.public.y
cte l2 =
Get l1
cte l1 =
Get materialize.public.y
cte l0 =
Get materialize.public.y
Return
Reduce aggregates=[min(#0)]
Get l4
Return
Filter (select(Get l6) < select(Get l7))
Get l3

Target cluster: quickstart

Expand All @@ -576,13 +576,13 @@ query T multiline
EXPLAIN RAW PLAN FOR
WITH a(a) AS (SELECT a FROM y) SELECT * FROM (WITH a(a) AS (SELECT a FROM a) SELECT a FROM a);
----
Return
Get l1
With
cte l1 =
Get l0
cte l0 =
cte [l0 as a] =
Get materialize.public.y
cte [l1 as a] =
Get l0
Return
Get l1

Target cluster: quickstart

Expand Down
Loading
Loading