libch 0.2.0

ClickHouse's insert and merge as short-lived operations on BigHouse `inodedb` disks, so one pool of workers serves many tenants.
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
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
//! The worker's `insert`, against the fork's binary. The conformance scenarios cover what every writer does;
//! these cover what is the worker's own: its report, its cost, and the tables it attaches without their parts.

use bytes::Bytes;
use libch::stock::{StockWriter, StockWriterOptions};
use libch::worker::{OpWriter, Usage};
use libch::writer::{Insert, Inserted, MergeCandidate, TableName};
use libch::{Error, Outcome, PartInfo, Writer, check};
use libch_testkit::ops::fail_point;
use libch_testkit::schema::{Batch, EVENTS_SCHEMA, batch, daily, events};
use libch_testkit::{Reader, TenantFixture, stack};

async fn tenant(statements: &[&str]) -> TenantFixture {
    let stack = stack().await;
    let fixture = TenantFixture::create(stack).await.unwrap();
    fixture.exclusively(statements).await.unwrap();
    fixture
}

async fn worker(fixture: &TenantFixture) -> OpWriter {
    OpWriter::new(stack().await.worker_command(), &fixture.tenant, &fixture.key)
}

async fn insert(writer: &OpWriter, table: &TableName, batch: &Batch) -> Result<Outcome<Inserted>, Error> {
    writer.insert(Insert { table, format: "JSONEachRow", token: &batch.token, data: batch.data.clone() }).await
}

fn rows(token: &str, keys: std::ops::Range<u64>) -> Batch {
    let data: String = keys.clone().map(|k| format!("{{\"k\":{k}}}\n")).collect();
    Batch { token: token.into(), data: Bytes::from(data), rows: keys.end - keys.start, sum: keys.sum() }
}

async fn assert_clean(fixture: &TenantFixture) {
    let report = check::tenant(&fixture.tenant, &fixture.key).await.unwrap();
    assert!(report.is_clean(), "{report}");
    let rejected = check::stock_load(&stack().await.stock_env(), &fixture.tenant, &fixture.key).await.unwrap();
    fixture.share().await.unwrap();
    assert!(rejected.is_empty(), "a stock writer rejects: {rejected:?}");
}

#[tokio::test]
async fn an_insert_reports_the_parts_it_committed() {
    let fixture = tenant(&EVENTS_SCHEMA).await;
    let writer = worker(&fixture).await;

    let first = insert(&writer, &events(), &batch(0, 1_000)).await.unwrap().committed().expect("the insert commits");
    let parts = first.parts.expect("a worker names its parts");
    let named: Vec<(String, String, u64)> = parts.iter().map(|part| (part.table.table.clone(), part.part.name(), part.rows)).collect();
    // The view groups a thousand events of one day by three kinds.
    // An aggregating table merges a block as it writes it, so its new part is at level one, as on a server.
    assert_eq!(named, [("events".to_string(), "202611_1_1_0".to_string(), 1_000), ("daily".to_string(), "all_1_1_1".to_string(), 3)]);
    assert!(parts.iter().all(|part| part.bytes > 0));
    assert_eq!(first.counter_conflicts, 0);

    let second = insert(&writer, &events(), &batch(1, 10)).await.unwrap().committed().unwrap().parts.unwrap();
    assert_eq!(second.iter().map(|part| part.part.name()).collect::<Vec<_>>(), ["202611_2_2_0", "all_2_2_1"], "the next insert takes the next numbers");

    let catalog = fixture.catalog().await.unwrap();
    for table in [events(), daily()] {
        let definition = catalog.table(&table).await.unwrap().unwrap();
        let stored = catalog.parts(&definition).await.unwrap();
        assert_eq!(stored.active().len(), 2, "{table:?}");
        assert_eq!(stored.counter.map(|counter| counter.value), Some(2), "each table's counter follows its own parts");
    }
    assert_clean(&fixture).await;
}

