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
use vortex_error::{vortex_err, VortexError, VortexResult};

use crate::encoding::Encoding;
use crate::{ArrayDType, ArrayData};

/// Trait for filling forward on an array, i.e., replacing nulls with the last non-null value.
///
/// If the array is non-nullable, it is returned as-is.
/// If the array is entirely nulls, the fill forward operation returns an array of the same length, filled with the default value of the array's type.
/// The DType of the returned array is the same as the input array; the Validity of the returned array is always either NonNullable or AllValid.
pub trait FillForwardFn<Array> {
    fn fill_forward(&self, array: &Array) -> VortexResult<ArrayData>;
}

impl<E: Encoding> FillForwardFn<ArrayData> for E
where
    E: FillForwardFn<E::Array>,
    for<'a> &'a E::Array: TryFrom<&'a ArrayData, Error = VortexError>,
{
    fn fill_forward(&self, array: &ArrayData) -> VortexResult<ArrayData> {
        let array_ref = <&E::Array>::try_from(array)?;
        let encoding = array
            .encoding()
            .as_any()
            .downcast_ref::<E>()
            .ok_or_else(|| vortex_err!("Mismatched encoding"))?;
        FillForwardFn::fill_forward(encoding, array_ref)
    }
}

pub fn fill_forward(array: impl AsRef<ArrayData>) -> VortexResult<ArrayData> {
    let array = array.as_ref();
    if !array.dtype().is_nullable() {
        return Ok(array.clone());
    }

    let filled = array
        .encoding()
        .fill_forward_fn()
        .map(|f| f.fill_forward(array))
        .unwrap_or_else(|| {
            Err(vortex_err!(
                NotImplemented: "fill_forward",
                array.encoding().id()
            ))
        })?;

    debug_assert_eq!(
        filled.len(),
        array.len(),
        "FillForward length mismatch {}",
        array.encoding().id()
    );
    debug_assert_eq!(
        filled.dtype(),
        array.dtype(),
        "FillForward dtype mismatch {}",
        array.encoding().id()
    );

    Ok(filled)
}