vortex-datafusion 0.72.0

Apache Datafusion integration for Vortex
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
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
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
// SPDX-License-Identifier: Apache-2.0
// SPDX-FileCopyrightText: Copyright the Vortex contributors

//! Test that checks we can evolve schemas in a compatible way across files.

use std::sync::Arc;

use arrow_schema::DataType;
use arrow_schema::Field;
use arrow_schema::Fields;
use arrow_schema::Schema;
use datafusion::arrow::array::Array;
use datafusion::arrow::array::ArrayRef as ArrowArrayRef;
use datafusion::arrow::array::DictionaryArray;
use datafusion::arrow::array::RecordBatch;
use datafusion::arrow::array::StructArray;
use datafusion::arrow::datatypes::UInt16Type;
use datafusion::arrow::datatypes::UInt32Type;
use datafusion::assert_batches_sorted_eq;
use datafusion_common::assert_batches_eq;
use datafusion_common::create_array;
use datafusion_common::record_batch;
use datafusion_expr::col;
use datafusion_expr::lit;
use datafusion_functions::expr_fn::get_field;
use rstest::rstest;

use crate::common_tests::TestSessionContext;

#[rstest]
#[tokio::test]
async fn test_filter_with_schema_evolution(
    #[values(false, true)] projection_pushdown: bool,
) -> anyhow::Result<()> {
    let ctx = TestSessionContext::new(projection_pushdown);

    // file1 only contains field "a"
    ctx.write_arrow_batch(
        "files/file1.vortex",
        &record_batch!(("a", Utf8, vec![Some("one"), Some("two"), Some("three")]))?,
    )
    .await?;

    // file2 only contains field "b"
    ctx.write_arrow_batch(
        "files/file2.vortex",
        &record_batch!(("b", Utf8, vec![Some("four"), Some("five"), Some("six")]))?,
    )
    .await?;

    ctx.session
        .sql(
            "CREATE EXTERNAL TABLE my_tbl \
                STORED AS vortex  \
                LOCATION '/files/'",
        )
        .await?;

    let table = ctx.session.table("my_tbl").await?;

    // Table schema contains both fields
    assert_eq!(
        table.schema().as_arrow(),
        &Schema::new(vec![
            Field::new("a", DataType::Utf8View, true),
            Field::new("b", DataType::Utf8View, true),
        ])
    );

    // Filter the result to only ones with a column, i.e. only file1
    let result = table.filter(col("a").is_not_null())?.collect().await?;

    let expected = [
        "+-------+---+",
        "| a     | b |",
        "+-------+---+",
        "| one   |   |",
        "| three |   |",
        "| two   |   |",
        "+-------+---+",
    ];
    assert_batches_sorted_eq!(expected, &result);

    Ok(())
}

#[rstest]
#[tokio::test]
async fn test_filter_schema_evolution_order(
    #[values(false, true)] projection_pushdown: bool,
) -> anyhow::Result<()> {
    let ctx = TestSessionContext::new(projection_pushdown);

    // file1 only contains field "a"
    ctx.write_arrow_batch(
        "files/file1.vortex",
        &record_batch!(("a", Int32, vec![Some(1), Some(3), Some(5)]))?,
    )
    .await?;

    // file2 containing fields "b" and "a", where "a" needs to be upcast at scan time.
    ctx.write_arrow_batch(
        "files/file2.vortex",
        &record_batch!(
            ("b", Utf8, vec![Some("two"), Some("four"), Some("six")]),
            ("a", Int16, vec![Some(2), Some(4), Some(6)])
        )?,
    )
    .await?;

    ctx.session
        .sql(
            "CREATE EXTERNAL TABLE my_tbl (a INT, b STRING) \
                STORED AS vortex  \
                LOCATION '/files/'",
        )
        .await?;

    let table = ctx.session.table("my_tbl").await?;

    // Table schema contains both fields
    assert_eq!(
        table.schema().as_arrow(),
        &Schema::new(vec![
            Field::new("a", DataType::Int32, true),
            Field::new("b", DataType::Utf8View, true),
        ])
    );

    // Filter referencing the b column, which only appears in file2
    let result = table
        .clone()
        .filter(col("b").eq(lit("two")))?
        .collect()
        .await?;

    assert_batches_eq!(
        &[
            "+---+-----+",
            "| a | b   |",
            "+---+-----+",
            "| 2 | two |",
            "+---+-----+",
        ],
        &result
    );

    // Filter on the "a" column, which has different types for each file
    let result = table.filter(col("a").gt_eq(lit(3i16)))?.collect().await?;

    // a field: present in both files
    // b field: only present in file2, file1 fills with nulls
    assert_batches_sorted_eq!(
        &[
            "+---+------+",
            "| a | b    |",
            "+---+------+",
            "| 3 |      |",
            "| 4 | four |",
            "| 5 |      |",
            "| 6 | six  |",
            "+---+------+",
        ],
        &result
    );

    Ok(())
}

