streamweave 0.10.1

Composable, async, stream-first computation in pure Rust
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
//! Common utility functions for array operations.

use crate::nodes::comparison::common::{compare_equal, compare_less_than};
use crate::nodes::filter_node;
use crate::nodes::map_node;
use std::any::Any;
use std::sync::Arc;

/// Gets the length of an array.
///
/// This function attempts to downcast the array to its expected type
/// and returns its length. It supports:
/// - Getting the length of Vec<Arc<dyn Any + Send + Sync>>
///
/// Returns the result as `Arc<dyn Any + Send + Sync>` (usize) or an error string.
pub fn array_length(v: &Arc<dyn Any + Send + Sync>) -> Result<Arc<dyn Any + Send + Sync>, String> {
  // Try to downcast array
  let arc_vec = v
    .clone()
    .downcast::<Vec<Arc<dyn Any + Send + Sync>>>()
    .map_err(|_| {
      format!(
        "Unsupported type for array length input: {} (input must be Vec<Arc<dyn Any + Send + Sync>>)",
        std::any::type_name_of_val(&**v)
      )
    })?;

  // Return the length as usize
  let length = arc_vec.len();
  Ok(Arc::new(length) as Arc<dyn Any + Send + Sync>)
}

/// Gets an element from an array by index.
///
/// This function attempts to downcast the array and index to their expected types
/// and returns the element at the specified index. It supports:
/// - Accessing elements by numeric index (usize, i32, i64, u32, u64)
/// - Handling out-of-bounds indices with errors
///
/// Returns the result as `Arc<dyn Any + Send + Sync>` or an error string.
pub fn array_index(
  v: &Arc<dyn Any + Send + Sync>,
  index: &Arc<dyn Any + Send + Sync>,
) -> Result<Arc<dyn Any + Send + Sync>, String> {
  // Try to downcast array
  let arc_vec = v
    .clone()
    .downcast::<Vec<Arc<dyn Any + Send + Sync>>>()
    .map_err(|_| {
      format!(
        "Unsupported type for array index input: {} (input must be Vec<Arc<dyn Any + Send + Sync>>)",
        std::any::type_name_of_val(&**v)
      )
    })?;

  // Helper to convert Arc<dyn Any> to usize, handling various numeric types
  let get_usize = |val: &Arc<dyn Any + Send + Sync>, name: &str| -> Result<usize, String> {
    if let Ok(arc_usize) = val.clone().downcast::<usize>() {
      Ok(*arc_usize)
    } else if let Ok(arc_i32) = val.clone().downcast::<i32>() {
      if *arc_i32 < 0 {
        return Err(format!("{} cannot be negative", name));
      }
      (*arc_i32)
        .try_into()
        .map_err(|_| format!("{} too large", name))
    } else if let Ok(arc_i64) = val.clone().downcast::<i64>() {
      if *arc_i64 < 0 {
        return Err(format!("{} cannot be negative", name));
      }
      (*arc_i64)
        .try_into()
        .map_err(|_| format!("{} too large", name))
    } else if let Ok(arc_u32) = val.clone().downcast::<u32>() {
      (*arc_u32)
        .try_into()
        .map_err(|_| format!("{} too large", name))
    } else if let Ok(arc_u64) = val.clone().downcast::<u64>() {
      (*arc_u64)
        .try_into()
        .map_err(|_| format!("{} too large", name))
    } else {
      Err(format!(
        "Unsupported type for {}: {} (must be numeric)",
        name,
        std::any::type_name_of_val(&**val)
      ))
    }
  };

  let idx = get_usize(index, "index")?;

  // Check bounds
  if idx >= arc_vec.len() {
    return Err(format!(
      "Index {} out of bounds for array of length {}",
      idx,
      arc_vec.len()
    ));
  }

  // Return the element at the index (clone the Arc)
  Ok(arc_vec[idx].clone())
}

