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
//! The COUNT expression-subquery engine's two statement-shape UNION scans
//! (`eval_count_query`): the explicit-vs-omitted RETURN mix and the
//! omitted-RETURN-requires-UNION-ALL rule. Both compositions are rejected
//! before the executor ever sees them for parsed text — the parser's
//! COUNT-site check first, then the front's validate battery
//! (`relate-front`'s `validate_count_subquery_union_shapes`, the
//! statically decidable home of the rules); these per-row scans stay as
//! defense-in-depth for callers that bypass validation, raising the
//! identical messages.
use std::collections::BTreeMap;
use relate_core::{Error, Result, Value};
use relate_ir::{Clause, Query, SingleQuery};
use super::subquery_eval::eval_count_query;
use crate::graph::Graph;
use crate::overlay::ExecGraph;
use crate::row::Row;
/// A COUNT body query with `parts`' clause lists as its UNION branches,
/// joined by `union_all` — the shape the classification scan reads. The
/// queries are built by hand for exactly the reason the scans exist: no
/// product path can produce them anymore (the scans fire before any part
/// runs, so the branch clause lists only need the tails the
/// classification reads).
fn run_count(parts: Vec<Vec<Clause>>, union_all: Vec<bool>) -> Result<Value> {
let query = Query {
parts: parts
.into_iter()
.map(|clauses| SingleQuery { clauses })
.collect(),
union_all,
next: None,
};
let g = Graph::default();
let view = ExecGraph::read(&g);
eval_count_query(&view, &Row::new(), &BTreeMap::new(), &query)
}
/// The classification scan still rejects a query whose parts mix an
/// explicit RETURN tail with omitted ones, handed to it directly —
/// defense-in-depth for a caller that bypasses front validation. The
/// message is byte-identical to the one the front battery raises (which
/// the lowering boundary wraps in the same `syntax error:` prefix).
#[test]
fn the_mix_scan_rejects_a_mixed_query_defense_in_depth() {
let err: Error = run_count(
vec![
vec![Clause::Return {
distinct: false,
group_by: None,
items: Vec::new(),
order: Vec::new(),
skip: None,
limit: None,
}],
Vec::new(),
],
vec![true],
)
.expect_err("the mix scan rejects the query");
assert_eq!(
err.to_string(),
"syntax error: DifferentColumnsInUnion: cannot mix explicit and omitted RETURN"
);
}
/// The same for the second rule: an all-omitted body whose branches join
/// with a plain UNION (not UNION ALL).
#[test]
fn the_omitted_scan_rejects_a_distinct_join_defense_in_depth() {
let err: Error = run_count(vec![Vec::new(), Vec::new()], vec![false])
.expect_err("the omitted scan rejects the query");
assert_eq!(
err.to_string(),
"syntax error: DifferentColumnsInUnion: omitted RETURN requires UNION ALL"
);
}