polars_parquet/arrow/read/statistics/
mod.rs

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
//! APIs exposing `crate::parquet`'s statistics as arrow's statistics.
use std::collections::VecDeque;

use arrow::array::*;
use arrow::datatypes::{ArrowDataType, Field, IntervalUnit, PhysicalType};
use arrow::types::{f16, i256, NativeType};
use arrow::with_match_primitive_type_full;
use ethnum::I256;
use polars_error::{polars_bail, PolarsResult};

use crate::parquet::schema::types::{
    PhysicalType as ParquetPhysicalType, PrimitiveType as ParquetPrimitiveType,
};
use crate::parquet::statistics::{PrimitiveStatistics, Statistics as ParquetStatistics};
use crate::parquet::types::int96_to_i64_ns;
use crate::read::ColumnChunkMetadata;

mod binary;
mod binview;
mod boolean;
mod dictionary;
mod fixlen;
mod list;
mod map;
mod null;
mod primitive;
mod struct_;
mod utf8;

use self::list::DynMutableListArray;
use super::PrimitiveLogicalType;

/// Arrow-deserialized parquet Statistics of a file
#[derive(Debug, PartialEq)]
pub struct Statistics {
    /// number of nulls. This is a [`UInt64Array`] for non-nested types
    pub null_count: Box<dyn Array>,
    /// number of dictinct values. This is a [`UInt64Array`] for non-nested types
    pub distinct_count: Box<dyn Array>,
    /// Minimum
    pub min_value: Box<dyn Array>,
    /// Maximum
    pub max_value: Box<dyn Array>,
}

/// Arrow-deserialized parquet Statistics of a file
#[derive(Debug)]
struct MutableStatistics {
    /// number of nulls
    pub null_count: Box<dyn MutableArray>,
    /// number of dictinct values
    pub distinct_count: Box<dyn MutableArray>,
    /// Minimum
    pub min_value: Box<dyn MutableArray>,
    /// Maximum
    pub max_value: Box<dyn MutableArray>,
}

impl From<MutableStatistics> for Statistics {
    fn from(mut s: MutableStatistics) -> Self {
        let null_count = match s.null_count.dtype().to_physical_type() {
            PhysicalType::Struct => s
                .null_count
                .as_box()
                .as_any()
                .downcast_ref::<StructArray>()
                .unwrap()
                .clone()
                .boxed(),
            PhysicalType::List => s
                .null_count
                .as_box()
                .as_any()
                .downcast_ref::<ListArray<i32>>()
                .unwrap()
                .clone()
                .boxed(),
            PhysicalType::LargeList => s
                .null_count
                .as_box()
                .as_any()
                .downcast_ref::<ListArray<i64>>()
                .unwrap()
                .clone()
                .boxed(),
            _ => s
                .null_count
                .as_box()
                .as_any()
                .downcast_ref::<UInt64Array>()
                .unwrap()
                .clone()
                .boxed(),
        };

        let distinct_count = match s.distinct_count.dtype().to_physical_type() {
            PhysicalType::Struct => s
                .distinct_count
                .as_box()
                .as_any()
                .downcast_ref::<StructArray>()
                .unwrap()
                .clone()
                .boxed(),
            PhysicalType::List => s
                .distinct_count
                .as_box()
                .as_any()
                .downcast_ref::<ListArray<i32>>()
                .unwrap()
                .clone()
                .boxed(),
            PhysicalType::LargeList => s
                .distinct_count
                .as_box()
                .as_any()
                .downcast_ref::<ListArray<i64>>()
                .unwrap()
                .clone()
                .boxed(),
            _ => s
                .distinct_count
                .as_box()
                .as_any()
                .downcast_ref::<UInt64Array>()
                .unwrap()
                .clone()
                .boxed(),
        };

        Self {
            null_count,
            distinct_count,
            min_value: s.min_value.as_box(),
            max_value: s.max_value.as_box(),
        }
    }
}

