-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
203 additions
and
62 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
use super::*; | ||
use arrow::bitmap::MutableBitmap; | ||
use arrow::buffer::MutableBuffer; | ||
use std::sync::Arc; | ||
|
||
impl CategoricalChunked { | ||
pub(crate) fn merge_categorical_map(&self, other: &Self) -> Arc<RevMapping> { | ||
match ( | ||
self.categorical_map.as_deref(), | ||
other.categorical_map.as_deref(), | ||
) { | ||
( | ||
Some(RevMapping::Global(l_map, l_slots, l_id)), | ||
Some(RevMapping::Global(r_map, r_slots, r_id)), | ||
) => { | ||
if l_id != r_id { | ||
panic!("The two categorical arrays are not created under the same global string cache. They cannot be merged") | ||
} | ||
let mut new_map = (*l_map).clone(); | ||
|
||
let mut offset_buf = MutableBuffer::new(); | ||
offset_buf.extend_from_slice(l_slots.offsets().as_slice()); | ||
|
||
let mut values_buf = MutableBuffer::new(); | ||
values_buf.extend_from_slice(l_slots.values().as_slice()); | ||
|
||
let validity_buf = if let Some(validity) = l_slots.validity() { | ||
let mut validity_buf = MutableBitmap::new(); | ||
let (b, offset, len) = validity.as_slice(); | ||
validity_buf.extend_from_slice(b, offset, len); | ||
Some(validity_buf) | ||
} else { | ||
None | ||
}; | ||
|
||
// Safety | ||
// all offsets are valid and the u8 data is valid utf8 | ||
let mut new_slots = unsafe { | ||
MutableUtf8Array::from_data_unchecked( | ||
DataType::Utf8.to_arrow(), | ||
offset_buf, | ||
values_buf, | ||
validity_buf, | ||
) | ||
}; | ||
|
||
for (cat, idx) in r_map.iter() { | ||
new_map.entry(*cat).or_insert_with(|| { | ||
// Safety | ||
// within bounds | ||
let str_val = unsafe { r_slots.value_unchecked(*idx as usize) }; | ||
let new_idx = new_slots.len() as u32; | ||
new_slots.push(Some(str_val)); | ||
|
||
new_idx | ||
}); | ||
} | ||
let new_rev = RevMapping::Global(new_map, new_slots.into(), *l_id); | ||
Arc::new(new_rev) | ||
} | ||
_ => { | ||
// pass for now. Still need to do some checks for local maps that are equal | ||
self.categorical_map.as_ref().unwrap().clone() | ||
} | ||
} | ||
} | ||
} |
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,70 @@ | ||
use crate::prelude::*; | ||
|
||
fn new_chunks(chunks: &mut Vec<ArrayRef>, other: &[ArrayRef], len: usize) { | ||
// replace an empty array | ||
if chunks.len() == 1 && len == 0 { | ||
*chunks = other.to_owned(); | ||
} else { | ||
chunks.extend_from_slice(other); | ||
} | ||
} | ||
|
||
impl<T> ChunkedArray<T> | ||
where | ||
T: PolarsNumericType, | ||
{ | ||
/// Append in place. | ||
pub fn append(&mut self, other: &Self) { | ||
let len = self.len(); | ||
new_chunks(&mut self.chunks, &other.chunks, len); | ||
} | ||
} | ||
|
||
impl BooleanChunked { | ||
pub fn append(&mut self, other: &Self) { | ||
let len = self.len(); | ||
new_chunks(&mut self.chunks, &other.chunks, len); | ||
} | ||
} | ||
impl Utf8Chunked { | ||
pub fn append(&mut self, other: &Self) { | ||
let len = self.len(); | ||
new_chunks(&mut self.chunks, &other.chunks, len); | ||
} | ||
} | ||
|
||
impl ListChunked { | ||
pub fn append(&mut self, other: &Self) { | ||
let len = self.len(); | ||
new_chunks(&mut self.chunks, &other.chunks, len); | ||
} | ||
} | ||
#[cfg(feature = "object")] | ||
impl<T: PolarsObject> ObjectChunked<T> { | ||
pub fn append(&mut self, other: &Self) { | ||
let len = self.len(); | ||
new_chunks(&mut self.chunks, &other.chunks, len); | ||
} | ||
} | ||
#[cfg(feature = "dtype-categorical")] | ||
impl CategoricalChunked { | ||
pub fn append(&mut self, other: &Self) { | ||
if let (Some(rev_map_l), Some(rev_map_r)) = ( | ||
self.categorical_map.as_ref(), | ||
other.categorical_map.as_ref(), | ||
) { | ||
// first assertion checks if the global string cache is equal, | ||
// the second checks if we append a slice from this array to self | ||
if !rev_map_l.same_src(rev_map_r) && !Arc::ptr_eq(rev_map_l, rev_map_r) { | ||
panic!("Appending categorical data can only be done if they are made under the same global string cache. \ | ||
Consider using a global string cache.") | ||
} | ||
|
||
let new_rev_map = self.merge_categorical_map(other); | ||
self.categorical_map = Some(new_rev_map); | ||
} | ||
|
||
let len = self.len(); | ||
new_chunks(&mut self.chunks, &other.chunks, len); | ||
} | ||
} |
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