This repository was archived by the owner on Sep 27, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 618
/
Copy pathstats_storage.h
144 lines (115 loc) · 4.04 KB
/
stats_storage.h
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
//===----------------------------------------------------------------------===//
//
// Peloton
//
// stats_storage.h
//
// Identification: src/include/optimizer/stats/stats_storage.h
//
// Copyright (c) 2015-16, Carnegie Mellon University Database Group
//
//===----------------------------------------------------------------------===//
#pragma once
#include "optimizer/stats/table_stats_collector.h"
#include "optimizer/stats/column_stats_collector.h"
#include <sstream>
#include "common/macros.h"
#include "common/internal_types.h"
#include "type/value_factory.h"
namespace peloton {
namespace storage {
class Database;
class DataTable;
}
namespace optimizer {
using ValueFrequencyPair = std::pair<type::Value, double>;
class ColumnStats;
class TableStats;
class StatsStorage {
public:
// Global Singleton
static StatsStorage *GetInstance();
StatsStorage();
/* Functions for adding, updating and quering stats */
void InsertOrUpdateTableStats(storage::DataTable *table,
TableStatsCollector *table_stats_collector,
concurrency::TransactionContext *txn = nullptr);
void InsertOrUpdateColumnStats(
oid_t database_id, oid_t table_id, oid_t column_id, int num_rows,
double cardinality, double frac_null, std::string most_common_vals,
std::string most_common_freqs, std::string histogram_bounds,
std::string column_name, bool has_index = false,
concurrency::TransactionContext *txn = nullptr);
std::shared_ptr<ColumnStats> GetColumnStatsByID(oid_t database_id,
oid_t table_id,
oid_t column_id);
std::shared_ptr<TableStats> GetTableStats(
oid_t database_id, oid_t table_id, concurrency::TransactionContext *txn);
std::shared_ptr<TableStats> GetTableStats(
oid_t database_id, oid_t table_id, std::vector<oid_t> column_ids,
concurrency::TransactionContext *txn);
/* Functions for triggerring stats collection */
ResultType AnalyzeStatsForAllTables(
storage::Database *database,
concurrency::TransactionContext *txn = nullptr);
ResultType AnalyzeStatsForTable(
storage::DataTable *table,
concurrency::TransactionContext *txn = nullptr);
ResultType AnalayzeStatsForColumns(storage::DataTable *table,
std::vector<std::string> column_names);
private:
std::unique_ptr<type::AbstractPool> pool_;
std::shared_ptr<ColumnStats> ConvertVectorToColumnStats(
oid_t database_id, oid_t table_id, oid_t column_id,
std::unique_ptr<std::vector<type::Value>> &column_stats_vector);
std::string ConvertDoubleArrayToString(std::vector<double> &double_array) {
if (double_array.size() == 0) {
return std::string();
}
std::stringstream ss;
for (size_t i = 0; i < double_array.size(); ++i) {
if (i != 0) {
ss << ",";
}
ss << double_array[i];
}
return ss.str();
}
std::vector<double> ConvertStringToDoubleArray(std::string str) {
std::vector<double> double_array;
std::stringstream ss(str);
double num;
while (ss >> num) {
double_array.push_back(num);
if (ss.peek() == ',') {
ss.ignore();
}
}
return double_array;
}
std::pair<std::string, std::string> ConvertValueFreqArrayToStrings(
std::vector<ValueFrequencyPair> &val_freqs) {
size_t array_size = val_freqs.size();
if (array_size == 0) {
return std::make_pair("", "");
}
auto type_id = val_freqs[0].first.GetTypeId();
if (type_id == type::TypeId::VARBINARY ||
type_id == type::TypeId::VARCHAR) {
return std::make_pair("", "");
}
std::stringstream ss_value;
std::stringstream ss_freq;
for (size_t i = 0; i < array_size; ++i) {
if (i != 0) {
ss_value << ",";
ss_freq << ",";
}
ss_value << val_freqs[i].first.ToString();
ss_freq << val_freqs[i].second;
}
return std::make_pair(ss_value.str(), ss_freq.str());
}
};
}
}