Skip to content

Commit

Permalink
feat: impl DateTime type and vector
Browse files Browse the repository at this point in the history
  • Loading branch information
v0y4g3r committed Nov 29, 2022
1 parent 8bee709 commit 17b5b72
Show file tree
Hide file tree
Showing 10 changed files with 133 additions and 34 deletions.
8 changes: 4 additions & 4 deletions src/datatypes2/src/data_type.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ use serde::{Deserialize, Serialize};
use crate::error::{self, Error, Result};
use crate::type_id::LogicalTypeId;
use crate::types::{
BinaryType, BooleanType, DateType, Float32Type, Float64Type, Int16Type, Int32Type, Int64Type,
Int8Type, UInt16Type, UInt32Type, UInt64Type, UInt8Type,
BinaryType, BooleanType, DateTimeType, DateType, Float32Type, Float64Type, Int16Type,
Int32Type, Int64Type, Int8Type, UInt16Type, UInt32Type, UInt64Type, UInt8Type,
};
use crate::value::Value;
use crate::vectors::MutableVector;
Expand Down Expand Up @@ -51,7 +51,7 @@ pub enum ConcreteDataType {

// Date types:
Date(DateType),
// DateTime(DateTimeType),
DateTime(DateTimeType),
// Timestamp(TimestampType),

// List(ListType),
Expand Down Expand Up @@ -191,7 +191,7 @@ macro_rules! impl_new_concrete_type_functions {

impl_new_concrete_type_functions!(
Boolean, UInt8, UInt16, UInt32, UInt64, Int8, Int16, Int32, Int64, Float32, Float64, Binary,
Date
Date, DateTime
);

// impl ConcreteDataType {
Expand Down
36 changes: 18 additions & 18 deletions src/datatypes2/src/scalars.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,14 @@

use std::any::Any;

use common_time::Date;
use common_time::{Date, DateTime};

use crate::types::{
Float32Type, Float64Type, Int16Type, Int32Type, Int64Type, Int8Type, UInt16Type, UInt32Type,
UInt64Type, UInt8Type,
};
use crate::vectors::{
BinaryVector, BooleanVector, DateVector, MutableVector, PrimitiveVector, Vector,
BinaryVector, BooleanVector, DateTimeVector, DateVector, MutableVector, PrimitiveVector, Vector,
};

fn get_iter_capacity<T, I: Iterator<Item = T>>(iter: &I) -> usize {
Expand Down Expand Up @@ -276,26 +276,26 @@ impl<'a> ScalarRef<'a> for Date {
}
}

// impl Scalar for DateTime {
// type VectorType = DateTimeVector;
// type RefType<'a> = DateTime;
impl Scalar for DateTime {
type VectorType = DateTimeVector;
type RefType<'a> = DateTime;

// fn as_scalar_ref(&self) -> Self::RefType<'_> {
// *self
// }
fn as_scalar_ref(&self) -> Self::RefType<'_> {
*self
}

// fn upcast_gat<'short, 'long: 'short>(long: Self::RefType<'long>) -> Self::RefType<'short> {
// long
// }
// }
fn upcast_gat<'short, 'long: 'short>(long: Self::RefType<'long>) -> Self::RefType<'short> {
long
}
}

// impl<'a> ScalarRef<'a> for DateTime {
// type ScalarType = DateTime;
impl<'a> ScalarRef<'a> for DateTime {
type ScalarType = DateTime;

// fn to_owned_scalar(&self) -> Self::ScalarType {
// *self
// }
// }
fn to_owned_scalar(&self) -> Self::ScalarType {
*self
}
}

// impl Scalar for Timestamp {
// type VectorType = TimestampVector;
Expand Down
2 changes: 2 additions & 0 deletions src/datatypes2/src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,13 @@
mod binary_type;
mod boolean_type;
mod date_type;
mod datetime_type;
mod primitive_type;