/// Extracts a slice from an array using start and end indices.
///
/// This function attempts to downcast the array and indices to their expected types
/// and returns a new array containing the sliced elements. It supports:
/// - Extracting slices with various numeric index types (usize, i32, i64, u32, u64)
/// - Handling out-of-bounds indices (clamps to array bounds)
/// - Invalid index range detection (start > end)
///
/// Returns the result as `Arc<dyn Any + Send + Sync>` (Vec<Arc<dyn Any + Send + Sync>>) or an error string.
pub fn array_slice(
  v: &Arc<dyn Any + Send + Sync>,
  start: &Arc<dyn Any + Send + Sync>,
  end: &Arc<dyn Any + Send + Sync>,
) -> Result<Arc<dyn Any + Send + Sync>, String> {
  // Try to downcast array
  let arc_vec = v
    .clone()
    .downcast::<Vec<Arc<dyn Any + Send + Sync>>>()
    .map_err(|_| {
      format!(
        "Unsupported type for array slice input: {} (input must be Vec<Arc<dyn Any + Send + Sync>>)",
        std::any::type_name_of_val(&**v)
      )
    })?;

  // Helper to convert Arc<dyn Any> to usize, handling various numeric types
  let get_usize = |val: &Arc<dyn Any + Send + Sync>, name: &str| -> Result<usize, String> {
    if let Ok(arc_usize) = val.clone().downcast::<usize>() {
      Ok(*arc_usize)
    } else if let Ok(arc_i32) = val.clone().downcast::<i32>() {
      if *arc_i32 < 0 {
        return Err(format!("{} cannot be negative", name));
      }
      (*arc_i32)
        .try_into()
        .map_err(|_| format!("{} too large", name))
    } else if let Ok(arc_i64) = val.clone().downcast::<i64>() {
      if *arc_i64 < 0 {
        return Err(format!("{} cannot be negative", name));
      }
      (*arc_i64)
        .try_into()
        .map_err(|_| format!("{} too large", name))
    } else if let Ok(arc_u32) = val.clone().downcast::<u32>() {
      (*arc_u32)
        .try_into()
        .map_err(|_| format!("{} too large", name))
    } else if let Ok(arc_u64) = val.clone().downcast::<u64>() {
      (*arc_u64)
        .try_into()
        .map_err(|_| format!("{} too large", name))
    } else {
      Err(format!(
        "Unsupported type for {}: {} (must be numeric)",
        name,
        std::any::type_name_of_val(&**val)
      ))
    }
  };

  let start_idx = get_usize(start, "start")?;
  let end_idx = get_usize(end, "end")?;

  // Validate range
  if start_idx > end_idx {
    return Err(format!(
      "Invalid slice range: start index {} is greater than end index {}",
      start_idx, end_idx
    ));
  }

  let array_len = arc_vec.len();

  // Clamp indices to array bounds
  let clamped_start = start_idx.min(array_len);
  let clamped_end = end_idx.min(array_len);

  // Extract slice (clone the Arc references)
  let result: Vec<Arc<dyn Any + Send + Sync>> = arc_vec[clamped_start..clamped_end].to_vec();

  Ok(Arc::new(result) as Arc<dyn Any + Send + Sync>)
}

/// Checks if an array contains a value.
///
/// This function attempts to downcast the array to its expected type
/// and checks if it contains the specified value. It supports:
/// - Checking for any value type (uses compare_equal for type-aware comparison)
/// - Type promotion (e.g., i32 matches i64)
/// - Returns false for incompatible types (via compare_equal)
///
/// Returns the result as `Arc<dyn Any + Send + Sync>` (boolean) or an error string.
pub fn array_contains(
  v: &Arc<dyn Any + Send + Sync>,
  value: &Arc<dyn Any + Send + Sync>,
) -> Result<Arc<dyn Any + Send + Sync>, String> {
  // Try to downcast array
  let arc_vec = v
    .clone()
    .downcast::<Vec<Arc<dyn Any + Send + Sync>>>()
    .map_err(|_| {
      format!(
        "Unsupported type for array contains input: {} (input must be Vec<Arc<dyn Any + Send + Sync>>)",
        std::any::type_name_of_val(&**v)
      )
    })?;

  // Iterate through array elements and check for equality
  for element in arc_vec.iter() {
    match compare_equal(element, value) {
      Ok(result_arc) => {
        // Downcast the boolean result
        if let Ok(arc_bool) = result_arc.downcast::<bool>()
          && *arc_bool
        {
          return Ok(Arc::new(true) as Arc<dyn Any + Send + Sync>);
        }
      }
      Err(_) => {
        // If comparison fails, continue to next element
        // (compare_equal returns false for incompatible types, so this shouldn't happen)
        continue;
      }
    }
  }

  // No match found
  Ok(Arc::new(false) as Arc<dyn Any + Send + Sync>)
}