fn make_mutable(dtype: &ArrowDataType, capacity: usize) -> PolarsResult<Box<dyn MutableArray>> {
    Ok(match dtype.to_physical_type() {
        PhysicalType::Boolean => {
            Box::new(MutableBooleanArray::with_capacity(capacity)) as Box<dyn MutableArray>
        },
        PhysicalType::Primitive(primitive) => with_match_primitive_type_full!(primitive, |$T| {
            Box::new(MutablePrimitiveArray::<$T>::with_capacity(capacity).to(dtype.clone()))
                as Box<dyn MutableArray>
        }),
        PhysicalType::Binary => {
            Box::new(MutableBinaryArray::<i32>::with_capacity(capacity)) as Box<dyn MutableArray>
        },
        PhysicalType::LargeBinary => {
            Box::new(MutableBinaryArray::<i64>::with_capacity(capacity)) as Box<dyn MutableArray>
        },
        PhysicalType::Utf8 => {
            Box::new(MutableUtf8Array::<i32>::with_capacity(capacity)) as Box<dyn MutableArray>
        },
        PhysicalType::LargeUtf8 => {
            Box::new(MutableUtf8Array::<i64>::with_capacity(capacity)) as Box<dyn MutableArray>
        },
        PhysicalType::FixedSizeBinary => {
            Box::new(MutableFixedSizeBinaryArray::try_new(dtype.clone(), vec![], None).unwrap())
                as _
        },
        PhysicalType::LargeList | PhysicalType::List | PhysicalType::FixedSizeList => Box::new(
            DynMutableListArray::try_with_capacity(dtype.clone(), capacity)?,
        )
            as Box<dyn MutableArray>,
        PhysicalType::Dictionary(_) => Box::new(
            dictionary::DynMutableDictionary::try_with_capacity(dtype.clone(), capacity)?,
        ),
        PhysicalType::Struct => Box::new(struct_::DynMutableStructArray::try_with_capacity(
            dtype.clone(),
            capacity,
        )?),
        PhysicalType::Map => Box::new(map::DynMutableMapArray::try_with_capacity(
            dtype.clone(),
            capacity,
        )?),
        PhysicalType::Null => {
            Box::new(MutableNullArray::new(ArrowDataType::Null, 0)) as Box<dyn MutableArray>
        },
        PhysicalType::BinaryView => {
            Box::new(MutablePlBinary::with_capacity(capacity)) as Box<dyn MutableArray>
        },
        PhysicalType::Utf8View => {
            Box::new(MutablePlString::with_capacity(capacity)) as Box<dyn MutableArray>
        },
        other => {
            polars_bail!(
                nyi = "deserializing parquet stats from {other:?} is still not implemented"
            )
        },
    })
}

fn create_dt(dtype: &ArrowDataType) -> ArrowDataType {
    match dtype.to_logical_type() {
        ArrowDataType::Struct(fields) => ArrowDataType::Struct(
            fields
                .iter()
                .map(|f| Field::new(f.name.clone(), create_dt(&f.dtype), f.is_nullable))
                .collect(),
        ),
        ArrowDataType::Map(f, ordered) => ArrowDataType::Map(
            Box::new(Field::new(
                f.name.clone(),
                create_dt(&f.dtype),
                f.is_nullable,
            )),
            *ordered,
        ),
        ArrowDataType::LargeList(f) => ArrowDataType::LargeList(Box::new(Field::new(
            f.name.clone(),
            create_dt(&f.dtype),
            f.is_nullable,
        ))),
        // FixedSizeList piggy backs on list
        ArrowDataType::List(f) | ArrowDataType::FixedSizeList(f, _) => {
            ArrowDataType::List(Box::new(Field::new(
                f.name.clone(),
                create_dt(&f.dtype),
                f.is_nullable,
            )))
        },
        _ => ArrowDataType::UInt64,
    }
}

impl MutableStatistics {
    fn try_new(field: &Field) -> PolarsResult<Self> {
        let min_value = make_mutable(&field.dtype, 0)?;
        let max_value = make_mutable(&field.dtype, 0)?;

        let dt = create_dt(&field.dtype);
        Ok(Self {
            null_count: make_mutable(&dt, 0)?,
            distinct_count: make_mutable(&dt, 0)?,
            min_value,
            max_value,
        })
    }
}

