|
| 1 | +// SPDX-License-Identifier: Apache-2.0 |
| 2 | +// SPDX-FileCopyrightText: Copyright the Vortex contributors |
| 3 | + |
| 4 | +use vortex_error::VortexResult; |
| 5 | +use vortex_error::vortex_ensure; |
| 6 | +use vortex_error::vortex_ensure_eq; |
| 7 | + |
| 8 | +use super::super::RowFnExecutionArgs; |
| 9 | +use crate::ArrayRef; |
| 10 | +use crate::ExecutionCtx; |
| 11 | +use crate::IntoArray; |
| 12 | +use crate::arrays::ConstantArray; |
| 13 | +use crate::builtins::ArrayBuiltins; |
| 14 | +use crate::dtype::DType; |
| 15 | +use crate::scalar::Scalar; |
| 16 | +use crate::scalar_fn::ScalarFnId; |
| 17 | + |
| 18 | +impl RowFnExecutionArgs { |
| 19 | + pub(super) fn all_null(&self) -> ArrayRef { |
| 20 | + ConstantArray::new(Scalar::null(self.result_dtype.clone()), self.row_count).into_array() |
| 21 | + } |
| 22 | + |
| 23 | + /// Validate the finished output and apply the row function's logical outer nullability. |
| 24 | + pub(super) fn finalize_output( |
| 25 | + &self, |
| 26 | + values: ArrayRef, |
| 27 | + expected_len: usize, |
| 28 | + ) -> VortexResult<ArrayRef> { |
| 29 | + validate_output(self.id, &self.result_dtype, expected_len, &values)?; |
| 30 | + |
| 31 | + cast_output_nullability(&self.result_dtype, values) |
| 32 | + } |
| 33 | + |
| 34 | + /// Validate the output from a row function before batch validity is attached. |
| 35 | + pub(super) fn validate_kernel_output( |
| 36 | + &self, |
| 37 | + values: ArrayRef, |
| 38 | + expected_len: usize, |
| 39 | + ctx: &mut ExecutionCtx, |
| 40 | + ) -> VortexResult<ArrayRef> { |
| 41 | + finalize_kernel_output(self.id, &self.output_dtype, expected_len, values, ctx) |
| 42 | + } |
| 43 | +} |
| 44 | + |
| 45 | +/// Validate the output produced directly by a row function. |
| 46 | +/// |
| 47 | +/// `values` **must** contain `expected_len` rows. Its dtype must match `result_dtype` except for |
| 48 | +/// outer nullability, and every produced row **must** be valid. Batch execution owns strict null |
| 49 | +/// propagation and attaches input-derived validity only after this boundary. |
| 50 | +pub(crate) fn finalize_kernel_output( |
| 51 | + id: ScalarFnId, |
| 52 | + result_dtype: &DType, |
| 53 | + expected_len: usize, |
| 54 | + values: ArrayRef, |
| 55 | + ctx: &mut ExecutionCtx, |
| 56 | +) -> VortexResult<ArrayRef> { |
| 57 | + validate_output(id, result_dtype, expected_len, &values)?; |
| 58 | + vortex_ensure!( |
| 59 | + values.all_valid(ctx)?, |
| 60 | + "the {id} row kernel must produce only valid rows, got at least one null row", |
| 61 | + ); |
| 62 | + |
| 63 | + cast_output_nullability(result_dtype, values) |
| 64 | +} |
| 65 | + |
| 66 | +/// Validate an output's shape and logical dtype without executing an outer-nullability cast. |
| 67 | +fn validate_output( |
| 68 | + id: ScalarFnId, |
| 69 | + result_dtype: &DType, |
| 70 | + expected_len: usize, |
| 71 | + values: &ArrayRef, |
| 72 | +) -> VortexResult<()> { |
| 73 | + vortex_ensure_eq!( |
| 74 | + values.len(), |
| 75 | + expected_len, |
| 76 | + "the {id} kernel output must contain {expected_len} rows, got {}", |
| 77 | + values.len(), |
| 78 | + ); |
| 79 | + let values_with_result_nullability = |
| 80 | + values.dtype().with_nullability(result_dtype.nullability()); |
| 81 | + vortex_ensure!( |
| 82 | + values_with_result_nullability == *result_dtype, |
| 83 | + "the {id} output dtype must match {result_dtype} except for outer nullability, got {}", |
| 84 | + values.dtype(), |
| 85 | + ); |
| 86 | + |
| 87 | + Ok(()) |
| 88 | +} |
| 89 | + |
| 90 | +/// Cast only the outer output nullability after validation accepts every other dtype component. |
| 91 | +/// |
| 92 | +/// This changes no logical dtype component other than outer nullability. An encoding that cannot |
| 93 | +/// rewrite its nullability directly can still retain a lazy cast until execution. |
| 94 | +fn cast_output_nullability(result_dtype: &DType, values: ArrayRef) -> VortexResult<ArrayRef> { |
| 95 | + if values.dtype() == result_dtype { |
| 96 | + Ok(values) |
| 97 | + } else { |
| 98 | + values.cast(result_dtype.clone()) |
| 99 | + } |
| 100 | +} |
0 commit comments