/// Finds the index of a value in an array.
///
/// This function attempts to downcast the array to its expected type
/// and finds the index of the specified value. It supports:
/// - Finding index for any value type (uses compare_equal for type-aware comparison)
/// - Type promotion (e.g., i32 matches i64)
/// - Returns -1 if value is not found (similar to JavaScript's indexOf)
///
/// Returns the result as `Arc<dyn Any + Send + Sync>` (i32 index, or -1 if not found) or an error string.
pub fn array_index_of(
  v: &Arc<dyn Any + Send + Sync>,
  value: &Arc<dyn Any + Send + Sync>,
) -> Result<Arc<dyn Any + Send + Sync>, String> {
  // Try to downcast array
  let arc_vec = v
    .clone()
    .downcast::<Vec<Arc<dyn Any + Send + Sync>>>()
    .map_err(|_| {
      format!(
        "Unsupported type for array index_of input: {} (input must be Vec<Arc<dyn Any + Send + Sync>>)",
        std::any::type_name_of_val(&**v)
      )
    })?;

  // Iterate through array elements and find the index
  for (index, element) in arc_vec.iter().enumerate() {
    match compare_equal(element, value) {
      Ok(result_arc) => {
        // Downcast the boolean result
        if let Ok(arc_bool) = result_arc.downcast::<bool>()
          && *arc_bool
        {
          // Found the value, return the index as i32
          return Ok(Arc::new(index as i32) as Arc<dyn Any + Send + Sync>);
        }
      }
      Err(_) => {
        // If comparison fails, continue to next element
        continue;
      }
    }
  }

  // No match found, return -1
  Ok(Arc::new(-1i32) as Arc<dyn Any + Send + Sync>)
}

/// Concatenates two arrays.
///
/// This function attempts to downcast both arrays to their expected types
/// and concatenates them. It supports:
/// - Concatenating two Vec<Arc<dyn Any + Send + Sync>> arrays
/// - Preserving element order (first array, then second array)
///
/// Returns the result as `Arc<dyn Any + Send + Sync>` (Vec<Arc<dyn Any + Send + Sync>>) or an error string.
pub fn array_concat(
  v1: &Arc<dyn Any + Send + Sync>,
  v2: &Arc<dyn Any + Send + Sync>,
) -> Result<Arc<dyn Any + Send + Sync>, String> {
  // Try to downcast first array
  let arc_vec1 = v1
    .clone()
    .downcast::<Vec<Arc<dyn Any + Send + Sync>>>()
    .map_err(|_| {
      format!(
        "Unsupported type for array concat first input: {} (input must be Vec<Arc<dyn Any + Send + Sync>>)",
        std::any::type_name_of_val(&**v1)
      )
    })?;

  // Try to downcast second array
  let arc_vec2 = v2
    .clone()
    .downcast::<Vec<Arc<dyn Any + Send + Sync>>>()
    .map_err(|_| {
      format!(
        "Unsupported type for array concat second input: {} (input must be Vec<Arc<dyn Any + Send + Sync>>)",
        std::any::type_name_of_val(&**v2)
      )
    })?;

  // Concatenate arrays (clone the Arc references)
  let mut result: Vec<Arc<dyn Any + Send + Sync>> =
    Vec::with_capacity(arc_vec1.len() + arc_vec2.len());
  result.extend(arc_vec1.iter().cloned());
  result.extend(arc_vec2.iter().cloned());

  Ok(Arc::new(result) as Arc<dyn Any + Send + Sync>)
}

