vortex_array/compute/
fill_null.rs1use std::sync::LazyLock;
5
6use arcref::ArcRef;
7use vortex_dtype::DType;
8use vortex_error::{VortexError, VortexResult, vortex_bail, vortex_err};
9use vortex_scalar::Scalar;
10
11use crate::compute::{ComputeFn, ComputeFnVTable, InvocationArgs, Kernel, Output, cast};
12use crate::vtable::VTable;
13use crate::{Array, ArrayRef, IntoArray};
14
15static FILL_NULL_FN: LazyLock<ComputeFn> = LazyLock::new(|| {
16 let compute = ComputeFn::new("fill_null".into(), ArcRef::new_ref(&FillNull));
17 for kernel in inventory::iter::<FillNullKernelRef> {
18 compute.register_kernel(kernel.0.clone());
19 }
20 compute
21});
22
23pub fn fill_null(array: &dyn Array, fill_value: &Scalar) -> VortexResult<ArrayRef> {
24 FILL_NULL_FN
25 .invoke(&InvocationArgs {
26 inputs: &[array.into(), fill_value.into()],
27 options: &(),
28 })?
29 .unwrap_array()
30}
31
32pub trait FillNullKernel: VTable {
33 fn fill_null(&self, array: &Self::Array, fill_value: &Scalar) -> VortexResult<ArrayRef>;
34}
35
36pub struct FillNullKernelRef(ArcRef<dyn Kernel>);
37inventory::collect!(FillNullKernelRef);
38
39#[derive(Debug)]
40pub struct FillNullKernelAdapter<V: VTable>(pub V);
41
42impl<V: VTable + FillNullKernel> FillNullKernelAdapter<V> {
43 pub const fn lift(&'static self) -> FillNullKernelRef {
44 FillNullKernelRef(ArcRef::new_ref(self))
45 }
46}
47
48impl<V: VTable + FillNullKernel> Kernel for FillNullKernelAdapter<V> {
49 fn invoke(&self, args: &InvocationArgs) -> VortexResult<Option<Output>> {
50 let inputs = FillNullArgs::try_from(args)?;
51 let Some(array) = inputs.array.as_opt::<V>() else {
52 return Ok(None);
53 };
54 Ok(Some(
55 V::fill_null(&self.0, array, inputs.fill_value)?.into(),
56 ))
57 }
58}
59
60struct FillNull;
61
62impl ComputeFnVTable for FillNull {
63 fn invoke(
64 &self,
65 args: &InvocationArgs,
66 kernels: &[ArcRef<dyn Kernel>],
67 ) -> VortexResult<Output> {
68 let FillNullArgs { array, fill_value } = FillNullArgs::try_from(args)?;
69
70 if !array.dtype().is_nullable() || array.all_valid() {
71 return Ok(cast(array, fill_value.dtype())?.into());
72 }
73
74 if fill_value.is_null() {
75 vortex_bail!("Cannot fill_null with a null value")
76 }
77
78 for kernel in kernels {
79 if let Some(output) = kernel.invoke(args)? {
80 return Ok(output);
81 }
82 }
83 if let Some(output) = array.invoke(&FILL_NULL_FN, args)? {
84 return Ok(output);
85 }
86
87 log::debug!("FillNullFn not implemented for {}", array.encoding_id());
88 if !array.is_canonical() {
89 let canonical_arr = array.to_canonical().into_array();
90 return Ok(fill_null(canonical_arr.as_ref(), fill_value)?.into());
91 }
92
93 vortex_bail!("fill null not implemented for DType {}", array.dtype())
94 }
95
96 fn return_dtype(&self, args: &InvocationArgs) -> VortexResult<DType> {
97 let FillNullArgs { array, fill_value } = FillNullArgs::try_from(args)?;
98 if !array.dtype().eq_ignore_nullability(fill_value.dtype()) {
99 vortex_bail!("FillNull value must match array type (ignoring nullability)");
100 }
101 Ok(fill_value.dtype().clone())
102 }
103
104 fn return_len(&self, args: &InvocationArgs) -> VortexResult<usize> {
105 let FillNullArgs { array, .. } = FillNullArgs::try_from(args)?;
106 Ok(array.len())
107 }
108
109 fn is_elementwise(&self) -> bool {
110 true
111 }
112}
113
114struct FillNullArgs<'a> {
115 array: &'a dyn Array,
116 fill_value: &'a Scalar,
117}
118
119impl<'a> TryFrom<&InvocationArgs<'a>> for FillNullArgs<'a> {
120 type Error = VortexError;
121
122 fn try_from(value: &InvocationArgs<'a>) -> Result<Self, Self::Error> {
123 if value.inputs.len() != 2 {
124 vortex_bail!("FillNull requires 2 arguments");
125 }
126
127 let array = value.inputs[0]
128 .array()
129 .ok_or_else(|| vortex_err!("FillNull requires an array"))?;
130 let fill_value = value.inputs[1]
131 .scalar()
132 .ok_or_else(|| vortex_err!("FillNull requires a scalar"))?;
133
134 Ok(FillNullArgs { array, fill_value })
135 }
136}