forked from apache/datafusion
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathbinary_view_map.rs
More file actions
792 lines (709 loc) · 27.8 KB
/
binary_view_map.rs
File metadata and controls
792 lines (709 loc) · 27.8 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
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
// 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.
//! [`ArrowBytesViewMap`] and [`ArrowBytesViewSet`] for storing maps/sets of values from
//! `StringViewArray`/`BinaryViewArray`.
use crate::binary_map::OutputType;
use arrow::array::NullBufferBuilder;
use arrow::array::cast::AsArray;
use arrow::array::{Array, ArrayRef, BinaryViewArray, ByteView, make_view};
use arrow::buffer::{Buffer, ScalarBuffer};
use arrow::datatypes::{BinaryViewType, ByteViewType, DataType, StringViewType};
use datafusion_common::hash_utils::RandomState;
use datafusion_common::hash_utils::create_hashes;
use datafusion_common::utils::proxy::{HashTableAllocExt, VecAllocExt};
use std::fmt::Debug;
use std::mem::size_of;
use std::sync::Arc;
/// HashSet optimized for storing string or binary values that can produce that
/// the final set as a `GenericBinaryViewArray` with minimal copies.
#[derive(Debug)]
pub struct ArrowBytesViewSet(ArrowBytesViewMap<()>);
impl ArrowBytesViewSet {
pub fn new(output_type: OutputType) -> Self {
Self(ArrowBytesViewMap::new(output_type))
}
/// Inserts each value from `values` into the set
pub fn insert(&mut self, values: &ArrayRef) {
fn make_payload_fn(_value: Option<&[u8]>) {}
fn observe_payload_fn(_payload: ()) {}
self.0
.insert_if_new(values, make_payload_fn, observe_payload_fn);
}
/// Return the contents of this map and replace it with a new empty map with
/// the same output type
pub fn take(&mut self) -> Self {
let mut new_self = Self::new(self.0.output_type);
std::mem::swap(self, &mut new_self);
new_self
}
/// Converts this set into a `StringViewArray` or `BinaryViewArray`
/// containing each distinct value that was interned.
/// This is done without copying the values.
pub fn into_state(self) -> ArrayRef {
self.0.into_state()
}
/// Returns the total number of distinct values (including nulls) seen so far
pub fn len(&self) -> usize {
self.0.len()
}
pub fn is_empty(&self) -> bool {
self.0.is_empty()
}
/// returns the total number of distinct values (not including nulls) seen so far
pub fn non_null_len(&self) -> usize {
self.0.non_null_len()
}
/// Return the total size, in bytes, of memory used to store the data in
/// this set, not including `self`
pub fn size(&self) -> usize {
self.0.size()
}
}
/// Optimized map for storing Arrow "byte view" types (`StringView`, `BinaryView`)
/// values that can produce the set of keys on
/// output as `GenericBinaryViewArray` without copies.
///
/// Equivalent to `HashSet<String, V>` but with better performance if you need
/// to emit the keys as an Arrow `StringViewArray` / `BinaryViewArray`. For other
/// purposes it is the same as a `HashMap<String, V>`
///
/// # Generic Arguments
///
/// * `V`: payload type
///
/// # Description
///
/// This is a specialized HashMap with the following properties:
///
/// 1. Optimized for storing and emitting Arrow byte types (e.g.
/// `StringViewArray` / `BinaryViewArray`) very efficiently by minimizing copying of
/// the string values themselves, both when inserting and when emitting the
/// final array.
///
/// 2. Retains the insertion order of entries in the final array. The values are
/// in the same order as they were inserted.
///
/// Note this structure can be used as a `HashSet` by specifying the value type
/// as `()`, as is done by [`ArrowBytesViewSet`].
///
/// This map is used by the special `COUNT DISTINCT` aggregate function to
/// store the distinct values, and by the `GROUP BY` operator to store
/// group values when they are a single string array.
/// Max size of the in-progress buffer before flushing to completed buffers
const BYTE_VIEW_MAX_BLOCK_SIZE: usize = 2 * 1024 * 1024;
pub struct ArrowBytesViewMap<V>
where
V: Debug + PartialEq + Eq + Clone + Copy + Default,
{
/// Should the output be StringView or BinaryView?
output_type: OutputType,
/// Underlying hash set for each distinct value
map: hashbrown::hash_table::HashTable<Entry<V>>,
/// Total size of the map in bytes
map_size: usize,
/// Views for all stored values (in insertion order)
views: Vec<u128>,
/// In-progress buffer for out-of-line string data
in_progress: Vec<u8>,
/// Completed buffers containing string data
completed: Vec<Buffer>,
/// Tracks null values (true = null)
nulls: NullBufferBuilder,
/// random state used to generate hashes
random_state: RandomState,
/// buffer that stores hash values (reused across batches to save allocations)
hashes_buffer: Vec<u64>,
/// `(payload, null_index)` for the 'null' value, if any
/// NOTE null_index is the logical index in the final array, not the index
/// in the buffer
null: Option<(V, usize)>,
}
/// The size, in number of entries, of the initial hash table
const INITIAL_MAP_CAPACITY: usize = 512;
impl<V> ArrowBytesViewMap<V>
where
V: Debug + PartialEq + Eq + Clone + Copy + Default,
{
pub fn new(output_type: OutputType) -> Self {
Self::with_capacity(output_type, INITIAL_MAP_CAPACITY)
}
pub fn with_capacity(output_type: OutputType, capacity: usize) -> Self {
let capacity = capacity.max(INITIAL_MAP_CAPACITY);
Self {
output_type,
map: hashbrown::hash_table::HashTable::with_capacity(capacity),
map_size: 0,
views: Vec::new(),
in_progress: Vec::new(),
completed: Vec::new(),
nulls: NullBufferBuilder::new(0),
random_state: RandomState::default(),
hashes_buffer: vec![],
null: None,
}
}
/// Return the contents of this map and replace it with a new empty map with
/// the same output type
pub fn take(&mut self) -> Self {
let mut new_self = Self::new(self.output_type);
std::mem::swap(self, &mut new_self);
new_self
}
/// Inserts each value from `values` into the map, invoking `payload_fn` for
/// each value if *not* already present, deferring the allocation of the
/// payload until it is needed.
///
/// Note that this is different than a normal map that would replace the
/// existing entry
///
/// # Arguments:
///
/// `values`: array whose values are inserted
///
/// `make_payload_fn`: invoked for each value that is not already present
/// to create the payload, in order of the values in `values`
///
/// `observe_payload_fn`: invoked once, for each value in `values`, that was
/// already present in the map, with corresponding payload value.
///
/// # Returns
///
/// The payload value for the entry, either the existing value or
/// the newly inserted value
///
/// # Safety:
///
/// Note that `make_payload_fn` and `observe_payload_fn` are only invoked
/// with valid values from `values`, not for the `NULL` value.
pub fn insert_if_new<MP, OP>(
&mut self,
values: &ArrayRef,
make_payload_fn: MP,
observe_payload_fn: OP,
) where
MP: FnMut(Option<&[u8]>) -> V,
OP: FnMut(V),
{
// Sanity check array type
match self.output_type {
OutputType::BinaryView => {
assert!(matches!(values.data_type(), DataType::BinaryView));
self.insert_if_new_inner::<MP, OP, BinaryViewType>(
values,
make_payload_fn,
observe_payload_fn,
)
}
OutputType::Utf8View => {
assert!(matches!(values.data_type(), DataType::Utf8View));
self.insert_if_new_inner::<MP, OP, StringViewType>(
values,
make_payload_fn,
observe_payload_fn,
)
}
_ => unreachable!("Utf8/Binary should use `ArrowBytesSet`"),
};
}
/// Generic version of [`Self::insert_if_new`] that handles `ByteViewType`
/// (both StringView and BinaryView)
///
/// Note this is the only function that is generic on [`ByteViewType`], which
/// avoids having to template the entire structure, making the code
/// simpler and understand and reducing code bloat due to duplication.
///
/// See comments on `insert_if_new` for more details
fn insert_if_new_inner<MP, OP, B>(
&mut self,
values: &ArrayRef,
mut make_payload_fn: MP,
mut observe_payload_fn: OP,
) where
MP: FnMut(Option<&[u8]>) -> V,
OP: FnMut(V),
B: ByteViewType,
{
// step 1: compute hashes
let batch_hashes = &mut self.hashes_buffer;
batch_hashes.clear();
batch_hashes.resize(values.len(), 0);
create_hashes([values], &self.random_state, batch_hashes)
// hash is supported for all types and create_hashes only
// returns errors for unsupported types
.unwrap();
// step 2: insert each value into the set, if not already present
let values = values.as_byte_view::<B>();
// Get raw views buffer for direct comparison
let input_views = values.views();
// Ensure lengths are equivalent
assert_eq!(values.len(), self.hashes_buffer.len());
for i in 0..values.len() {
let view_u128 = input_views[i];
let hash = self.hashes_buffer[i];
// handle null value via validity bitmap check
if values.is_null(i) {
let payload = if let Some(&(payload, _offset)) = self.null.as_ref() {
payload
} else {
let payload = make_payload_fn(None);
let null_index = self.views.len();
self.views.push(0);
self.nulls.append_null();
self.null = Some((payload, null_index));
payload
};
observe_payload_fn(payload);
continue;
}
// Extract length from the view (first 4 bytes of u128 in little-endian)
let len = view_u128 as u32;
// Check if value already exists
let maybe_payload = {
// Borrow completed and in_progress for comparison
let completed = &self.completed;
let in_progress = &self.in_progress;
self.map
.find(hash, |header| {
if header.hash != hash {
return false;
}
// Fast path: inline strings can be compared directly
if len <= 12 {
return header.view == view_u128;
}
// For larger strings: first compare the 4-byte prefix
let stored_prefix = (header.view >> 32) as u32;
let input_prefix = (view_u128 >> 32) as u32;
if stored_prefix != input_prefix {
return false;
}
// Prefix matched - compare full bytes
let byte_view = ByteView::from(header.view);
let stored_len = byte_view.length as usize;
let buffer_index = byte_view.buffer_index as usize;
let offset = byte_view.offset as usize;
let stored_value = if buffer_index < completed.len() {
&completed[buffer_index].as_slice()
[offset..offset + stored_len]
} else {
&in_progress[offset..offset + stored_len]
};
let input_value: &[u8] = values.value(i).as_ref();
stored_value == input_value
})
.map(|entry| entry.payload)
};
let payload = if let Some(payload) = maybe_payload {
payload
} else {
// no existing value, make a new one
let value: &[u8] = values.value(i).as_ref();
let payload = make_payload_fn(Some(value));
// Create view pointing to our buffers
let new_view = self.append_value(value);
let new_header = Entry {
view: new_view,
hash,
payload,
};
self.map
.insert_accounted(new_header, |h| h.hash, &mut self.map_size);
payload
};
observe_payload_fn(payload);
}
}
/// Converts this set into a `StringViewArray`, or `BinaryViewArray`,
/// containing each distinct value
/// that was inserted. This is done without copying the values.
///
/// The values are guaranteed to be returned in the same order in which
/// they were first seen.
pub fn into_state(mut self) -> ArrayRef {
// Flush any remaining in-progress buffer
if !self.in_progress.is_empty() {
let flushed = std::mem::take(&mut self.in_progress);
self.completed.push(Buffer::from_vec(flushed));
}
// Build null buffer if we have any nulls
let null_buffer = self.nulls.finish();
let views = ScalarBuffer::from(self.views);
let array =
unsafe { BinaryViewArray::new_unchecked(views, self.completed, null_buffer) };
match self.output_type {
OutputType::BinaryView => Arc::new(array),
OutputType::Utf8View => {
// SAFETY: all input was valid utf8
let array = unsafe { array.to_string_view_unchecked() };
Arc::new(array)
}
_ => unreachable!("Utf8/Binary should use `ArrowBytesMap`"),
}
}
/// Append a value to our buffers and return the view pointing to it
fn append_value(&mut self, value: &[u8]) -> u128 {
let len = value.len();
let view = if len <= 12 {
make_view(value, 0, 0)
} else {
// Ensure buffer is big enough
if self.in_progress.len() + len > BYTE_VIEW_MAX_BLOCK_SIZE {
let flushed = std::mem::replace(
&mut self.in_progress,
Vec::with_capacity(BYTE_VIEW_MAX_BLOCK_SIZE),
);
self.completed.push(Buffer::from_vec(flushed));
}
let buffer_index = self.completed.len() as u32;
let offset = self.in_progress.len() as u32;
self.in_progress.extend_from_slice(value);
make_view(value, buffer_index, offset)
};
self.views.push(view);
self.nulls.append_non_null();
view
}
/// Total number of entries (including null, if present)
pub fn len(&self) -> usize {
self.non_null_len() + self.null.map(|_| 1).unwrap_or(0)
}
/// Is the set empty?
pub fn is_empty(&self) -> bool {
self.map.is_empty() && self.null.is_none()
}
/// Number of non null entries
pub fn non_null_len(&self) -> usize {
self.map.len()
}
/// Return the total size, in bytes, of memory used to store the data in
/// this set, not including `self`
pub fn size(&self) -> usize {
let views_size = self.views.len() * size_of::<u128>();
let in_progress_size = self.in_progress.capacity();
let completed_size: usize = self.completed.iter().map(|b| b.len()).sum();
let nulls_size = self.nulls.allocated_size();
self.map_size
+ views_size
+ in_progress_size
+ completed_size
+ nulls_size
+ self.hashes_buffer.allocated_size()
}
}
impl<V> Debug for ArrowBytesViewMap<V>
where
V: Debug + PartialEq + Eq + Clone + Copy + Default,
{
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("ArrowBytesMap")
.field("map", &"<map>")
.field("map_size", &self.map_size)
.field("views_len", &self.views.len())
.field("completed_buffers", &self.completed.len())
.field("random_state", &self.random_state)
.field("hashes_buffer", &self.hashes_buffer)
.finish()
}
}
/// Entry in the hash table -- see [`ArrowBytesViewMap`] for more details
///
/// Stores the view pointing to our internal buffers, eliminating the need
/// for a separate builder index. For inline strings (<=12 bytes), the view
/// contains the entire value. For out-of-line strings, the view contains
/// buffer_index and offset pointing directly to our storage.
#[derive(Debug, PartialEq, Eq, Hash, Clone, Copy)]
struct Entry<V>
where
V: Debug + PartialEq + Eq + Clone + Copy + Default,
{
/// The u128 view pointing to our internal buffers. For inline strings,
/// this contains the complete value. For larger strings, this contains
/// the buffer_index/offset into our completed/in_progress buffers.
view: u128,
hash: u64,
/// value stored by the entry
payload: V,
}
#[cfg(test)]
mod tests {
use arrow::array::{GenericByteViewArray, StringViewArray};
use datafusion_common::HashMap;
use super::*;
// asserts that the set contains the expected strings, in the same order
fn assert_set(set: ArrowBytesViewSet, expected: &[Option<&str>]) {
let strings = set.into_state();
let strings = strings.as_string_view();
let state = strings.into_iter().collect::<Vec<_>>();
assert_eq!(state, expected);
}
#[test]
fn string_view_set_empty() {
let mut set = ArrowBytesViewSet::new(OutputType::Utf8View);
let array: ArrayRef = Arc::new(StringViewArray::new_null(0));
set.insert(&array);
assert_eq!(set.len(), 0);
assert_eq!(set.non_null_len(), 0);
assert_set(set, &[]);
}
#[test]
fn string_view_set_one_null() {
let mut set = ArrowBytesViewSet::new(OutputType::Utf8View);
let array: ArrayRef = Arc::new(StringViewArray::new_null(1));
set.insert(&array);
assert_eq!(set.len(), 1);
assert_eq!(set.non_null_len(), 0);
assert_set(set, &[None]);
}
#[test]
fn string_view_set_many_null() {
let mut set = ArrowBytesViewSet::new(OutputType::Utf8View);
let array: ArrayRef = Arc::new(StringViewArray::new_null(11));
set.insert(&array);
assert_eq!(set.len(), 1);
assert_eq!(set.non_null_len(), 0);
assert_set(set, &[None]);
}
#[test]
fn test_string_view_set_basic() {
// basic test for mixed small and large string values
let values = GenericByteViewArray::from(vec![
Some("a"),
Some("b"),
Some("CXCCCCCCCCAABB"), // 14 bytes
Some(""),
Some("cbcxx"), // 5 bytes
None,
Some("AAAAAAAA"), // 8 bytes
Some("BBBBBQBBBAAA"), // 12 bytes
Some("a"),
Some("cbcxx"),
Some("b"),
Some("cbcxx"),
Some(""),
None,
Some("BBBBBQBBBAAA"),
Some("BBBBBQBBBAAA"),
Some("AAAAAAAA"),
Some("CXCCCCCCCCAABB"),
]);
let mut set = ArrowBytesViewSet::new(OutputType::Utf8View);
let array: ArrayRef = Arc::new(values);
set.insert(&array);
// values mut appear be in the order they were inserted
assert_set(
set,
&[
Some("a"),
Some("b"),
Some("CXCCCCCCCCAABB"),
Some(""),
Some("cbcxx"),
None,
Some("AAAAAAAA"),
Some("BBBBBQBBBAAA"),
],
);
}
#[test]
fn test_string_set_non_utf8() {
// basic test for mixed small and large string values
let values = GenericByteViewArray::from(vec![
Some("a"),
Some("✨🔥✨🔥✨🔥✨🔥✨🔥✨🔥✨🔥✨🔥"),
Some("🔥"),
Some("✨✨✨"),
Some("foobarbaz"),
Some("🔥"),
Some("✨🔥✨🔥✨🔥✨🔥✨🔥✨🔥✨🔥✨🔥"),
]);
let mut set = ArrowBytesViewSet::new(OutputType::Utf8View);
let array: ArrayRef = Arc::new(values);
set.insert(&array);
// strings mut appear be in the order they were inserted
assert_set(
set,
&[
Some("a"),
Some("✨🔥✨🔥✨🔥✨🔥✨🔥✨🔥✨🔥✨🔥"),
Some("🔥"),
Some("✨✨✨"),
Some("foobarbaz"),
],
);
}
// Test use of binary output type
#[test]
fn test_binary_set() {
let v: Vec<Option<&[u8]>> = vec![
Some(b"a"),
Some(b"CXCCCCCCCCCCCCC"),
None,
Some(b"CXCCCCCCCCCCCCC"),
];
let values: ArrayRef = Arc::new(BinaryViewArray::from(v));
let expected: Vec<Option<&[u8]>> =
vec![Some(b"a"), Some(b"CXCCCCCCCCCCCCC"), None];
let expected: ArrayRef = Arc::new(GenericByteViewArray::from(expected));
let mut set = ArrowBytesViewSet::new(OutputType::BinaryView);
set.insert(&values);
assert_eq!(&set.into_state(), &expected);
}
// inserting strings into the set does not increase reported memory
#[test]
fn test_string_set_memory_usage() {
let strings1 = StringViewArray::from(vec![
Some("a"),
Some("b"),
Some("CXCCCCCCCCCCC"), // 13 bytes
Some("AAAAAAAA"), // 8 bytes
Some("BBBBBQBBB"), // 9 bytes
]);
let total_strings1_len = strings1
.iter()
.map(|s| s.map(|s| s.len()).unwrap_or(0))
.sum::<usize>();
let values1: ArrayRef = Arc::new(StringViewArray::from(strings1));
// Much larger strings in strings2
let strings2 = StringViewArray::from(vec![
"FOO".repeat(1000),
"BAR larger than 12 bytes.".repeat(100_000),
"more unique.".repeat(1000),
"more unique2.".repeat(1000),
"FOO".repeat(3000),
]);
let total_strings2_len = strings2
.iter()
.map(|s| s.map(|s| s.len()).unwrap_or(0))
.sum::<usize>();
let values2: ArrayRef = Arc::new(StringViewArray::from(strings2));
let mut set = ArrowBytesViewSet::new(OutputType::Utf8View);
let size_empty = set.size();
set.insert(&values1);
let size_after_values1 = set.size();
assert!(size_empty < size_after_values1);
assert!(
size_after_values1 > total_strings1_len,
"expect {size_after_values1} to be more than {total_strings1_len}"
);
assert!(size_after_values1 < total_strings1_len + total_strings2_len);
// inserting the same strings should not affect the size
set.insert(&values1);
assert_eq!(set.size(), size_after_values1);
assert_eq!(set.len(), 5);
// inserting the large strings should increase the reported size
set.insert(&values2);
let size_after_values2 = set.size();
assert!(size_after_values2 > size_after_values1);
assert_eq!(set.len(), 10);
}
#[derive(Debug, PartialEq, Eq, Default, Clone, Copy)]
struct TestPayload {
// store the string value to check against input
index: usize, // store the index of the string (each new string gets the next sequential input)
}
/// Wraps an [`ArrowBytesViewMap`], validating its invariants
struct TestMap {
map: ArrowBytesViewMap<TestPayload>,
// stores distinct strings seen, in order
strings: Vec<Option<String>>,
// map strings to index in strings
indexes: HashMap<Option<String>, usize>,
}
impl TestMap {
/// creates a map with TestPayloads for the given strings and then
/// validates the payloads
fn new() -> Self {
Self {
map: ArrowBytesViewMap::new(OutputType::Utf8View),
strings: vec![],
indexes: HashMap::new(),
}
}
/// Inserts strings into the map
fn insert(&mut self, strings: &[Option<&str>]) {
let string_array = StringViewArray::from(strings.to_vec());
let arr: ArrayRef = Arc::new(string_array);
let mut next_index = self.indexes.len();
let mut actual_new_strings = vec![];
let mut actual_seen_indexes = vec![];
// update self with new values, keeping track of newly added values
for str in strings {
let str = str.map(|s| s.to_string());
let index = self.indexes.get(&str).cloned().unwrap_or_else(|| {
actual_new_strings.push(str.clone());
let index = self.strings.len();
self.strings.push(str.clone());
self.indexes.insert(str, index);
index
});
actual_seen_indexes.push(index);
}
// insert the values into the map, recording what we did
let mut seen_new_strings = vec![];
let mut seen_indexes = vec![];
self.map.insert_if_new(
&arr,
|s| {
let value = s
.map(|s| String::from_utf8(s.to_vec()).expect("Non utf8 string"));
let index = next_index;
next_index += 1;
seen_new_strings.push(value);
TestPayload { index }
},
|payload| {
seen_indexes.push(payload.index);
},
);
assert_eq!(actual_seen_indexes, seen_indexes);
assert_eq!(actual_new_strings, seen_new_strings);
}
/// Call `self.map.into_array()` validating that the strings are in the same
/// order as they were inserted
fn into_array(self) -> ArrayRef {
let Self {
map,
strings,
indexes: _,
} = self;
let arr = map.into_state();
let expected: ArrayRef = Arc::new(StringViewArray::from(strings));
assert_eq!(&arr, &expected);
arr
}
}
#[test]
fn test_map() {
let input = vec![
// Note mix of short/long strings
Some("A"),
Some("bcdefghijklmnop1234567"),
Some("X"),
Some("Y"),
None,
Some("qrstuvqxyzhjwya"),
Some("✨🔥"),
Some("🔥"),
Some("🔥🔥🔥🔥🔥🔥"),
];
let mut test_map = TestMap::new();
test_map.insert(&input);
test_map.insert(&input); // put it in twice
let expected_output: ArrayRef = Arc::new(StringViewArray::from(input));
assert_eq!(&test_map.into_array(), &expected_output);
}
}