Skip to content
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

feat: Implements DateType and DateVector #651

Merged
merged 4 commits into from
Nov 29, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
293 changes: 146 additions & 147 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, Float32Type, Float64Type, Int16Type, Int32Type, Int64Type, Int8Type,
UInt16Type, UInt32Type, UInt64Type, UInt8Type,
BinaryType, BooleanType, DateType, Float32Type, Float64Type, Int16Type, Int32Type, Int64Type,
Int8Type, UInt16Type, UInt32Type, UInt64Type, UInt8Type,
};
use crate::value::Value;
use crate::vectors::MutableVector;
Expand All @@ -45,17 +45,19 @@ pub enum ConcreteDataType {
Float32(Float32Type),
Float64(Float64Type),

// String types
// String types:
Binary(BinaryType),
// String(StringType),

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

// List(ListType),
}

// TODO(yingwen): Consider moving these methods to the DataType trait.
impl ConcreteDataType {
// pub fn is_float(&self) -> bool {
// matches!(
Expand Down Expand Up @@ -143,17 +145,17 @@ impl TryFrom<&ArrowDataType> for ConcreteDataType {
let concrete_type = match dt {
// ArrowDataType::Null => Self::null_datatype(),
ArrowDataType::Boolean => Self::boolean_datatype(),
// ArrowDataType::UInt8 => Self::uint8_datatype(),
// ArrowDataType::UInt16 => Self::uint16_datatype(),
// ArrowDataType::UInt32 => Self::uint32_datatype(),
// ArrowDataType::UInt64 => Self::uint64_datatype(),
// ArrowDataType::Int8 => Self::int8_datatype(),
// ArrowDataType::Int16 => Self::int16_datatype(),
// ArrowDataType::Int32 => Self::int32_datatype(),
// ArrowDataType::Int64 => Self::int64_datatype(),
// ArrowDataType::Float32 => Self::float32_datatype(),
// ArrowDataType::Float64 => Self::float64_datatype(),
// ArrowDataType::Date32 => Self::date_datatype(),
ArrowDataType::UInt8 => Self::uint8_datatype(),
ArrowDataType::UInt16 => Self::uint16_datatype(),
ArrowDataType::UInt32 => Self::uint32_datatype(),
ArrowDataType::UInt64 => Self::uint64_datatype(),
ArrowDataType::Int8 => Self::int8_datatype(),
ArrowDataType::Int16 => Self::int16_datatype(),
ArrowDataType::Int32 => Self::int32_datatype(),
ArrowDataType::Int64 => Self::int64_datatype(),
ArrowDataType::Float32 => Self::float32_datatype(),
ArrowDataType::Float64 => Self::float64_datatype(),
ArrowDataType::Date32 => Self::date_datatype(),
// ArrowDataType::Date64 => Self::datetime_datatype(),
// ArrowDataType::Timestamp(u, _) => ConcreteDataType::from_arrow_time_unit(u),
ArrowDataType::Binary | ArrowDataType::LargeBinary => Self::binary_datatype(),
Expand Down Expand Up @@ -187,12 +189,10 @@ macro_rules! impl_new_concrete_type_functions {
}
}

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

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

// impl ConcreteDataType {
// pub fn list_datatype(inner_type: ConcreteDataType) -> ConcreteDataType {
Expand Down Expand Up @@ -246,135 +246,134 @@ pub trait DataType: std::fmt::Debug + Send + Sync {

pub type DataTypeRef = Arc<dyn DataType>;

// #[cfg(test)]
// mod tests {
// use arrow::datatypes::Field;

// use super::*;
// TODO(yingwen): Pass all tests.
#[cfg(test)]
mod tests {
use super::*;

// #[test]
// fn test_concrete_type_as_datatype_trait() {
// let concrete_type = ConcreteDataType::boolean_datatype();
#[test]
fn test_concrete_type_as_datatype_trait() {
let concrete_type = ConcreteDataType::boolean_datatype();

// assert_eq!("Boolean", concrete_type.name());
// assert_eq!(Value::Boolean(false), concrete_type.default_value());
// assert_eq!(LogicalTypeId::Boolean, concrete_type.logical_type_id());
// assert_eq!(ArrowDataType::Boolean, concrete_type.as_arrow_type());
// }
assert_eq!("Boolean", concrete_type.name());
assert_eq!(Value::Boolean(false), concrete_type.default_value());
assert_eq!(LogicalTypeId::Boolean, concrete_type.logical_type_id());
assert_eq!(ArrowDataType::Boolean, concrete_type.as_arrow_type());
}

// #[test]
// fn test_from_arrow_type() {
// assert!(matches!(
// ConcreteDataType::from_arrow_type(&ArrowDataType::Null),
// ConcreteDataType::Null(_)
// ));
// assert!(matches!(
// ConcreteDataType::from_arrow_type(&ArrowDataType::Boolean),
// ConcreteDataType::Boolean(_)
// ));
// assert!(matches!(
// ConcreteDataType::from_arrow_type(&ArrowDataType::Binary),
// ConcreteDataType::Binary(_)
// ));
// assert!(matches!(
// ConcreteDataType::from_arrow_type(&ArrowDataType::LargeBinary),
// ConcreteDataType::Binary(_)
// ));
// assert!(matches!(
// ConcreteDataType::from_arrow_type(&ArrowDataType::Int8),
// ConcreteDataType::Int8(_)
// ));
// assert!(matches!(
// ConcreteDataType::from_arrow_type(&ArrowDataType::Int16),
// ConcreteDataType::Int16(_)
// ));
// assert!(matches!(
// ConcreteDataType::from_arrow_type(&ArrowDataType::Int32),
// ConcreteDataType::Int32(_)
// ));
// assert!(matches!(
// ConcreteDataType::from_arrow_type(&ArrowDataType::Int64),
// ConcreteDataType::Int64(_)
// ));
// assert!(matches!(
// ConcreteDataType::from_arrow_type(&ArrowDataType::UInt8),
// ConcreteDataType::UInt8(_)
// ));
// assert!(matches!(
// ConcreteDataType::from_arrow_type(&ArrowDataType::UInt16),
// ConcreteDataType::UInt16(_)
// ));
// assert!(matches!(
// ConcreteDataType::from_arrow_type(&ArrowDataType::UInt32),
// ConcreteDataType::UInt32(_)
// ));
// assert!(matches!(
// ConcreteDataType::from_arrow_type(&ArrowDataType::UInt64),
// ConcreteDataType::UInt64(_)
// ));
// assert!(matches!(
// ConcreteDataType::from_arrow_type(&ArrowDataType::Float32),
// ConcreteDataType::Float32(_)
// ));
// assert!(matches!(
// ConcreteDataType::from_arrow_type(&ArrowDataType::Float64),
// ConcreteDataType::Float64(_)
// ));
// assert!(matches!(
// ConcreteDataType::from_arrow_type(&ArrowDataType::Utf8),
// ConcreteDataType::String(_)
// ));
// assert!(matches!(
// ConcreteDataType::from_arrow_type(&ArrowDataType::Utf8),
// ConcreteDataType::String(_)
// ));
// assert_eq!(
// ConcreteDataType::from_arrow_type(&ArrowDataType::List(Box::new(Field::new(
// "item",
// ArrowDataType::Int32,
// true,
// )))),
// ConcreteDataType::List(ListType::new(ConcreteDataType::int32_datatype()))
// );
// assert!(matches!(
// ConcreteDataType::from_arrow_type(&ArrowDataType::Date32),
// ConcreteDataType::Date(_)
// ));
// }
#[test]
fn test_from_arrow_type() {
// assert!(matches!(
// ConcreteDataType::from_arrow_type(&ArrowDataType::Null),
// ConcreteDataType::Null(_)
// ));
assert!(matches!(
ConcreteDataType::from_arrow_type(&ArrowDataType::Boolean),
ConcreteDataType::Boolean(_)
));
assert!(matches!(
ConcreteDataType::from_arrow_type(&ArrowDataType::Binary),
ConcreteDataType::Binary(_)
));
assert!(matches!(
ConcreteDataType::from_arrow_type(&ArrowDataType::LargeBinary),
ConcreteDataType::Binary(_)
));
assert!(matches!(
ConcreteDataType::from_arrow_type(&ArrowDataType::Int8),
ConcreteDataType::Int8(_)
));
assert!(matches!(
ConcreteDataType::from_arrow_type(&ArrowDataType::Int16),
ConcreteDataType::Int16(_)
));
assert!(matches!(
ConcreteDataType::from_arrow_type(&ArrowDataType::Int32),
ConcreteDataType::Int32(_)
));
assert!(matches!(
ConcreteDataType::from_arrow_type(&ArrowDataType::Int64),
ConcreteDataType::Int64(_)
));
assert!(matches!(
ConcreteDataType::from_arrow_type(&ArrowDataType::UInt8),
ConcreteDataType::UInt8(_)
));
assert!(matches!(
ConcreteDataType::from_arrow_type(&ArrowDataType::UInt16),
ConcreteDataType::UInt16(_)
));
assert!(matches!(
ConcreteDataType::from_arrow_type(&ArrowDataType::UInt32),
ConcreteDataType::UInt32(_)
));
assert!(matches!(
ConcreteDataType::from_arrow_type(&ArrowDataType::UInt64),
ConcreteDataType::UInt64(_)
));
assert!(matches!(
ConcreteDataType::from_arrow_type(&ArrowDataType::Float32),
ConcreteDataType::Float32(_)
));
assert!(matches!(
ConcreteDataType::from_arrow_type(&ArrowDataType::Float64),
ConcreteDataType::Float64(_)
));
// assert!(matches!(
// ConcreteDataType::from_arrow_type(&ArrowDataType::Utf8),
// ConcreteDataType::String(_)
// ));
// assert!(matches!(
// ConcreteDataType::from_arrow_type(&ArrowDataType::Utf8),
// ConcreteDataType::String(_)
// ));
// assert_eq!(
// ConcreteDataType::from_arrow_type(&ArrowDataType::List(Box::new(Field::new(
// "item",
// ArrowDataType::Int32,
// true,
// )))),
// ConcreteDataType::List(ListType::new(ConcreteDataType::int32_datatype()))
// );
assert!(matches!(
ConcreteDataType::from_arrow_type(&ArrowDataType::Date32),
ConcreteDataType::Date(_)
));
}

// #[test]
// fn test_from_arrow_timestamp() {
// assert_eq!(
// ConcreteDataType::timestamp_millis_datatype(),
// ConcreteDataType::from_arrow_time_unit(&arrow::datatypes::TimeUnit::Millisecond)
// );
// assert_eq!(
// ConcreteDataType::timestamp_datatype(TimeUnit::Microsecond),
// ConcreteDataType::from_arrow_time_unit(&arrow::datatypes::TimeUnit::Microsecond)
// );
// assert_eq!(
// ConcreteDataType::timestamp_datatype(TimeUnit::Nanosecond),
// ConcreteDataType::from_arrow_time_unit(&arrow::datatypes::TimeUnit::Nanosecond)
// );
// assert_eq!(
// ConcreteDataType::timestamp_datatype(TimeUnit::Second),
// ConcreteDataType::from_arrow_time_unit(&arrow::datatypes::TimeUnit::Second)
// );
// }
// #[test]
// fn test_from_arrow_timestamp() {
// assert_eq!(
// ConcreteDataType::timestamp_millis_datatype(),
// ConcreteDataType::from_arrow_time_unit(&arrow::datatypes::TimeUnit::Millisecond)
// );
// assert_eq!(
// ConcreteDataType::timestamp_datatype(TimeUnit::Microsecond),
// ConcreteDataType::from_arrow_time_unit(&arrow::datatypes::TimeUnit::Microsecond)
// );
// assert_eq!(
// ConcreteDataType::timestamp_datatype(TimeUnit::Nanosecond),
// ConcreteDataType::from_arrow_time_unit(&arrow::datatypes::TimeUnit::Nanosecond)
// );
// assert_eq!(
// ConcreteDataType::timestamp_datatype(TimeUnit::Second),
// ConcreteDataType::from_arrow_time_unit(&arrow::datatypes::TimeUnit::Second)
// );
// }

// #[test]
// fn test_is_timestamp() {
// assert!(ConcreteDataType::timestamp_millis_datatype().is_timestamp());
// assert!(ConcreteDataType::timestamp_datatype(TimeUnit::Second).is_timestamp());
// assert!(ConcreteDataType::timestamp_datatype(TimeUnit::Millisecond).is_timestamp());
// assert!(ConcreteDataType::timestamp_datatype(TimeUnit::Microsecond).is_timestamp());
// assert!(ConcreteDataType::timestamp_datatype(TimeUnit::Nanosecond).is_timestamp());
// assert!(ConcreteDataType::int64_datatype().is_timestamp());
// }
// #[test]
// fn test_is_timestamp() {
// assert!(ConcreteDataType::timestamp_millis_datatype().is_timestamp());
// assert!(ConcreteDataType::timestamp_datatype(TimeUnit::Second).is_timestamp());
// assert!(ConcreteDataType::timestamp_datatype(TimeUnit::Millisecond).is_timestamp());
// assert!(ConcreteDataType::timestamp_datatype(TimeUnit::Microsecond).is_timestamp());
// assert!(ConcreteDataType::timestamp_datatype(TimeUnit::Nanosecond).is_timestamp());
// assert!(ConcreteDataType::int64_datatype().is_timestamp());
// }

// #[test]
// fn test_is_null() {
// assert!(ConcreteDataType::null_datatype().is_null());
// assert!(!ConcreteDataType::int32_datatype().is_null());
// }
// }
// #[test]
// fn test_is_null() {
// assert!(ConcreteDataType::null_datatype().is_null());
// assert!(!ConcreteDataType::int32_datatype().is_null());
// }
}
Loading