Skip to content
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

Optimize bulk writing of all blocks of bloom filter #3340

Merged
merged 1 commit into from
Dec 13, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 9 additions & 6 deletions parquet/src/bloom_filter/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ use crate::format::{
};
use bytes::{Buf, Bytes};
use std::hash::Hasher;
use std::io::{BufWriter, Write};
use std::io::Write;
use std::sync::Arc;
use thrift::protocol::{
TCompactInputProtocol, TCompactOutputProtocol, TOutputProtocol, TSerializable,
Expand Down Expand Up @@ -176,18 +176,17 @@ impl Sbbf {
Self(data)
}

/// Write the bloom filter data (header and then bitset) to the output
pub(crate) fn write<W: Write>(&self, writer: W) -> Result<(), ParquetError> {
// Use a BufWriter to avoid costs of writing individual blocks
let mut writer = BufWriter::new(writer);
/// Write the bloom filter data (header and then bitset) to the output. This doesn't
/// flush the writer in order to boost performance of bulk writing all blocks. Caller
/// must remember to flush the writer.
pub(crate) fn write<W: Write>(&self, mut writer: W) -> Result<(), ParquetError> {
let mut protocol = TCompactOutputProtocol::new(&mut writer);
let header = self.header();
header.write_to_out_protocol(&mut protocol).map_err(|e| {
ParquetError::General(format!("Could not write bloom filter header: {}", e))
})?;
protocol.flush()?;
self.write_bitset(&mut writer)?;
writer.flush()?;
Ok(())
}

Expand Down Expand Up @@ -288,6 +287,10 @@ impl Sbbf {
let block = &self.0[block_index];
block_check(block, hash as u32)
}

pub(crate) fn block_num(&self) -> usize {
self.0.len()
}
}

// per spec we use xxHash with seed=0
Expand Down
9 changes: 7 additions & 2 deletions parquet/src/file/writer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
use crate::bloom_filter::Sbbf;
use crate::format as parquet;
use crate::format::{ColumnIndex, OffsetIndex, RowGroup};
use std::io::BufWriter;
use std::{io::Write, sync::Arc};
use thrift::protocol::{TCompactOutputProtocol, TOutputProtocol, TSerializable};

Expand Down Expand Up @@ -225,23 +226,27 @@ impl<W: Write> SerializedFileWriter<W> {
// iter row group
// iter each column
// write bloom filter to the file
let mut start_offset = self.buf.bytes_written();
let mut writer = BufWriter::new(&mut self.buf);

for (row_group_idx, row_group) in row_groups.iter_mut().enumerate() {
for (column_idx, column_chunk) in row_group.columns.iter_mut().enumerate() {
match &self.bloom_filters[row_group_idx][column_idx] {
Some(bloom_filter) => {
let start_offset = self.buf.bytes_written();
bloom_filter.write(&mut self.buf)?;
bloom_filter.write(&mut writer)?;
// set offset and index for bloom filter
column_chunk
.meta_data
.as_mut()
.expect("can't have bloom filter without column metadata")
.bloom_filter_offset = Some(start_offset as i64);
start_offset += bloom_filter.block_num() * 32;
}
None => {}
}
}
}
writer.flush()?;
Ok(())
}

Expand Down