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
//! Table- and column-level statistics used for cost estimation.
//!
//! These structures describe the shape of the data behind a schema:
//! how many rows a table holds, how many distinct values a column has,
//! what fraction of a column is null, and so on. A query planner or
//! optimizer consumes them to estimate selectivity and cardinality.
//!
//! The types are deliberately serialization-friendly (`serde` +
//! `serde_json::Value` for open-ended bounds) so a
//! [`StatisticsProvider`](crate::statistics::StatisticsProvider) can
//! load them from a database system table, a JSON/YAML file, or any
//! other external source.
use ;
use HashMap;
/// Statistics describing a single column.
///
/// Every field defaults to a neutral value so a partially populated
/// record (e.g. a provider that only knows `null_fraction`) is still
/// valid. `min_value`, `max_value`, and `histogram` use
/// [`serde_json::Value`] because a column can hold any SQL type.
///
/// # Examples
///
/// ```
/// use vlorql_core::statistics::ColumnStatistics;
/// use serde_json::json;
///
/// let stats = ColumnStatistics {
/// null_fraction: 0.1,
/// distinct_count: 1_000,
/// min_value: Some(json!(1)),
/// max_value: Some(json!(9_999)),
/// avg_column_width: Some(4),
/// histogram: None,
/// };
/// assert_eq!(stats.distinct_count, 1_000);
/// ```
/// Statistics describing a single table and its columns.
///
/// # Examples
///
/// ```
/// use vlorql_core::statistics::{ColumnStatistics, TableStatistics};
///
/// let mut table = TableStatistics {
/// row_count: 42,
/// ..TableStatistics::default()
/// };
/// table
/// .columns
/// .insert("id".to_owned(), ColumnStatistics::default());
/// assert_eq!(table.row_count, 42);
/// assert!(table.columns.contains_key("id"));
/// ```
/// A catalog of table statistics for an entire schema.
///
/// This is the top-level document a
/// [`StatisticsProvider`](crate::statistics::StatisticsProvider)
/// produces and the shape a JSON/YAML statistics file is expected to
/// have.
///
/// # Examples
///
/// ```
/// use vlorql_core::statistics::{StatisticsCatalog, TableStatistics};
///
/// let mut catalog = StatisticsCatalog::default();
/// catalog
/// .tables
/// .insert("users".to_owned(), TableStatistics::default());
/// assert!(catalog.tables.contains_key("users"));
/// ```