polyc-query 2026.10.1

The Query plane's read model: a DataFusion engine over signed projection artifacts, behind a verified credential.
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
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
//! Proves the fixed SQL statement `crate::control_plane::dashboard`
//! (`crates/control-plane/src/forensics.rs`'s `DASHBOARD_CONTEXT_SQL`) sends
//! to the Fleet Query plane for `/api/dashboard`'s per-conversation context
//! (POLY-392) — against real `DataFusion` `MemTable`s built from the exact
//! `conversation-core/v1`, `conversation-execution/v1`, and
//! `conversation-security/v1` schemas the registry declares, not a live
//! projected stack.
//!
//! Mirrors `dashboard_attribution_sql.rs`'s own shape and disclaimer: this is
//! a syntax-and-semantics proof for the SQL text itself, not the Query
//! plane's authority, resolution, or transport.

#![allow(clippy::unwrap_used)]

use std::sync::Arc;

use arrow::array::{BooleanArray, ListArray, RecordBatch, StringArray, UInt64Array};
use datafusion::datasource::MemTable;
use datafusion::prelude::SessionContext;
use polyc_projection::family::{
    CONVERSATION_CORE_ENTRY, CONVERSATION_EXECUTION_ENTRY, CONVERSATION_MESSAGES,
    CONVERSATION_SECURITY_ENTRY, CONVERSATION_TURNS, EXECUTION_SUMMARY, SECURITY_ATTRIBUTION,
    SECURITY_ATTRIBUTION_PROVENANCE,
};

/// The statement under test — the exact constant `crate::forensics`
/// (`crates/control-plane/src/forensics.rs`) runs in production.
const CONTEXT_SQL: &str = polyc_query_model::statements::DASHBOARD_CONTEXT_SQL;

/// The same logical-to-Arrow mapping `polyc_projector::artifact::arrow_schema`
/// applies, restated here rather than imported — see
/// `financial_dashboard_sql.rs`'s own `schema` for why a Component cannot
/// depend on `polyc-projector`, a Container.
fn schema(
    entry: polyc_projection::family::FamilyEntry,
    table: polyc_projection::family::TableId,
) -> arrow::datatypes::SchemaRef {
    use polyc_projection::family::LogicalType;

    let declared = entry.table(table).expect("table declared");
    let fields: Vec<arrow::datatypes::Field> = declared
        .fields()
        .iter()
        .map(|field| {
            let data_type = match field.logical_type() {
                LogicalType::Utf8 => arrow::datatypes::DataType::Utf8,
                LogicalType::FixedBytes { len } => {
                    arrow::datatypes::DataType::FixedSizeBinary(i32::try_from(len).unwrap())
                }
                LogicalType::UInt64 => arrow::datatypes::DataType::UInt64,
                LogicalType::Boolean => arrow::datatypes::DataType::Boolean,
            };
            arrow::datatypes::Field::new(field.name(), data_type, field.nullable())
        })
        .collect();
    Arc::new(arrow::datatypes::Schema::new(fields))
}

fn fixed_incarnation(len: usize) -> arrow::array::FixedSizeBinaryArray {
    let mut builder = arrow::array::FixedSizeBinaryBuilder::with_capacity(len, 32);
    for _ in 0..len {
        builder.append_value([0_u8; 32]).unwrap();
    }
    builder.finish()
}

