|
| 1 | +// Licensed to the Apache Software Foundation (ASF) under one |
| 2 | +// or more contributor license agreements. See the NOTICE file |
| 3 | +// distributed with this work for additional information |
| 4 | +// regarding copyright ownership. The ASF licenses this file |
| 5 | +// to you under the Apache License, Version 2.0 (the |
| 6 | +// "License"); you may not use this file except in compliance |
| 7 | +// with the License. You may obtain a copy of the License at |
| 8 | +// |
| 9 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +// |
| 11 | +// Unless required by applicable law or agreed to in writing, |
| 12 | +// software distributed under the License is distributed on an |
| 13 | +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 14 | +// KIND, either express or implied. See the License for the |
| 15 | +// specific language governing permissions and limitations |
| 16 | +// under the License. |
| 17 | + |
| 18 | +use arrow::array::Array; |
| 19 | +use arrow::buffer::NullBuffer; |
| 20 | +use arrow::datatypes::DataType; |
| 21 | +use datafusion_common::{Result, ScalarValue}; |
| 22 | +use datafusion_expr::ColumnarValue; |
| 23 | +use std::sync::Arc; |
| 24 | + |
| 25 | +pub(crate) enum NullMaskResolution { |
| 26 | + /// Return NULL as the result (e.g., scalar inputs with at least one NULL) |
| 27 | + ReturnNull, |
| 28 | + /// No null mask needed (e.g., all scalar inputs are non-NULL) |
| 29 | + NoMask, |
| 30 | + /// Null mask to apply for arrays |
| 31 | + Apply(NullBuffer), |
| 32 | +} |
| 33 | + |
| 34 | +/// Compute NULL mask for the arguments using NullBuffer::union |
| 35 | +pub(crate) fn compute_null_mask( |
| 36 | + args: &[ColumnarValue], |
| 37 | + number_rows: usize, |
| 38 | +) -> Result<NullMaskResolution> { |
| 39 | + // Check if all arguments are scalars |
| 40 | + let all_scalars = args |
| 41 | + .iter() |
| 42 | + .all(|arg| matches!(arg, ColumnarValue::Scalar(_))); |
| 43 | + |
| 44 | + if all_scalars { |
| 45 | + // For scalars, check if any is NULL |
| 46 | + for arg in args { |
| 47 | + if let ColumnarValue::Scalar(scalar) = arg |
| 48 | + && scalar.is_null() |
| 49 | + { |
| 50 | + return Ok(NullMaskResolution::ReturnNull); |
| 51 | + } |
| 52 | + } |
| 53 | + // No NULLs in scalars |
| 54 | + Ok(NullMaskResolution::NoMask) |
| 55 | + } else { |
| 56 | + // For arrays, compute NULL mask for each row using NullBuffer::union |
| 57 | + let array_len = args |
| 58 | + .iter() |
| 59 | + .find_map(|arg| match arg { |
| 60 | + ColumnarValue::Array(array) => Some(array.len()), |
| 61 | + _ => None, |
| 62 | + }) |
| 63 | + .unwrap_or(number_rows); |
| 64 | + |
| 65 | + // Convert all scalars to arrays for uniform processing |
| 66 | + let arrays: Result<Vec<_>> = args |
| 67 | + .iter() |
| 68 | + .map(|arg| match arg { |
| 69 | + ColumnarValue::Array(array) => Ok(Arc::clone(array)), |
| 70 | + ColumnarValue::Scalar(scalar) => scalar.to_array_of_size(array_len), |
| 71 | + }) |
| 72 | + .collect(); |
| 73 | + let arrays = arrays?; |
| 74 | + |
| 75 | + // Use NullBuffer::union to combine all null buffers |
| 76 | + let combined_nulls = arrays |
| 77 | + .iter() |
| 78 | + .map(|arr| arr.nulls()) |
| 79 | + .fold(None, |acc, nulls| NullBuffer::union(acc.as_ref(), nulls)); |
| 80 | + |
| 81 | + match combined_nulls { |
| 82 | + Some(nulls) => Ok(NullMaskResolution::Apply(nulls)), |
| 83 | + None => Ok(NullMaskResolution::NoMask), |
| 84 | + } |
| 85 | + } |
| 86 | +} |
| 87 | + |
| 88 | +/// Apply NULL mask to the result using NullBuffer::union |
| 89 | +pub(crate) fn apply_null_mask( |
| 90 | + result: ColumnarValue, |
| 91 | + null_mask: NullMaskResolution, |
| 92 | + return_type: &DataType, |
| 93 | +) -> Result<ColumnarValue> { |
| 94 | + match (result, null_mask) { |
| 95 | + // Scalar with ReturnNull mask means return NULL of the correct type |
| 96 | + (ColumnarValue::Scalar(_), NullMaskResolution::ReturnNull) => { |
| 97 | + Ok(ColumnarValue::Scalar(ScalarValue::try_from(return_type)?)) |
| 98 | + } |
| 99 | + // Scalar without mask, return as-is |
| 100 | + (scalar @ ColumnarValue::Scalar(_), NullMaskResolution::NoMask) => Ok(scalar), |
| 101 | + // Array with NULL mask - use NullBuffer::union to combine nulls |
| 102 | + (ColumnarValue::Array(array), NullMaskResolution::Apply(null_mask)) => { |
| 103 | + // Combine the result's existing nulls with our computed null mask |
| 104 | + let combined_nulls = NullBuffer::union(array.nulls(), Some(&null_mask)); |
| 105 | + |
| 106 | + // Create new array with combined nulls |
| 107 | + let new_array = array |
| 108 | + .into_data() |
| 109 | + .into_builder() |
| 110 | + .nulls(combined_nulls) |
| 111 | + .build()?; |
| 112 | + |
| 113 | + Ok(ColumnarValue::Array(Arc::new(arrow::array::make_array( |
| 114 | + new_array, |
| 115 | + )))) |
| 116 | + } |
| 117 | + // Array without NULL mask, return as-is |
| 118 | + (array @ ColumnarValue::Array(_), NullMaskResolution::NoMask) => Ok(array), |
| 119 | + // Edge cases that shouldn't happen in practice |
| 120 | + (scalar, _) => Ok(scalar), |
| 121 | + } |
| 122 | +} |
0 commit comments