mutable
A Database System for Research and Fast Prototyping
Loading...
Searching...
No Matches
Schema.cpp
Go to the documentation of this file.
2
3#include <algorithm>
4#include <cmath>
5#include <iostream>
6#include <iterator>
13#include <mutable/lex/Token.hpp>
14#include <mutable/Options.hpp>
15#include <mutable/parse/AST.hpp>
17#include <mutable/util/fn.hpp>
18#include <stdexcept>
19
20
21using namespace m;
22
23
24/*======================================================================================================================
25 * Schema
26 *====================================================================================================================*/
27
29{
30 return Identifier(Catalog::Get().pool("$const"));
31}
32
34 : name(Catalog::Get().pool(""))
35{
36 if (auto d = cast<const ast::Designator>(&expr)) {
37 prefix = d->table_name.text;
38 name = d->attr_name.text.assert_not_none();
39 } else {
40 std::ostringstream oss;
41 oss << expr;
42 prefix = {};
43 name = Catalog::Get().pool(oss.str().c_str());
44 }
45}
46
48
50void Schema::dump(std::ostream &out) const { out << *this << std::endl; }
51void Schema::dump() const { dump(std::cerr); }
53
54
55/*======================================================================================================================
56 * Attribute
57 *====================================================================================================================*/
58
60 auto primary_key = table.primary_key();
61 auto pred = [this](const auto &ref){ return *this == ref.get(); };
62 return unique or (primary_key.size() == 1 and
63 std::find_if(primary_key.cbegin(), primary_key.cend(), pred) != primary_key.cend());
64}
65
67void Attribute::dump(std::ostream &out) const
68{
69 out << "Attribute `" << table.name() << "`.`" << name << "`, "
70 << "id " << id << ", "
71 << "type " << *type
72 << std::endl;
73}
74
75void Attribute::dump() const { dump(std::cerr); }
77
78
79/*======================================================================================================================
80 * ConcreteTable
81 *====================================================================================================================*/
82
84{
85 Schema S;
86 for (auto attr = this->begin_all(); attr != this->end_all(); ++attr) {
88 if (attr->not_nullable)
90 if (attr->is_unique())
91 constraints |= Schema::entry_type::UNIQUE;
92 if (attr->reference and attr->reference->is_unique())
94 if (attr->is_hidden)
95 constraints |= Schema::entry_type::IS_HIDDEN;
96 S.add({alias.has_value() ? alias : this->name(), attr->name}, attr->type, constraints);
97 }
98 return S;
99}
100
102 view v(cbegin_all(), cend_all(), [](auto it) -> auto & { return it->type; });
103 layout_ = factory.make(v.begin(), v.end());
104}
105
107void ConcreteTable::dump(std::ostream &out) const
108{
109 out << "Table `" << name_ << '`';
110 for (const auto &attr : attrs_)
111 out << "\n` " << attr.id << ": `" << attr.name << "` " << *attr.type;
112 out << std::endl;
113}
114
115void ConcreteTable::dump() const { dump(std::cerr); }
117
118
119/*======================================================================================================================
120 * MultiVersioningTable
121 *====================================================================================================================*/
122
124 : TableDecorator(std::move(table))
125{
126 auto &C = Catalog::Get();
127
128 /*----- Add hidden timestamp attributes. -----*/
129 auto i8 = Type::Get_Integer(Type::TY_Vector, 8);
130 auto ts_begin = C.pool("$ts_begin");
131 auto ts_end = C.pool("$ts_end");
132 table_->push_back(ts_begin, i8);
133 table_->push_back(ts_end, i8);
134 (*table_)[ts_begin].is_hidden = true;
135 (*table_)[ts_begin].not_nullable = true;
136 (*table_)[ts_end].is_hidden = true;
137 (*table_)[ts_end].not_nullable = true;
138}
139
141void MultiVersioningTable::dump(std::ostream &out) const
142{
143 out << "MultiVersioningTable Decorator" << std::endl;
144 table_->dump(out);
145}
146
147void MultiVersioningTable::dump() const { dump(std::cerr); }
149
150
151namespace {
152
153
155{
156 Catalog &C = Catalog::Get();
157
158 auto pos = Position(nullptr);
159 ast::Token ts_begin(pos, C.pool("$ts_begin"), TK_IDENTIFIER);
160 ast::Token ts_end(pos, C.pool("$ts_end"), TK_IDENTIFIER);
161
162 for (auto &ds : G.sources()) {
163 if (auto bt = cast<const BaseTable>(ds.get())) {
164 /* Set timestamp filter */
165 auto it = std::find_if(bt->table().cbegin_hidden(),
166 bt->table().end_hidden(),
167 [&](const Attribute & attr) {
168 return attr.name == C.pool("$ts_begin");
169 });
170
171 if (it != bt->table().end_hidden()) {
172 ast::Token table_name(pos, bt->table().name(), TK_EOF);
173 /*----- Build AST -----*/
174 // $ts_begin
175 std::unique_ptr<ast::Expr> ts_begin_designator = std::make_unique<ast::Designator>(
176 ts_begin,
177 table_name,
178 ts_begin,
179 Type::Get_Integer(Type::TY_Vector, 8),
180 &*it
181 );
182
183 // TST := transaction start time constant
184 std::unique_ptr<ast::Expr> ts_begin_transaction_constant = std::make_unique<ast::Constant>(ast::Token(
185 pos,
186 C.pool(std::to_string(G.transaction()->start_time()).c_str()),
187 m::TK_DEC_INT
188 ));
189 ts_begin_transaction_constant->type(Type::Get_Integer(Type::TY_Vector, 8));
190
191 // $ts_begin <= TST
192 std::unique_ptr<ast::Expr> ts_begin_filter_clause = std::make_unique<ast::BinaryExpr>(
193 ast::Token(pos, C.pool("<="), TK_LESS_EQUAL),
194 std::move(ts_begin_designator),
195 std::move(ts_begin_transaction_constant)
196 );
197 ts_begin_filter_clause->type(Type::Get_Boolean(Type::TY_Vector));
198
199 // $ts_end
200 std::unique_ptr<ast::Expr> ts_end_designator = std::make_unique<ast::Designator>(
201 ts_end,
202 table_name,
203 ts_end,
204 Type::Get_Integer(Type::TY_Vector, 8),
205 &bt->table()[C.pool("$ts_end")]
206 );
207
208 // TST := transaction start time constant
209 std::unique_ptr<ast::Expr> ts_end_transaction_constant = std::make_unique<ast::Constant>(ast::Token(
210 pos,
211 C.pool(std::to_string(G.transaction()->start_time()).c_str()),
212 m::TK_DEC_INT
213 ));
214 ts_end_transaction_constant->type(Type::Get_Integer(Type::TY_Vector, 8));
215
216 // $ts_end > TST
217 std::unique_ptr<ast::Expr> ts_end_greater_transaction_expr = std::make_unique<ast::BinaryExpr>(
218 ast::Token(pos, C.pool(">"), TK_GREATER),
219 std::move(ts_end_designator),
220 std::move(ts_end_transaction_constant)
221 );
222 ts_end_greater_transaction_expr->type(Type::Get_Boolean(Type::TY_Vector));
223
224 // $ts_end
225 std::unique_ptr<ast::Expr> ts_end_designator_2 = std::make_unique<ast::Designator>(
226 ts_end,
227 table_name,
228 ts_end,
229 Type::Get_Integer(Type::TY_Vector, 8),
230 &bt->table()[C.pool("$ts_end")]
231 );
232
233 // -1
234 std::unique_ptr<ast::Expr> neg_one_constant = std::make_unique<ast::Constant>(ast::Token(
235 pos,
236 C.pool("-1"),
237 m::TK_DEC_INT
238 ));
239 neg_one_constant->type(Type::Get_Integer(Type::TY_Vector, 8));
240
241 // $ts_end = -1
242 std::unique_ptr<ast::Expr> ts_end_eq_zero = std::make_unique<ast::BinaryExpr>(
243 ast::Token(pos, C.pool("="), TK_EQUAL),
244 std::move(ts_end_designator_2),
245 std::move(neg_one_constant)
246 );
247 ts_end_eq_zero->type(Type::Get_Boolean(Type::TY_Vector));
248
249 // $ts_end > TST OR $ts_end = -1
250 std::unique_ptr<ast::Expr> ts_end_filter_clause = std::make_unique<ast::BinaryExpr>(
251 ast::Token(pos, C.pool("OR"), TK_Or),
252 std::move(ts_end_greater_transaction_expr),
253 std::move(ts_end_eq_zero)
254 );
255 ts_end_filter_clause->type(Type::Get_Boolean(Type::TY_Vector));
256
257 // $ts_begin <= TST AND ($ts_end > TST OR $ts_end = 0)
258 std::unique_ptr<ast::Expr> filter_expr = std::make_unique<ast::BinaryExpr>(
259 ast::Token(pos, C.pool("AND"), TK_And),
260 std::move(ts_begin_filter_clause),
261 std::move(ts_end_filter_clause)
262 );
263 filter_expr->type(Type::Get_Boolean(Type::TY_Vector));
264
265 G.add_custom_filter(std::move(filter_expr), *ds);
266 }
267 }
268 }
269}
270
271__attribute__((constructor(202)))
272void register_pre_optimization()
273{
274 Catalog &C = Catalog::Get();
275 C.register_pre_optimization(C.pool("multi-versioning"),
277 "adds timestamp filters to the QueryGraph");
278}
279
280
281}
282
283
284/*======================================================================================================================
285 * Function
286 *====================================================================================================================*/
287
288constexpr const char * Function::FNID_TO_STR_[];
289constexpr const char * Function::KIND_TO_STR_[];
290
292void Function::dump(std::ostream &out) const
293{
294 out << "Function{ name = \"" << name << "\", fnid = " << FNID_TO_STR_[fnid] << ", kind = " << KIND_TO_STR_[kind]
295 << "}" << std::endl;
296}
298
299
300/*======================================================================================================================
301 * Database
302 *====================================================================================================================*/
303
305 : name(name)
306{
308}
309
311{
312 for (auto &f : functions_)
313 delete f.second;
314}
315
317 auto it = tables_.find(name);
318 if (it != tables_.end()) throw std::invalid_argument("table with that name already exists");
319 it = tables_.emplace_hint(it, std::move(name), Catalog::Get().table_factory().make(name));
320 return *it->second;
321}
322
324{
325 try {
326 return functions_.at(name);
327 } catch (std::out_of_range) {
328 /* not defined within the database; search the global catalog */
330 }
331}
__attribute__((constructor(202))) static void register_interpreter()
#define id(X)
void apply_timestamp_filter(QueryGraph &G)
Definition: Schema.cpp:154
‍mutable namespace
Definition: Backend.hpp:10
ThreadSafeStringPool::proxy_type ThreadSafePooledString
Definition: Pool.hpp:464
and
Definition: enum_ops.hpp:12
ThreadSafeStringPool::proxy_optional_type ThreadSafePooledOptionalString
Definition: Pool.hpp:465
STL namespace.
An attribute of a table.
Definition: Schema.hpp:289
bool is_unique() const
Returns true iff this Attribute is unique, i.e.
Definition: Schema.cpp:59
void dump() const
Definition: Schema.cpp:75
The catalog contains all Databases and keeps track of all meta information of the database system.
Definition: Catalog.hpp:215
ThreadSafePooledString pool(const char *str) const
Creates an internalized copy of the string str by adding it to the internal StringPool.
Definition: Catalog.hpp:274
static Catalog & Get()
Return a reference to the single Catalog instance.
std::unique_ptr< CardinalityEstimator > create_cardinality_estimator(ThreadSafePooledString database) const
Creates a new CardinalityEstimator.
Definition: Catalog.hpp:409
void register_pre_optimization(ThreadSafePooledString name, PreOptimizationCallback optimization, const char *description=nullptr)
Registers a new pre-optimization with the given name.
Definition: Catalog.hpp:610
const Function * get_function(const ThreadSafePooledString &name) const
Returns a reference to the Function with the given name.
Definition: Catalog.hpp:310
const storage::DataLayout & layout() const override
Returns a reference to the physical data layout.
Definition: Schema.hpp:702
virtual void dump() const override
Definition: Schema.cpp:115
Schema schema(const ThreadSafePooledOptionalString &alias={}) const override
Returns a Schema for this Table given the alias alias.
Definition: Schema.cpp:83
Database(ThreadSafePooledString name)
Definition: Schema.cpp:304
std::unordered_map< ThreadSafePooledString, Function * > functions_
functions defined in this database
Definition: Schema.hpp:895
std::unordered_map< ThreadSafePooledString, std::unique_ptr< Table > > tables_
the tables of this database
Definition: Schema.hpp:894
const Function * get_function(const ThreadSafePooledString &name) const
Returns a reference to the Function with the given name.
Definition: Schema.cpp:323
Table & add_table(ThreadSafePooledString name)
Adds a new Table to this Database.
Definition: Schema.cpp:316
std::unique_ptr< CardinalityEstimator > cardinality_estimator_
the CardinalityEstimator of this Database
Definition: Schema.hpp:896
ThreadSafePooledString name
the name of the database
Definition: Schema.hpp:892
Defines a function.
Definition: Schema.hpp:828
void dump() const
static constexpr const char * FNID_TO_STR_[]
Definition: Schema.hpp:858
static constexpr const char * KIND_TO_STR_[]
Definition: Schema.hpp:864
fnid_t fnid
the function id
Definition: Schema.hpp:841
ThreadSafePooledString name
the name of the function
Definition: Schema.hpp:840
void dump() const override
Definition: Schema.cpp:147
MultiVersioningTable(std::unique_ptr< Table > table)
Definition: Schema.cpp:123
Pooled< T, Pool, false > assert_not_none() const
Definition: Pool.hpp:239
bool has_value() const
Definition: Pool.hpp:237
The query graph represents all data sources and joins in a graph structure.
Definition: QueryGraph.hpp:172
void add_custom_filter(std::unique_ptr< ast::Expr > filter_expr, DataSource &ds)
Creates a cnf::CNF from filter_expr and adds it to the current filter of the given DataSource ds by l...
Definition: QueryGraph.hpp:308
const auto & sources() const
Definition: QueryGraph.hpp:257
void transaction(Scheduler::Transaction *t)
Set the transaction ID.
Definition: QueryGraph.hpp:296
An Identifier is composed of a name and an optional prefix.
Definition: Schema.hpp:42
static Identifier GetConstant()
Definition: Schema.cpp:28
Identifier(ThreadSafePooledString name)
Definition: Schema.hpp:50
ThreadSafePooledOptionalString prefix
optional prefix of this Identifier, may not have a value
Definition: Schema.hpp:47
ThreadSafePooledString name
the name of this Identifier
Definition: Schema.hpp:48
@ NOT_NULLABLE
entry must not be NULL
Definition: Schema.hpp:80
@ IS_HIDDEN
entry is hidden to the user
Definition: Schema.hpp:83
@ REFERENCES_UNIQUE
entry references unique values
Definition: Schema.hpp:82
@ UNIQUE
entry has unique values
Definition: Schema.hpp:81
A Schema represents a sequence of identifiers, optionally with a prefix, and their associated types.
Definition: Schema.hpp:39
void dump() const
Definition: Schema.cpp:51
void add(entry_type e)
Adds the entry e to this Schema.
Definition: Schema.hpp:181
Abstract Decorator class that concrete TableDecorator inherit from.
Definition: Schema.hpp:748
std::unique_ptr< Table > table_
Definition: Schema.hpp:750
A table is a sorted set of attributes.
Definition: Schema.hpp:388
static Pooled< Boolean > Get_Boolean(category_t category)
Returns a Boolean type of the given category.
Definition: Type.cpp:68
static Pooled< Numeric > Get_Integer(category_t category, unsigned num_bytes)
Returns a Numeric type for integrals of given category and num_bytes bytes.
Definition: Type.cpp:94
An expression.
Definition: AST.hpp:39
This is an interface for factories that compute particular DataLayouts for a given sequence of Types,...
DataLayout make(const Schema &schema, std::size_t num_tuples=0) const
Returns a DataLayout for the given Types contained in schema and length num_tuples.
Definition: ADT.hpp:750