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
use std::sync::{Arc, OnceLock};
use arrow::bitmap::bitmask::BitMask;
use arrow::bitmap::{Bitmap, MutableBitmap};
use polars_core::frame::DataFrame;
use polars_core::prelude::{BooleanChunked, ChunkAgg, DataType, PlIndexMap};
use polars_core::schema::{Schema, SchemaRef};
use polars_core::utils::accumulate_dataframes_vertical_unchecked;
use polars_error::{PolarsResult, feature_gated};
use polars_io::cloud::CloudOptions;
use polars_plan::dsl::deletion::DeletionFilesList;
use polars_plan::dsl::{CastColumnsPolicy, ScanSource};
use polars_utils::format_pl_smallstr;
use polars_utils::pl_path::PlRefPath;
use polars_utils::pl_str::PlSmallStr;
use polars_utils::relaxed_cell::RelaxedCell;
use polars_utils::slice_enum::Slice;
use crate::async_executor::{self, AbortOnDropHandle, TaskPriority};
use crate::metrics::IOMetrics;
use crate::nodes::io_sources::multi_scan::reader_interface::builder::FileReaderBuilder;
use crate::nodes::io_sources::multi_scan::reader_interface::{BeginReadArgs, FileReaderCallbacks};
#[cfg(feature = "parquet")]
use crate::nodes::io_sources::parquet::builder::ParquetReaderBuilder;
#[derive(Clone)]
pub enum DeletionFilesProvider {
None,
#[cfg(feature = "parquet")]
IcebergPositionDelete {
paths: Arc<PlIndexMap<usize, Arc<[String]>>>,
// Amortized allocations
reader_builder: ParquetReaderBuilder,
projected_schema: SchemaRef,
},
}
impl DeletionFilesProvider {
pub fn new(
deletion_files: Option<DeletionFilesList>,
execution_state: &crate::execute::StreamingExecutionState,
io_metrics: Option<Arc<IOMetrics>>,
) -> Self {
if deletion_files.is_none() {
return Self::None;
}
match deletion_files.unwrap() {
DeletionFilesList::IcebergPositionDelete(paths) => feature_gated!("parquet", {
let reader_builder = ParquetReaderBuilder {
first_metadata: None,
options: Arc::new(polars_io::prelude::ParquetOptions {
schema: Some(Arc::new(Schema::from_iter([
(PlSmallStr::from_static("file_path"), DataType::String),
(PlSmallStr::from_static("pos"), DataType::Int64),
]))),
parallel: polars_io::prelude::ParallelStrategy::Auto,
low_memory: false,
use_statistics: false,
}),
prefetch_limit: RelaxedCell::new_usize(0),
prefetch_semaphore: std::sync::OnceLock::new(),
shared_prefetch_wait_group_slot: Default::default(),
io_metrics: io_metrics.map(OnceLock::from).unwrap_or_default(),
};
reader_builder.set_execution_state(execution_state);
Self::IcebergPositionDelete {
paths,
reader_builder,
projected_schema: Arc::new(Schema::from_iter([
(PlSmallStr::from_static("file_path"), DataType::String),
(PlSmallStr::from_static("pos"), DataType::Int64),
])),
}
}),
}
}
pub fn spawn_row_deletions_init(
&self,
scan_source_idx: usize,
cloud_options: Option<Arc<CloudOptions>>,
num_pipelines: usize,
verbose: bool,
) -> Option<RowDeletionsInit> {
match self {
Self::None => None,
#[cfg(feature = "parquet")]
Self::IcebergPositionDelete {
paths,
reader_builder,
projected_schema,
} => {
let paths = paths.get(&scan_source_idx)?;
if verbose {
let s = if paths.len() == 1 { "" } else { "s" };
eprintln!(
"[DeletionFilesProvider[Iceberg]]: scan_source_idx: {}, {} file{s}",
scan_source_idx,
paths.len(),
)
}
// We create the readers and immediately spawn off tasks to initialize all of them.
let file_readers = paths
.iter()
.enumerate()
.map(|(deletion_file_idx, path)| {
let source = ScanSource::Path(PlRefPath::new(path));
let mut reader = reader_builder.build_file_reader(
source,
cloud_options.clone(),
deletion_file_idx,
);
if verbose {
eprintln!(
"[DeletionFilesProvider[Iceberg]]: scan_source_idx: {scan_source_idx}, \
deletion_file_idx: {deletion_file_idx}, \
deletion_file_path: {path}"
)
}
AbortOnDropHandle::new(async_executor::spawn(
TaskPriority::Low,
async move {
reader.initialize().await?;
PolarsResult::Ok(reader)
},
))
})
.collect::<Vec<_>>();
let projected_schema = projected_schema.clone();
// We choose to load deletion files immediately during the initialization phase -
// the main driver loop of the multi file may need to serially `.await` on this
// between initializing readers when there is a slice.
//
// This does mean deletion file loads are tied to `NUM_READERS_PRE_INIT`, but this
// should be fine as the size of the data should not be too big.
let handle = AbortOnDropHandle::new(async_executor::spawn(
TaskPriority::Low,
async move {
let handles = file_readers
.into_iter()
.map(|init_fut| {
use crate::nodes::io_sources::multi_scan::components::projection::Projection;
let begin_read_args = BeginReadArgs {
projection: Projection::Plain(projected_schema.clone()),
row_index: None,
pre_slice: None,
predicate: None,
cast_columns_policy: CastColumnsPolicy::ERROR_ON_MISMATCH,
num_pipelines,
disable_morsel_split: false,
callbacks: FileReaderCallbacks {
file_schema_tx: None,
n_rows_in_file_tx: None,
row_position_on_end_tx: None,
},
};
AbortOnDropHandle::new(async_executor::spawn(
TaskPriority::Low,
async move {
let mut reader = init_fut.await?;
let (mut rx, handle) =
reader.begin_read(begin_read_args)?;
let mut dfs = vec![];
while let Ok(morsel) = rx.recv().await {
dfs.push(morsel.into_df());
}
handle.await?;
let df = accumulate_dataframes_vertical_unchecked(dfs);
// Some quick testing on AWS Athena showed that it doesn't
// write deletion files that reference multiple distinct
// file paths, so we don't handle that for now.
assert!(
df.column("file_path")?.n_unique()? <= 1,
"assertion failed: iceberg position delete file: \
n_unique(data_file_paths) <= 1. \
This is a bug, please open an issue"
);
let positions_col = df.column("pos")?.clone();
let max_idx = usize::try_from(
positions_col
.as_materialized_series_maintain_scalar()
.i64()
.unwrap()
.max()
.unwrap_or(0),
)
.unwrap();
PolarsResult::Ok((positions_col, max_idx))
},
))
})
.collect::<Vec<_>>();
let mut position_columns = Vec::with_capacity(handles.len());
let mut filter_mask_len: usize = 0;
for handle in handles {
let (positions_col, max_idx) = handle.await?;
filter_mask_len = filter_mask_len.max(max_idx.saturating_add(1));
position_columns.push(positions_col);
}
let mut filter_mask = MutableBitmap::from_len_set(filter_mask_len);
for c in position_columns {
for idx in c.as_materialized_series_maintain_scalar().i64().unwrap() {
let idx = usize::try_from(idx.unwrap()).unwrap();
filter_mask.set(idx, false);
}
}
let bitmap = filter_mask.freeze();
// Also trigger the bitcount to reduce blocking later down.
bitmap.unset_bits();
debug_assert!(bitmap.lazy_unset_bits().is_some());
let mask = BooleanChunked::from_bitmap(PlSmallStr::EMPTY, bitmap);
let mask = ExternalFilterMask::IcebergPositionDelete { mask };
if verbose {
let num_deleted_rows = mask.num_deleted_rows();
let max_index = mask.len().checked_sub(1);
eprintln!(
"[DeletionFilesProvider[Iceberg]]: \
scan_source_idx: {scan_source_idx}, \
num_deleted_rows: {num_deleted_rows}, \
max_index: {max_index:?}",
)
}
Ok(mask)
},
));
Some(RowDeletionsInit::Initializing(handle))
},
}
}
}
pub enum RowDeletionsInit {
Initializing(AbortOnDropHandle<PolarsResult<ExternalFilterMask>>),
/// If negative slice resolution happens we will already have the initialized filter mask
/// much earlier in the pipeline, but the channel interface still requires a `RowDeletionsInit`,
/// so this enum variant is used to hold the initialized mask.
Initialized(ExternalFilterMask),
}
impl RowDeletionsInit {
/// Loads the deletion information into a filter mask.
pub async fn into_external_filter_mask(self) -> PolarsResult<ExternalFilterMask> {
match self {
Self::Initializing(handle) => handle.await,
Self::Initialized(v) => Ok(v),
}
}
}
#[derive(Debug, Clone)]
pub enum ExternalFilterMask {
/// Note: Iceberg positional deletes can have a mask length shorter than the actual data.
IcebergPositionDelete { mask: BooleanChunked },
}
impl ExternalFilterMask {
pub fn variant_name(&self) -> &'static str {
use ExternalFilterMask::*;
match self {
IcebergPositionDelete { .. } => "IcebergPositionDelete",
}
}
/// Human-friendly verbose log display.
pub fn log_display(this: Option<&Self>) -> PlSmallStr {
match this {
None => PlSmallStr::from_static("None"),
Some(mask) => {
let mask_variant = mask.variant_name();
let n = mask.num_deleted_rows();
let s = if n == 1 { "" } else { "s" };
format_pl_smallstr!("{mask_variant}(<{n} deletion{s}>)")
},
}
}
pub fn filter_df(&self, df: &mut DataFrame) -> PolarsResult<()> {
match self {
Self::IcebergPositionDelete { mask } => {
if !mask.is_empty() {
*df = if mask.len() < df.height() {
accumulate_dataframes_vertical_unchecked([
df.slice(0, mask.len()).filter_seq(mask)?,
df.slice(i64::try_from(mask.len()).unwrap(), df.height() - mask.len()),
])
} else {
df.filter_seq(mask)?
}
}
},
}
Ok(())
}
pub fn slice(&self, offset: usize, len: usize) -> Self {
match self {
Self::IcebergPositionDelete { mask } => {
// This is not a valid offset, it's also a sentinel value from `RowCounter::MAX`.
assert_ne!(offset, usize::MAX);
let offset = offset.min(mask.len());
let len = len.min(mask.len() - offset);
let mask = mask.slice(i64::try_from(offset).unwrap(), len);
Self::IcebergPositionDelete { mask }
},
}
}
pub fn num_deleted_rows(&self) -> usize {
match self {
Self::IcebergPositionDelete { mask } => mask
.rechunk()
.downcast_get(0)
.unwrap()
.values()
.unset_bits(),
}
}
/// Calculates the physical pre_slice that can be applied before performing row deletions.
///
/// By default, a `pre_slice` is applied after rows are deleted. This function takes a `pre_slice`
/// and translates it to `physical` positions (i.e. one that can be applied before row deletions).
/// This is done by expanding the range of the slice to account for the deleted rows.
///
/// This involves 2 `nth_set_bit` searches for offset and length.
///
/// # Panics
/// Panics if `slice` is negative.
pub fn calc_physical_slice(&self, slice: Slice) -> Slice {
let mask = self.get_mask();
let phys_slice = match slice {
Slice::Positive { offset, len } => {
let phys_offset = nth_set_bit_extend(&mask, offset);
let phys_len = if phys_offset >= mask.len() || len == 0 {
// We are past any row deletions
len
} else {
let mask = mask.clone().sliced(phys_offset, mask.len() - phys_offset);
nth_set_bit_extend(&mask, len - 1).saturating_add(1)
};
Slice::Positive {
offset: phys_offset,
len: phys_len,
}
},
// We cannot calculate the physical slice for the negative case because we don't know
// the total length of the file.
//
// TODO: Bitmap-based deletion vectors (e.g. Delta, Iceberg V3) are sized as the exact
// row-count, so the slice can be normalized to positive before reaching here.
Slice::Negative { .. } => {
panic!()
},
};
assert!(phys_slice.len() >= slice.len());
phys_slice
}
fn get_mask(&self) -> Bitmap {
match self {
Self::IcebergPositionDelete { mask } => {
mask.rechunk().downcast_get(0).unwrap().values().clone()
},
}
}
pub fn len(&self) -> usize {
match self {
Self::IcebergPositionDelete { mask } => mask.len(),
}
}
}
/// Calculates the nth set bit as though `mask` were extended infinitely with trues.
fn nth_set_bit_extend(mask: &Bitmap, n: usize) -> usize {
if let Some(n_additional) = n.checked_sub(mask.set_bits()) {
mask.len().saturating_add(n_additional)
} else {
BitMask::from_bitmap(mask).nth_set_bit_idx(n, 0).unwrap()
}
}
#[cfg(test)]
mod tests {
use polars_utils::slice_enum::Slice;
use super::ExternalFilterMask;
fn split_mask<I>(mask: I, slice: Slice) -> (Vec<bool>, Slice)
where
I: IntoIterator<Item = bool>,
{
let mask = ExternalFilterMask::IcebergPositionDelete {
mask: mask.into_iter().collect(),
};
let slice = mask.calc_physical_slice(slice);
let mask = {
let Slice::Positive { offset, len } =
slice.clone().restrict_to_bounds(mask.get_mask().len())
else {
unreachable!()
};
mask.get_mask().sliced(offset, len)
};
(mask.into_iter().collect(), slice)
}
#[test]
fn test_split_slice_positive() {
const T: bool = true;
const F: bool = false;
{
let mask = [];
let slice = Slice::Positive { offset: 0, len: 0 };
let (mask, slice) = split_mask(mask, slice);
assert!(mask.is_empty());
assert_eq!(slice, Slice::Positive { offset: 0, len: 0 });
}
{
let mask = [];
let slice = Slice::Positive { offset: 1, len: 1 };
let (mask, slice) = split_mask(mask, slice);
assert!(mask.is_empty());
assert_eq!(slice, Slice::Positive { offset: 1, len: 1 });
}
{
let mask = [F, T];
let slice = Slice::Positive { offset: 0, len: 0 };
let (mask, slice) = split_mask(mask, slice);
assert!(mask.is_empty());
assert_eq!(slice, Slice::Positive { offset: 1, len: 0 });
}
{
let mask = [F, T];
let slice = Slice::Positive {
offset: usize::MAX,
len: 0,
};
let (mask, slice) = split_mask(mask, slice);
assert!(mask.is_empty());
assert_eq!(
slice,
Slice::Positive {
offset: usize::MAX,
len: 0
}
);
}
{
let mask = [F, T, F, F, T, F, T, F, T];
// true_index : 0 1 2 3 (index after deletion)
// phys_index : 0, 1, 2, 3, 4, 5, 6, 7, 8
// offset : ^ = 4
// len : ^^^^^^^ = 3 items
let slice = Slice::Positive { offset: 1, len: 2 };
let (mask, slice) = split_mask(mask, slice);
assert_eq!(mask.as_slice(), &[T, F, T]);
assert_eq!(slice, Slice::Positive { offset: 4, len: 3 });
}
{
let mask = [T, F];
let slice = Slice::Positive { offset: 0, len: 2 };
let (mask, slice) = split_mask(mask, slice);
assert_eq!(mask.as_slice(), &[T, F]);
assert_eq!(slice, Slice::Positive { offset: 0, len: 3 });
}
{
let mask = [T, F];
let slice = Slice::Positive {
offset: 100,
len: 50,
};
let (mask, slice) = split_mask(mask, slice);
assert!(mask.is_empty());
assert_eq!(
slice,
Slice::Positive {
offset: 101,
len: 50
}
);
}
{
let mask = [T, F];
let slice = Slice::Positive { offset: 0, len: 50 };
let (mask, slice) = split_mask(mask, slice);
assert_eq!(mask.as_slice(), &[T, F]);
assert_eq!(slice, Slice::Positive { offset: 0, len: 51 });
}
{
let mask = [T, F, F, T, F];
let slice = Slice::Positive { offset: 0, len: 50 };
let (mask, slice) = split_mask(mask, slice);
assert_eq!(mask.as_slice(), &[T, F, F, T, F]);
assert_eq!(slice, Slice::Positive { offset: 0, len: 53 });
}
}
}