vortex_array/scalar_fn/builtin/
is_not_null.rs1use std::fmt::Display;
5use std::fmt::Formatter;
6use std::slice;
7
8use vortex_error::VortexExpect as _;
9use vortex_error::VortexResult;
10use vortex_session::VortexSession;
11use vortex_session::registry::CachedId;
12
13use crate::ArrayRef;
14use crate::ExecutionCtx;
15use crate::arrays::ScalarFnArray;
16use crate::dtype::DType;
17use crate::dtype::Nullability;
18use crate::scalar_fn::Arity;
19use crate::scalar_fn::ChildName;
20use crate::scalar_fn::EmptyOptions;
21use crate::scalar_fn::ExecutionArgs;
22use crate::scalar_fn::ReduceNode;
23use crate::scalar_fn::ReduceNodeValidity;
24use crate::scalar_fn::ScalarFnId;
25use crate::scalar_fn::ScalarFnVTable;
26use crate::scalar_fn::ScalarFnVTableExt;
27use crate::scalar_fn::builtin::boolean::Boolean;
28use crate::scalar_fn::builtin::not::Not;
29use crate::scalar_fn::builtin::operators::BooleanOperator;
30use crate::scalar_fn::display::ExprDisplay;
31use crate::scalar_fn::is_not_null_node;
32
33pub fn reduce_null_check<T: ReduceNode>(
56 node: &T,
57 is_null: bool,
58 check: impl Fn(&T) -> VortexResult<T>,
59) -> VortexResult<Option<T>> {
60 let child = node.child(0);
61 if !child.node_dtype()?.is_nullable() {
62 return Ok(Some(node.new_constant((!is_null).into())));
63 }
64
65 if let ReduceNodeValidity::Reduced(validity) = child.validity()? {
66 return Ok(Some(if is_null {
67 not_node(&validity)?
68 } else {
69 validity
70 }));
71 }
72
73 let Some(child_fn) = child.scalar_fn() else {
74 return Ok(None);
75 };
76 let Some(operator) = child_fn.as_opt::<Boolean>() else {
77 return Ok(None);
78 };
79 let is_and = *operator == BooleanOperator::And;
80
81 let left = child.child(0);
82 let right = child.child(1);
83 let (nullable, non_nullable) = match (
84 left.node_dtype()?.is_nullable(),
85 right.node_dtype()?.is_nullable(),
86 ) {
87 (true, false) => (left, right),
88 (false, true) => (right, left),
89 _ => return Ok(None),
91 };
92
93 let left = check(&nullable)?;
96 let right = if is_null == is_and {
99 non_nullable
100 } else {
101 not_node(&non_nullable)?
102 };
103 let combine = if is_null {
104 BooleanOperator::And
105 } else {
106 BooleanOperator::Or
107 };
108 Ok(Some(node.new_node(Boolean.bind(combine), &[left, right])?))
109}
110
111fn not_node<T: ReduceNode>(value: &T) -> VortexResult<T> {
112 value.new_node(Not.bind(EmptyOptions), slice::from_ref(value))
113}
114
115#[derive(Clone)]
117pub struct IsNotNull;
118
119impl IsNotNull {
120 #[expect(clippy::new_ret_no_self, reason = "constructs the lazy result array")]
122 pub fn new(input: ArrayRef) -> ScalarFnArray {
123 ScalarFnArray::try_new(IsNotNull.bind(EmptyOptions), [input])
124 .vortex_expect("IsNotNull has one child and an infallible return dtype")
125 }
126}
127
128impl ScalarFnVTable for IsNotNull {
129 type Options = EmptyOptions;
130
131 fn id(&self) -> ScalarFnId {
132 static ID: CachedId = CachedId::new("vortex.is_not_null");
133 *ID
134 }
135
136 fn serialize(&self, _instance: &Self::Options) -> VortexResult<Option<Vec<u8>>> {
137 Ok(Some(vec![]))
138 }
139
140 fn deserialize(
141 &self,
142 _metadata: &[u8],
143 _session: &VortexSession,
144 ) -> VortexResult<Self::Options> {
145 Ok(EmptyOptions)
146 }
147
148 fn arity(&self, _options: &Self::Options) -> Arity {
149 Arity::Exact(1)
150 }
151
152 fn child_name(&self, _instance: &Self::Options, child_idx: usize) -> ChildName {
153 match child_idx {
154 0 => ChildName::from("input"),
155 _ => unreachable!("Invalid child index {} for IsNotNull expression", child_idx),
156 }
157 }
158
159 fn fmt_sql(
160 &self,
161 _options: &Self::Options,
162 expr: &dyn ExprDisplay,
163 f: &mut Formatter<'_>,
164 ) -> std::fmt::Result {
165 write!(f, "is_not_null(")?;
166 Display::fmt(expr.display_child(0), f)?;
167 write!(f, ")")
168 }
169
170 fn return_dtype(&self, _options: &Self::Options, _arg_dtypes: &[DType]) -> VortexResult<DType> {
171 Ok(DType::Bool(Nullability::NonNullable))
172 }
173
174 fn execute(
175 &self,
176 _data: &Self::Options,
177 args: &dyn ExecutionArgs,
178 _ctx: &mut ExecutionCtx,
179 ) -> VortexResult<ArrayRef> {
180 Ok(args.get(0)?.validity()?.to_array(args.row_count()))
181 }
182
183 fn reduce<T: ReduceNode>(&self, _options: &Self::Options, node: &T) -> VortexResult<Option<T>> {
184 reduce_null_check(node, false, is_not_null_node)
185 }
186
187 fn is_strict(&self, _instance: &Self::Options) -> bool {
188 false
190 }
191
192 fn is_infallible(&self, _instance: &Self::Options) -> bool {
193 true
194 }
195}