forked from dolthub/go-mysql-server
/
statistics.go
88 lines (77 loc) · 2.73 KB
/
statistics.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
// Copyright 2022 Dolthub, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package sql
import (
"fmt"
"time"
)
// StatisticsTable is a table that can provide information about its number of rows and other facts to improve query
// planning performance.
type StatisticsTable interface {
Table
// DataLength returns the length of the data file (varies by engine).
DataLength(ctx *Context) (uint64, error)
// RowCount returns the row count for this table
RowCount(ctx *Context) (uint64, error)
}
type StatsReader interface {
CatalogTable
// Hist returns a HistogramMap providing statistics for a table's columns
Hist(ctx *Context, db, table string) (HistogramMap, error)
// RowCount returns a table's row count if the table implements sql.StatisticsTable,
// false if the table does not, or an error if the table was not found.
RowCount(ctx *Context, db, table string) (uint64, bool, error)
}
type StatsWriter interface {
CatalogTable
Analyze(ctx *Context, db, table string) error
}
type StatsReadWriter interface {
StatsReader
StatsWriter
}
// HistogramBucket represents a bucket in a histogram
// inspiration pulled from MySQL and Cockroach DB
type HistogramBucket struct {
LowerBound float64 // inclusive
UpperBound float64 // inclusive
Frequency float64
}
// Histogram is all statistics we care about for each column
type Histogram struct {
Buckets []*HistogramBucket
Mean float64
Min float64
Max float64
Count uint64
NullCount uint64
DistinctCount uint64
}
// HistogramMap is a map from column name to associated histogram
type HistogramMap map[string]*Histogram
// TableStatistics provides access to statistical information about the values stored in a table
type TableStatistics struct {
// RowCount returns the number of rows in this table.
RowCount uint64
// CreatedAt returns the time at which the current statistics for this table were generated.
CreatedAt time.Time
// Histograms returns a map from all column names to their associated histograms.
Histograms HistogramMap
}
func (ts *TableStatistics) Histogram(colName string) (*Histogram, error) {
if res, ok := ts.Histograms[colName]; ok {
return res, nil
}
return &Histogram{}, fmt.Errorf("column %s not found", colName)
}