Skip to content

Commit 60651f6

Browse files
committed
[Interpreter] Add physical operators for the Interpreter.
The added physical operators implement a simple 1:1 mapping between logical and physical operators. Thus, for executing a query, the former visitor pattern can still be used.
1 parent 82ec3f7 commit 60651f6

File tree

4 files changed

+76
-2
lines changed

4 files changed

+76
-2
lines changed

src/backend/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
set(
22
BACKEND_SOURCES
33
Interpreter.cpp
4+
InterpreterOperator.cpp
45
StackMachine.cpp
56
)
67

src/backend/Interpreter.hpp

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
#pragma once
22

3+
#include "backend/InterpreterOperator.hpp"
34
#include "backend/StackMachine.hpp"
45
#include <cerrno>
56
#include <ctime>
@@ -216,9 +217,11 @@ struct Interpreter : Backend, ConstOperatorVisitor
216217
public:
217218
Interpreter() = default;
218219

219-
void register_operators(PhysicalOptimizer &phys_opt) const override { /* nothing to be done */ }
220+
void register_operators(PhysicalOptimizer &phys_opt) const override { register_interpreter_operators(phys_opt); }
220221

221-
void execute(const MatchBase &plan) const override { M_unreachable("currently not supported"); }
222+
void execute(const MatchBase &plan) const override {
223+
(*const_cast<Interpreter*>(this))(plan.get_matched_singleton()); // use former visitor pattern on logical operators
224+
}
222225

223226
using ConstOperatorVisitor::operator();
224227
#define DECLARE(CLASS) void operator()(Const<CLASS> &op) override;

src/backend/InterpreterOperator.cpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
#include "backend/InterpreterOperator.hpp"
2+
3+
4+
using namespace m;
5+
6+
7+
void m::register_interpreter_operators(PhysicalOptimizer &phys_opt)
8+
{
9+
#define REGISTER(CLASS) phys_opt.register_operator<interpreter::CLASS>();
10+
M_OPERATOR_LIST(REGISTER)
11+
#undef REGISTER
12+
}

src/backend/InterpreterOperator.hpp

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
#pragma once
2+
3+
#include <mutable/IR/PhysicalOptimizer.hpp>
4+
5+
6+
namespace m {
7+
8+
/** This macro declares a simple 1:1 mapping between logical and physical operator for the `Interpreter`. */
9+
#define DECLARE(CLASS) \
10+
/* forward declarations */ \
11+
namespace interpreter { struct CLASS; } \
12+
template<> struct Match<interpreter::CLASS>; \
13+
\
14+
namespace interpreter { \
15+
\
16+
struct CLASS : PhysicalOperator<CLASS, m::CLASS> \
17+
{ \
18+
static void execute(const Match<CLASS>&, setup_t, pipeline_t, teardown_t) { M_unreachable("not implemented"); } \
19+
static double cost(const Match<CLASS>&) { return 1.0; } \
20+
static ConditionSet \
21+
adapt_post_conditions(const Match<CLASS>&, std::vector<std::reference_wrapper<const ConditionSet>>&&) { \
22+
return ConditionSet(); /* has to be overwritten for operators with multiple children, i.e. `JoinOperator` */ \
23+
} \
24+
}; \
25+
\
26+
} \
27+
\
28+
template<> \
29+
struct Match<interpreter::CLASS> : MatchBase \
30+
{ \
31+
const CLASS &op; \
32+
std::vector<std::reference_wrapper<const MatchBase>> children; \
33+
\
34+
Match(const CLASS *op, std::vector<std::reference_wrapper<const MatchBase>> &&children) \
35+
: op(*op) \
36+
, children(std::move(children)) \
37+
{ } \
38+
\
39+
void execute(setup_t, pipeline_t, teardown_t) const override { \
40+
M_unreachable("must not be called since `Interpreter` uses former visitor pattern on logical operators for " \
41+
"execution"); \
42+
} \
43+
\
44+
const Operator & get_matched_singleton() const override { return op; } \
45+
\
46+
protected: \
47+
void print(std::ostream &out, unsigned level) const override { \
48+
indent(out, level) << "interpreter::" << #CLASS << " (cumulative cost " << cost() << ')'; \
49+
for (auto &child : children) \
50+
child.get().print(out, level + 1); \
51+
} \
52+
};
53+
M_OPERATOR_LIST(DECLARE)
54+
#undef DECLARE
55+
56+
void register_interpreter_operators(PhysicalOptimizer &phys_opt);
57+
58+
}

0 commit comments

Comments
 (0)