/// `conv-a` carries two committed turns (the second, `turn-2`, has the
/// greater `complete_position` and must win), three messages (a `system`
/// row at the lowest position that must be skipped, an `internal_only`
/// `user` row that must also be skipped, and the real opening `user` row),
/// two summaries (the one with the greater `covers_through_position` must
/// win), and attribution posted through two distinct edges. `conv-b` carries
/// exactly one of each, proving the single-row case. `conv-c` carries
/// nothing in any of the four tables and must be entirely absent from the
/// result — the dashboard still lists it from `dashboard_conversations.sql`;
/// this statement just contributes nothing for it.
#[allow(
    clippy::too_many_lines,
    reason = "one MemTable per one of the five tables this statement reads, each already a \
              single clearly-named fixture; splitting further would only add indirection for \
              one test-only call site"
)]
fn context() -> SessionContext {
    let ctx = SessionContext::new();

    let turns_schema = schema(CONVERSATION_CORE_ENTRY, CONVERSATION_TURNS);
    let turns = RecordBatch::try_new(
        Arc::clone(&turns_schema),
        vec![
            Arc::new(StringArray::from(vec!["conv-a", "conv-a", "conv-b"])),
            Arc::new(fixed_incarnation(3)),
            Arc::new(StringArray::from(vec!["turn-1", "turn-2", "turn-only"])),
            Arc::new(UInt64Array::from(vec![0_u64, 10, 0])),
            Arc::new(UInt64Array::from(vec![1_u64, 11, 1])),
            Arc::new(UInt64Array::from(vec![2_u64, 12, 2])),
        ],
    )
    .unwrap();
    ctx.register_table(
        "turns",
        Arc::new(MemTable::try_new(turns_schema, vec![vec![turns]]).unwrap()),
    )
    .unwrap();

    let messages_schema = schema(CONVERSATION_CORE_ENTRY, CONVERSATION_MESSAGES);
    let messages = RecordBatch::try_new(
        Arc::clone(&messages_schema),
        vec![
            Arc::new(StringArray::from(vec![
                "conv-a", "conv-a", "conv-a", "conv-b",
            ])),
            Arc::new(fixed_incarnation(4)),
            Arc::new(UInt64Array::from(vec![0_u64, 1, 2, 0])),
            Arc::new(StringArray::from(vec![
                "turn-1",
                "turn-1",
                "turn-1",
                "turn-only",
            ])),
            Arc::new(StringArray::from(vec!["system", "user", "user", "user"])),
            Arc::new(BooleanArray::from(vec![false, true, false, false])),
            Arc::new(BooleanArray::from(vec![false; 4])),
            Arc::new(StringArray::from(vec![
                "context note",
                "internal draft, must be skipped",
                "hello from conv-a, the real opening message",
                "hi from conv-b",
            ])),
            Arc::new(StringArray::from(vec![
                "trusted", "trusted", "trusted", "trusted",
            ])),
        ],
    )
    .unwrap();
    ctx.register_table(
        "messages",
        Arc::new(MemTable::try_new(messages_schema, vec![vec![messages]]).unwrap()),
    )
    .unwrap();

    let summary_schema = schema(CONVERSATION_EXECUTION_ENTRY, EXECUTION_SUMMARY);
    let summary = RecordBatch::try_new(
        Arc::clone(&summary_schema),
        vec![
            Arc::new(StringArray::from(vec!["conv-a", "conv-a", "conv-b"])),
            Arc::new(fixed_incarnation(3)),
            Arc::new(UInt64Array::from(vec![0_u64, 5, 0])),
            Arc::new(StringArray::from(vec!["sum-1", "sum-2", "sum-only"])),
            Arc::new(StringArray::from(vec![
                "stale summary",
                "latest summary",
                "conv-b's only summary",
            ])),
            Arc::new(UInt64Array::from(vec![3_u64, 12, 2])),
        ],
    )
    .unwrap();
    ctx.register_table(
        "summary",
        Arc::new(MemTable::try_new(summary_schema, vec![vec![summary]]).unwrap()),
    )
    .unwrap();

    let attribution_schema = schema(CONVERSATION_SECURITY_ENTRY, SECURITY_ATTRIBUTION);
    let attribution = RecordBatch::try_new(
        Arc::clone(&attribution_schema),
        vec![
            Arc::new(StringArray::from(vec!["conv-a", "conv-a", "conv-b"])),
            Arc::new(fixed_incarnation(3)),
            Arc::new(UInt64Array::from(vec![0_u64, 1, 0])),
            Arc::new(StringArray::from(vec!["turn-1", "turn-1", "turn-only"])),
            Arc::new(StringArray::from(vec![
                "persona-a",
                "persona-b",
                "persona-c",
            ])),
            Arc::new(StringArray::from(vec![
                "initiator",
                "participant",
                "initiator",
            ])),
        ],
    )
    .unwrap();
    ctx.register_table(
        "attribution",
        Arc::new(MemTable::try_new(attribution_schema, vec![vec![attribution]]).unwrap()),
    )
    .unwrap();

    let provenance_schema = schema(CONVERSATION_SECURITY_ENTRY, SECURITY_ATTRIBUTION_PROVENANCE);
    let provenance = RecordBatch::try_new(
        Arc::clone(&provenance_schema),
        vec![
            Arc::new(StringArray::from(vec!["conv-a", "conv-a", "conv-b"])),
            Arc::new(fixed_incarnation(3)),
            Arc::new(UInt64Array::from(vec![0_u64, 1, 0])),
            Arc::new(StringArray::from(vec!["team", "team", "team"])),
            Arc::new(StringArray::from(vec!["team", "team", "team"])),
            Arc::new(StringArray::from(vec!["u1", "u2", "u3"])),
            Arc::new(StringArray::from(vec!["Alice", "Bob", "Carol"])),
            // conv-a's two attribution rows post through two DISTINCT edges;
            // a fanout or dedup bug in `array_agg` would show up as a
            // three-or-one-element array instead of the correct two.
            Arc::new(StringArray::from(vec!["edge-2", "edge-1", "edge-3"])),
            Arc::new(StringArray::from(vec!["", "", ""])),
            Arc::new(StringArray::from(vec!["", "", ""])),
        ],
    )
    .unwrap();
    ctx.register_table(
        "attribution_provenance",
        Arc::new(MemTable::try_new(provenance_schema, vec![vec![provenance]]).unwrap()),
    )
    .unwrap();

    ctx
}

