-
Notifications
You must be signed in to change notification settings - Fork 406
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
perf: improve record batch partitioning #1396
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change | ||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|
|
@@ -26,31 +26,30 @@ | |||||||||||
//! })) | ||||||||||||
//! } | ||||||||||||
//! ``` | ||||||||||||
use super::{ | ||||||||||||
stats::{create_add, NullCounts}, | ||||||||||||
utils::{ | ||||||||||||
arrow_schema_without_partitions, next_data_path, record_batch_without_partitions, | ||||||||||||
stringified_partition_value, PartitionPath, | ||||||||||||
}, | ||||||||||||
DeltaWriter, DeltaWriterError, | ||||||||||||
}; | ||||||||||||
use crate::builder::DeltaTableBuilder; | ||||||||||||
use crate::writer::stats::apply_null_counts; | ||||||||||||
use crate::writer::utils::ShareableBuffer; | ||||||||||||
use crate::DeltaTableError; | ||||||||||||
use crate::{action::Add, storage::DeltaObjectStore, DeltaTable, DeltaTableMetaData, Schema}; | ||||||||||||
use arrow::array::{Array, UInt32Array}; | ||||||||||||
use arrow::compute::{lexicographical_partition_ranges, lexsort_to_indices, take, SortColumn}; | ||||||||||||
use arrow::datatypes::{Schema as ArrowSchema, SchemaRef as ArrowSchemaRef}; | ||||||||||||
use arrow::error::ArrowError; | ||||||||||||
use arrow::record_batch::RecordBatch; | ||||||||||||
|
||||||||||||
use std::collections::HashMap; | ||||||||||||
use std::convert::TryFrom; | ||||||||||||
use std::sync::Arc; | ||||||||||||
|
||||||||||||
use arrow_array::{ArrayRef, RecordBatch, UInt32Array}; | ||||||||||||
use arrow_ord::{partition::lexicographical_partition_ranges, sort::SortColumn}; | ||||||||||||
use arrow_row::{RowConverter, SortField}; | ||||||||||||
use arrow_schema::{ArrowError, Schema as ArrowSchema, SchemaRef as ArrowSchemaRef}; | ||||||||||||
use arrow_select::take::take; | ||||||||||||
use bytes::Bytes; | ||||||||||||
use object_store::ObjectStore; | ||||||||||||
use parquet::{arrow::ArrowWriter, errors::ParquetError}; | ||||||||||||
use parquet::{basic::Compression, file::properties::WriterProperties}; | ||||||||||||
use std::collections::HashMap; | ||||||||||||
use std::convert::TryFrom; | ||||||||||||
use std::sync::Arc; | ||||||||||||
|
||||||||||||
use super::stats::{create_add, NullCounts}; | ||||||||||||
use super::utils::{ | ||||||||||||
arrow_schema_without_partitions, next_data_path, record_batch_without_partitions, | ||||||||||||
stringified_partition_value, PartitionPath, | ||||||||||||
}; | ||||||||||||
use super::{DeltaTableError, DeltaWriter, DeltaWriterError}; | ||||||||||||
use crate::builder::DeltaTableBuilder; | ||||||||||||
use crate::writer::{stats::apply_null_counts, utils::ShareableBuffer}; | ||||||||||||
use crate::{action::Add, storage::DeltaObjectStore, DeltaTable, DeltaTableMetaData, Schema}; | ||||||||||||
|
||||||||||||
/// Writes messages to a delta lake table. | ||||||||||||
pub struct RecordBatchWriter { | ||||||||||||
|
@@ -354,24 +353,18 @@ pub(crate) fn divide_by_partition_values( | |||||||||||
|
||||||||||||
let schema = values.schema(); | ||||||||||||
|
||||||||||||
// collect all columns in order relevant for partitioning | ||||||||||||
let sort_columns = partition_columns | ||||||||||||
.clone() | ||||||||||||
.into_iter() | ||||||||||||
.map(|col| { | ||||||||||||
Ok(SortColumn { | ||||||||||||
values: values.column(schema.index_of(&col)?).clone(), | ||||||||||||
options: None, | ||||||||||||
}) | ||||||||||||
}) | ||||||||||||
let projection = partition_columns | ||||||||||||
.iter() | ||||||||||||
.map(|n| Ok(schema.index_of(n)?)) | ||||||||||||
.collect::<Result<Vec<_>, DeltaWriterError>>()?; | ||||||||||||
let sort_columns = values.project(&projection)?; | ||||||||||||
|
||||||||||||
let indices = lexsort_to_indices(sort_columns.as_slice(), None)?; | ||||||||||||
let sorted_partition_columns = sort_columns | ||||||||||||
let indices = lexsort_to_indices(sort_columns.columns()); | ||||||||||||
let sorted_partition_columns = partition_columns | ||||||||||||
.iter() | ||||||||||||
.map(|c| { | ||||||||||||
Ok(SortColumn { | ||||||||||||
values: take(c.values.as_ref(), &indices, None)?, | ||||||||||||
values: take(values.column(schema.index_of(c)?), &indices, None)?, | ||||||||||||
options: None, | ||||||||||||
}) | ||||||||||||
}) | ||||||||||||
|
@@ -410,6 +403,18 @@ pub(crate) fn divide_by_partition_values( | |||||||||||
Ok(partitions) | ||||||||||||
} | ||||||||||||
|
||||||||||||
fn lexsort_to_indices(arrays: &[ArrayRef]) -> UInt32Array { | ||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||||
let fields = arrays | ||||||||||||
.iter() | ||||||||||||
.map(|a| SortField::new(a.data_type().clone())) | ||||||||||||
.collect(); | ||||||||||||
let mut converter = RowConverter::new(fields).unwrap(); | ||||||||||||
let rows = converter.convert_columns(arrays).unwrap(); | ||||||||||||
let mut sort: Vec<_> = rows.iter().enumerate().collect(); | ||||||||||||
sort.sort_unstable_by(|(_, a), (_, b)| a.cmp(b)); | ||||||||||||
UInt32Array::from_iter_values(sort.iter().map(|(i, _)| *i as u32)) | ||||||||||||
} | ||||||||||||
|
||||||||||||
#[cfg(test)] | ||||||||||||
mod tests { | ||||||||||||
use super::*; | ||||||||||||
|
@@ -506,7 +511,6 @@ mod tests { | |||||||||||
|
||||||||||||
let mut writer = RecordBatchWriter::for_table(&table).unwrap(); | ||||||||||||
let partitions = writer.divide_by_partition_values(&batch).unwrap(); | ||||||||||||
println!("partitions: {:?}", partitions); | ||||||||||||
|
||||||||||||
let expected_keys = vec![ | ||||||||||||
String::from("modified=2021-02-01"), | ||||||||||||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I am not following why this function is being used here instead of the previous import from
arrow::compute
as far as I can tell the only difference is this version doesn't take the optionallimit
parameter?😕
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This version uses arrow-row rather then the "regular" compute kernels. https://github.com/apache/arrow-rs/blob/77aa8f5b2645a91724048f5c1d644c6b52880028/arrow-ord/src/sort.rs#L1081-L1082
the implementation is actually lifted from a comment in the docs :) https://github.com/apache/arrow-rs/blob/77aa8f5b2645a91724048f5c1d644c6b52880028/arrow-row/src/lib.rs#L105-L117