mutable
A Database System for Research and Fast Prototyping
Loading...
Searching...
No Matches
Tuple.cpp
Go to the documentation of this file.
2
3#include <ctime>
6#include <mutable/util/fn.hpp>
7
8
9using namespace m;
10
11
12/*======================================================================================================================
13 Value
14 *====================================================================================================================*/
15
17void Value::print(std::ostream &out, const Type &ty) const
18{
20 [this, &out](const Boolean&) { out << (as_b() ? "TRUE" : "FALSE"); },
21 [this, &out](const CharacterSequence &cs) { out << '"'; out.write(as<const char*>(), cs.length); out << '"'; },
22 [this, &out](const Date&) {
23 const int32_t date = as_i(); // signed because year is signed
24 const auto oldfill = out.fill('0');
25 const auto oldfmt = out.flags();
26 out << std::internal
27 << std::setw(date >> 9 > 0 ? 4 : 5) << (date >> 9) << '-'
28 << std::setw(2) << ((date >> 5) & 0xF) << '-'
29 << std::setw(2) << (date & 0x1F);
30 out.fill(oldfill);
31 out.flags(oldfmt);
32 },
33 [this, &out](const DateTime&) {
34 const time_t time = as_i();
35 std::tm tm;
36 gmtime_r(&time, &tm);
37 out << put_tm(tm);
38 },
39 [this, &out](const Numeric &n) {
40 switch (n.kind) {
41 case Numeric::N_Int:
42 out << as_i();
43 break;
44
45 case Numeric::N_Decimal: {
46 const int64_t div = powi(10L, n.scale);
47 const int64_t pre = as_i() / div;
48 const int64_t post = as_i() % div;
49 out << pre << '.';
50 auto old_fill = out.fill('0');
51 out << std::setw(n.scale) << post;
52 out.fill(old_fill);
53 break;
54 }
55
56 case Numeric::N_Float:
57 if (n.size() == 32)
58 out << as_f();
59 else
60 out << as_d();
61 break;
62 }
63 },
64 [](auto&&) { M_unreachable("invalid value type"); }
65 }, ty);
66}
67
68void Value::dump(std::ostream &out) const { out << *this << std::endl; }
69void Value::dump() const { dump(std::cerr); }
71
72
73/*======================================================================================================================
74 Tuple
75 *====================================================================================================================*/
76
78#ifdef M_ENABLE_SANITY_FIELDS
79 : num_values_(S.num_entries())
80#endif
81{
82 std::size_t additional_bytes = 0;
83 for (auto &e : S) {
84 if (auto cs = cast<const CharacterSequence>(e.type))
85 additional_bytes += cs->length + 1;
86 }
87 values_ = (Value*) malloc(S.num_entries() * sizeof(Value) + additional_bytes);
88 uint8_t *p = reinterpret_cast<uint8_t*>(values_) + S.num_entries() * sizeof(Value);
89 for (std::size_t i = 0; i != S.num_entries(); ++i) {
90 if (auto cs = cast<const CharacterSequence>(S[i].type)) {
91 new (&values_[i]) Value(p);
92 *p = '\0'; // terminating NUL byte
93 p += cs->length + 1;
94 } else {
95 new (&values_[i]) Value();
96 }
97 }
98 clear();
99}
100
101Tuple::Tuple(std::vector<const Type*> types)
102#ifdef M_ENABLE_SANITY_FIELDS
103 : num_values_(types.size())
104#endif
105{
106 std::size_t additional_bytes = 0;
107 for (auto &ty : types) {
108 if (auto cs = cast<const CharacterSequence>(ty))
109 additional_bytes += cs->length + 1;
110 }
111 values_ = (Value*) malloc(types.size() * sizeof(Value) + additional_bytes);
112 uint8_t *p = reinterpret_cast<uint8_t*>(values_) + types.size() * sizeof(Value);
113 for (std::size_t i = 0; i != types.size(); ++i) {
114 if (auto cs = cast<const CharacterSequence>(types[i])) {
115 new (&values_[i]) Value(p);
116 p += cs->length + 1;
117 } else {
118 new (&values_[i]) Value();
119 }
120 }
121 clear();
122}
123
124Tuple Tuple::clone(const Schema &S) const
125{
126 Tuple cpy(S);
127 for (std::size_t i = 0; i != S.num_entries(); ++i) {
128 if (S[i].type->is_character_sequence()) {
129 strcpy(reinterpret_cast<char*>(cpy[i].as_p()), reinterpret_cast<char*>((*this)[i].as_p()));
130 cpy.not_null(i);
131 } else {
132 cpy.set(i, (*this)[i]);
133 }
134 }
135 return cpy;
136}
137
139void Tuple::print(std::ostream &out, const Schema &schema) const
140{
141 for (std::size_t i = 0; i != schema.num_entries(); ++i) {
142 if (i != 0) out << ',';
143 if (is_null(i))
144 out << "NULL";
145 else
146 values_[i].print(out, *schema[i].type);
147 }
148}
149
150void Tuple::dump(std::ostream &out) const { out << *this << std::endl; }
151void Tuple::dump() const { dump(std::cerr); }
#define M_unreachable(MSG)
Definition: macro.hpp:146
‍mutable namespace
Definition: Backend.hpp:10
T M_EXPORT powi(const T base, const U exp)
Power function for integral types.
Definition: fn.hpp:428
auto visit(Callable &&callable, Base &obj, m::tag< Callable > &&=m::tag< Callable >())
Generic implementation to visit a class hierarchy, with similar syntax as std::visit.
Definition: Visitor.hpp:138
The boolean type.
Definition: Type.hpp:230
The type of character strings, both fixed length and varying length.
Definition: Type.hpp:290
The date type.
Definition: Type.hpp:364
The date type.
Definition: Type.hpp:335
The numeric type represents integer and floating-point types of different precision and scale.
Definition: Type.hpp:393
A Schema represents a sequence of identifiers, optionally with a prefix, and their associated types.
Definition: Schema.hpp:39
std::size_t num_entries() const
Returns the number of entries in this Schema.
Definition: Schema.hpp:124
Tuple()
Definition: Tuple.hpp:209
Value * values_
the Values in this Tuple
Definition: Tuple.hpp:194
void print(std::ostream &out, const Schema &schema) const
Print this Tuple using the given schema.
Definition: Tuple.cpp:139
Tuple clone(const Schema &S) const
Create a clone of this Tuple interpreted using the Schema S.
Definition: Tuple.cpp:124
bool is_null(std::size_t idx) const
Returns true iff the Value at index idx is NULL.
Definition: Tuple.hpp:219
void not_null(std::size_t idx)
Sets the Value at index idx to not-NULL.
Definition: Tuple.hpp:234
void dump() const
Definition: Tuple.cpp:151
void set(std::size_t idx, Value val)
Assigns the Value val to this Tuple at index idx and clears the respective NULL bit.
Definition: Tuple.hpp:240
This class represents types in the SQL type system.
Definition: Type.hpp:46
This class holds a SQL attribute value.
Definition: Tuple.hpp:19
void dump() const
Definition: Tuple.cpp:69
auto & as_f()
Returns a reference to the value interpreted as of type float.
Definition: Tuple.hpp:105
M_LCOV_EXCL_STOP void print(std::ostream &out, const Type &ty) const
Interpret this Value as of Type ty and print a human-readable representation to out.
Definition: Tuple.cpp:17
auto & as_b()
Returns a reference to the value interpreted as of type bool.
Definition: Tuple.hpp:101
auto & as_d()
Returns a reference to the value interpreted as of type double.
Definition: Tuple.hpp:107
auto & as_i()
Returns a reference to the value interpreted as of type int64_t.
Definition: Tuple.hpp:103