minarrow 0.10.1

Apache Arrow-compatible, Rust-first columnar data library for high-performance computing, native streaming, and embedded workloads. Minimal dependencies, ultra-low-latency access, automatic 64-byte SIMD alignment, and fast compile times. Great for real-time analytics, HPC pipelines, and systems integration.
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
// Copyright 2025 Peter Garfield Bower
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

use std::sync::Arc;

#[cfg(feature = "scalar_type")]
use crate::Scalar;
use crate::enums::error::{KernelError, MinarrowError};
use crate::enums::operators::ArithmeticOperator;
use crate::kernels::broadcast::array::broadcast_array_add;
use crate::kernels::broadcast::broadcast_value;
use crate::kernels::broadcast::table_view::broadcast_tableview_to_arrayview;
use crate::kernels::routing::arithmetic::resolve_binary_arithmetic;
use crate::structs::field_array::create_field_for_array;
use crate::{Array, ArrayV, Bitmask, Field, FieldArray, Table, TableV, Value};
#[cfg(feature = "chunked")]
use crate::{SuperArray, SuperArrayV, SuperTable, SuperTableV};

/// General table broadcasting function that supports all arithmetic operators
pub fn broadcast_table_with_operator(
    op: ArithmeticOperator,
    table_l: impl Into<TableV>,
    table_r: impl Into<TableV>,
) -> Result<Table, MinarrowError> {
    let table_l: TableV = table_l.into();
    let table_r: TableV = table_r.into();

    // Ensure tables have same number of columns
    if table_l.cols.len() != table_r.cols.len() {
        return Err(MinarrowError::ShapeError {
            message: format!(
                "Table column count mismatch: {} vs {}",
                table_l.cols.len(),
                table_r.cols.len()
            ),
        });
    }

    let mut result_field_arrays = Vec::new();

    for (i, (col_l, col_r)) in table_l.cols.iter().zip(table_r.cols.iter()).enumerate() {
        // Route through array broadcasting
        let result_array = resolve_binary_arithmetic(op, col_l.clone(), col_r.clone(), None)?;

        // Create new FieldArray with the field schema from the left table
        let result_field_array =
            FieldArray::new(table_l.fields[i].as_ref().clone(), result_array);
        result_field_arrays.push(result_field_array);
    }

    Ok(Table::new(table_l.name.clone(), Some(result_field_arrays)))
}

/// Broadcasts addition over table columns element-wise
///
/// Both tables must have the same number of columns and rows.
/// Addition is applied column-wise between corresponding columns.
pub fn broadcast_table_add(
    lhs: impl Into<TableV>,
    rhs: impl Into<TableV>,
    null_mask: Option<Arc<Bitmask>>,
) -> Result<Table, KernelError> {
    let lhs_table: TableV = lhs.into();
    let rhs_table: TableV = rhs.into();

    // Check shape compatibility
    if lhs_table.cols.len() != rhs_table.cols.len() {
        return Err(KernelError::BroadcastingError(format!(
            "Table column count mismatch: LHS {} cols, RHS {} cols",
            lhs_table.cols.len(),
            rhs_table.cols.len()
        )));
    }

    if lhs_table.len != rhs_table.len {
        return Err(KernelError::BroadcastingError(format!(
            "Table row count mismatch: LHS {} rows, RHS {} rows",
            lhs_table.len, rhs_table.len
        )));
    }

    // Apply addition column-wise
    let mut result_cols = Vec::with_capacity(lhs_table.cols.len());

    for (i, (lhs_col, rhs_col)) in lhs_table.cols.iter().zip(rhs_table.cols.iter()).enumerate() {
        let result_array =
            broadcast_array_add(lhs_col.clone(), rhs_col.clone(), null_mask.as_deref()).map_err(
                |e| KernelError::BroadcastingError(format!("Column {} addition failed: {}", i, e)),
            )?;

        // Create FieldArray with name from left table
        let field_name = if i < lhs_table.fields.len() {
            lhs_table.fields[i].name.clone()
        } else {
            format!("col_{}", i)
        };

        let field_dtype = if i < lhs_table.fields.len() {
            lhs_table.fields[i].dtype.clone()
        } else {
            result_array.arrow_type()
        };

        let field = Field::new(
            field_name,
            field_dtype,
            result_array.null_mask().is_some(), // nullable based on result array
            None,                               // metadata
        );
        let field_array = FieldArray::new(field, result_array);

        result_cols.push(field_array);
    }

    // Create result table with same name as left table
    Ok(Table::new(lhs_table.name.clone(), Some(result_cols)))
}

