arrow-udf-runtime 0.9.0

Runtime for Arrow UDFs.
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
//
// Copyright 2024 RisingWave Labs
//
// 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.

//! Convert arrow array from/to js objects.

use anyhow::{Context, Result};
use arrow_array::{ArrowNativeTypeOp, array::*, builder::*};
use arrow_buffer::{OffsetBuffer, i256};
use arrow_schema::{DataType, Field, Fields};
use rquickjs::{
    Ctx, Error, FromJs, Function, IntoJs, Object, TypedArray, Value, function::Args,
    function::Constructor,
};
use std::{borrow::Cow, sync::Arc};

macro_rules! get_jsvalue {
    ($array_type: ty, $ctx:expr, $array:expr, $i:expr) => {{
        let array = $array.as_any().downcast_ref::<$array_type>().unwrap();
        array.value($i).into_js($ctx)
    }};
}

macro_rules! get_date_ms_js_value {
    ($array_type: ty, $ctx:expr, $array:expr, $i:expr) => {{
        let array = $array.as_any().downcast_ref::<$array_type>().unwrap();
        let date_constructor: Constructor = $ctx.globals().get("Date")?;
        let date_ms = array
            .value_as_datetime($i)
            .expect("failed to get date as datetime")
            .and_utc()
            .timestamp_millis();
        date_constructor.construct((date_ms,))?
    }};
}

macro_rules! build_timestamp_array {
    ($builder_type: ty, $date_primitive_type:ty, $ctx:expr, $values:expr, $op:tt, $coeff:expr) => {{
        let date_to_ms_epoch: Function = $ctx
            .eval("(function(x) { return x.getTime() })")
            .context("failed to get date to ms epoch function")?;

        let mut builder = <$builder_type>::with_capacity($values.len());

        for val in $values {
            if val.is_null() || val.is_undefined() {
                builder.append_null();
            } else {
                let date: i64 = date_to_ms_epoch.call((val,))?;
                let date = date $op $coeff;
                builder.append_value(date as $date_primitive_type);
            }
        }
        Ok(Arc::new(builder.finish()))
    }};
}

macro_rules! get_typed_array {
    ($array_type: ty, $ctx:expr, $array:expr) => {{
        let array = $array.as_any().downcast_ref::<$array_type>().unwrap();
        if let Some(nulls) = array.nulls() {
            let mut values = Vec::with_capacity(array.len());
            for i in 0..array.len() {
                let value = if nulls.is_null(i) {
                    Value::new_null($ctx.clone())
                } else {
                    array.value(i).into_js($ctx)?
                };
                values.push(value);
            }
            values.into_js($ctx)
        } else {
            TypedArray::new($ctx.clone(), array.values().as_ref()).map(|a| a.into_value())
        }
    }};
}

macro_rules! build_array {
    (NullBuilder, $ctx:expr, $values:expr) => {{
        let mut builder = NullBuilder::new();
        for val in $values {
            if val.is_null() || val.is_undefined() {
                builder.append_null();
            } else {
                builder.append_empty_value();
            }
        }
        Ok(Arc::new(builder.finish()))
    }};
    // primitive types
    ($builder_type: ty, $ctx:expr, $values:expr) => {{
        let mut builder = <$builder_type>::with_capacity($values.len());
        for val in $values {
            if val.is_null() || val.is_undefined() {
                builder.append_null();
            } else {
                builder.append_value(FromJs::from_js($ctx, val)?);
            }
        }
        Ok(Arc::new(builder.finish()))
    }};
    // string and bytea
    ($builder_type: ty, $elem_type: ty, $ctx:expr, $values:expr) => {{
        let mut builder = <$builder_type>::with_capacity($values.len(), 1024);
        for val in $values {
            if val.is_null() || val.is_undefined() {
                builder.append_null();
            } else {
                builder.append_value(<$elem_type>::from_js($ctx, val)?);
            }
        }
        Ok(Arc::new(builder.finish()))
    }};
    // view
    ($builder_type: ty, $elem_type: ty, $ctx:expr, $values:expr, $dummy: expr) => {{
        let mut builder = <$builder_type>::with_capacity($values.len());
        for val in $values {
            if val.is_null() || val.is_undefined() {
                builder.append_null();
            } else {
                builder.append_value(<$elem_type>::from_js($ctx, val)?);
            }
        }
        Ok(Arc::new(builder.finish()))
    }};
}

