Skip to content

Commit 57457df

Browse files
rustyconoverclaude
andcommitted
Canonical constraint records: public builder, automatic read profiles, serve-time checks
vgi.v3 TableInfo.constraints carries canonical ConstraintInfo IPC records, and the DuckDB extension refuses ATTACH unless (a) the constraint read profile was negotiated, (b) every record is ENFORCED + ALL_ROWS, and (c) Arrow nullability equals the NOT_NULL/PRIMARY_KEY column-path set for every table, descending through ordinary STRUCTs. - include/vgi/constraints.h (new, public): ConstraintRecordSpec / constraint_record() with explicit, defaultless enforcement + validation; ConstraintRecordView / read_constraint_record(); validate_constraint_records(); CONSTRAINT_RECORDS_READ / SCALAR_EXPRESSION_READ. Mirrors vgi-typescript. - ATTACH offers CONSTRAINT_RECORDS_READ + SCALAR_EXPRESSION_READ whenever a table the attachment serves (including time-travel snapshots) carries records, merged with any explicit catalog_read_profiles; the scalar profile is still selected only alongside records. - Every served table is checked against rules (b) and (c), naming the table and column path; legacy descriptor fields stay refused, now naming vgi::constraint_record() as the replacement. - The example worker builds its fixtures through the public API instead of the internal src/wire.h, and drops its explicit profile list. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
1 parent a75108c commit 57457df

11 files changed

Lines changed: 1104 additions & 168 deletions