/// Test for correct schema evolution behavior in the presence of nested struct fields.
/// We use a hypothetical schema of some observability data with "wide records", struct columns
/// with nullable payloads that may or may not be present for every file.
#[rstest]
#[tokio::test]
async fn test_filter_schema_evolution_struct_fields(
    #[values(false, true)] projection_pushdown: bool,
) -> anyhow::Result<()> {
    let ctx = TestSessionContext::new(projection_pushdown);

    fn make_metrics(
        hostname: &str,
        uptime: Vec<i64>,
        instance: Option<Vec<Option<&str>>>,
    ) -> RecordBatch {
        let values_array: ArrowArrayRef = create_array!(Int64, uptime);
        let payload_array = if let Some(tags) = instance {
            let tags_array: ArrowArrayRef = create_array!(Utf8, tags);
            Arc::new(StructArray::new(
                vec![
                    Field::new("uptime", DataType::Int64, true),
                    Field::new("instance", DataType::Utf8, true),
                ]
                .into(),
                vec![values_array, tags_array],
                None,
            ))
        } else {
            Arc::new(StructArray::new(
                vec![Field::new("uptime", DataType::Int64, true)].into(),
                vec![values_array],
                None,
            ))
        };

        let len = payload_array.len();
        let hostname_array = create_array!(Utf8, vec![Some(hostname); len]);

        let payload_type = payload_array.data_type().clone();
        let hostname_type = hostname_array.data_type().clone();

        RecordBatch::from(StructArray::new(
            vec![
                Field::new("hostname", hostname_type, true),
                Field::new("payload", payload_type, true),
            ]
            .into(),
            vec![hostname_array, payload_array],
            None,
        ))
    }

    let host01 = make_metrics("host01.local", vec![1, 2, 3, 4], None);
    let host02 = make_metrics(
        "host02.local",
        vec![10, 20, 30, 40],
        // host02 has new logging code which adds the new "instance" nested field in its payload
        Some(vec![Some("c6i"), Some("c6i"), Some("m5"), Some("r5")]),
    );

    // Write metrics files to storage
    ctx.write_arrow_batch("files/host01.vortex", &host01)
        .await?;
    ctx.write_arrow_batch("files/host02.vortex", &host02)
        .await?;

    let read_schema = host02.schema();

    let provider = ctx
        .table_provider("tbl", "/files/", Arc::clone(&read_schema))
        .await?;

    let table = ctx.session.read_table(provider)?;

    // Table schema contains both fields
    assert_eq!(table.schema().as_arrow(), read_schema.as_ref(),);

    // Scan all the records, NULLs are filled in for nested optional fields.
    let full_scan = table.clone().collect().await?;

    assert_batches_sorted_eq!(
        &[
            "+--------------+-----------------------------+",
            "| hostname     | payload                     |",
            "+--------------+-----------------------------+",
            "| host01.local | {uptime: 1, instance: }     |",
            "| host01.local | {uptime: 2, instance: }     |",
            "| host01.local | {uptime: 3, instance: }     |",
            "| host01.local | {uptime: 4, instance: }     |",
            "| host02.local | {uptime: 10, instance: c6i} |",
            "| host02.local | {uptime: 20, instance: c6i} |",
            "| host02.local | {uptime: 30, instance: m5}  |",
            "| host02.local | {uptime: 40, instance: r5}  |",
            "+--------------+-----------------------------+",
        ],
        &full_scan
    );

    // run a filter that touches both the payload.uptime AND the payload.instance nested fields
    let filtered_scan = table
        .filter(
            // payload.instance = 'c6i' OR payload.uptime < 10
            // We need to perform filtering over nested columns which don't exist in every
            // file type.
            get_field(col("payload"), "instance")
                .eq(lit("c6i"))
                .or(get_field(col("payload"), "uptime").lt(lit(10))),
        )?
        .collect()
        .await?;

    assert_batches_sorted_eq!(
        &[
            "+--------------+-----------------------------+",
            "| hostname     | payload                     |",
            "+--------------+-----------------------------+",
            "| host01.local | {uptime: 1, instance: }     |",
            "| host01.local | {uptime: 2, instance: }     |",
            "| host01.local | {uptime: 3, instance: }     |",
            "| host01.local | {uptime: 4, instance: }     |",
            "| host02.local | {uptime: 10, instance: c6i} |",
            "| host02.local | {uptime: 20, instance: c6i} |",
            "+--------------+-----------------------------+",
        ],
        &filtered_scan
    );

    Ok(())
}