/// Reverses the order of elements in an array.
///
/// This function attempts to downcast the array to its expected type
/// and reverses the element order. It supports:
/// - Reversing Vec<Arc<dyn Any + Send + Sync>> arrays
/// - Preserving element references (clones Arc references)
///
/// Returns the result as `Arc<dyn Any + Send + Sync>` (Vec<Arc<dyn Any + Send + Sync>>) or an error string.
pub fn array_reverse(v: &Arc<dyn Any + Send + Sync>) -> Result<Arc<dyn Any + Send + Sync>, String> {
  // Try to downcast array
  let arc_vec = v
    .clone()
    .downcast::<Vec<Arc<dyn Any + Send + Sync>>>()
    .map_err(|_| {
      format!(
        "Unsupported type for array reverse input: {} (input must be Vec<Arc<dyn Any + Send + Sync>>)",
        std::any::type_name_of_val(&**v)
      )
    })?;

  // Reverse the array (clone the Arc references)
  let mut result: Vec<Arc<dyn Any + Send + Sync>> = arc_vec.iter().cloned().collect();
  result.reverse();

  Ok(Arc::new(result) as Arc<dyn Any + Send + Sync>)
}

/// Sorts an array of elements.
///
/// This function attempts to downcast the array to its expected type
/// and sorts the elements. It supports:
/// - Sorting Vec<Arc<dyn Any + Send + Sync>> arrays
/// - Ascending or descending order
/// - Type-aware comparison (uses compare_less_than for type promotion)
/// - Incompatible types are placed at the end (via compare_less_than returning false)
///
/// Returns the result as `Arc<dyn Any + Send + Sync>` (Vec<Arc<dyn Any + Send + Sync>>) or an error string.
pub fn array_sort(
  v: &Arc<dyn Any + Send + Sync>,
  ascending: bool,
) -> Result<Arc<dyn Any + Send + Sync>, String> {
  // Try to downcast array
  let arc_vec = v
    .clone()
    .downcast::<Vec<Arc<dyn Any + Send + Sync>>>()
    .map_err(|_| {
      format!(
        "Unsupported type for array sort input: {} (input must be Vec<Arc<dyn Any + Send + Sync>>)",
        std::any::type_name_of_val(&**v)
      )
    })?;

  // Create a mutable copy for sorting
  let mut result: Vec<Arc<dyn Any + Send + Sync>> = arc_vec.iter().cloned().collect();

  // Sort using compare_less_than (for numeric types) and direct comparison (for strings)
  result.sort_by(|a, b| {
    // Try string comparison first
    if let (Ok(arc_str1), Ok(arc_str2)) = (
      a.clone().downcast::<String>(),
      b.clone().downcast::<String>(),
    ) {
      return arc_str1.as_str().cmp(arc_str2.as_str());
    }

    // Try numeric comparison using compare_less_than
    match compare_less_than(a, b) {
      Ok(result_arc) => {
        if let Ok(arc_bool) = result_arc.downcast::<bool>()
          && *arc_bool
        {
          return std::cmp::Ordering::Less;
        }
        // Check if equal (if not less, check if greater)
        if let Ok(result_arc2) = compare_less_than(b, a)
          && let Ok(arc_bool2) = result_arc2.downcast::<bool>()
          && *arc_bool2
        {
          return std::cmp::Ordering::Greater;
        }
        std::cmp::Ordering::Equal
      }
      Err(_) => {
        // If comparison fails, consider them equal (incompatible types)
        std::cmp::Ordering::Equal
      }
    }
  });

  // Reverse if descending
  if !ascending {
    result.reverse();
  }

  Ok(Arc::new(result) as Arc<dyn Any + Send + Sync>)
}

