Change CTE order in EXPLAIN output #30983
Open
+11,137
−11,092
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This PR modifies how EXPLAIN prints Let/LetRec: It used to be
Return
first, and CTEs in bottom-up order, now it's CTEs in top-down order, and thenReturn
. The main argument for the old order was that data flowed upwards both inside CTEs and across CTEs. However, the bottom-up order of CTE bindings kept tripping up optimizer developers, so it's probably better to have the CTEs in binding order (top-down). As @mgree noted, other databases also print in top-down order.For example:
used to be
and now it would be
The first commit is straightforward: just a few lines of code changes, and a bunch of expected test output rewrites (most of it with auto-rewrite, except for
testdrive
).The second commit is unfortunately not so straightforward. The problem is that the
.spec
test framework's input format is supposed to correspond to EXPLAIN's output format, but now we are reversing EXPLAIN's output format. There are several options on how to handle this situation:REWRITE_SPEC_INPUTS
env var (similar toREWRITE
), which would simply roundtrip spec test inputs through the spec parser and EXPLAIN, and use the result to rewrite the inputs. This would be the cleanest solution from a usability perspective. However, it's not totally straightforward to implement, because the spec framework relies on the (external)datadriven
framework, which doesn't provide a facility for rewriting inputs out of the box. We'd probably need to crack opendatadriven
, and copy-paste some code from it to implement this ourselves. I'm open to doing this. I have a feeling that we won't be able to avoid this in the long run, when we keep making further changes to EXPLAIN output.Now, 2. vs. 3. is open for debate. This PR just did 2. so far, because it was a matter of minutes to implement, but if we want to commit to the
spec
test framework for the long-term, then sooner or later we'll have to do 3., so might as well do it now (maybe in an immediate follow-up PR), to avoid keeping around the tech-debt of needing to keep in mind both the old and new orders when reading spec test inputs.Motivation
Tips for reviewer
Checklist
$T ⇔ Proto$T
mapping (possibly in a backwards-incompatible way), then it is tagged with aT-proto
label.