pub use binary_type::BinaryType;
pub use boolean_type::BooleanType;
pub use date_type::DateType;
pub use datetime_type::DateTimeType;
pub use primitive_type::{
Float32Type, Float64Type, Int16Type, Int32Type, Int64Type, Int8Type, LogicalPrimitiveType,
NativeType, UInt16Type, UInt32Type, UInt64Type, UInt8Type, WrapperType,
Expand Down
73 changes: 73 additions & 0 deletions src/datatypes2/src/types/datetime_type.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
use arrow::datatypes::{DataType as ArrowDataType, Date64Type};
use common_time::DateTime;
use serde::{Deserialize, Serialize};
use snafu::OptionExt;

use crate::data_type::{ConcreteDataType, DataType};
use crate::error;
use crate::prelude::{LogicalTypeId, MutableVector, ScalarVectorBuilder, Value, ValueRef, Vector};
use crate::types::LogicalPrimitiveType;
use crate::vectors::{DateTimeVector, DateTimeVectorBuilder, PrimitiveVector};

/// Data type for [`DateTime`].
#[derive(Debug, Default, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct DateTimeType;

impl DataType for DateTimeType {
fn name(&self) -> &str {
"DateTime"
}

fn logical_type_id(&self) -> LogicalTypeId {
LogicalTypeId::DateTime
}

fn default_value(&self) -> Value {
Value::DateTime(DateTime::default())
}

fn as_arrow_type(&self) -> ArrowDataType {
ArrowDataType::Date64
}

fn create_mutable_vector(&self, capacity: usize) -> Box<dyn MutableVector> {
Box::new(DateTimeVectorBuilder::with_capacity(capacity))
}
}

impl LogicalPrimitiveType for DateTimeType {
type ArrowPrimitive = Date64Type;
type Native = i64;
type Wrapper = DateTime;

fn build_data_type() -> ConcreteDataType {
ConcreteDataType::datetime_datatype()
}

fn type_name() -> &'static str {
"DateTime"
}

fn cast_vector(vector: &dyn Vector) -> crate::Result<&PrimitiveVector<Self>> {
vector
.as_any()
.downcast_ref::<DateTimeVector>()
.with_context(|| error::CastTypeSnafu {
msg: format!(
"Failed to cast {} to DateTimeVector",
vector.vector_type_name()
),
})
}

fn cast_value_ref(value: ValueRef) -> crate::Result<Option<Self::Wrapper>> {
match value {
ValueRef::Null => Ok(None),
ValueRef::DateTime(v) => Ok(Some(v)),
other => error::CastTypeSnafu {
msg: format!("Failed to cast value {:?} to DateTime", other,),
}
.fail(),
}
}
}
17 changes: 15 additions & 2 deletions src/datatypes2/src/types/primitive_type.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
use std::cmp::Ordering;

use arrow::datatypes::{ArrowNativeType, ArrowPrimitiveType, DataType as ArrowDataType};
use common_time::Date;
use common_time::{Date, DateTime};
use num::NumCast;
use serde::{Deserialize, Serialize};
use snafu::OptionExt;
Expand All @@ -24,7 +24,7 @@ use crate::data_type::{ConcreteDataType, DataType};
use crate::error::{self, Result};
use crate::scalars::{Scalar, ScalarRef, ScalarVectorBuilder};
use crate::type_id::LogicalTypeId;
use crate::types::DateType;
use crate::types::{DateTimeType, DateType};
use crate::value::{Value, ValueRef};
use crate::vectors::{MutableVector, PrimitiveVector, PrimitiveVectorBuilder, Vector};

Expand Down Expand Up @@ -168,6 +168,19 @@ impl WrapperType for Date {
}
}

impl WrapperType for DateTime {
type LogicalType = DateTimeType;
type Native = i64;

fn from_native(value: Self::Native) -> Self {
DateTime::new(value)
}

fn into_native(self) -> Self::Native {
self.val()
}
}

