hawdb-sql 0.13.0

An embedded Rust graph database for local data-plane workloads
Documentation
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
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
// Copyright 2026 Nowledge
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

use super::*;

pub(super) fn manifest(corpus: &Corpus) -> Check {
    let manifest = &corpus.manifest;
    if manifest.protocol != "hawdb-sql-frontend-corpus-v1"
        || manifest.audited_base_revision.len() != 40
        || !manifest
            .audited_base_revision
            .bytes()
            .all(|byte| byte.is_ascii_hexdigit())
        || manifest.case_count != corpus.cases.len()
        || corpus.cases.is_empty()
    {
        return Err("invalid corpus identity or case count".into());
    }
    let cases: BTreeMap<_, _> = corpus
        .cases
        .iter()
        .map(|case| (case.id.as_str(), case))
        .collect();
    if cases.len() != corpus.cases.len() {
        return Err("duplicate corpus case ID".into());
    }
    coverage(corpus)?;
    let waivers: BTreeMap<_, _> = manifest
        .waivers
        .iter()
        .map(|waiver| (waiver.id.as_str(), waiver))
        .collect();
    if waivers.len() != manifest.waivers.len() {
        return Err("duplicate waiver ID".into());
    }
    let mut referenced = BTreeSet::new();
    for case in &corpus.cases {
        if case.sql.is_empty()
            || case.source.symbol.is_empty()
            || case.source.adaptation.is_empty()
            || (case.source.adaptation.starts_with("verbatim") && case.source.line == 0)
        {
            return Err(format!("{}: incomplete source provenance", case.id));
        }
        let diverges = case.production.accepts() != case.owned.accepts();
        if diverges != case.waiver.is_some() {
            return Err(format!("{}: missing or stale waiver", case.id));
        }
        if let Some(id) = &case.waiver {
            let waiver = waivers
                .get(id.as_str())
                .ok_or_else(|| format!("{}: unknown waiver {id}", case.id))?;
            let direction = if case.production.accepts() {
                "upstream_accepts"
            } else {
                "owned_accepts"
            };
            if waiver.direction != direction
                || !waiver.families.contains(&case.family)
                || !waiver
                    .issue
                    .starts_with("https://github.com/nowledge-co/hawdb/issues/")
                || waiver.reason.len() < 40
            {
                return Err(format!("{}: waiver scope does not match", case.id));
            }
            referenced.insert(id.as_str());
        }
    }
    if referenced != waivers.keys().copied().collect() {
        return Err("unreferenced waiver".into());
    }

    let mut inventoried = BTreeSet::new();
    let mut source_paths = BTreeSet::new();
    for source in &manifest.sources {
        if !source_paths.insert(source.path.as_str()) || source.case_ids.is_empty() {
            return Err("duplicate or empty source inventory".into());
        }
        let bytes =
            source_bytes(&source.path).ok_or_else(|| format!("unknown source {}", source.path))?;
        if crate::test_support::sha256_hex(bytes) != source.sha256 {
            return Err(format!("source inventory needs review: {}", source.path));
        }
        for id in &source.case_ids {
            let case = cases
                .get(id.as_str())
                .ok_or_else(|| format!("missing inventoried case {id}"))?;
            if case.source.path != source.path || !inventoried.insert(id.as_str()) {
                return Err(format!("{id}: mismatched or repeated source inventory"));
            }
        }
    }
    if source_paths != SOURCE_FILES.iter().map(|(path, _)| *path).collect() {
        return Err("incomplete source inventory".into());
    }
    for id in &manifest.local_case_ids {
        let case = cases
            .get(id.as_str())
            .ok_or_else(|| format!("missing local case {id}"))?;
        if case.source.path != "docs/SQL_FRONTEND_CORPUS.md" || !inventoried.insert(id.as_str()) {
            return Err(format!("{id}: invalid local counterpart provenance"));
        }
    }
    if inventoried != cases.keys().copied().collect() {
        return Err("uninventoried corpus case".into());
    }
    Ok(())
}

