-
Notifications
You must be signed in to change notification settings - Fork 3.8k
/
with.go
51 lines (45 loc) · 1.57 KB
/
with.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
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
}