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
use std::fmt::{Debug, Formatter};

use super::Scalar;
use crate::array::ViewType;
use crate::datatypes::ArrowDataType;

/// The implementation of [`Scalar`] for utf8, semantically equivalent to [`Option<String>`].
#[derive(PartialEq, Eq)]
pub struct BinaryViewScalar<T: ViewType + ?Sized> {
    value: Option<T::Owned>,
    phantom: std::marker::PhantomData<T>,
}

impl<T: ViewType + ?Sized> Debug for BinaryViewScalar<T> {
    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
        write!(f, "Scalar({:?})", self.value)
    }
}

impl<T: ViewType + ?Sized> Clone for BinaryViewScalar<T> {
    fn clone(&self) -> Self {
        Self {
            value: self.value.clone(),
            phantom: Default::default(),
        }
    }
}

impl<T: ViewType + ?Sized> BinaryViewScalar<T> {
    /// Returns a new [`BinaryViewScalar`]
    #[inline]
    pub fn new(value: Option<&T>) -> Self {
        Self {
            value: value.map(|x| x.into_owned()),
            phantom: std::marker::PhantomData,
        }
    }

    /// Returns the value irrespectively of the validity.
    #[inline]
    pub fn value(&self) -> Option<&T> {
        self.value.as_ref().map(|x| x.as_ref())
    }
}

impl<T: ViewType + ?Sized> From<Option<&T>> for BinaryViewScalar<T> {
    #[inline]
    fn from(v: Option<&T>) -> Self {
        Self::new(v)
    }
}

impl<T: ViewType + ?Sized> Scalar for BinaryViewScalar<T> {
    #[inline]
    fn as_any(&self) -> &dyn std::any::Any {
        self
    }

    #[inline]
    fn is_valid(&self) -> bool {
        self.value.is_some()
    }

    #[inline]
    fn data_type(&self) -> &ArrowDataType {
        if T::IS_UTF8 {
            &ArrowDataType::Utf8View
        } else {
            &ArrowDataType::BinaryView
        }
    }
}