fn coverage(corpus: &Corpus) -> Check {
    let families = [
        "select",
        "select_graph_table",
        "create_property_graph",
        "create_table",
        "create_index",
        "alter_table",
        "insert",
        "update",
        "delete",
        "explain",
    ];
    if corpus
        .cases
        .iter()
        .any(|case| !families.contains(&case.family.as_str()))
    {
        return Err("unclassified statement family".into());
    }
    let expected = BTreeSet::from(["select", "select_graph_table", "create_property_graph"]);
    if corpus
        .manifest
        .owned_families
        .iter()
        .map(String::as_str)
        .collect::<BTreeSet<_>>()
        != expected
        || corpus.manifest.owned_families.len() != expected.len()
    {
        return Err("owned family declaration is incomplete".into());
    }
    for family in expected {
        for accepts in [true, false] {
            if !corpus
                .cases
                .iter()
                .any(|case| case.family == family && case.owned.accepts() == accepts)
            {
                return Err(format!(
                    "missing owned family coverage: {family}, accepts={accepts}"
                ));
            }
        }
    }
    Ok(())
}

pub(super) fn frozen_inputs(corpus: &Corpus) -> Check {
    let workload: serde_json::Value =
        serde_json::from_str(WORKLOAD).map_err(|error| error.to_string())?;
    let statements = workload["statements"]
        .as_array()
        .ok_or("missing frozen statements")?;
    let workload_cases: Vec<_> = corpus
        .cases
        .iter()
        .filter(|case| case.source.adaptation == "frozen_workload")
        .collect();
    if workload_cases.len() != statements.len() {
        return Err("incomplete frozen workload inventory".into());
    }
    for statement in statements {
        let name = statement["name"]
            .as_str()
            .ok_or("missing frozen statement name")?;
        let matched: Vec<_> = workload_cases
            .iter()
            .filter(|case| case.source.reference.as_deref() == Some(name))
            .collect();
        if matched.len() != 1
            || matched[0].sql != statement["sql"].as_str().ok_or("missing frozen SQL")?
        {
            return Err(format!("frozen SQL mismatch: {name}"));
        }
        let Outcome::Accept { parameters } = &matched[0].production else {
            return Err(format!("frozen production statement must prepare: {name}"));
        };
        let expected = statement["parameters"]
            .as_array()
            .ok_or("missing frozen parameters")?;
        if parameters.len() != expected.len() || parameters.iter().copied().ne(1..=expected.len()) {
            return Err(format!("frozen parameter contract mismatch: {name}"));
        }
    }
    let lines: Vec<_> = SCHEMA
        .lines()
        .enumerate()
        .filter(|(_, line)| !line.trim().is_empty())
        .collect();
    let schema_cases: Vec<_> = corpus
        .cases
        .iter()
        .filter(|case| case.source.adaptation == "frozen_schema")
        .collect();
    if schema_cases.len() != lines.len() {
        return Err("incomplete frozen schema inventory".into());
    }
    for (line_number, sql) in lines {
        let matched: Vec<_> = schema_cases
            .iter()
            .filter(|case| case.source.line == line_number + 1)
            .collect();
        if matched.len() != 1 || matched[0].sql != sql || !matched[0].production.accepts() {
            return Err(format!("frozen schema mismatch: line {}", line_number + 1));
        }
    }
    Ok(())
}

#[derive(Deserialize)]
#[serde(deny_unknown_fields)]
struct Literal {
    path: String,
    function: String,
    line: usize,
    sql: String,
    formatted: bool,
    cases: Vec<String>,
    exclusion: String,
}

pub(super) fn literal_inventory(corpus: &Corpus) -> Check {
    let literals: Vec<Literal> =
        serde_json::from_str(LITERALS).map_err(|error| error.to_string())?;
    check_literals(corpus, &literals)
}

fn check_literals(corpus: &Corpus, literals: &[Literal]) -> Check {
    if literals.len() != corpus.manifest.literal_count || literals.is_empty() {
        return Err("incomplete source literal inventory".into());
    }
    let cases: BTreeMap<_, _> = corpus
        .cases
        .iter()
        .map(|case| (case.id.as_str(), case))
        .collect();
    for literal in literals {
        if source_bytes(&literal.path).is_none()
            || literal.line == 0
            || literal.sql.is_empty()
            || literal.cases.is_empty() == literal.exclusion.is_empty()
        {
            return Err("unclassified source literal".into());
        }
        for id in &literal.cases {
            let case = cases
                .get(id.as_str())
                .ok_or_else(|| format!("missing literal case {id}"))?;
            if case.source.path != literal.path
                || case.source.symbol != literal.function
                || case.source.line != literal.line
            {
                return Err(format!("{id}: literal provenance mismatch"));
            }
            if !literal.formatted {
                let sql = if case
                    .source
                    .adaptation
                    .contains("wrap_graph_table_as_from_item")
                {
                    format!("SELECT * FROM {}", literal.sql)
                } else {
                    literal.sql.clone()
                };
                if case.sql != sql {
                    return Err(format!("{id}: literal text mismatch"));
                }
            }
        }
    }
    Ok(())
}