#[tokio::test]
async fn a_batch_sent_again_ends_as_a_duplicate_and_writes_nothing() {
    let fixture = tenant(&EVENTS_SCHEMA).await;
    let writer = worker(&fixture).await;
    let batch = batch(0, 50);
    assert!(insert(&writer, &events(), &batch).await.unwrap().is_committed());
    let before = fixture.catalog().await.unwrap().parts(&fixture.catalog().await.unwrap().table(&events()).await.unwrap().unwrap()).await.unwrap();

    assert_eq!(insert(&writer, &events(), &batch).await.unwrap(), Outcome::Duplicate);
    assert_eq!(insert(&worker(&fixture).await, &events(), &batch).await.unwrap(), Outcome::Duplicate, "on any worker");

    let catalog = fixture.catalog().await.unwrap();
    let after = catalog.parts(&catalog.table(&events()).await.unwrap().unwrap()).await.unwrap();
    assert_eq!(after.entries.keys().collect::<Vec<_>>(), before.entries.keys().collect::<Vec<_>>());
    assert_eq!(after.other.iter().filter(|name| name.starts_with("tmp")).count(), 0, "a duplicate leaves no temporary directory");
    assert_eq!(Reader::number_once(stack().await, &fixture, "SELECT count() FROM d.events").await.unwrap(), 50);
    assert_clean(&fixture).await;
}

async fn usage_of_an_insert(fixture: &TenantFixture, index: u64) -> Usage {
    let writer = worker(fixture).await;
    assert!(insert(&writer, &events(), &batch(index, 1_000)).await.unwrap().is_committed());
    writer.usage()
}

#[tokio::test]
async fn an_insert_costs_the_same_whatever_the_parts() {
    let stack = stack().await;
    let small = tenant(&EVENTS_SCHEMA).await;
    let large = tenant(&EVENTS_SCHEMA).await;
    // A stock shared writer fills one tenant with parts, and gives both a counter of the shared epoch.
    for (fixture, batches) in [(&small, 1), (&large, 150)] {
        let stock = StockWriter::start(&stack.stock_env(), &fixture.tenant, &fixture.key, &StockWriterOptions::default()).await.unwrap();
        for index in 0..batches {
            let batch = batch(index, 5);
            assert!(stock.insert(Insert { table: &events(), format: "JSONEachRow", token: &batch.token, data: batch.data }).await.unwrap().is_committed());
        }
        stock.stop().await.unwrap();
    }

    let (few, many) = (usage_of_an_insert(&small, 1_000).await, usage_of_an_insert(&large, 1_000).await);
    let calls = |usage: &Usage| (usage.inodedb_commits, usage.inodedb_stats, usage.inodedb_reads, usage.inodedb_lists);
    assert_eq!(calls(&few), calls(&many), "an insert reads nothing that grows with the table's parts: {few:?} and {many:?}");
    assert_eq!(many.inodedb_commits, 2, "one commit for the table and one for the view's target");
    assert_eq!(many.s3_reads + many.s3_writes, 0, "a thousand rows fit in the part's inline files");
    println!("an insert of 1,000 rows through a projection and a view: {many:?}");
}

#[tokio::test]
async fn a_table_without_tokens_is_refused_unless_the_writer_waives_exactly_once() {
    let fixture = tenant(&["CREATE DATABASE d", "CREATE TABLE d.plain (k UInt64) ENGINE = MergeTree ORDER BY k"]).await;
    let table = TableName::new("d", "plain");
    let batch = rows("only", 0..10);

    let refused = insert(&worker(&fixture).await, &table, &batch).await.unwrap();
    assert!(matches!(&refused, Outcome::Refused(reason) if reason.contains("d.plain") && reason.contains("keeps no deduplication tokens")), "{refused:?}");
    assert_eq!(Reader::number_once(stack().await, &fixture, "SELECT count() FROM d.plain").await.unwrap(), 0, "a refusal writes nothing");

    let waiving = worker(&fixture).await.at_least_once();
    assert!(insert(&waiving, &table, &batch).await.unwrap().is_committed());
    assert!(insert(&waiving, &table, &batch).await.unwrap().is_committed(), "without tokens a second insert lands again");
    assert_eq!(Reader::number_once(stack().await, &fixture, "SELECT count() FROM d.plain").await.unwrap(), 20);
    assert_clean(&fixture).await;
}

#[tokio::test]
async fn a_view_target_without_tokens_is_refused_too() {
    let fixture = tenant(&[
        "CREATE DATABASE d",
        "CREATE TABLE d.source (k UInt64) ENGINE = MergeTree ORDER BY k SETTINGS non_replicated_deduplication_window = 100",
        "CREATE TABLE d.target (k UInt64) ENGINE = MergeTree ORDER BY k",
        "CREATE MATERIALIZED VIEW d.copy TO d.target AS SELECT k FROM d.source",
    ])
    .await;
    let refused = insert(&worker(&fixture).await, &TableName::new("d", "source"), &rows("only", 0..10)).await.unwrap();
    assert!(matches!(&refused, Outcome::Refused(reason) if reason.contains("d.target")), "{refused:?}");
    assert_eq!(Reader::number_once(stack().await, &fixture, "SELECT count() FROM d.source").await.unwrap(), 0);
}

