datafusion_functions_aggregate/min_max/min_max_bytes.rs
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 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515
// Licensed to the Apache Software Foundation (ASF) under one
// or more contributor license agreements. See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership. The ASF licenses this file
// "License"); you may not use this file except in compliance
// with the License. You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.
use arrow::array::{
Array, ArrayRef, AsArray, BinaryBuilder, BinaryViewBuilder, BooleanArray,
LargeBinaryBuilder, LargeStringBuilder, StringBuilder, StringViewBuilder,
};
use arrow_schema::DataType;
use datafusion_common::{internal_err, Result};
use datafusion_expr::{EmitTo, GroupsAccumulator};
use datafusion_functions_aggregate_common::aggregate::groups_accumulator::nulls::apply_filter_as_nulls;
use std::mem::size_of;
use std::sync::Arc;
/// Implements fast Min/Max [`GroupsAccumulator`] for "bytes" types ([`StringArray`],
/// [`BinaryArray`], [`StringViewArray`], etc)
///
/// This implementation dispatches to the appropriate specialized code in
/// [`MinMaxBytesState`] based on data type and comparison function
///
/// [`StringArray`]: arrow::array::StringArray
/// [`BinaryArray`]: arrow::array::BinaryArray
/// [`StringViewArray`]: arrow::array::StringViewArray
#[derive(Debug)]
pub(crate) struct MinMaxBytesAccumulator {
/// Inner data storage.
inner: MinMaxBytesState,
/// if true, is `MIN` otherwise is `MAX`
is_min: bool,
}
impl MinMaxBytesAccumulator {
/// Create a new accumulator for computing `min(val)`
pub fn new_min(data_type: DataType) -> Self {
Self {
inner: MinMaxBytesState::new(data_type),
is_min: true,
}
}
/// Create a new accumulator fo computing `max(val)`
pub fn new_max(data_type: DataType) -> Self {
Self {
inner: MinMaxBytesState::new(data_type),
is_min: false,
}
}
}
impl GroupsAccumulator for MinMaxBytesAccumulator {
fn update_batch(
&mut self,
values: &[ArrayRef],
group_indices: &[usize],
opt_filter: Option<&BooleanArray>,
total_num_groups: usize,
) -> Result<()> {
let array = &values[0];
assert_eq!(array.len(), group_indices.len());
assert_eq!(array.data_type(), &self.inner.data_type);
// apply filter if needed
let array = apply_filter_as_nulls(array, opt_filter)?;
// dispatch to appropriate kernel / specialized implementation
fn string_min(a: &[u8], b: &[u8]) -> bool {
// safety: only called from this function, which ensures a and b come
// from an array with valid utf8 data
unsafe {
let a = std::str::from_utf8_unchecked(a);
let b = std::str::from_utf8_unchecked(b);
a < b
}
}
fn string_max(a: &[u8], b: &[u8]) -> bool {
// safety: only called from this function, which ensures a and b come
// from an array with valid utf8 data
unsafe {
let a = std::str::from_utf8_unchecked(a);
let b = std::str::from_utf8_unchecked(b);
a > b
}
}
fn binary_min(a: &[u8], b: &[u8]) -> bool {
a < b
}
fn binary_max(a: &[u8], b: &[u8]) -> bool {
a > b
}
fn str_to_bytes<'a>(
it: impl Iterator<Item = Option<&'a str>>,
) -> impl Iterator<Item = Option<&'a [u8]>> {
it.map(|s| s.map(|s| s.as_bytes()))
}
match (self.is_min, &self.inner.data_type) {
// Utf8/LargeUtf8/Utf8View Min
(true, &DataType::Utf8) => self.inner.update_batch(
str_to_bytes(array.as_string::<i32>().iter()),
group_indices,
total_num_groups,
string_min,
),
(true, &DataType::LargeUtf8) => self.inner.update_batch(
str_to_bytes(array.as_string::<i64>().iter()),
group_indices,
total_num_groups,
string_min,
),
(true, &DataType::Utf8View) => self.inner.update_batch(
str_to_bytes(array.as_string_view().iter()),
group_indices,
total_num_groups,
string_min,
),
// Utf8/LargeUtf8/Utf8View Max
(false, &DataType::Utf8) => self.inner.update_batch(
str_to_bytes(array.as_string::<i32>().iter()),
group_indices,
total_num_groups,
string_max,
),
(false, &DataType::LargeUtf8) => self.inner.update_batch(
str_to_bytes(array.as_string::<i64>().iter()),
group_indices,
total_num_groups,
string_max,
),
(false, &DataType::Utf8View) => self.inner.update_batch(
str_to_bytes(array.as_string_view().iter()),
group_indices,
total_num_groups,
string_max,
),
// Binary/LargeBinary/BinaryView Min
(true, &DataType::Binary) => self.inner.update_batch(
array.as_binary::<i32>().iter(),
group_indices,
total_num_groups,
binary_min,
),
(true, &DataType::LargeBinary) => self.inner.update_batch(
array.as_binary::<i64>().iter(),
group_indices,
total_num_groups,
binary_min,
),
(true, &DataType::BinaryView) => self.inner.update_batch(
array.as_binary_view().iter(),
group_indices,
total_num_groups,
binary_min,
),
// Binary/LargeBinary/BinaryView Max
(false, &DataType::Binary) => self.inner.update_batch(
array.as_binary::<i32>().iter(),
group_indices,
total_num_groups,
binary_max,
),
(false, &DataType::LargeBinary) => self.inner.update_batch(
array.as_binary::<i64>().iter(),
group_indices,
total_num_groups,
binary_max,
),
(false, &DataType::BinaryView) => self.inner.update_batch(
array.as_binary_view().iter(),
group_indices,
total_num_groups,
binary_max,
),
_ => internal_err!(
"Unexpected combination for MinMaxBytesAccumulator: ({:?}, {:?})",
self.is_min,
self.inner.data_type
),
}
}
fn evaluate(&mut self, emit_to: EmitTo) -> Result<ArrayRef> {
let (data_capacity, min_maxes) = self.inner.emit_to(emit_to);
// Convert the Vec of bytes to a vec of Strings (at no cost)
fn bytes_to_str(
min_maxes: Vec<Option<Vec<u8>>>,
) -> impl Iterator<Item = Option<String>> {
min_maxes.into_iter().map(|opt| {
opt.map(|bytes| {
// Safety: only called on data added from update_batch which ensures
// the input type matched the output type
unsafe { String::from_utf8_unchecked(bytes) }
})
})
}
let result: ArrayRef = match self.inner.data_type {
DataType::Utf8 => {
let mut builder =
StringBuilder::with_capacity(min_maxes.len(), data_capacity);
for opt in bytes_to_str(min_maxes) {
match opt {
None => builder.append_null(),
Some(s) => builder.append_value(s.as_str()),
}
}
Arc::new(builder.finish())
}
DataType::LargeUtf8 => {
let mut builder =
LargeStringBuilder::with_capacity(min_maxes.len(), data_capacity);
for opt in bytes_to_str(min_maxes) {
match opt {
None => builder.append_null(),
Some(s) => builder.append_value(s.as_str()),
}
}
Arc::new(builder.finish())
}
DataType::Utf8View => {
let block_size = capacity_to_view_block_size(data_capacity);
let mut builder = StringViewBuilder::with_capacity(min_maxes.len())
.with_fixed_block_size(block_size);
for opt in bytes_to_str(min_maxes) {
match opt {
None => builder.append_null(),
Some(s) => builder.append_value(s.as_str()),
}
}
Arc::new(builder.finish())
}
DataType::Binary => {
let mut builder =
BinaryBuilder::with_capacity(min_maxes.len(), data_capacity);
for opt in min_maxes {
match opt {
None => builder.append_null(),
Some(s) => builder.append_value(s.as_ref() as &[u8]),
}
}
Arc::new(builder.finish())
}
DataType::LargeBinary => {
let mut builder =
LargeBinaryBuilder::with_capacity(min_maxes.len(), data_capacity);
for opt in min_maxes {
match opt {
None => builder.append_null(),
Some(s) => builder.append_value(s.as_ref() as &[u8]),
}
}
Arc::new(builder.finish())
}
DataType::BinaryView => {
let block_size = capacity_to_view_block_size(data_capacity);
let mut builder = BinaryViewBuilder::with_capacity(min_maxes.len())
.with_fixed_block_size(block_size);
for opt in min_maxes {
match opt {
None => builder.append_null(),
Some(s) => builder.append_value(s.as_ref() as &[u8]),
}
}
Arc::new(builder.finish())
}
_ => {
return internal_err!(
"Unexpected data type for MinMaxBytesAccumulator: {:?}",
self.inner.data_type
);
}
};
assert_eq!(&self.inner.data_type, result.data_type());
Ok(result)
}
fn state(&mut self, emit_to: EmitTo) -> Result<Vec<ArrayRef>> {
// min/max are their own states (no transition needed)
self.evaluate(emit_to).map(|arr| vec![arr])
}
fn merge_batch(
&mut self,
values: &[ArrayRef],
group_indices: &[usize],
opt_filter: Option<&BooleanArray>,
total_num_groups: usize,
) -> Result<()> {
// min/max are their own states (no transition needed)
self.update_batch(values, group_indices, opt_filter, total_num_groups)
}
fn convert_to_state(
&self,
values: &[ArrayRef],
opt_filter: Option<&BooleanArray>,
) -> Result<Vec<ArrayRef>> {
// Min/max do not change the values as they are their own states
// apply the filter by combining with the null mask, if any
let output = apply_filter_as_nulls(&values[0], opt_filter)?;
Ok(vec![output])
}
fn supports_convert_to_state(&self) -> bool {
true
}
fn size(&self) -> usize {
self.inner.size()
}
}
/// Returns the block size in (contiguous buffer size) to use
/// for a given data capacity (total string length)
///
/// This is a heuristic to avoid allocating too many small buffers
fn capacity_to_view_block_size(data_capacity: usize) -> u32 {
let max_block_size = 2 * 1024 * 1024;
if let Ok(block_size) = u32::try_from(data_capacity) {
block_size.min(max_block_size)
} else {
max_block_size
}
}
/// Stores internal Min/Max state for "bytes" types.
///
/// This implementation is general and stores the minimum/maximum for each
/// groups in an individual byte array, which balances allocations and memory
/// fragmentation (aka garbage).
///
/// ```text
/// ┌─────────────────────────────────┐
/// ┌─────┐ ┌────▶│Option<Vec<u8>> (["A"]) │───────────▶ "A"
/// │ 0 │────┘ └─────────────────────────────────┘
/// ├─────┤ ┌─────────────────────────────────┐
/// │ 1 │─────────▶│Option<Vec<u8>> (["Z"]) │───────────▶ "Z"
/// └─────┘ └─────────────────────────────────┘ ...
/// ... ...
/// ┌─────┐ ┌────────────────────────────────┐
/// │ N-2 │─────────▶│Option<Vec<u8>> (["A"]) │────────────▶ "A"
/// ├─────┤ └────────────────────────────────┘
/// │ N-1 │────┐ ┌────────────────────────────────┐
/// └─────┘ └────▶│Option<Vec<u8>> (["Q"]) │────────────▶ "Q"
/// └────────────────────────────────┘
///
/// min_max: Vec<Option<Vec<u8>>
/// ```
///
/// Note that for `StringViewArray` and `BinaryViewArray`, there are potentially
/// more efficient implementations (e.g. by managing a string data buffer
/// directly), but then garbage collection, memory management, and final array
/// construction becomes more complex.
///
/// See discussion on <https://github.com/apache/datafusion/issues/6906>
#[derive(Debug)]
struct MinMaxBytesState {
/// The minimum/maximum value for each group
min_max: Vec<Option<Vec<u8>>>,
/// The data type of the array
data_type: DataType,
/// The total bytes of the string data (for pre-allocating the final array,
/// and tracking memory usage)
total_data_bytes: usize,
}
#[derive(Debug, Clone, Copy)]
enum MinMaxLocation<'a> {
/// the min/max value is stored in the existing `min_max` array
ExistingMinMax,
/// the min/max value is stored in the input array at the given index
Input(&'a [u8]),
}
/// Implement the MinMaxBytesAccumulator with a comparison function
/// for comparing strings
impl MinMaxBytesState {
/// Create a new MinMaxBytesAccumulator
///
/// # Arguments:
/// * `data_type`: The data type of the arrays that will be passed to this accumulator
fn new(data_type: DataType) -> Self {
Self {
min_max: vec![],
data_type,
total_data_bytes: 0,
}
}
/// Set the specified group to the given value, updating memory usage appropriately
fn set_value(&mut self, group_index: usize, new_val: &[u8]) {
match self.min_max[group_index].as_mut() {
None => {
self.min_max[group_index] = Some(new_val.to_vec());
self.total_data_bytes += new_val.len();
}
Some(existing_val) => {
// Copy data over to avoid re-allocating
self.total_data_bytes -= existing_val.len();
self.total_data_bytes += new_val.len();
existing_val.clear();
existing_val.extend_from_slice(new_val);
}
}
}
/// Updates the min/max values for the given string values
///
/// `cmp` is the comparison function to use, called like `cmp(new_val, existing_val)`
/// returns true if the `new_val` should replace `existing_val`
fn update_batch<'a, F, I>(
&mut self,
iter: I,
group_indices: &[usize],
total_num_groups: usize,
mut cmp: F,
) -> Result<()>
where
F: FnMut(&[u8], &[u8]) -> bool + Send + Sync,
I: IntoIterator<Item = Option<&'a [u8]>>,
{
self.min_max.resize(total_num_groups, None);
// Minimize value copies by calculating the new min/maxes for each group
// in this batch (either the existing min/max or the new input value)
// and updating the owne values in `self.min_maxes` at most once
let mut locations = vec![MinMaxLocation::ExistingMinMax; total_num_groups];
// Figure out the new min value for each group
for (new_val, group_index) in iter.into_iter().zip(group_indices.iter()) {
let group_index = *group_index;
let Some(new_val) = new_val else {
continue; // skip nulls
};
let existing_val = match locations[group_index] {
// previous input value was the min/max, so compare it
MinMaxLocation::Input(existing_val) => existing_val,
MinMaxLocation::ExistingMinMax => {
let Some(exising_val) = self.min_max[group_index].as_ref() else {
// no existing min/max, so this is the new min/max
locations[group_index] = MinMaxLocation::Input(new_val);
continue;
};
exising_val.as_ref()
}
};
// Compare the new value to the existing value, replacing if necessary
if cmp(new_val, existing_val) {
locations[group_index] = MinMaxLocation::Input(new_val);
}
}
// Update self.min_max with any new min/max values we found in the input
for (group_index, location) in locations.iter().enumerate() {
match location {
MinMaxLocation::ExistingMinMax => {}
MinMaxLocation::Input(new_val) => self.set_value(group_index, new_val),
}
}
Ok(())
}
/// Emits the specified min_max values
///
/// Returns (data_capacity, min_maxes), updating the current value of total_data_bytes
///
/// - `data_capacity`: the total length of all strings and their contents,
/// - `min_maxes`: the actual min/max values for each group
fn emit_to(&mut self, emit_to: EmitTo) -> (usize, Vec<Option<Vec<u8>>>) {
match emit_to {
EmitTo::All => {
(
std::mem::take(&mut self.total_data_bytes), // reset total bytes and min_max
std::mem::take(&mut self.min_max),
)
}
EmitTo::First(n) => {
let first_min_maxes: Vec<_> = self.min_max.drain(..n).collect();
let first_data_capacity: usize = first_min_maxes
.iter()
.map(|opt| opt.as_ref().map(|s| s.len()).unwrap_or(0))
.sum();
self.total_data_bytes -= first_data_capacity;
(first_data_capacity, first_min_maxes)
}
}
}
fn size(&self) -> usize {
self.total_data_bytes + self.min_max.len() * size_of::<Option<Vec<u8>>>()
}
}