feat: Implement BinaryType and BinaryVector

This commit is contained in:
evenyag
2022-04-24 13:45:12 +08:00
parent d20191572e
commit c9c5e69adf
10 changed files with 80 additions and 9 deletions

7
src/common/src/bytes.rs Normal file
View File

@@ -0,0 +1,7 @@
/// Bytes buffer.
#[derive(Debug, Default, Clone)]
pub struct Bytes(Vec<u8>);
/// String buffer with arbitrary encoding.
#[derive(Debug, Default, Clone)]
pub struct StringBytes(Vec<u8>);

View File

@@ -1,7 +1 @@
/// Bytes buffer.
#[derive(Debug, Default, Clone)]
pub struct Bytes(Vec<u8>);
/// String buffer with arbitrary encoding.
#[derive(Debug, Default, Clone)]
pub struct StringBytes(Vec<u8>);
pub mod bytes;

View File

@@ -4,7 +4,7 @@ use crate::type_id::LogicalTypeId;
use crate::value::Value;
/// Data type abstraction.
pub trait DataType: std::fmt::Debug {
pub trait DataType: std::fmt::Debug + Send + Sync {
/// Name of this data type.
fn name(&self) -> &str;

View File

@@ -1,6 +1,11 @@
mod data_type;
pub mod prelude;
mod schema;
pub mod type_id;
mod types;
pub mod value;
pub mod vectors;
use arrow2::array::BinaryArray;
pub type LargeBinaryArray = BinaryArray<i64>;

View File

@@ -0,0 +1,3 @@
pub use crate::data_type::{DataType, DataTypeRef};
pub use crate::type_id::LogicalTypeId;
pub use crate::value::Value;

View File

@@ -1,2 +1,3 @@
pub mod binary_type;
pub mod primitive_traits;
pub mod primitive_type;

View File

@@ -0,0 +1,30 @@
use std::sync::Arc;
use common::bytes::StringBytes;
use crate::data_type::{DataType, DataTypeRef};
use crate::type_id::LogicalTypeId;
use crate::value::Value;
#[derive(Debug, Default)]
pub struct BinaryType;
impl BinaryType {
pub fn arc() -> DataTypeRef {
Arc::new(Self)
}
}
impl DataType for BinaryType {
fn name(&self) -> &str {
"Binary"
}
fn logical_type_id(&self) -> LogicalTypeId {
LogicalTypeId::String
}
fn default_value(&self) -> Value {
StringBytes::default().into()
}
}

View File

@@ -1,4 +1,4 @@
use common::{Bytes, StringBytes};
use common::bytes::{Bytes, StringBytes};
/// Value holds a single arbitrary value of any [DataType](crate::data_type::DataType).
#[derive(Debug)]
@@ -57,3 +57,5 @@ impl_from!(Int32, i32);
impl_from!(Int64, i64);
impl_from!(Float32, f32);
impl_from!(Float64, f64);
impl_from!(String, StringBytes);
impl_from!(Binary, Bytes);

View File

@@ -1,3 +1,4 @@
pub mod binary;
pub mod primitive;
use std::any::Any;
@@ -8,6 +9,8 @@ use crate::data_type::DataTypeRef;
/// Vector of data values.
pub trait Vector: Send + Sync {
/// Returns the data type of the vector.
///
/// This may require heap allocation.
fn data_type(&self) -> DataTypeRef;
/// Returns the vector as [Any](std::any::Any) so that it can be

View File

@@ -0,0 +1,26 @@
use std::any::Any;
use crate::data_type::DataTypeRef;
use crate::types::binary_type::BinaryType;
use crate::vectors::Vector;
use crate::LargeBinaryArray;
/// Vector of binary strings.
#[derive(Debug)]
pub struct BinaryVector {
array: LargeBinaryArray,
}
impl Vector for BinaryVector {
fn data_type(&self) -> DataTypeRef {
BinaryType::arc()
}
fn as_any(&self) -> &dyn Any {
self
}
fn len(&self) -> usize {
self.array.len()
}
}