#[rstest]
#[tokio::test]
async fn test_schema_evolution_struct_of_dict(
    #[values(false, true)] projection_pushdown: bool,
) -> anyhow::Result<()> {
    let ctx = TestSessionContext::new(projection_pushdown);

    // First file
    let struct_fields = Fields::from(vec![
        Field::new_dictionary("a", DataType::UInt16, DataType::Utf8, true),
        Field::new_dictionary("b", DataType::UInt16, DataType::Utf8, true),
    ]);
    let struct_array = StructArray::new(
        struct_fields.clone(),
        vec![
            Arc::new(DictionaryArray::<UInt16Type>::from_iter(["x1", "y1", "x1"])),
            Arc::new(DictionaryArray::<UInt16Type>::from_iter(["p1", "p1", "q1"])),
        ],
        None,
    );

    let batch = RecordBatch::try_new(
        Arc::new(Schema::new(vec![Field::new(
            "my_struct",
            DataType::Struct(struct_fields),
            true,
        )])),
        vec![Arc::new(struct_array)],
    )?;

    ctx.write_arrow_batch("files/file1.vortex", &batch).await?;

    // Second file
    let struct_fields = Fields::from(vec![
        Field::new_dictionary("a", DataType::UInt32, DataType::Utf8, true),
        Field::new_dictionary("b", DataType::UInt32, DataType::Utf8, true),
        Field::new_dictionary("c", DataType::UInt32, DataType::Utf8, true),
    ]);
    let struct_array = StructArray::new(
        struct_fields.clone(),
        vec![
            Arc::new(DictionaryArray::<UInt32Type>::from_iter(["x2", "y2", "x2"])),
            Arc::new(DictionaryArray::<UInt32Type>::from_iter(["p2", "p2", "q2"])),
            Arc::new(DictionaryArray::<UInt32Type>::from_iter(["a2", "b2", "c2"])),
        ],
        None,
    );

    let batch = RecordBatch::try_new(
        Arc::new(Schema::new(vec![Field::new(
            "my_struct",
            DataType::Struct(struct_fields.clone()),
            true,
        )])),
        vec![Arc::new(struct_array)],
    )?;

    ctx.write_arrow_batch("files/file2.vortex", &batch).await?;

    let read_schema = batch.schema();

    let provider = ctx
        .table_provider("tbl", "/files/", Arc::clone(&read_schema))
        .await?;

    let table = ctx.session.read_table(provider)?;

    assert_eq!(table.schema().as_arrow(), read_schema.as_ref());

    let full_scan = table.clone().collect().await?;

    assert_batches_sorted_eq!(
        &[
            "+-----------------------+",
            "| my_struct             |",
            "+-----------------------+",
            "| {a: x1, b: p1, c: }   |",
            "| {a: x1, b: q1, c: }   |",
            "| {a: x2, b: p2, c: a2} |",
            "| {a: x2, b: q2, c: c2} |",
            "| {a: y1, b: p1, c: }   |",
            "| {a: y2, b: p2, c: b2} |",
            "+-----------------------+",
        ],
        &full_scan
    );

    let filter =
        get_field(col("my_struct"), "a")
            .eq(lit("x1"))
            .or(get_field(col("my_struct"), "a").eq(lit("x2")));
    // run a filter that touches both the payload.uptime AND the payload.instance nested fields

    let filtered_scan = table.filter(filter)?.collect().await?;

    assert_eq!(filtered_scan[0].schema(), read_schema);

    assert_batches_sorted_eq!(
        &[
            "+-----------------------+",
            "| my_struct             |",
            "+-----------------------+",
            "| {a: x1, b: p1, c: }   |",
            "| {a: x1, b: q1, c: }   |",
            "| {a: x2, b: p2, c: a2} |",
            "| {a: x2, b: q2, c: c2} |",
            "+-----------------------+",
        ],
        &filtered_scan
    );

    Ok(())
}