macro_rules! build_json_array {
    ($array_type: ty, $ctx:expr, $values:expr) => {{
        let mut builder = <$array_type>::with_capacity($values.len(), 1024);
        for val in $values {
            if val.is_null() || val.is_undefined() {
                builder.append_null();
            } else if let Some(s) = $ctx.json_stringify(val)? {
                builder.append_value(s.to_string()?);
            } else {
                builder.append_null();
            }
        }
        Ok(Arc::new(builder.finish()))
    }};
    ($array_type: ty, $ctx:expr, $values:expr, $view: expr) => {{
        let mut builder = <$array_type>::with_capacity($values.len());
        for val in $values {
            if val.is_null() || val.is_undefined() {
                builder.append_null();
            } else if let Some(s) = $ctx.json_stringify(val)? {
                builder.append_value(s.to_string()?);
            } else {
                builder.append_null();
            }
        }
        Ok(Arc::new(builder.finish()))
    }};
}

#[derive(Debug, Clone)]
pub struct Converter {
    arrow_extension_key: Cow<'static, str>,
    json_extension_name: Cow<'static, str>,
    decimal_extension_name: Cow<'static, str>,
}

impl Converter {
    pub(crate) fn new() -> Self {
        Self {
            arrow_extension_key: "ARROW:extension:name".into(),
            json_extension_name: "arrowudf.json".into(),
            decimal_extension_name: "arrowudf.decimal".into(),
        }
    }

    /// Set the key for the arrow extension.
    ///
    /// The default value is `ARROW:extension:name`.
    pub fn set_arrow_extension_key(&mut self, key: &str) {
        self.arrow_extension_key = key.to_string().into();
    }

    /// Set the name for the json extension.
    ///
    /// The default value is `arrowudf.json`.
    pub fn set_json_extension_name(&mut self, name: &str) {
        self.json_extension_name = name.to_string().into();
    }

    /// Set the name for the decimal extension.
    ///
    /// The default value is `arrowudf.decimal`.
    pub fn set_decimal_extension_name(&mut self, name: &str) {
        self.decimal_extension_name = name.to_string().into();
    }