#[tokio::test]
async fn a_view_that_reads_stored_rows_is_refused() {
    let dimension = "CREATE TABLE d.dimension (k UInt64) ENGINE = MergeTree ORDER BY k SETTINGS non_replicated_deduplication_window = 100";
    let target = "CREATE TABLE d.known (k UInt64, known UInt8) ENGINE = MergeTree ORDER BY k SETTINGS non_replicated_deduplication_window = 100";
    for (reads, view) in [
        ("d.dimension", "CREATE MATERIALIZED VIEW d.known_mv TO d.known AS SELECT k, k IN (SELECT k FROM d.dimension) AS known FROM d.keyed"),
        ("d.keyed", "CREATE MATERIALIZED VIEW d.known_mv TO d.known AS SELECT k, k IN (SELECT k FROM d.keyed) AS known FROM d.keyed"),
    ] {
        let fixture = tenant(&["CREATE DATABASE d", KEYED, dimension, target, "INSERT INTO d.dimension VALUES (1)", view]).await;
        let table = TableName::new("d", "keyed");
        let refused = insert(&worker(&fixture).await, &table, &rows("only", 0..10)).await.unwrap();
        assert!(matches!(&refused, Outcome::Refused(reason) if reason.contains("d.known_mv") && reason.contains(&format!("reads {reads}"))), "{refused:?}");
        // The refusal comes before the table's own part, so the table and the view's target stay in step.
        assert_eq!(Reader::number_once(stack().await, &fixture, "SELECT count() FROM d.keyed").await.unwrap(), 0);

        // A table no view reads stored rows of takes inserts as ever.
        assert!(insert(&worker(&fixture).await, &TableName::new("d", "dimension"), &rows("more", 5..6)).await.unwrap().is_committed());
    }
}

#[tokio::test]
async fn a_view_that_names_a_table_function_fails_the_insert() {
    let target = "CREATE TABLE d.known (k UInt64, known UInt8) ENGINE = MergeTree ORDER BY k SETTINGS non_replicated_deduplication_window = 100";
    for (function, source) in [("numbers", "numbers(5)"), ("url", "url('http://127.0.0.1:1/rows', 'CSV', 'number UInt64')")] {
        let view = format!("CREATE MATERIALIZED VIEW d.known_mv TO d.known AS SELECT k, k IN (SELECT number FROM {source}) AS known FROM d.keyed");
        let fixture = tenant(&["CREATE DATABASE d", KEYED, target, &view]).await;
        let table = TableName::new("d", "keyed");
        // The worker registers no table function, so a view that names one reaches nothing outside the tenant.
        // Such a view would fail as it runs, after the table's own part is in, so the worker refuses the insert.
        let refused = insert(&worker(&fixture).await, &table, &rows("only", 0..10)).await.unwrap();
        assert!(
            matches!(&refused, Outcome::Refused(reason) if reason.contains("d.known_mv") && reason.contains(&format!("table function `{function}`"))),
            "{refused:?}"
        );
        assert_eq!(parts_and_counter(&fixture, &table).await.0, Vec::<String>::new(), "the insert wrote no part");
    }
}

/// A server holds an insert back, or refuses it, by what the table's parts add up to: `parts_to_delay_insert`,
/// `parts_to_throw_insert`, `max_parts_in_total`, the two `inactive_parts_to_*_insert`,
/// `max_avg_part_size_for_too_many_parts` and the two `dead_blobs_to_*_insert`. A worker loads no part, so none of
/// them fires, and the caller reads the counts from the catalog and holds a tenant back itself. The limits on
/// the batch itself stay: here the partitions one batch may write.
#[tokio::test]
async fn limits_on_a_tables_parts_move_to_the_caller_and_limits_on_the_batch_stay() {
    let limited = "CREATE TABLE d.limited (k UInt64) ENGINE = MergeTree PARTITION BY k % 10 ORDER BY k \
                   SETTINGS parts_to_delay_insert = 1, parts_to_throw_insert = 2, max_parts_in_total = 3, non_replicated_deduplication_window = 100";
    let fixture = tenant(&["CREATE DATABASE d", limited]).await;
    let table = TableName::new("d", "limited");
    let writer = worker(&fixture)
        .await
        .with_settings_xml("<profiles><default><max_partitions_per_insert_block>3</max_partitions_per_insert_block></default></profiles>");

    // Five parts in one partition, where a server refuses the third insert.
    for index in 0..5 {
        let batch = Batch { token: format!("tens-{index}"), data: Bytes::from(format!("{{\"k\":{}}}\n", index * 10)), rows: 1, sum: 0 };
        assert!(insert(&writer, &table, &batch).await.unwrap().is_committed(), "insert {index}");
    }
    let catalog = fixture.catalog().await.unwrap();
    let crowded = catalog.crowded_tables(2).await.unwrap();
    assert_eq!(crowded.len(), 1, "{crowded:?}");
    assert_eq!((crowded[0].table.name.clone(), crowded[0].partitions[0].partition_id.as_str(), crowded[0].partitions[0].active), (table.clone(), "0", 5));

    // A batch across four partitions passes the cap of three, fails as it does on a server, and writes nothing.
    let wide = insert(&writer, &table, &rows("wide", 1..5)).await.unwrap_err();
    assert_eq!(wide.clickhouse_code(), Some(252), "{wide}");
    assert_eq!(parts_and_counter(&fixture, &table).await.0.len(), 5);
    assert!(insert(&writer, &table, &rows("narrow", 1..4)).await.unwrap().is_committed(), "three partitions are within the cap");
    assert_clean(&fixture).await;
}

