feat: Allow DataType/Vector converting into arrow's type

This commit is contained in:
evenyag
2022-04-25 10:42:16 +08:00
parent 445fd75712
commit 7a015d4f2a
6 changed files with 33 additions and 0 deletions

View File

@@ -1,5 +1,7 @@
use std::sync::Arc;
use arrow2::datatypes::DataType as ArrowDataType;
use crate::type_id::LogicalTypeId;
use crate::value::Value;
@@ -13,6 +15,9 @@ pub trait DataType: std::fmt::Debug + Send + Sync {
/// Returns the default value of this type.
fn default_value(&self) -> Value;
/// Convert this type as [arrow2::datatypes::DataType].
fn as_arrow_type(&self) -> ArrowDataType;
}
pub type DataTypeRef = Arc<dyn DataType>;

View File

@@ -1,5 +1,6 @@
use std::sync::Arc;
use arrow2::datatypes::DataType as ArrowDataType;
use common::bytes::StringBytes;
use crate::data_type::{DataType, DataTypeRef};
@@ -27,4 +28,8 @@ impl DataType for BinaryType {
fn default_value(&self) -> Value {
StringBytes::default().into()
}
fn as_arrow_type(&self) -> ArrowDataType {
ArrowDataType::LargeBinary
}
}

View File

@@ -1,6 +1,8 @@
use std::marker::PhantomData;
use std::sync::Arc;
use arrow2::datatypes::DataType as ArrowDataType;
use crate::data_type::{DataType, DataTypeRef};
use crate::type_id::LogicalTypeId;
use crate::types::primitive_traits::Primitive;
@@ -49,6 +51,10 @@ macro_rules! impl_numeric {
fn default_value(&self) -> Value {
$Type::default().into()
}
fn as_arrow_type(&self) -> ArrowDataType {
ArrowDataType::$TypeId
}
}
impl std::fmt::Debug for PrimitiveType<$Type> {

View File

@@ -4,6 +4,8 @@ pub mod primitive;
use std::any::Any;
use std::sync::Arc;
use arrow2::array::ArrayRef;
use crate::data_type::DataTypeRef;
/// Vector of data values.
@@ -24,6 +26,9 @@ pub trait Vector: Send + Sync {
fn is_empty(&self) -> bool {
self.len() == 0
}
/// Convert this vector to a new arrow [ArrayRef].
fn to_arrow_array(&self) -> ArrayRef;
}
pub type VectorRef = Arc<dyn Vector>;

View File

@@ -1,5 +1,7 @@
use std::any::Any;
use std::sync::Arc;
use arrow2::array::ArrayRef;
use arrow2::array::BinaryValueIter;
use arrow2::bitmap::utils::ZipValidity;
@@ -27,6 +29,10 @@ impl Vector for BinaryVector {
fn len(&self) -> usize {
self.array.len()
}
fn to_arrow_array(&self) -> ArrayRef {
Arc::new(self.array.clone())
}
}
impl ScalarVector for BinaryVector {

View File

@@ -1,6 +1,8 @@
use std::any::Any;
use std::slice::Iter;
use std::sync::Arc;
use arrow2::array::ArrayRef;
use arrow2::array::{MutablePrimitiveArray, PrimitiveArray};
use arrow2::bitmap::utils::ZipValidity;
@@ -33,6 +35,10 @@ impl<T: Primitive + CreateDataType> Vector for PrimitiveVector<T> {
fn len(&self) -> usize {
self.array.len()
}
fn to_arrow_array(&self) -> ArrayRef {
Arc::new(self.array.clone())
}
}
impl<T: Primitive + CreateDataType> ScalarVector for PrimitiveVector<T> {