/// Filters an array based on a predicate function.
///
/// This function attempts to downcast the array to its expected type
/// and filters elements based on the predicate. It supports:
/// - Filtering Vec<Arc<dyn Any + Send + Sync>> arrays
/// - Predicate function that evaluates each element to a boolean
/// - Preserving element references (clones Arc references for kept elements)
///
/// Returns the result as `Arc<dyn Any + Send + Sync>` (Vec<Arc<dyn Any + Send + Sync>>) or an error string.
pub async fn array_filter(
  v: &Arc<dyn Any + Send + Sync>,
  predicate: &filter_node::FilterConfig,
) -> Result<Arc<dyn Any + Send + Sync>, String> {
  // Try to downcast array
  let arc_vec = v
    .clone()
    .downcast::<Vec<Arc<dyn Any + Send + Sync>>>()
    .map_err(|_| {
      format!(
        "Unsupported type for array filter input: {} (input must be Vec<Arc<dyn Any + Send + Sync>>)",
        std::any::type_name_of_val(&**v)
      )
    })?;

  // Filter elements using the predicate
  let mut result: Vec<Arc<dyn Any + Send + Sync>> = Vec::new();
  for element in arc_vec.iter() {
    match predicate.apply(element.clone()).await {
      Ok(true) => {
        // Element passes filter - keep it (zero-copy: clone Arc reference)
        result.push(element.clone());
      }
      Ok(false) => {
        // Element filtered out - skip it
      }
      Err(e) => {
        return Err(format!("Predicate error: {}", e));
      }
    }
  }

  Ok(Arc::new(result) as Arc<dyn Any + Send + Sync>)
}

/// Maps an array by applying a transformation function to each element.
///
/// This function attempts to downcast the array to its expected type
/// and applies the transformation to each element. It supports:
/// - Mapping Vec<Arc<dyn Any + Send + Sync>> arrays
/// - Transformation function that transforms each element
/// - Preserving element order
///
/// Returns the result as `Arc<dyn Any + Send + Sync>` (Vec<Arc<dyn Any + Send + Sync>>) or an error string.
pub async fn array_map(
  v: &Arc<dyn Any + Send + Sync>,
  map_fn: &map_node::MapConfig,
) -> Result<Arc<dyn Any + Send + Sync>, String> {
  // Try to downcast array
  let arc_vec = v
    .clone()
    .downcast::<Vec<Arc<dyn Any + Send + Sync>>>()
    .map_err(|_| {
      format!(
        "Unsupported type for array map input: {} (input must be Vec<Arc<dyn Any + Send + Sync>>)",
        std::any::type_name_of_val(&**v)
      )
    })?;

  // Map elements using the transformation function
  let mut result: Vec<Arc<dyn Any + Send + Sync>> = Vec::with_capacity(arc_vec.len());
  for element in arc_vec.iter() {
    match map_fn.apply(element.clone()).await {
      Ok(transformed) => {
        // Element transformed successfully - add to result
        result.push(transformed);
      }
      Err(e) => {
        return Err(format!("Map function error: {}", e));
      }
    }
  }

  Ok(Arc::new(result) as Arc<dyn Any + Send + Sync>)
}

