-
Notifications
You must be signed in to change notification settings - Fork 3.8k
/
Copy pathstatistics_builder.go
4731 lines (4203 loc) · 174 KB
/
statistics_builder.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 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 memo
import (
"math"
"reflect"
"github.com/cockroachdb/cockroach/pkg/geo/geoindex"
"github.com/cockroachdb/cockroach/pkg/sql/catalog/colinfo"
"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/constraint"
"github.com/cockroachdb/cockroach/pkg/sql/opt/props"
"github.com/cockroachdb/cockroach/pkg/sql/sem/eval"
"github.com/cockroachdb/cockroach/pkg/sql/sem/tree"
"github.com/cockroachdb/cockroach/pkg/sql/stats"
"github.com/cockroachdb/cockroach/pkg/sql/types"
"github.com/cockroachdb/cockroach/pkg/util/buildutil"
"github.com/cockroachdb/cockroach/pkg/util/json"
"github.com/cockroachdb/cockroach/pkg/util/log"
"github.com/cockroachdb/errors"
"github.com/cockroachdb/redact"
)
var statsAnnID = opt.NewTableAnnID()
const (
// This is the value used for inequality filters such as x < 1 in
// "Access Path Selection in a Relational Database Management System"
// by Pat Selinger et al.
unknownFilterSelectivity = 1.0 / 3.0
// TODO(rytaft): Add other selectivities for other types of predicates.
// This is an arbitrary row count used in the absence of any real statistics.
unknownRowCount = 1000
// UnknownDistinctCountRatio is the ratio of distinct column values to number
// of rows, which is used in the absence of any real statistics for non-key
// columns. TODO(rytaft): See if there is an industry standard value for
// this.
UnknownDistinctCountRatio = 0.1
// UnknownNullCountRatio is the ratio of null column values to number of rows
// for nullable columns, which is used in the absence of any real statistics.
UnknownNullCountRatio = 0.01
// UnknownAvgRowSize is the average size of a row in bytes, which is used in
// the absence of any real statistics.
UnknownAvgRowSize = 8
// Use a small row count for generator functions; this allows use of lookup
// join in cases like using json_array_elements with a small constant array.
unknownGeneratorRowCount = 10
// Since the generator row count is so small, we need a larger distinct count
// ratio for generator functions.
unknownGeneratorDistinctCountRatio = 0.7
// When subtracting floating point numbers, avoid precision errors by making
// sure the result is greater than or equal to epsilon.
epsilon = 1e-10
// This is the minimum cardinality an expression should have in order to make
// it worth adding the overhead of using a histogram.
minCardinalityForHistogram = 100
// This is the default selectivity estimated for inverted joins until we can
// get better statistics on inverted indexes.
unknownInvertedJoinSelectivity = 1.0 / 100.0
// multiColWeight is the weight to assign the selectivity calculation using
// multi-column statistics versus the calculation using single-column
// statistics. See the comment above selectivityFromMultiColDistinctCounts for
// details.
multiColWeight = 9.0 / 10.0
// defaultColSize is the default size of a column in bytes. This is used
// when the table statistics have an avgSize of 0 for a given column.
defaultColSize = 4.0
// maxValuesForFullHistogramFromCheckConstraint is the maximum number of
// values from the spans a check constraint is allowed to have in order to build
// a histogram from it.
maxValuesForFullHistogramFromCheckConstraint = tabledesc.MaxBucketAllowed
)
// statisticsBuilder is responsible for building the statistics that are
// used by the coster to estimate the cost of expressions.
//
// Background
// ----------
//
// Conceptually, there are two kinds of statistics: table statistics and
// relational expression statistics.
//
// 1. Table statistics
//
// Table statistics are stats derived from the underlying data in the
// database. These stats are calculated either automatically or on-demand for
// each table, and include the number of rows in the table as well as
// statistics about selected individual columns or sets of columns. The column
// statistics include the number of null values, the number of distinct values,
// and optionally, a histogram of the data distribution (only applicable for
// single columns, not sets of columns). These stats are only collected
// periodically to avoid overloading the database, so they may be stale. They
// are currently persisted in the system.table_statistics table (see sql/stats
// for details). Inside the optimizer, they are cached in a props.Statistics
// object as a table annotation in opt.Metadata.
//
// 2. Relational expression statistics
//
// Relational expression statistics are derived from table statistics, and are
// only valid for a particular memo group. They are used to estimate how the
// underlying table statistics change as different relational operators are
// applied. The same types of statistics are stored for relational expressions
// as for tables (row count, null count, distinct count, etc.). Inside the
// optimizer, they are stored in a props.Statistics object in the logical
// properties of the relational expression's memo group.
//
// For example, here is a query plan with corresponding estimated statistics at
// each level:
//
// Query: SELECT y FROM a WHERE x=1
//
// Plan: Project y Row Count: 10, Distinct(x): 1
// |
// Select x=1 Row Count: 10, Distinct(x): 1
// |
// Scan a Row Count: 100, Distinct(x): 10
//
// The statistics for the Scan operator were presumably retrieved from the
// underlying table statistics cached in the metadata. The statistics for
// the Select operator are determined as follows: Since the predicate x=1
// reduces the number of distinct values of x down to 1, and the previous
// distinct count of x was 10, the selectivity of the predicate is 1/10.
// Thus, the estimated number of output rows is 1/10 * 100 = 10. Finally, the
// Project operator passes through the statistics from its child expression.
//
// Statistics for expressions high up in the query tree tend to be quite
// inaccurate since the estimation errors from lower expressions are
// compounded. Still, statistics are useful throughout the query tree to help
// the optimizer choose between multiple alternative, logically equivalent
// plans.
//
// How statisticsBuilder works
// ---------------------------
//
// statisticsBuilder is responsible for building the second type of statistics,
// relational expression statistics. It builds the statistics lazily, and only
// calculates column statistics if needed to estimate the row count of an
// expression (currently, the row count is the only statistic used by the
// coster).
//
// Every relational operator has a buildXXX and a colStatXXX function. For
// example, Scan has buildScan and colStatScan. buildScan is called when the
// logical properties of a Scan expression are built. The goal of each buildXXX
// function is to calculate the number of rows output by the expression so that
// its cost can be estimated by the coster.
//
// In order to determine the row count, column statistics may be required for a
// subset of the columns of the expression. Column statistics are calculated
// recursively from the child expression(s) via calls to the colStatFromInput
// function. colStatFromInput finds the child expression that might contain the
// requested stats, and calls colStat on the child. colStat checks if the
// requested stats are already cached for the child expression, and if not,
// calls colStatXXX (where the XXX corresponds to the operator of the child
// expression). The child expression may need to calculate column statistics
// from its children, and if so, it makes another recursive call to
// colStatFromInput.
//
// The "base case" for colStatFromInput is a Scan, where the "input" is the raw
// table itself; the table statistics are retrieved from the metadata (the
// metadata may in turn need to fetch the stats from the database if they are
// not already cached). If a particular table statistic is not available, a
// best-effort guess is made (see colStatLeaf for details).
//
// To better understand how the statisticsBuilder works, let us consider this
// simple query, which consists of a scan followed by an aggregation:
//
// SELECT count(*), x, y FROM t GROUP BY x, y
//
// The statistics for the scan of t will be calculated first, since logical
// properties are built bottom-up. The estimated row count is retrieved from
// the table statistics in the metadata, so no column statistics are needed.
//
// The statistics for the group by operator are calculated second. The row
// count for GROUP BY can be determined by the distinct count of its grouping
// columns. Therefore, the statisticsBuilder recursively updates the statistics
// for the scan operator to include column stats for x and y, and then uses
// these column stats to update the statistics for GROUP BY.
//
// At each stage where column statistics are requested, the statisticsBuilder
// makes a call to colStatFromChild, which in turn calls colStat on the child
// to retrieve the cached statistics or calculate them recursively. Assuming
// that no statistics are cached, this is the order of function calls for the
// above example (somewhat simplified):
//
// +-------------+ +--------------+
// 1. | buildScan t | 2. | buildGroupBy |
// +-------------+ +--------------+
// | |
// +-----------------------+ +-------------------------+
// | makeTableStatistics t | | colStatFromChild (x, y) |
// +-----------------------+ +-------------------------+
// |
// +--------------------+
// | colStatScan (x, y) |
// +--------------------+
// |
// +---------------------+
// | colStatTable (x, y) |
// +---------------------+
// |
// +--------------------+
// | colStatLeaf (x, y) |
// +--------------------+
//
// See props/statistics.go for more details.
type statisticsBuilder struct {
evalCtx *eval.Context
md *opt.Metadata
}
func (sb *statisticsBuilder) init(evalCtx *eval.Context, md *opt.Metadata) {
// This initialization pattern ensures that fields are not unwittingly
// reused. Field reuse must be explicit.
*sb = statisticsBuilder{
evalCtx: evalCtx,
md: md,
}
}
func (sb *statisticsBuilder) clear() {
sb.evalCtx = nil
sb.md = nil
}
// colStatFromChild retrieves a column statistic from a specific child of the
// given expression.
func (sb *statisticsBuilder) colStatFromChild(
colSet opt.ColSet, e RelExpr, childIdx int,
) *props.ColumnStatistic {
// Helper function to return the column statistic if the output columns of
// the child with the given index intersect colSet.
child := e.Child(childIdx).(RelExpr)
childProps := child.Relational()
if !colSet.SubsetOf(childProps.OutputCols) {
colSet = colSet.Intersection(childProps.OutputCols)
if colSet.Empty() {
// All the columns in colSet are outer columns; therefore, we can treat
// them as a constant.
return &props.ColumnStatistic{Cols: colSet, DistinctCount: 1}
}
}
return sb.colStat(colSet, child)
}
// statsFromChild retrieves the main statistics struct from a specific child
// of the given expression.
func (sb *statisticsBuilder) statsFromChild(e RelExpr, childIdx int) *props.Statistics {
return &e.Child(childIdx).(RelExpr).Relational().Stats
}
// availabilityFromInput determines the availability of the underlying table
// statistics from the children of the expression.
func (sb *statisticsBuilder) availabilityFromInput(e RelExpr) bool {
switch t := e.(type) {
case *ScanExpr:
return sb.makeTableStatistics(t.Table).Available
case *LookupJoinExpr:
ensureLookupJoinInputProps(t, sb)
return t.lookupProps.Stats.Available && t.Input.Relational().Stats.Available
case *InvertedJoinExpr:
ensureInvertedJoinInputProps(t, sb)
return t.lookupProps.Stats.Available && t.Input.Relational().Stats.Available
case *ZigzagJoinExpr:
ensureZigzagJoinInputProps(t, sb)
return t.leftProps.Stats.Available
}
available := true
for i, n := 0, e.ChildCount(); i < n; i++ {
if child, ok := e.Child(i).(RelExpr); ok {
available = available && child.Relational().Stats.Available
}
}
return available
}
// colStatFromInput retrieves a column statistic from the input(s) of a Scan,
// Select, or Join. The input to the Scan is the "raw" table.
//
// colStatFromInput also retrieves a pointer to the full statistics from the
// relevant input.
func (sb *statisticsBuilder) colStatFromInput(
colSet opt.ColSet, e RelExpr,
) (*props.ColumnStatistic, *props.Statistics) {
var lookupJoin *LookupJoinExpr
var invertedJoin *InvertedJoinExpr
var zigzagJoin *ZigzagJoinExpr
switch t := e.(type) {
case *ScanExpr:
return sb.colStatTable(t.Table, colSet), sb.makeTableStatistics(t.Table)
case *SelectExpr, *InvertedFilterExpr:
return sb.colStatFromChild(colSet, t, 0 /* childIdx */), sb.statsFromChild(e, 0 /* childIdx */)
case *LookupJoinExpr:
lookupJoin = t
ensureLookupJoinInputProps(lookupJoin, sb)
case *InvertedJoinExpr:
invertedJoin = t
ensureInvertedJoinInputProps(invertedJoin, sb)
case *ZigzagJoinExpr:
zigzagJoin = t
ensureZigzagJoinInputProps(zigzagJoin, sb)
}
if lookupJoin != nil || invertedJoin != nil || zigzagJoin != nil ||
opt.IsJoinOp(e) || e.Op() == opt.MergeJoinOp {
var leftProps *props.Relational
if zigzagJoin != nil {
leftProps = &zigzagJoin.leftProps
} else {
leftProps = e.Child(0).(RelExpr).Relational()
}
intersectsLeft := leftProps.OutputCols.Intersects(colSet)
var intersectsRight bool
if lookupJoin != nil {
intersectsRight = lookupJoin.lookupProps.OutputCols.Intersects(colSet)
} else if invertedJoin != nil {
intersectsRight = invertedJoin.lookupProps.OutputCols.Intersects(colSet)
} else if zigzagJoin != nil {
intersectsRight = zigzagJoin.rightProps.OutputCols.Intersects(colSet)
} else {
intersectsRight = e.Child(1).(RelExpr).Relational().OutputCols.Intersects(colSet)
}
// It's possible that colSet intersects both left and right if we have a
// lookup join that was converted from an index join, so check the left
// side first.
if intersectsLeft {
if zigzagJoin != nil {
return sb.colStatTable(zigzagJoin.LeftTable, colSet),
sb.makeTableStatistics(zigzagJoin.LeftTable)
}
return sb.colStatFromChild(colSet, e, 0 /* childIdx */),
sb.statsFromChild(e, 0 /* childIdx */)
}
if intersectsRight {
if lookupJoin != nil {
return sb.colStatTable(lookupJoin.Table, colSet),
sb.makeTableStatistics(lookupJoin.Table)
}
if invertedJoin != nil {
// TODO(rytaft): use inverted index stats when available.
return sb.colStatTable(invertedJoin.Table, colSet),
sb.makeTableStatistics(invertedJoin.Table)
}
if zigzagJoin != nil {
return sb.colStatTable(zigzagJoin.RightTable, colSet),
sb.makeTableStatistics(zigzagJoin.RightTable)
}
return sb.colStatFromChild(colSet, e, 1 /* childIdx */),
sb.statsFromChild(e, 1 /* childIdx */)
}
// All columns in colSet are outer columns; therefore, we can treat them
// as a constant. Return table stats from the left side.
if zigzagJoin != nil {
return &props.ColumnStatistic{Cols: colSet, DistinctCount: 1},
sb.makeTableStatistics(zigzagJoin.LeftTable)
}
return &props.ColumnStatistic{Cols: colSet, DistinctCount: 1}, sb.statsFromChild(e, 0 /* childIdx */)
}
panic(errors.AssertionFailedf("unsupported operator type %s", redact.Safe(e.Op())))
}
// colStat gets a column statistic for the given set of columns if it exists.
// If the column statistic is not available in the current expression,
// colStat recursively tries to find it in the children of the expression,
// lazily populating s.ColStats with the statistic as it gets passed up the
// expression tree.
func (sb *statisticsBuilder) colStat(colSet opt.ColSet, e RelExpr) *props.ColumnStatistic {
if colSet.Empty() {
panic(errors.AssertionFailedf("column statistics cannot be determined for empty column set"))
}
// Check if the requested column statistic is already cached.
if stat, ok := e.Relational().Stats.ColStats.Lookup(colSet); ok {
return stat
}
// Only calculate statistics on the normalized expression in a memo group.
e = e.FirstExpr()
// The statistic was not found in the cache, so calculate it based on the
// type of expression.
switch e.Op() {
case opt.ScanOp:
return sb.colStatScan(colSet, e.(*ScanExpr))
case opt.SelectOp:
return sb.colStatSelect(colSet, e.(*SelectExpr))
case opt.ProjectOp:
return sb.colStatProject(colSet, e.(*ProjectExpr))
case opt.InvertedFilterOp:
return sb.colStatInvertedFilter(colSet, e.(*InvertedFilterExpr))
case opt.ValuesOp:
return sb.colStatValues(colSet, e.(*ValuesExpr))
case opt.LiteralValuesOp:
return sb.colStatLiteralValues(colSet, e.(*LiteralValuesExpr))
case opt.InnerJoinOp, opt.LeftJoinOp, opt.RightJoinOp, opt.FullJoinOp,
opt.SemiJoinOp, opt.AntiJoinOp, opt.InnerJoinApplyOp, opt.LeftJoinApplyOp,
opt.SemiJoinApplyOp, opt.AntiJoinApplyOp, opt.MergeJoinOp, opt.LookupJoinOp,
opt.InvertedJoinOp, opt.ZigzagJoinOp:
return sb.colStatJoin(colSet, e)
case opt.IndexJoinOp:
return sb.colStatIndexJoin(colSet, e.(*IndexJoinExpr))
case opt.UnionOp, opt.IntersectOp, opt.ExceptOp,
opt.UnionAllOp, opt.IntersectAllOp, opt.ExceptAllOp:
return sb.colStatSetNode(colSet, e)
case opt.GroupByOp, opt.ScalarGroupByOp, opt.DistinctOnOp, opt.EnsureDistinctOnOp,
opt.UpsertDistinctOnOp, opt.EnsureUpsertDistinctOnOp:
return sb.colStatGroupBy(colSet, e)
case opt.LimitOp:
return sb.colStatLimit(colSet, e.(*LimitExpr))
case opt.OffsetOp:
return sb.colStatOffset(colSet, e.(*OffsetExpr))
case opt.Max1RowOp:
return sb.colStatMax1Row(colSet, e.(*Max1RowExpr))
case opt.OrdinalityOp:
return sb.colStatOrdinality(colSet, e.(*OrdinalityExpr))
case opt.WindowOp:
return sb.colStatWindow(colSet, e.(*WindowExpr))
case opt.ProjectSetOp:
return sb.colStatProjectSet(colSet, e.(*ProjectSetExpr))
case opt.WithScanOp:
return sb.colStatWithScan(colSet, e.(*WithScanExpr))
case opt.InsertOp, opt.UpdateOp, opt.UpsertOp, opt.DeleteOp:
return sb.colStatMutation(colSet, e)
case opt.SequenceSelectOp:
return sb.colStatSequenceSelect(colSet, e.(*SequenceSelectExpr))
case opt.ExplainOp, opt.ShowTraceForSessionOp,
opt.OpaqueRelOp, opt.OpaqueMutationOp, opt.OpaqueDDLOp, opt.RecursiveCTEOp:
return sb.colStatUnknown(colSet, e.Relational())
case opt.WithOp:
return sb.colStat(colSet, e.Child(1).(RelExpr))
case opt.FakeRelOp:
rel := e.Relational()
return sb.colStatLeaf(colSet, &rel.Stats, &rel.FuncDeps, rel.NotNullCols)
}
panic(errors.AssertionFailedf("unrecognized relational expression type: %v", redact.Safe(e.Op())))
}
// colStatLeaf creates a column statistic for a given column set (if it doesn't
// already exist in s), by deriving the statistic from the general statistics.
// Used when there is no child expression to retrieve statistics from, typically
// with the Statistics derived for a table.
func (sb *statisticsBuilder) colStatLeaf(
colSet opt.ColSet, s *props.Statistics, fd *props.FuncDepSet, notNullCols opt.ColSet,
) *props.ColumnStatistic {
// Ensure that the requested column statistic is in the cache.
colStat, added := s.ColStats.Add(colSet)
if !added {
// Already in the cache.
return colStat
}
// Build single-column stats from non-null check constraints, if they exist.
if colSet.Len() == 1 {
if ok := sb.buildStatsFromCheckConstraints(colStat, s); ok {
return colStat
}
}
// If some of the columns are a lax key, the distinct count equals the row
// count. The null count is 0 if any of these columns are not nullable,
// otherwise copy the null count from the nullable columns in colSet.
if fd.ColsAreLaxKey(colSet) {
if colSet.Intersects(notNullCols) {
colStat.NullCount = 0
} else {
nullableCols := colSet.Difference(notNullCols)
if nullableCols.Equals(colSet) {
// No column statistics on this colSet - use the unknown
// null count ratio.
colStat.NullCount = s.RowCount * UnknownNullCountRatio
} else {
colStatLeaf := sb.colStatLeaf(nullableCols, s, fd, notNullCols)
// Fetch the colStat again since it may now have a different address.
colStat, _ = s.ColStats.Lookup(colSet)
colStat.NullCount = colStatLeaf.NullCount
}
}
// Only one of the null values counts towards the distinct count.
colStat.DistinctCount = s.RowCount - max(colStat.NullCount-1, 0)
return colStat
}
if colSet.Len() == 1 {
// There is only one column, and it was not found in the cache above, so we
// do not have statistics available for it.
col, _ := colSet.Next(0)
colStat.DistinctCount = UnknownDistinctCountRatio * s.RowCount
colStat.NullCount = UnknownNullCountRatio * s.RowCount
if notNullCols.Contains(col) {
colStat.NullCount = 0
}
// Some types (e.g., bool and enum) have a known maximum number of distinct
// values.
maxDistinct, ok := distinctCountFromType(sb.md, sb.md.ColumnMeta(col).Type)
if ok {
if colStat.NullCount > 0 {
// Add one for the null value.
maxDistinct++
}
colStat.DistinctCount = min(colStat.DistinctCount, float64(maxDistinct))
}
} else {
distinctCount := 1.0
nullCount := s.RowCount
colSet.ForEach(func(i opt.ColumnID) {
colStatLeaf := sb.colStatLeaf(opt.MakeColSet(i), s, fd, notNullCols)
distinctCount *= colStatLeaf.DistinctCount
// Multiply by the expected chance of collisions with nulls already
// collected.
nullCount *= colStatLeaf.NullCount / s.RowCount
})
// Fetch the colStat again since it may now have a different address.
colStat, _ = s.ColStats.Lookup(colSet)
colStat.DistinctCount = min(distinctCount, s.RowCount)
colStat.NullCount = min(nullCount, s.RowCount)
}
return colStat
}
// +-------+
// | Table |
// +-------+
// makeTableStatistics returns the available statistics for the given table.
// Statistics are derived lazily and are cached in the metadata, since they may
// be accessed multiple times during query optimization. For more details, see
// props.Statistics.
func (sb *statisticsBuilder) makeTableStatistics(tabID opt.TableID) *props.Statistics {
stats, ok := sb.md.TableAnnotation(tabID, statsAnnID).(*props.Statistics)
if ok {
// Already made.
return stats
}
tab := sb.md.Table(tabID)
// Create a mapping from table column ordinals to inverted index column
// ordinals. This allows us to do a fast lookup while iterating over all
// stats from a statistic's column to any associated inverted columns.
// TODO(mgartner): It might be simpler to iterate over all the table columns
// looking for inverted columns.
invertedIndexCols := make(map[int]invertedIndexColInfo)
for indexI, indexN := 0, tab.IndexCount(); indexI < indexN; indexI++ {
index := tab.Index(indexI)
if !index.IsInverted() {
continue
}
col := index.InvertedColumn()
srcOrd := col.InvertedSourceColumnOrdinal()
info := invertedIndexCols[srcOrd]
info.invIdxColOrds = append(info.invIdxColOrds, col.Ordinal())
invertedIndexCols[srcOrd] = info
}
// Make now and annotate the metadata table with it for next time.
stats = &props.Statistics{}
// Find the most recent statistic. (Stats are ordered with most recent first.)
var first int
if !sb.evalCtx.SessionData().OptimizerUseForecasts {
for first < tab.StatisticCount() && tab.Statistic(first).IsForecast() {
first++
}
}
if first >= tab.StatisticCount() {
// No statistics.
stats.Available = false
stats.RowCount = unknownRowCount
} else {
// Use the RowCount from the most recent statistic.
stats.Available = true
stats.RowCount = float64(tab.Statistic(first).RowCount())
// Make sure the row count is at least 1. The stats may be stale, and we
// can end up with weird and inefficient plans if we estimate 0 rows.
stats.RowCount = max(stats.RowCount, 1)
// Add all the column statistics, using the most recent statistic for each
// column set. Stats are ordered with most recent first.
for i := first; i < tab.StatisticCount(); i++ {
stat := tab.Statistic(i)
if stat.IsForecast() && !sb.evalCtx.SessionData().OptimizerUseForecasts {
continue
}
if stat.ColumnCount() > 1 && !sb.evalCtx.SessionData().OptimizerUseMultiColStats {
continue
}
var cols opt.ColSet
var colOrd int
for i := 0; i < stat.ColumnCount(); i++ {
colOrd = stat.ColumnOrdinal(i)
cols.Add(tabID.ColumnID(colOrd))
}
// We currently only use average column sizes of single column
// statistics, so we can ignore multi-column average sizes.
if stat.ColumnCount() == 1 && stat.AvgSize() != 0 {
if stats.AvgColSizes == nil {
stats.AvgColSizes = make([]uint64, tab.ColumnCount())
}
stats.AvgColSizes[colOrd] = stat.AvgSize()
}
needHistogram := cols.Len() == 1 && stat.Histogram() != nil &&
sb.evalCtx.SessionData().OptimizerUseHistograms
seenInvertedStat := false
invertedStatistic := false
var invertedColOrds []int
if needHistogram {
info := invertedIndexCols[stat.ColumnOrdinal(0)]
invertedColOrds = info.invIdxColOrds
seenInvertedStat = info.foundInvertedHistogram
// If some of the columns are inverted and the statistics is of type
// BYTES, it means we have an inverted statistic on this column set.
invertedStatistic = len(invertedColOrds) > 0 && stat.HistogramType().Family() == types.BytesFamily
}
colStat, ok := stats.ColStats.Add(cols)
if ok || (colStat.Histogram == nil && !invertedStatistic && seenInvertedStat) {
// Set the statistic if either:
// 1. We have no statistic for the current colset at all
// 2. All of the following conditions hold:
// a. The previously found statistic for the colset has no histogram
// b. the current statistic is not inverted
// c. the previously found statistic for this colset was inverted
// If these conditions hold, it means that the previous histogram
// we found for the current colset was derived from an inverted
// histogram, and therefore the existing forward statistic doesn't have
// a histogram at all, and the new statistic we just found has a
// non-inverted histogram that we should be using instead.
colStat.DistinctCount = float64(stat.DistinctCount())
colStat.NullCount = float64(stat.NullCount())
if needHistogram && !invertedStatistic {
// A statistic is inverted if the column is invertible and its
// histogram contains buckets of types BYTES.
// NOTE: this leaves an ambiguity which would surface if we ever
// permitted an inverted index on BYTES-type columns. A deeper fix
// is tracked here: https://github.com/cockroachdb/cockroach/issues/50655
col := cols.SingleColumn()
colStat.Histogram = &props.Histogram{}
colStat.Histogram.Init(sb.evalCtx, col, stat.Histogram())
}
// Make sure the distinct count is at least 1, for the same reason as
// the row count above.
colStat.DistinctCount = max(colStat.DistinctCount, 1)
// Make sure the values are consistent in case some of the column stats
// were added at different times (and therefore have a different row
// count).
sb.finalizeFromRowCountAndDistinctCounts(colStat, stats)
}
// Add inverted histograms if necessary.
if needHistogram && invertedStatistic {
// Record the fact that we are adding an inverted statistic to this
// column set.
info := invertedIndexCols[stat.ColumnOrdinal(0)]
info.foundInvertedHistogram = true
invertedIndexCols[stat.ColumnOrdinal(0)] = info
for _, invertedColOrd := range invertedColOrds {
invCol := tabID.ColumnID(invertedColOrd)
invCols := opt.MakeColSet(invCol)
if invColStat, ok := stats.ColStats.Add(invCols); ok {
invColStat.Histogram = &props.Histogram{}
invColStat.Histogram.Init(sb.evalCtx, invCol, stat.Histogram())
// Set inverted entry counts from the histogram. Make sure the
// distinct count is at least 1, for the same reason as the row
// count above.
invColStat.DistinctCount = max(invColStat.Histogram.DistinctValuesCount(), 1)
// Inverted indexes don't have nulls.
invColStat.NullCount = 0
if stat.AvgSize() != 0 {
if stats.AvgColSizes == nil {
stats.AvgColSizes = make([]uint64, tab.ColumnCount())
}
stats.AvgColSizes[invertedColOrd] = stat.AvgSize()
}
}
}
}
}
}
sb.md.SetTableAnnotation(tabID, statsAnnID, stats)
return stats
}
// invertedIndexColInfo is used to store information about an inverted column.
type invertedIndexColInfo struct {
// invIdxColOrds is the list of inverted index column ordinals for a given
// inverted column.
invIdxColOrds []int
// foundInvertedHistogram is set to true if we've found an inverted histogram
// for a given inverted column.
foundInvertedHistogram bool
}
func (sb *statisticsBuilder) colStatTable(
tabID opt.TableID, colSet opt.ColSet,
) *props.ColumnStatistic {
tableStats := sb.makeTableStatistics(tabID)
tableFD := MakeTableFuncDep(sb.md, tabID)
tableNotNullCols := makeTableNotNullCols(sb.md, tabID)
return sb.colStatLeaf(colSet, tableStats, tableFD, tableNotNullCols)
}
func (sb *statisticsBuilder) colAvgSize(tabID opt.TableID, col opt.ColumnID) uint64 {
tableStats := sb.makeTableStatistics(tabID)
ord := tabID.ColumnOrdinal(col)
if ord >= len(tableStats.AvgColSizes) {
return defaultColSize
}
if avgSize := tableStats.AvgColSizes[ord]; avgSize > 0 {
return avgSize
}
return defaultColSize
}
// +------+
// | Scan |
// +------+
func (sb *statisticsBuilder) buildScan(scan *ScanExpr, relProps *props.Relational) {
s := &relProps.Stats
if zeroCardinality := s.Init(relProps); zeroCardinality {
// Short cut if cardinality is 0.
return
}
s.Available = sb.availabilityFromInput(scan)
inputStats := sb.makeTableStatistics(scan.Table)
s.RowCount = inputStats.RowCount
pred := scan.PartialIndexPredicate(sb.md)
// If the constraints and pred are nil, then this scan is an unconstrained
// scan on a non-partial index. The stats of the scan are the same as the
// underlying table stats.
if scan.Constraint == nil && scan.InvertedConstraint == nil && pred == nil {
sb.finalizeFromCardinality(relProps)
return
}
// If the constraints are nil but pred is not, then this scan is an
// unconstrained scan over a partial index. The selectivity of the partial
// index predicate expression must be applied to the underlying table stats.
if scan.Constraint == nil && scan.InvertedConstraint == nil {
notNullCols := relProps.NotNullCols.Copy()
// Add any not-null columns from the predicate constraints.
for i := range pred {
if c := pred[i].ScalarProps().Constraints; c != nil {
notNullCols.UnionWith(c.ExtractNotNullCols(sb.evalCtx))
}
}
sb.filterRelExpr(pred, scan, notNullCols, relProps, s, MakeTableFuncDep(sb.md, scan.Table))
sb.finalizeFromCardinality(relProps)
return
}
// If the constraint is nil or it has a single span, apply the constraint
// selectivity, the inverted constraint selectivity, and the partial index
// predicate (if they exist) to the underlying table stats.
if scan.Constraint == nil || scan.Constraint.Spans.Count() < 2 {
sb.constrainScan(scan, scan.Constraint, pred, relProps, s)
sb.finalizeFromCardinality(relProps)
return
}
// Otherwise, there are multiple spans in this constraint. To calculate the
// row count and selectivity, split the spans up and apply each one
// separately, then union the result. This is important for correctly
// handling a constraint such as:
//
// /a/b: [/5 - /5] [/NULL/5 - /NULL/5]
//
// If we didn't split the spans, the selectivity of column b would be
// completely ignored, and the calculated row count would be too high.
var spanStats, spanStatsUnion props.Statistics
var c constraint.Constraint
keyCtx := constraint.KeyContext{EvalCtx: sb.evalCtx, Columns: scan.Constraint.Columns}
// Make a copy of the stats so we don't modify the original.
spanStatsUnion.CopyFrom(s)
// Get the stats for each span and union them together.
c.InitSingleSpan(&keyCtx, scan.Constraint.Spans.Get(0))
sb.constrainScan(scan, &c, pred, relProps, &spanStatsUnion)
for i, n := 1, scan.Constraint.Spans.Count(); i < n; i++ {
spanStats.CopyFrom(s)
c.InitSingleSpan(&keyCtx, scan.Constraint.Spans.Get(i))
sb.constrainScan(scan, &c, pred, relProps, &spanStats)
spanStatsUnion.UnionWith(&spanStats)
}
// Now that we have the correct row count, use the combined spans and the
// partial index predicate (if it exists) to get the correct column stats.
sb.constrainScan(scan, scan.Constraint, pred, relProps, s)
// Copy in the row count and selectivity that were calculated above, if
// less than the values calculated from the combined spans.
//
// We must take the minimum in case we used unknownFilterSelectivity for
// some of the spans. For example, if no histogram is available for the
// constraint /1: [/'a' - /'b'] [/'c' - /'d'] [/'e' - /'f'], we would
// calculate selectivity = 1/9 + 1/9 + 1/9 = 1/3 in spanStatsUnion, which
// is too high. Instead, we should use the value calculated from the
// combined spans, which in this case is simply 1/9.
s.Selectivity = props.MinSelectivity(s.Selectivity, spanStatsUnion.Selectivity)
s.RowCount = min(s.RowCount, spanStatsUnion.RowCount)
sb.finalizeFromCardinality(relProps)
}
// constrainScan is called from buildScan to calculate the stats for the scan
// based on the given constraints and partial index predicate.
//
// The constraint and invertedConstraint arguments are both non-nil only when a
// multi-column inverted index is scanned. In this case, the constraint must
// have only single-key spans.
//
// The pred argument is the predicate expression of the partial index that the
// scan operates on. If it is not a partial index, pred should be nil.
func (sb *statisticsBuilder) constrainScan(
scan *ScanExpr,
constraint *constraint.Constraint,
pred FiltersExpr,
relProps *props.Relational,
s *props.Statistics,
) {
var numUnappliedConjuncts float64
var constrainedCols, histCols opt.ColSet
idx := sb.md.Table(scan.Table).Index(scan.Index)
// Calculate distinct counts and histograms for inverted constrained columns
// -------------------------------------------------------------------------
if scan.InvertedConstraint != nil {
// The constrained column is the inverted column in the inverted index.
// Using scan.Cols here would also include the PK, which we don't want.
invertedConstrainedCol := scan.Table.ColumnID(idx.InvertedColumn().Ordinal())
constrainedCols.Add(invertedConstrainedCol)
if sb.shouldUseHistogram(relProps) {
// TODO(mjibson): set distinctCount to something correct. Max is
// fine for now because ensureColStat takes the minimum of the
// passed value and colSet's distinct count.
const distinctCount = math.MaxFloat64
colSet := opt.MakeColSet(invertedConstrainedCol)
sb.ensureColStat(colSet, distinctCount, scan, s)
inputStat, _ := sb.colStatFromInput(colSet, scan)
if inputHist := inputStat.Histogram; inputHist != nil {
// If we have a histogram, set the row count to its total, unfiltered
// count. This is needed because s.RowCount is currently the row count
// of the table, but should instead reflect the number of inverted index
// entries. (Make sure the row count is at least 1. The stats may be
// stale, and we can end up with weird and inefficient plans if we
// estimate 0 rows.)
s.RowCount = max(inputHist.ValuesCount(), 1)
if colStat, ok := s.ColStats.Lookup(colSet); ok {
colStat.Histogram = inputHist.InvertedFilter(scan.InvertedConstraint)
histCols.Add(invertedConstrainedCol)
sb.updateDistinctCountFromHistogram(colStat, inputStat.DistinctCount)
}
} else {
// Just assume a single closed span such as ["\xfd", "\xfe").
// This corresponds to two "conjuncts" as defined in
// numConjunctsInConstraint.
numUnappliedConjuncts += 2
}
} else {
// Assume a single closed span.
numUnappliedConjuncts += 2
}
}
// Calculate distinct counts and histograms for constrained columns
// ----------------------------------------------------------------
if constraint != nil {
constrainedColsLocal, histColsLocal := sb.applyIndexConstraint(constraint, scan, relProps, s)
constrainedCols.UnionWith(constrainedColsLocal)
histCols.UnionWith(histColsLocal)
}
// Calculate distinct counts and histograms for the partial index predicate
// ------------------------------------------------------------------------
if pred != nil {
predUnappliedConjucts, predConstrainedCols, predHistCols :=
sb.applyFilters(pred, scan, relProps, false /* skipOrTermAccounting */)
numUnappliedConjuncts += predUnappliedConjucts
constrainedCols.UnionWith(predConstrainedCols)
constrainedCols = sb.tryReduceCols(constrainedCols, s, MakeTableFuncDep(sb.md, scan.Table))
histCols.UnionWith(predHistCols)
}
// Set null counts to 0 for non-nullable columns
// ---------------------------------------------
notNullCols := relProps.NotNullCols.Copy()
if constraint != nil {
// Add any not-null columns from this constraint.
notNullCols.UnionWith(constraint.ExtractNotNullCols(sb.evalCtx))
}
// Add any not-null columns from the predicate constraints.
for i := range pred {
if c := pred[i].ScalarProps().Constraints; c != nil {
notNullCols.UnionWith(c.ExtractNotNullCols(sb.evalCtx))
}
}
sb.updateNullCountsFromNotNullCols(notNullCols, s)
// Calculate row count and selectivity
// -----------------------------------
corr := sb.correlationFromMultiColDistinctCounts(constrainedCols, scan, s)
s.ApplySelectivity(sb.selectivityFromConstrainedCols(constrainedCols, histCols, scan, s, corr))
s.ApplySelectivity(sb.selectivityFromUnappliedConjuncts(numUnappliedConjuncts))
s.ApplySelectivity(sb.selectivityFromNullsRemoved(scan, notNullCols, constrainedCols))
}
func (sb *statisticsBuilder) colStatScan(colSet opt.ColSet, scan *ScanExpr) *props.ColumnStatistic {
relProps := scan.Relational()
s := &relProps.Stats
inputColStat := sb.colStatTable(scan.Table, colSet)
colStat := sb.copyColStat(colSet, s, inputColStat)
if sb.shouldUseHistogram(relProps) {
colStat.Histogram = inputColStat.Histogram
}
if s.Selectivity != props.OneSelectivity {
tableStats := sb.makeTableStatistics(scan.Table)
colStat.ApplySelectivity(s.Selectivity, tableStats.RowCount)
}
if colSet.Intersects(relProps.NotNullCols) {
colStat.NullCount = 0
}
sb.finalizeFromRowCountAndDistinctCounts(colStat, s)
return colStat
}
// +--------+
// | Select |