#[rstest]
#[tokio::test]
async fn test_schema_evolution_struct_field_order(
    #[values(false, true)] projection_pushdown: bool,
) -> anyhow::Result<()> {
    let ctx = TestSessionContext::new(projection_pushdown);

    // File1: labels = {region, service} - service at position 1
    let file1_labels: ArrowArrayRef = Arc::new(StructArray::new(
        Fields::from(vec![
            Field::new("region", DataType::Utf8, true),
            Field::new("service", DataType::Utf8, true),
        ]),
        vec![
            create_array!(Utf8, vec![Some("us-east"), Some("us-west")]),
            create_array!(Utf8, vec![Some("api"), Some("api")]),
        ],
        None,
    ));

    ctx.write_arrow_batch(
        "reorder/file1.vortex",
        &RecordBatch::try_from_iter([("labels", file1_labels)])?,
    )
    .await?;

    // File2: labels = {service, instance, job} - service at position 0
    let file2_labels: ArrowArrayRef = Arc::new(StructArray::new(
        Fields::from(vec![
            Field::new("service", DataType::Utf8, true),
            Field::new("instance", DataType::Utf8, true),
            Field::new("job", DataType::Utf8, true),
        ]),
        vec![
            create_array!(Utf8, vec![Some("api"), Some("api")]),
            create_array!(Utf8, vec![Some("host-0"), Some("host-1")]),
            create_array!(Utf8, vec![Some("scraper"), Some("scraper")]),
        ],
        None,
    ));
    ctx.write_arrow_batch(
        "reorder/file2.vortex",
        &RecordBatch::try_from_iter([("labels", file2_labels)])?,
    )
    .await?;

    let target_schema = Arc::new(Schema::new(vec![Field::new(
        "labels",
        DataType::Struct(Fields::from(vec![
            Field::new("region", DataType::Utf8, true),
            Field::new("service", DataType::Utf8, true),
            Field::new("instance", DataType::Utf8, true),
            Field::new("job", DataType::Utf8, true),
        ])),
        true,
    )]));

    let table = ctx.table_provider("tbl", "/reorder", target_schema).await?;

    let result = ctx
        .session
        .read_table(table)?
        .select(vec![
            get_field(col("labels"), "region").alias("region"),
            get_field(col("labels"), "service").alias("service"),
            get_field(col("labels"), "instance").alias("instance"),
            get_field(col("labels"), "job").alias("job"),
        ])?
        .collect()
        .await?;

    assert_batches_sorted_eq!(
        [
            "+---------+---------+----------+---------+",
            "| region  | service | instance | job     |",
            "+---------+---------+----------+---------+",
            "| us-east | api     |          |         |",
            "| us-west | api     |          |         |",
            "|         | api     | host-0   | scraper |",
            "|         | api     | host-1   | scraper |",
            "+---------+---------+----------+---------+",
        ],
        &result
    );

    Ok(())
}