/// Joins an array of elements into a string using a delimiter.
///
/// This function attempts to downcast the array to its expected type
/// and joins elements with a delimiter. It supports:
/// - Joining Vec<Arc<dyn Any + Send + Sync>> arrays
/// - Converting each element to string representation
/// - Custom delimiter string
///
/// Returns the result as `Arc<dyn Any + Send + Sync>` (String) or an error string.
pub fn array_join(
  v: &Arc<dyn Any + Send + Sync>,
  delimiter: &Arc<dyn Any + Send + Sync>,
) -> Result<Arc<dyn Any + Send + Sync>, String> {
  // Try to downcast array
  let arc_vec = v
    .clone()
    .downcast::<Vec<Arc<dyn Any + Send + Sync>>>()
    .map_err(|_| {
      format!(
        "Unsupported type for array join input: {} (input must be Vec<Arc<dyn Any + Send + Sync>>)",
        std::any::type_name_of_val(&**v)
      )
    })?;

  // Try to downcast delimiter
  let arc_delimiter = delimiter.clone().downcast::<String>().map_err(|_| {
    format!(
      "Unsupported type for delimiter: {} (delimiter must be String)",
      std::any::type_name_of_val(&**delimiter)
    )
  })?;

  // Convert each element to String and join
  let mut parts = Vec::new();
  for (idx, item) in arc_vec.iter().enumerate() {
    // Try to convert element to string
    let str_repr = if let Ok(arc_str) = item.clone().downcast::<String>() {
      (*arc_str).clone()
    } else if let Ok(arc_i32) = item.clone().downcast::<i32>() {
      arc_i32.to_string()
    } else if let Ok(arc_i64) = item.clone().downcast::<i64>() {
      arc_i64.to_string()
    } else if let Ok(arc_u32) = item.clone().downcast::<u32>() {
      arc_u32.to_string()
    } else if let Ok(arc_u64) = item.clone().downcast::<u64>() {
      arc_u64.to_string()
    } else if let Ok(arc_f32) = item.clone().downcast::<f32>() {
      arc_f32.to_string()
    } else if let Ok(arc_f64) = item.clone().downcast::<f64>() {
      arc_f64.to_string()
    } else if let Ok(arc_bool) = item.clone().downcast::<bool>() {
      arc_bool.to_string()
    } else {
      return Err(format!(
        "Unsupported type for array element at index {}: {} (element must be convertible to string)",
        idx,
        std::any::type_name_of_val(&**item)
      ));
    };
    parts.push(str_repr);
  }

  // Join with delimiter
  let result = parts.join(&*arc_delimiter);
  Ok(Arc::new(result) as Arc<dyn Any + Send + Sync>)
}

/// Splits an array into chunks of a specified size.
///
/// This function attempts to downcast the array to its expected type
/// and splits it into chunks. It supports:
/// - Splitting Vec<Arc<dyn Any + Send + Sync>> arrays
/// - Configurable chunk size
/// - Preserving element references (clones Arc references)
///
/// Returns the result as `Arc<dyn Any + Send + Sync>` (Vec<Vec<Arc<dyn Any + Send + Sync>>>) or an error string.
pub fn array_split(
  v: &Arc<dyn Any + Send + Sync>,
  chunk_size: &Arc<dyn Any + Send + Sync>,
) -> Result<Arc<dyn Any + Send + Sync>, String> {
  // Try to downcast array
  let arc_vec = v
    .clone()
    .downcast::<Vec<Arc<dyn Any + Send + Sync>>>()
    .map_err(|_| {
      format!(
        "Unsupported type for array split input: {} (input must be Vec<Arc<dyn Any + Send + Sync>>)",
        std::any::type_name_of_val(&**v)
      )
    })?;

  // Helper to convert chunk_size to usize
  let get_usize = |val: &Arc<dyn Any + Send + Sync>, name: &str| -> Result<usize, String> {
    if let Ok(arc_i32) = val.clone().downcast::<i32>() {
      let i = *arc_i32;
      if i < 0 {
        return Err(format!("{} must be non-negative, got {}", name, i));
      }
      Ok(i as usize)
    } else if let Ok(arc_i64) = val.clone().downcast::<i64>() {
      let i = *arc_i64;
      if i < 0 {
        return Err(format!("{} must be non-negative, got {}", name, i));
      }
      if i > usize::MAX as i64 {
        return Err(format!("{} overflow: {} exceeds usize::MAX", name, i));
      }
      Ok(i as usize)
    } else if let Ok(arc_u32) = val.clone().downcast::<u32>() {
      Ok(*arc_u32 as usize)
    } else if let Ok(arc_u64) = val.clone().downcast::<u64>() {
      let u = *arc_u64;
      if u > usize::MAX as u64 {
        return Err(format!("{} overflow: {} exceeds usize::MAX", name, u));
      }
      Ok(u as usize)
    } else {
      Err(format!(
        "Unsupported type for {}: {} (must be i32, i64, u32, or u64)",
        name,
        std::any::type_name_of_val(&**val)
      ))
    }
  };

  let size = get_usize(chunk_size, "chunk_size")?;
  if size == 0 {
    return Err("chunk_size must be greater than 0".to_string());
  }

  // Split into chunks
  let mut result: Vec<Arc<dyn Any + Send + Sync>> = Vec::new();
  for chunk in arc_vec.chunks(size) {
    let chunk_vec: Vec<Arc<dyn Any + Send + Sync>> = chunk.to_vec();
    result.push(Arc::new(chunk_vec) as Arc<dyn Any + Send + Sync>);
  }

  Ok(Arc::new(result) as Arc<dyn Any + Send + Sync>)
}

