Skip to content
This repository was archived by the owner on Apr 21, 2020. It is now read-only.

Commit 53d9ad5

Browse files
committed
Fixing bugs and fully implementing string compare in predicates
1 parent 906026b commit 53d9ad5

File tree

8 files changed

+80
-21
lines changed

8 files changed

+80
-21
lines changed

include/cexpr/string.hpp

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,18 @@ namespace cexpr
120120
return i == N;
121121
}
122122

123+
template <typename OtherCharT>
124+
bool operator==(std::basic_string<OtherCharT> const& other) const
125+
{
126+
return other == string_;
127+
}
128+
129+
template <typename OtherCharT>
130+
bool operator!=(std::basic_string<OtherCharT> const& other) const
131+
{
132+
return !(other == string_);
133+
}
134+
123135
private:
124136
CharT string_[N];
125137
std::size_t size_;
@@ -129,17 +141,15 @@ namespace cexpr
129141
string(const CharT[N]) -> string<CharT, N>;
130142

131143
template <typename CharT, std::size_t N>
132-
constexpr bool operator==(std::basic_string<CharT> const& str, string<CharT, N> const& cstr)
144+
bool operator==(std::basic_string<CharT> const& str, string<CharT, N> const& cstr)
133145
{
134-
if (str.size() != N)
135-
{
136-
return false;
137-
}
138-
139-
std::size_t i{};
140-
for (; i < N && str[i] == cstr[i]; ++i);
146+
return cstr == str;
147+
}
141148

142-
return i == N;
149+
template <typename CharT, std::size_t N>
150+
bool operator!=(std::basic_string<CharT> const& str, string<CharT, N> const& cstr)
151+
{
152+
return cstr != str;
143153
}
144154

145155
} // namespace cexpr

include/ra/selection.hpp

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ namespace ra
1414

1515
static auto& next()
1616
{
17-
auto& output_row = Input::next();
17+
output_row = Input::next();
1818

1919
while(!Predicate::eval(output_row))
2020
{
@@ -23,6 +23,12 @@ namespace ra
2323

2424
return output_row;
2525
}
26+
27+
private:
28+
static output_type output_row;
2629
};
2730

31+
template <typename Output, typename Input>
32+
typename selection<Output, Input>::output_type selection<Output, Input>::output_row{};
33+
2834
} // namespace ra

include/sql/predicate.hpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,12 @@ namespace sql
1313
// shim to allow all value types like double or float
1414
// to be used as non-type template parameters.
1515
template <typename ValT>
16-
struct number
16+
struct value
1717
{
18-
constexpr number(ValT val) : value{ val }
18+
constexpr value(ValT v) : val{ v }
1919
{}
2020

21-
ValT value;
21+
ValT val;
2222
};
2323

2424
} // namespace
@@ -81,7 +81,7 @@ namespace sql
8181
{
8282
static constexpr auto eval(Row const& row)
8383
{
84-
return Const.value;
84+
return Const.val;
8585
}
8686
};
8787

include/sql/query.hpp

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ namespace sql
6161
}
6262

6363
template <typename ValT, typename CharT, std::size_t N>
64-
constexpr number<ValT> convert(cexpr::string<CharT, N> const& str)
64+
constexpr value<ValT> convert(cexpr::string<CharT, N> const& str)
6565
{
6666
auto curr{ str.cbegin() }, end{ str.cend() };
6767
constexpr CharT nul{ '\0' }, dot{ '.' }, zro{ '0' }, min{ '-' };
@@ -91,7 +91,7 @@ namespace sql
9191
}
9292
}
9393

94-
return number<ValT>{ acc * sign };
94+
return value<ValT>{ acc * sign };
9595
}
9696

9797
constexpr bool isintegral(std::string_view const& tv) noexcept
@@ -192,7 +192,7 @@ namespace sql
192192
constexpr auto pos{ Pos + 1 % tokens_.count() };
193193
constexpr cexpr::string<char, tokens_[pos].length() + 1> name{ tokens_[pos] };
194194

