## Coverage Report

#### Created: 2024-10-13 08:39

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

    Line
    Count
    Source (jump to first uncovered line)
    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, DataFusionError, Result};
    24
    use arrow::{
    25
        array::{
    26
            Array, BinaryArray, BooleanArray, Date32Array, Date64Array, Decimal128Array,
    27
            Decimal256Array, DictionaryArray, FixedSizeBinaryArray, FixedSizeListArray,
    28
            Float32Array, Float64Array, GenericBinaryArray, GenericListArray,
    29
            GenericStringArray, Int32Array, Int64Array, IntervalDayTimeArray,
    30
            IntervalMonthDayNanoArray, IntervalYearMonthArray, LargeListArray, ListArray,
    31
            MapArray, NullArray, OffsetSizeTrait, PrimitiveArray, StringArray, StructArray,
    32
            Time32MillisecondArray, Time32SecondArray, Time64MicrosecondArray,
    33
            Time64NanosecondArray, TimestampMicrosecondArray, TimestampMillisecondArray,
    34
            TimestampNanosecondArray, TimestampSecondArray, UInt32Array, UInt64Array,
    35
            UInt8Array, UnionArray,
    36
        },
    37
        datatypes::{ArrowDictionaryKeyType, ArrowPrimitiveType},
    38
    };
    39
    use arrow_array::{BinaryViewArray, StringViewArray};
    40
    41
    // Downcast ArrayRef to Date32Array
    42
    0
    pub fn as_date32_array(array: &dyn Array) -> Result<&Date32Array> {
    43
    0
        Ok(downcast_value!(array, Date32Array))
    44
    0
    }
    45
    46
    // Downcast ArrayRef to StructArray
    47
    7
    pub fn as_struct_array(array: &dyn Array) -> Result<&StructArray> {
    48
    7
        Ok(downcast_value!(array, StructArray))
    49
    7
    }
    50
    51
    // Downcast ArrayRef to UInt8Array
    52
    0
    pub fn as_uint8_array(array: &dyn Array) -> Result<&UInt8Array> {
    53
    0
        Ok(downcast_value!(array, UInt8Array))
    54
    0
    }
    55
    56
    // Downcast ArrayRef to Int32Array
    57
    0
    pub fn as_int32_array(array: &dyn Array) -> Result<&Int32Array> {
    58
    0
        Ok(downcast_value!(array, Int32Array))
    59
    0
    }
    60
    61
    // Downcast ArrayRef to Int64Array
    62
    0
    pub fn as_int64_array(array: &dyn Array) -> Result<&Int64Array> {
    63
    0
        Ok(downcast_value!(array, Int64Array))
    64
    0
    }
    65
    66
    // Downcast ArrayRef to Decimal128Array
    67
    0
    pub fn as_decimal128_array(array: &dyn Array) -> Result<&Decimal128Array> {
    68
    0
        Ok(downcast_value!(array, Decimal128Array))
    69
    0
    }
    70
    71
    // Downcast ArrayRef to Decimal256Array
    72
    0
    pub fn as_decimal256_array(array: &dyn Array) -> Result<&Decimal256Array> {
    73
    0
        Ok(downcast_value!(array, Decimal256Array))
    74
    0
    }
    75
    76
    // Downcast ArrayRef to Float32Array
    77
    0
    pub fn as_float32_array(array: &dyn Array) -> Result<&Float32Array> {
    78
    0
        Ok(downcast_value!(array, Float32Array))
    79
    0
    }
    80
    81
    // Downcast ArrayRef to Float64Array
    82
    0
    pub fn as_float64_array(array: &dyn Array) -> Result<&Float64Array> {
    83
    0
        Ok(downcast_value!(array, Float64Array))
    84
    0
    }
    85
    86
    // Downcast ArrayRef to StringArray
    87
    14
    pub fn as_string_array(array: &dyn Array) -> Result<&StringArray> {
    88
    14
        Ok(downcast_value!(array, StringArray))
    89
    14
    }
    90
    91
    // Downcast ArrayRef to StringViewArray
    92
    0
    pub fn as_string_view_array(array: &dyn Array) -> Result<&StringViewArray> {
    93
    0
        Ok(downcast_value!(array, StringViewArray))
    94
    0
    }
    95
    96
    // Downcast ArrayRef to UInt32Array
    97
    0
    pub fn as_uint32_array(array: &dyn Array) -> Result<&UInt32Array> {
    98
    0
        Ok(downcast_value!(array, UInt32Array))
    99
    0
    }
    100
    101
    // Downcast ArrayRef to UInt64Array
    102
    0
    pub fn as_uint64_array(array: &dyn Array) -> Result<&UInt64Array> {
    103
    0
        Ok(downcast_value!(array, UInt64Array))
    104
    0
    }
    105
    106
    // Downcast ArrayRef to BooleanArray
    107
    66.4k
    pub fn as_boolean_array(array: &dyn Array) -> Result<&BooleanArray> {
    108
    66.4k
        Ok(downcast_value!(array, BooleanArray))
    109
    66.4k
    }
    110
    111
    // Downcast ArrayRef to ListArray
    112
    0
    pub fn as_list_array(array: &dyn Array) -> Result<&ListArray> {
    113
    0
        Ok(downcast_value!(array, ListArray))
    114
    0
    }
    115
    116
    // Downcast ArrayRef to DictionaryArray
    117
    0
    pub fn as_dictionary_array<T: ArrowDictionaryKeyType>(
    118
    0
        array: &dyn Array,
    119
    0
    ) -> Result<&DictionaryArray<T>> {
    120
    0
        Ok(downcast_value!(array, DictionaryArray, T))
    121
    0
    }
    122
    123
    // Downcast ArrayRef to GenericBinaryArray
    124
    0
    pub fn as_generic_binary_array<T: OffsetSizeTrait>(
    125
    0
        array: &dyn Array,
    126
    0
    ) -> Result<&GenericBinaryArray<T>> {
    127
    0
        Ok(downcast_value!(array, GenericBinaryArray, T))
    128
    0
    }
    129
    130
    // Downcast ArrayRef to GenericListArray
    131
    0
    pub fn as_generic_list_array<T: OffsetSizeTrait>(
    132
    0
        array: &dyn Array,
    133
    0
    ) -> Result<&GenericListArray<T>> {
    134
    0
        Ok(downcast_value!(array, GenericListArray, T))
    135
    0
    }
    136
    137
    // Downcast ArrayRef to LargeListArray
    138
    0
    pub fn as_large_list_array(array: &dyn Array) -> Result<&LargeListArray> {
    139
    0
        Ok(downcast_value!(array, LargeListArray))
    140
    0
    }
    141
    142
    // Downcast ArrayRef to PrimitiveArray
    143
    3
    pub fn as_primitive_array<T: ArrowPrimitiveType>(
    144
    3
        array: &dyn Array,
    145
    3
    ) -> Result<&PrimitiveArray<T>> {
    146
    3
        Ok(downcast_value!(array, PrimitiveArray, T))
    147
    3
    }
    148
    149
    // Downcast ArrayRef to MapArray
    150
    0
    pub fn as_map_array(array: &dyn Array) -> Result<&MapArray> {
    151
    0
        Ok(downcast_value!(array, MapArray))
    152
    0
    }
    153
    154
    // Downcast ArrayRef to NullArray
    155
    0
    pub fn as_null_array(array: &dyn Array) -> Result<&NullArray> {
    156
    0
        Ok(downcast_value!(array, NullArray))
    157
    0
    }
    158
    159
    // Downcast ArrayRef to NullArray
    160
    0
    pub fn as_union_array(array: &dyn Array) -> Result<&UnionArray> {
    161
    0
        Ok(downcast_value!(array, UnionArray))
    162
    0
    }
    163
    164
    // Downcast ArrayRef to Time32SecondArray
    165
    0
    pub fn as_time32_second_array(array: &dyn Array) -> Result<&Time32SecondArray> {
    166
    0
        Ok(downcast_value!(array, Time32SecondArray))
    167
    0
    }
    168
    169
    // Downcast ArrayRef to Time32MillisecondArray
    170
    0
    pub fn as_time32_millisecond_array(array: &dyn Array) -> Result<&Time32MillisecondArray> {
    171
    0
        Ok(downcast_value!(array, Time32MillisecondArray))
    172
    0
    }
    173
    174
    // Downcast ArrayRef to Time64MicrosecondArray
    175
    0
    pub fn as_time64_microsecond_array(array: &dyn Array) -> Result<&Time64MicrosecondArray> {
    176
    0
        Ok(downcast_value!(array, Time64MicrosecondArray))
    177
    0
    }
    178
    179
    // Downcast ArrayRef to Time64NanosecondArray
    180
    0
    pub fn as_time64_nanosecond_array(array: &dyn Array) -> Result<&Time64NanosecondArray> {
    181
    0
        Ok(downcast_value!(array, Time64NanosecondArray))
    182
    0
    }
    183
    184
    // Downcast ArrayRef to TimestampNanosecondArray
    185
    0
    pub fn as_timestamp_nanosecond_array(
    186
    0
        array: &dyn Array,
    187
    0
    ) -> Result<&TimestampNanosecondArray> {
    188
    0
        Ok(downcast_value!(array, TimestampNanosecondArray))
    189
    0
    }
    190
    191
    // Downcast ArrayRef to TimestampMillisecondArray
    192
    0
    pub fn as_timestamp_millisecond_array(
    193
    0
        array: &dyn Array,
    194
    0
    ) -> Result<&TimestampMillisecondArray> {
    195
    0
        Ok(downcast_value!(array, TimestampMillisecondArray))
    196
    0
    }
    197
    198
    // Downcast ArrayRef to TimestampMicrosecondArray
    199
    0
    pub fn as_timestamp_microsecond_array(
    200
    0
        array: &dyn Array,
    201
    0
    ) -> Result<&TimestampMicrosecondArray> {
    202
    0
        Ok(downcast_value!(array, TimestampMicrosecondArray))
    203
    0
    }
    204
    205
    // Downcast ArrayRef to TimestampSecondArray
    206
    0
    pub fn as_timestamp_second_array(array: &dyn Array) -> Result<&TimestampSecondArray> {
    207
    0
        Ok(downcast_value!(array, TimestampSecondArray))
    208
    0
    }
    209
    210
    // Downcast ArrayRef to IntervalYearMonthArray
    211
    0
    pub fn as_interval_ym_array(array: &dyn Array) -> Result<&IntervalYearMonthArray> {
    212
    0
        Ok(downcast_value!(array, IntervalYearMonthArray))
    213
    0
    }
    214
    215
    // Downcast ArrayRef to IntervalDayTimeArray
    216
    0
    pub fn as_interval_dt_array(array: &dyn Array) -> Result<&IntervalDayTimeArray> {
    217
    0
        Ok(downcast_value!(array, IntervalDayTimeArray))
    218
    0
    }
    219
    220
    // Downcast ArrayRef to IntervalMonthDayNanoArray
    221
    0
    pub fn as_interval_mdn_array(array: &dyn Array) -> Result<&IntervalMonthDayNanoArray> {
    222
    0
        Ok(downcast_value!(array, IntervalMonthDayNanoArray))
    223
    0
    }
    224
    225
    // Downcast ArrayRef to BinaryArray
    226
    0
    pub fn as_binary_array(array: &dyn Array) -> Result<&BinaryArray> {
    227
    0
        Ok(downcast_value!(array, BinaryArray))
    228
    0
    }
    229
    230
    // Downcast ArrayRef to BinaryViewArray
    231
    0
    pub fn as_binary_view_array(array: &dyn Array) -> Result<&BinaryViewArray> {
    232
    0
        Ok(downcast_value!(array, BinaryViewArray))
    233
    0
    }
    234
    235
    // Downcast ArrayRef to FixedSizeListArray
    236
    0
    pub fn as_fixed_size_list_array(array: &dyn Array) -> Result<&FixedSizeListArray> {
    237
    0
        Ok(downcast_value!(array, FixedSizeListArray))
    238
    0
    }
    239
    240
    // Downcast ArrayRef to FixedSizeListArray
    241
    0
    pub fn as_fixed_size_binary_array(array: &dyn Array) -> Result<&FixedSizeBinaryArray> {
    242
    0
        Ok(downcast_value!(array, FixedSizeBinaryArray))
    243
    0
    }
    244
    245
    // Downcast ArrayRef to Date64Array
    246
    0
    pub fn as_date64_array(array: &dyn Array) -> Result<&Date64Array> {
    247
    0
        Ok(downcast_value!(array, Date64Array))
    248
    0
    }
    249
    250
    // Downcast ArrayRef to GenericBinaryArray
    251
    0
    pub fn as_generic_string_array<T: OffsetSizeTrait>(
    252
    0
        array: &dyn Array,
    253
    0
    ) -> Result<&GenericStringArray<T>> {
    254
    0
        Ok(downcast_value!(array, GenericStringArray, T))
    255
    0
    }