fn push_others(
    from: Option<&ParquetStatistics>,
    distinct_count: &mut UInt64Vec,
    null_count: &mut UInt64Vec,
) {
    let from = if let Some(from) = from {
        from
    } else {
        distinct_count.push(None);
        null_count.push(None);
        return;
    };
    use ParquetStatistics as S;
    let (distinct, null_count1) = match from {
        S::Binary(s) => (s.distinct_count, s.null_count),
        S::Boolean(s) => (s.distinct_count, s.null_count),
        S::FixedLen(s) => (s.distinct_count, s.null_count),
        S::Int32(s) => (s.distinct_count, s.null_count),
        S::Int64(s) => (s.distinct_count, s.null_count),
        S::Int96(s) => (s.distinct_count, s.null_count),
        S::Float(s) => (s.distinct_count, s.null_count),
        S::Double(s) => (s.distinct_count, s.null_count),
    };

    distinct_count.push(distinct.map(|x| x as u64));
    null_count.push(null_count1.map(|x| x as u64));
}

fn push(
    stats: &mut VecDeque<(Option<ParquetStatistics>, ParquetPrimitiveType)>,
    min: &mut dyn MutableArray,
    max: &mut dyn MutableArray,
    distinct_count: &mut dyn MutableArray,
    null_count: &mut dyn MutableArray,
) -> PolarsResult<()> {
    match min.dtype().to_logical_type() {
        List(_) | LargeList(_) | FixedSizeList(_, _) => {
            let min = min
                .as_mut_any()
                .downcast_mut::<list::DynMutableListArray>()
                .unwrap();
            let max = max
                .as_mut_any()
                .downcast_mut::<list::DynMutableListArray>()
                .unwrap();
            let distinct_count = distinct_count
                .as_mut_any()
                .downcast_mut::<list::DynMutableListArray>()
                .unwrap();
            let null_count = null_count
                .as_mut_any()
                .downcast_mut::<list::DynMutableListArray>()
                .unwrap();
            return push(
                stats,
                min.inner.as_mut(),
                max.inner.as_mut(),
                distinct_count.inner.as_mut(),
                null_count.inner.as_mut(),
            );
        },
        Dictionary(_, _, _) => {
            let min = min
                .as_mut_any()
                .downcast_mut::<dictionary::DynMutableDictionary>()
                .unwrap();
            let max = max
                .as_mut_any()
                .downcast_mut::<dictionary::DynMutableDictionary>()
                .unwrap();
            return push(
                stats,
                min.inner.as_mut(),
                max.inner.as_mut(),
                distinct_count,
                null_count,
            );
        },
        Struct(fields) => {
            if fields.is_empty() {
                return Ok(());
            }

            let min = min
                .as_mut_any()
                .downcast_mut::<struct_::DynMutableStructArray>()
                .unwrap();
            let max = max
                .as_mut_any()
                .downcast_mut::<struct_::DynMutableStructArray>()
                .unwrap();
            let distinct_count = distinct_count
                .as_mut_any()
                .downcast_mut::<struct_::DynMutableStructArray>()
                .unwrap();
            let null_count = null_count
                .as_mut_any()
                .downcast_mut::<struct_::DynMutableStructArray>()
                .unwrap();

            return min
                .inner_mut()
                .iter_mut()
                .zip(max.inner_mut())
                .zip(distinct_count.inner_mut())
                .zip(null_count.inner_mut())
                .try_for_each(|(((min, max), distinct_count), null_count)| {
                    push(
                        stats,
                        min.as_mut(),
                        max.as_mut(),
                        distinct_count.as_mut(),
                        null_count.as_mut(),
                    )
                });
        },
        Map(_, _) => {
            let min = min
                .as_mut_any()
                .downcast_mut::<map::DynMutableMapArray>()
                .unwrap();
            let max = max
                .as_mut_any()
                .downcast_mut::<map::DynMutableMapArray>()
                .unwrap();
            let distinct_count = distinct_count
                .as_mut_any()
                .downcast_mut::<map::DynMutableMapArray>()
                .unwrap();
            let null_count = null_count
                .as_mut_any()
                .downcast_mut::<map::DynMutableMapArray>()
                .unwrap();
            return push(
                stats,
                min.inner.as_mut(),
                max.inner.as_mut(),
                distinct_count.inner.as_mut(),
                null_count.inner.as_mut(),
            );
        },
        _ => {},
    }

    let (from, type_) = stats.pop_front().unwrap();
    let from = from.as_ref();

    let distinct_count = distinct_count
        .as_mut_any()
        .downcast_mut::<UInt64Vec>()
        .unwrap();
    let null_count = null_count.as_mut_any().downcast_mut::<UInt64Vec>().unwrap();

    push_others(from, distinct_count, null_count);

    let physical_type = &type_.physical_type;

    macro_rules! rmap {
        ($from:expr, $map:ident) => {{
            $from.map(ParquetStatistics::$map)
        }};
    }

    use ArrowDataType::*;
    use ParquetPhysicalType as PPT;
    match min.dtype().to_logical_type() {
        Boolean => boolean::push(rmap!(from, expect_as_boolean), min, max),
        Int8 => primitive::push(rmap!(from, expect_as_int32), min, max, |x: i32| Ok(x as i8)),
        Int16 => primitive::push(
            rmap!(from, expect_as_int32),
            min,
            max,
            |x: i32| Ok(x as i16),
        ),
        Date32 | Time32(_) => {
            primitive::push::<i32, i32, _>(rmap!(from, expect_as_int32), min, max, Ok)
        },
        Interval(IntervalUnit::YearMonth) => {
            fixlen::push_year_month(rmap!(from, expect_as_fixedlen), min, max)
        },
        Interval(IntervalUnit::DayTime) => {
            fixlen::push_days_ms(rmap!(from, expect_as_fixedlen), min, max)
        },
        UInt8 => primitive::push(rmap!(from, expect_as_int32), min, max, |x: i32| Ok(x as u8)),
        UInt16 => primitive::push(
            rmap!(from, expect_as_int32),
            min,
            max,
            |x: i32| Ok(x as u16),
        ),
        UInt32 => match physical_type {
            // some implementations of parquet write arrow's u32 into i64.
            PPT::Int64 => {
                primitive::push(
                    rmap!(from, expect_as_int64),
                    min,
                    max,
                    |x: i64| Ok(x as u32),
                )
            },
            PPT::Int32 => {
                primitive::push(
                    rmap!(from, expect_as_int32),
                    min,
                    max,
                    |x: i32| Ok(x as u32),
                )
            },
            other => polars_bail!(nyi = "Can't decode UInt32 type from parquet type {other:?}"),
        },
        Int32 => primitive::push::<i32, i32, _>(rmap!(from, expect_as_int32), min, max, Ok),
        Date64 => match physical_type {
            PPT::Int64 => {
                primitive::push::<i64, i64, _>(rmap!(from, expect_as_int64), min, max, Ok)
            },
            // some implementations of parquet write arrow's date64 into i32.
            PPT::Int32 => primitive::push(rmap!(from, expect_as_int32), min, max, |x: i32| {
                Ok(x as i64 * 86400000)
            }),
            other => polars_bail!(nyi = "Can't decode Date64 type from parquet type {other:?}"),
        },
        Int64 | Time64(_) | Duration(_) => {
            primitive::push::<i64, i64, _>(rmap!(from, expect_as_int64), min, max, Ok)
        },
        UInt64 => primitive::push(
            rmap!(from, expect_as_int64),
            min,
            max,
            |x: i64| Ok(x as u64),
        ),
        Timestamp(time_unit, _) => {
            let time_unit = *time_unit;
            if physical_type == &PPT::Int96 {
                let from = from.map(|from| {
                    let from = from.expect_as_int96();
                    PrimitiveStatistics::<i64> {
                        primitive_type: from.primitive_type.clone(),
                        null_count: from.null_count,
                        distinct_count: from.distinct_count,
                        min_value: from.min_value.map(int96_to_i64_ns),
                        max_value: from.max_value.map(int96_to_i64_ns),
                    }
                });
                primitive::push(from.as_ref(), min, max, |x: i64| {
                    Ok(primitive::timestamp(
                        type_.logical_type.as_ref(),
                        time_unit,
                        x,
                    ))
                })
            } else {
                primitive::push(rmap!(from, expect_as_int64), min, max, |x: i64| {
                    Ok(primitive::timestamp(
                        type_.logical_type.as_ref(),
                        time_unit,
                        x,
                    ))
                })
            }
        },
        Float32 => primitive::push::<f32, f32, _>(rmap!(from, expect_as_float), min, max, Ok),
        Float64 => primitive::push::<f64, f64, _>(rmap!(from, expect_as_double), min, max, Ok),
        Decimal(_, _) => match physical_type {
            PPT::Int32 => primitive::push(rmap!(from, expect_as_int32), min, max, |x: i32| {
                Ok(x as i128)
            }),
            PPT::Int64 => primitive::push(rmap!(from, expect_as_int64), min, max, |x: i64| {
                Ok(x as i128)
            }),
            PPT::FixedLenByteArray(n) if *n > 16 => polars_bail!(
                nyi = "Can't decode Decimal128 type from Fixed Size Byte Array of len {n:?}"
            ),
            PPT::FixedLenByteArray(n) => {
                fixlen::push_i128(rmap!(from, expect_as_fixedlen), *n, min, max)
            },
            _ => unreachable!(),
        },
        Decimal256(_, _) => match physical_type {
            PPT::Int32 => primitive::push(rmap!(from, expect_as_int32), min, max, |x: i32| {
                Ok(i256(I256::new(x.into())))
            }),
            PPT::Int64 => primitive::push(rmap!(from, expect_as_int64), min, max, |x: i64| {
                Ok(i256(I256::new(x.into())))
            }),
            PPT::FixedLenByteArray(n) if *n <= 16 => {
                fixlen::push_i256_with_i128(rmap!(from, expect_as_fixedlen), *n, min, max)
            },
            PPT::FixedLenByteArray(n) if *n > 32 => polars_bail!(
                nyi = "Can't decode Decimal256 type from Fixed Size Byte Array of len {n:?}"
            ),
            PPT::FixedLenByteArray(_) => {
                fixlen::push_i256(rmap!(from, expect_as_fixedlen), min, max)
            },
            _ => unreachable!(),
        },
        Binary => binary::push::<i32>(rmap!(from, expect_as_binary), min, max),
        LargeBinary => binary::push::<i64>(rmap!(from, expect_as_binary), min, max),
        Utf8 => utf8::push::<i32>(rmap!(from, expect_as_binary), min, max),
        LargeUtf8 => utf8::push::<i64>(rmap!(from, expect_as_binary), min, max),
        BinaryView => binview::push::<[u8]>(rmap!(from, expect_as_binary), min, max),
        Utf8View => binview::push::<str>(rmap!(from, expect_as_binary), min, max),
        FixedSizeBinary(_) => fixlen::push(rmap!(from, expect_as_fixedlen), min, max),

        Null => null::push(min, max),
        other => todo!("{:?}", other),
    }
}