/// A view that reads a dictionary would reach whatever the dictionary's source names: here an HTTP address. The
/// worker loads no dictionary, and refuses the insert before the table's own part is in.
#[tokio::test]
async fn a_view_that_reads_a_dictionary_fails_the_insert() {
    let dictionary = "CREATE DICTIONARY d.names (k UInt64, name String) PRIMARY KEY k SOURCE(HTTP(url 'http://127.0.0.1:1/names' format 'CSV')) \
                      LAYOUT(FLAT()) LIFETIME(0)";
    let target = "CREATE TABLE d.named (k UInt64, name String) ENGINE = MergeTree ORDER BY k SETTINGS non_replicated_deduplication_window = 100";
    let view = "CREATE MATERIALIZED VIEW d.named_mv TO d.named AS SELECT k, dictGetString('d.names', 'name', k) AS name FROM d.keyed";
    let fixture = tenant(&["CREATE DATABASE d", KEYED, dictionary, target, view]).await;
    let table = TableName::new("d", "keyed");
    let refused = insert(&worker(&fixture).await, &table, &rows("only", 0..10)).await.unwrap();
    assert!(matches!(&refused, Outcome::Refused(reason) if reason.contains("d.named_mv") && reason.contains("`dictGetString`")), "{refused:?}");
    assert_eq!(parts_and_counter(&fixture, &table).await.0, Vec::<String>::new(), "the insert wrote no part");
}

const KEYED: &str = "CREATE TABLE d.keyed (k UInt64, v UInt64 DEFAULT 0) ENGINE = MergeTree ORDER BY k SETTINGS non_replicated_deduplication_window = 100";

/// The names of a table's part directories and its counter's value.
async fn parts_and_counter(fixture: &TenantFixture, table: &TableName) -> (Vec<String>, Option<i64>) {
    let catalog = fixture.catalog().await.unwrap();
    let definition = catalog.table(table).await.unwrap().unwrap();
    let parts = catalog.parts(&definition).await.unwrap();
    (parts.active().iter().map(PartInfo::name).collect(), parts.counter.map(|counter| counter.value))
}

#[tokio::test]
async fn an_insert_numbers_its_part_above_what_an_exclusive_writer_left() {
    let stack = stack().await;
    let table = TableName::new("d", "keyed");
    // An exclusive writer numbers its parts itself and keeps no counter.
    let fixture = tenant(&["CREATE DATABASE d", KEYED, "INSERT INTO d.keyed (k) VALUES (1)", "INSERT INTO d.keyed (k) VALUES (2)"]).await;
    assert_eq!(parts_and_counter(&fixture, &table).await, (vec!["all_1_1_0".to_string(), "all_2_2_0".to_string()], None));

    let writer = worker(&fixture).await;
    let inserted = insert(&writer, &table, &rows("three", 3..4)).await.unwrap().committed().unwrap().parts.unwrap();
    assert_eq!(inserted[0].part.name(), "all_3_3_0", "the part takes the number after the exclusive writer's last");
    assert_eq!(parts_and_counter(&fixture, &table).await.1, Some(3));

    // The exclusive writer returns, inserts, merges and mutates. Its mutation takes a block number too.
    fixture
        .exclusively(&[
            "INSERT INTO d.keyed (k) VALUES (4)",
            "OPTIMIZE TABLE d.keyed FINAL",
            "ALTER TABLE d.keyed UPDATE v = k WHERE 1 SETTINGS mutations_sync = 2",
        ])
        .await
        .unwrap();
    let (parts, counter) = parts_and_counter(&fixture, &table).await;
    assert_eq!((parts, counter), (vec!["all_1_4_1_5".to_string()], Some(3)), "the counter trails the exclusive writer's numbers");

    let inserted = insert(&writer, &table, &rows("six", 6..7)).await.unwrap().committed().unwrap().parts.unwrap();
    assert_eq!(inserted[0].part.name(), "all_6_6_0", "above the merged part's blocks and the mutation's version");
    let inserted = insert(&writer, &table, &rows("seven", 7..8)).await.unwrap().committed().unwrap().parts.unwrap();
    assert_eq!(inserted[0].part.name(), "all_7_7_0", "the counter is the epoch's own from here on");

    let reader = Reader::open(stack, &fixture).await.unwrap();
    assert_eq!(reader.value("SELECT groupArray(k) FROM (SELECT k FROM d.keyed ORDER BY k)").await.unwrap().trim(), "[1,2,3,4,6,7]");
    reader.close().await.unwrap();
    assert_clean(&fixture).await;
}

