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
//! When two keys are one key (the code review of 2026-11-10, the
//! stdlib rows B1 and B2, the value rows B3 and B12, the graph row
//! T3). The spec has two rules and the engine compared four ways:
//!
//! - a record's keys and an indexed table's labels are equal AS
//! VALUES (spec: Record): `1` and `1.0` one key, `1` and `"1"`
//! two, one instant under two offsets one, `1_km` and `1000_m`
//! one;
//! - `correlate`'s key and a join condition's equality compare AS
//! `=` COMPARES (spec: Concatenation, correlated tables): the
//! above, and a number with its text.
use std::path::{Path, PathBuf};
fn fixture(tag: &str) -> PathBuf {
let dir = std::env::temp_dir().join(format!("quarb-keys-{}-{tag}", std::process::id()));
let _ = std::fs::remove_dir_all(&dir);
std::fs::create_dir_all(&dir).unwrap();
std::fs::write(
dir.join("inst.csv"),
"d,v\n2013-01-01T00:00:00Z,1\n2013-01-02T00:00:00Z,2\n",
)
.unwrap();
std::fs::write(
dir.join("legs.json"),
r#"{"orders": [{"id": 1, "km": "1 km"}, {"id": 2, "km": "2 km"}, {"id": 4, "km": "4 km"}],
"customers": [{"name": "Xenophon", "d": "1000 m"}, {"name": "Cyrus", "d": "2000 m"},
{"name": "Clearchus", "d": "4 km"}]}"#,
)
.unwrap();
dir
}
fn qua(dir: &Path, args: &[&str]) -> (bool, String, String) {
let out = std::process::Command::new(env!("CARGO_BIN_EXE_qua"))
.env("QUARB_LOCALE", "C")
.current_dir(dir)
.args(args)
.stdin(std::process::Stdio::null())
.output()
.unwrap();
(
out.status.success(),
String::from_utf8(out.stdout).unwrap().trim().to_string(),
String::from_utf8_lossy(&out.stderr).to_string(),
)
}
fn ok(dir: &Path, args: &[&str]) -> String {
let (success, out, err) = qua(dir, args);
assert!(success, "{args:?}: {err}");
out
}
fn refused(dir: &Path, args: &[&str]) -> String {
let (success, out, err) = qua(dir, args);
assert!(!success, "{args:?} answered: {out}");
err
}
/// `correlate` matches its key as `=` compares. The key was a
/// double or the print text: an id past 2^53 joined its neighbour's
/// row, `true` joined "true", and one instant under two offsets did
/// not join.
#[test]
fn correlate_matches_its_key_as_equals_compares() {
let d = fixture("correlate");
// two integers compare exactly: a 64-bit id is not a double
assert_eq!(
ok(
&d,
&[
"= #(@(id; who); %(9007199254740993; 'Achilles'); %(9007199254740992; 'Hector')) \
| correlate((#(@(id; city); %(9007199254740992; 'Troy')) | #(-i=:id)); --on=:id) \
| $_:city"
]
),
"@(null; \"Troy\")"
);
// a declared index wins over the undeclared key (ruling
// 2026-11-10): under an index declared text the key is read as
// its text, the boolean too, and joins. (Compared as values the
// boolean was not its text, and the row found nothing.)
assert_eq!(
ok(
&d,
&["= #(@(k; who); %(true; 'Achilles')) \
| correlate((#(@(k<text>; city); %('true'; 'Troy')) | #(-i=:k)); --on=:k) | $_:city"]
),
"@(\"Troy\")"
);
// a number meets its text, as `1 = \"1\"` holds
assert_eq!(
ok(
&d,
&["= #(@(k; who); %(1; 'Achilles')) \
| correlate((#(@(k; city); %('1'; 'Troy')) | #(-i=:k)); --on=:k) | $_:city"]
),
"@(\"Troy\")"
);
// one instant under two written offsets
assert_eq!(
ok(
&d,
&["= #(@(k; who); %(2024-01-01T10:00:00+01:00; 'Achilles')) \
| correlate((#(@(k; city); %(2024-01-01T09:00:00Z; 'Troy')) | #(-i=:k)); --on=:k) \
| $_:city"]
),
"@(\"Troy\")"
);
// one magnitude under two units
assert_eq!(
ok(
&d,
&["= #(@(k; who); %(1_km; 'Hector')) \
| correlate((#(@(k; city); %(1000_m; 'Ilium')) | #(-i=:k)); --on=:k) | $_:city"]
),
"@(\"Ilium\")"
);
// a null key matches nothing, and a label `=` tells apart from
// no other is not a repeated one
assert_eq!(
ok(
&d,
&["= #(@(k; who); %(null; 'Achilles')) \
| correlate((#(@(k; city); %(1; 'Troy'); %('a'; 'Ilium')) | #(-i=:k)); --on=:k) \
| $_:city"]
),
"@(null)"
);
// the operand's index repeats a label when `=` holds between
// two of them, however they are written
let e = refused(
&d,
&["= #(@(k; who); %(1; 'Achilles')) \
| correlate((#(@(k; city); %(1; 'Troy'); %(1.0; 'Ilium')) | #(-i=:k)); --on=:k)"],
);
assert!(e.contains("is not unique"), "{e}");
}
/// Two indexed tables align on their labels as values: `1` and
/// `1.0` are one label. They were compared by structure, so `+`
/// answered four rows of nulls. Two undeclared indexes meet at
/// their common type (ruling 2026-11-10), so the labels of an
/// integer index and a float one are floats in the answer.
#[test]
fn indexed_tables_align_on_labels_equal_as_values() {
let d = fixture("align");
let left = "(#(@(k; v); %(1; 10); %(2; 20)) | #(-i=:k))";
let right = "(#(@(k; v); %(1.0; 1); %(2.0; 2)) | #(-i=:k))";
assert_eq!(
ok(&d, &[&format!("= {left} + {right}")]),
"#(@(k; v);\n %(1.0; 11);\n %(2.0; 22))"
);
// `++!` updates the left's rows where it appended the right's
assert_eq!(
ok(&d, &[&format!("= {left} ++! {right}")]),
"#(@(k; v);\n %(1.0; 1);\n %(2.0; 2))"
);
// columns set side by side meet on the label
assert_eq!(
ok(
&d,
&["= (#(@(k; v); %(1; 10); %(2; 20)) | #(-i=:k)) \
++ (#(@(k; w); %(2.0; 'b'); %(1.0; 'a')) | #(-i=:k))"]
),
"#(@(k; v; w);\n %(1.0; 10; \"a\");\n %(2.0; 20; \"b\"))"
);
// a number and its text stay two keys, nothing coerces them: a
// record keyed 1 and one keyed "1" align on neither
assert_eq!(ok(&d, &["= %(1 = 10) + %('1' = 1) | keys | count"]), "2");
// pivot's row keys are the result's labels
assert_eq!(
ok(
&d,
&["= @(%(r = 1; c = 'a'; v = 10); %(r = 1.0; c = 'b'; v = 20)) | pivot(:r; :c; :v)"]
),
"#(@(r; a; b);\n %(1; 10; 20))"
);
}
/// A label and a record key are found as values: the written
/// offset of an instant and the written unit of a quantity are
/// display.
#[test]
fn a_label_is_found_as_a_value() {
let d = fixture("lookup");
assert_eq!(ok(&d, &["= %(1_km = 1; 2_km = 2)[[1000_m]]"]), "1");
assert_eq!(ok(&d, &["= %(1 = 'a')[[1.0]]"]), "a");
assert_eq!(ok(&d, &["= %(1 = 'a')[['1']]"]), "null");
// the row labelled 00:00Z, asked for under another offset and
// as the day
for label in ["2013-01-01T01:00:00+01:00", "2013-01-01"] {
assert_eq!(
ok(
&d,
&[&format!("/* @| #(-i=:d) | $_[[{label}]] | :v"), "inst.csv"]
),
"1",
"{label}"
);
}
// two rows under one label, however written: the column is no
// record keyed by it
let e = refused(
&d,
&["= #(@(d; v); %(1_km; 1); %(1000_m; 2)) | #(-i=:d) | $_:v"],
);
assert!(e.contains("stands on several rows"), "{e}");
}
/// The keyed join and the value match offer every candidate `=`
/// would pick. Two unit texts are two texts (the user's ruling of
/// 2026-11-29): they meet by their spelling, in the join as under
/// `=`, and by magnitude where one side of the condition is read
/// under its unit.
#[test]
fn the_keyed_join_meets_unit_text_as_the_comparison_does() {
let d = fixture("join");
assert_eq!(
ok(
&d,
&[
"/orders/* <=> /customers/*[::d = $$0::km] | ::id",
"legs.json"
]
),
"4"
);
assert_eq!(
ok(
&d,
&[
"/orders/* <=> /customers/*[::d<_m> = $$0::km] | ::id",
"legs.json"
]
),
"1\n2\n4"
);
assert_eq!(
ok(
&d,
&["/orders/*::km=>(/customers/*::d) | ::name", "legs.json"]
),
"Clearchus"
);
}