pub fn cast_statistics(
    statistics: ParquetStatistics,
    primitive_type: &ParquetPrimitiveType,
    output_type: &ArrowDataType,
) -> ParquetStatistics {
    use {ArrowDataType as DT, PrimitiveLogicalType as PT};

    match (primitive_type.logical_type, output_type) {
        (Some(PT::Float16), DT::Float32) => {
            let statistics = statistics.expect_fixedlen();

            let primitive_type = primitive_type.clone();

            ParquetStatistics::Float(PrimitiveStatistics::<f32> {
                primitive_type,
                null_count: statistics.null_count,
                distinct_count: statistics.distinct_count,
                min_value: statistics
                    .min_value
                    .as_ref()
                    .map(|v| f16::from_le_bytes([v[0], v[1]]).to_f32()),
                max_value: statistics
                    .max_value
                    .as_ref()
                    .map(|v| f16::from_le_bytes([v[0], v[1]]).to_f32()),
            })
        },
        _ => statistics,
    }
}

/// Deserializes the statistics in the column chunks from a single `row_group`
/// into [`Statistics`] associated from `field`'s name.
///
/// # Errors
/// This function errors if the deserialization of the statistics fails (e.g. invalid utf8)
pub fn deserialize<'a>(
    field: &Field,
    field_md: impl ExactSizeIterator<Item = &'a ColumnChunkMetadata>,
) -> PolarsResult<Statistics> {
    let mut statistics = MutableStatistics::try_new(field)?;

    let mut stats = field_md
        .map(|column| {
            let primitive_type = &column.descriptor().descriptor.primitive_type;
            Ok((
                column
                    .statistics()
                    .transpose()?
                    .map(|stats| cast_statistics(stats, primitive_type, &field.dtype)),
                primitive_type.clone(),
            ))
        })
        .collect::<PolarsResult<VecDeque<(Option<ParquetStatistics>, ParquetPrimitiveType)>>>()?;
    push(
        &mut stats,
        statistics.min_value.as_mut(),
        statistics.max_value.as_mut(),
        statistics.distinct_count.as_mut(),
        statistics.null_count.as_mut(),
    )?;

    Ok(statistics.into())
}