Repository navigation
Expand file tree
/
Copy pathsql_dialect_tests.cpp
More file actions
344 lines (322 loc) · 15.3 KB
/
Copy pathsql_dialect_tests.cpp
File metadata and controls
344 lines (322 loc) · 15.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
#include <teaql/sql/dialect.hpp>
#include <teaql/runtime/context.hpp>
#include <iostream>
using namespace teaql;
static void check(bool ok, const char* message) {
if (!ok) {
throw std::runtime_error(message);
}
}
class TestDialect : public SqlDialect {
public:
DatabaseKind kind() const override {
return DatabaseKind::Sqlite;
}
String quote_ident(Memory r, std::string_view name) const override {
return quote_identifier_if_needed(r, name, '"');
}
String placeholder(Memory r, std::size_t) const override {
return String("?", r);
}
};
// The source trait deliberately exposes individual compiler stages for provider
// overrides. These tests ensure dispatch still occurs through those stages.
class HookDialect final : public TestDialect {
public:
mutable unsigned single_calls = 0, gbk_calls = 0, projection_calls = 0, aggregate_calls = 0,
order_calls = 0;
String compile_single_arg_function(Memory r,
const EntityDescriptor& e,
std::string_view fn,
std::span<const Expr> args,
SqlBindings& bindings) const override {
++single_calls;
return SqlDialect::compile_single_arg_function(r, e, fn, args, bindings);
}
String compile_gbk_function(Memory r,
const EntityDescriptor& e,
std::span<const Expr> args,
SqlBindings& bindings) const override {
++gbk_calls;
auto out = SqlDialect::compile_gbk_function(r, e, args, bindings);
out += " COLLATE fixture";
return out;
}
String select_projection(Memory r,
const EntityDescriptor& e,
const SelectQuery& q,
SqlBindings& bindings) const override {
++projection_calls;
return SqlDialect::select_projection(r, e, q, bindings);
}
String aggregate_projection(Memory r,
const EntityDescriptor& e,
const SelectQuery& q,
SqlBindings& bindings) const override {
++aggregate_calls;
return SqlDialect::aggregate_projection(r, e, q, bindings);
}
String resolve_order_field(Memory r,
const EntityDescriptor& e,
const OrderBy& order,
SqlBindings& bindings) const override {
++order_calls;
return SqlDialect::resolve_order_field(r, e, order, bindings);
}
std::string_view aggregate_function_sql(AggregateFunction fn) const override {
return fn == AggregateFunction::BitXor ? "fixture_bit_xor"
: SqlDialect::aggregate_function_sql(fn);
}
String schema_type_sql(Memory r, DataType type, const PropertyDescriptor& p) const override {
return type == DataType::Json ? String("JSON", r) : SqlDialect::schema_type_sql(r, type, p);
}
std::string_view fallback_default_value_sql(DataType type) const override {
return type == DataType::Json ? "'[]'" : SqlDialect::fallback_default_value_sql(type);
}
};
static void extension_points() {
GlobalContext global;
auto r = global.memory_resource();
auto& d = global.make_resource<HookDialect>();
const SqlDialect& api = d;
auto& e = global.make<EntityDescriptor>("Record");
e.property("name", DataType::Text);
e.property("payload", DataType::Json).not_null();
auto& q = global.make<SelectQuery>("Record");
q.project_expr("sound", Expr::soundex(Expr::column(r, "name")))
.project_expr("total", Expr::count_all(r))
.order_gbk_asc("name");
auto plain = api.compile_select(r, e, q);
check(d.single_calls == 0 && d.gbk_calls == 1 && d.projection_calls == 1 && d.order_calls == 1,
"SOUNDEX, zero-argument COUNT, GBK and order retain distinct dispatch");
check(plain.sql.find("ORDER BY name COLLATE fixture ASC") != String::npos,
"provider GBK override is used");
q.project_expr("n", Expr::sum_expr(Expr::column(r, "name")))
.aggregate(Aggregate::bit_xor(r, "name", "xor"));
auto aggregate = api.compile_select(r, e, q);
check(d.single_calls == 1 && d.aggregate_calls == 1 && d.projection_calls == 1,
"aggregate projection is independent of ordinary projection");
check(aggregate.sql.find("fixture_bit_xor(name) AS xor") != String::npos,
"aggregate naming override is used");
auto& payload = e.properties[1];
check(api.compile_add_column(r, e, payload) ==
"ALTER TABLE record_data ADD COLUMN payload JSON NOT NULL DEFAULT '[]'",
"schema type and fallback value are independent hooks");
check(!api.large_in_uses_array_param() && !api.prefers_small_parent_relation_probes() &&
api.schema_setup_sqls().empty(),
"default capability flags do not claim provider support");
}
static void scope_after_failures() {
GlobalContext global;
auto r = global.memory_resource();
auto& d = global.make_resource<TestDialect>();
auto& outer = global.make<EntityDescriptor>("Outer");
outer.property("token", DataType::Text, "access_token");
outer.mask_fields({});
auto& inner = global.make<EntityDescriptor>("Inner");
inner.property("public", DataType::Text);
inner.mask_fields({});
auto& q = global.make<SelectQuery>("Inner");
q.project_expr("first", Expr::value(r, "inner plain literal"));
q.filter(Expr::eq(r, "missing", "value"));
Expr subquery(r, Expr::Kind::SubQuery);
subquery.op = BinaryOp::In;
subquery.args.emplace_back(Expr::column(r, "token"));
subquery.subquery_data = detail::make_box<SubQueryExpr>(r, inner, q);
auto expr = Expr::binary(std::move(subquery), BinaryOp::Eq, Expr::column(r, "token"));
auto& bindings = global.make<SqlBindings>();
bool failed = false;
try {
(void)d.compile_expr(r, outer, expr, bindings);
} catch (const SqlCompileError& e) {
failed = e.code == SqlCompileError::Code::UnknownField;
}
check(failed, "subquery compilation reports structured inner error");
(void)d.compile_expr(r, outer, Expr::value(r, "after failure"), bindings);
auto policies = bindings.log_context(r, DatabaseKind::Sqlite).parameter_policies;
check(bindings.size() == 2 && policies[0] == SqlParameterLogPolicy::Unknown &&
policies[1] == SqlParameterLogPolicy::Unknown,
"inner scope isolation and outer scope restoration survive exceptions");
// Failure after binding must restore scope even when it comes from a
// provider hook instead of a SqlCompileError.
struct ThrowingDialect final : TestDialect {
String placeholder(Memory, std::size_t) const override {
throw std::bad_alloc{};
}
} failing;
try {
(void)failing.compile_expr(r, outer, Expr::eq(r, "token", "credential"), bindings);
} catch (const std::bad_alloc&) {
}
(void)d.compile_expr(r, outer, Expr::value(r, "outside"), bindings);
policies = bindings.log_context(r, DatabaseKind::Sqlite).parameter_policies;
check(policies[2] == SqlParameterLogPolicy::Credential &&
policies[3] == SqlParameterLogPolicy::Unknown,
"allocation failure does not leave credential scope active");
}
static void destination_lifetime() {
GlobalContext global;
auto r = global.memory_resource();
auto user = global.make_user_context();
auto u = user->memory_resource();
auto& d = global.make_resource<TestDialect>();
auto& model = user->make<EntityDescriptor>("Temporary");
model.property("payload", DataType::Json);
model.mask_fields({});
auto& q = user->make<SelectQuery>("Temporary");
{
Value::Object payload(u);
String large(4096, 'x', u);
payload.emplace(String("nested", u), Value(u, large));
q.filter(Expr::eq(u, "payload", Value(u, payload)))
.comment("temporary comment with an independently allocated buffer");
}
auto& retained = global.make<CompiledQuery>(d.compile_select(r, model, q));
auto& transient = user->make<CompiledQuery>(d.compile_select(u, model, q));
auto& cloned = global.make<CompiledQuery>(transient);
user.reset();
check(retained == cloned &&
retained.sql == "SELECT payload FROM temporary_data WHERE (payload = ?)",
"compiler destination and explicit deep copy outlive inputs");
const auto& object = std::get<Value::Object>(retained.params[0].data);
check(object.get_allocator().resource() == r && object.begin()->second.memory_resource() == r,
"nested binding objects use destination allocation domain");
check(std::get<String>(object.begin()->second.data).size() == 4096 &&
retained.comment->get_allocator().resource() == r,
"nested text and comments do not retain user memory");
}
static void schema_domains() {
GlobalContext global;
auto r = global.memory_resource();
auto user = global.make_user_context();
auto u = user->memory_resource();
auto& model = user->make<EntityDescriptor>("LongEntity");
model.table("straße_with_a_long_physical_table_name");
model.property("id", DataType::U64, "the_long_physical_identity_column").id();
model.property("version", DataType::I64, "the_long_physical_version_column").version();
model.property("ownerId", DataType::U64, "the_long_physical_owner_column");
SchemaIndexSpec* retained = nullptr;
SchemaIndexSpec* assigned = nullptr;
SqlCompileError* retained_error = nullptr;
{
auto specs = schema_index_specs(u, model, 31);
check(specs.size() == 2 && specs[0].unique && !specs[1].unique,
"version and property-derived indexes remain independent");
retained = &global.make<SchemaIndexSpec>(specs[0]);
assigned = &global.make<SchemaIndexSpec>();
*assigned = std::move(specs[1]);
check(assigned->memory_resource() == r &&
assigned->columns[0].get_allocator().resource() == r,
"cross-domain assignment deep copies nested index columns");
const EntityDescriptor* entities[] = {&model};
try {
validate_schema_identifier_lengths(u, entities, 16);
} catch (const SqlCompileError& e) {
retained_error = &global.make<SqlCompileError>(e);
}
}
user.reset();
check(retained->name.size() <= 31 &&
retained->table == "straße_with_a_long_physical_table_name" &&
retained->columns[0] == "the_long_physical_identity_column",
"generated names are bounded without changing physical model names");
check(retained_error && retained_error->identifier == retained->table &&
retained_error->actual_bytes == retained->table.size(),
"physical-name error carries original UTF-8 bytes in explicit destination lifetime");
check(assigned->columns[0] == "the_long_physical_owner_column",
"assigned nested columns survive source close");
const std::string_view invalid[] = {std::string_view("\xC0\x80", 2),
std::string_view("\xED\xA0\x80", 3),
std::string_view("\xF4\x90\x80\x80", 4),
std::string_view("\xF0\x9F", 2)};
for (auto bytes : invalid) {
bool rejected = false;
try {
(void)schema_foreign_key_name(r, bytes, "a", "b", "c", 63);
} catch (const std::invalid_argument&) {
rejected = true;
}
check(rejected, "C++ byte strings outside Rust UTF-8 domain are rejected");
}
}
static void unicode_quote_lifetime() {
GlobalContext global;
auto user = global.make_user_context();
auto owner = user->memory_resource();
Ref<String> retained;
{
auto quoted = quote_identifier_if_needed(owner, "has « space", U'«');
check(quoted == "«has «« space«" && quoted.get_allocator().resource() == owner,
"Unicode quote escaping preserves scalar boundaries and the destination allocator");
check(quote_identifier_if_needed(owner, "some\" quoted", U'"') ==
quote_identifier_if_needed(owner, "some\" quoted", '"'),
"ASCII quote overloads retain the same escaping contract");
check(quote_identifier_if_needed(owner, "\"already quoted\"", U'«') == "\"already quoted\"",
"existing wrapped identifiers remain unchanged for any quote scalar");
retained = user->make_global<String>(quoted);
}
for (auto code : {char32_t{0xd800}, char32_t{0x110000}}) {
bool rejected = false;
try {
(void)quote_identifier_if_needed(owner, "has space", code);
} catch (const std::invalid_argument&) {
rejected = true;
}
check(rejected, "C++ quote values outside the Rust char domain are rejected");
}
user.reset();
auto next = global.make_user_context();
auto rebound = next->rebind_global(retained);
check(next->get(rebound) == "«has «« space«",
"explicit Global copy of quoted text survives User deletion");
}
static void grouped_partition_validation() {
GlobalContext global;
auto user_context = global.make_user_context();
auto r = user_context->memory_resource();
auto& dialect = user_context->make_resource<TestDialect>();
auto& entity = user_context->make<EntityDescriptor>("Metric");
entity.property("id", DataType::U64).id();
entity.property("owner", DataType::U64);
entity.property("category", DataType::Text);
auto& query = user_context->make<SelectQuery>("Metric");
query.group_by("owner")
.group_by("category")
.count_field("id", "rows")
.partition_by("owner")
.page(1, 1)
.order_expr_desc(Expr::count_all(r))
.having(Expr::binary(Expr::count_all(r), BinaryOp::Gte, Expr::value(r, std::uint64_t(2))));
auto compiled = dialect.compile_select(r, entity, query);
check(compiled.sql.find("GROUP BY owner, category HAVING (COUNT(*) >= ?)) AS") != String::npos,
"grouping and HAVING are retained inside the ranked relation query");
check(compiled.params.size() == 1 && compiled.params[0] == Value(r, std::uint64_t(2)),
"ranked HAVING retains its binding");
query.group_by("missing");
bool rejected = false;
try {
(void)dialect.compile_select(r, entity, query);
} catch (const SqlCompileError& error) {
rejected = error.code == SqlCompileError::Code::UnknownField;
}
check(rejected, "partitioning does not bypass grouped-field validation");
}
int main() {
try {
auto* previous = std::pmr::set_default_resource(std::pmr::null_memory_resource());
extension_points();
scope_after_failures();
destination_lifetime();
schema_domains();
unicode_quote_lifetime();
grouped_partition_validation();
std::pmr::set_default_resource(previous);
std::cout << "SQL dialect contracts passed\n";
} catch (const SqlCompileError& e) {
std::cerr << "unexpected SQL compile error: " << unsigned(e.code) << '\n';
return 1;
} catch (const std::exception& e) {
std::cerr << e.what() << '\n';
return 1;
}
}