fn string_list_value(array: &ListArray, row: usize) -> Vec<String> {
    let value = array.value(row);
    let strings = value.as_any().downcast_ref::<StringArray>().unwrap();
    strings.iter().map(|s| s.unwrap().to_owned()).collect()
}

#[tokio::test]
async fn one_row_per_conversation_with_latest_turn_opening_message_summary_and_edges() {
    let ctx = context();
    let df = ctx.sql(CONTEXT_SQL).await.unwrap();
    let batches = df.collect().await.unwrap();
    let rows: usize = batches.iter().map(RecordBatch::num_rows).sum();
    // conv-a (two turns, three messages, two summaries, two attribution
    // rows) and conv-b (one of each) each collapse to exactly one row;
    // conv-c never appears — a fan-out bug would report more than one row
    // for conv-a.
    assert_eq!(rows, 2, "one row per conversation carrying any context");

    let batch = &batches[0];
    let ids = batch
        .column_by_name("id")
        .unwrap()
        .as_any()
        .downcast_ref::<StringArray>()
        .unwrap();
    let latest_turn_ids = batch
        .column_by_name("latest_turn_id")
        .unwrap()
        .as_any()
        .downcast_ref::<StringArray>()
        .unwrap();
    let previews = batch
        .column_by_name("opening_message_preview")
        .unwrap()
        .as_any()
        .downcast_ref::<StringArray>()
        .unwrap();
    let summaries = batch
        .column_by_name("latest_summary_text")
        .unwrap()
        .as_any()
        .downcast_ref::<StringArray>()
        .unwrap();
    let edge_ids = batch
        .column_by_name("edge_ids")
        .unwrap()
        .as_any()
        .downcast_ref::<ListArray>()
        .unwrap();

    let a = ids
        .iter()
        .position(|id| id == Some("conv-a"))
        .expect("conv-a present");
    assert_eq!(
        latest_turn_ids.value(a),
        "turn-2",
        "turn-2 has the greater complete_position"
    );
    assert_eq!(
        previews.value(a),
        "hello from conv-a, the real opening message",
        "the system row and the internal_only user row are both skipped"
    );
    assert_eq!(
        summaries.value(a),
        "latest summary",
        "sum-2 has the greater covers_through_position"
    );
    assert_eq!(
        string_list_value(edge_ids, a),
        vec!["edge-1".to_owned(), "edge-2".to_owned()],
        "conv-a's two attribution rows post through two distinct edges, sorted"
    );

    let b = ids
        .iter()
        .position(|id| id == Some("conv-b"))
        .expect("conv-b present");
    assert_eq!(latest_turn_ids.value(b), "turn-only");
    assert_eq!(previews.value(b), "hi from conv-b");
    assert_eq!(summaries.value(b), "conv-b's only summary");
    assert_eq!(string_list_value(edge_ids, b), vec!["edge-3".to_owned()]);
}