‎CMakeLists.txt‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@ add_library(vgi
7575
src/attach_ticket.cpp
7676
src/attach_ticket_service.cpp
7777
src/catalog.cpp
78+
src/constraints.cpp
7879
src/catalog_contents.cpp
7980
src/memory_catalog.cpp
8081
src/arg_schema.cpp

‎example-worker/constraint_fixtures.cpp‎

Lines changed: 26 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -34,15 +34,12 @@ const arrow::Array& column(const Columns& columns, std::string_view name) {
3434
throw std::invalid_argument("constraint fixture missing column: " + std::string(name));
3535
}
3636

37-
std::vector<std::string> column_names(const std::shared_ptr<arrow::RecordBatch>& definition,
38-
const std::string& field = "column_paths") {
39-
auto paths = std::static_pointer_cast<arrow::ListArray>(vgi::wire::column(definition, field));
40-
auto components = std::static_pointer_cast<arrow::ListArray>(paths->value_slice(0));
37+
// The fixtures key only on top-level columns.
38+
std::vector<std::string> column_names(const std::vector<vgi::ColumnPath>& paths) {
4139
std::vector<std::string> names;
42-
for (int64_t i = 0; i < components->length(); ++i) {
43-
auto path = std::static_pointer_cast<arrow::StringArray>(components->value_slice(i));
44-
if (path->length() != 1) throw std::invalid_argument("fixture needs top-level key");
45-
names.push_back(path->GetString(0));
40+
for (const auto& path : paths) {
41+
if (path.size() != 1) throw std::invalid_argument("fixture needs top-level key");
42+
names.push_back(path.front());
4643
}
4744
return names;
4845
}
@@ -109,34 +106,32 @@ void validate_rows(const std::string& table, const Columns& columns, int64_t ver
109106
// Validate every published definition, refusing a new unsupported kind
110107
// instead of silently trusting it. Keys use typed values, not SQL text.
111108
for (const auto& bytes : definitions(table, version)) {
112-
const auto info = vgi::wire::decode_ipc(bytes);
113-
const auto definition = vgi::wire::get_ipc(info, "definition");
114-
const auto kind = vgi::wire::get_enum(definition, "kind");
115-
const auto names = column_names(definition);
116-
if (kind == "NOT_NULL") {
109+
const auto record = vgi::read_constraint_record(bytes);
110+
const auto kind = record.kind;
111+
const std::string kind_name = vgi::to_string(kind);
112+
const auto names = record.kind == vgi::ConstraintKind::Check ? std::vector<std::string>{}
113+
: column_names(record.columns);
114+
if (kind == vgi::ConstraintKind::NotNull) {
117115
for (const auto& name : names)
118116
if (column(columns, name).null_count() != 0)
119117
throw std::invalid_argument("fixture NOT NULL violated: " + name);
120-
} else if (kind == "PRIMARY_KEY" || kind == "UNIQUE") {
118+
} else if (kind == vgi::ConstraintKind::PrimaryKey || kind == vgi::ConstraintKind::Unique) {
121119
std::set<Key> keys;
122120
for (int64_t row = 0; row < size; ++row) {
123121
auto value = key(columns, names, row);
124-
if (!value && kind == "PRIMARY_KEY")
122+
if (!value && kind == vgi::ConstraintKind::PrimaryKey)
125123
throw std::invalid_argument("fixture PRIMARY KEY contains NULL");
126124
if (value && !keys.insert(*value).second)
127-
throw std::invalid_argument("fixture " + kind + " contains duplicate key");
125+
throw std::invalid_argument("fixture " + kind_name + " contains duplicate key");
128126
}
129-
} else if (kind == "FOREIGN_KEY") {
130-
const auto foreign = vgi::wire::get_ipc(definition, "foreign_key");
131-
const auto target = vgi::wire::get_ipc(foreign, "referenced_table");
132-
if (vgi::wire::get_string(target, "name") != "departments" ||
133-
vgi::wire::get_string_list(target, "schema_path") !=
134-
std::vector<std::string>{"data"} ||
135-
vgi::wire::get_enum(foreign, "match") != "SIMPLE")
127+
} else if (kind == vgi::ConstraintKind::ForeignKey) {
128+
const auto& target = record.references;
129+
if (!target || target->table != "departments" ||
130+
target->schema_path != std::vector<std::string>{"data"})
136131
throw std::invalid_argument("unsupported fixture foreign key");
137132
const auto referenced = rows("departments");
138133
validate_rows("departments", referenced);
139-
const auto target_names = column_names(foreign, "referenced_column_paths");
134+
const auto target_names = column_names(target->columns);
140135
if (target_names != std::vector<std::string>{"id"} || names.size() != 1)
141136
throw std::invalid_argument("unsupported fixture referenced key");
142137
std::set<Key> keys;
@@ -147,8 +142,8 @@ void validate_rows(const std::string& table, const Columns& columns, int64_t ver
147142
if (value && !keys.contains(*value))
148143
throw std::invalid_argument("fixture FOREIGN KEY missing referenced row");
149144
}
150-
} else if (kind == "CHECK") {
151-
if (vgi::wire::get_binary(definition, "check_expression") != budget_check())
145+
} else if (kind == vgi::ConstraintKind::Check) {
146+
if (record.check_sql != kBudgetCheck)
152147
throw std::invalid_argument("unsupported fixture CHECK expression");
153148
const auto& array = column(columns, "budget");
154149
if (array.type_id() != arrow::Type::DOUBLE)
@@ -158,7 +153,7 @@ void validate_rows(const std::string& table, const Columns& columns, int64_t ver
158153
!(static_cast<const arrow::DoubleArray&>(array).Value(row) >= 0))
159154
throw std::invalid_argument("fixture CHECK budget >= 0 violated");
160155
} else {
161-
throw std::invalid_argument("unsupported fixture constraint kind: " + kind);
156+
throw std::invalid_argument("unsupported fixture constraint kind: " + kind_name);
162157
}
163158
}
164159
}
@@ -167,10 +162,10 @@ std::shared_ptr<arrow::Schema> required_schema(const std::shared_ptr<arrow::Sche
167162
const std::string& table, int64_t version) {
168163
std::set<std::string> required;
169164
for (const auto& bytes : definitions(table, version)) {
170-
auto definition = vgi::wire::get_ipc(vgi::wire::decode_ipc(bytes), "definition");
171-
auto kind = vgi::wire::get_enum(definition, "kind");
172-
if (kind == "NOT_NULL" || kind == "PRIMARY_KEY")
173-
for (const auto& name : column_names(definition)) required.insert(name);
165+
const auto record = vgi::read_constraint_record(bytes);
166+
if (record.kind == vgi::ConstraintKind::NotNull ||
167+
record.kind == vgi::ConstraintKind::PrimaryKey)
168+
for (const auto& name : column_names(record.columns)) required.insert(name);
174169
}
175170
auto fields = schema->fields();
176171
for (auto& field : fields)

‎example-worker/constraint_fixtures.h‎

Lines changed: 67 additions & 101 deletions
Original file line numberDiff line numberDiff line change
@@ -1,153 +1,119 @@
11
// © Copyright 2025, 2026 Query Farm LLC - https://query.farm
22
#pragma once
33

4+
#include <memory>
5+
#include <optional>
46
#include <stdexcept>
57
#include <string>
8+
#include <utility>
69
#include <vector>
710

8-
#include <arrow/array/array_nested.h>
11+
#include <arrow/array.h>
12+
#include <arrow/type.h>
913

10-
#include <vgi/generated/vgi_protocol_schemas.hpp>
11-
12-
#include "../src/wire.h"
14+
#include <vgi/constraints.h>
1315

1416
namespace example {
1517
namespace constraint_fixtures {
1618

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";
4621

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"};
6325
}
6426

27+
// Fixed provider keys, not engine OIDs. Every fixture fact is attested
28+
// ENFORCED + ALL_ROWS: validate_rows() proves it against the immutable rows.
6529
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);
9144
}
9245

9346
// Private fixture definitions. records() validates the immutable scan arrays
9447
// before publishing these trusted facts; there is no writable table endpoint.
9548
inline std::vector<std::string> definitions(const std::string& table, int64_t version = 0) {
9649
if (table == "departments") {
9750
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),
10360
};
10461
}
10562
if (table == "products") {
10663
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"}}),
10967
};
11068
}
11169
if (table == "employees") {
11270
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()),
12082
};
12183
}
12284
if (table == "projects") {
12385
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,
12893
{{"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()),
13196
};
13297
}
13398
if (table == "versioned_constraints") {
13499
if (version < 1 || version > 3)
135100
throw std::invalid_argument("unknown constraint fixture version");
136101
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"}}),
138104
};
139105
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"}}));
142108
result.push_back(constraint(table, "fixture:versioned_constraints:primary",
143-
"PRIMARY_KEY", {{"id"}}));
109+
vgi::ConstraintKind::PrimaryKey, {{"id"}}));
144110
result.push_back(constraint(table, "fixture:versioned_constraints:unique:email",
145-
"UNIQUE", {{"email"}}));
111+
vgi::ConstraintKind::Unique, {{"email"}}));
146112
}
147113
if (version >= 3)
148114
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()));
151117
return result;
152118
}
153119
throw std::invalid_argument("unknown constraint fixture table: " + table);

‎example-worker/main.cpp‎

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,8 @@ int main(int argc, char** argv) {
2323
// wrapper that names one of the other fixtures serves that one alone, and
2424
// its discovery answer has to list exactly it.
2525
const bool composite = catalog.name == "example";
26-
if (composite) {
27-
catalog.catalog_read_profiles = {"vgi.constraint.records.read.v1",
28-
"vgi.expression.duckdb.scalar.v1"};
29-
}
26+
// No catalog_read_profiles: the constraint tables' records make ATTACH
27+
// offer the constraint read profiles on their own.
3028
const std::string catalog_name = catalog.name;
3129
if (catalog.name == "versioned") {
3230
// The versioned fixture: three data versions, one implementation, and

0 commit comments

Comments
 (0)