-
Notifications
You must be signed in to change notification settings - Fork 3.8k
/
Copy pathshow_stats.go
334 lines (310 loc) · 9.78 KB
/
show_stats.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
// Copyright 2017 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 sql
import (
"context"
encjson "encoding/json"
"github.com/cockroachdb/cockroach/pkg/sql/catalog"
"github.com/cockroachdb/cockroach/pkg/sql/catalog/colinfo"
"github.com/cockroachdb/cockroach/pkg/sql/catalog/descpb"
"github.com/cockroachdb/cockroach/pkg/sql/sem/tree"
"github.com/cockroachdb/cockroach/pkg/sql/sqlerrors"
"github.com/cockroachdb/cockroach/pkg/sql/stats"
"github.com/cockroachdb/cockroach/pkg/sql/types"
"github.com/cockroachdb/cockroach/pkg/util/errorutil"
"github.com/cockroachdb/cockroach/pkg/util/json"
"github.com/cockroachdb/cockroach/pkg/util/protoutil"
"github.com/cockroachdb/errors"
)
var showTableStatsColumns = colinfo.ResultColumns{
{Name: "statistics_name", Typ: types.String},
{Name: "column_names", Typ: types.StringArray},
{Name: "created", Typ: types.Timestamp},
{Name: "row_count", Typ: types.Int},
{Name: "distinct_count", Typ: types.Int},
{Name: "null_count", Typ: types.Int},
{Name: "avg_size", Typ: types.Int},
{Name: "histogram_id", Typ: types.Int},
}
var showTableStatsJSONColumns = colinfo.ResultColumns{
{Name: "statistics", Typ: types.Jsonb},
}
const showTableStatsOptForecast = "forecast"
var showTableStatsOptValidate = map[string]KVStringOptValidate{
showTableStatsOptForecast: KVStringOptRequireNoValue,
}
// ShowTableStats returns a SHOW STATISTICS statement for the specified table.
// Privileges: Any privilege on table.
func (p *planner) ShowTableStats(ctx context.Context, n *tree.ShowTableStats) (planNode, error) {
optsFn, err := p.TypeAsStringOpts(ctx, n.Options, showTableStatsOptValidate)
if err != nil {
return nil, err
}
opts, err := optsFn()
if err != nil {
return nil, err
}
// We avoid the cache so that we can observe the stats without
// taking a lease, like other SHOW commands.
desc, err := p.ResolveUncachedTableDescriptorEx(ctx, n.Table, true /*required*/, tree.ResolveRequireTableDesc)
if err != nil {
return nil, err
}
if err := p.CheckAnyPrivilege(ctx, desc); err != nil {
return nil, err
}
columns := showTableStatsColumns
if n.UsingJSON {
columns = showTableStatsJSONColumns
}
return &delayedNode{
name: n.String(),
columns: columns,
constructor: func(ctx context.Context, p *planner) (_ planNode, err error) {
// We need to query the table_statistics and then do some post-processing:
// - convert column IDs to column names
// - if the statistic has a histogram, we return the statistic ID as a
// "handle" which can be used with SHOW HISTOGRAM.
// TODO(yuzefovich): refactor the code to use the iterator API
// (currently it is not possible due to a panic-catcher below).
stmt := `SELECT
"tableID",
"statisticID",
name,
"columnIDs",
"createdAt",
"rowCount",
"distinctCount",
"nullCount",
"avgSize",
histogram
FROM system.table_statistics
WHERE "tableID" = $1
ORDER BY "createdAt"`
rows, err := p.ExtendedEvalContext().ExecCfg.InternalExecutor.QueryBuffered(
ctx,
"read-table-stats",
p.txn,
stmt,
desc.GetID(),
)
if err != nil {
return nil, err
}
const (
tableIDIdx = iota
statIDIdx
nameIdx
columnIDsIdx
createdAtIdx
rowCountIdx
distinctCountIdx
nullCountIdx
avgSizeIdx
histogramIdx
numCols
)
// Guard against crashes in the code below (e.g. #56356).
defer func() {
if r := recover(); r != nil {
// This code allows us to propagate internal errors without having to add
// error checks everywhere throughout the code. This is only possible
// because the code does not update shared state and does not manipulate
// locks.
if ok, e := errorutil.ShouldCatch(r); ok {
err = e
} else {
// Other panic objects can't be considered "safe" and thus are
// propagated as crashes that terminate the session.
panic(r)
}
}
}()
if _, withForecast := opts[showTableStatsOptForecast]; withForecast {
observed := make([]*stats.TableStatistic, 0, len(rows))
for _, row := range rows {
// Skip stats on dropped columns.
colIDs := row[columnIDsIdx].(*tree.DArray).Array
ignoreStatsRowWithDroppedColumn := false
for _, colID := range colIDs {
cid := descpb.ColumnID(*colID.(*tree.DInt))
if _, err := desc.FindColumnWithID(cid); err != nil {
if sqlerrors.IsUndefinedColumnError(err) {
ignoreStatsRowWithDroppedColumn = true
break
} else {
return nil, err
}
}
}
if ignoreStatsRowWithDroppedColumn {
continue
}
stat, err := stats.NewTableStatisticProto(row)
if err != nil {
return nil, err
}
obs := &stats.TableStatistic{TableStatisticProto: *stat}
if obs.HistogramData != nil && !obs.HistogramData.ColumnType.UserDefined() {
if err := stats.DecodeHistogramBuckets(obs); err != nil {
return nil, err
}
}
observed = append(observed, obs)
}
// Reverse the list to sort by CreatedAt descending.
for i := 0; i < len(observed)/2; i++ {
j := len(observed) - i - 1
observed[i], observed[j] = observed[j], observed[i]
}
forecasts := stats.ForecastTableStatistics(ctx, p.EvalContext(), observed)
// Iterate in reverse order to match the ORDER BY "columnIDs".
for i := len(forecasts) - 1; i >= 0; i-- {
forecastRow, err := tableStatisticProtoToRow(&forecasts[i].TableStatisticProto)
if err != nil {
return nil, err
}
rows = append(rows, forecastRow)
}
}
v := p.newContainerValuesNode(columns, 0)
if n.UsingJSON {
result := make([]stats.JSONStatistic, 0, len(rows))
for _, r := range rows {
var statsRow stats.JSONStatistic
colIDs := r[columnIDsIdx].(*tree.DArray).Array
statsRow.Columns = make([]string, len(colIDs))
ignoreStatsRowWithDroppedColumn := false
for j, d := range colIDs {
statsRow.Columns[j], err = statColumnString(desc, d)
if err != nil && sqlerrors.IsUndefinedColumnError(err) {
ignoreStatsRowWithDroppedColumn = true
break
}
}
if ignoreStatsRowWithDroppedColumn {
continue
}
statsRow.CreatedAt = tree.AsStringWithFlags(r[createdAtIdx], tree.FmtBareStrings)
statsRow.RowCount = (uint64)(*r[rowCountIdx].(*tree.DInt))
statsRow.DistinctCount = (uint64)(*r[distinctCountIdx].(*tree.DInt))
statsRow.NullCount = (uint64)(*r[nullCountIdx].(*tree.DInt))
statsRow.AvgSize = (uint64)(*r[avgSizeIdx].(*tree.DInt))
if r[nameIdx] != tree.DNull {
statsRow.Name = string(*r[nameIdx].(*tree.DString))
}
if err := statsRow.DecodeAndSetHistogram(ctx, &p.semaCtx, r[histogramIdx]); err != nil {
v.Close(ctx)
return nil, err
}
result = append(result, statsRow)
}
encoded, err := encjson.Marshal(result)
if err != nil {
v.Close(ctx)
return nil, err
}
jsonResult, err := json.ParseJSON(string(encoded))
if err != nil {
v.Close(ctx)
return nil, err
}
if _, err := v.rows.AddRow(ctx, tree.Datums{tree.NewDJSON(jsonResult)}); err != nil {
v.Close(ctx)
return nil, err
}
return v, nil
}
for _, r := range rows {
if len(r) != numCols {
v.Close(ctx)
return nil, errors.Errorf("incorrect columns from internal query")
}
colIDs := r[columnIDsIdx].(*tree.DArray).Array
colNames := tree.NewDArray(types.String)
colNames.Array = make(tree.Datums, len(colIDs))
ignoreStatsRowWithDroppedColumn := false
var colName string
for i, d := range colIDs {
colName, err = statColumnString(desc, d)
if err != nil && sqlerrors.IsUndefinedColumnError(err) {
ignoreStatsRowWithDroppedColumn = true
break
}
colNames.Array[i] = tree.NewDString(colName)
}
if ignoreStatsRowWithDroppedColumn {
continue
}
histogramID := tree.DNull
if r[histogramIdx] != tree.DNull {
histogramID = r[statIDIdx]
}
res := tree.Datums{
r[nameIdx],
colNames,
r[createdAtIdx],
r[rowCountIdx],
r[distinctCountIdx],
r[nullCountIdx],
r[avgSizeIdx],
histogramID,
}
if _, err := v.rows.AddRow(ctx, res); err != nil {
v.Close(ctx)
return nil, err
}
}
return v, nil
},
}, nil
}
func statColumnString(desc catalog.TableDescriptor, colID tree.Datum) (colName string, err error) {
id := descpb.ColumnID(*colID.(*tree.DInt))
colDesc, err := desc.FindColumnWithID(id)
if err != nil {
// This can happen if a column was removed.
return "<unknown>", err
}
return colDesc.GetName(), nil
}
func tableStatisticProtoToRow(stat *stats.TableStatisticProto) (tree.Datums, error) {
name := tree.DNull
if stat.Name != "" {
name = tree.NewDString(stat.Name)
}
columnIDs := tree.NewDArray(types.Int)
for _, c := range stat.ColumnIDs {
if err := columnIDs.Append(tree.NewDInt(tree.DInt(c))); err != nil {
return nil, err
}
}
row := tree.Datums{
tree.NewDInt(tree.DInt(stat.TableID)),
tree.NewDInt(tree.DInt(stat.StatisticID)),
name,
columnIDs,
&tree.DTimestamp{Time: stat.CreatedAt},
tree.NewDInt(tree.DInt(stat.RowCount)),
tree.NewDInt(tree.DInt(stat.DistinctCount)),
tree.NewDInt(tree.DInt(stat.NullCount)),
tree.NewDInt(tree.DInt(stat.AvgSize)),
}
if stat.HistogramData == nil {
row = append(row, tree.DNull)
} else {
histogram, err := protoutil.Marshal(stat.HistogramData)
if err != nil {
return nil, err
}
row = append(row, tree.NewDBytes(tree.DBytes(histogram)))
}
return row, nil
}