#[tokio::test]
async fn an_insert_that_cannot_run_fails_with_the_engines_exception() {
    const UNKNOWN_TABLE: i32 = 60;
    const UNKNOWN_FORMAT: i32 = 73;
    let fixture = tenant(&["CREATE DATABASE d", KEYED]).await;
    let writer = worker(&fixture).await;

    let absent = insert(&writer, &TableName::new("d", "absent"), &rows("only", 0..1)).await.unwrap_err();
    assert_eq!(absent.clickhouse_code(), Some(UNKNOWN_TABLE), "{absent}");

    let table = TableName::new("d", "keyed");
    let unknown = writer.insert(Insert { table: &table, format: "NoSuchFormat", token: "only", data: Bytes::from_static(b"1\n") }).await.unwrap_err();
    assert_eq!(unknown.clickhouse_code(), Some(UNKNOWN_FORMAT), "{unknown}");

    let malformed =
        writer.insert(Insert { table: &table, format: "JSONEachRow", token: "only", data: Bytes::from_static(b"{\"k\": \"not a number\"}\n") }).await;
    assert!(malformed.unwrap_err().clickhouse_code().is_some());
    assert_eq!(Reader::number_once(stack().await, &fixture, "SELECT count() FROM d.keyed").await.unwrap(), 0, "a failed insert writes nothing");
}

#[tokio::test]
async fn a_batch_of_no_rows_commits_nothing() {
    let fixture = tenant(&["CREATE DATABASE d", KEYED]).await;
    let table = TableName::new("d", "keyed");
    let inserted = worker(&fixture).await.insert(Insert { table: &table, format: "JSONEachRow", token: "empty", data: Bytes::new() }).await.unwrap();
    assert_eq!(inserted.committed().unwrap().parts.unwrap().len(), 0);
}

#[tokio::test]
async fn a_large_batch_arrives_whole() {
    let fixture = tenant(&["CREATE DATABASE d", KEYED]).await;
    let table = TableName::new("d", "keyed");
    // About four megabytes, so the batch spans several frames and the part's files leave the inline limit.
    let batch = rows("large", 0..400_000);
    assert!(batch.data.len() > 3 << 20);
    let writer = worker(&fixture).await;
    let parts = insert(&writer, &table, &batch).await.unwrap().committed().unwrap().parts.unwrap();
    assert_eq!(parts.iter().map(|part| part.rows).sum::<u64>(), 400_000);
    assert!(writer.usage().s3_writes > 0, "a part this size keeps its files in S3");

    let reader = Reader::open(stack().await, &fixture).await.unwrap();
    assert_eq!(reader.number("SELECT count() FROM d.keyed").await.unwrap(), 400_000);
    assert_eq!(reader.number("SELECT sum(k) FROM d.keyed").await.unwrap(), batch.sum);
    reader.close().await.unwrap();
    assert_clean(&fixture).await;
}