#[tokio::test]
async fn opening_message_preview_is_cut_to_two_hundred_characters() {
    let ctx = SessionContext::new();

    let turns_schema = schema(CONVERSATION_CORE_ENTRY, CONVERSATION_TURNS);
    let turns = RecordBatch::try_new(
        Arc::clone(&turns_schema),
        vec![
            Arc::new(StringArray::from(vec!["conv-long"])),
            Arc::new(fixed_incarnation(1)),
            Arc::new(StringArray::from(vec!["turn-1"])),
            Arc::new(UInt64Array::from(vec![0_u64])),
            Arc::new(UInt64Array::from(vec![1_u64])),
            Arc::new(UInt64Array::from(vec![2_u64])),
        ],
    )
    .unwrap();
    ctx.register_table(
        "turns",
        Arc::new(MemTable::try_new(turns_schema, vec![vec![turns]]).unwrap()),
    )
    .unwrap();

    let long_text: String = "a".repeat(5_000);
    let messages_schema = schema(CONVERSATION_CORE_ENTRY, CONVERSATION_MESSAGES);
    let messages = RecordBatch::try_new(
        Arc::clone(&messages_schema),
        vec![
            Arc::new(StringArray::from(vec!["conv-long"])),
            Arc::new(fixed_incarnation(1)),
            Arc::new(UInt64Array::from(vec![0_u64])),
            Arc::new(StringArray::from(vec!["turn-1"])),
            Arc::new(StringArray::from(vec!["user"])),
            Arc::new(BooleanArray::from(vec![false])),
            Arc::new(BooleanArray::from(vec![false])),
            Arc::new(StringArray::from(vec![long_text.as_str()])),
            Arc::new(StringArray::from(vec!["trusted"])),
        ],
    )
    .unwrap();
    ctx.register_table(
        "messages",
        Arc::new(MemTable::try_new(messages_schema, vec![vec![messages]]).unwrap()),
    )
    .unwrap();

    let summary_schema = schema(CONVERSATION_EXECUTION_ENTRY, EXECUTION_SUMMARY);
    let summary = RecordBatch::new_empty(summary_schema.clone());
    ctx.register_table(
        "summary",
        Arc::new(MemTable::try_new(summary_schema, vec![vec![summary]]).unwrap()),
    )
    .unwrap();

    let attribution_schema = schema(CONVERSATION_SECURITY_ENTRY, SECURITY_ATTRIBUTION);
    let attribution = RecordBatch::new_empty(attribution_schema.clone());
    ctx.register_table(
        "attribution",
        Arc::new(MemTable::try_new(attribution_schema, vec![vec![attribution]]).unwrap()),
    )
    .unwrap();

    let provenance_schema = schema(CONVERSATION_SECURITY_ENTRY, SECURITY_ATTRIBUTION_PROVENANCE);
    let provenance = RecordBatch::new_empty(provenance_schema.clone());
    ctx.register_table(
        "attribution_provenance",
        Arc::new(MemTable::try_new(provenance_schema, vec![vec![provenance]]).unwrap()),
    )
    .unwrap();

    let df = ctx.sql(CONTEXT_SQL).await.unwrap();
    let batches = df.collect().await.unwrap();
    let batch = &batches[0];
    let previews = batch
        .column_by_name("opening_message_preview")
        .unwrap()
        .as_any()
        .downcast_ref::<StringArray>()
        .unwrap();
    assert_eq!(
        previews.value(0).chars().count(),
        200,
        "the statement itself cuts the preview to 200 characters — the wire never carries more"
    );
}

