10
10
#include < mutable/catalog/CostFunctionCout.hpp>
11
11
#include < mutable/IR/Operator.hpp>
12
12
#include < mutable/IR/PlanTable.hpp>
13
+ #include < mutable/lex/Token.hpp>
13
14
#include < mutable/Options.hpp>
15
+ #include < mutable/parse/AST.hpp>
14
16
#include < mutable/util/enum_ops.hpp>
15
17
#include < mutable/util/fn.hpp>
16
18
#include < stdexcept>
@@ -126,6 +128,7 @@ MultiVersioningTable::MultiVersioningTable(std::unique_ptr<Table> table)
126
128
(*table_)[ts_begin].is_hidden = true ;
127
129
(*table_)[ts_begin].not_nullable = true ;
128
130
(*table_)[ts_end].is_hidden = true ;
131
+ (*table_)[ts_end].not_nullable = true ;
129
132
}
130
133
131
134
M_LCOV_EXCL_START
@@ -139,6 +142,137 @@ void MultiVersioningTable::dump() const { dump(std::cerr); }
139
142
M_LCOV_EXCL_STOP
140
143
141
144
145
+ namespace {
146
+
147
+
148
+ void apply_timestamp_filter (QueryGraph &G)
149
+ {
150
+ Catalog &C = Catalog::Get ();
151
+
152
+ auto pos = Position (nullptr );
153
+ ast::Token ts_begin (pos, C.pool (" $ts_begin" ), TK_IDENTIFIER);
154
+ ast::Token ts_end (pos, C.pool (" $ts_end" ), TK_IDENTIFIER);
155
+
156
+ for (auto &ds : G.sources ()) {
157
+ if (auto bt = cast<const BaseTable>(ds.get ())) {
158
+ /* Set timestamp filter */
159
+ auto it = std::find_if (bt->table ().cbegin_hidden (),
160
+ bt->table ().end_hidden (),
161
+ [&](const Attribute & attr) {
162
+ return attr.name == C.pool (" $ts_begin" );
163
+ });
164
+
165
+ if (it != bt->table ().end_hidden ()) {
166
+ ast::Token table_name (pos, bt->table ().name (), TK_EOF);
167
+ /* ----- Build AST -----*/
168
+ // $ts_begin
169
+ std::unique_ptr<ast::Expr> ts_begin_designator = std::make_unique<ast::Designator>(
170
+ ts_begin,
171
+ table_name,
172
+ ts_begin,
173
+ Type::Get_Integer (Type::TY_Vector, 8 ),
174
+ &*it
175
+ );
176
+
177
+ // TST := transaction start time constant
178
+ std::unique_ptr<ast::Expr> ts_begin_transaction_constant = std::make_unique<ast::Constant>(ast::Token (
179
+ pos,
180
+ C.pool (std::to_string (G.transaction ()->start_time ()).c_str ()),
181
+ m::TK_DEC_INT
182
+ ));
183
+ ts_begin_transaction_constant->type (Type::Get_Integer (Type::TY_Vector, 8 ));
184
+
185
+ // $ts_begin <= TST
186
+ std::unique_ptr<ast::Expr> ts_begin_filter_clause = std::make_unique<ast::BinaryExpr>(
187
+ ast::Token (pos, C.pool (" <=" ), TK_LESS_EQUAL),
188
+ std::move (ts_begin_designator),
189
+ std::move (ts_begin_transaction_constant)
190
+ );
191
+ ts_begin_filter_clause->type (Type::Get_Boolean (Type::TY_Vector));
192
+
193
+ // $ts_end
194
+ std::unique_ptr<ast::Expr> ts_end_designator = std::make_unique<ast::Designator>(
195
+ ts_end,
196
+ table_name,
197
+ ts_end,
198
+ Type::Get_Integer (Type::TY_Vector, 8 ),
199
+ &bt->table ()[C.pool (" $ts_end" )]
200
+ );
201
+
202
+ // TST := transaction start time constant
203
+ std::unique_ptr<ast::Expr> ts_end_transaction_constant = std::make_unique<ast::Constant>(ast::Token (
204
+ pos,
205
+ C.pool (std::to_string (G.transaction ()->start_time ()).c_str ()),
206
+ m::TK_DEC_INT
207
+ ));
208
+ ts_end_transaction_constant->type (Type::Get_Integer (Type::TY_Vector, 8 ));
209
+
210
+ // $ts_end > TST
211
+ std::unique_ptr<ast::Expr> ts_end_greater_transaction_expr = std::make_unique<ast::BinaryExpr>(
212
+ ast::Token (pos, C.pool (" >" ), TK_GREATER),
213
+ std::move (ts_end_designator),
214
+ std::move (ts_end_transaction_constant)
215
+ );
216
+ ts_end_greater_transaction_expr->type (Type::Get_Boolean (Type::TY_Vector));
217
+
218
+ // $ts_end
219
+ std::unique_ptr<ast::Expr> ts_end_designator_2 = std::make_unique<ast::Designator>(
220
+ ts_end,
221
+ table_name,
222
+ ts_end,
223
+ Type::Get_Integer (Type::TY_Vector, 8 ),
224
+ &bt->table ()[C.pool (" $ts_end" )]
225
+ );
226
+
227
+ // 1
228
+ std::unique_ptr<ast::Expr> zero_constant = std::make_unique<ast::Constant>(ast::Token (
229
+ pos,
230
+ C.pool (" 0" ),
231
+ m::TK_DEC_INT
232
+ ));
233
+ zero_constant->type (Type::Get_Integer (Type::TY_Vector, 8 ));
234
+
235
+ // $ts_end = 0
236
+ std::unique_ptr<ast::Expr> ts_end_eq_zero = std::make_unique<ast::BinaryExpr>(
237
+ ast::Token (pos, C.pool (" =" ), TK_EQUAL),
238
+ std::move (ts_end_designator_2),
239
+ std::move (zero_constant)
240
+ );
241
+ ts_end_eq_zero->type (Type::Get_Boolean (Type::TY_Vector));
242
+
243
+ // $ts_end > TST OR $ts_end = 0
244
+ std::unique_ptr<ast::Expr> ts_end_filter_clause = std::make_unique<ast::BinaryExpr>(
245
+ ast::Token (pos, C.pool (" OR" ), TK_Or),
246
+ std::move (ts_end_greater_transaction_expr),
247
+ std::move (ts_end_eq_zero)
248
+ );
249
+ ts_end_filter_clause->type (Type::Get_Boolean (Type::TY_Vector));
250
+
251
+ // $ts_begin <= TST AND ($ts_end > TST OR $ts_end = 0)
252
+ std::unique_ptr<ast::Expr> filter_expr = std::make_unique<ast::BinaryExpr>(
253
+ ast::Token (pos, C.pool (" AND" ), TK_And),
254
+ std::move (ts_begin_filter_clause),
255
+ std::move (ts_end_filter_clause)
256
+ );
257
+ filter_expr->type (Type::Get_Boolean (Type::TY_Vector));
258
+
259
+ G.add_custom_filter (std::move (filter_expr), *ds);
260
+ }
261
+ }
262
+ }
263
+ }
264
+
265
+ __attribute__ ((constructor(202 )))
266
+ void register_pre_optimization ()
267
+ {
268
+ Catalog &C = Catalog::Get ();
269
+ C.register_pre_optimization (" multi-versioning" , apply_timestamp_filter, " adds timestamp filters to the QueryGraph" );
270
+ }
271
+
272
+
273
+ }
274
+
275
+
142
276
/* ======================================================================================================================
143
277
* Function
144
278
*====================================================================================================================*/
0 commit comments