#[test]
fn rejects_missing_coverage_and_inventory() {
    let mut corpus = Corpus::load();
    corpus
        .cases
        .retain(|case| case.family != "select" || case.owned.accepts());
    corpus.manifest.case_count = corpus.cases.len();
    assert!(manifest(&corpus)
        .unwrap_err()
        .contains("missing owned family coverage"));
    let mut corpus = Corpus::load();
    corpus.cases.pop();
    assert!(manifest(&corpus).is_err());
    let mut corpus = Corpus::load();
    corpus.manifest.sources[0].sha256 = "0".repeat(64);
    assert!(manifest(&corpus)
        .unwrap_err()
        .contains("source inventory needs review"));
    let mut corpus = Corpus::load();
    corpus.manifest.sources.pop();
    assert!(manifest(&corpus)
        .unwrap_err()
        .contains("incomplete source inventory"));
    let mut corpus = Corpus::load();
    corpus.cases[0].family = "unclassified".into();
    assert!(manifest(&corpus)
        .unwrap_err()
        .contains("unclassified statement family"));
}

#[test]
fn rejects_missing_or_unclassified_literals() {
    let corpus = Corpus::load();
    let mut literals: Vec<Literal> = serde_json::from_str(LITERALS).unwrap();
    literals.pop();
    assert!(check_literals(&corpus, &literals)
        .unwrap_err()
        .contains("incomplete source literal inventory"));
    let mut literals: Vec<Literal> = serde_json::from_str(LITERALS).unwrap();
    literals
        .iter_mut()
        .find(|literal| literal.cases.is_empty())
        .unwrap()
        .exclusion
        .clear();
    assert!(check_literals(&corpus, &literals)
        .unwrap_err()
        .contains("unclassified source literal"));
}

#[test]
fn rejects_missing_stale_and_unreferenced_waivers() {
    let mut corpus = Corpus::load();
    let case = corpus
        .cases
        .iter_mut()
        .find(|case| case.waiver.is_some())
        .unwrap();
    case.waiver = None;
    assert!(manifest(&corpus)
        .unwrap_err()
        .contains("missing or stale waiver"));
    let mut corpus = Corpus::load();
    let case = corpus
        .cases
        .iter_mut()
        .find(|case| case.waiver.is_some())
        .unwrap();
    case.production = case.owned.clone();
    assert!(manifest(&corpus)
        .unwrap_err()
        .contains("missing or stale waiver"));
    let mut corpus = Corpus::load();
    let mut unused = corpus.manifest.waivers[0].clone();
    unused.id = "unused".into();
    corpus.manifest.waivers.push(unused);
    assert!(manifest(&corpus)
        .unwrap_err()
        .contains("unreferenced waiver"));
}

#[test]
fn rejects_frozen_source_and_parameter_drift() {
    let mut corpus = Corpus::load();
    let case = corpus
        .cases
        .iter_mut()
        .find(|case| case.source.adaptation == "frozen_workload")
        .unwrap();
    case.sql.push(' ');
    assert!(frozen_inputs(&corpus)
        .unwrap_err()
        .contains("frozen SQL mismatch"));
    let mut corpus = Corpus::load();
    let case = corpus
        .cases
        .iter_mut()
        .find(|case| case.source.adaptation == "frozen_workload")
        .unwrap();
    case.production = Outcome::Accept { parameters: vec![] };
    assert!(frozen_inputs(&corpus)
        .unwrap_err()
        .contains("frozen parameter contract mismatch"));
}

#[test]
fn both_frontends_enforce_the_recorded_outcomes_and_error_codes() {
    let corpus = Corpus::load();
    let mut case = corpus
        .cases
        .iter()
        .find(|case| case.production.accepts() && case.owned.accepts())
        .unwrap()
        .clone();
    case.production = Outcome::Reject {
        code: "Parse".into(),
    };
    assert!(check_case(&case).unwrap_err().contains("production"));
    let mut case = corpus
        .cases
        .iter()
        .find(|case| !case.owned.accepts())
        .unwrap()
        .clone();
    case.owned = Outcome::Reject {
        code: "InvalidCharacter".into(),
    };
    assert!(check_case(&case).unwrap_err().contains("owned"));
}