/// Broadcasts addition over SuperTable chunks (batched tables)
///
/// Both SuperTables must have the same number of chunks and compatible shapes.
/// Addition is applied chunk-wise between corresponding table chunks.
#[cfg(feature = "chunked")]
pub fn broadcast_super_table_add(
    lhs: impl Into<SuperTableV>,
    rhs: impl Into<SuperTableV>,
    null_mask: Option<Arc<Bitmask>>,
) -> Result<SuperTable, KernelError> {
    let lhs_table: SuperTableV = lhs.into();
    let rhs_table: SuperTableV = rhs.into();

    // Check chunk count compatibility
    if lhs_table.slices.len() != rhs_table.slices.len() {
        return Err(KernelError::BroadcastingError(format!(
            "SuperTable chunk count mismatch: LHS {} chunks, RHS {} chunks",
            lhs_table.slices.len(),
            rhs_table.slices.len()
        )));
    }

    // Apply addition chunk-wise
    let mut result_tables = Vec::with_capacity(lhs_table.slices.len());

    for (i, (lhs_chunk, rhs_chunk)) in lhs_table
        .slices
        .iter()
        .zip(rhs_table.slices.iter())
        .enumerate()
    {
        let result_table =
            broadcast_table_add(lhs_chunk.clone(), rhs_chunk.clone(), null_mask.clone()).map_err(
                |e| KernelError::BroadcastingError(format!("Chunk {} addition failed: {}", i, e)),
            )?;

        result_tables.push(Arc::new(result_table));
    }

    // Create result SuperTable - use name from first slice if available
    let name = if !lhs_table.slices.is_empty() && !lhs_table.slices[0].name.is_empty() {
        lhs_table.slices[0].name.clone()
    } else {
        "SuperTable".to_string()
    };
    Ok(SuperTable::from_batches(result_tables, Some(name)))
}

/// Helper function for table-array broadcasting - apply table columns to array
pub fn broadcast_table_to_array(
    op: ArithmeticOperator,
    table: &Table,
    array: &Array,
) -> Result<Table, MinarrowError> {
    let new_cols: Result<Vec<_>, _> = table
        .cols
        .iter()
        .map(|field_array| {
            let col_array = &field_array.array;
            let result_array = match (
                Value::Array(Arc::new(col_array.clone())),
                Value::Array(Arc::new(array.clone())),
            ) {
                (a, b) => broadcast_value(op, a, b)?,
            };

            match result_array {
                Value::Array(result_array) => {
                    let result_array = Arc::unwrap_or_clone(result_array);
                    // Preserve original field metadata but update type if needed
                    let new_field = create_field_for_array(
                        &field_array.field.name,
                        &result_array,
                        Some(&array),
                        Some(field_array.field.metadata.clone()),
                    );
                    Ok(FieldArray::new(new_field, result_array))
                }
                _ => Err(MinarrowError::TypeError {
                    from: "table-array broadcasting",
                    to: "Array result",
                    message: Some("Expected Array result from broadcasting".to_string()),
                }),
            }
        })
        .collect();

    let table_out = Table::new(table.name.clone(), Some(new_cols?));
    #[cfg(feature = "table_metadata")]
    {
        let mut t = table_out;
        t.metadata = table.metadata.clone();
        return Ok(t);
    }
    #[cfg(not(feature = "table_metadata"))]
    Ok(table_out)
}