macro_rules! define_logical_primitive_type {
($Native: ident, $TypeId: ident, $DataType: ident) => {
#[derive(Debug, Clone, Default, PartialEq, Eq, Serialize, Deserialize)]
Expand Down
2 changes: 2 additions & 0 deletions src/datatypes2/src/vectors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ use crate::vectors::operations::VectorOp;
pub mod binary;
pub mod boolean;
pub mod date;
pub mod datetime;
mod eq;
mod helper;
pub mod operations;
Expand All @@ -37,6 +38,7 @@ pub mod primitive;
pub use binary::*;
pub use boolean::*;
pub use date::*;
pub use datetime::*;
pub use helper::Helper;
pub use primitive::*;

Expand Down
7 changes: 7 additions & 0 deletions src/datatypes2/src/vectors/datetime.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
use crate::types::DateTimeType;
use crate::vectors::{PrimitiveVector, PrimitiveVectorBuilder};

/// Vector of [`DateTime`](common_time::Date)
pub type DateTimeVector = PrimitiveVector<DateTimeType>;
/// Builder for [`DateTimeVector`].
pub type DateTimeVectorBuilder = PrimitiveVectorBuilder<DateTimeType>;
10 changes: 6 additions & 4 deletions src/datatypes2/src/vectors/eq.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@
use std::sync::Arc;

use crate::data_type::DataType;
use crate::vectors::{BinaryVector, BooleanVector, DateVector, PrimitiveVector, Vector};
use crate::vectors::{
BinaryVector, BooleanVector, DateTimeVector, DateVector, PrimitiveVector, Vector,
};
use crate::with_match_primitive_type_id;

impl Eq for dyn Vector + '_ {}
Expand Down Expand Up @@ -73,7 +75,7 @@ fn equal(lhs: &dyn Vector, rhs: &dyn Vector) -> bool {
Binary(_) => is_vector_eq!(BinaryVector, lhs, rhs),
// String(_) => is_vector_eq!(StringVector, lhs, rhs),
Date(_) => is_vector_eq!(DateVector, lhs, rhs),
// DateTime(_) => is_vector_eq!(DateTimeVector, lhs, rhs),
DateTime(_) => is_vector_eq!(DateTimeVector, lhs, rhs),
// Timestamp(_) => is_vector_eq!(TimestampVector, lhs, rhs),
// List(_) => is_vector_eq!(ListVector, lhs, rhs),
UInt8(_) | UInt16(_) | UInt32(_) | UInt64(_) | Int8(_) | Int16(_) | Int32(_) | Int64(_)
Expand Down Expand Up @@ -125,8 +127,8 @@ mod tests {
// 5,
// )));
assert_vector_ref_eq(Arc::new(BooleanVector::from(vec![true, false])));
// assert_vector_ref_eq(Arc::new(DateVector::from(vec![Some(100), Some(120)])));
// assert_vector_ref_eq(Arc::new(DateTimeVector::from(vec![Some(100), Some(120)])));
assert_vector_ref_eq(Arc::new(DateVector::from(vec![Some(100), Some(120)])));
assert_vector_ref_eq(Arc::new(DateTimeVector::from(vec![Some(100), Some(120)])));
// assert_vector_ref_eq(Arc::new(TimestampVector::from_values([100, 120])));

// let mut arrow_array = MutableListArray::<i32, MutablePrimitiveArray<i64>>::new();
Expand Down
10 changes: 5 additions & 5 deletions src/datatypes2/src/vectors/helper.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,9 @@ use snafu::OptionExt;
use crate::error::{self, Result};
use crate::scalars::Scalar;
use crate::vectors::{
BinaryVector, BooleanVector, Float32Vector, Float64Vector, Int16Vector, Int32Vector,
Int64Vector, Int8Vector, MutableVector, UInt16Vector, UInt32Vector, UInt64Vector, UInt8Vector,
Vector, VectorRef,
BinaryVector, BooleanVector, DateTimeVector, DateVector, Float32Vector, Float64Vector,
Int16Vector, Int32Vector, Int64Vector, Int8Vector, MutableVector, UInt16Vector, UInt32Vector,
UInt64Vector, UInt8Vector, Vector, VectorRef,
};

pub struct Helper;
Expand Down Expand Up @@ -195,8 +195,8 @@ impl Helper {
// ArrowDataType::Utf8 | ArrowDataType::LargeUtf8 => {
// Arc::new(StringVector::try_from_arrow_array(array)?)
// }
// ArrowDataType::Date32 => Arc::new(DateVector::try_from_arrow_array(array)?),
// ArrowDataType::Date64 => Arc::new(DateTimeVector::try_from_arrow_array(array)?),
ArrowDataType::Date32 => Arc::new(DateVector::try_from_arrow_array(array)?),
ArrowDataType::Date64 => Arc::new(DateTimeVector::try_from_arrow_array(array)?),
// ArrowDataType::List(_) => Arc::new(ListVector::try_from_arrow_array(array)?),
// ArrowDataType::Timestamp(_, _) => {
// Arc::new(TimestampVector::try_from_arrow_array(array)?)
Expand Down
2 changes: 1 addition & 1 deletion src/datatypes2/src/vectors/operations/find_unique.rs
Original file line number Diff line number Diff line change
Expand Up @@ -352,7 +352,7 @@ mod tests {
#[test]
fn test_find_unique_date_like() {
impl_find_unique_date_like_test!(DateVector, Date, new);
// impl_find_unique_date_like_test!(DateTimeVector, DateTime, new);
impl_find_unique_date_like_test!(DateTimeVector, DateTime, new);
// impl_find_unique_date_like_test!(TimestampVector, Timestamp, from_millis);
}
}

0 comments on commit 17b5b72

Please sign in to comment.