12#include <unordered_map>
18template<std::
size_t N>
29 using block_t = std::conditional_t<IsConst, const Block, Block>;
30 using reference = std::conditional_t<IsConst, const Tuple&, Tuple&>;
31 using pointer = std::conditional_t<IsConst, const Tuple*, Tuple*>;
42 return this->mask_ == other.
mask_;
49 std::size_t
index()
const {
return __builtin_ctzl(
mask_); }
62 static_assert(N <= 64,
"maximum block size exceeded");
88 std::size_t
size()
const {
return __builtin_popcountl(
mask_); }
106 bool alive(std::size_t index)
const {
108 return mask_ & (1UL << index);
127 M_insist(
alive(index),
"cannot access a dead tuple directly");
149 for (
auto &t :
data_)
156 out <<
"Block<" << block.
capacity() <<
"> with " << block.
size() <<
" elements:\n";
157 for (std::size_t i = 0; i != block.
capacity(); ++i) {
158 out <<
" " << i <<
": ";
168 void dump(std::ostream &out)
const
208 using ConstOperatorVisitor::operator();
209#define DECLARE(CLASS) void operator()(Const<CLASS> &op) override;
226 using ConstOperatorVisitor::operator();
227#define DECLARE(CLASS) void operator()(Const<CLASS> &op) override;
243 return int64_t(strtoll(*c.
tok.
text,
nullptr, 8));
246 return int64_t(strtoll(*c.
tok.
text,
nullptr, 10));
249 return int64_t(strtoll(*c.
tok.
text,
nullptr, 16));
253 return strtod(*c.
tok.
text,
nullptr);
259 case TK_STRING_LITERAL: {
267 int year, month, day;
268 if (3 != sscanf(*c.
tok.
text,
"d'%d-%d-%d'", &year, &month, &day))
270 return int32_t(
unsigned(year) << 9 | month << 5 | day);
276 std::string str_datetime(*c.
tok.
text);
277 std::istringstream ss(str_datetime.substr(2, str_datetime.length() - 3));
282 M_insist(not ss.fail(),
"failed to parse date time");
284#if defined(__APPLE__)
286 constexpr long SECONDS_PER_400_YEAR_CYCLE = 12622780800L;
287 int cycles = tm.tm_year < 0 ? -tm.tm_year / 400 + 1 : 0;
288 tm.tm_year += cycles * 400;
293 const time_t time = timegm(&tm);
294 M_insist(time != -1,
"datetime out of bounds");
296#if defined(__APPLE__)
297 return int64_t(time - cycles * SECONDS_PER_400_YEAR_CYCLE);
299 return int64_t(time);
310 M_insist(errno == 0,
"constant could not be parsed");
324 const Schema &layout_schema, std::size_t row_id = 0, std::size_t tuple_id = 0);
337 const Schema &layout_schema, std::size_t row_id = 0, std::size_t tuple_id = 0);
#define M_OPERATOR_LIST(X)
#define M_unreachable(MSG)
std::string interpret(const std::string &str, char esc='\\', char quote='"')
void M_EXPORT setbit(T *bytes, bool value, uint32_t n)
Defines the interface of all execution Backends.
static constexpr bool IsConst
std::size_t index() const
pointer operator->() const
bool operator!=(the_iterator other)
std::conditional_t< IsConst, const Tuple &, Tuple & > reference
bool operator==(the_iterator other)
reference operator*() const
the_iterator & operator++()
the_iterator operator++(int)
std::conditional_t< IsConst, const Block, Block > block_t
the_iterator(block_t &vec, uint64_t mask)
A block of size N contains N tuples.
Block(Schema schema)
Create a new Block with tuples of Schema schema.
const_iterator end() const
bool empty() const
Returns true iff the block has no alive tuples, i.e. size() == 0.
const Tuple & operator[](std::size_t index) const
Returns the tuple at index index.
void erase(std::size_t index)
Erase the tuple at the given index from this Block.
void clear()
Renders all tuples dead and removes their attributes.
const_iterator at(std::size_t index) const
Returns an iterator to the tuple at index index.
uint64_t mask() const
Returns the bit mask that identifies which tuples of this Block are alive.
Tuple & operator[](std::size_t index)
Returns the tuple at index index.
std::array< Tuple, N > data_
an array of the tuples of this Block; some slots may be unused
the_iterator< false > iterator
void erase(const_iterator it)
Erase the tuple identified by it from this Block.
Block(const Block &)=delete
void erase(iterator it)
Erase the tuple identified by it from this Block.
const_iterator cbegin() const
bool alive(std::size_t index) const
Check whether the tuple at the given index is alive.
void dump(std::ostream &out) const
static constexpr std::size_t capacity()
Return the capacity of this Block.
the_iterator< true > const_iterator
const_iterator begin() const
const_iterator cend() const
const Tuple * data() const
Return a pointer to the underlying array of tuples.
Tuple * data()
Return a pointer to the underlying array of tuples.
std::size_t size() const
Return the number of alive tuples in this Block.
void fill()
Make all tuples in this Block alive.
static constexpr uint64_t AllOnes()
Returns a bit vector with left-most capacity() many bits set to 1 and the others set to 0.
uint64_t mask_
a mast identifying which slots of data_ are in use
static constexpr std::size_t CAPACITY
the capacity of a block
void mask(uint64_t new_mask)
Returns the bit mask that identifies which tuples of this Block are alive.
const Schema & schema() const
iterator at(std::size_t index)
Returns an iterator to the tuple at index index.
M_LCOV_EXCL_START friend std::ostream & operator<<(std::ostream &out, const Block< N > &block)
Print a textual representation of this Block to out.
ThreadSafePooledString pool(const char *str) const
Creates an internalized copy of the string str by adding it to the internal StringPool.
static Catalog & Get()
Return a reference to the single Catalog instance.
Evaluates SQL operator trees on the database.
M_OPERATOR_LIST(DECLARE) static Value eval(const ast
static StackMachine compile_store(const Schema &tuple_schema, void *address, const storage::DataLayout &layout, const Schema &layout_schema, std::size_t row_id=0, std::size_t tuple_id=0)
Compile a StackMachine to store a tuple of Schema tuple_schema using a given memory address and a giv...
void register_operators(PhysicalOptimizer &phys_opt) const override
Registers all physical operators of this Backend in phys_opt.
static StackMachine compile_load(const Schema &tuple_schema, void *address, const storage::DataLayout &layout, const Schema &layout_schema, std::size_t row_id=0, std::size_t tuple_id=0)
Compile a StackMachine to load a tuple of Schema tuple_schema using a given memory address and a give...
void execute(const MatchBase &plan) const override
Executes the already computed physical covering represented by plan using this Backend.
virtual const Operator & get_matched_root() const =0
Returns the matched logical root operator for physical operators.
An Operator represents an operation in a query plan.
The physical optimizer interface.
Implements push-based evaluation of a pipeline in the plan.
Pipeline(const Schema &schema)
const Schema & schema() const
void push(const Operator &pipeline_start)
A Schema represents a sequence of identifiers, optionally with a prefix, and their associated types.
A stack machine that evaluates an expression.
This class holds a SQL attribute value.
A constant: a string literal or a numeric constant.
Token tok
the token of the expression; serves as an anchor to locate the expression in the source
ThreadSafePooledOptionalString text
declared as optional for dummy tokens
Models how data is laid out in a linear address space.