/// Directional timing at a synthetic-fleet scale: 1,000 conversations, 100
/// messages each (100,000 `messages` rows — the table none of the three
/// earlier dashboard statements reads, per `ExactParquetTable`'s own
/// doc — `crates/query/src/core_execution/provider.rs` — pushing projection
/// and `LIMIT` but never a filter, so this statement's `WHERE role = 'user'`
/// predicate cannot prune the scan), two summaries and two attribution rows
/// each.
///
/// Ignored by default: `cargo nextest run -p polyc-query --test
/// dashboard_context_sql -- --ignored` (or `cargo test -- --ignored`).
/// SQL-planning-and-execution time over in-memory Arrow batches only — it
/// does NOT include the request path's Parquet reads from object storage,
/// credential/grant admission, or JSON encoding, so it is a lower bound on
/// the real `/api/dashboard` request's added cost, not the number itself.
/// No harness in this repository seeds a real projected fleet at this
/// scale (`crates/query-service/tests/production_composition.rs` composes
/// real planes but not at fleet size), so this is the cheapest real signal
/// available without building one. Observed at introduction (debug profile,
/// developer machine, five runs): median 589.8ms, worst 605.2ms — the SQL
/// itself is not pathologically slow at this scale, but this excludes the
/// Parquet-read and network cost `ExactParquetTable`'s unfiltered scan adds
/// in production, which is unmeasured.
#[tokio::test]
#[ignore = "synthetic-scale timing probe, not a correctness test — run manually"]
#[allow(
    clippy::too_many_lines,
    reason = "one MemTable per one of the five tables this statement reads, at fleet scale, \
              each already a single clearly-named fixture; splitting further would only add \
              indirection for one test-only call site"
)]
async fn dashboard_context_sql_at_synthetic_fleet_scale() {
    const CONVERSATIONS: usize = 1_000;
    const MESSAGES_PER_CONVERSATION: usize = 100;
    const RUNS: usize = 5;

    let ctx = SessionContext::new();
    let ids: Vec<String> = (0..CONVERSATIONS).map(|i| format!("conv-{i:05}")).collect();

    let turns_schema = schema(CONVERSATION_CORE_ENTRY, CONVERSATION_TURNS);
    let turns = RecordBatch::try_new(
        Arc::clone(&turns_schema),
        vec![
            Arc::new(StringArray::from(ids.clone())),
            Arc::new(fixed_incarnation(CONVERSATIONS)),
            Arc::new(StringArray::from(
                ids.iter()
                    .map(|id| format!("{id}-turn"))
                    .collect::<Vec<_>>(),
            )),
            Arc::new(UInt64Array::from(vec![0_u64; CONVERSATIONS])),
            Arc::new(UInt64Array::from(vec![1_u64; CONVERSATIONS])),
            Arc::new(UInt64Array::from(vec![2_u64; CONVERSATIONS])),
        ],
    )
    .unwrap();
    ctx.register_table(
        "turns",
        Arc::new(MemTable::try_new(turns_schema, vec![vec![turns]]).unwrap()),
    )
    .unwrap();

    let total_messages = CONVERSATIONS * MESSAGES_PER_CONVERSATION;
    let message_partitions: Vec<&str> = ids
        .iter()
        .flat_map(|id| std::iter::repeat_n(id.as_str(), MESSAGES_PER_CONVERSATION))
        .collect();
    let message_turn_ids: Vec<String> = ids
        .iter()
        .flat_map(|id| std::iter::repeat_n(format!("{id}-turn"), MESSAGES_PER_CONVERSATION))
        .collect();
    let message_positions: Vec<u64> = (0..CONVERSATIONS)
        .flat_map(|_| 0..u64::try_from(MESSAGES_PER_CONVERSATION).unwrap())
        .collect();
    let message_roles: Vec<&str> = (0..total_messages).map(|_| "user").collect();
    let message_text: String = "m".repeat(400);
    let message_texts: Vec<&str> = (0..total_messages).map(|_| message_text.as_str()).collect();
    let message_trust: Vec<&str> = (0..total_messages).map(|_| "trusted").collect();

    let messages_schema = schema(CONVERSATION_CORE_ENTRY, CONVERSATION_MESSAGES);
    let messages = RecordBatch::try_new(
        Arc::clone(&messages_schema),
        vec![
            Arc::new(StringArray::from(message_partitions)),
            Arc::new(fixed_incarnation(total_messages)),
            Arc::new(UInt64Array::from(message_positions)),
            Arc::new(StringArray::from(message_turn_ids)),
            Arc::new(StringArray::from(message_roles)),
            Arc::new(BooleanArray::from(vec![false; total_messages])),
            Arc::new(BooleanArray::from(vec![false; total_messages])),
            Arc::new(StringArray::from(message_texts)),
            Arc::new(StringArray::from(message_trust)),
        ],
    )
    .unwrap();
    ctx.register_table(
        "messages",
        Arc::new(MemTable::try_new(messages_schema, vec![vec![messages]]).unwrap()),
    )
    .unwrap();

    let summary_partitions: Vec<&str> = ids
        .iter()
        .flat_map(|id| std::iter::repeat_n(id.as_str(), 2))
        .collect();
    let summary_count = summary_partitions.len();
    let summary_schema = schema(CONVERSATION_EXECUTION_ENTRY, EXECUTION_SUMMARY);
    let summary = RecordBatch::try_new(
        Arc::clone(&summary_schema),
        vec![
            Arc::new(StringArray::from(summary_partitions)),
            Arc::new(fixed_incarnation(summary_count)),
            Arc::new(UInt64Array::from(
                (0..CONVERSATIONS)
                    .flat_map(|_| [0_u64, 1])
                    .collect::<Vec<_>>(),
            )),
            Arc::new(StringArray::from(vec!["sum"; summary_count])),
            Arc::new(StringArray::from(vec!["summary text"; summary_count])),
            Arc::new(UInt64Array::from(
                (0..CONVERSATIONS)
                    .flat_map(|_| [0_u64, 1])
                    .collect::<Vec<_>>(),
            )),
        ],
    )
    .unwrap();
    ctx.register_table(
        "summary",
        Arc::new(MemTable::try_new(summary_schema, vec![vec![summary]]).unwrap()),
    )
    .unwrap();

    let attribution_partitions: Vec<&str> = ids
        .iter()
        .flat_map(|id| std::iter::repeat_n(id.as_str(), 2))
        .collect();
    let attribution_count = attribution_partitions.len();
    let attribution_schema = schema(CONVERSATION_SECURITY_ENTRY, SECURITY_ATTRIBUTION);
    let attribution = RecordBatch::try_new(
        Arc::clone(&attribution_schema),
        vec![
            Arc::new(StringArray::from(attribution_partitions.clone())),
            Arc::new(fixed_incarnation(attribution_count)),
            Arc::new(UInt64Array::from(
                (0..CONVERSATIONS)
                    .flat_map(|_| [0_u64, 1])
                    .collect::<Vec<_>>(),
            )),
            Arc::new(StringArray::from(vec!["turn"; attribution_count])),
            Arc::new(StringArray::from(vec!["persona"; attribution_count])),
            Arc::new(StringArray::from(vec!["initiator"; attribution_count])),
        ],
    )
    .unwrap();
    ctx.register_table(
        "attribution",
        Arc::new(MemTable::try_new(attribution_schema, vec![vec![attribution]]).unwrap()),
    )
    .unwrap();

    let provenance_schema = schema(CONVERSATION_SECURITY_ENTRY, SECURITY_ATTRIBUTION_PROVENANCE);
    let provenance = RecordBatch::try_new(
        Arc::clone(&provenance_schema),
        vec![
            Arc::new(StringArray::from(attribution_partitions)),
            Arc::new(fixed_incarnation(attribution_count)),
            Arc::new(UInt64Array::from(
                (0..CONVERSATIONS)
                    .flat_map(|_| [0_u64, 1])
                    .collect::<Vec<_>>(),
            )),
            Arc::new(StringArray::from(vec!["team"; attribution_count])),
            Arc::new(StringArray::from(vec!["team"; attribution_count])),
            Arc::new(StringArray::from(vec!["u"; attribution_count])),
            Arc::new(StringArray::from(vec!["Name"; attribution_count])),
            Arc::new(StringArray::from(vec!["edge-1"; attribution_count])),
            Arc::new(StringArray::from(vec![""; attribution_count])),
            Arc::new(StringArray::from(vec![""; attribution_count])),
        ],
    )
    .unwrap();
    ctx.register_table(
        "attribution_provenance",
        Arc::new(MemTable::try_new(provenance_schema, vec![vec![provenance]]).unwrap()),
    )
    .unwrap();

    let mut durations = Vec::with_capacity(RUNS);
    for _ in 0..RUNS {
        let started = std::time::Instant::now();
        let df = ctx.sql(CONTEXT_SQL).await.unwrap();
        let batches = df.collect().await.unwrap();
        let elapsed = started.elapsed();
        let rows: usize = batches.iter().map(RecordBatch::num_rows).sum();
        assert_eq!(rows, CONVERSATIONS, "one row per conversation, at scale");
        durations.push(elapsed);
    }
    durations.sort();
    eprintln!(
        "dashboard_context.sql at {CONVERSATIONS} conversations x \
         {MESSAGES_PER_CONVERSATION} messages: median {:?}, worst {:?} over {RUNS} runs",
        durations[RUNS / 2],
        durations[RUNS - 1]
    );
}