/// Helper function for table-scalar broadcasting - apply table columns to scalar
#[cfg(feature = "scalar_type")]
pub fn broadcast_table_to_scalar(
    op: ArithmeticOperator,
    table: &Table,
    scalar: &Scalar,
) -> Result<Table, MinarrowError> {
    let new_cols: Result<Vec<_>, _> = table
        .cols
        .iter()
        .map(|field_array| {
            let col_array = &field_array.array;
            let result_array = match (
                Value::Array(Arc::new(col_array.clone())),
                Value::Scalar(scalar.clone()),
            ) {
                (a, b) => broadcast_value(op, a, b)?,
            };

            match result_array {
                Value::Array(result_array) => {
                    let result_array = Arc::unwrap_or_clone(result_array);
                    // Preserve original field metadata but update type if needed
                    let new_field = create_field_for_array(
                        &field_array.field.name,
                        &result_array,
                        Some(&col_array),
                        Some(field_array.field.metadata.clone()),
                    );
                    Ok(FieldArray::new(new_field, result_array))
                }
                _ => Err(MinarrowError::TypeError {
                    from: "table-scalar broadcasting",
                    to: "Array result",
                    message: Some("Expected Array result from broadcasting".to_string()),
                }),
            }
        })
        .collect();

    let table_out = Table::new(table.name.clone(), Some(new_cols?));
    #[cfg(feature = "table_metadata")]
    {
        let mut t = table_out;
        t.metadata = table.metadata.clone();
        return Ok(t);
    }
    #[cfg(not(feature = "table_metadata"))]
    Ok(table_out)
}

/// Helper function for table-arrayview broadcasting - work directly with view without conversion
#[cfg(feature = "views")]
pub fn broadcast_table_to_arrayview(
    op: ArithmeticOperator,
    table: &Table,
    array_view: &ArrayV,
) -> Result<Table, MinarrowError> {
    // Work directly with the view's underlying array and window bounds
    let new_cols: Result<Vec<_>, _> = table
        .cols
        .iter()
        .map(|field_array| {
            let col_array = &field_array.array;
            // Create a view of the column array that matches the input view's window
            let col_view = ArrayV::new(col_array.clone(), array_view.offset, array_view.len());
            let result_array = match (
                Value::ArrayView(Arc::new(col_view)),
                Value::ArrayView(Arc::new(array_view.clone())),
            ) {
                (a, b) => broadcast_value(op, a, b)?,
            };

            match result_array {
                Value::Array(result_array) => {
                    let result_array = Arc::unwrap_or_clone(result_array);
                    let new_field = create_field_for_array(
                        &field_array.field.name,
                        &result_array,
                        Some(&array_view.array),
                        Some(field_array.field.metadata.clone()),
                    );
                    Ok(FieldArray::new(new_field, result_array))
                }
                _ => Err(MinarrowError::TypeError {
                    from: "table-arrayview broadcasting",
                    to: "Array result",
                    message: Some("Expected Array result from view broadcasting".to_string()),
                }),
            }
        })
        .collect();

    let table_out = Table::new(table.name.clone(), Some(new_cols?));
    #[cfg(feature = "table_metadata")]
    {
        let mut t = table_out;
        t.metadata = table.metadata.clone();
        return Ok(t);
    }
    #[cfg(not(feature = "table_metadata"))]
    Ok(table_out)
}