/// Test that complex projection expressions (arithmetic) work correctly
/// with both projection pushdown enabled and disabled.
#[rstest]
#[tokio::test]
async fn test_projection_expressions(
    #[values(false, true)] projection_pushdown: bool,
) -> anyhow::Result<()> {
    let ctx = TestSessionContext::new(projection_pushdown);

    ctx.write_arrow_batch(
        "files/data.vortex",
        &record_batch!(
            ("a", Int32, vec![Some(1), Some(2), Some(3)]),
            ("b", Int32, vec![Some(10), Some(20), Some(30)])
        )?,
    )
    .await?;

    ctx.session
        .sql(
            "CREATE EXTERNAL TABLE my_tbl \
                STORED AS vortex  \
                LOCATION '/files/'",
        )
        .await?;

    let table = ctx.session.table("my_tbl").await?;

    // Test arithmetic projection: a + b * 2
    let result = table
        .select(vec![(col("a") + col("b") * lit(2)).alias("computed")])?
        .collect()
        .await?;

    assert_batches_eq!(
        &[
            "+----------+",
            "| computed |",
            "+----------+",
            "| 21       |",
            "| 42       |",
            "| 63       |",
            "+----------+",
        ],
        &result
    );

    Ok(())
}

/// Test that Dictionary columns are preserved correctly when scanning with a defined schema.
/// This reproduces an issue from the polarsignals benchmark where Dictionary(UInt32, Utf8)
/// columns were being returned as Utf8View.
#[rstest]
#[tokio::test]
async fn test_dictionary_column_type_preservation(
    #[values(false, true)] projection_pushdown: bool,
) -> anyhow::Result<()> {
    let ctx = TestSessionContext::new(projection_pushdown);

    // Create a batch with Dictionary columns (like polarsignals schema)
    let dict_fields = Fields::from(vec![
        Field::new_dictionary("producer", DataType::UInt32, DataType::Utf8, false),
        Field::new_dictionary("sample_type", DataType::UInt32, DataType::Utf8, false),
    ]);

    let producer_array: ArrowArrayRef = Arc::new(DictionaryArray::<UInt32Type>::from_iter([
        "agent", "agent", "agent",
    ]));
    let sample_type_array: ArrowArrayRef = Arc::new(DictionaryArray::<UInt32Type>::from_iter([
        "samples", "samples", "samples",
    ]));

    let batch = RecordBatch::try_new(
        Arc::new(Schema::new(dict_fields.to_vec())),
        vec![producer_array, sample_type_array],
    )?;

    ctx.write_arrow_batch("files/data.vortex", &batch).await?;

    // Create table with explicit schema that expects Dictionary types
    let table_schema = batch.schema();
    let provider = ctx
        .table_provider("tbl", "/files/", Arc::clone(&table_schema))
        .await?;

    let table = ctx.session.read_table(provider)?;

    // Verify the schema matches
    assert_eq!(table.schema().as_arrow(), table_schema.as_ref());

    // Query and verify the result schema preserves Dictionary types
    let result = table
        .filter(col("producer").eq(lit("agent")))?
        .collect()
        .await?;

    assert!(!result.is_empty(), "Expected results from query");

    // Check that the result schema preserves Dictionary types
    let result_schema = result[0].schema();
    assert_eq!(
        result_schema.field(0).data_type(),
        &DataType::Dictionary(Box::new(DataType::UInt32), Box::new(DataType::Utf8)),
        "producer column should preserve Dictionary type"
    );
    assert_eq!(
        result_schema.field(1).data_type(),
        &DataType::Dictionary(Box::new(DataType::UInt32), Box::new(DataType::Utf8)),
        "sample_type column should preserve Dictionary type"
    );

    Ok(())
}

