-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement dictionary encoding (#136)
* Implement dictionary encoding * Chunked array dictionary encoding
- Loading branch information
1 parent
fcdf5b8
commit 6ee73b7
Showing
7 changed files
with
189 additions
and
22 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,79 @@ | ||
use std::sync::Arc; | ||
|
||
use arrow::array::{AsArray, GenericByteDictionaryBuilder, PrimitiveDictionaryBuilder}; | ||
use arrow::datatypes::{ | ||
BinaryType, ByteArrayType, Int32Type, LargeBinaryType, LargeUtf8Type, Utf8Type, | ||
}; | ||
use arrow::downcast_primitive_array; | ||
use arrow_array::{ArrayRef, ArrowPrimitiveType, GenericByteArray, PrimitiveArray}; | ||
use arrow_schema::{ArrowError, DataType, Field}; | ||
use pyo3::prelude::*; | ||
use pyo3_arrow::error::PyArrowResult; | ||
use pyo3_arrow::ffi::ArrayIterator; | ||
use pyo3_arrow::input::AnyArray; | ||
use pyo3_arrow::{PyArray, PyArrayReader}; | ||
|
||
// Note: for chunked array input, each output chunk will not necessarily have the same dictionary | ||
#[pyfunction] | ||
pub(crate) fn dictionary_encode(py: Python, array: AnyArray) -> PyArrowResult<PyObject> { | ||
match array { | ||
AnyArray::Array(array) => { | ||
let (array, _field) = array.into_inner(); | ||
let output_array = dictionary_encode_array(array)?; | ||
Ok(PyArray::from_array_ref(output_array).to_arro3(py)?) | ||
} | ||
AnyArray::Stream(stream) => { | ||
let reader = stream.into_reader()?; | ||
|
||
let existing_field = reader.field(); | ||
let output_data_type = DataType::Dictionary( | ||
Box::new(DataType::Int32), | ||
Box::new(existing_field.data_type().clone()), | ||
); | ||
let output_field = Field::new("", output_data_type, true); | ||
|
||
let iter = reader.into_iter().map(move |array| { | ||
let output_array = dictionary_encode_array(array?)?; | ||
Ok(output_array) | ||
}); | ||
Ok( | ||
PyArrayReader::new(Box::new(ArrayIterator::new(iter, output_field.into()))) | ||
.to_arro3(py)?, | ||
) | ||
} | ||
} | ||
} | ||
|
||
fn dictionary_encode_array(array: ArrayRef) -> Result<ArrayRef, ArrowError> { | ||
let array_ref = array.as_ref(); | ||
let array = downcast_primitive_array!( | ||
array_ref => { | ||
primitive_dictionary_encode(array_ref) | ||
} | ||
DataType::Utf8 => bytes_dictionary_encode(array.as_bytes::<Utf8Type>()), | ||
DataType::LargeUtf8 => bytes_dictionary_encode(array.as_bytes::<LargeUtf8Type>()), | ||
DataType::Binary => bytes_dictionary_encode(array.as_bytes::<BinaryType>()), | ||
DataType::LargeBinary => bytes_dictionary_encode(array.as_bytes::<LargeBinaryType>()), | ||
DataType::Dictionary(_, _) => array, | ||
d => return Err(ArrowError::ComputeError(format!("{d:?} not supported in rank"))) | ||
); | ||
Ok(array) | ||
} | ||
|
||
#[inline(never)] | ||
fn primitive_dictionary_encode<T: ArrowPrimitiveType>(array: &PrimitiveArray<T>) -> ArrayRef { | ||
let mut builder = PrimitiveDictionaryBuilder::<Int32Type, T>::new(); | ||
for value in array { | ||
builder.append_option(value); | ||
} | ||
Arc::new(builder.finish()) | ||
} | ||
|
||
#[inline(never)] | ||
fn bytes_dictionary_encode<T: ByteArrayType>(array: &GenericByteArray<T>) -> ArrayRef { | ||
let mut builder = GenericByteDictionaryBuilder::<Int32Type, T>::new(); | ||
for value in array { | ||
builder.append_option(value); | ||
} | ||
Arc::new(builder.finish()) | ||
} |
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,45 @@ | ||
import pyarrow as pa | ||
import pyarrow.compute as pc | ||
from arro3.core import ChunkedArray | ||
from arro3.compute import dictionary_encode | ||
|
||
|
||
def test_dictionary_encode(): | ||
arr = pa.array([1, 2, 3, 1, 2, 2, 3, 1, 1, 1], type=pa.uint16()) | ||
out = dictionary_encode(arr) | ||
out_pc = pc.dictionary_encode(arr) # type: ignore | ||
assert pa.array(out) == out_pc | ||
|
||
arr = pa.array(["1", "2", "3", "1", "2", "2", "3", "1", "1", "1"], type=pa.utf8()) | ||
out = dictionary_encode(arr) | ||
out_pc = pc.dictionary_encode(arr) # type: ignore | ||
assert pa.array(out) == out_pc | ||
|
||
arr = arr.cast(pa.large_utf8()) | ||
out = dictionary_encode(arr) | ||
out_pc = pc.dictionary_encode(arr) # type: ignore | ||
assert pa.array(out) == out_pc | ||
|
||
arr = arr.cast(pa.binary()) | ||
out = dictionary_encode(arr) | ||
out_pc = pc.dictionary_encode(arr) # type: ignore | ||
assert pa.array(out) == out_pc | ||
|
||
arr = arr.cast(pa.large_binary()) | ||
out = dictionary_encode(arr) | ||
out_pc = pc.dictionary_encode(arr) # type: ignore | ||
assert pa.array(out) == out_pc | ||
|
||
|
||
def test_dictionary_encode_chunked(): | ||
arr = pa.chunked_array([[3, 2, 3], [1, 2, 2], [3, 1, 1, 1]], type=pa.uint16()) | ||
out = ChunkedArray(dictionary_encode(arr)) | ||
|
||
out_retour = pa.chunked_array(out) | ||
out_pc = pc.dictionary_encode(arr) # type: ignore | ||
|
||
# Since these arrays have different dictionaries, array and arrow scalar comparison | ||
# will fail. | ||
assert len(out_retour) == len(out_pc) | ||
for i in range(len(out_retour)): | ||
assert out_retour[i].as_py() == out_pc[i].as_py() |