/// Helper function for Table-SuperArrayView broadcasting - promote Table to aligned SuperTableView
#[cfg(all(feature = "chunked", feature = "views"))]
pub fn broadcast_table_to_superarrayview(
    op: ArithmeticOperator,
    table: &Table,
    super_array_view: &SuperArrayV,
) -> Result<SuperTableV, MinarrowError> {
    // 1. Validate lengths match
    if table.n_rows != super_array_view.len {
        return Err(MinarrowError::ShapeError {
            message: format!(
                "Table rows ({}) does not match SuperArrayView length ({})",
                table.n_rows, super_array_view.len
            ),
        });
    }

    // 2. Promote Table to SuperTableView with aligned chunking
    let mut current_offset = 0;
    let mut table_slices = Vec::new();

    for array_slice in super_array_view.slices.iter() {
        let chunk_len = array_slice.len();
        let table_slice = TableV::from_table(table.clone(), current_offset, chunk_len);
        table_slices.push(table_slice);
        current_offset += chunk_len;
    }

    let aligned_super_table = SuperTableV {
        slices: table_slices,
        len: table.n_rows,
    };

    // 3. Broadcast per chunk using indexed loops
    let mut result_slices = Vec::new();
    for i in 0..aligned_super_table.slices.len() {
        let table_slice = &aligned_super_table.slices[i];
        let array_slice = &super_array_view.slices[i];
        let slice_result = broadcast_tableview_to_arrayview(op, table_slice, array_slice)?;
        result_slices.push(slice_result);
    }

    Ok(SuperTableV {
        slices: result_slices,
        len: super_array_view.len,
    })
}