/// Test that nested struct fields with Dictionary types are preserved correctly.
/// This reproduces the polarsignals benchmark issue where accessing `labels.comm`
/// (a Dictionary field inside a struct) returns Utf8View instead of Dictionary.
#[rstest]
#[tokio::test]
async fn test_nested_struct_dictionary_type_preservation(
    #[values(false, true)] projection_pushdown: bool,
) -> anyhow::Result<()> {
    let ctx = TestSessionContext::new(projection_pushdown);

    // Create a struct with Dictionary fields (like polarsignals labels)
    let labels_fields = Fields::from(vec![
        Field::new_dictionary("comm", DataType::UInt32, DataType::Utf8, true),
        Field::new_dictionary("node", DataType::UInt32, DataType::Utf8, true),
    ]);

    let comm_array: ArrowArrayRef = Arc::new(DictionaryArray::<UInt32Type>::from_iter([
        "proc_a", "proc_b", "proc_a",
    ]));
    let node_array: ArrowArrayRef = Arc::new(DictionaryArray::<UInt32Type>::from_iter([
        "node_1", "node_1", "node_2",
    ]));

    let labels_struct = StructArray::new(labels_fields.clone(), vec![comm_array, node_array], None);

    // Add other columns like in polarsignals
    let value_array = create_array!(Int64, vec![Some(100i64), Some(200), Some(300)]);
    let producer_array: ArrowArrayRef = Arc::new(DictionaryArray::<UInt32Type>::from_iter([
        "agent", "agent", "agent",
    ]));

    let schema = Arc::new(Schema::new(vec![
        Field::new("labels", DataType::Struct(labels_fields.clone()), false),
        Field::new("value", DataType::Int64, false),
        Field::new_dictionary("producer", DataType::UInt32, DataType::Utf8, false),
    ]));

    let batch = RecordBatch::try_new(
        Arc::clone(&schema),
        vec![Arc::new(labels_struct), value_array, producer_array],
    )?;

    ctx.write_arrow_batch("files/data.vortex", &batch).await?;

    let provider = ctx
        .table_provider("tbl", "/files/", Arc::clone(&schema))
        .await?;
    let table = ctx.session.read_table(provider)?;

    // Query that projects a nested struct field (like in polarsignals Q0)
    let result = table
        .clone()
        .filter(col("producer").eq(lit("agent")))?
        .select(vec![
            col("value"),
            get_field(col("labels"), "comm").alias("comm"),
        ])?
        .collect()
        .await?;

    assert!(!result.is_empty(), "Expected results from query");

    // The nested dictionary field should preserve its type
    let result_schema = result[0].schema();
    assert_eq!(
        result_schema.field(1).data_type(),
        &DataType::Dictionary(Box::new(DataType::UInt32), Box::new(DataType::Utf8)),
        "labels.comm should preserve Dictionary type, got {:?}",
        result_schema.field(1).data_type()
    );

    Ok(())
}

