-
Notifications
You must be signed in to change notification settings - Fork 234
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
fix: copy arrays when placing them in the v2 writer's accumulation queue #2249
Merged
westonpace
merged 4 commits into
lancedb:main
from
westonpace:fix/copy-arrays-when-writing
Apr 25, 2024
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
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
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 |
---|---|---|
@@ -0,0 +1,60 @@ | ||
// SPDX-License-Identifier: Apache-2.0 | ||
// SPDX-FileCopyrightText: Copyright The Lance Authors | ||
|
||
use std::sync::Arc; | ||
|
||
use arrow_array::{make_array, Array, RecordBatch}; | ||
use arrow_buffer::{Buffer, NullBuffer}; | ||
use arrow_data::ArrayData; | ||
|
||
pub fn deep_copy_buffer(buffer: &Buffer) -> Buffer { | ||
Buffer::from(Vec::from(buffer.as_slice())) | ||
} | ||
|
||
fn deep_copy_nulls(nulls: &NullBuffer) -> Buffer { | ||
deep_copy_buffer(nulls.inner().inner()) | ||
} | ||
|
||
pub fn deep_copy_array_data(data: &ArrayData) -> ArrayData { | ||
let data_type = data.data_type().clone(); | ||
let len = data.len(); | ||
let null_count = data.null_count(); | ||
let null_bit_buffer = data.nulls().map(deep_copy_nulls); | ||
let offset = data.offset(); | ||
let buffers = data | ||
.buffers() | ||
.iter() | ||
.map(deep_copy_buffer) | ||
.collect::<Vec<_>>(); | ||
let child_data = data | ||
.child_data() | ||
.iter() | ||
.map(deep_copy_array_data) | ||
.collect::<Vec<_>>(); | ||
unsafe { | ||
ArrayData::new_unchecked( | ||
data_type, | ||
len, | ||
Some(null_count), | ||
null_bit_buffer, | ||
offset, | ||
buffers, | ||
child_data, | ||
) | ||
} | ||
} | ||
|
||
pub fn deep_copy_array(array: &dyn Array) -> Arc<dyn Array> { | ||
let data = array.to_data(); | ||
let data = deep_copy_array_data(&data); | ||
make_array(data) | ||
} | ||
|
||
pub fn deep_copy_batch(batch: &RecordBatch) -> crate::Result<RecordBatch> { | ||
let arrays = batch | ||
.columns() | ||
.iter() | ||
.map(|array| deep_copy_array(array)) | ||
.collect::<Vec<_>>(); | ||
RecordBatch::try_new(batch.schema().clone(), arrays) | ||
} |
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
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
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 |
---|---|---|
|
@@ -20,6 +20,7 @@ use arrow_buffer::{BooleanBuffer, Buffer, NullBuffer, ScalarBuffer}; | |
use arrow_schema::{DataType, IntervalUnit, TimeUnit}; | ||
use bytes::BytesMut; | ||
use futures::{future::BoxFuture, FutureExt}; | ||
use lance_arrow::deepcopy::deep_copy_array; | ||
use log::{debug, trace}; | ||
use snafu::{location, Location}; | ||
|
||
|
@@ -430,6 +431,7 @@ impl LogicalPageDecoder for PrimitiveFieldDecoder { | |
|
||
pub struct PrimitiveFieldEncoder { | ||
cache_bytes: u64, | ||
keep_original_array: bool, | ||
buffered_arrays: Vec<ArrayRef>, | ||
current_bytes: u64, | ||
encoder: Arc<dyn ArrayEncoder>, | ||
|
@@ -451,9 +453,15 @@ impl PrimitiveFieldEncoder { | |
} | ||
} | ||
|
||
pub fn try_new(cache_bytes: u64, data_type: &DataType, column_index: u32) -> Result<Self> { | ||
pub fn try_new( | ||
cache_bytes: u64, | ||
keep_original_array: bool, | ||
data_type: &DataType, | ||
column_index: u32, | ||
) -> Result<Self> { | ||
Ok(Self { | ||
cache_bytes, | ||
keep_original_array, | ||
column_index, | ||
buffered_arrays: Vec::new(), | ||
current_bytes: 0, | ||
|
@@ -463,11 +471,13 @@ impl PrimitiveFieldEncoder { | |
|
||
pub fn new_with_encoder( | ||
cache_bytes: u64, | ||
keep_original_array: bool, | ||
column_index: u32, | ||
encoder: Arc<dyn ArrayEncoder>, | ||
) -> Self { | ||
Self { | ||
cache_bytes, | ||
keep_original_array, | ||
column_index, | ||
buffered_arrays: Vec::new(), | ||
current_bytes: 0, | ||
|
@@ -502,14 +512,20 @@ impl FieldEncoder for PrimitiveFieldEncoder { | |
// Buffers data, if there is enough to write a page then we create an encode task | ||
fn maybe_encode(&mut self, array: ArrayRef) -> Result<Vec<EncodeTask>> { | ||
self.current_bytes += array.get_array_memory_size() as u64; | ||
self.buffered_arrays.push(array); | ||
if self.current_bytes > self.cache_bytes { | ||
// Push into buffered_arrays without copy since we are about to flush anyways | ||
self.buffered_arrays.push(array); | ||
Comment on lines
+516
to
+517
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. Nice 👍 |
||
debug!( | ||
"Flushing column {} page of size {} bytes (unencoded)", | ||
self.column_index, self.current_bytes | ||
); | ||
Ok(vec![self.do_flush()]) | ||
} else { | ||
if self.keep_original_array { | ||
self.buffered_arrays.push(array); | ||
} else { | ||
self.buffered_arrays.push(deep_copy_array(array.as_ref())) | ||
} | ||
trace!( | ||
"Accumulating data for column {}. Now at {} bytes", | ||
self.column_index, | ||
|
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
Oops, something went wrong.
Oops, something went wrong.
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.
Do we need to copy data here?
Can we just do
buffer.data.clone()
?https://docs.rs/arrow-buffer/51.0.0/src/arrow_buffer/buffer/immutable.rs.html#34
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.
We cannot. When the buffer is imported over FFI, the refcount of the buffer is tied to the entire batch. Thus, it can't be freed until all other columns in the same batch are also ready to be freed. By copying the buffer into Rust, we gain control over the lifetime and can free it earlier.
Like Weston said in the PR description, we can solve this by importing each column individually over FFI.