    /// Get array element as a JS Value.
    pub(super) fn get_jsvalue<'a>(
        &self,
        ctx: &Ctx<'a>,
        field: &Field,
        array: &dyn Array,
        i: usize,
    ) -> Result<Value<'a>, Error> {
        if array.is_null(i) {
            return Ok(Value::new_null(ctx.clone()));
        }

        match array.data_type() {
            DataType::Null => Ok(Value::new_null(ctx.clone())),
            DataType::Boolean => get_jsvalue!(BooleanArray, ctx, array, i),
            DataType::Int8 => get_jsvalue!(Int8Array, ctx, array, i),
            DataType::Int16 => get_jsvalue!(Int16Array, ctx, array, i),
            DataType::Int32 => get_jsvalue!(Int32Array, ctx, array, i),
            DataType::Int64 => get_jsvalue!(Int64Array, ctx, array, i),
            DataType::UInt8 => get_jsvalue!(UInt8Array, ctx, array, i),
            DataType::UInt16 => get_jsvalue!(UInt16Array, ctx, array, i),
            DataType::UInt32 => get_jsvalue!(UInt32Array, ctx, array, i),
            DataType::UInt64 => get_jsvalue!(UInt64Array, ctx, array, i),
            DataType::Float32 => get_jsvalue!(Float32Array, ctx, array, i),
            DataType::Float64 => get_jsvalue!(Float64Array, ctx, array, i),
            DataType::Utf8 => match field.metadata().get(self.arrow_extension_key.as_ref()) {
                Some(x) if x == self.json_extension_name.as_ref() => {
                    let array = array.as_any().downcast_ref::<StringArray>().unwrap();
                    ctx.json_parse(array.value(i))
                }
                Some(x) if x == self.decimal_extension_name.as_ref() => {
                    let array = array.as_any().downcast_ref::<StringArray>().unwrap();

                    self.call_bigdecimal(ctx, array.value(i))
                }
                _ => get_jsvalue!(StringArray, ctx, array, i),
            },
            DataType::Binary => match field.metadata().get(self.arrow_extension_key.as_ref()) {
                Some(x) if x == self.json_extension_name.as_ref() => {
                    let array = array.as_any().downcast_ref::<BinaryArray>().unwrap();
                    ctx.json_parse(array.value(i))
                }
                _ => get_jsvalue!(BinaryArray, ctx, array, i),
            },
            DataType::LargeUtf8 => get_jsvalue!(LargeStringArray, ctx, array, i),
            DataType::LargeBinary => {
                match field.metadata().get(self.arrow_extension_key.as_ref()) {
                    Some(x) if x == self.json_extension_name.as_ref() => {
                        let array = array.as_any().downcast_ref::<LargeBinaryArray>().unwrap();
                        ctx.json_parse(array.value(i))
                    }
                    _ => get_jsvalue!(LargeBinaryArray, ctx, array, i),
                }
            }
            DataType::Utf8View => get_jsvalue!(StringViewArray, ctx, array, i),
            DataType::BinaryView => match field.metadata().get(self.arrow_extension_key.as_ref()) {
                Some(x) if x == self.json_extension_name.as_ref() => {
                    let array = array.as_any().downcast_ref::<BinaryViewArray>().unwrap();
                    ctx.json_parse(array.value(i))
                }
                _ => get_jsvalue!(BinaryViewArray, ctx, array, i),
            },
            DataType::Decimal128(_, _) => {
                let array = array.as_any().downcast_ref::<Decimal128Array>().unwrap();
                let decimal_str = array.value_as_string(i);

                self.call_bigdecimal(ctx, &decimal_str)
            }
            DataType::Decimal256(_, _) => {
                let array = array.as_any().downcast_ref::<Decimal256Array>().unwrap();
                let decimal_str = array.value_as_string(i);

                self.call_bigdecimal(ctx, &decimal_str)
            }
            // TODO: handle tz correctly. requires probably converting tz str into a Chrono Tz
            DataType::Timestamp(unit, _tz) => {
                match unit {
                    // TODO: test this
                    arrow_schema::TimeUnit::Second => {
                        get_date_ms_js_value!(TimestampSecondArray, ctx, array, i)
                    }
                    arrow_schema::TimeUnit::Millisecond => {
                        get_date_ms_js_value!(TimestampMillisecondArray, ctx, array, i)
                    }
                    arrow_schema::TimeUnit::Microsecond => {
                        get_date_ms_js_value!(TimestampMicrosecondArray, ctx, array, i)
                    }
                    arrow_schema::TimeUnit::Nanosecond => {
                        get_date_ms_js_value!(TimestampNanosecondArray, ctx, array, i)
                    }
                }
            }
            DataType::Date32 => {
                get_date_ms_js_value!(Date32Array, ctx, array, i)
            }
            // list
            DataType::List(inner) => {
                let array = array.as_any().downcast_ref::<ListArray>().unwrap();
                let list = array.value(i);
                match inner.data_type() {
                    DataType::Int8 => get_typed_array!(Int8Array, ctx, list),
                    DataType::Int16 => get_typed_array!(Int16Array, ctx, list),
                    DataType::Int32 => get_typed_array!(Int32Array, ctx, list),
                    DataType::Int64 => get_typed_array!(Int64Array, ctx, list),
                    DataType::UInt8 => get_typed_array!(UInt8Array, ctx, list),
                    DataType::UInt16 => get_typed_array!(UInt16Array, ctx, list),
                    DataType::UInt32 => get_typed_array!(UInt32Array, ctx, list),
                    DataType::UInt64 => get_typed_array!(UInt64Array, ctx, list),
                    DataType::Float32 => get_typed_array!(Float32Array, ctx, list),
                    DataType::Float64 => get_typed_array!(Float64Array, ctx, list),
                    _ => {
                        let mut values = Vec::with_capacity(list.len());
                        for j in 0..list.len() {
                            values.push(self.get_jsvalue(ctx, field, list.as_ref(), j)?);
                        }
                        values.into_js(ctx)
                    }
                }
            }
            // large list
            DataType::LargeList(inner) => {
                let array = array.as_any().downcast_ref::<LargeListArray>().unwrap();
                let list = array.value(i);
                match inner.data_type() {
                    DataType::Int8 => get_typed_array!(Int8Array, ctx, list),
                    DataType::Int16 => get_typed_array!(Int16Array, ctx, list),
                    DataType::Int32 => get_typed_array!(Int32Array, ctx, list),
                    DataType::Int64 => get_typed_array!(Int64Array, ctx, list),
                    DataType::UInt8 => get_typed_array!(UInt8Array, ctx, list),
                    DataType::UInt16 => get_typed_array!(UInt16Array, ctx, list),
                    DataType::UInt32 => get_typed_array!(UInt32Array, ctx, list),
                    DataType::UInt64 => get_typed_array!(UInt64Array, ctx, list),
                    DataType::Float32 => get_typed_array!(Float32Array, ctx, list),
                    DataType::Float64 => get_typed_array!(Float64Array, ctx, list),
                    _ => {
                        let mut values = Vec::with_capacity(list.len());
                        for j in 0..list.len() {
                            values.push(self.get_jsvalue(ctx, field, list.as_ref(), j)?);
                        }
                        values.into_js(ctx)
                    }
                }
            }
            DataType::Map(_, _) => {
                let array = array.as_any().downcast_ref::<MapArray>().unwrap();
                let list = array.value(i);
                if list.num_columns() != 2 {
                    return Err(Error::Unknown);
                }
                let fields = list.fields();
                let key_field = &fields[0];
                let value_field = &fields[1];

                let columns = list.columns();
                let keys = &columns[0];
                let values = &columns[1];

                let object = Object::new(ctx.clone())?;
                for j in 0..list.len() {
                    let key = self.get_jsvalue(ctx, key_field, keys, j)?;
                    let value = self.get_jsvalue(ctx, value_field, values, j)?;
                    object.set(key, value)?;
                }
                Ok(object.into_value())
            }
            DataType::Struct(fields) => {
                let array = array.as_any().downcast_ref::<StructArray>().unwrap();
                let object = Object::new(ctx.clone())?;
                for (j, field) in fields.iter().enumerate() {
                    let value = self.get_jsvalue(ctx, field, array.column(j).as_ref(), i)?;
                    object.set(field.name(), value)?;
                }
                Ok(object.into_value())
            }
            _other => Err(Error::Unknown),
        }
    }

    pub(super) fn build_array<'a>(
        &self,
        field: &Field,
        ctx: &Ctx<'a>,
        values: Vec<Value<'a>>,
    ) -> Result<ArrayRef> {
        match field.data_type() {
            DataType::Null => build_array!(NullBuilder, ctx, values),
            DataType::Boolean => build_array!(BooleanBuilder, ctx, values),
            DataType::Int8 => build_array!(Int8Builder, ctx, values),
            DataType::Int16 => build_array!(Int16Builder, ctx, values),
            DataType::Int32 => build_array!(Int32Builder, ctx, values),
            DataType::Int64 => build_array!(Int64Builder, ctx, values),
            DataType::UInt8 => build_array!(UInt8Builder, ctx, values),
            DataType::UInt16 => build_array!(UInt16Builder, ctx, values),
            DataType::UInt32 => build_array!(UInt32Builder, ctx, values),
            DataType::UInt64 => build_array!(UInt64Builder, ctx, values),
            DataType::Float32 => build_array!(Float32Builder, ctx, values),
            DataType::Float64 => build_array!(Float64Builder, ctx, values),
            DataType::Utf8 => match field.metadata().get(self.arrow_extension_key.as_ref()) {
                Some(x) if x == self.json_extension_name.as_ref() => {
                    build_json_array!(StringBuilder, ctx, values)
                }
                Some(x) if x == self.decimal_extension_name.as_ref() => {
                    let mut builder = StringBuilder::with_capacity(values.len(), 1024);
                    let bigdecimal_to_string: Function = ctx
                        .eval("BigDecimal.prototype.toString")
                        .context("failed to get BigDecimal.prototype.string")?;

                    for val in values {
                        if val.is_null() || val.is_undefined() {
                            builder.append_null();
                        } else {
                            let mut args = Args::new(ctx.clone(), 0);
                            args.this(val)?;

                            let string: String = bigdecimal_to_string.call_arg(args).context(
                                "failed to convert BigDecimal to string. make sure you return a BigDecimal value",
                            )?;

                            builder.append_value(string);
                        }
                    }
                    Ok(Arc::new(builder.finish()))
                }
                _ => build_array!(StringBuilder, String, ctx, values),
            },
            DataType::LargeUtf8 => build_array!(LargeStringBuilder, String, ctx, values),
            DataType::Utf8View => build_array!(StringViewBuilder, String, ctx, values, 1),
            DataType::Binary => match field.metadata().get(self.arrow_extension_key.as_ref()) {
                Some(x) if x == self.json_extension_name.as_ref() => {
                    build_json_array!(BinaryBuilder, ctx, values)
                }
                _ => build_array!(BinaryBuilder, Vec::<u8>, ctx, values),
            },
            DataType::LargeBinary => {
                match field.metadata().get(self.arrow_extension_key.as_ref()) {
                    Some(x) if x == self.json_extension_name.as_ref() => {
                        build_json_array!(LargeBinaryBuilder, ctx, values)
                    }
                    _ => build_array!(LargeBinaryBuilder, Vec::<u8>, ctx, values),
                }
            }
            DataType::BinaryView => match field.metadata().get(self.arrow_extension_key.as_ref()) {
                Some(x) if x == self.json_extension_name.as_ref() => {
                    build_json_array!(BinaryViewBuilder, ctx, values, 1)
                }
                _ => build_array!(BinaryViewBuilder, Vec::<u8>, ctx, values, 1),
            },
            DataType::Decimal128(precision, scale) => {
                let mut builder = Decimal128Builder::with_capacity(values.len())
                    .with_precision_and_scale(*precision, *scale)?;

                let bigdecimal_to_precision: Function =
                    self.get_bigdecimal_to_precision_function(ctx)?;

                for val in values {
                    if val.is_null() || val.is_undefined() {
                        builder.append_null();
                    } else {
                        let mut args = Args::new(ctx.clone(), 0);
                        args.this(val)?;
                        args.push_arg(*precision)?;
                        let string: String = bigdecimal_to_precision.call_arg(args).context(
                            "failed to convert BigDecimal to string. make sure you return a BigDecimal value",
                        )?;

                        let decimal_integer = self.decimal_string_to_i128(&string, *scale)?;
                        builder.append_value(decimal_integer);
                    }
                }
                Ok(Arc::new(builder.finish()))
            }
            DataType::Decimal256(precision, scale) => {
                let mut builder = Decimal256Builder::with_capacity(values.len())
                    .with_precision_and_scale(*precision, *scale)?;

                let bigdecimal_to_precision = self.get_bigdecimal_to_precision_function(ctx)?;

                for val in values {
                    if val.is_null() || val.is_undefined() {
                        builder.append_null();
                    } else {
                        let mut args = Args::new(ctx.clone(), 0);
                        args.this(val)?;
                        args.push_arg(*precision)?;
                        let string: String = bigdecimal_to_precision.call_arg(args).context(
                            "failed to convert BigDecimal to string. make sure you return a BigDecimal value",
                        )?;
                        let decimal_integer = self.decimal_string_to_i256(&string, *scale)?;
                        builder.append_value(decimal_integer);
                    }
                }
                Ok(Arc::new(builder.finish()))
            }
            DataType::Timestamp(unit, _tz) => {
                match unit {
                    // TODO denomenator is not quite right because if the fundamental unit is in
                    // milliseconds, then to convert nanoseconds to milliseconds, you need to divide by 1_000_000
                    arrow_schema::TimeUnit::Second => {
                        build_timestamp_array!(TimestampSecondBuilder, i64, ctx, values, /, 1000)
                    }
                    arrow_schema::TimeUnit::Millisecond => {
                        build_timestamp_array!(TimestampMillisecondBuilder, i64, ctx, values, /, 1)
                    }
                    arrow_schema::TimeUnit::Microsecond => {
                        build_timestamp_array!(TimestampMicrosecondBuilder, i64, ctx, values, *, 1000)
                    }
                    arrow_schema::TimeUnit::Nanosecond => {
                        build_timestamp_array!(TimestampNanosecondBuilder, i64, ctx, values, *, 1_000_000)
                    }
                }
            }
            DataType::Date32 => {
                build_timestamp_array!(Date32Builder, i32, ctx, values, /, 1000 * 60 * 60 * 24)
            }
            // list
            DataType::List(inner) => {
                // flatten lists
                let mut flatten_values = vec![];
                let mut offsets = Vec::<i32>::with_capacity(values.len() + 1);
                offsets.push(0);
                for val in &values {
                    if !val.is_null() && !val.is_undefined() {
                        let array = val.as_array().context("failed to convert to array")?;
                        flatten_values.reserve(array.len());
                        for elem in array.iter() {
                            flatten_values.push(elem?);
                        }
                    }
                    offsets.push(flatten_values.len() as i32);
                }
                let values_array = self.build_array(inner, ctx, flatten_values)?;
                let nulls = values
                    .iter()
                    .map(|v| !v.is_null() && !v.is_undefined())
                    .collect();
                Ok(Arc::new(ListArray::new(
                    inner.clone(),
                    OffsetBuffer::new(offsets.into()),
                    values_array,
                    Some(nulls),
                )))
            }
            DataType::LargeList(inner) => {
                // flatten lists
                let mut flatten_values = vec![];
                let mut offsets = Vec::<i64>::with_capacity(values.len() + 1);
                offsets.push(0);
                for val in &values {
                    if !val.is_null() && !val.is_undefined() {
                        let array = val.as_array().context("failed to convert to array")?;
                        flatten_values.reserve(array.len());
                        for elem in array.iter() {
                            flatten_values.push(elem?);
                        }
                    }
                    offsets.push(flatten_values.len() as i64);
                }
                let values_array = self.build_array(inner, ctx, flatten_values)?;
                let nulls = values
                    .iter()
                    .map(|v| !v.is_null() && !v.is_undefined())
                    .collect();
                Ok(Arc::new(LargeListArray::new(
                    inner.clone(),
                    OffsetBuffer::new(offsets.into()),
                    values_array,
                    Some(nulls),
                )))
            }
            DataType::Map(inner, _) => {
                let (key_field, value_field) = match inner.data_type() {
                    DataType::Struct(fields) => {
                        if fields.len() != 2 {
                            return Err(anyhow::anyhow!(
                                "Invalid map inner struct fields length {}",
                                fields.len()
                            ));
                        }
                        (fields[0].clone(), fields[1].clone())
                    }
                    _ => {
                        return Err(anyhow::anyhow!(
                            "Invalid map inner datatype {}",
                            inner.data_type()
                        ));
                    }
                };
                let mut flatten_keys = vec![];
                let mut flatten_values = vec![];
                let mut offsets = Vec::<i32>::with_capacity(values.len() + 1);
                offsets.push(0);
                for val in &values {
                    if !val.is_null() && !val.is_undefined() {
                        let object = val.as_object().context("failed to convert to object")?;
                        for key in object.keys() {
                            flatten_keys.push(key?);
                        }
                        for value in object.values() {
                            flatten_values.push(value?);
                        }
                    }
                    offsets.push(flatten_keys.len() as i32);
                }
                let arrays = vec![
                    self.build_array(&key_field, ctx, flatten_keys)?,
                    self.build_array(&value_field, ctx, flatten_values)?,
                ];
                let struct_array =
                    StructArray::new(Fields::from([key_field, value_field]), arrays, None);

                let nulls = values
                    .iter()
                    .map(|v| !v.is_null() && !v.is_undefined())
                    .collect();
                Ok(Arc::new(MapArray::new(
                    inner.clone(),
                    OffsetBuffer::new(offsets.into()),
                    struct_array,
                    Some(nulls),
                    false,
                )))
            }
            DataType::Struct(fields) => {
                let mut arrays = Vec::with_capacity(fields.len());
                for field in fields {
                    let mut field_values = Vec::with_capacity(values.len());
                    for val in &values {
                        let v = if val.is_null() || val.is_undefined() {
                            Value::new_null(ctx.clone())
                        } else {
                            let object = val.as_object().context("expect object")?;
                            object.get(field.name())?
                        };
                        field_values.push(v);
                    }
                    arrays.push(self.build_array(field, ctx, field_values)?);
                }
                let nulls = values
                    .iter()
                    .map(|v| !v.is_null() && !v.is_undefined())
                    .collect();
                Ok(Arc::new(StructArray::new(
                    fields.clone(),
                    arrays,
                    Some(nulls),
                )))
            }
            other => Err(anyhow::anyhow!("Unimplemented datatype {}", other)),
        }
    }

    fn call_bigdecimal<'a>(
        &self,
        ctx: &Ctx<'a>,
        value: &str,
    ) -> rquickjs::Result<rquickjs::Value<'a>> {
        let bigdecimal: Function = ctx.globals().get("BigDecimal")?;
        bigdecimal.call((value,))
    }

    fn get_bigdecimal_to_precision_function<'a>(&self, ctx: &Ctx<'a>) -> Result<Function<'a>> {
        ctx.eval("BigDecimal.prototype.toPrecision")
            .context("failed to get BigDecimal.prototype.toPrecision")
    }

    fn decimal_string_to_i128(&self, s: &str, scale: i8) -> Result<i128> {
        if scale < 0 {
            return Err(anyhow::anyhow!(
                "currently only supports non-negative scale"
            ));
        }

        let (integer, fractional): (i128, i128) = match s.split_once('.') {
            Some((i, f)) => (
                i.parse().context("failed to parse integer part")?,
                (f[..scale as usize])
                    .to_string()
                    .parse()
                    .context("failed to parse fractional part")?,
            ),
            None => (s.parse().context("failed to parse integer part")?, 0),
        };

        Ok((integer * 10_i128.pow(scale as u32)) + fractional)
    }

    fn decimal_string_to_i256(&self, s: &str, scale: i8) -> Result<i256> {
        if scale < 0 {
            return Err(anyhow::anyhow!(
                "currently only supports non-negative scale"
            ));
        }

        // TODO: apply pattern from i128 here
        let (integer, fractional) = match s.split_once('.') {
            Some((i, f)) => (
                i256::from_string(i)
                    .ok_or_else(|| anyhow::anyhow!("failed to parse integer part"))?,
                i256::from_string(&f[..scale as usize])
                    .ok_or_else(|| anyhow::anyhow!("failed to parse fractional part"))?,
            ),
            None => (
                i256::from_string(s)
                    .ok_or_else(|| anyhow::anyhow!("failed to parse integer part"))?,
                i256::ZERO,
            ),
        };

        Ok((integer * i256::from_i128(10).pow_checked(scale as u32)?) + fractional)
    }
}