-
Notifications
You must be signed in to change notification settings - Fork 3.8k
/
new_stat.go
152 lines (143 loc) · 3.62 KB
/
new_stat.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
// 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 stats
import (
"context"
"github.com/cockroachdb/cockroach/pkg/clusterversion"
"github.com/cockroachdb/cockroach/pkg/settings/cluster"
"github.com/cockroachdb/cockroach/pkg/sql/catalog/descpb"
"github.com/cockroachdb/cockroach/pkg/sql/isql"
"github.com/cockroachdb/cockroach/pkg/sql/sem/tree"
"github.com/cockroachdb/cockroach/pkg/sql/types"
"github.com/cockroachdb/cockroach/pkg/util/protoutil"
"github.com/cockroachdb/errors"
)
// InsertNewStats inserts a slice of statistics at the current time into the
// system table.
func InsertNewStats(
ctx context.Context, settings *cluster.Settings, txn isql.Txn, tableStats []*TableStatisticProto,
) error {
var err error
for _, statistic := range tableStats {
err = InsertNewStat(
ctx,
settings,
txn,
statistic.TableID,
statistic.Name,
statistic.ColumnIDs,
int64(statistic.RowCount),
int64(statistic.DistinctCount),
int64(statistic.NullCount),
int64(statistic.AvgSize),
statistic.HistogramData,
statistic.PartialPredicate,
statistic.FullStatisticID,
)
if err != nil {
return err
}
}
return nil
}
// InsertNewStat inserts a new statistic in the system table.
//
// The stats cache will automatically update asynchronously (as well as the
// stats caches on all other nodes).
func InsertNewStat(
ctx context.Context,
settings *cluster.Settings,
txn isql.Txn,
tableID descpb.ID,
name string,
columnIDs []descpb.ColumnID,
rowCount, distinctCount, nullCount, avgSize int64,
h *HistogramData,
partialPredicate string,
fullStatisticID uint64,
) error {
// We must pass a nil interface{} if we want to insert a NULL.
var nameVal, histogramVal interface{}
if name != "" {
nameVal = name
}
if h != nil {
var err error
histogramVal, err = protoutil.Marshal(h)
if err != nil {
return err
}
}
columnIDsVal := tree.NewDArray(types.Int)
for _, c := range columnIDs {
if err := columnIDsVal.Append(tree.NewDInt(tree.DInt(int(c)))); err != nil {
return err
}
}
if !settings.Version.IsActive(ctx, clusterversion.V23_1AddPartialStatisticsColumns) {
if partialPredicate != "" {
return errors.New("unable to insert new partial statistic as cluster version is from before V23.1.")
}
_, err := txn.Exec(
ctx, "insert-statistic", txn.KV(),
`INSERT INTO system.table_statistics (
"tableID",
"name",
"columnIDs",
"rowCount",
"distinctCount",
"nullCount",
"avgSize",
histogram
) VALUES ($1, $2, $3, $4, $5, $6, $7, $8)`,
tableID,
nameVal,
columnIDsVal,
rowCount,
distinctCount,
nullCount,
avgSize,
histogramVal,
)
return err
}
// Need to assign to a nil interface{} to be able
// to insert NULL value.
var predicateValue interface{}
if partialPredicate != "" {
predicateValue = partialPredicate
}
_, err := txn.Exec(
ctx, "insert-statistic", txn.KV(),
`INSERT INTO system.table_statistics (
"tableID",
"name",
"columnIDs",
"rowCount",
"distinctCount",
"nullCount",
"avgSize",
histogram,
"partialPredicate",
"fullStatisticID"
) VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10)`,
tableID,
nameVal,
columnIDsVal,
rowCount,
distinctCount,
nullCount,
avgSize,
histogramVal,
predicateValue,
fullStatisticID,
)
return err
}