-
Notifications
You must be signed in to change notification settings - Fork 811
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
ArrayData Enumeration for Remaining Layouts #3769
Merged
Merged
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
d09da27
Add StructArrayData
tustvold 5af35ff
Add ListArrayData
tustvold 6690f40
Add DictionaryArrayData
tustvold 8d49647
Format
tustvold 5aad042
Add FixedSizeBinaryArrayData
tustvold e31e94b
Add UnionArrayData
tustvold 7e5629a
Docs
tustvold 0d7d25f
Add FixedSizeListArrayData
tustvold dceb30a
Derive Debug and Clone
tustvold 0679455
Add RunArrayData
tustvold ec9011e
Review feedback
tustvold 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 |
---|---|---|
@@ -0,0 +1,174 @@ | ||
// Licensed to the Apache Software Foundation (ASF) under one | ||
// or more contributor license agreements. See the NOTICE file | ||
// distributed with this work for additional information | ||
// regarding copyright ownership. The ASF licenses this file | ||
// to you under the Apache License, Version 2.0 (the | ||
// "License"); you may not use this file except in compliance | ||
// with the License. You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, | ||
// software distributed under the License is distributed on an | ||
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
// KIND, either express or implied. See the License for the | ||
// specific language governing permissions and limitations | ||
// under the License. | ||
|
||
use crate::data::types::DictionaryKeyType; | ||
use crate::ArrayData; | ||
use arrow_buffer::buffer::{NullBuffer, ScalarBuffer}; | ||
use arrow_buffer::ArrowNativeType; | ||
use arrow_schema::DataType; | ||
|
||
mod private { | ||
use super::*; | ||
|
||
pub trait DictionaryKeySealed { | ||
/// Downcast [`ArrayDataDictionary`] to `[DictionaryArrayData`] | ||
fn downcast_ref(data: &ArrayDataDictionary) -> Option<&DictionaryArrayData<Self>> | ||
where | ||
Self: DictionaryKey; | ||
|
||
/// Downcast [`ArrayDataDictionary`] to `[DictionaryArrayData`] | ||
fn downcast(data: ArrayDataDictionary) -> Option<DictionaryArrayData<Self>> | ||
where | ||
Self: DictionaryKey; | ||
|
||
/// Cast [`DictionaryArrayData`] to [`ArrayDataDictionary`] | ||
fn upcast(v: DictionaryArrayData<Self>) -> ArrayDataDictionary | ||
where | ||
Self: DictionaryKey; | ||
} | ||
} | ||
|
||
/// Types of dictionary key used by dictionary arrays | ||
pub trait DictionaryKey: private::DictionaryKeySealed + ArrowNativeType { | ||
const TYPE: DictionaryKeyType; | ||
} | ||
|
||
macro_rules! dictionary { | ||
($t:ty,$v:ident) => { | ||
impl DictionaryKey for $t { | ||
const TYPE: DictionaryKeyType = DictionaryKeyType::$v; | ||
} | ||
impl private::DictionaryKeySealed for $t { | ||
fn downcast_ref( | ||
data: &ArrayDataDictionary, | ||
) -> Option<&DictionaryArrayData<Self>> { | ||
match data { | ||
ArrayDataDictionary::$v(v) => Some(v), | ||
_ => None, | ||
} | ||
} | ||
|
||
fn downcast(data: ArrayDataDictionary) -> Option<DictionaryArrayData<Self>> { | ||
match data { | ||
ArrayDataDictionary::$v(v) => Some(v), | ||
_ => None, | ||
} | ||
} | ||
|
||
fn upcast(v: DictionaryArrayData<Self>) -> ArrayDataDictionary { | ||
ArrayDataDictionary::$v(v) | ||
} | ||
} | ||
}; | ||
} | ||
|
||
dictionary!(i8, Int8); | ||
dictionary!(i16, Int16); | ||
dictionary!(i32, Int32); | ||
dictionary!(i64, Int64); | ||
dictionary!(u8, UInt8); | ||
dictionary!(u16, UInt16); | ||
dictionary!(u32, UInt32); | ||
dictionary!(u64, UInt64); | ||
|
||
/// An enumeration of the types of [`DictionaryArrayData`] | ||
#[derive(Debug, Clone)] | ||
pub enum ArrayDataDictionary { | ||
Int8(DictionaryArrayData<i8>), | ||
Int16(DictionaryArrayData<i16>), | ||
Int32(DictionaryArrayData<i32>), | ||
Int64(DictionaryArrayData<i64>), | ||
UInt8(DictionaryArrayData<u8>), | ||
UInt16(DictionaryArrayData<u16>), | ||
UInt32(DictionaryArrayData<u32>), | ||
UInt64(DictionaryArrayData<u64>), | ||
} | ||
|
||
impl ArrayDataDictionary { | ||
/// Downcast this [`ArrayDataDictionary`] to the corresponding [`DictionaryArrayData`] | ||
pub fn downcast_ref<K: DictionaryKey>(&self) -> Option<&DictionaryArrayData<K>> { | ||
K::downcast_ref(self) | ||
} | ||
|
||
/// Downcast this [`ArrayDataDictionary`] to the corresponding [`DictionaryArrayData`] | ||
pub fn downcast<K: DictionaryKey>(self) -> Option<DictionaryArrayData<K>> { | ||
K::downcast(self) | ||
} | ||
} | ||
|
||
impl<K: DictionaryKey> From<DictionaryArrayData<K>> for ArrayDataDictionary { | ||
fn from(value: DictionaryArrayData<K>) -> Self { | ||
K::upcast(value) | ||
} | ||
} | ||
|
||
/// ArrayData for [dictionary arrays](https://arrow.apache.org/docs/format/Columnar.html#dictionary-encoded-layout) | ||
#[derive(Debug, Clone)] | ||
pub struct DictionaryArrayData<K: DictionaryKey> { | ||
data_type: DataType, | ||
nulls: Option<NullBuffer>, | ||
keys: ScalarBuffer<K>, | ||
child: Box<ArrayData>, | ||
} | ||
|
||
impl<K: DictionaryKey> DictionaryArrayData<K> { | ||
/// Create a new [`DictionaryArrayData`] | ||
/// | ||
/// # Safety | ||
/// | ||
/// - `data_type` must be valid for this layout | ||
/// - child must have a type matching `data_type` | ||
/// - all values in `keys` must be `0 < v < child.len()` or be a null according to `nulls` | ||
/// - `nulls` must have the same length as `child` | ||
pub unsafe fn new_unchecked( | ||
data_type: DataType, | ||
keys: ScalarBuffer<K>, | ||
nulls: Option<NullBuffer>, | ||
child: ArrayData, | ||
) -> Self { | ||
Self { | ||
data_type, | ||
nulls, | ||
keys, | ||
child: Box::new(child), | ||
} | ||
} | ||
|
||
/// Returns the null buffer if any | ||
#[inline] | ||
pub fn nulls(&self) -> Option<&NullBuffer> { | ||
self.nulls.as_ref() | ||
} | ||
|
||
/// Returns the keys | ||
#[inline] | ||
pub fn keys(&self) -> &[K] { | ||
&self.keys | ||
} | ||
|
||
/// Returns the child data | ||
#[inline] | ||
pub fn child(&self) -> &ArrayData { | ||
self.child.as_ref() | ||
} | ||
|
||
/// Returns the data type of this array | ||
#[inline] | ||
pub fn data_type(&self) -> &DataType { | ||
&self.data_type | ||
} | ||
} |
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.
The value_ prefix is redundant