#[tokio::test]
async fn an_insert_over_its_memory_cap_fails_and_leaves_nothing() {
    let wide = "CREATE TABLE d.wide (k UInt64, text String) ENGINE = MergeTree ORDER BY k SETTINGS non_replicated_deduplication_window = 100";
    let fixture = tenant(&["CREATE DATABASE d", wide]).await;
    let table = TableName::new("d", "wide");
    let texts = |token: &str, keys: std::ops::Range<u64>, width: usize| {
        let text = "x".repeat(width);
        let data: String = keys.clone().map(|k| format!("{{\"k\":{k},\"text\":\"{text}\"}}\n")).collect();
        Batch { token: token.into(), data: Bytes::from(data), rows: keys.end - keys.start, sum: 0 }
    };
    let capped = worker(&fixture).await.with_memory_limit(MEMORY_CAP);

    // A small batch fits under the cap.
    assert!(insert(&capped, &table, &texts("small", 0..1_000, 100)).await.unwrap().is_committed());
    let before = parts_and_counter(&fixture, &table).await;

    // A block gathers rows up to 256 MB before it is written, and 160 MB of text passes the cap on the way. The
    // worker names the limit, and the table gains nothing.
    let large = texts("large", 1_000..21_000, 8_000);
    let failed = insert(&capped, &table, &large).await.unwrap_err();
    assert_eq!(failed.clickhouse_code(), Some(241), "{failed}");
    assert!(failed.to_string().contains("memory limit") || failed.to_string().contains("Memory limit"), "{failed}");
    assert_eq!(parts_and_counter(&fixture, &table).await, before);
    assert_clean(&fixture).await;

    // The same batch through a worker with room commits, under the same token.
    assert!(insert(&worker(&fixture).await, &table, &large).await.unwrap().is_committed());
    assert_eq!(Reader::number_once(stack().await, &fixture, "SELECT count() FROM d.wide").await.unwrap(), 21_000);
}

/// A cap the engine and a small insert fit under, and a block of 160 MB does not.
const MEMORY_CAP: u64 = 96 << 20;

/// A part's small files travel in the commit that makes the part, and InodeDB bounds a commit's size. A part
/// with more small files than one commit carries still commits whole, because the disk writes the largest of
/// them to blobs until the commit fits: here a wide part of 1,200 columns, each 12 KB of values that compress to
/// nothing less, 14 MB in all.
#[tokio::test]
async fn a_part_of_thousands_of_small_files_commits() {
    const COLUMNS: u64 = 1_200;
    const ROWS: u64 = 1_500;
    let columns: String = (0..COLUMNS).map(|column| format!(", c{column} UInt64 DEFAULT sipHash64(k, {column})")).collect();
    let create = format!(
        "CREATE TABLE d.columns (k UInt64{columns}) ENGINE = MergeTree ORDER BY k \
         SETTINGS min_bytes_for_wide_part = 0, min_rows_for_wide_part = 0, non_replicated_deduplication_window = 100"
    );
    let fixture = tenant(&["CREATE DATABASE d", &create]).await;
    let table = TableName::new("d", "columns");
    let writer = worker(&fixture).await;

    let inserted = insert(&writer, &table, &rows("columns", 0..ROWS)).await.unwrap().committed().expect("the insert commits");
    let parts = inserted.parts.expect("a worker names its parts");
    assert_eq!(parts.iter().map(|part| part.rows).collect::<Vec<_>>(), [ROWS]);
    assert!(parts[0].bytes > COLUMNS * ROWS * 8, "{} bytes", parts[0].bytes);

    // The definition passes 16 KiB, so the disk keeps it in a blob, and the catalog reads it from there.
    let catalog = fixture.catalog().await.unwrap();
    let stored = catalog.table(&table).await.unwrap().expect("the catalog lists the table");
    assert!(stored.definition.len() > 16 << 10 && stored.definition.contains("`c1199` UInt64"), "{} bytes", stored.definition.len());
    assert!(stored.is_merge_tree());
    assert_eq!(catalog.rows(&stored).await.unwrap(), ROWS);

    let equal = "SELECT countIf(c0 = sipHash64(k, 0) AND c1199 = sipHash64(k, 1199)) FROM d.columns";
    assert_eq!(Reader::number_once(stack().await, &fixture, equal).await.unwrap(), ROWS, "the first and last columns read back");
    assert_clean(&fixture).await;
}

/// A batch of `rows` rows whose `data` holds `paths` paths each, with keys from `first` on.
fn json_rows(token: &str, first: u64, rows: u64, paths: u64) -> Batch {
    let data: String = (first..first + rows)
        .map(|k| {
            let fields: Vec<String> = (0..paths).map(|path| format!("\"p{path}\":{}", k * path)).collect();
            format!("{{\"k\":{k},\"data\":{{{}}}}}\n", fields.join(","))
        })
        .collect();
    Batch { token: token.into(), data: Bytes::from(data), rows, sum: (first..first + rows).sum() }
}

