|
1 | 1 | // © Copyright 2025, 2026 Query Farm LLC - https://query.farm |
2 | 2 | #pragma once |
3 | 3 |
|
| 4 | +#include <memory> |
| 5 | +#include <optional> |
4 | 6 | #include <stdexcept> |
5 | 7 | #include <string> |
| 8 | +#include <utility> |
6 | 9 | #include <vector> |
7 | 10 |
|
8 | | -#include <arrow/array/array_nested.h> |
| 11 | +#include <arrow/array.h> |
| 12 | +#include <arrow/type.h> |
9 | 13 |
|
10 | | -#include <vgi/generated/vgi_protocol_schemas.hpp> |
11 | | - |
12 | | -#include "../src/wire.h" |
| 14 | +#include <vgi/constraints.h> |
13 | 15 |
|
14 | 16 | namespace example { |
15 | 17 | namespace constraint_fixtures { |
16 | 18 |
|
17 | | -namespace gen = ::vgi::generated; |
18 | | -using vgi::wire::encode_ipc; |
19 | | -using vgi::wire::ResultBuilder; |
20 | | - |
21 | | -// Fixed provider keys, not engine OIDs. |
22 | | -inline std::string ref(const std::string& table, const std::string& key) { |
23 | | - return encode_ipc(ResultBuilder(gen::CatalogObjectRefSchema()) |
24 | | - .set_string("kind", "constraint") |
25 | | - .set_string_list("schema_path", {"data"}) |
26 | | - .set_string("name", key) |
27 | | - .set_string("owner_table", table) |
28 | | - .finish()); |
29 | | -} |
30 | | - |
31 | | -inline std::string department_foreign_key() { |
32 | | - auto table = ResultBuilder(gen::CatalogObjectRefSchema()) |
33 | | - .set_string("kind", "table") |
34 | | - .set_string_list("schema_path", {"data"}) |
35 | | - .set_string("name", "departments") |
36 | | - .set_null("owner_table") |
37 | | - .finish(); |
38 | | - return encode_ipc(ResultBuilder(gen::ForeignKeyDefinitionSchema()) |
39 | | - .set_binary("referenced_table", encode_ipc(table)) |
40 | | - .set_string_list_list("referenced_column_paths", {{"id"}}) |
41 | | - .set_enum("match", "SIMPLE") |
42 | | - .set_enum("on_update", "NO_ACTION") |
43 | | - .set_enum("on_delete", "NO_ACTION") |
44 | | - .finish()); |
45 | | -} |
| 19 | +// The one CHECK the fixtures publish, kept for validate_rows to compare. |
| 20 | +inline constexpr const char* kBudgetCheck = "budget >= 0"; |
46 | 21 |
|
47 | | -inline std::string budget_check() { |
48 | | - auto context = ResultBuilder(gen::QueryEvaluationContextSchema()) |
49 | | - .set_string("timezone", "UTC") |
50 | | - .set_string("collation", "binary") |
51 | | - .set_string("default_order", "asc") |
52 | | - .set_string("default_null_order", "nulls_last") |
53 | | - .set_bool("preserve_insertion_order", true) |
54 | | - .finish(); |
55 | | - auto context_struct = arrow::StructArray::Make(context->columns(), context->schema()->fields()); |
56 | | - if (!context_struct.ok()) throw std::runtime_error(context_struct.status().ToString()); |
57 | | - return encode_ipc(ResultBuilder(gen::SqlExpressionSchema()) |
58 | | - .set_string("sql", "budget >= 0") |
59 | | - .set_string("profile", "vgi.expression.duckdb.scalar.v1") |
60 | | - .set_string_list("schema_path", {"data"}) |
61 | | - .set_array("evaluation_context", context_struct.MoveValueUnsafe()) |
62 | | - .finish()); |
| 22 | +// The departments primary key, which both foreign keys point at. |
| 23 | +inline vgi::ConstraintReference departments_key() { |
| 24 | + return {{"data"}, "departments", {{"id"}}, "fixture:departments:primary"}; |
63 | 25 | } |
64 | 26 |
|
| 27 | +// Fixed provider keys, not engine OIDs. Every fixture fact is attested |
| 28 | +// ENFORCED + ALL_ROWS: validate_rows() proves it against the immutable rows. |
65 | 29 | inline std::string constraint(const std::string& table, const std::string& key, |
66 | | - const std::string& kind, |
67 | | - const std::vector<std::vector<std::string>>& paths, |
68 | | - const std::string& check = {}, const std::string& foreign = {}) { |
69 | | - auto definition = ResultBuilder(gen::ConstraintDefinitionSchema()); |
70 | | - definition.set_enum("kind", kind) |
71 | | - .set_string_list_list("column_paths", paths) |
72 | | - .set_null("declared_name"); |
73 | | - if (check.empty()) |
74 | | - definition.set_null("check_expression"); |
75 | | - else |
76 | | - definition.set_binary("check_expression", check); |
77 | | - if (foreign.empty()) |
78 | | - definition.set_null("foreign_key"); |
79 | | - else |
80 | | - definition.set_binary("foreign_key", foreign); |
81 | | - auto info = ResultBuilder(gen::ConstraintInfoSchema()); |
82 | | - info.set_binary("ref", ref(table, key)) |
83 | | - .set_binary("definition", encode_ipc(definition.finish())) |
84 | | - .set_enum("enforcement", "ENFORCED") |
85 | | - .set_enum("validation", "ALL_ROWS"); |
86 | | - if (foreign.empty()) |
87 | | - info.set_null("referenced_constraint"); |
88 | | - else |
89 | | - info.set_binary("referenced_constraint", ref("departments", "fixture:departments:primary")); |
90 | | - return encode_ipc(info.finish()); |
| 30 | + vgi::ConstraintKind kind, std::vector<vgi::ColumnPath> paths, |
| 31 | + std::optional<std::string> check = std::nullopt, |
| 32 | + std::optional<vgi::ConstraintReference> foreign = std::nullopt) { |
| 33 | + vgi::ConstraintRecordSpec spec; |
| 34 | + spec.schema_path = {"data"}; |
| 35 | + spec.table = table; |
| 36 | + spec.id = key; |
| 37 | + spec.kind = kind; |
| 38 | + spec.columns = std::move(paths); |
| 39 | + spec.enforcement = vgi::ConstraintEnforcement::Enforced; |
| 40 | + spec.validation = vgi::ConstraintValidation::AllRows; |
| 41 | + spec.check_sql = std::move(check); |
| 42 | + spec.references = std::move(foreign); |
| 43 | + return vgi::constraint_record(spec); |
91 | 44 | } |
92 | 45 |
|
93 | 46 | // Private fixture definitions. records() validates the immutable scan arrays |
94 | 47 | // before publishing these trusted facts; there is no writable table endpoint. |
95 | 48 | inline std::vector<std::string> definitions(const std::string& table, int64_t version = 0) { |
96 | 49 | if (table == "departments") { |
97 | 50 | return { |
98 | | - constraint(table, "fixture:departments:nn:id", "NOT_NULL", {{"id"}}), |
99 | | - constraint(table, "fixture:departments:nn:name", "NOT_NULL", {{"name"}}), |
100 | | - constraint(table, "fixture:departments:primary", "PRIMARY_KEY", {{"id"}}), |
101 | | - constraint(table, "fixture:departments:unique:name", "UNIQUE", {{"name"}}), |
102 | | - constraint(table, "fixture:departments:check:budget", "CHECK", {}, budget_check()), |
| 51 | + constraint(table, "fixture:departments:nn:id", vgi::ConstraintKind::NotNull, {{"id"}}), |
| 52 | + constraint(table, "fixture:departments:nn:name", vgi::ConstraintKind::NotNull, |
| 53 | + {{"name"}}), |
| 54 | + constraint(table, "fixture:departments:primary", vgi::ConstraintKind::PrimaryKey, |
| 55 | + {{"id"}}), |
| 56 | + constraint(table, "fixture:departments:unique:name", vgi::ConstraintKind::Unique, |
| 57 | + {{"name"}}), |
| 58 | + constraint(table, "fixture:departments:check:budget", vgi::ConstraintKind::Check, {}, |
| 59 | + kBudgetCheck), |
103 | 60 | }; |
104 | 61 | } |
105 | 62 | if (table == "products") { |
106 | 63 | return { |
107 | | - constraint(table, "fixture:products:nn:id", "NOT_NULL", {{"id"}}), |
108 | | - constraint(table, "fixture:products:primary", "PRIMARY_KEY", {{"id"}}), |
| 64 | + constraint(table, "fixture:products:nn:id", vgi::ConstraintKind::NotNull, {{"id"}}), |
| 65 | + constraint(table, "fixture:products:primary", vgi::ConstraintKind::PrimaryKey, |
| 66 | + {{"id"}}), |
109 | 67 | }; |
110 | 68 | } |
111 | 69 | if (table == "employees") { |
112 | 70 | return { |
113 | | - constraint(table, "fixture:employees:nn:id", "NOT_NULL", {{"id"}}), |
114 | | - constraint(table, "fixture:employees:nn:name", "NOT_NULL", {{"name"}}), |
115 | | - constraint(table, "fixture:employees:nn:email", "NOT_NULL", {{"email"}}), |
116 | | - constraint(table, "fixture:employees:primary", "PRIMARY_KEY", {{"id"}}), |
117 | | - constraint(table, "fixture:employees:unique:email", "UNIQUE", {{"email"}}), |
118 | | - constraint(table, "fixture:employees:fk:department", "FOREIGN_KEY", {{"department_id"}}, |
119 | | - {}, department_foreign_key()), |
| 71 | + constraint(table, "fixture:employees:nn:id", vgi::ConstraintKind::NotNull, {{"id"}}), |
| 72 | + constraint(table, "fixture:employees:nn:name", vgi::ConstraintKind::NotNull, |
| 73 | + {{"name"}}), |
| 74 | + constraint(table, "fixture:employees:nn:email", vgi::ConstraintKind::NotNull, |
| 75 | + {{"email"}}), |
| 76 | + constraint(table, "fixture:employees:primary", vgi::ConstraintKind::PrimaryKey, |
| 77 | + {{"id"}}), |
| 78 | + constraint(table, "fixture:employees:unique:email", vgi::ConstraintKind::Unique, |
| 79 | + {{"email"}}), |
| 80 | + constraint(table, "fixture:employees:fk:department", vgi::ConstraintKind::ForeignKey, |
| 81 | + {{"department_id"}}, std::nullopt, departments_key()), |
120 | 82 | }; |
121 | 83 | } |
122 | 84 | if (table == "projects") { |
123 | 85 | return { |
124 | | - constraint(table, "fixture:projects:nn:department", "NOT_NULL", {{"department_id"}}), |
125 | | - constraint(table, "fixture:projects:nn:code", "NOT_NULL", {{"project_code"}}), |
126 | | - constraint(table, "fixture:projects:nn:title", "NOT_NULL", {{"title"}}), |
127 | | - constraint(table, "fixture:projects:primary", "PRIMARY_KEY", |
| 86 | + constraint(table, "fixture:projects:nn:department", vgi::ConstraintKind::NotNull, |
| 87 | + {{"department_id"}}), |
| 88 | + constraint(table, "fixture:projects:nn:code", vgi::ConstraintKind::NotNull, |
| 89 | + {{"project_code"}}), |
| 90 | + constraint(table, "fixture:projects:nn:title", vgi::ConstraintKind::NotNull, |
| 91 | + {{"title"}}), |
| 92 | + constraint(table, "fixture:projects:primary", vgi::ConstraintKind::PrimaryKey, |
128 | 93 | {{"department_id"}, {"project_code"}}), |
129 | | - constraint(table, "fixture:projects:fk:department", "FOREIGN_KEY", {{"department_id"}}, |
130 | | - {}, department_foreign_key()), |
| 94 | + constraint(table, "fixture:projects:fk:department", vgi::ConstraintKind::ForeignKey, |
| 95 | + {{"department_id"}}, std::nullopt, departments_key()), |
131 | 96 | }; |
132 | 97 | } |
133 | 98 | if (table == "versioned_constraints") { |
134 | 99 | if (version < 1 || version > 3) |
135 | 100 | throw std::invalid_argument("unknown constraint fixture version"); |
136 | 101 | std::vector<std::string> result = { |
137 | | - constraint(table, "fixture:versioned_constraints:nn:id", "NOT_NULL", {{"id"}}), |
| 102 | + constraint(table, "fixture:versioned_constraints:nn:id", vgi::ConstraintKind::NotNull, |
| 103 | + {{"id"}}), |
138 | 104 | }; |
139 | 105 | if (version >= 2) { |
140 | | - result.push_back( |
141 | | - constraint(table, "fixture:versioned_constraints:nn:name", "NOT_NULL", {{"name"}})); |
| 106 | + result.push_back(constraint(table, "fixture:versioned_constraints:nn:name", |
| 107 | + vgi::ConstraintKind::NotNull, {{"name"}})); |
142 | 108 | result.push_back(constraint(table, "fixture:versioned_constraints:primary", |
143 | | - "PRIMARY_KEY", {{"id"}})); |
| 109 | + vgi::ConstraintKind::PrimaryKey, {{"id"}})); |
144 | 110 | result.push_back(constraint(table, "fixture:versioned_constraints:unique:email", |
145 | | - "UNIQUE", {{"email"}})); |
| 111 | + vgi::ConstraintKind::Unique, {{"email"}})); |
146 | 112 | } |
147 | 113 | if (version >= 3) |
148 | 114 | result.push_back(constraint(table, "fixture:versioned_constraints:fk:department", |
149 | | - "FOREIGN_KEY", {{"department_id"}}, {}, |
150 | | - department_foreign_key())); |
| 115 | + vgi::ConstraintKind::ForeignKey, {{"department_id"}}, |
| 116 | + std::nullopt, departments_key())); |
151 | 117 | return result; |
152 | 118 | } |
153 | 119 | throw std::invalid_argument("unknown constraint fixture table: " + table); |
|
0 commit comments