/// Test reproducing the polarsignals benchmark schema with multiple dictionary columns
/// and filters on dictionary columns.
#[rstest]
#[tokio::test]
async fn test_polarsignals_like_schema(
    #[values(false, true)] projection_pushdown: bool,
) -> anyhow::Result<()> {
    let ctx = TestSessionContext::new(projection_pushdown);

    // Create labels struct with dictionary fields
    let labels_fields = Fields::from(vec![Field::new_dictionary(
        "comm",
        DataType::UInt32,
        DataType::Utf8,
        true,
    )]);

    let comm_array: ArrowArrayRef = Arc::new(DictionaryArray::<UInt32Type>::from_iter([
        "proc_a", "proc_b", "proc_a",
    ]));

    let labels_struct = StructArray::new(labels_fields.clone(), vec![comm_array], None);

    // Create multiple dictionary columns like polarsignals
    let value_array = create_array!(Int64, vec![Some(1i64), Some(2), Some(3)]);
    let producer_array: ArrowArrayRef = Arc::new(DictionaryArray::<UInt32Type>::from_iter([
        "agent", "agent", "agent",
    ]));
    let sample_type_array: ArrowArrayRef = Arc::new(DictionaryArray::<UInt32Type>::from_iter([
        "samples", "samples", "samples",
    ]));
    let sample_unit_array: ArrowArrayRef = Arc::new(DictionaryArray::<UInt32Type>::from_iter([
        "count", "count", "count",
    ]));
    let period_type_array: ArrowArrayRef = Arc::new(DictionaryArray::<UInt32Type>::from_iter([
        "cpu", "cpu", "cpu",
    ]));
    let period_unit_array: ArrowArrayRef = Arc::new(DictionaryArray::<UInt32Type>::from_iter([
        "nanoseconds",
        "nanoseconds",
        "nanoseconds",
    ]));

    let schema = Arc::new(Schema::new(vec![
        Field::new("labels", DataType::Struct(labels_fields.clone()), false),
        Field::new("value", DataType::Int64, false),
        Field::new_dictionary("producer", DataType::UInt32, DataType::Utf8, false),
        Field::new_dictionary("sample_type", DataType::UInt32, DataType::Utf8, false),
        Field::new_dictionary("sample_unit", DataType::UInt32, DataType::Utf8, false),
        Field::new_dictionary("period_type", DataType::UInt32, DataType::Utf8, false),
        Field::new_dictionary("period_unit", DataType::UInt32, DataType::Utf8, false),
    ]));

    let batch = RecordBatch::try_new(
        Arc::clone(&schema),
        vec![
            Arc::new(labels_struct),
            value_array,
            producer_array,
            sample_type_array,
            sample_unit_array,
            period_type_array,
            period_unit_array,
        ],
    )?;

    ctx.write_arrow_batch("files/data.vortex", &batch).await?;

    let provider = ctx
        .table_provider("tbl", "/files/", Arc::clone(&schema))
        .await?;
    let table = ctx.session.read_table(provider)?;

    // Query like polarsignals Q0: filter on multiple dictionary columns, project value and labels.comm
    let result = table
        .filter(col("producer").eq(lit("agent")))?
        .filter(col("sample_type").eq(lit("samples")))?
        .filter(col("sample_unit").eq(lit("count")))?
        .filter(col("period_type").eq(lit("cpu")))?
        .filter(col("period_unit").eq(lit("nanoseconds")))?
        .select(vec![
            col("value"),
            get_field(col("labels"), "comm").alias("comm"),
        ])?
        .collect()
        .await?;

    assert!(!result.is_empty(), "Expected results from query");

    // Verify result values
    assert_batches_eq!(
        &[
            "+-------+--------+",
            "| value | comm   |",
            "+-------+--------+",
            "| 1     | proc_a |",
            "| 2     | proc_b |",
            "| 3     | proc_a |",
            "+-------+--------+",
        ],
        &result
    );

    Ok(())
}

/// Test using SQL to create an external table (closer to how benchmarks work).
/// This tests that Dictionary column types are preserved when using ListingTable.
#[rstest]
#[tokio::test]
async fn test_external_table_dictionary_columns(
    #[values(false, true)] projection_pushdown: bool,
) -> anyhow::Result<()> {
    let ctx = TestSessionContext::new(projection_pushdown);

    // Create a simple batch with dictionary columns
    let producer_array: ArrowArrayRef = Arc::new(DictionaryArray::<UInt32Type>::from_iter([
        "agent", "agent", "agent",
    ]));
    let sample_type_array: ArrowArrayRef = Arc::new(DictionaryArray::<UInt32Type>::from_iter([
        "samples", "samples", "samples",
    ]));
    let value_array = create_array!(Int64, vec![Some(1i64), Some(2), Some(3)]);

    let schema = Arc::new(Schema::new(vec![
        Field::new_dictionary("producer", DataType::UInt32, DataType::Utf8, false),
        Field::new_dictionary("sample_type", DataType::UInt32, DataType::Utf8, false),
        Field::new("value", DataType::Int64, false),
    ]));

    let batch = RecordBatch::try_new(
        Arc::clone(&schema),
        vec![producer_array, sample_type_array, value_array],
    )?;

    ctx.write_arrow_batch("files/data.vortex", &batch).await?;

    // Use SQL to create external table (like the benchmark does)
    ctx.session
        .sql(
            "CREATE EXTERNAL TABLE stacktraces \
                STORED AS vortex \
                LOCATION '/files/'",
        )
        .await?;

    // Query with filter on dictionary column and projection
    let result = ctx
        .session
        .sql(
            "SELECT value, sample_type FROM stacktraces \
             WHERE producer = 'agent' AND sample_type = 'samples'",
        )
        .await?
        .collect()
        .await?;

    assert!(!result.is_empty(), "Expected results from query");

    assert_batches_eq!(
        &[
            "+-------+-------------+",
            "| value | sample_type |",
            "+-------+-------------+",
            "| 1     | samples     |",
            "| 2     | samples     |",
            "| 3     | samples     |",
            "+-------+-------------+",
        ],
        &result
    );

    Ok(())
}

