forked from cockroachdb/cockroach
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
opt: use Presentation for cteSource.cols in optbuilder
The columns in `cteSource` are only used for their IDs and names, which is better represented by a `Presentation`. This will make things easier for recursive CTEs. We also make the "no columns" error more general since our syntax allows more types of statements inside WITH. Release note: None
- Loading branch information
1 parent
3673ad8
commit 84d9fa5
Showing
6 changed files
with
79 additions
and
48 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
// Copyright 2019 The Cockroach Authors. | ||
// | ||
// Use of this software is governed by the Business Source License | ||
// included in the file licenses/BSL.txt. | ||
// | ||
// As of the Change Date specified in that file, in accordance with | ||
// the Business Source License, use of this software will be governed | ||
// by the Apache License, Version 2.0, included in the file | ||
// licenses/APL.txt. | ||
|
||
package optbuilder | ||
|
||
import ( | ||
"github.com/cockroachdb/cockroach/pkg/sql/opt/props/physical" | ||
"github.com/cockroachdb/cockroach/pkg/sql/pgwire/pgcode" | ||
"github.com/cockroachdb/cockroach/pkg/sql/pgwire/pgerror" | ||
"github.com/cockroachdb/cockroach/pkg/sql/sem/tree" | ||
"github.com/cockroachdb/errors" | ||
) | ||
|
||
// getCTECols renames a presentation for the scope, renaming the columns to | ||
// those provided in the AliasClause (if any). Throws an error if there is a | ||
// mistmatch in the number of columns. | ||
func (b *Builder) getCTECols(cteScope *scope, name tree.AliasClause) physical.Presentation { | ||
presentation := cteScope.makePresentation() | ||
|
||
if len(presentation) == 0 { | ||
err := pgerror.Newf( | ||
pgcode.FeatureNotSupported, | ||
"WITH clause %q does not return any columns", | ||
tree.ErrString(&name), | ||
) | ||
panic(errors.WithHint(err, "missing RETURNING clause?")) | ||
} | ||
|
||
if name.Cols == nil { | ||
return presentation | ||
} | ||
|
||
if len(presentation) != len(name.Cols) { | ||
panic(pgerror.Newf( | ||
pgcode.InvalidColumnReference, | ||
"source %q has %d columns available but %d columns specified", | ||
name.Alias, len(presentation), len(name.Cols), | ||
)) | ||
} | ||
for i := range presentation { | ||
presentation[i].Alias = string(name.Cols[i]) | ||
} | ||
return presentation | ||
} |