/// Helper function for Table-SuperArray broadcasting - broadcast table against each chunk
#[cfg(feature = "chunked")]
pub fn broadcast_table_to_superarray(
    op: ArithmeticOperator,
    table: &Table,
    super_array: &SuperArray,
) -> Result<SuperArray, MinarrowError> {
    let new_chunks: Result<Vec<_>, _> = super_array
        .chunks()
        .iter()
        .map(|chunk| {
            let chunk_array = chunk;
            let result_table = broadcast_table_to_array(op, table, chunk_array)?;
            // Convert result table back to a FieldArray chunk with matching structure
            if result_table.cols.len() == 1 {
                Ok(result_table.cols[0].clone())
            } else {
                Err(MinarrowError::ShapeError {
                    message: "Table-SuperArray broadcasting should result in single column"
                        .to_string(),
                })
            }
        })
        .collect();

    Ok(SuperArray::from_chunks(new_chunks?))
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::traits::selection::ColumnSelection;
    use crate::{Array, FieldArray, IntegerArray, vec64};

    fn create_test_table(name: &str, data1: &[i32], data2: &[i32]) -> Table {
        let col1 = FieldArray::from_arr(
            "col1",
            Array::from_int32(IntegerArray::from_slice(&vec64![
                data1[0], data1[1], data1[2]
            ])),
        );
        let col2 = FieldArray::from_arr(
            "col2",
            Array::from_int32(IntegerArray::from_slice(&vec64![
                data2[0], data2[1], data2[2]
            ])),
        );

        Table::new(name.to_string(), Some(vec![col1, col2]))
    }

    #[test]
    fn test_table_plus_table() {
        let table1 = create_test_table("table1", &[1, 2, 3], &[10, 20, 30]);
        let table2 = create_test_table("table2", &[4, 5, 6], &[40, 50, 60]);

        let result = broadcast_table_add(table1, table2, None).unwrap();

        assert_eq!(result.n_cols(), 2);
        assert_eq!(result.n_rows(), 3);
        assert_eq!(result.name, "table1"); // Takes name from left table

        // Check first column: [1,2,3] + [4,5,6] = [5,7,9]
        if let Some(col1) = result.col_ix(0) {
            if let crate::Array::NumericArray(crate::NumericArray::Int32(arr)) = &col1.array {
                assert_eq!(arr.data.as_slice(), &[5, 7, 9]);
            } else {
                panic!("Expected Int32 array in first column");
            }
        } else {
            panic!("Could not get first column");
        }

        // Check second column: [10,20,30] + [40,50,60] = [50,70,90]
        if let Some(col2) = result.col_ix(1) {
            if let crate::Array::NumericArray(crate::NumericArray::Int32(arr)) = &col2.array {
                assert_eq!(arr.data.as_slice(), &[50, 70, 90]);
            } else {
                panic!("Expected Int32 array in second column");
            }
        } else {
            panic!("Could not get second column");
        }
    }

    #[test]
    #[should_panic(expected = "column count mismatch")]
    fn test_mismatched_column_count() {
        let col1 = FieldArray::from_arr(
            "col1",
            Array::from_int32(IntegerArray::from_slice(&vec64![1, 2, 3])),
        );
        let table1 = Table::new("table1".to_string(), Some(vec![col1])); // 1 column

        let table2 = create_test_table("table2", &[4, 5, 6], &[40, 50, 60]); // 2 columns

        let _ = broadcast_table_add(table1, table2, None).unwrap();
    }

    #[test]
    #[should_panic(expected = "row count mismatch")]
    fn test_mismatched_row_count() {
        let col1 = FieldArray::from_arr(
            "col1",
            Array::from_int32(IntegerArray::from_slice(&vec64![1, 2])),
        );
        let col2 = FieldArray::from_arr(
            "col2",
            Array::from_int32(IntegerArray::from_slice(&vec64![10, 20])),
        );
        let table1 = Table::new("table1".to_string(), Some(vec![col1, col2])); // 2 rows

        let table2 = create_test_table("table2", &[4, 5, 6], &[40, 50, 60]); // 3 rows

        let _ = broadcast_table_add(table1, table2, None).unwrap();
    }

    #[test]
    fn test_broadcast_table_with_operator_multiply() {
        let table1 = create_test_table("table1", &[2, 3, 4], &[5, 6, 7]);
        let table2 = create_test_table("table2", &[10, 10, 10], &[2, 2, 2]);

        let result =
            broadcast_table_with_operator(ArithmeticOperator::Multiply, table1, table2).unwrap();

        // col1: [2,3,4] * [10,10,10] = [20,30,40]
        if let crate::Array::NumericArray(crate::NumericArray::Int32(arr)) = &result.cols[0].array {
            assert_eq!(arr.data.as_slice(), &[20, 30, 40]);
        } else {
            panic!("Expected Int32 array");
        }

        // col2: [5,6,7] * [2,2,2] = [10,12,14]
        if let crate::Array::NumericArray(crate::NumericArray::Int32(arr)) = &result.cols[1].array {
            assert_eq!(arr.data.as_slice(), &[10, 12, 14]);
        } else {
            panic!("Expected Int32 array");
        }
    }

    #[test]
    fn test_broadcast_table_to_array() {
        let table = create_test_table("table1", &[10, 20, 30], &[100, 200, 300]);
        let arr = Array::from_int32(IntegerArray::from_slice(&vec64![1, 2, 3]));

        let result = broadcast_table_to_array(ArithmeticOperator::Add, &table, &arr).unwrap();

        // col1: [10,20,30] + [1,2,3] = [11,22,33]
        if let crate::Array::NumericArray(crate::NumericArray::Int32(arr)) = &result.cols[0].array {
            assert_eq!(arr.data.as_slice(), &[11, 22, 33]);
        } else {
            panic!("Expected Int32 array");
        }

        // col2: [100,200,300] + [1,2,3] = [101,202,303]
        if let crate::Array::NumericArray(crate::NumericArray::Int32(arr)) = &result.cols[1].array {
            assert_eq!(arr.data.as_slice(), &[101, 202, 303]);
        } else {
            panic!("Expected Int32 array");
        }
    }

    #[cfg(feature = "scalar_type")]
    #[test]
    fn test_broadcast_table_to_scalar() {
        let table = create_test_table("table1", &[10, 20, 30], &[100, 200, 300]);
        let scalar = Scalar::Int32(5);

        let result =
            broadcast_table_to_scalar(ArithmeticOperator::Multiply, &table, &scalar).unwrap();

        // col1: [10,20,30] * 5 = [50,100,150]
        if let crate::Array::NumericArray(crate::NumericArray::Int32(arr)) = &result.cols[0].array {
            assert_eq!(arr.data.as_slice(), &[50, 100, 150]);
        } else {
            panic!("Expected Int32 array");
        }

        // col2: [100,200,300] * 5 = [500,1000,1500]
        if let crate::Array::NumericArray(crate::NumericArray::Int32(arr)) = &result.cols[1].array {
            assert_eq!(arr.data.as_slice(), &[500, 1000, 1500]);
        } else {
            panic!("Expected Int32 array");
        }
    }

    #[cfg(feature = "views")]
    #[test]
    fn test_broadcast_table_to_arrayview() {
        let table = create_test_table("table1", &[10, 20, 30], &[100, 200, 300]);
        let arr = Array::from_int32(IntegerArray::from_slice(&vec64![2, 3, 4]));
        let array_view = ArrayV::from(arr);

        let result =
            broadcast_table_to_arrayview(ArithmeticOperator::Subtract, &table, &array_view)
                .unwrap();

        // col1: [10,20,30] - [2,3,4] = [8,17,26]
        if let crate::Array::NumericArray(crate::NumericArray::Int32(arr)) = &result.cols[0].array {
            assert_eq!(arr.data.as_slice(), &[8, 17, 26]);
        } else {
            panic!("Expected Int32 array");
        }

        // col2: [100,200,300] - [2,3,4] = [98,197,296]
        if let crate::Array::NumericArray(crate::NumericArray::Int32(arr)) = &result.cols[1].array {
            assert_eq!(arr.data.as_slice(), &[98, 197, 296]);
        } else {
            panic!("Expected Int32 array");
        }
    }

    #[cfg(feature = "chunked")]
    #[test]
    fn test_broadcast_super_table_add() {
        let table1 = create_test_table("table1", &[1, 2, 3], &[10, 20, 30]);
        let table2 = create_test_table("table2", &[4, 5, 6], &[40, 50, 60]);
        let table3 = create_test_table("table3", &[7, 8, 9], &[70, 80, 90]);
        let table4 = create_test_table("table4", &[1, 1, 1], &[2, 2, 2]);

        let super_table1 = SuperTableV {
            slices: vec![
                TableV::from_table(table1, 0, 3),
                TableV::from_table(table2, 0, 3),
            ],
            len: 6,
        };

        let super_table2 = SuperTableV {
            slices: vec![
                TableV::from_table(table3, 0, 3),
                TableV::from_table(table4, 0, 3),
            ],
            len: 6,
        };

        let result = broadcast_super_table_add(super_table1, super_table2, None).unwrap();

        assert_eq!(result.batches.len(), 2);

        // First batch: table1 + table3
        // col1: [1,2,3] + [7,8,9] = [8,10,12]
        if let crate::Array::NumericArray(crate::NumericArray::Int32(arr)) =
            &result.batches[0].cols[0].array
        {
            assert_eq!(arr.data.as_slice(), &[8, 10, 12]);
        } else {
            panic!("Expected Int32 array");
        }

        // col2: [10,20,30] + [70,80,90] = [80,100,120]
        if let crate::Array::NumericArray(crate::NumericArray::Int32(arr)) =
            &result.batches[0].cols[1].array
        {
            assert_eq!(arr.data.as_slice(), &[80, 100, 120]);
        } else {
            panic!("Expected Int32 array");
        }

        // Second batch: table2 + table4
        // col1: [4,5,6] + [1,1,1] = [5,6,7]
        if let crate::Array::NumericArray(crate::NumericArray::Int32(arr)) =
            &result.batches[1].cols[0].array
        {
            assert_eq!(arr.data.as_slice(), &[5, 6, 7]);
        } else {
            panic!("Expected Int32 array");
        }

        // col2: [40,50,60] + [2,2,2] = [42,52,62]
        if let crate::Array::NumericArray(crate::NumericArray::Int32(arr)) =
            &result.batches[1].cols[1].array
        {
            assert_eq!(arr.data.as_slice(), &[42, 52, 62]);
        } else {
            panic!("Expected Int32 array");
        }
    }

    #[cfg(feature = "chunked")]
    #[test]
    fn test_broadcast_table_to_superarray() {
        use crate::ffi::arrow_dtype::ArrowType;

        // Table with 3 rows to match each chunk size
        let table = Table::build(
            vec![FieldArray::new(
                Field::new("col1".to_string(), ArrowType::Int32, false, None),
                Array::from_int32(IntegerArray::from_slice(&vec64![2, 3, 4])),
            )],
            3,
            "test".to_string(),
        );

        let field = Field::new("data".to_string(), ArrowType::Int32, false, None);
        let chunks = vec![
            FieldArray::new(
                field.clone(),
                Array::from_int32(IntegerArray::from_slice(&vec64![10, 20, 30])),
            ),
            FieldArray::new(
                field.clone(),
                Array::from_int32(IntegerArray::from_slice(&vec64![40, 50, 60])),
            ),
        ];
        let super_array = SuperArray::from_chunks(chunks);

        let result =
            broadcast_table_to_superarray(ArithmeticOperator::Add, &table, &super_array).unwrap();

        assert_eq!(result.chunks().len(), 2);

        // Both chunks: [2,3,4] + [10,20,30] = [12,23,34] and [2,3,4] + [40,50,60] = [42,53,64]
        if let crate::Array::NumericArray(crate::NumericArray::Int32(arr)) = &result.chunks()[0] {
            assert_eq!(arr.data.as_slice(), &[12, 23, 34]);
        } else {
            panic!("Expected Int32 array");
        }

        if let crate::Array::NumericArray(crate::NumericArray::Int32(arr)) = &result.chunks()[1] {
            assert_eq!(arr.data.as_slice(), &[42, 53, 64]);
        } else {
            panic!("Expected Int32 array");
        }
    }

    #[cfg(all(feature = "chunked", feature = "views"))]
    #[test]
    fn test_broadcast_table_to_superarrayview() {
        use crate::ffi::arrow_dtype::ArrowType;

        let table = Table::build(
            vec![FieldArray::new(
                Field::new("col1".to_string(), ArrowType::Int32, false, None),
                Array::from_int32(IntegerArray::from_slice(&vec64![1, 2, 3, 4, 5, 6])),
            )],
            6,
            "test".to_string(),
        );

        let arr = Array::from_int32(IntegerArray::from_slice(&vec64![10, 20, 30, 40, 50, 60]));
        let field = Field::new("data".to_string(), ArrowType::Int32, false, None);

        let slices = vec![
            ArrayV::from(arr.clone()).slice(0, 3),
            ArrayV::from(arr.clone()).slice(3, 3),
        ];
        let super_array_view = SuperArrayV {
            slices,
            field: Arc::new(field),
            len: 6,
        };

        let result = broadcast_table_to_superarrayview(
            ArithmeticOperator::Multiply,
            &table,
            &super_array_view,
        )
        .unwrap();

        assert_eq!(result.slices.len(), 2);
        assert_eq!(result.len, 6);

        // First slice: [1,2,3] * [10,20,30] = [10,40,90]
        let slice1 = result.slices[0].to_table();
        if let crate::Array::NumericArray(crate::NumericArray::Int32(arr)) = &slice1.cols[0].array {
            assert_eq!(arr.data.as_slice(), &[10, 40, 90]);
        } else {
            panic!("Expected Int32 array");
        }

        // Second slice: [4,5,6] * [40,50,60] = [160,250,360]
        let slice2 = result.slices[1].to_table();
        if let crate::Array::NumericArray(crate::NumericArray::Int32(arr)) = &slice2.cols[0].array {
            assert_eq!(arr.data.as_slice(), &[160, 250, 360]);
        } else {
            panic!("Expected Int32 array");
        }
    }
}