/// A tenant with a table of a `JSON` column whose parts are wide from `wide_from_rows` rows on.
async fn json_tenant(data_type: &str, wide_from_rows: u64) -> (TenantFixture, TableName) {
    let create = format!(
        "CREATE TABLE d.j (k UInt64, data {data_type}) ENGINE = MergeTree ORDER BY k \
         SETTINGS min_bytes_for_wide_part = 0, min_rows_for_wide_part = {wide_from_rows}, non_replicated_deduplication_window = 100"
    );
    (tenant(&["CREATE DATABASE d", &create]).await, TableName::new("d", "j"))
}

/// The table's parts by kind, its rows, the sum of one path, and the most paths a row keeps in columns of their
/// own and in the column's shared data.
async fn json_state(fixture: &TenantFixture, path: u64) -> (String, String) {
    let reader = Reader::open(stack().await, fixture).await.unwrap();
    let parts = reader.value("SELECT toString(groupArray((name, part_type))) FROM system.parts WHERE database = 'd' AND active").await.unwrap();
    let rows = format!(
        "SELECT toString((count(), sum(data.p{path}::Nullable(UInt64)), max(length(JSONDynamicPaths(data))), max(length(JSONSharedDataPaths(data))))) FROM d.j"
    );
    let rows = reader.value(&rows).await.unwrap();
    reader.close().await.unwrap();
    (parts.replace('\\', ""), rows)
}

/// The names in the table's directory that start with `tmp`.
async fn json_temporary_directories(fixture: &TenantFixture, table: &TableName) -> Vec<String> {
    let catalog = fixture.catalog().await.unwrap();
    let parts = catalog.parts(&catalog.table(table).await.unwrap().unwrap()).await.unwrap();
    parts.other.into_iter().filter(|name| name.starts_with("tmp")).collect()
}

/// A wide part writes ten files for each path a `JSON` column keeps in a column of its own, and the type keeps
/// 1,024: more files than one InodeDB commit carries. The disk commits a part of more than 4,096 files in stages
/// under the part's temporary directory, and its last commit publishes the part. So an insert at the type's cap
/// commits in three commits where a small one takes one, and paths past the cap go to the column's shared data.
#[tokio::test]
async fn a_wide_part_past_one_commit_commits_in_stages() {
    let (fixture, table) = json_tenant("JSON", 0).await;
    let writer = worker(&fixture).await;
    let commits = |before: Usage, after: Usage| after.inodedb_commits - before.inodedb_commits;

    // A table's first insert also makes its coordination files.
    assert!(insert(&writer, &table, &json_rows("first", 300, 20, 1)).await.unwrap().is_committed());
    let start = writer.usage();
    assert!(insert(&writer, &table, &json_rows("small", 0, 20, 100)).await.unwrap().is_committed());
    let small = writer.usage();
    assert!(insert(&writer, &table, &json_rows("at-the-cap", 100, 20, 1_024)).await.unwrap().is_committed());
    let large = writer.usage();
    assert_eq!((commits(start, small), commits(small, large)), (1, 3), "1,019 files commit at once, and 10,259 in two stages and a last commit");

    assert!(insert(&writer, &table, &json_rows("past-the-cap", 200, 20, 1_500)).await.unwrap().is_committed());
    let sum: u64 = (200..220u64).map(|k| k * 1_499).sum();
    assert_eq!(
        json_state(&fixture, 1_499).await,
        ("[('all_1_1_0','Wide'),('all_2_2_0','Wide'),('all_3_3_0','Wide'),('all_4_4_0','Wide')]".to_string(), format!("(80,{sum},1024,476)"))
    );
    assert!(json_temporary_directories(&fixture, &table).await.is_empty());
    assert_clean(&fixture).await;
}

