-
Notifications
You must be signed in to change notification settings - Fork 3.8k
/
orderby.go
277 lines (245 loc) · 9.9 KB
/
orderby.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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
// Copyright 2018 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"
"github.com/cockroachdb/cockroach/pkg/sql/opt/cat"
"github.com/cockroachdb/cockroach/pkg/sql/pgwire/pgcode"
"github.com/cockroachdb/cockroach/pkg/sql/pgwire/pgerror"
"github.com/cockroachdb/cockroach/pkg/sql/privilege"
"github.com/cockroachdb/cockroach/pkg/sql/sem/tree"
"github.com/cockroachdb/cockroach/pkg/sql/types"
)
// analyzeOrderBy analyzes an Ordering physical property from the ORDER BY
// clause and adds the resulting typed expressions to orderByScope.
func (b *Builder) analyzeOrderBy(
orderBy tree.OrderBy, inScope, projectionsScope *scope, rejectFlags tree.SemaRejectFlags,
) (orderByScope *scope) {
if orderBy == nil {
return nil
}
orderByScope = inScope.push()
orderByScope.cols = make([]scopeColumn, 0, len(orderBy))
// We need to save and restore the previous value of the field in
// semaCtx in case we are recursively called within a subquery
// context.
defer b.semaCtx.Properties.Restore(b.semaCtx.Properties)
b.semaCtx.Properties.Require(exprKindOrderBy.String(), rejectFlags)
inScope.context = exprKindOrderBy
for i := range orderBy {
b.analyzeOrderByArg(orderBy[i], inScope, projectionsScope, orderByScope)
}
return orderByScope
}
// buildOrderBy builds an Ordering physical property from the ORDER BY clause.
// ORDER BY is not a relational expression, but instead a required physical
// property on the output.
//
// Since the ordering property can only refer to output columns, we may need
// to add a projection for the ordering columns. For example, consider the
// following query:
// SELECT a FROM t ORDER BY c
// The `c` column must be retained in the projection (and the presentation
// property then omits it).
//
// buildOrderBy builds a set of memo groups for any ORDER BY columns that are
// not already present in the SELECT list (as represented by the initial set
// of columns in projectionsScope). buildOrderBy adds these new ORDER BY
// columns to the projectionsScope and sets the ordering property on the
// projectionsScope. This property later becomes part of the required physical
// properties returned by Build.
func (b *Builder) buildOrderBy(inScope, projectionsScope, orderByScope *scope) {
if orderByScope == nil {
return
}
orderByScope.ordering = make([]opt.OrderingColumn, 0, len(orderByScope.cols))
for i := range orderByScope.cols {
b.buildOrderByArg(inScope, projectionsScope, orderByScope, &orderByScope.cols[i])
}
projectionsScope.setOrdering(orderByScope.cols, orderByScope.ordering)
}
// findIndexByName returns an index in the table with the given name. If the
// name is empty the primary index is returned.
func (b *Builder) findIndexByName(table cat.Table, name tree.UnrestrictedName) (cat.Index, error) {
if name == "" {
return table.Index(0), nil
}
for i, n := 0, table.IndexCount(); i < n; i++ {
idx := table.Index(i)
if tree.Name(name) == idx.Name() {
return idx, nil
}
}
return nil, pgerror.Newf(pgcode.UndefinedObject,
`index %q not found`, name)
}
// addOrderByOrDistinctOnColumn builds extraCol.expr as a column in extraColsScope; if it is
// already projected in projectionsScope then that projection is re-used.
func (b *Builder) addOrderByOrDistinctOnColumn(
inScope, projectionsScope, extraColsScope *scope, extraCol *scopeColumn,
) {
// Use an existing projection if possible (even if it has side-effects; see
// the SQL99 rules described in analyzeExtraArgument). Otherwise, build a new
// projection.
if col := projectionsScope.findExistingCol(
extraCol.getExpr(),
true, /* allowSideEffects */
); col != nil {
extraCol.id = col.id
} else {
b.buildScalar(extraCol.getExpr(), inScope, extraColsScope, extraCol, nil)
}
}
// analyzeOrderByIndex appends to the orderByScope a column for each indexed
// column in the specified index, including the implicit primary key columns.
func (b *Builder) analyzeOrderByIndex(order *tree.Order, inScope, orderByScope *scope) {
tab, tn := b.resolveTable(&order.Table, privilege.SELECT)
index, err := b.findIndexByName(tab, order.Index)
if err != nil {
panic(err)
}
// We fully qualify the table name in case another table expression was
// aliased to the same name as an existing table.
tn.ExplicitCatalog = true
tn.ExplicitSchema = true
// Append each key column from the index (including the implicit primary key
// columns) to the ordering scope.
for i, n := 0, index.KeyColumnCount(); i < n; i++ {
// Columns which are indexable are always orderable.
col := index.Column(i)
desc := col.Descending
// DESC inverts the order of the index.
if order.Direction == tree.Descending {
desc = !desc
}
colItem := tree.NewColumnItem(&tn, col.ColName())
expr := inScope.resolveType(colItem, types.Any)
outCol := orderByScope.addColumn(scopeColName(""), expr)
outCol.descending = desc
}
}
// analyzeOrderByArg analyzes a single ORDER BY argument. Typically this is a
// single column, with the exception of qualified star "table.*". The resulting
// typed expression(s) are added to orderByScope.
func (b *Builder) analyzeOrderByArg(
order *tree.Order, inScope, projectionsScope, orderByScope *scope,
) {
if order.OrderType == tree.OrderByIndex {
b.analyzeOrderByIndex(order, inScope, orderByScope)
return
}
// Set NULL order. The default order is nulls first for ascending order and
// nulls last for descending order.
nullsDefaultOrder := true
if order.NullsOrder != tree.DefaultNullsOrder &&
((order.NullsOrder == tree.NullsFirst && order.Direction == tree.Descending) ||
(order.NullsOrder == tree.NullsLast && order.Direction != tree.Descending)) {
nullsDefaultOrder = false
}
// Analyze the ORDER BY column(s).
start := len(orderByScope.cols)
b.analyzeExtraArgument(order.Expr, inScope, projectionsScope, orderByScope, nullsDefaultOrder)
for i := start; i < len(orderByScope.cols); i++ {
col := &orderByScope.cols[i]
col.descending = order.Direction == tree.Descending
}
}
// buildOrderByArg sets up the projection of a single ORDER BY argument.
// The projection column is built in the orderByScope and used to build
// an ordering on the same scope.
func (b *Builder) buildOrderByArg(
inScope, projectionsScope, orderByScope *scope, orderByCol *scopeColumn,
) {
// Build the ORDER BY column.
b.addOrderByOrDistinctOnColumn(inScope, projectionsScope, orderByScope, orderByCol)
// Add the new column to the ordering.
orderByScope.ordering = append(orderByScope.ordering,
opt.MakeOrderingColumn(orderByCol.id, orderByCol.descending),
)
}
// analyzeExtraArgument analyzes a single ORDER BY or DISTINCT ON argument.
// Typically this is a single column, with the exception of qualified star
// (table.*). The resulting typed expression(s) are added to extraColsScope. The
// nullsDefaultOrder bool determines whether an extra ordering column is
// required to explicitly place nulls first or nulls last (when
// nullsDefaultOrder is false).
func (b *Builder) analyzeExtraArgument(
expr tree.Expr, inScope, projectionsScope, extraColsScope *scope, nullsDefaultOrder bool,
) {
// Unwrap parenthesized expressions like "((a))" to "a".
expr = tree.StripParens(expr)
// The logical data source for ORDER BY or DISTINCT ON is the list of column
// expressions for a SELECT, as specified in the input SQL text (or an entire
// UNION or VALUES clause). Alas, SQL has some historical baggage from SQL92
// and there are some special cases:
//
// SQL92 rules:
//
// 1) if the expression is the aliased (AS) name of an
// expression in a SELECT clause, then use that
// expression as sort key.
// e.g. SELECT a AS b, b AS c ORDER BY b
// this sorts on the first column.
//
// 2) column ordinals. If a simple integer literal is used,
// optionally enclosed within parentheses but *not subject to
// any arithmetic*, then this refers to one of the columns of
// the data source. Then use the SELECT expression at that
// ordinal position as sort key.
//
// SQL99 rules:
//
// 3) otherwise, if the expression is already in the SELECT list,
// then use that expression as sort key.
// e.g. SELECT b AS c ORDER BY b
// this sorts on the first column.
// (this is an optimization)
//
// 4) if the sort key is not dependent on the data source (no
// IndexedVar) then simply do not sort. (this is an optimization)
//
// 5) otherwise, add a new projection with the ORDER BY expression
// and use that as sort key.
// e.g. SELECT a FROM t ORDER by b
// e.g. SELECT a, b FROM t ORDER by a+b
// First, deal with projection aliases.
idx := colIdxByProjectionAlias(expr, inScope.context.String(), projectionsScope)
// If the expression does not refer to an alias, deal with
// column ordinals.
if idx == -1 {
idx = colIndex(len(projectionsScope.cols), expr, inScope.context.String())
}
var exprs tree.TypedExprs
if idx != -1 {
exprs = []tree.TypedExpr{projectionsScope.cols[idx].getExpr()}
} else {
exprs = b.expandStarAndResolveType(expr, inScope)
// ORDER BY (a, b) -> ORDER BY a, b
exprs = flattenTuples(exprs)
}
for _, e := range exprs {
// Ensure we can order on the given column(s).
ensureColumnOrderable(e)
if !nullsDefaultOrder {
extraColsScope.addColumn(
scopeColName("").WithMetadataName("nulls_ordering"),
tree.NewTypedIsNullExpr(e),
)
}
extraColsScope.addColumn(scopeColName(""), e)
}
}
func ensureColumnOrderable(e tree.TypedExpr) {
typ := e.ResolvedType()
if typ.Family() == types.JsonFamily ||
(typ.Family() == types.ArrayFamily && typ.ArrayContents().Family() == types.JsonFamily) {
panic(unimplementedWithIssueDetailf(35706, "", "can't order by column type jsonb"))
}
}