195-
return TreeNode<Pos + 3, sql::constant<name, Row>>{};
195+
return TreeNode<Pos + 3, sql::constant<value<decltype(name)>{ name }, Row>>{};
196196
}
197197
else if constexpr (isdigit(tokens_[Pos][0]))
198198
{
@@ -224,7 +224,7 @@ namespace sql
224224
constexpr cexpr::string<char, tokens_[pos].length() + 1> name{ tokens_[pos] };
225225
constexpr auto node{ sql::operation<name, Row, typename Left::node, typename decltype(right)::node>{} };
226226

227-
return TreeNode<right.pos, decltype(node)>{};
227+
return TreeNode<right.pos, std::remove_cvref_t<decltype(node)>>{};
228228
}
229229
}
230230

@@ -270,7 +270,7 @@ namespace sql
270270
constexpr cexpr::string<char, tokens_[pos].length() + 1> name{ tokens_[pos] };
271271
constexpr auto node{ sql::operation<name, Row, typename Left::node, typename decltype(right)::node>{} };
272272

273-
return recurse_logical<TreeNode<right.pos, decltype(node)>, Row>();
273+
return recurse_logical<TreeNode<right.pos, std::remove_cvref_t<decltype(node)>>, Row>();
274274
}
275275
}
276276

@@ -342,7 +342,7 @@ namespace sql
342342

343343
if constexpr (root.pos + 1 < tokens_.count() && (tokens_[root.pos] == "where" || tokens_[root.pos] == "WHERE"))
344344
{
345-
constexpr auto predicate{ parse_logical<root.pos + 1, typename decltype(root)::node::output_type>() };
345+
constexpr auto predicate{ parse_logical<root.pos + 1, std::remove_cvref_t<typename decltype(root)::node::output_type>>() };
346346

347347
return ra::selection<typename decltype(predicate)::node, typename decltype(root)::node>{};
348348
}

include/sql/tokens.hpp

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,21 @@ namespace sql
7777
last = curr;
7878
last = next(last, end);
7979

80-
tokens_[i++] = token_view{ curr, (std::size_t) last - (std::size_t) curr };
80+
if (*curr == '\"' || *curr == '\'')
81+
{
82+
tokens_[i++] = token_view{ curr, 1 };
83+
for (char c{ *curr++ }; last != end && *last != c; ++last);
84+
}
85+
86+
auto len{ reinterpret_cast<std::size_t>(last) - reinterpret_cast<std::size_t>(curr) };
87+
tokens_[i++] = token_view{ curr, len };
88+
89+
if (*last == '\"' || *last == '\'')
90+
{
91+
tokens_[i++] = token_view{ last, 1 };
92+
++last;
93+
}
94+
8195
curr = last;
8296
}
8397
}

resources/tests/compose.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,10 @@ def data(tokens):
3636
return cs, ts
3737

3838
def templ(query, ts):
39+
num = 0
40+
for t in ts:
41+
query = query.replace(t, "T" + str(num))
42+
num += 1
3943
templ_spec = "\t\t\"" + query + "\""
4044
for t in ts:
4145
templ_spec += ",\n\t\t" + t

resources/tests/runner.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,9 @@ def main():
1818
for line in stream:
1919
res = "Failed"
2020
print("Test " + str(num) + ":\t" + res + "\n\t" + query)
21+
if res == "Failed":
22+
exit()
23+
num += 1
2124

2225
if __name__ == "__main__":
2326
main()

resources/tests/test.cpp

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
#include <iostream>
2+
3+
#include "data.hpp"
4+
#include "sql/query.hpp"
5+
6+
using query =
7+
sql::query<
8+
"select genre, year, pages from T0 where genre != \"science fiction\" and NOT year > 1970 and not pages = 300",
9+
books
10+
>;
11+
12+
int main()
13+
{
14+
books t0{ sql::load<books, '\t'>(data_folder + books_data) };
15+
16+
for (query q{ t0 }; auto const& [c0, c1, c2] : q)
17+
{
18+
std::cout << c0 << '|' << c1 << '|' << c2 << '\n';
19+
}
20+
21+
return 0;
22+
}

0 commit comments

Comments
 (0)