## Coverage Report

#### Created: 2025-11-22 06:00

next uncovered line (L), next uncovered region (R), next uncovered branch (B)

    /Users/andrewlamb/Software/datafusion/datafusion/common/src/cast.rs

    Line
    Count
    Source
    1
    // Licensed to the Apache Software Foundation (ASF) under one
    2
    // or more contributor license agreements.  See the NOTICE file
    3
    // distributed with this work for additional information
    4
    // regarding copyright ownership.  The ASF licenses this file
    5
    // to you under the Apache License, Version 2.0 (the
    6
    // "License"); you may not use this file except in compliance
    7
    // with the License.  You may obtain a copy of the License at
    8
    //
    9
    //   http://www.apache.org/licenses/LICENSE-2.0
    10
    //
    11
    // Unless required by applicable law or agreed to in writing,
    12
    // software distributed under the License is distributed on an
    13
    // "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
    14
    // KIND, either express or implied.  See the License for the
    15
    // specific language governing permissions and limitations
    16
    // under the License.
    17
    18
    //! This module provides DataFusion specific casting functions
    19
    //! that provide error handling. They are intended to "never fail"
    20
    //! but provide an error message rather than a panic, as the corresponding
    21
    //! kernels in arrow-rs such as `as_boolean_array` do.
    22
    23
    use crate::{downcast_value, Result};
    24
    use arrow::array::{
    25
        BinaryViewArray, Decimal32Array, Decimal64Array, DurationMicrosecondArray,
    26
        DurationMillisecondArray, DurationNanosecondArray, DurationSecondArray, Float16Array,
    27
        Int16Array, Int8Array, LargeBinaryArray, LargeListViewArray, LargeStringArray,
    28
        ListViewArray, StringViewArray, UInt16Array,
    29
    };
    30
    use arrow::{
    31
        array::{
    32
            Array, BinaryArray, BooleanArray, Date32Array, Date64Array, Decimal128Array,
    33
            Decimal256Array, DictionaryArray, FixedSizeBinaryArray, FixedSizeListArray,
    34
            Float32Array, Float64Array, GenericBinaryArray, GenericListArray,
    35
            GenericStringArray, Int32Array, Int64Array, IntervalDayTimeArray,
    36
            IntervalMonthDayNanoArray, IntervalYearMonthArray, LargeListArray, ListArray,
    37
            MapArray, NullArray, OffsetSizeTrait, PrimitiveArray, StringArray, StructArray,
    38
            Time32MillisecondArray, Time32SecondArray, Time64MicrosecondArray,
    39
            Time64NanosecondArray, TimestampMicrosecondArray, TimestampMillisecondArray,
    40
            TimestampNanosecondArray, TimestampSecondArray, UInt32Array, UInt64Array,
    41
            UInt8Array, UnionArray,
    42
        },
    43
        datatypes::{ArrowDictionaryKeyType, ArrowPrimitiveType},
    44
    };
    45
    46
    // Downcast Array to Date32Array
    47
    0
    pub fn as_date32_array(array: &dyn Array) -> Result<&Date32Array> {
    48
    0
        Ok(downcast_value!(array, Date32Array))
    49
    0
    }
    50
    51
    // Downcast Array to Date64Array
    52
    0
    pub fn as_date64_array(array: &dyn Array) -> Result<&Date64Array> {
    53
    0
        Ok(downcast_value!(array, Date64Array))
    54
    0
    }
    55
    56
    // Downcast Array to StructArray
    57
    42
    pub fn as_struct_array(array: &dyn Array) -> Result<&StructArray> {
    58
    42
        Ok(downcast_value!(array, StructArray))
    59
    42
    }
    60
    61
    // Downcast Array to Int8Array
    62
    0
    pub fn as_int8_array(array: &dyn Array) -> Result<&Int8Array> {
    63
    0
        Ok(downcast_value!(array, Int8Array))
    64
    0
    }
    65
    66
    // Downcast Array to UInt8Array
    67
    0
    pub fn as_uint8_array(array: &dyn Array) -> Result<&UInt8Array> {
    68
    0
        Ok(downcast_value!(array, UInt8Array))
    69
    0
    }
    70
    71
    // Downcast Array to Int16Array
    72
    0
    pub fn as_int16_array(array: &dyn Array) -> Result<&Int16Array> {
    73
    0
        Ok(downcast_value!(array, Int16Array))
    74
    0
    }
    75
    76
    // Downcast Array to UInt16Array
    77
    0
    pub fn as_uint16_array(array: &dyn Array) -> Result<&UInt16Array> {
    78
    0
        Ok(downcast_value!(array, UInt16Array))
    79
    0
    }
    80
    81
    // Downcast Array to Int32Array
    82
    2
    pub fn as_int32_array(array: &dyn Array) -> Result<&Int32Array> {
    83
    2
        Ok(downcast_value!(array, Int32Array))
    84
    2
    }
    85
    86
    // Downcast Array to UInt32Array
    87
    0
    pub fn as_uint32_array(array: &dyn Array) -> Result<&UInt32Array> {
    88
    0
        Ok(downcast_value!(array, UInt32Array))
    89
    0
    }
    90
    91
    // Downcast Array to Int64Array
    92
    12
    pub fn as_int64_array(array: &dyn Array) -> Result<&Int64Array> {
    93
    12
        Ok(downcast_value!(array, Int64Array))
    94
    12
    }
    95
    96
    // Downcast Array to UInt64Array
    97
    0
    pub fn as_uint64_array(array: &dyn Array) -> Result<&UInt64Array> {
    98
    0
        Ok(downcast_value!(array, UInt64Array))
    99
    0
    }
    100
    101
    // Downcast Array to Decimal32Array
    102
    0
    pub fn as_decimal32_array(array: &dyn Array) -> Result<&Decimal32Array> {
    103
    0
        Ok(downcast_value!(array, Decimal32Array))
    104
    0
    }
    105
    106
    // Downcast Array to Decimal64Array
    107
    0
    pub fn as_decimal64_array(array: &dyn Array) -> Result<&Decimal64Array> {
    108
    0
        Ok(downcast_value!(array, Decimal64Array))
    109
    0
    }
    110
    111
    // Downcast Array to Decimal128Array
    112
    412
    pub fn as_decimal128_array(array: &dyn Array) -> Result<&Decimal128Array> {
    113
    412
        Ok(downcast_value!(array, Decimal128Array))
    114
    412
    }
    115
    116
    // Downcast Array to Decimal256Array
    117
    0
    pub fn as_decimal256_array(array: &dyn Array) -> Result<&Decimal256Array> {
    118
    0
        Ok(downcast_value!(array, Decimal256Array))
    119
    0
    }
    120
    121
    // Downcast Array to Float16Array
    122
    0
    pub fn as_float16_array(array: &dyn Array) -> Result<&Float16Array> {
    123
    0
        Ok(downcast_value!(array, Float16Array))
    124
    0
    }
    125
    126
    // Downcast Array to Float32Array
    127
    0
    pub fn as_float32_array(array: &dyn Array) -> Result<&Float32Array> {
    128
    0
        Ok(downcast_value!(array, Float32Array))
    129
    0
    }
    130
    131
    // Downcast Array to Float64Array
    132
    0
    pub fn as_float64_array(array: &dyn Array) -> Result<&Float64Array> {
    133
    0
        Ok(downcast_value!(array, Float64Array))
    134
    0
    }
    135
    136
    // Downcast Array to StringArray
    137
    54
    pub fn as_string_array(array: &dyn Array) -> Result<&StringArray> {
    138
    54
        Ok(downcast_value!(array, StringArray))
    139
    54
    }
    140
    141
    // Downcast Array to StringViewArray
    142
    16
    pub fn as_string_view_array(array: &dyn Array) -> Result<&StringViewArray> {
    143
    16
        Ok(downcast_value!(array, StringViewArray))
    144
    16
    }
    145
    146
    // Downcast Array to LargeStringArray
    147
    8
    pub fn as_large_string_array(array: &dyn Array) -> Result<&LargeStringArray> {
    148
    8
        Ok(downcast_value!(array, LargeStringArray))
    149
    8
    }
    150
    151
    // Downcast Array to BooleanArray
    152
    8
    pub fn as_boolean_array(array: &dyn Array) -> Result<&BooleanArray> {
    153
    8
        Ok(downcast_value!(array, BooleanArray))
    154
    8
    }
    155
    156
    // Downcast Array to ListArray
    157
    0
    pub fn as_list_array(array: &dyn Array) -> Result<&ListArray> {
    158
    0
        Ok(downcast_value!(array, ListArray))
    159
    0
    }
    160
    161
    // Downcast Array to DictionaryArray
    162
    0
    pub fn as_dictionary_array<T: ArrowDictionaryKeyType>(
    163
    0
        array: &dyn Array,
    164
    0
    ) -> Result<&DictionaryArray<T>> {
    165
    0
        Ok(downcast_value!(array, DictionaryArray, T))
    166
    0
    }
    167
    168
    // Downcast Array to GenericBinaryArray
    169
    16
    pub fn as_generic_binary_array<T: OffsetSizeTrait>(
    170
    16
        array: &dyn Array,
    171
    16
    ) -> Result<&GenericBinaryArray<T>> {
    172
    16
        Ok(downcast_value!(array, GenericBinaryArray, T))
    173
    16
    }
    174
    175
    // Downcast Array to GenericListArray
    176
    0
    pub fn as_generic_list_array<T: OffsetSizeTrait>(
    177
    0
        array: &dyn Array,
    178
    0
    ) -> Result<&GenericListArray<T>> {
    179
    0
        Ok(downcast_value!(array, GenericListArray, T))
    180
    0
    }
    181
    182
    // Downcast Array to LargeListArray
    183
    0
    pub fn as_large_list_array(array: &dyn Array) -> Result<&LargeListArray> {
    184
    0
        Ok(downcast_value!(array, LargeListArray))
    185
    0
    }
    186
    187
    // Downcast Array to PrimitiveArray
    188
    0
    pub fn as_primitive_array<T: ArrowPrimitiveType>(
    189
    0
        array: &dyn Array,
    190
    0
    ) -> Result<&PrimitiveArray<T>> {
    191
    0
        Ok(downcast_value!(array, PrimitiveArray, T))
    192
    0
    }
    193
    194
    // Downcast Array to MapArray
    195
    0
    pub fn as_map_array(array: &dyn Array) -> Result<&MapArray> {
    196
    0
        Ok(downcast_value!(array, MapArray))
    197
    0
    }
    198
    199
    // Downcast Array to NullArray
    200
    0
    pub fn as_null_array(array: &dyn Array) -> Result<&NullArray> {
    201
    0
        Ok(downcast_value!(array, NullArray))
    202
    0
    }
    203
    204
    // Downcast Array to NullArray
    205
    0
    pub fn as_union_array(array: &dyn Array) -> Result<&UnionArray> {
    206
    0
        Ok(downcast_value!(array, UnionArray))
    207
    0
    }
    208
    209
    // Downcast Array to Time32SecondArray
    210
    0
    pub fn as_time32_second_array(array: &dyn Array) -> Result<&Time32SecondArray> {
    211
    0
        Ok(downcast_value!(array, Time32SecondArray))
    212
    0
    }
    213
    214
    // Downcast Array to Time32MillisecondArray
    215
    0
    pub fn as_time32_millisecond_array(array: &dyn Array) -> Result<&Time32MillisecondArray> {
    216
    0
        Ok(downcast_value!(array, Time32MillisecondArray))
    217
    0
    }
    218
    219
    // Downcast Array to Time64MicrosecondArray
    220
    0
    pub fn as_time64_microsecond_array(array: &dyn Array) -> Result<&Time64MicrosecondArray> {
    221
    0
        Ok(downcast_value!(array, Time64MicrosecondArray))
    222
    0
    }
    223
    224
    // Downcast Array to Time64NanosecondArray
    225
    0
    pub fn as_time64_nanosecond_array(array: &dyn Array) -> Result<&Time64NanosecondArray> {
    226
    0
        Ok(downcast_value!(array, Time64NanosecondArray))
    227
    0
    }
    228
    229
    // Downcast Array to TimestampNanosecondArray
    230
    0
    pub fn as_timestamp_nanosecond_array(
    231
    0
        array: &dyn Array,
    232
    0
    ) -> Result<&TimestampNanosecondArray> {
    233
    0
        Ok(downcast_value!(array, TimestampNanosecondArray))
    234
    0
    }
    235
    236
    // Downcast Array to TimestampMillisecondArray
    237
    0
    pub fn as_timestamp_millisecond_array(
    238
    0
        array: &dyn Array,
    239
    0
    ) -> Result<&TimestampMillisecondArray> {
    240
    0
        Ok(downcast_value!(array, TimestampMillisecondArray))
    241
    0
    }
    242
    243
    // Downcast Array to TimestampMicrosecondArray
    244
    0
    pub fn as_timestamp_microsecond_array(
    245
    0
        array: &dyn Array,
    246
    0
    ) -> Result<&TimestampMicrosecondArray> {
    247
    0
        Ok(downcast_value!(array, TimestampMicrosecondArray))
    248
    0
    }
    249
    250
    // Downcast Array to TimestampSecondArray
    251
    0
    pub fn as_timestamp_second_array(array: &dyn Array) -> Result<&TimestampSecondArray> {
    252
    0
        Ok(downcast_value!(array, TimestampSecondArray))
    253
    0
    }
    254
    255
    // Downcast Array to IntervalYearMonthArray
    256
    0
    pub fn as_interval_ym_array(array: &dyn Array) -> Result<&IntervalYearMonthArray> {
    257
    0
        Ok(downcast_value!(array, IntervalYearMonthArray))
    258
    0
    }
    259
    260
    // Downcast Array to IntervalDayTimeArray
    261
    0
    pub fn as_interval_dt_array(array: &dyn Array) -> Result<&IntervalDayTimeArray> {
    262
    0
        Ok(downcast_value!(array, IntervalDayTimeArray))
    263
    0
    }
    264
    265
    // Downcast Array to IntervalMonthDayNanoArray
    266
    0
    pub fn as_interval_mdn_array(array: &dyn Array) -> Result<&IntervalMonthDayNanoArray> {
    267
    0
        Ok(downcast_value!(array, IntervalMonthDayNanoArray))
    268
    0
    }
    269
    270
    // Downcast Array to DurationSecondArray
    271
    0
    pub fn as_duration_second_array(array: &dyn Array) -> Result<&DurationSecondArray> {
    272
    0
        Ok(downcast_value!(array, DurationSecondArray))
    273
    0
    }
    274
    275
    // Downcast Array to DurationMillisecondArray
    276
    0
    pub fn as_duration_millisecond_array(
    277
    0
        array: &dyn Array,
    278
    0
    ) -> Result<&DurationMillisecondArray> {
    279
    0
        Ok(downcast_value!(array, DurationMillisecondArray))
    280
    0
    }
    281
    282
    // Downcast Array to DurationMicrosecondArray
    283
    0
    pub fn as_duration_microsecond_array(
    284
    0
        array: &dyn Array,
    285
    0
    ) -> Result<&DurationMicrosecondArray> {
    286
    0
        Ok(downcast_value!(array, DurationMicrosecondArray))
    287
    0
    }
    288
    289
    // Downcast Array to DurationNanosecondArray
    290
    0
    pub fn as_duration_nanosecond_array(
    291
    0
        array: &dyn Array,
    292
    0
    ) -> Result<&DurationNanosecondArray> {
    293
    0
        Ok(downcast_value!(array, DurationNanosecondArray))
    294
    0
    }
    295
    296
    // Downcast Array to BinaryArray
    297
    0
    pub fn as_binary_array(array: &dyn Array) -> Result<&BinaryArray> {
    298
    0
        Ok(downcast_value!(array, BinaryArray))
    299
    0
    }
    300
    301
    // Downcast Array to BinaryViewArray
    302
    16
    pub fn as_binary_view_array(array: &dyn Array) -> Result<&BinaryViewArray> {
    303
    16
        Ok(downcast_value!(array, BinaryViewArray))
    304
    16
    }
    305
    306
    // Downcast Array to LargeBinaryArray
    307
    8
    pub fn as_large_binary_array(array: &dyn Array) -> Result<&LargeBinaryArray> {
    308
    8
        Ok(downcast_value!(array, LargeBinaryArray))
    309
    8
    }
    310
    311
    // Downcast Array to FixedSizeListArray
    312
    0
    pub fn as_fixed_size_list_array(array: &dyn Array) -> Result<&FixedSizeListArray> {
    313
    0
        Ok(downcast_value!(array, FixedSizeListArray))
    314
    0
    }
    315
    316
    // Downcast Array to FixedSizeBinaryArray
    317
    0
    pub fn as_fixed_size_binary_array(array: &dyn Array) -> Result<&FixedSizeBinaryArray> {
    318
    0
        Ok(downcast_value!(array, FixedSizeBinaryArray))
    319
    0
    }
    320
    321
    // Downcast Array to GenericBinaryArray
    322
    0
    pub fn as_generic_string_array<T: OffsetSizeTrait>(
    323
    0
        array: &dyn Array,
    324
    0
    ) -> Result<&GenericStringArray<T>> {
    325
    0
        Ok(downcast_value!(array, GenericStringArray, T))
    326
    0
    }
    327
    328
    // Downcast Array to ListViewArray
    329
    0
    pub fn as_list_view_array(array: &dyn Array) -> Result<&ListViewArray> {
    330
    0
        Ok(downcast_value!(array, ListViewArray))
    331
    0
    }
    332
    333
    // Downcast Array to LargeListViewArray
    334
    0
    pub fn as_large_list_view_array(array: &dyn Array) -> Result<&LargeListViewArray> {
    335
    0
        Ok(downcast_value!(array, LargeListViewArray))
    336
    0
    }