-
Notifications
You must be signed in to change notification settings - Fork 3.8k
/
select.go
1560 lines (1406 loc) · 53.3 KB
/
select.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
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Copyright 2018 The Cockroach Authors.
//
// Use of this software is governed by the CockroachDB Software License
// included in the /LICENSE file.
package optbuilder
import (
"context"
"fmt"
"github.com/cockroachdb/cockroach/pkg/server/telemetry"
"github.com/cockroachdb/cockroach/pkg/sql/catalog/catpb"
"github.com/cockroachdb/cockroach/pkg/sql/catalog/colinfo"
"github.com/cockroachdb/cockroach/pkg/sql/catalog/descpb"
"github.com/cockroachdb/cockroach/pkg/sql/catalog/tabledesc"
"github.com/cockroachdb/cockroach/pkg/sql/opt"
"github.com/cockroachdb/cockroach/pkg/sql/opt/cat"
"github.com/cockroachdb/cockroach/pkg/sql/opt/memo"
"github.com/cockroachdb/cockroach/pkg/sql/opt/props"
"github.com/cockroachdb/cockroach/pkg/sql/parser"
"github.com/cockroachdb/cockroach/pkg/sql/parser/statements"
"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/asof"
"github.com/cockroachdb/cockroach/pkg/sql/sem/cast"
"github.com/cockroachdb/cockroach/pkg/sql/sem/eval"
"github.com/cockroachdb/cockroach/pkg/sql/sem/tree"
"github.com/cockroachdb/cockroach/pkg/sql/sqlerrors"
"github.com/cockroachdb/cockroach/pkg/sql/sqltelemetry"
"github.com/cockroachdb/cockroach/pkg/sql/types"
"github.com/cockroachdb/cockroach/pkg/util/errorutil/unimplemented"
"github.com/cockroachdb/cockroach/pkg/util/intsets"
"github.com/cockroachdb/errors"
)
// buildDataSource builds a set of memo groups that represent the given table
// expression. For example, if the tree.TableExpr consists of a single table,
// the resulting set of memo groups will consist of a single group with a
// scanOp operator. Joins will result in the construction of several groups,
// including two for the left and right table scans, at least one for the join
// condition, and one for the join itself.
//
// See Builder.buildStmt for a description of the remaining input and
// return values.
func (b *Builder) buildDataSource(
texpr tree.TableExpr, indexFlags *tree.IndexFlags, lockCtx lockingContext, inScope *scope,
) (outScope *scope) {
defer func(prevAtRoot bool, prevInsideDataSource bool) {
inScope.atRoot = prevAtRoot
b.insideDataSource = prevInsideDataSource
}(inScope.atRoot, b.insideDataSource)
inScope.atRoot = false
b.insideDataSource = true
// NB: The case statements are sorted lexicographically.
switch source := (texpr).(type) {
case *tree.AliasedTableExpr:
if source.IndexFlags != nil {
telemetry.Inc(sqltelemetry.IndexHintUseCounter)
telemetry.Inc(sqltelemetry.IndexHintSelectUseCounter)
indexFlags = source.IndexFlags
}
if source.As.Alias == "" {
// The alias is an empty string. If we are in a view or UDF
// definition, we are also expanding stars and for this we need
// to ensure all unnamed subqueries have a name. (unnamed
// subqueries are a CRDB extension, so the behavior in that case
// can be CRDB-specific.)
//
// We do not perform this name assignment in the common case
// (everything else besides CREATE VIEW/FUNCTION) so as to save
// the cost of the string alloc / name propagation.
if _, ok := source.Expr.(*tree.Subquery); ok && (b.insideFuncDef || b.insideViewDef) {
b.subqueryNameIdx++
// The structure of this name is analogous to the auto-generated
// names for anonymous scalar expressions.
source.As.Alias = tree.Name(fmt.Sprintf("?subquery%d?", b.subqueryNameIdx))
}
}
if source.As.Alias != "" {
inScope.alias = &source.As
lockCtx.filter(source.As.Alias)
lockCtx.withoutTargets()
}
outScope = b.buildDataSource(source.Expr, indexFlags, lockCtx, inScope)
if source.Ordinality {
outScope = b.buildWithOrdinality(outScope)
}
// Overwrite output properties with any alias information.
b.renameSource(source.As, outScope)
return outScope
case *tree.JoinTableExpr:
return b.buildJoin(source, lockCtx, inScope)
case *tree.TableName:
tn := source
// CTEs take precedence over other data sources.
if cte := inScope.resolveCTE(tn); cte != nil {
lockCtx.locking.ignoreLockingForCTE()
outScope = inScope.push()
inCols := make(opt.ColList, len(cte.cols), len(cte.cols)+len(inScope.ordering))
outCols := make(opt.ColList, len(cte.cols), len(cte.cols)+len(inScope.ordering))
outScope.cols, outScope.extraCols = nil, nil
for i, col := range cte.cols {
id := col.ID
c := b.factory.Metadata().ColumnMeta(id)
newCol := b.synthesizeColumn(outScope, scopeColName(tree.Name(col.Alias)), c.Type, nil, nil)
newCol.table = *tn
inCols[i] = id
outCols[i] = newCol.id
}
outScope.expr = b.factory.ConstructWithScan(&memo.WithScanPrivate{
With: cte.id,
Name: string(cte.name.Alias),
InCols: inCols,
OutCols: outCols,
ID: b.factory.Metadata().NextUniqueID(),
Mtr: cte.mtr,
})
return outScope
}
ds, depName, resName := b.resolveDataSource(tn, privilege.SELECT)
lockCtx.filter(tn.ObjectName)
if lockCtx.locking.isSet() {
// If this table was on the null-extended side of an outer join, we are not
// allowed to lock it.
if lockCtx.isNullExtended {
panic(pgerror.Newf(
pgcode.FeatureNotSupported, "%s cannot be applied to the nullable side of an outer join",
lockCtx.locking.get().Strength))
}
// With sql_safe_updates set, we cannot use SELECT FOR UPDATE without a
// WHERE or LIMIT clause.
if !lockCtx.safeUpdate && b.evalCtx.SessionData().SafeUpdates {
panic(pgerror.DangerousStatementf(
"SELECT %s without WHERE or LIMIT clause", lockCtx.locking.get().Strength,
))
}
// SELECT ... FOR [KEY] UPDATE/SHARE also requires UPDATE privileges.
b.checkPrivilege(depName, ds, privilege.UPDATE)
}
switch t := ds.(type) {
case cat.Table:
tabMeta := b.addTable(t, &resName)
locking := b.lockingSpecForTableScan(lockCtx.locking, tabMeta)
return b.buildScan(
tabMeta,
tableOrdinals(t, columnKinds{
includeMutations: false,
includeSystem: true,
includeInverted: false,
}),
indexFlags, locking, inScope,
false, /* disableNotVisibleIndex */
)
case cat.Sequence:
return b.buildSequenceSelect(t, &resName, inScope)
case cat.View:
return b.buildView(t, &resName, lockCtx, inScope)
default:
panic(errors.AssertionFailedf("unknown DataSource type %T", ds))
}
case *tree.ParenTableExpr:
return b.buildDataSource(source.Expr, indexFlags, lockCtx, inScope)
case *tree.RowsFromExpr:
return b.buildZip(source.Items, inScope)
case *tree.Subquery:
// Remove any target relations from the current scope's locking spec, as
// those only apply to relations in this statement. Interestingly, this
// would not be necessary if we required all subqueries to have aliases like
// Postgres does, because then the AliasedTableExpr case would handle this.
lockCtx.withoutTargets()
outScope = b.buildSelectStmt(source.Select, lockCtx, nil /* desiredTypes */, inScope)
// Treat the subquery result as an anonymous data source (i.e. column names
// are not qualified). Remove hidden columns, as they are not accessible
// outside the subquery.
outScope.setTableAlias("")
outScope.removeHiddenCols()
return outScope
case *tree.StatementSource:
// This is the special '[ ... ]' syntax. We treat this as syntactic sugar
// for a top-level CTE, so it cannot refer to anything in the input scope.
// See #41078.
if b.insideFuncDef {
panic(unimplemented.NewWithIssue(
92961, "statement source (square bracket syntax) within user-defined function",
))
}
emptyScope := b.allocScope()
innerScope := b.buildStmt(source.Statement, nil /* desiredTypes */, emptyScope)
if len(innerScope.cols) == 0 {
panic(pgerror.Newf(pgcode.UndefinedColumn,
"statement source \"%v\" does not return any columns", source.Statement))
}
id := b.factory.Memo().NextWithID()
b.factory.Metadata().AddWithBinding(id, innerScope.expr)
cte := &cteSource{
name: tree.AliasClause{},
cols: innerScope.makePresentationWithHiddenCols(),
originalExpr: source.Statement,
expr: innerScope.expr,
id: id,
}
b.addCTE(cte)
inCols := make(opt.ColList, len(cte.cols))
outCols := make(opt.ColList, len(cte.cols))
for i, col := range cte.cols {
id := col.ID
c := b.factory.Metadata().ColumnMeta(id)
inCols[i] = id
outCols[i] = b.factory.Metadata().AddColumn(col.Alias, c.Type)
}
lockCtx.locking.ignoreLockingForCTE()
outScope = inScope.push()
// Similar to appendColumnsFromScope, but with re-numbering the column IDs.
for i, col := range innerScope.cols {
col.scalar = nil
col.id = outCols[i]
outScope.cols = append(outScope.cols, col)
}
outScope.expr = b.factory.ConstructWithScan(&memo.WithScanPrivate{
With: cte.id,
Name: string(cte.name.Alias),
InCols: inCols,
OutCols: outCols,
ID: b.factory.Metadata().NextUniqueID(),
Mtr: cte.mtr,
})
return outScope
case *tree.TableRef:
ds, depName := b.resolveDataSourceRef(source, privilege.SELECT)
lockCtx.filter(source.As.Alias)
if lockCtx.locking.isSet() {
// If this table was on the null-extended side of an outer join, we are not
// allowed to lock it.
if lockCtx.isNullExtended {
panic(pgerror.Newf(
pgcode.FeatureNotSupported, "%s cannot be applied to the nullable side of an outer join",
lockCtx.locking.get().Strength))
}
// With sql_safe_updates set, we cannot use SELECT FOR UPDATE without a
// WHERE or LIMIT clause.
if !lockCtx.safeUpdate && b.evalCtx.SessionData().SafeUpdates {
panic(pgerror.DangerousStatementf(
"SELECT %s without WHERE or LIMIT clause", lockCtx.locking.get().Strength,
))
}
// SELECT ... FOR [KEY] UPDATE/SHARE also requires UPDATE privileges.
b.checkPrivilege(depName, ds, privilege.UPDATE)
}
switch t := ds.(type) {
case cat.Table:
outScope = b.buildScanFromTableRef(t, source, indexFlags, lockCtx.locking, inScope)
case cat.View:
if source.Columns != nil {
panic(pgerror.Newf(pgcode.FeatureNotSupported,
"cannot specify an explicit column list when accessing a view by reference"))
}
tn := tree.MakeUnqualifiedTableName(t.Name())
outScope = b.buildView(t, &tn, lockCtx, inScope)
case cat.Sequence:
tn := tree.MakeUnqualifiedTableName(t.Name())
// Any explicitly listed columns are ignored.
outScope = b.buildSequenceSelect(t, &tn, inScope)
default:
panic(errors.AssertionFailedf("unsupported catalog object"))
}
b.renameSource(source.As, outScope)
return outScope
default:
panic(errors.AssertionFailedf("unknown table expr: %T", texpr))
}
}
// buildView parses the view query text and builds it as a Select expression.
func (b *Builder) buildView(
view cat.View, viewName *tree.TableName, lockCtx lockingContext, inScope *scope,
) (outScope *scope) {
if b.sourceViews == nil {
b.sourceViews = make(map[string]struct{})
}
// Check whether there is a circular dependency between views.
if _, ok := b.sourceViews[viewName.FQString()]; ok {
panic(pgerror.Newf(pgcode.InvalidObjectDefinition, "cyclic view dependency for relation %s", viewName))
}
b.sourceViews[viewName.FQString()] = struct{}{}
defer func() {
delete(b.sourceViews, viewName.FQString())
}()
// Cache the AST so that multiple references won't need to reparse.
if b.views == nil {
b.views = make(map[cat.View]*tree.Select)
}
// Check whether view has already been parsed, and if not, parse now.
sel, ok := b.views[view]
if !ok {
stmt, err := parser.ParseOne(view.Query())
if err != nil {
wrapped := pgerror.Wrapf(err, pgcode.Syntax,
"failed to parse underlying query from view %q", view.Name())
panic(wrapped)
}
sel, ok = stmt.AST.(*tree.Select)
if !ok {
panic(errors.AssertionFailedf("expected SELECT statement"))
}
b.views[view] = sel
// Keep track of referenced views for EXPLAIN (opt, env).
b.factory.Metadata().AddView(view)
}
// When building the view, we don't want to check for the SELECT privilege
// on the underlying tables, just on the view itself. Checking on the
// underlying tables as well would defeat the purpose of having separate
// SELECT privileges on the view, which is intended to allow for exposing
// some subset of a restricted table's data to less privileged users.
if !b.skipSelectPrivilegeChecks {
b.skipSelectPrivilegeChecks = true
defer func() { b.skipSelectPrivilegeChecks = false }()
}
trackDeps := b.trackSchemaDeps
if trackDeps {
// We are only interested in the direct dependency on this view descriptor.
// Any further dependency by the view's query should not be tracked.
b.trackSchemaDeps = false
defer func() { b.trackSchemaDeps = true }()
}
// We don't want the view to be able to refer to any outer scopes in the
// query. This shouldn't happen if the view is valid but there may be
// cornercases (e.g. renaming tables referenced by the view). To be safe, we
// build the view with an empty scope. But after that, we reattach the scope
// to the existing scope chain because we want the rest of the query to be
// able to refer to the higher scopes (see #46180).
emptyScope := b.allocScope()
lockCtx.withoutTargets()
outScope = b.buildSelect(sel, lockCtx, nil /* desiredTypes */, emptyScope)
emptyScope.parent = inScope
// Update data source name to be the name of the view. And if view columns
// are specified, then update names of output columns.
hasCols := view.ColumnNameCount() > 0
for i := range outScope.cols {
outScope.cols[i].table = *viewName
if hasCols {
outScope.cols[i].name = scopeColName(view.ColumnName(i))
}
}
if trackDeps && !view.IsSystemView() {
dep := opt.SchemaDep{DataSource: view}
for i := range outScope.cols {
dep.ColumnOrdinals.Add(i)
}
b.schemaDeps = append(b.schemaDeps, dep)
}
return outScope
}
// renameSource applies an AS clause to the columns in scope.
func (b *Builder) renameSource(as tree.AliasClause, scope *scope) {
if as.Alias != "" {
colAlias := as.Cols
// Special case for Postgres compatibility: if a data source does not
// currently have a name, and it is a set-generating function or a scalar
// function with just one column, and the AS clause doesn't specify column
// names, then use the specified table name both as the column name and
// table name.
noColNameSpecified := len(colAlias) == 0
if scope.isAnonymousTable() && noColNameSpecified && scope.singleSRFColumn {
colAlias = tree.ColumnDefList{tree.ColumnDef{Name: as.Alias}}
}
// If an alias was specified, use that to qualify the column names.
tableAlias := tree.MakeUnqualifiedTableName(as.Alias)
scope.setTableAlias(as.Alias)
// If input expression is a ScanExpr, then override metadata aliases for
// pretty-printing.
scan, isScan := scope.expr.(*memo.ScanExpr)
if isScan {
tabMeta := b.factory.Metadata().TableMeta(scan.ScanPrivate.Table)
tabMeta.Alias = tree.MakeUnqualifiedTableName(as.Alias)
}
if len(colAlias) > 0 {
// The column aliases can only refer to explicit columns.
for colIdx, aliasIdx := 0, 0; aliasIdx < len(colAlias); colIdx++ {
if colIdx >= len(scope.cols) {
srcName := tree.ErrString(&tableAlias)
panic(pgerror.Newf(
pgcode.InvalidColumnReference,
"source %q has %d columns available but %d columns specified",
srcName, aliasIdx, len(colAlias),
))
}
col := &scope.cols[colIdx]
if col.visibility != visible {
continue
}
col.name = scopeColName(colAlias[aliasIdx].Name)
if isScan {
// Override column metadata alias.
colMeta := b.factory.Metadata().ColumnMeta(col.id)
colMeta.Alias = string(colAlias[aliasIdx].Name)
}
aliasIdx++
}
}
}
}
// buildScanFromTableRef adds support for numeric references in queries.
// For example:
// SELECT * FROM [53 as t]; (table reference)
// SELECT * FROM [53(1) as t]; (+columnar reference)
// SELECT * FROM [53(1) as t]@1; (+index reference)
// Note, the query SELECT * FROM [53() as t] is unsupported. Column lists must
// be non-empty
func (b *Builder) buildScanFromTableRef(
tab cat.Table,
ref *tree.TableRef,
indexFlags *tree.IndexFlags,
locking lockingSpec,
inScope *scope,
) (outScope *scope) {
var ordinals []int
if ref.Columns != nil {
// See tree.TableRef: "Note that a nil [Columns] array means 'unspecified'
// (all columns). whereas an array of length 0 means 'zero columns'.
// Lists of zero columns are not supported and will throw an error."
if len(ref.Columns) == 0 {
panic(pgerror.Newf(pgcode.Syntax,
"an explicit list of column IDs must include at least one column"))
}
ordinals = resolveNumericColumnRefs(tab, ref.Columns)
} else {
ordinals = tableOrdinals(tab, columnKinds{
includeMutations: false,
includeSystem: true,
includeInverted: false,
})
}
tn := tree.MakeUnqualifiedTableName(tab.Name())
tabMeta := b.addTable(tab, &tn)
locking = b.lockingSpecForTableScan(locking, tabMeta)
return b.buildScan(
tabMeta, ordinals, indexFlags, locking, inScope, false, /* disableNotVisibleIndex */
)
}
// addTable adds a table to the metadata and returns the TableMeta. The table
// name is passed separately in order to preserve knowledge of whether the
// catalog and schema names were explicitly specified.
func (b *Builder) addTable(tab cat.Table, alias *tree.TableName) *opt.TableMeta {
md := b.factory.Metadata()
tabID := md.AddTable(tab, alias)
return md.TableMeta(tabID)
}
// errorOnInvalidMultiregionDB panics if the table described by tabMeta is owned
// by a non-multiregion database or a multiregion database with SURVIVE REGION
// FAILURE goal.
func errorOnInvalidMultiregionDB(
ctx context.Context, evalCtx *eval.Context, tabMeta *opt.TableMeta,
) {
survivalGoal, ok := tabMeta.GetDatabaseSurvivalGoal(ctx, evalCtx.Planner)
// non-multiregional database or SURVIVE REGION FAILURE option
if !ok {
err := pgerror.Newf(pgcode.QueryHasNoHomeRegion,
"Query has no home region. Try accessing only tables in multi-region databases with ZONE survivability. %s",
sqlerrors.EnforceHomeRegionFurtherInfo)
panic(err)
} else if survivalGoal == descpb.SurvivalGoal_REGION_FAILURE {
err := pgerror.Newf(pgcode.QueryHasNoHomeRegion,
"The enforce_home_region setting cannot be combined with REGION survivability. Try accessing only tables in multi-region databases with ZONE survivability. %s",
sqlerrors.EnforceHomeRegionFurtherInfo)
panic(err)
}
}
// buildScan builds a memo group for a ScanOp expression on the given table. If
// the ordinals list contains any VirtualComputed columns, a ProjectOp is built
// on top.
//
// The resulting scope and expression output the given table ordinals. If an
// ordinal is for a VirtualComputed column, the ordinals it depends on must also
// be in the list (in practice, this coincides with all "ordinary" table columns
// being in the list).
//
// If scanMutationCols is true, then include columns being added or dropped from
// the table. These are currently required by the execution engine as "fetch
// columns", when performing mutation DML statements (INSERT, UPDATE, UPSERT,
// DELETE).
//
// NOTE: Callers must take care that mutation columns (columns that are being
//
// added or dropped from the table) are only used when performing mutation
// DML statements (INSERT, UPDATE, UPSERT, DELETE). They cannot be used in
// any other way because they may not have been initialized yet by the
// backfiller!
//
// See Builder.buildStmt for a description of the remaining input and return
// values.
func (b *Builder) buildScan(
tabMeta *opt.TableMeta,
ordinals []int,
indexFlags *tree.IndexFlags,
locking lockingSpec,
inScope *scope,
disableNotVisibleIndex bool,
) (outScope *scope) {
if ordinals == nil {
panic(errors.AssertionFailedf("no ordinals"))
}
tab := tabMeta.Table
tabID := tabMeta.MetaID
if indexFlags != nil {
if indexFlags.IgnoreForeignKeys {
tabMeta.IgnoreForeignKeys = true
}
if indexFlags.IgnoreUniqueWithoutIndexKeys {
tabMeta.IgnoreUniqueWithoutIndexKeys = true
}
}
outScope = inScope.push()
// We collect VirtualComputed columns separately; these cannot be scanned,
// they can only be projected afterward.
var scanColIDs, virtualColIDs opt.ColSet
var virtualMutationColOrds intsets.Fast
outScope.cols = make([]scopeColumn, len(ordinals))
for i, ord := range ordinals {
col := tab.Column(ord)
colID := tabID.ColumnID(ord)
name := col.ColName()
kind := col.Kind()
mutation := kind == cat.WriteOnly || kind == cat.DeleteOnly
if col.IsVirtualComputed() {
virtualColIDs.Add(colID)
if mutation {
virtualMutationColOrds.Add(ord)
}
} else {
scanColIDs.Add(colID)
}
outScope.cols[i] = scopeColumn{
id: colID,
name: scopeColName(name),
table: tabMeta.Alias,
typ: col.DatumType(),
visibility: columnVisibility(col.Visibility()),
kind: kind,
mutation: mutation,
tableOrdinal: ord,
}
}
if tab.IsRefreshViewRequired() {
err := errors.WithHint(
pgerror.Newf(pgcode.ObjectNotInPrerequisiteState, "materialized view %q has not been populated", tab.Name()),
"use the REFRESH MATERIALIZED VIEW command.")
panic(err)
}
if tab.IsVirtualTable() {
if indexFlags != nil {
panic(pgerror.Newf(pgcode.Syntax,
"index flags not allowed with virtual tables"))
}
if locking.isSet() {
panic(pgerror.Newf(pgcode.Syntax,
"%s not allowed with virtual tables", locking.get().Strength))
}
private := memo.ScanPrivate{Table: tabID, Cols: scanColIDs}
outScope.expr = b.factory.ConstructScan(&private)
// Add the partial indexes after constructing the scan so we can use the
// logical properties of the scan to fully normalize the index predicates.
b.addPartialIndexPredicatesForTable(tabMeta, outScope.expr)
// Note: virtual tables should not be collected as view dependencies.
return outScope
}
// Scanning tables in databases that don't use the SURVIVE ZONE FAILURE option
// is disallowed when EnforceHomeRegion is true.
if b.evalCtx.SessionData().EnforceHomeRegion && statements.IsANSIDML(b.stmt) {
errorOnInvalidMultiregionDB(b.ctx, b.evalCtx, tabMeta)
// Populate the remote regions touched by the multiregion database used in
// this query. If a query dynamically errors out as having no home region,
// the query will be replanned with each of the remote regions,
// one-at-a-time, with AOST follower_read_timestamp(). If one of these
// retries doesn't error out, that region will be reported to the user as
// the query's home region.
if len(b.evalCtx.RemoteRegions) == 0 {
if regionsNames, ok := tabMeta.GetRegionsInDatabase(b.ctx, b.evalCtx.Planner); ok {
if gatewayRegion, ok := b.evalCtx.Locality.Find("region"); ok {
b.evalCtx.RemoteRegions = make(catpb.RegionNames, 0, len(regionsNames))
for _, regionName := range regionsNames {
if string(regionName) != gatewayRegion {
b.evalCtx.RemoteRegions = append(b.evalCtx.RemoteRegions, regionName)
}
}
}
}
}
}
private := memo.ScanPrivate{Table: tabID, Cols: scanColIDs}
if indexFlags != nil {
private.Flags.NoIndexJoin = indexFlags.NoIndexJoin
private.Flags.NoZigzagJoin = indexFlags.NoZigzagJoin
private.Flags.NoFullScan = indexFlags.NoFullScan
private.Flags.ForceInvertedIndex = indexFlags.ForceInvertedIndex
if indexFlags.Index != "" || indexFlags.IndexID != 0 {
idx := -1
for i := 0; i < tab.IndexCount(); i++ {
if tab.Index(i).Name() == tree.Name(indexFlags.Index) ||
tab.Index(i).ID() == cat.StableID(indexFlags.IndexID) {
idx = i
break
}
}
// Fallback to referencing @primary as the PRIMARY KEY.
// Note that indexes with "primary" as their name takes precedence above.
if idx == -1 && indexFlags.Index == tabledesc.LegacyPrimaryKeyIndexName {
idx = 0
}
if idx == -1 {
var err error
if indexFlags.Index != "" {
err = pgerror.Newf(pgcode.UndefinedObject,
"index %q not found", tree.ErrString(&indexFlags.Index))
} else {
err = pgerror.Newf(pgcode.UndefinedObject,
"index [%d] not found", indexFlags.IndexID)
}
panic(err)
}
private.Flags.ForceIndex = true
private.Flags.Index = idx
private.Flags.Direction = indexFlags.Direction
}
private.Flags.ForceZigzag = indexFlags.ForceZigzag
if len(indexFlags.ZigzagIndexes) > 0 {
private.Flags.ForceZigzag = true
for _, name := range indexFlags.ZigzagIndexes {
var found bool
for i := 0; i < tab.IndexCount(); i++ {
if tab.Index(i).Name() == tree.Name(name) {
if private.Flags.ZigzagIndexes.Contains(i) {
panic(pgerror.New(pgcode.DuplicateObject, "FORCE_ZIGZAG index duplicated"))
}
private.Flags.ZigzagIndexes.Add(i)
found = true
break
}
}
if !found {
panic(pgerror.Newf(pgcode.UndefinedObject, "index %q not found", tree.ErrString(&name)))
}
}
}
if len(indexFlags.ZigzagIndexIDs) > 0 {
private.Flags.ForceZigzag = true
for _, id := range indexFlags.ZigzagIndexIDs {
var found bool
for i := 0; i < tab.IndexCount(); i++ {
if tab.Index(i).ID() == cat.StableID(id) {
if private.Flags.ZigzagIndexes.Contains(i) {
panic(pgerror.New(pgcode.DuplicateObject, "FORCE_ZIGZAG index duplicated"))
}
private.Flags.ZigzagIndexes.Add(i)
found = true
break
}
}
if !found {
panic(pgerror.Newf(pgcode.UndefinedObject, "index [%d] not found", id))
}
}
}
}
if locking.isSet() {
private.Locking = locking.get()
if private.Locking.IsLocking() && b.shouldUseGuaranteedDurability() {
// Under weaker isolation levels we use fully-durable locks for SELECT FOR
// UPDATE statements, SELECT FOR SHARE statements, and constraint checks
// (e.g. FK checks), regardless of locking strength and wait policy.
// Unlike mutation statements, SELECT FOR UPDATE statements do not lay
// down intents, so we cannot rely on the durability of intents to
// guarantee exclusion until commit as we do for mutation statements. And
// unlike serializable isolation, weaker isolation levels do not perform
// read refreshing, so we cannot rely on read refreshing to guarantee
// exclusion.
//
// Under serializable isolation we only use fully-durable locks if
// enable_durable_locking_for_serializable is set. (Serializable isolation
// does not require locking for correctness, so by default we use
// best-effort locks for better performance.)
private.Locking.Durability = tree.LockDurabilityGuaranteed
}
if private.Locking.WaitPolicy == tree.LockWaitSkipLocked && tab.FamilyCount() > 1 {
// TODO(rytaft): We may be able to support this if enough columns are
// pruned that only a single family is scanned.
panic(pgerror.Newf(pgcode.FeatureNotSupported,
"SKIP LOCKED cannot be used for tables with multiple column families",
))
}
}
if b.evalCtx.AsOfSystemTime != nil && b.evalCtx.AsOfSystemTime.BoundedStaleness {
private.Flags.NoIndexJoin = true
private.Flags.NoZigzagJoin = true
}
private.Flags.DisableNotVisibleIndex = disableNotVisibleIndex
b.addCheckConstraintsForTable(tabMeta)
b.addComputedColsForTable(tabMeta, virtualMutationColOrds)
tabMeta.CacheIndexPartitionLocalities(b.ctx, b.evalCtx)
outScope.expr = b.factory.ConstructScan(&private)
// Add the partial indexes after constructing the scan so we can use the
// logical properties of the scan to fully normalize the index predicates.
b.addPartialIndexPredicatesForTable(tabMeta, outScope.expr)
if !virtualColIDs.Empty() {
// Project the expressions for the virtual columns (and pass through all
// scanned columns).
// TODO(radu): we don't currently support virtual columns depending on other
// virtual columns.
proj := make(memo.ProjectionsExpr, 0, virtualColIDs.Len())
virtualColIDs.ForEach(func(col opt.ColumnID) {
item := b.factory.ConstructProjectionsItem(tabMeta.ComputedCols[col], col)
if !item.ScalarProps().OuterCols.SubsetOf(scanColIDs) {
panic(errors.AssertionFailedf("scanned virtual column depends on non-scanned column"))
}
proj = append(proj, item)
})
outScope.expr = b.factory.ConstructProject(outScope.expr, proj, scanColIDs)
}
if b.trackSchemaDeps {
dep := opt.SchemaDep{DataSource: tab}
dep.ColumnIDToOrd = make(map[opt.ColumnID]int)
// We will track the ColumnID to Ord mapping so Ords can be added
// when a column is referenced.
for i, col := range outScope.cols {
dep.ColumnIDToOrd[col.id] = ordinals[i]
}
if private.Flags.ForceIndex {
dep.SpecificIndex = true
dep.Index = private.Flags.Index
}
b.schemaDeps = append(b.schemaDeps, dep)
}
return outScope
}
// addCheckConstraintsForTable extracts filters from the check constraints that
// apply to the table and adds them to the table metadata (see
// TableMeta.Constraints). To do this, the scalar expressions of the check
// constraints are built here.
//
// These expressions are used as "known truths" about table data; as such they
// can only contain immutable operators.
func (b *Builder) addCheckConstraintsForTable(tabMeta *opt.TableMeta) {
// Columns of a user defined type have a constraint to ensure
// enum values for that column belong to the UDT. We do not want to
// track view deps here, or else a view depending on a table with a
// column that is a UDT will result in a type dependency being added
// between the view and the UDT, even if the view does not use that column.
if b.trackSchemaDeps {
b.trackSchemaDeps = false
defer func() {
b.trackSchemaDeps = true
}()
}
tab := tabMeta.Table
// Check if we have any validated check constraints. Only validated
// constraints are known to hold on existing table data.
numChecks := tab.CheckCount()
chkIdx := 0
for ; chkIdx < numChecks; chkIdx++ {
if tab.Check(chkIdx).Validated() {
break
}
}
if chkIdx == numChecks {
return
}
// Create a scope that can be used for building the scalar expressions.
tableScope := b.allocScope()
tableScope.appendOrdinaryColumnsFromTable(tabMeta, &tabMeta.Alias)
// Synthesized CHECK expressions, e.g., for columns of ENUM types, may
// reference inaccessible columns. This can happen when the type of an
// indexed expression is an ENUM. We make these columns visible so that they
// can be resolved while building the CHECK expressions. This is safe to do
// for all CHECK expressions, not just synthesized ones, because
// user-created CHECK expressions will never reference inaccessible columns
// - this is enforced during CREATE TABLE and ALTER TABLE statements.
for i := range tableScope.cols {
tableScope.cols[i].visibility = columnVisibility(cat.Visible)
}
// Find the non-nullable table columns. Mutation columns can be NULL during
// backfill, so they should be excluded.
var notNullCols opt.ColSet
for i, n := 0, tab.ColumnCount(); i < n; i++ {
if col := tab.Column(i); !col.IsNullable() && !col.IsMutation() {
notNullCols.Add(tabMeta.MetaID.ColumnID(i))
}
}
var filters memo.FiltersExpr
// Skip to the first validated constraint we found above.
for ; chkIdx < numChecks; chkIdx++ {
checkConstraint := tab.Check(chkIdx)
// Only add validated check constraints to the table's metadata.
if !checkConstraint.Validated() {
continue
}
expr, err := parser.ParseExpr(checkConstraint.Constraint())
if err != nil {
panic(err)
}
texpr := tableScope.resolveAndRequireType(expr, types.Bool)
var condition opt.ScalarExpr
b.factory.FoldingControl().TemporarilyDisallowStableFolds(func() {
condition = b.buildScalar(texpr, tableScope, nil, nil, nil)
})
// Check constraints that are guaranteed to not evaluate to NULL
// are the only ones converted into filters. This is because a NULL
// constraint is interpreted as passing, whereas a NULL filter is not.
if memo.ExprIsNeverNull(condition, notNullCols) {
// Check if the expression contains non-immutable operators.
var sharedProps props.Shared
memo.BuildSharedProps(condition, &sharedProps, b.evalCtx)
if !sharedProps.VolatilitySet.HasStable() && !sharedProps.VolatilitySet.HasVolatile() {
filters = append(filters, b.factory.ConstructFiltersItem(condition))
}
}
}
if len(filters) > 0 {
tabMeta.SetConstraints(&filters)
}
}
// addComputedColsForTable finds all computed columns in the given table and
// caches them in the table metadata as scalar expressions. These expressions
// are used as "known truths" about table data. Any columns for which the
// expression contains non-immutable operators are omitted.
// `includeVirtualMutationColOrds` is the set of virtual computed column
// ordinals which are under mutation, but whose computed expressions should be
// built anyway. Normally `addComputedColsForTable` does not build expressions
// for columns under mutation.
func (b *Builder) addComputedColsForTable(
tabMeta *opt.TableMeta, includeVirtualMutationColOrds intsets.Fast,
) {
// We do not want to track view deps here, otherwise a view depending
// on a table with a computed column of a UDT will result in a
// type dependency being added between the view and the UDT,
// even if the view does not use that column.
if b.trackSchemaDeps {
b.trackSchemaDeps = false
defer func() {
b.trackSchemaDeps = true
}()
}
var tableScope *scope
tab := tabMeta.Table
for i, n := 0, tab.ColumnCount(); i < n; i++ {
tabCol := tab.Column(i)
if !tabCol.IsComputed() {
continue
}
if tabCol.IsMutation() && !includeVirtualMutationColOrds.Contains(i) {
// Mutation columns can be NULL during backfill, so they won't equal the
// computed column expression value (in general).
continue
}
expr, err := parser.ParseExpr(tabCol.ComputedExprStr())
if err != nil {
panic(err)
}
if tableScope == nil {
tableScope = b.allocScope()
tableScope.appendOrdinaryColumnsFromTable(tabMeta, &tabMeta.Alias)
}
colType := tabCol.DatumType()
if texpr := tableScope.resolveAndRequireType(expr, colType); texpr != nil {
colID := tabMeta.MetaID.ColumnID(i)
var scalar opt.ScalarExpr
b.factory.FoldingControl().TemporarilyDisallowStableFolds(func() {
scalar = b.buildScalar(texpr, tableScope, nil, nil, nil)
// Add an assignment cast if the types are not identical.
scalarType := scalar.DataType()
if !colType.Identical(scalarType) {
// Assert that an assignment cast is available from the
// expression type to the column type.
if !cast.ValidCast(scalarType, colType, cast.ContextAssignment) {
panic(sqlerrors.NewInvalidAssignmentCastError(
scalarType, colType, string(tabCol.ColName()),
))
}
// TODO(mgartner): This should be an assignment cast, but
// until #81698 is addressed, that could cause reads to
// error after adding a virtual computed column to a table.
scalar = b.factory.ConstructCast(scalar, colType)
}
})
// Check if the expression contains non-immutable operators.
var sharedProps props.Shared
memo.BuildSharedProps(scalar, &sharedProps, b.evalCtx)
if !sharedProps.VolatilitySet.HasStable() && !sharedProps.VolatilitySet.HasVolatile() {
tabMeta.AddComputedCol(colID, scalar, sharedProps.OuterCols)
}
}
}
}
func (b *Builder) buildSequenceSelect(
seq cat.Sequence, seqName *tree.TableName, inScope *scope,
) (outScope *scope) {
md := b.factory.Metadata()
outScope = inScope.push()
cols := make(opt.ColList, len(colinfo.SequenceSelectColumns))
for i, c := range colinfo.SequenceSelectColumns {
cols[i] = md.AddColumn(c.Name, c.Typ)
}
outScope.cols = make([]scopeColumn, 3)
for i, c := range cols {
col := md.ColumnMeta(c)
outScope.cols[i] = scopeColumn{
id: c,
name: scopeColName(tree.Name(col.Alias)),
table: *seqName,
typ: col.Type,
}
}
private := memo.SequenceSelectPrivate{
Sequence: md.AddSequence(seq),
Cols: cols,
}
outScope.expr = b.factory.ConstructSequenceSelect(&private)
if b.trackSchemaDeps {
b.schemaDeps = append(b.schemaDeps, opt.SchemaDep{DataSource: seq})