/// Flattens a nested array one level.
///
/// This function attempts to downcast the array to its expected type
/// and flattens nested arrays one level. It supports:
/// - Flattening Vec<Arc<dyn Any + Send + Sync>> arrays where elements are also arrays
/// - Flattening one level only (not recursive)
/// - Preserving element references (clones Arc references)
///
/// Returns the result as `Arc<dyn Any + Send + Sync>` (Vec<Arc<dyn Any + Send + Sync>>) or an error string.
pub fn array_flatten(v: &Arc<dyn Any + Send + Sync>) -> Result<Arc<dyn Any + Send + Sync>, String> {
  // Try to downcast array
  let arc_vec = v
    .clone()
    .downcast::<Vec<Arc<dyn Any + Send + Sync>>>()
    .map_err(|_| {
      format!(
        "Unsupported type for array flatten input: {} (input must be Vec<Arc<dyn Any + Send + Sync>>)",
        std::any::type_name_of_val(&**v)
      )
    })?;

  // Flatten one level: iterate through elements and if an element is an array, add its elements
  let mut result: Vec<Arc<dyn Any + Send + Sync>> = Vec::new();
  for item in arc_vec.iter() {
    // Try to downcast element to array
    if let Ok(nested_array) = item.clone().downcast::<Vec<Arc<dyn Any + Send + Sync>>>() {
      // Element is an array - flatten it by adding its elements
      result.extend(nested_array.iter().cloned());
    } else {
      // Element is not an array - add it as-is
      result.push(item.clone());
    }
  }

  Ok(Arc::new(result) as Arc<dyn Any + Send + Sync>)
}

/// Removes duplicate elements from an array, keeping the first occurrence of each unique element.
///
/// This function attempts to downcast the array to its expected type
/// and removes duplicates. It supports:
/// - Removing duplicates from Vec<Arc<dyn Any + Send + Sync>> arrays
/// - Type-aware comparison using compare_equal (supports type promotion)
/// - Preserving element order (keeps first occurrence)
/// - Preserving element references (clones Arc references)
///
/// Returns the result as `Arc<dyn Any + Send + Sync>` (Vec<Arc<dyn Any + Send + Sync>>) or an error string.
pub fn array_unique(v: &Arc<dyn Any + Send + Sync>) -> Result<Arc<dyn Any + Send + Sync>, String> {
  // Try to downcast array
  let arc_vec = v
    .clone()
    .downcast::<Vec<Arc<dyn Any + Send + Sync>>>()
    .map_err(|_| {
      format!(
        "Unsupported type for array unique input: {} (input must be Vec<Arc<dyn Any + Send + Sync>>)",
        std::any::type_name_of_val(&**v)
      )
    })?;

  // Build result by keeping only first occurrence of each unique element
  let mut result: Vec<Arc<dyn Any + Send + Sync>> = Vec::new();

  for element in arc_vec.iter() {
    // Check if this element is already in the result
    let mut is_duplicate = false;
    for existing in result.iter() {
      match compare_equal(element, existing) {
        Ok(result_arc) => {
          if let Ok(arc_bool) = result_arc.downcast::<bool>()
            && *arc_bool
          {
            is_duplicate = true;
            break;
          }
        }
        Err(_) => {
          // If comparison fails, consider them different (incompatible types)
          continue;
        }
      }
    }

    // Only add if not a duplicate
    if !is_duplicate {
      result.push(element.clone());
    }
  }

  Ok(Arc::new(result) as Arc<dyn Any + Send + Sync>)
}