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
use sim_kernel::{Datum, Symbol};
use sim_relation_core::{
ColumnName, DomainId, IndexName, ProviderName, RelationId, RevisionName, SchemaName,
StorageRepr, TableName, ToRelationDatum,
};
use std::collections::BTreeSet;
/// A normalized provider-observed column.
#[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord)]
pub struct PhysicalColumn {
/// Observed column name.
pub name: ColumnName,
/// Normalized logical domain.
pub domain: DomainId,
/// Exact provider-boundary representation.
pub storage: StorageRepr,
/// Observed nullability.
pub nullable: bool,
/// Provider ordinal preserving semantic column order.
pub ordinal: u32,
}
/// A normalized provider-observed index.
#[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord)]
pub struct PhysicalIndex {
/// Observed index name.
pub name: IndexName,
/// Observed key columns in order.
pub columns: Vec<ColumnName>,
/// Observed uniqueness.
pub unique: bool,
}
/// A normalized provider-observed table.
#[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord)]
pub struct PhysicalTable {
/// Observed table name.
pub name: TableName,
/// Observed columns.
pub columns: Vec<PhysicalColumn>,
/// Observed indexes.
pub indexes: Vec<PhysicalIndex>,
}
/// Immutable normalized evidence observed from a live provider catalog.
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct PhysicalSchema {
provider: ProviderName,
schema: SchemaName,
revision: RevisionName,
tables: Vec<PhysicalTable>,
}
impl PhysicalSchema {
/// Normalizes an observed catalog. Tables/indexes are unordered; columns are sorted by provider ordinal.
pub fn normalize(
provider: ProviderName,
schema: SchemaName,
revision: RevisionName,
mut tables: Vec<PhysicalTable>,
) -> Result<Self, &'static str> {
let mut table_names = BTreeSet::new();
for table in &mut tables {
if !table_names.insert(table.name.clone()) {
return Err("duplicate physical table");
}
table.columns.sort_by_key(|v| v.ordinal);
if table
.columns
.windows(2)
.any(|v| v[0].ordinal == v[1].ordinal)
{
return Err("duplicate physical ordinal");
}
let mut names = BTreeSet::new();
if table.columns.iter().any(|v| !names.insert(v.name.clone())) {
return Err("duplicate physical column");
}
table.indexes.sort_by(|a, b| a.name.cmp(&b.name));
}
tables.sort_by(|a, b| a.name.cmp(&b.name));
Ok(Self {
provider,
schema,
revision,
tables,
})
}
/// Returns normalized tables.
pub fn tables(&self) -> &[PhysicalTable] {
&self.tables
}
/// Returns the distinct physical identity.
pub fn id(&self) -> Result<RelationId, sim_kernel::Error> {
RelationId::of(self)
}
}
fn storage(v: StorageRepr) -> Symbol {
Symbol::new(match v {
StorageRepr::Bool => "bool",
StorageRepr::I64 => "i64",
StorageRepr::F64 => "f64",
StorageRepr::Text => "text",
StorageRepr::Bytes => "bytes",
})
}
impl ToRelationDatum for PhysicalSchema {
fn to_datum(&self) -> Datum {
Datum::Node {
tag: Symbol::qualified("relation-schema", "physical-schema"),
fields: vec![
(
Symbol::new("provider"),
Datum::Symbol(self.provider.symbol().clone()),
),
(
Symbol::new("schema"),
Datum::Symbol(self.schema.symbol().clone()),
),
(
Symbol::new("revision"),
Datum::Symbol(self.revision.symbol().clone()),
),
(
Symbol::new("tables"),
Datum::Vector(
self.tables
.iter()
.map(|t| Datum::Node {
tag: Symbol::qualified("relation-schema", "physical-table"),
fields: vec![
(Symbol::new("name"), Datum::Symbol(t.name.symbol().clone())),
(
Symbol::new("columns"),
Datum::Vector(
t.columns
.iter()
.map(|c| Datum::Node {
tag: Symbol::qualified(
"relation-schema",
"physical-column",
),
fields: vec![
(
Symbol::new("name"),
Datum::Symbol(c.name.symbol().clone()),
),
(
Symbol::new("domain"),
Datum::Symbol(
c.domain.symbol().clone(),
),
),
(
Symbol::new("storage"),
Datum::Symbol(storage(c.storage)),
),
(
Symbol::new("nullable"),
Datum::Bool(c.nullable),
),
(
Symbol::new("ordinal"),
Datum::Number(
sim_kernel::NumberLiteral {
domain: Symbol::qualified(
"core", "u32",
),
canonical: c
.ordinal
.to_string(),
},
),
),
],
})
.collect(),
),
),
(
Symbol::new("indexes"),
Datum::Vector(
t.indexes
.iter()
.map(|i| Datum::Node {
tag: Symbol::qualified(
"relation-schema",
"physical-index",
),
fields: vec![
(
Symbol::new("name"),
Datum::Symbol(i.name.symbol().clone()),
),
(
Symbol::new("columns"),
Datum::Vector(
i.columns
.iter()
.map(|n| {
Datum::Symbol(
n.symbol().clone(),
)
})
.collect(),
),
),
(
Symbol::new("unique"),
Datum::Bool(i.unique),
),
],
})
.collect(),
),
),
],
})
.collect(),
),
),
],
}
}
}