-
Notifications
You must be signed in to change notification settings - Fork 972
/
mod.rs
668 lines (596 loc) · 24 KB
/
mod.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
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
// 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.
//! Execution plans that read file formats
mod avro;
#[cfg(test)]
mod chunked_store;
mod csv;
mod delimited_stream;
mod file_stream;
mod json;
mod parquet;
pub(crate) use self::csv::plan_to_csv;
pub use self::csv::CsvExec;
pub(crate) use self::parquet::plan_to_parquet;
pub use self::parquet::ParquetExec;
use arrow::{
array::{ArrayData, ArrayRef, DictionaryArray},
buffer::Buffer,
datatypes::{DataType, Field, Schema, SchemaRef, UInt16Type},
error::{ArrowError, Result as ArrowResult},
record_batch::RecordBatch,
};
pub use avro::AvroExec;
pub(crate) use json::plan_to_json;
pub use json::NdJsonExec;
use crate::datasource::{listing::PartitionedFile, object_store::ObjectStoreUrl};
use crate::{
error::{DataFusionError, Result},
scalar::ScalarValue,
};
use arrow::array::{new_null_array, UInt16BufferBuilder};
use arrow::record_batch::RecordBatchOptions;
use lazy_static::lazy_static;
use log::info;
use std::{
collections::HashMap,
fmt::{Display, Formatter, Result as FmtResult},
sync::Arc,
vec,
};
use super::{ColumnStatistics, Statistics};
lazy_static! {
/// The datatype used for all partitioning columns for now
pub static ref DEFAULT_PARTITION_COLUMN_DATATYPE: DataType = DataType::Dictionary(Box::new(DataType::UInt16), Box::new(DataType::Utf8));
}
/// The base configurations to provide when creating a physical plan for
/// any given file format.
#[derive(Debug, Clone)]
pub struct FileScanConfig {
/// Object store URL
pub object_store_url: ObjectStoreUrl,
/// Schema before projection. It contains the columns that are expected
/// to be in the files without the table partition columns.
pub file_schema: SchemaRef,
/// List of files to be processed, grouped into partitions
pub file_groups: Vec<Vec<PartitionedFile>>,
/// Estimated overall statistics of the files, taking `filters` into account.
pub statistics: Statistics,
/// Columns on which to project the data. Indexes that are higher than the
/// number of columns of `file_schema` refer to `table_partition_cols`.
pub projection: Option<Vec<usize>>,
/// The minimum number of records required from this source plan
pub limit: Option<usize>,
/// The partitioning column names
pub table_partition_cols: Vec<String>,
}
impl FileScanConfig {
/// Project the schema and the statistics on the given column indices
fn project(&self) -> (SchemaRef, Statistics) {
if self.projection.is_none() && self.table_partition_cols.is_empty() {
return (Arc::clone(&self.file_schema), self.statistics.clone());
}
let proj_iter: Box<dyn Iterator<Item = usize>> = match &self.projection {
Some(proj) => Box::new(proj.iter().copied()),
None => Box::new(
0..(self.file_schema.fields().len() + self.table_partition_cols.len()),
),
};
let mut table_fields = vec![];
let mut table_cols_stats = vec![];
for idx in proj_iter {
if idx < self.file_schema.fields().len() {
table_fields.push(self.file_schema.field(idx).clone());
if let Some(file_cols_stats) = &self.statistics.column_statistics {
table_cols_stats.push(file_cols_stats[idx].clone())
} else {
table_cols_stats.push(ColumnStatistics::default())
}
} else {
let partition_idx = idx - self.file_schema.fields().len();
table_fields.push(Field::new(
&self.table_partition_cols[partition_idx],
DEFAULT_PARTITION_COLUMN_DATATYPE.clone(),
false,
));
// TODO provide accurate stat for partition column (#1186)
table_cols_stats.push(ColumnStatistics::default())
}
}
let table_stats = Statistics {
num_rows: self.statistics.num_rows,
is_exact: self.statistics.is_exact,
// TODO correct byte size?
total_byte_size: None,
column_statistics: Some(table_cols_stats),
};
let table_schema = Arc::new(Schema::new(table_fields));
(table_schema, table_stats)
}
fn projected_file_column_names(&self) -> Option<Vec<String>> {
self.projection.as_ref().map(|p| {
p.iter()
.filter(|col_idx| **col_idx < self.file_schema.fields().len())
.map(|col_idx| self.file_schema.field(*col_idx).name())
.cloned()
.collect()
})
}
fn file_column_projection_indices(&self) -> Option<Vec<usize>> {
self.projection.as_ref().map(|p| {
p.iter()
.filter(|col_idx| **col_idx < self.file_schema.fields().len())
.copied()
.collect()
})
}
}
/// A wrapper to customize partitioned file display
#[derive(Debug)]
struct FileGroupsDisplay<'a>(&'a [Vec<PartitionedFile>]);
impl<'a> Display for FileGroupsDisplay<'a> {
fn fmt(&self, f: &mut Formatter) -> FmtResult {
let parts: Vec<_> = self
.0
.iter()
.map(|pp| {
pp.iter()
.map(|pf| pf.object_meta.location.as_ref())
.collect::<Vec<_>>()
.join(", ")
})
.collect();
write!(f, "[{}]", parts.join(", "))
}
}
/// A wrapper to customize partitioned file display
#[derive(Debug)]
struct ProjectSchemaDisplay<'a>(&'a SchemaRef);
impl<'a> Display for ProjectSchemaDisplay<'a> {
fn fmt(&self, f: &mut Formatter) -> FmtResult {
let parts: Vec<_> = self
.0
.fields()
.iter()
.map(|x| x.name().to_owned())
.collect::<Vec<String>>();
write!(f, "[{}]", parts.join(", "))
}
}
/// A utility which can adapt file-level record batches to a table schema which may have a schema
/// obtained from merging multiple file-level schemas.
///
/// This is useful for enabling schema evolution in partitioned datasets.
///
/// This has to be done in two stages.
///
/// 1. Before reading the file, we have to map projected column indexes from the table schema to
/// the file schema.
///
/// 2. After reading a record batch we need to map the read columns back to the expected columns
/// indexes and insert null-valued columns wherever the file schema was missing a colum present
/// in the table schema.
#[derive(Clone, Debug)]
pub(crate) struct SchemaAdapter {
/// Schema for the table
table_schema: SchemaRef,
}
impl SchemaAdapter {
pub(crate) fn new(table_schema: SchemaRef) -> SchemaAdapter {
Self { table_schema }
}
/// Map a column index in the table schema to a column index in a particular
/// file schema
/// Panics if index is not in range for the table schema
pub(crate) fn map_column_index(
&self,
index: usize,
file_schema: &Schema,
) -> Option<usize> {
let field = self.table_schema.field(index);
file_schema.index_of(field.name()).ok()
}
/// Map projected column indexes to the file schema. This will fail if the table schema
/// and the file schema contain a field with the same name and different types.
pub fn map_projections(
&self,
file_schema: &Schema,
projections: &[usize],
) -> Result<Vec<usize>> {
let mut mapped: Vec<usize> = vec![];
for idx in projections {
let field = self.table_schema.field(*idx);
if let Ok(mapped_idx) = file_schema.index_of(field.name().as_str()) {
if file_schema.field(mapped_idx).data_type() == field.data_type() {
mapped.push(mapped_idx)
} else {
let msg = format!("Failed to map column projection for field {}. Incompatible data types {:?} and {:?}", field.name(), file_schema.field(mapped_idx).data_type(), field.data_type());
info!("{}", msg);
return Err(DataFusionError::Execution(msg));
}
}
}
Ok(mapped)
}
/// Re-order projected columns by index in record batch to match table schema column ordering. If the record
/// batch does not contain a column for an expected field, insert a null-valued column at the
/// required column index.
pub fn adapt_batch(
&self,
batch: RecordBatch,
projections: &[usize],
) -> Result<RecordBatch> {
let batch_rows = batch.num_rows();
let batch_schema = batch.schema();
let mut cols: Vec<ArrayRef> = Vec::with_capacity(batch.columns().len());
let batch_cols = batch.columns().to_vec();
for field_idx in projections {
let table_field = &self.table_schema.fields()[*field_idx];
if let Some((batch_idx, _name)) =
batch_schema.column_with_name(table_field.name().as_str())
{
cols.push(batch_cols[batch_idx].clone());
} else {
cols.push(new_null_array(table_field.data_type(), batch_rows))
}
}
let projected_schema = Arc::new(self.table_schema.clone().project(projections)?);
// Necessary to handle empty batches
let mut options = RecordBatchOptions::default();
options.row_count = Some(batch.num_rows());
Ok(RecordBatch::try_new_with_options(
projected_schema,
cols,
&options,
)?)
}
}
/// A helper that projects partition columns into the file record batches.
///
/// One interesting trick is the usage of a cache for the key buffers of the partition column
/// dictionaries. Indeed, the partition columns are constant, so the dictionaries that represent them
/// have all their keys equal to 0. This enables us to re-use the same "all-zero" buffer across batches,
/// which makes the space consumption of the partition columns O(batch_size) instead of O(record_count).
struct PartitionColumnProjector {
/// An Arrow buffer initialized to zeros that represents the key array of all partition
/// columns (partition columns are materialized by dictionary arrays with only one
/// value in the dictionary, thus all the keys are equal to zero).
key_buffer_cache: Option<Buffer>,
/// Mapping between the indexes in the list of partition columns and the target
/// schema. Sorted by index in the target schema so that we can iterate on it to
/// insert the partition columns in the target record batch.
projected_partition_indexes: Vec<(usize, usize)>,
/// The schema of the table once the projection was applied.
projected_schema: SchemaRef,
}
impl PartitionColumnProjector {
// Create a projector to insert the partitioning columns into batches read from files
// - projected_schema: the target schema with both file and partitioning columns
// - table_partition_cols: all the partitioning column names
fn new(projected_schema: SchemaRef, table_partition_cols: &[String]) -> Self {
let mut idx_map = HashMap::new();
for (partition_idx, partition_name) in table_partition_cols.iter().enumerate() {
if let Ok(schema_idx) = projected_schema.index_of(partition_name) {
idx_map.insert(partition_idx, schema_idx);
}
}
let mut projected_partition_indexes: Vec<_> = idx_map.into_iter().collect();
projected_partition_indexes.sort_by(|(_, a), (_, b)| a.cmp(b));
Self {
projected_partition_indexes,
key_buffer_cache: None,
projected_schema,
}
}
// Transform the batch read from the file by inserting the partitioning columns
// to the right positions as deduced from `projected_schema`
// - file_batch: batch read from the file, with internal projection applied
// - partition_values: the list of partition values, one for each partition column
fn project(
&mut self,
file_batch: RecordBatch,
partition_values: &[ScalarValue],
) -> ArrowResult<RecordBatch> {
let expected_cols =
self.projected_schema.fields().len() - self.projected_partition_indexes.len();
if file_batch.columns().len() != expected_cols {
return Err(ArrowError::SchemaError(format!(
"Unexpected batch schema from file, expected {} cols but got {}",
expected_cols,
file_batch.columns().len()
)));
}
let mut cols = file_batch.columns().to_vec();
for &(pidx, sidx) in &self.projected_partition_indexes {
cols.insert(
sidx,
create_dict_array(
&mut self.key_buffer_cache,
&partition_values[pidx],
file_batch.num_rows(),
),
)
}
RecordBatch::try_new(Arc::clone(&self.projected_schema), cols)
}
}
fn create_dict_array(
key_buffer_cache: &mut Option<Buffer>,
val: &ScalarValue,
len: usize,
) -> ArrayRef {
// build value dictionary
let dict_vals = val.to_array();
// build keys array
let sliced_key_buffer = match key_buffer_cache {
Some(buf) if buf.len() >= len * 2 => buf.slice(buf.len() - len * 2),
_ => {
let mut key_buffer_builder = UInt16BufferBuilder::new(len * 2);
key_buffer_builder.advance(len * 2); // keys are all 0
key_buffer_cache.insert(key_buffer_builder.finish()).clone()
}
};
// create data type
let data_type =
DataType::Dictionary(Box::new(DataType::UInt16), Box::new(val.get_datatype()));
debug_assert_eq!(data_type, *DEFAULT_PARTITION_COLUMN_DATATYPE);
// assemble pieces together
let mut builder = ArrayData::builder(data_type)
.len(len)
.add_buffer(sliced_key_buffer);
builder = builder.add_child_data(dict_vals.data().clone());
Arc::new(DictionaryArray::<UInt16Type>::from(
builder.build().unwrap(),
))
}
#[cfg(test)]
mod tests {
use crate::{
test::{build_table_i32, columns},
test_util::aggr_test_schema,
};
use super::*;
#[test]
fn physical_plan_config_no_projection() {
let file_schema = aggr_test_schema();
let conf = config_for_projection(
Arc::clone(&file_schema),
None,
Statistics::default(),
vec!["date".to_owned()],
);
let (proj_schema, proj_statistics) = conf.project();
assert_eq!(proj_schema.fields().len(), file_schema.fields().len() + 1);
assert_eq!(
proj_schema.field(file_schema.fields().len()).name(),
"date",
"partition columns are the last columns"
);
assert_eq!(
proj_statistics
.column_statistics
.expect("projection creates column statistics")
.len(),
file_schema.fields().len() + 1
);
// TODO implement tests for partition column statistics once implemented
let col_names = conf.projected_file_column_names();
assert_eq!(col_names, None);
let col_indices = conf.file_column_projection_indices();
assert_eq!(col_indices, None);
}
#[test]
fn physical_plan_config_with_projection() {
let file_schema = aggr_test_schema();
let conf = config_for_projection(
Arc::clone(&file_schema),
Some(vec![file_schema.fields().len(), 0]),
Statistics {
num_rows: Some(10),
// assign the column index to distinct_count to help assert
// the source statistic after the projection
column_statistics: Some(
(0..file_schema.fields().len())
.map(|i| ColumnStatistics {
distinct_count: Some(i),
..Default::default()
})
.collect(),
),
..Default::default()
},
vec!["date".to_owned()],
);
let (proj_schema, proj_statistics) = conf.project();
assert_eq!(
columns(&proj_schema),
vec!["date".to_owned(), "c1".to_owned()]
);
let proj_stat_cols = proj_statistics
.column_statistics
.expect("projection creates column statistics");
assert_eq!(proj_stat_cols.len(), 2);
// TODO implement tests for proj_stat_cols[0] once partition column
// statistics are implemented
assert_eq!(proj_stat_cols[1].distinct_count, Some(0));
let col_names = conf.projected_file_column_names();
assert_eq!(col_names, Some(vec!["c1".to_owned()]));
let col_indices = conf.file_column_projection_indices();
assert_eq!(col_indices, Some(vec![0]));
}
#[test]
fn partition_column_projector() {
let file_batch = build_table_i32(
("a", &vec![0, 1, 2]),
("b", &vec![-2, -1, 0]),
("c", &vec![10, 11, 12]),
);
let partition_cols =
vec!["year".to_owned(), "month".to_owned(), "day".to_owned()];
// create a projected schema
let conf = config_for_projection(
file_batch.schema(),
// keep all cols from file and 2 from partitioning
Some(vec![
0,
1,
2,
file_batch.schema().fields().len(),
file_batch.schema().fields().len() + 2,
]),
Statistics::default(),
partition_cols.clone(),
);
let (proj_schema, _) = conf.project();
// created a projector for that projected schema
let mut proj = PartitionColumnProjector::new(proj_schema, &partition_cols);
// project first batch
let projected_batch = proj
.project(
// file_batch is ok here because we kept all the file cols in the projection
file_batch,
&[
ScalarValue::Utf8(Some("2021".to_owned())),
ScalarValue::Utf8(Some("10".to_owned())),
ScalarValue::Utf8(Some("26".to_owned())),
],
)
.expect("Projection of partition columns into record batch failed");
let expected = vec![
"+---+----+----+------+-----+",
"| a | b | c | year | day |",
"+---+----+----+------+-----+",
"| 0 | -2 | 10 | 2021 | 26 |",
"| 1 | -1 | 11 | 2021 | 26 |",
"| 2 | 0 | 12 | 2021 | 26 |",
"+---+----+----+------+-----+",
];
crate::assert_batches_eq!(expected, &[projected_batch]);
// project another batch that is larger than the previous one
let file_batch = build_table_i32(
("a", &vec![5, 6, 7, 8, 9]),
("b", &vec![-10, -9, -8, -7, -6]),
("c", &vec![12, 13, 14, 15, 16]),
);
let projected_batch = proj
.project(
// file_batch is ok here because we kept all the file cols in the projection
file_batch,
&[
ScalarValue::Utf8(Some("2021".to_owned())),
ScalarValue::Utf8(Some("10".to_owned())),
ScalarValue::Utf8(Some("27".to_owned())),
],
)
.expect("Projection of partition columns into record batch failed");
let expected = vec![
"+---+-----+----+------+-----+",
"| a | b | c | year | day |",
"+---+-----+----+------+-----+",
"| 5 | -10 | 12 | 2021 | 27 |",
"| 6 | -9 | 13 | 2021 | 27 |",
"| 7 | -8 | 14 | 2021 | 27 |",
"| 8 | -7 | 15 | 2021 | 27 |",
"| 9 | -6 | 16 | 2021 | 27 |",
"+---+-----+----+------+-----+",
];
crate::assert_batches_eq!(expected, &[projected_batch]);
// project another batch that is smaller than the previous one
let file_batch = build_table_i32(
("a", &vec![0, 1, 3]),
("b", &vec![2, 3, 4]),
("c", &vec![4, 5, 6]),
);
let projected_batch = proj
.project(
// file_batch is ok here because we kept all the file cols in the projection
file_batch,
&[
ScalarValue::Utf8(Some("2021".to_owned())),
ScalarValue::Utf8(Some("10".to_owned())),
ScalarValue::Utf8(Some("28".to_owned())),
],
)
.expect("Projection of partition columns into record batch failed");
let expected = vec![
"+---+---+---+------+-----+",
"| a | b | c | year | day |",
"+---+---+---+------+-----+",
"| 0 | 2 | 4 | 2021 | 28 |",
"| 1 | 3 | 5 | 2021 | 28 |",
"| 3 | 4 | 6 | 2021 | 28 |",
"+---+---+---+------+-----+",
];
crate::assert_batches_eq!(expected, &[projected_batch]);
}
#[test]
fn schema_adapter_adapt_projections() {
let table_schema = Arc::new(Schema::new(vec![
Field::new("c1", DataType::Utf8, true),
Field::new("c2", DataType::Int64, true),
Field::new("c3", DataType::Int8, true),
]));
let file_schema = Schema::new(vec![
Field::new("c1", DataType::Utf8, true),
Field::new("c2", DataType::Int64, true),
]);
let file_schema_2 = Arc::new(Schema::new(vec![
Field::new("c3", DataType::Int8, true),
Field::new("c2", DataType::Int64, true),
]));
let file_schema_3 =
Arc::new(Schema::new(vec![Field::new("c3", DataType::Float32, true)]));
let adapter = SchemaAdapter::new(table_schema);
let projections1: Vec<usize> = vec![0, 1, 2];
let projections2: Vec<usize> = vec![2];
let mapped = adapter
.map_projections(&file_schema, projections1.as_slice())
.expect("mapping projections");
assert_eq!(mapped, vec![0, 1]);
let mapped = adapter
.map_projections(&file_schema, projections2.as_slice())
.expect("mapping projections");
assert!(mapped.is_empty());
let mapped = adapter
.map_projections(&file_schema_2, projections1.as_slice())
.expect("mapping projections");
assert_eq!(mapped, vec![1, 0]);
let mapped = adapter
.map_projections(&file_schema_2, projections2.as_slice())
.expect("mapping projections");
assert_eq!(mapped, vec![0]);
let mapped = adapter.map_projections(&file_schema_3, projections1.as_slice());
assert!(mapped.is_err());
}
// sets default for configs that play no role in projections
fn config_for_projection(
file_schema: SchemaRef,
projection: Option<Vec<usize>>,
statistics: Statistics,
table_partition_cols: Vec<String>,
) -> FileScanConfig {
FileScanConfig {
file_schema,
file_groups: vec![vec![]],
limit: None,
object_store_url: ObjectStoreUrl::parse("test:///").unwrap(),
projection,
statistics,
table_partition_cols,
}
}
}