|
| 1 | +// SPDX-License-Identifier: Apache-2.0 |
| 2 | +// SPDX-FileCopyrightText: Copyright the Vortex contributors |
| 3 | + |
| 4 | +use std::cell::Cell; |
| 5 | +use std::slice; |
| 6 | +use std::sync::LazyLock; |
| 7 | + |
| 8 | +use rstest::rstest; |
| 9 | +use vortex_buffer::buffer; |
| 10 | +use vortex_error::VortexResult; |
| 11 | +use vortex_error::vortex_err; |
| 12 | +use vortex_session::VortexSession; |
| 13 | + |
| 14 | +use super::ExpectedOutcome; |
| 15 | +use super::assert_execution_paths; |
| 16 | +use crate::ArrayRef; |
| 17 | +use crate::IntoArray; |
| 18 | +use crate::VortexSessionExecute; |
| 19 | +use crate::arrays::BoolArray; |
| 20 | +use crate::arrays::Constant; |
| 21 | +use crate::arrays::PrimitiveArray; |
| 22 | +use crate::arrays::StructArray; |
| 23 | +use crate::builtins::ArrayBuiltins; |
| 24 | +use crate::dtype::DType; |
| 25 | +use crate::dtype::Nullability; |
| 26 | +use crate::dtype::PType; |
| 27 | +use crate::scalar_fn::fns::operators::Operator; |
| 28 | + |
| 29 | +static SESSION: LazyLock<VortexSession> = LazyLock::new(VortexSession::empty); |
| 30 | + |
| 31 | +#[track_caller] |
| 32 | +fn assert_cast_paths(input: ArrayRef, expected: ArrayRef) { |
| 33 | + let dtype = expected.dtype().clone(); |
| 34 | + assert_execution_paths( |
| 35 | + &[input], |
| 36 | + |inputs| inputs[0].cast(dtype.clone()), |
| 37 | + ExpectedOutcome::Array(expected), |
| 38 | + &mut SESSION.create_execution_ctx(), |
| 39 | + ); |
| 40 | +} |
| 41 | + |
| 42 | +#[rstest] |
| 43 | +#[case::non_nullable(buffer![1i32, 2, 3].into_array(), buffer![1i64, 2, 3].into_array())] |
| 44 | +#[case::partially_valid( |
| 45 | + PrimitiveArray::from_option_iter([Some(1i32), None, Some(3)]).into_array(), |
| 46 | + PrimitiveArray::from_option_iter([Some(1i64), None, Some(3)]).into_array() |
| 47 | +)] |
| 48 | +#[case::empty( |
| 49 | + PrimitiveArray::empty::<i32>(Nullability::NonNullable).into_array(), |
| 50 | + PrimitiveArray::empty::<i64>(Nullability::NonNullable).into_array() |
| 51 | +)] |
| 52 | +fn test_cast_paths(#[case] input: ArrayRef, #[case] expected: ArrayRef) { |
| 53 | + assert_cast_paths(input, expected); |
| 54 | +} |
| 55 | + |
| 56 | +#[test] |
| 57 | +fn test_nested_cast_paths() -> VortexResult<()> { |
| 58 | + let input = StructArray::from_fields(&[("value", buffer![1i32, 2, 3].into_array())])?; |
| 59 | + let expected = StructArray::from_fields(&[("value", buffer![1i64, 2, 3].into_array())])?; |
| 60 | + |
| 61 | + assert_cast_paths(input.into_array(), expected.into_array()); |
| 62 | + Ok(()) |
| 63 | +} |
| 64 | + |
| 65 | +#[test] |
| 66 | +fn test_binary_paths() { |
| 67 | + assert_execution_paths( |
| 68 | + &[ |
| 69 | + buffer![1i32, 3, 2].into_array(), |
| 70 | + buffer![2i32, 1, 2].into_array(), |
| 71 | + ], |
| 72 | + |inputs| inputs[0].binary(inputs[1].clone(), Operator::Lt), |
| 73 | + ExpectedOutcome::Array(BoolArray::from_iter([true, false, false]).into_array()), |
| 74 | + &mut SESSION.create_execution_ctx(), |
| 75 | + ); |
| 76 | +} |
| 77 | + |
| 78 | +#[test] |
| 79 | +fn test_construction_errors() { |
| 80 | + assert_execution_paths( |
| 81 | + &[buffer![1i32, 2].into_array()], |
| 82 | + |_inputs| Err(vortex_err!("operation is not supported")), |
| 83 | + ExpectedOutcome::Error, |
| 84 | + &mut SESSION.create_execution_ctx(), |
| 85 | + ); |
| 86 | +} |
| 87 | + |
| 88 | +#[test] |
| 89 | +fn test_execution_errors() { |
| 90 | + assert_execution_paths( |
| 91 | + &[buffer![1000i32, 2000].into_array()], |
| 92 | + |inputs| inputs[0].cast(DType::Primitive(PType::I8, Nullability::NonNullable)), |
| 93 | + ExpectedOutcome::Error, |
| 94 | + &mut SESSION.create_execution_ctx(), |
| 95 | + ); |
| 96 | +} |
| 97 | + |
| 98 | +#[test] |
| 99 | +fn test_empty_inputs_only_run_batch() { |
| 100 | + let calls = Cell::new(0); |
| 101 | + let input = PrimitiveArray::empty::<i32>(Nullability::NonNullable).into_array(); |
| 102 | + |
| 103 | + assert_execution_paths( |
| 104 | + slice::from_ref(&input), |
| 105 | + |inputs| { |
| 106 | + calls.set(calls.get() + 1); |
| 107 | + Ok(inputs[0].clone()) |
| 108 | + }, |
| 109 | + ExpectedOutcome::Array(input.clone()), |
| 110 | + &mut SESSION.create_execution_ctx(), |
| 111 | + ); |
| 112 | + assert_eq!(calls.get(), 1); |
| 113 | +} |
| 114 | + |
| 115 | +#[rstest] |
| 116 | +#[case::value(buffer![1i64, 3].into_array())] |
| 117 | +#[case::null(PrimitiveArray::from_option_iter([Some(1i64), None]).into_array())] |
| 118 | +#[should_panic(expected = "arrays do not match")] |
| 119 | +fn test_rejects_incorrect_expected_values(#[case] expected: ArrayRef) { |
| 120 | + assert_cast_paths(buffer![1i32, 2].into_array(), expected); |
| 121 | +} |
| 122 | + |
| 123 | +#[test] |
| 124 | +#[should_panic(expected = "arrays differ in type")] |
| 125 | +fn test_rejects_incorrect_expected_dtype() { |
| 126 | + assert_execution_paths( |
| 127 | + &[buffer![1i32].into_array()], |
| 128 | + |inputs| Ok(inputs[0].clone()), |
| 129 | + ExpectedOutcome::Array(buffer![1i64].into_array()), |
| 130 | + &mut SESSION.create_execution_ctx(), |
| 131 | + ); |
| 132 | +} |
| 133 | + |
| 134 | +#[test] |
| 135 | +#[should_panic(expected = "row construction path failed")] |
| 136 | +fn test_rejects_mixed_success_and_error() { |
| 137 | + let calls = Cell::new(0); |
| 138 | + assert_execution_paths( |
| 139 | + &[buffer![1i32, 2].into_array()], |
| 140 | + |inputs| { |
| 141 | + let call = calls.get(); |
| 142 | + calls.set(call + 1); |
| 143 | + if call == 1 { |
| 144 | + return Err(vortex_err!("row construction failed")); |
| 145 | + } |
| 146 | + |
| 147 | + Ok(inputs[0].clone()) |
| 148 | + }, |
| 149 | + ExpectedOutcome::Array(buffer![1i32, 2].into_array()), |
| 150 | + &mut SESSION.create_execution_ctx(), |
| 151 | + ); |
| 152 | +} |
| 153 | + |
| 154 | +#[test] |
| 155 | +#[should_panic(expected = "arrays do not match")] |
| 156 | +fn test_rejects_divergent_row_values() { |
| 157 | + let calls = Cell::new(0); |
| 158 | + assert_execution_paths( |
| 159 | + &[buffer![1i32, 2].into_array()], |
| 160 | + |inputs| { |
| 161 | + let call = calls.get(); |
| 162 | + calls.set(call + 1); |
| 163 | + if call == 1 { |
| 164 | + return Ok(buffer![1i32, 3].into_array()); |
| 165 | + } |
| 166 | + |
| 167 | + Ok(inputs[0].clone()) |
| 168 | + }, |
| 169 | + ExpectedOutcome::Array(buffer![1i32, 2].into_array()), |
| 170 | + &mut SESSION.create_execution_ctx(), |
| 171 | + ); |
| 172 | +} |
| 173 | + |
| 174 | +#[test] |
| 175 | +#[should_panic(expected = "arrays do not match")] |
| 176 | +fn test_rejects_divergent_constant_values() { |
| 177 | + assert_execution_paths( |
| 178 | + &[buffer![1i32, 2].into_array()], |
| 179 | + |inputs| { |
| 180 | + if inputs[0].is::<Constant>() { |
| 181 | + return Ok(buffer![9i32].into_array()); |
| 182 | + } |
| 183 | + |
| 184 | + Ok(inputs[0].clone()) |
| 185 | + }, |
| 186 | + ExpectedOutcome::Array(buffer![1i32, 2].into_array()), |
| 187 | + &mut SESSION.create_execution_ctx(), |
| 188 | + ); |
| 189 | +} |
| 190 | + |
| 191 | +#[test] |
| 192 | +#[should_panic(expected = "batch path must return an error")] |
| 193 | +fn test_rejects_success_when_error_expected() { |
| 194 | + assert_execution_paths( |
| 195 | + &[buffer![1i32].into_array()], |
| 196 | + |inputs| Ok(inputs[0].clone()), |
| 197 | + ExpectedOutcome::Error, |
| 198 | + &mut SESSION.create_execution_ctx(), |
| 199 | + ); |
| 200 | +} |
0 commit comments