/// Test using SQL to access struct fields with dictionary types (like polarsignals Q0).
/// This reproduces the polarsignals benchmark error where `labels.comm` returns
/// Utf8View instead of Dictionary(UInt32, Utf8).
#[rstest]
#[tokio::test]
async fn test_sql_struct_field_dictionary_type(
    #[values(false, true)] projection_pushdown: bool,
) -> anyhow::Result<()> {
    let ctx = TestSessionContext::new(projection_pushdown);

    // Create labels struct with dictionary fields (like polarsignals)
    let labels_fields = Fields::from(vec![
        Field::new_dictionary("comm", DataType::UInt32, DataType::Utf8, true),
        Field::new_dictionary("node", DataType::UInt32, DataType::Utf8, true),
    ]);

    let comm_array: ArrowArrayRef = Arc::new(DictionaryArray::<UInt32Type>::from_iter([
        "proc_a", "proc_b", "proc_a",
    ]));
    let node_array: ArrowArrayRef = Arc::new(DictionaryArray::<UInt32Type>::from_iter([
        "node_1", "node_1", "node_2",
    ]));

    let labels_struct = StructArray::new(labels_fields.clone(), vec![comm_array, node_array], None);

    // Add other columns like in polarsignals
    let value_array = create_array!(Int64, vec![Some(1i64), Some(2), Some(3)]);
    let producer_array: ArrowArrayRef = Arc::new(DictionaryArray::<UInt32Type>::from_iter([
        "agent", "agent", "agent",
    ]));
    let sample_type_array: ArrowArrayRef = Arc::new(DictionaryArray::<UInt32Type>::from_iter([
        "samples", "samples", "samples",
    ]));

    let schema = Arc::new(Schema::new(vec![
        Field::new("labels", DataType::Struct(labels_fields.clone()), false),
        Field::new("value", DataType::Int64, false),
        Field::new_dictionary("producer", DataType::UInt32, DataType::Utf8, false),
        Field::new_dictionary("sample_type", DataType::UInt32, DataType::Utf8, false),
    ]));

    let batch = RecordBatch::try_new(
        Arc::clone(&schema),
        vec![
            Arc::new(labels_struct),
            value_array,
            producer_array,
            sample_type_array,
        ],
    )?;

    ctx.write_arrow_batch("files/data.vortex", &batch).await?;

    // Create table provider with explicit schema (like the benchmark)
    let provider = ctx.table_provider("stacktraces", "/files/", schema).await?;
    ctx.session.register_table("stacktraces", provider)?;

    // Query like polarsignals Q0: filter on dictionary columns, project struct field
    let result = ctx
        .session
        .sql(
            "SELECT value, labels.comm FROM stacktraces \
             WHERE producer = 'agent' AND sample_type = 'samples'",
        )
        .await?
        .collect()
        .await?;

    assert!(!result.is_empty(), "Expected results from query");

    // Verify the result values
    assert_batches_eq!(
        [
            "+-------+--------------------------+",
            "| value | stacktraces.labels[comm] |",
            "+-------+--------------------------+",
            "| 1     | proc_a                   |",
            "| 2     | proc_b                   |",
            "| 3     | proc_a                   |",
            "+-------+--------------------------+",
        ],
        &result
    );

    // Verify that labels.comm preserves Dictionary type
    let result_schema = result[0].schema();
    assert_eq!(
        result_schema.field(1).data_type(),
        &DataType::Dictionary(Box::new(DataType::UInt32), Box::new(DataType::Utf8)),
        "labels.comm should preserve Dictionary type, got {:?}",
        result_schema.field(1).data_type()
    );

    Ok(())
}