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

opt: fix hidden column handling for WITH #41187

Merged
merged 1 commit into from
Oct 1, 2019
Merged
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
7 changes: 5 additions & 2 deletions pkg/sql/opt/optbuilder/scope.go
Original file line number Diff line number Diff line change
Expand Up @@ -399,11 +399,14 @@ func (s *scope) hasSameColumns(other *scope) bool {
return s.colSetWithExtraCols().Equals(other.colSetWithExtraCols())
}

// removeHiddenCols removes hidden columns from the scope.
// removeHiddenCols removes hidden columns from the scope (and moves them to
// extraCols, in case they are referenced by ORDER BY or DISTINCT ON).
func (s *scope) removeHiddenCols() {
n := 0
for i := range s.cols {
if !s.cols[i].hidden {
if s.cols[i].hidden {
s.extraCols = append(s.extraCols, s.cols[i])
} else {
if n != i {
s.cols[n] = s.cols[i]
}
Expand Down
1 change: 1 addition & 0 deletions pkg/sql/opt/optbuilder/select.go
Original file line number Diff line number Diff line change
Expand Up @@ -579,6 +579,7 @@ func (b *Builder) buildCTE(
outScope.ctes = make(map[string]*cteSource)
for i := range ctes {
cteScope := b.buildStmt(ctes[i].Stmt, nil /* desiredTypes */, outScope)
cteScope.removeHiddenCols()
cols := cteScope.cols
name := ctes[i].Name.Alias

Expand Down
26 changes: 26 additions & 0 deletions pkg/sql/opt/optbuilder/testdata/with
Original file line number Diff line number Diff line change
Expand Up @@ -762,3 +762,29 @@ with &1 (t)
│ └── mapping:
│ └── xy.x:1(int) => x:16(int)
└── variable: xy.x [type=int]

# Check hidden column handling: level, node_type should not be output.
build
WITH cte AS (EXPLAIN (VERBOSE) SELECT 1) SELECT * FROM cte
----
with &1 (cte)
├── columns: tree:9(string) field:10(string) description:11(string) columns:12(string) ordering:13(string)
├── project
│ ├── columns: tree:2(string) field:5(string) description:6(string) columns:7(string) ordering:8(string)
│ └── explain
│ ├── columns: tree:2(string) level:3(int) node_type:4(string) field:5(string) description:6(string) columns:7(string) ordering:8(string)
│ ├── mode: verbose
│ └── project
│ ├── columns: "?column?":1(int!null)
│ ├── values
│ │ └── tuple [type=tuple]
│ └── projections
│ └── const: 1 [type=int]
└── with-scan &1 (cte)
├── columns: tree:9(string) field:10(string) description:11(string) columns:12(string) ordering:13(string)
└── mapping:
├── tree:2(string) => tree:9(string)
├── field:5(string) => field:10(string)
├── description:6(string) => description:11(string)
├── columns:7(string) => columns:12(string)
└── ordering:8(string) => ordering:13(string)