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
//! Contains the operator [`nullif`].
use crate::array::PrimitiveArray;
use crate::bitmap::Bitmap;
use crate::compute::comparison::{
    primitive_compare_values_op, primitive_compare_values_op_scalar, Simd8, Simd8PartialEq,
};
use crate::datatypes::DataType;
use crate::scalar::PrimitiveScalar;
use crate::scalar::Scalar;
use crate::{array::Array, types::NativeType};

use super::utils::combine_validities;

/// Returns an array whose validity is null iff `lhs == rhs` or `lhs` is null.
/// This has the same semantics as postgres - the validity of the rhs is ignored.
/// # Panic
/// This function panics iff
/// * The arguments do not have the same logical type
/// * The arguments do not have the same length
/// # Example
/// ```rust
/// # use arrow2::array::Int32Array;
/// # use arrow2::datatypes::DataType;
/// # use arrow2::compute::nullif::primitive_nullif;
/// # fn main() {
/// let lhs = Int32Array::from(&[None, None, Some(1), Some(1), Some(1)]);
/// let rhs = Int32Array::from(&[None, Some(1), None, Some(1), Some(0)]);
/// let result = primitive_nullif(&lhs, &rhs);
///
/// let expected = Int32Array::from(&[None, None, Some(1), None, Some(1)]);
///
/// assert_eq!(expected, result);
/// # }
/// ```
pub fn primitive_nullif<T>(lhs: &PrimitiveArray<T>, rhs: &PrimitiveArray<T>) -> PrimitiveArray<T>
where
    T: NativeType + Simd8,
    T::Simd: Simd8PartialEq,
{
    let equal = primitive_compare_values_op(lhs.values(), rhs.values(), |lhs, rhs| lhs.neq(rhs));
    let equal: Option<Bitmap> = equal.into();

    let validity = combine_validities(lhs.validity(), equal.as_ref());

    PrimitiveArray::<T>::new(lhs.data_type().clone(), lhs.values().clone(), validity)
}

/// Returns a [`PrimitiveArray`] whose validity is null iff `lhs == rhs` or `lhs` is null.
///
/// This has the same semantics as postgres.
/// # Panic
/// This function panics iff
/// * The arguments do not have the same logical type
/// # Example
/// ```rust
/// # use arrow2::array::Int32Array;
/// # use arrow2::datatypes::DataType;
/// # use arrow2::compute::nullif::primitive_nullif_scalar;
/// # fn main() {
/// let lhs = Int32Array::from(&[None, None, Some(1), Some(0), Some(1)]);
/// let result = primitive_nullif_scalar(&lhs, 0);
///
/// let expected = Int32Array::from(&[None, None, Some(1), None, Some(1)]);
///
/// assert_eq!(expected, result);
/// # }
/// ```
pub fn primitive_nullif_scalar<T>(lhs: &PrimitiveArray<T>, rhs: T) -> PrimitiveArray<T>
where
    T: NativeType + Simd8,
    T::Simd: Simd8PartialEq,
{
    let equal = primitive_compare_values_op_scalar(lhs.values(), rhs, |lhs, rhs| lhs.neq(rhs));
    let equal: Option<Bitmap> = equal.into();

    let validity = combine_validities(lhs.validity(), equal.as_ref());

    PrimitiveArray::<T>::new(lhs.data_type().clone(), lhs.values().clone(), validity)
}

/// Returns an [`Array`] with the same type as `lhs` and whose validity
/// is null iff either `lhs == rhs` or `lhs` is null.
///
/// This has the same semantics as postgres - the validity of the rhs is ignored.
/// # Panics
/// This function panics iff
/// * The arguments do not have the same logical type
/// * The arguments do not have the same length
/// * The physical type is not supported for this operation (use [`can_nullif`] to check)
/// # Example
/// ```rust
/// # use arrow2::array::Int32Array;
/// # use arrow2::datatypes::DataType;
/// # use arrow2::compute::nullif::nullif;
/// # fn main() {
/// let lhs = Int32Array::from(&[None, None, Some(1), Some(1), Some(1)]);
/// let rhs = Int32Array::from(&[None, Some(1), None, Some(1), Some(0)]);
/// let result = nullif(&lhs, &rhs);
///
/// let expected = Int32Array::from(&[None, None, Some(1), None, Some(1)]);
///
/// assert_eq!(expected, result.as_ref());
/// # }
/// ```
pub fn nullif(lhs: &dyn Array, rhs: &dyn Array) -> Box<dyn Array> {
    assert_eq!(lhs.data_type(), rhs.data_type());
    assert_eq!(lhs.len(), rhs.len());

    use crate::datatypes::PhysicalType::*;
    match lhs.data_type().to_physical_type() {
        Primitive(primitive) => with_match_primitive_type!(primitive, |$T| {
            Box::new(primitive_nullif::<$T>(
                lhs.as_any().downcast_ref().unwrap(),
                rhs.as_any().downcast_ref().unwrap(),
            ))
        }),
        other => unimplemented!("Nullif is not implemented for physical type {:?}", other),
    }
}

/// Returns an [`Array`] with the same type as `lhs` and whose validity
/// is null iff either `lhs == rhs` or `lhs` is null.
/// # Panics
/// iff
/// * Scalar is null
/// * lhs and rhs do not have the same type
/// * The physical type is not supported for this operation (use [`can_nullif`] to check)
/// # Example
/// ```rust
/// # use arrow2::array::Int32Array;
/// # use arrow2::scalar::PrimitiveScalar;
/// # use arrow2::datatypes::DataType;
/// # use arrow2::compute::nullif::nullif_scalar;
/// # fn main() {
/// let lhs = Int32Array::from(&[None, None, Some(1), Some(0), Some(1)]);
/// let rhs = PrimitiveScalar::<i32>::from(Some(0));
/// let result = nullif_scalar(&lhs, &rhs);
///
/// let expected = Int32Array::from(&[None, None, Some(1), None, Some(1)]);
///
/// assert_eq!(expected, result.as_ref());
/// # }
/// ```
pub fn nullif_scalar(lhs: &dyn Array, rhs: &dyn Scalar) -> Box<dyn Array> {
    assert_eq!(lhs.data_type(), rhs.data_type());
    use crate::datatypes::PhysicalType::*;
    match lhs.data_type().to_physical_type() {
        Primitive(primitive) => with_match_primitive_type!(primitive, |$T| {
            let scalar = rhs.as_any().downcast_ref::<PrimitiveScalar<$T>>().unwrap();
            let scalar = scalar.value().expect("Scalar to be non-null");

            Box::new(primitive_nullif_scalar::<$T>(
                lhs.as_any().downcast_ref().unwrap(),
                scalar,
            ))
        }),
        other => unimplemented!("Nullif is not implemented for physical type {:?}", other),
    }
}

/// Returns whether [`nullif`] and [`nullif_scalar`] is implemented for the datatypes.
pub fn can_nullif(lhs: &DataType, rhs: &DataType) -> bool {
    if lhs != rhs {
        return false;
    };
    use crate::datatypes::PhysicalType;
    matches!(lhs.to_physical_type(), PhysicalType::Primitive(_))
}