Skip to main content

vortex_fsst/compute/
cast.rs

1// SPDX-License-Identifier: Apache-2.0
2// SPDX-FileCopyrightText: Copyright the Vortex contributors
3
4use vortex_array::ArrayRef;
5use vortex_array::IntoArray;
6use vortex_array::arrays::VarBinVTable;
7use vortex_array::builtins::ArrayBuiltins;
8use vortex_array::compute::CastReduce;
9use vortex_dtype::DType;
10use vortex_error::VortexResult;
11
12use crate::FSSTArray;
13use crate::FSSTVTable;
14
15impl CastReduce for FSSTVTable {
16    fn cast(array: &FSSTArray, dtype: &DType) -> VortexResult<Option<ArrayRef>> {
17        // FSST is a string compression encoding.
18        // For nullability changes, we can cast the codes and symbols arrays
19        if array.dtype().eq_ignore_nullability(dtype) {
20            // Cast codes array to handle nullability
21            let new_codes = array
22                .codes()
23                .to_array()
24                .cast(array.codes().dtype().with_nullability(dtype.nullability()))?;
25
26            Ok(Some(
27                FSSTArray::try_new(
28                    dtype.clone(),
29                    array.symbols().clone(),
30                    array.symbol_lengths().clone(),
31                    new_codes.as_::<VarBinVTable>().clone(),
32                    array.uncompressed_lengths().clone(),
33                )?
34                .into_array(),
35            ))
36        } else {
37            Ok(None)
38        }
39    }
40}
41
42#[cfg(test)]
43mod tests {
44    use rstest::rstest;
45    use vortex_array::arrays::VarBinArray;
46    use vortex_array::builtins::ArrayBuiltins;
47    use vortex_array::compute::conformance::cast::test_cast_conformance;
48    use vortex_dtype::DType;
49    use vortex_dtype::Nullability;
50
51    use crate::fsst_compress;
52    use crate::fsst_train_compressor;
53
54    #[test]
55    fn test_cast_fsst_nullability() {
56        let strings = VarBinArray::from_iter(
57            vec![Some("hello"), Some("world"), Some("hello world")],
58            DType::Utf8(Nullability::NonNullable),
59        );
60
61        let compressor = fsst_train_compressor(&strings);
62        let fsst = fsst_compress(strings, &compressor);
63
64        // Cast to nullable
65        let casted = fsst
66            .to_array()
67            .cast(DType::Utf8(Nullability::Nullable))
68            .unwrap();
69        assert_eq!(casted.dtype(), &DType::Utf8(Nullability::Nullable));
70    }
71
72    #[rstest]
73    #[case(VarBinArray::from_iter(
74        vec![Some("hello"), Some("world"), Some("hello world")],
75        DType::Utf8(Nullability::NonNullable)
76    ))]
77    #[case(VarBinArray::from_iter(
78        vec![Some("foo"), None, Some("bar"), Some("foobar")],
79        DType::Utf8(Nullability::Nullable)
80    ))]
81    #[case(VarBinArray::from_iter(
82        vec![Some("test")],
83        DType::Utf8(Nullability::NonNullable)
84    ))]
85    fn test_cast_fsst_conformance(#[case] array: VarBinArray) {
86        let compressor = fsst_train_compressor(&array);
87        let fsst = fsst_compress(&array, &compressor);
88        test_cast_conformance(fsst.as_ref());
89    }
90}