mirror of
https://github.com/GreptimeTeam/greptimedb.git
synced 2026-01-03 20:02:54 +00:00
feat: Implement BinaryType and BinaryVector
This commit is contained in:
7
src/common/src/bytes.rs
Normal file
7
src/common/src/bytes.rs
Normal 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>);
|
||||
@@ -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;
|
||||
|
||||
@@ -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;
|
||||
|
||||
|
||||
@@ -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>;
|
||||
|
||||
3
src/datatypes/src/prelude.rs
Normal file
3
src/datatypes/src/prelude.rs
Normal file
@@ -0,0 +1,3 @@
|
||||
pub use crate::data_type::{DataType, DataTypeRef};
|
||||
pub use crate::type_id::LogicalTypeId;
|
||||
pub use crate::value::Value;
|
||||
@@ -1,2 +1,3 @@
|
||||
pub mod binary_type;
|
||||
pub mod primitive_traits;
|
||||
pub mod primitive_type;
|
||||
|
||||
30
src/datatypes/src/types/binary_type.rs
Normal file
30
src/datatypes/src/types/binary_type.rs
Normal 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()
|
||||
}
|
||||
}
|
||||
@@ -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);
|
||||
|
||||
@@ -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
|
||||
|
||||
26
src/datatypes/src/vectors/binary.rs
Normal file
26
src/datatypes/src/vectors/binary.rs
Normal 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()
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user