forked from apache/datafusion
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgroup_column.rs
More file actions
514 lines (461 loc) · 18.5 KB
/
Copy pathgroup_column.rs
File metadata and controls
514 lines (461 loc) · 18.5 KB
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
// 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
// to you under the Apache License, Version 2.0 (the
// "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::BufferBuilder;
use arrow::array::GenericBinaryArray;
use arrow::array::GenericStringArray;
use arrow::array::OffsetSizeTrait;
use arrow::array::PrimitiveArray;
use arrow::array::{Array, ArrayRef, ArrowPrimitiveType, AsArray};
use arrow::buffer::OffsetBuffer;
use arrow::buffer::ScalarBuffer;
use arrow::datatypes::ByteArrayType;
use arrow::datatypes::DataType;
use arrow::datatypes::GenericBinaryType;
use datafusion_common::utils::proxy::VecAllocExt;
use crate::aggregates::group_values::null_builder::MaybeNullBufferBuilder;
use arrow_array::types::GenericStringType;
use datafusion_physical_expr_common::binary_map::{OutputType, INITIAL_BUFFER_CAPACITY};
use std::sync::Arc;
use std::vec;
/// Trait for storing a single column of group values in [`GroupValuesColumn`]
///
/// Implementations of this trait store an in-progress collection of group values
/// (similar to various builders in Arrow-rs) that allow for quick comparison to
/// incoming rows.
///
/// [`GroupValuesColumn`]: crate::aggregates::group_values::GroupValuesColumn
pub trait GroupColumn: Send + Sync {
/// Returns equal if the row stored in this builder at `lhs_row` is equal to
/// the row in `array` at `rhs_row`
///
/// Note that this comparison returns true if both elements are NULL
fn equal_to(&self, lhs_row: usize, array: &ArrayRef, rhs_row: usize) -> bool;
/// Appends the row at `row` in `array` to this builder
fn append_val(&mut self, array: &ArrayRef, row: usize);
/// Returns the number of rows stored in this builder
fn len(&self) -> usize;
/// Returns the number of bytes used by this [`GroupColumn`]
fn size(&self) -> usize;
/// Builds a new array from all of the stored rows
fn build(self: Box<Self>) -> ArrayRef;
/// Builds a new array from the first `n` stored rows, shifting the
/// remaining rows to the start of the builder
fn take_n(&mut self, n: usize) -> ArrayRef;
}
/// An implementation of [`GroupColumn`] for primitive values
///
/// Optimized to skip null buffer construction if the input is known to be non nullable
///
/// # Template parameters
///
/// `T`: the native Rust type that stores the data
/// `NULLABLE`: if the data can contain any nulls
#[derive(Debug)]
pub struct PrimitiveGroupValueBuilder<T: ArrowPrimitiveType, const NULLABLE: bool> {
group_values: Vec<T::Native>,
nulls: MaybeNullBufferBuilder,
}
impl<T, const NULLABLE: bool> PrimitiveGroupValueBuilder<T, NULLABLE>
where
T: ArrowPrimitiveType,
{
/// Create a new `PrimitiveGroupValueBuilder`
pub fn new() -> Self {
Self {
group_values: vec![],
nulls: MaybeNullBufferBuilder::new(),
}
}
}
impl<T: ArrowPrimitiveType, const NULLABLE: bool> GroupColumn
for PrimitiveGroupValueBuilder<T, NULLABLE>
{
fn equal_to(&self, lhs_row: usize, array: &ArrayRef, rhs_row: usize) -> bool {
// Perf: skip null check (by short circuit) if input is not nullable
if NULLABLE {
// In nullable path, we should check if both `exist row` and `input row`
// are null/not null
let is_exist_null = self.nulls.is_null(lhs_row);
let null_match = is_exist_null == array.is_null(rhs_row);
if !null_match {
// If `is_null`s in `exist row` and `input row` don't match, return not equal to
return false;
} else if is_exist_null {
// If `is_null`s in `exist row` and `input row` match, and they are `null`s,
// return equal to
//
// NOTICE: we should not check their values when they are `null`s, because they are
// meaningless actually, and not ensured to be same
//
return true;
}
// Otherwise, we need to check their values
}
self.group_values[lhs_row] == array.as_primitive::<T>().value(rhs_row)
}
fn append_val(&mut self, array: &ArrayRef, row: usize) {
// Perf: skip null check if input can't have nulls
if NULLABLE {
if array.is_null(row) {
self.nulls.append(true);
self.group_values.push(T::default_value());
} else {
self.nulls.append(false);
self.group_values.push(array.as_primitive::<T>().value(row));
}
} else {
self.group_values.push(array.as_primitive::<T>().value(row));
}
}
fn len(&self) -> usize {
self.group_values.len()
}
fn size(&self) -> usize {
self.group_values.allocated_size() + self.nulls.allocated_size()
}
fn build(self: Box<Self>) -> ArrayRef {
let Self {
group_values,
nulls,
} = *self;
let nulls = nulls.build();
if !NULLABLE {
assert!(nulls.is_none(), "unexpected nulls in non nullable input");
}
Arc::new(PrimitiveArray::<T>::new(
ScalarBuffer::from(group_values),
nulls,
))
}
fn take_n(&mut self, n: usize) -> ArrayRef {
let first_n = self.group_values.drain(0..n).collect::<Vec<_>>();
let first_n_nulls = if NULLABLE { self.nulls.take_n(n) } else { None };
Arc::new(PrimitiveArray::<T>::new(
ScalarBuffer::from(first_n),
first_n_nulls,
))
}
}
/// An implementation of [`GroupColumn`] for binary and utf8 types.
///
/// Stores a collection of binary or utf8 group values in a single buffer
/// in a way that allows:
///
/// 1. Efficient comparison of incoming rows to existing rows
/// 2. Efficient construction of the final output array
pub struct ByteGroupValueBuilder<O>
where
O: OffsetSizeTrait,
{
output_type: OutputType,
buffer: BufferBuilder<u8>,
/// Offsets into `buffer` for each distinct value. These offsets as used
/// directly to create the final `GenericBinaryArray`. The `i`th string is
/// stored in the range `offsets[i]..offsets[i+1]` in `buffer`. Null values
/// are stored as a zero length string.
offsets: Vec<O>,
/// Nulls
nulls: MaybeNullBufferBuilder,
}
impl<O> ByteGroupValueBuilder<O>
where
O: OffsetSizeTrait,
{
pub fn new(output_type: OutputType) -> Self {
Self {
output_type,
buffer: BufferBuilder::new(INITIAL_BUFFER_CAPACITY),
offsets: vec![O::default()],
nulls: MaybeNullBufferBuilder::new(),
}
}
fn append_val_inner<B>(&mut self, array: &ArrayRef, row: usize)
where
B: ByteArrayType,
{
let arr = array.as_bytes::<B>();
if arr.is_null(row) {
self.nulls.append(true);
// nulls need a zero length in the offset buffer
let offset = self.buffer.len();
self.offsets.push(O::usize_as(offset));
} else {
self.nulls.append(false);
let value: &[u8] = arr.value(row).as_ref();
self.buffer.append_slice(value);
self.offsets.push(O::usize_as(self.buffer.len()));
}
}
fn equal_to_inner<B>(&self, lhs_row: usize, array: &ArrayRef, rhs_row: usize) -> bool
where
B: ByteArrayType,
{
let arr = array.as_bytes::<B>();
self.nulls.is_null(lhs_row) == arr.is_null(rhs_row)
&& self.value(lhs_row) == (arr.value(rhs_row).as_ref() as &[u8])
}
/// return the current value of the specified row irrespective of null
pub fn value(&self, row: usize) -> &[u8] {
let l = self.offsets[row].as_usize();
let r = self.offsets[row + 1].as_usize();
// Safety: the offsets are constructed correctly and never decrease
unsafe { self.buffer.as_slice().get_unchecked(l..r) }
}
}
impl<O> GroupColumn for ByteGroupValueBuilder<O>
where
O: OffsetSizeTrait,
{
fn equal_to(&self, lhs_row: usize, column: &ArrayRef, rhs_row: usize) -> bool {
// Sanity array type
match self.output_type {
OutputType::Binary => {
debug_assert!(matches!(
column.data_type(),
DataType::Binary | DataType::LargeBinary
));
self.equal_to_inner::<GenericBinaryType<O>>(lhs_row, column, rhs_row)
}
OutputType::Utf8 => {
debug_assert!(matches!(
column.data_type(),
DataType::Utf8 | DataType::LargeUtf8
));
self.equal_to_inner::<GenericStringType<O>>(lhs_row, column, rhs_row)
}
_ => unreachable!("View types should use `ArrowBytesViewMap`"),
}
}
fn append_val(&mut self, column: &ArrayRef, row: usize) {
// Sanity array type
match self.output_type {
OutputType::Binary => {
debug_assert!(matches!(
column.data_type(),
DataType::Binary | DataType::LargeBinary
));
self.append_val_inner::<GenericBinaryType<O>>(column, row)
}
OutputType::Utf8 => {
debug_assert!(matches!(
column.data_type(),
DataType::Utf8 | DataType::LargeUtf8
));
self.append_val_inner::<GenericStringType<O>>(column, row)
}
_ => unreachable!("View types should use `ArrowBytesViewMap`"),
};
}
fn len(&self) -> usize {
self.offsets.len() - 1
}
fn size(&self) -> usize {
self.buffer.capacity() * std::mem::size_of::<u8>()
+ self.offsets.allocated_size()
+ self.nulls.allocated_size()
}
fn build(self: Box<Self>) -> ArrayRef {
let Self {
output_type,
mut buffer,
offsets,
nulls,
} = *self;
let null_buffer = nulls.build();
// SAFETY: the offsets were constructed correctly in `insert_if_new` --
// monotonically increasing, overflows were checked.
let offsets = unsafe { OffsetBuffer::new_unchecked(ScalarBuffer::from(offsets)) };
let values = buffer.finish();
match output_type {
OutputType::Binary => {
// SAFETY: the offsets were constructed correctly
Arc::new(unsafe {
GenericBinaryArray::new_unchecked(offsets, values, null_buffer)
})
}
OutputType::Utf8 => {
// SAFETY:
// 1. the offsets were constructed safely
//
// 2. the input arrays were all the correct type and thus since
// all the values that went in were valid (e.g. utf8) so are all
// the values that come out
Arc::new(unsafe {
GenericStringArray::new_unchecked(offsets, values, null_buffer)
})
}
_ => unreachable!("View types should use `ArrowBytesViewMap`"),
}
}
fn take_n(&mut self, n: usize) -> ArrayRef {
debug_assert!(self.len() >= n);
let null_buffer = self.nulls.take_n(n);
let first_remaining_offset = O::as_usize(self.offsets[n]);
// Given offests like [0, 2, 4, 5] and n = 1, we expect to get
// offsets [0, 2, 3]. We first create two offsets for first_n as [0, 2] and the remaining as [2, 4, 5].
// And we shift the offset starting from 0 for the remaining one, [2, 4, 5] -> [0, 2, 3].
let mut first_n_offsets = self.offsets.drain(0..n).collect::<Vec<_>>();
let offset_n = *self.offsets.first().unwrap();
self.offsets
.iter_mut()
.for_each(|offset| *offset = offset.sub(offset_n));
first_n_offsets.push(offset_n);
// SAFETY: the offsets were constructed correctly in `insert_if_new` --
// monotonically increasing, overflows were checked.
let offsets =
unsafe { OffsetBuffer::new_unchecked(ScalarBuffer::from(first_n_offsets)) };
let mut remaining_buffer =
BufferBuilder::new(self.buffer.len() - first_remaining_offset);
// TODO: Current approach copy the remaining and truncate the original one
// Find out a way to avoid copying buffer but split the original one into two.
remaining_buffer.append_slice(&self.buffer.as_slice()[first_remaining_offset..]);
self.buffer.truncate(first_remaining_offset);
let values = self.buffer.finish();
self.buffer = remaining_buffer;
match self.output_type {
OutputType::Binary => {
// SAFETY: the offsets were constructed correctly
Arc::new(unsafe {
GenericBinaryArray::new_unchecked(offsets, values, null_buffer)
})
}
OutputType::Utf8 => {
// SAFETY:
// 1. the offsets were constructed safely
//
// 2. we asserted the input arrays were all the correct type and
// thus since all the values that went in were valid (e.g. utf8)
// so are all the values that come out
Arc::new(unsafe {
GenericStringArray::new_unchecked(offsets, values, null_buffer)
})
}
_ => unreachable!("View types should use `ArrowBytesViewMap`"),
}
}
}
#[cfg(test)]
mod tests {
use std::sync::Arc;
use arrow::datatypes::Int64Type;
use arrow_array::{ArrayRef, Int64Array, StringArray};
use arrow_buffer::{BooleanBufferBuilder, NullBuffer};
use datafusion_physical_expr::binary_map::OutputType;
use crate::aggregates::group_values::group_column::PrimitiveGroupValueBuilder;
use super::{ByteGroupValueBuilder, GroupColumn};
#[test]
fn test_take_n() {
let mut builder = ByteGroupValueBuilder::<i32>::new(OutputType::Utf8);
let array = Arc::new(StringArray::from(vec![Some("a"), None])) as ArrayRef;
// a, null, null
builder.append_val(&array, 0);
builder.append_val(&array, 1);
builder.append_val(&array, 1);
// (a, null) remaining: null
let output = builder.take_n(2);
assert_eq!(&output, &array);
// null, a, null, a
builder.append_val(&array, 0);
builder.append_val(&array, 1);
builder.append_val(&array, 0);
// (null, a) remaining: (null, a)
let output = builder.take_n(2);
let array = Arc::new(StringArray::from(vec![None, Some("a")])) as ArrayRef;
assert_eq!(&output, &array);
let array = Arc::new(StringArray::from(vec![
Some("a"),
None,
Some("longstringfortest"),
])) as ArrayRef;
// null, a, longstringfortest, null, null
builder.append_val(&array, 2);
builder.append_val(&array, 1);
builder.append_val(&array, 1);
// (null, a, longstringfortest, null) remaining: (null)
let output = builder.take_n(4);
let array = Arc::new(StringArray::from(vec![
None,
Some("a"),
Some("longstringfortest"),
None,
])) as ArrayRef;
assert_eq!(&output, &array);
}
#[test]
fn test_nullable_primitive_equal_to() {
// Will cover such cases:
// - exist null, input not null
// - exist null, input null; values not equal
// - exist null, input null; values equal
// - exist not null, input null
// - exist not null, input not null; values not equal
// - exist not null, input not null; values equal
// Define PrimitiveGroupValueBuilder
let mut builder = PrimitiveGroupValueBuilder::<Int64Type, true>::new();
let builder_array = Arc::new(Int64Array::from(vec![
None,
None,
None,
Some(1),
Some(2),
Some(3),
])) as ArrayRef;
builder.append_val(&builder_array, 0);
builder.append_val(&builder_array, 1);
builder.append_val(&builder_array, 2);
builder.append_val(&builder_array, 3);
builder.append_val(&builder_array, 4);
builder.append_val(&builder_array, 5);
// Define input array
let (_, values, _) =
Int64Array::from(vec![Some(1), Some(2), None, None, Some(1), Some(3)])
.into_parts();
let mut boolean_buffer_builder = BooleanBufferBuilder::new(6);
boolean_buffer_builder.append(true);
boolean_buffer_builder.append(false);
boolean_buffer_builder.append(false);
boolean_buffer_builder.append(false);
boolean_buffer_builder.append(true);
boolean_buffer_builder.append(true);
let nulls = NullBuffer::new(boolean_buffer_builder.finish());
let input_array = Arc::new(Int64Array::new(values, Some(nulls))) as ArrayRef;
// Check
assert!(!builder.equal_to(0, &input_array, 0));
assert!(builder.equal_to(1, &input_array, 1));
assert!(builder.equal_to(2, &input_array, 2));
assert!(!builder.equal_to(3, &input_array, 3));
assert!(!builder.equal_to(4, &input_array, 4));
assert!(builder.equal_to(5, &input_array, 5));
}
#[test]
fn test_not_nullable_primitive_equal_to() {
// Will cover such cases:
// - values equal
// - values not equal
// Define PrimitiveGroupValueBuilder
let mut builder = PrimitiveGroupValueBuilder::<Int64Type, false>::new();
let builder_array =
Arc::new(Int64Array::from(vec![Some(0), Some(1)])) as ArrayRef;
builder.append_val(&builder_array, 0);
builder.append_val(&builder_array, 1);
// Define input array
let input_array = Arc::new(Int64Array::from(vec![Some(0), Some(2)])) as ArrayRef;
// Check
assert!(builder.equal_to(0, &input_array, 0));
assert!(!builder.equal_to(1, &input_array, 1));
}
}