/// Merges whose parts pass one commit: two compact parts of 1,000 paths each merge into a wide part, twice, and
/// the two wide parts merge into one. Housekeeping then removes the sources, the wide ones of 10,000 files each
/// among them, and the table reads back whole.
#[tokio::test]
async fn merges_of_many_json_paths_into_wide_parts_commit_in_stages() {
    let (fixture, table) = json_tenant("JSON", 30).await;
    let writer = worker(&fixture).await;
    let mut compact = Vec::new();
    for batch in 0..4u64 {
        let inserted = insert(&writer, &table, &json_rows(&format!("b{batch}"), batch * 100, 20, 1_000)).await.unwrap().committed().unwrap();
        compact.push(inserted.parts.unwrap()[0].part.clone());
    }
    let sum: u64 = (0..4u64).flat_map(|batch| batch * 100..batch * 100 + 20).map(|k| k * 999).sum();
    let compact_names = "[('all_1_1_0','Compact'),('all_2_2_0','Compact'),('all_3_3_0','Compact'),('all_4_4_0','Compact')]";
    assert_eq!(json_state(&fixture, 999).await, (compact_names.to_string(), format!("(80,{sum},1000,0)")));

    let merge = |sources: Vec<PartInfo>| {
        let (writer, table) = (writer.clone(), table.clone());
        async move { writer.merge(&table, &MergeCandidate { partition_id: "all".into(), sources }).await.unwrap().committed().expect("the merge commits") }
    };
    let before = writer.usage();
    let first = merge(compact[..2].to_vec()).await;
    assert_eq!((first.part.name(), first.rows), ("all_1_2_1".to_string(), Some(40)));
    assert!(writer.usage().inodedb_commits - before.inodedb_commits >= 3, "the merged part's files pass one commit");
    let second = merge(compact[2..].to_vec()).await;
    assert_eq!(json_state(&fixture, 999).await, ("[('all_1_2_1','Wide'),('all_3_4_1','Wide')]".to_string(), format!("(80,{sum},1000,0)")));
    let whole = merge(vec![first.part, second.part]).await;
    assert_eq!((whole.part.name(), whole.rows), ("all_1_4_2".to_string(), Some(80)));

    tokio::time::sleep(std::time::Duration::from_millis(2_100)).await;
    let housekept = writer.clone().with_settings_xml("<merge_tree><old_parts_lifetime>1</old_parts_lifetime></merge_tree>").housekeep(&table).await;
    let housekept = housekept.unwrap().committed().unwrap();
    assert_eq!(housekept.removed.len(), 6, "four compact parts and two wide ones: {housekept:?}");
    assert_eq!(json_state(&fixture, 999).await, ("[('all_1_4_2','Wide')]".to_string(), format!("(80,{sum},1000,0)")));
    assert!(json_temporary_directories(&fixture, &table).await.is_empty());
    assert_clean(&fixture).await;
}

/// A worker killed between two stages of an insert leaves the part's temporary directory with the files its
/// stages committed, and no part. The batch sent again lands once. Housekeeping removes the directory once it is
/// as old as the table's `temporary_directories_lifetime`.
#[tokio::test]
async fn an_insert_killed_between_stages_leaves_a_directory_for_housekeeping() {
    let (fixture, table) = json_tenant("JSON", 0).await;
    let writer = worker(&fixture).await;
    let batch = json_rows("staged", 0, 20, 1_024);

    let doomed = writer.clone().with_settings_xml(fail_point("inodedb_pause_after_stage")).with_deadline(std::time::Duration::from_secs(5));
    let error = insert(&doomed, &table, &batch).await.expect_err("the worker dies after its first stage");
    assert!(error.to_string().contains("was killed"), "{error}");
    let catalog = fixture.catalog().await.unwrap();
    assert!(catalog.parts(&catalog.table(&table).await.unwrap().unwrap()).await.unwrap().entries.is_empty(), "no part joined the table");
    let left = json_temporary_directories(&fixture, &table).await;
    assert_eq!(left.len(), 1, "{left:?}");
    assert!(left[0].starts_with("tmp_insert_"), "{left:?}");
    let report = check::tenant(&fixture.tenant, &fixture.key).await.unwrap();
    assert!(report.is_clean(), "{report}");

    assert!(insert(&writer, &table, &batch).await.unwrap().is_committed());
    assert_eq!(insert(&writer, &table, &batch).await.unwrap(), Outcome::Duplicate);
    let sum: u64 = (0..20u64).map(|k| k * 1_023).sum();
    assert_eq!(json_state(&fixture, 1_023).await.1, format!("(20,{sum},1024,0)"));

    // Housekeeping under the default lifetime leaves the dead worker's directory, and removes it once it is old.
    assert_eq!(writer.housekeep(&table).await.unwrap().committed().unwrap().temporary_directories, 0);
    assert_eq!(json_temporary_directories(&fixture, &table).await, left);
    tokio::time::sleep(std::time::Duration::from_millis(2_100)).await;
    let short = "<merge_tree><temporary_directories_lifetime>1</temporary_directories_lifetime></merge_tree>";
    let housekept = writer.clone().with_settings_xml(short).housekeep(&table).await.unwrap().committed().unwrap();
    assert_eq!(housekept.temporary_directories, 1, "{housekept:?}");
    assert!(json_temporary_directories(&fixture, &table).await.is_empty());
    assert_eq!(json_state(&fixture, 1_023).await.1, format!("(20,{sum},1024,0)"));
    assert_clean(&fixture).await;
}