Skip to content

Commit

Permalink
chore: rename cast to try_cast
Browse files Browse the repository at this point in the history
  • Loading branch information
QuenKar committed Sep 18, 2023
1 parent 35ae27e commit 9b08af3
Show file tree
Hide file tree
Showing 13 changed files with 42 additions and 40 deletions.
2 changes: 1 addition & 1 deletion src/datatypes/src/data_type.rs
Original file line number Diff line number Diff line change
Expand Up @@ -408,7 +408,7 @@ pub trait DataType: std::fmt::Debug + Send + Sync {

/// Casts the value to this DataType.
/// Return None if cast failed.
fn cast(&self, from: Value) -> Option<Value>;
fn try_cast(&self, from: Value) -> Option<Value>;
}

pub type DataTypeRef = Arc<dyn DataType>;
Expand Down
2 changes: 1 addition & 1 deletion src/datatypes/src/types/binary_type.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ impl DataType for BinaryType {
false
}

fn cast(&self, from: Value) -> Option<Value> {
fn try_cast(&self, from: Value) -> Option<Value> {
match from {
Value::Binary(v) => Some(Value::Binary(v)),
_ => None,
Expand Down
6 changes: 3 additions & 3 deletions src/datatypes/src/types/boolean_type.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ impl DataType for BooleanType {
false
}

fn cast(&self, from: Value) -> Option<Value> {
fn try_cast(&self, from: Value) -> Option<Value> {
match from {
Value::Boolean(v) => Some(Value::Boolean(v)),
Value::UInt8(v) => numeric_to_bool(v),
Expand Down Expand Up @@ -110,15 +110,15 @@ mod tests {
macro_rules! test_cast_to_bool {
($value: expr, $expected: expr) => {
let val = $value;
let b = ConcreteDataType::boolean_datatype().cast(val).unwrap();
let b = ConcreteDataType::boolean_datatype().try_cast(val).unwrap();
assert_eq!(b, Value::Boolean($expected));
};
}

macro_rules! test_cast_from_bool {
($value: expr, $datatype: expr, $expected: expr) => {
let val = Value::Boolean($value);
let b = $datatype.cast(val).unwrap();
let b = $datatype.try_cast(val).unwrap();
assert_eq!(b, $expected);
};
}
Expand Down
12 changes: 6 additions & 6 deletions src/datatypes/src/types/date_type.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ impl DataType for DateType {
false
}

fn cast(&self, from: Value) -> Option<Value> {
fn try_cast(&self, from: Value) -> Option<Value> {
match from {
Value::Int32(v) => Some(Value::Date(Date::from(v))),
Value::String(v) => Date::from_str(v.as_utf8()).map(Value::Date).ok(),
Expand Down Expand Up @@ -113,26 +113,26 @@ mod tests {
std::env::set_var("TZ", "Asia/Shanghai");
// timestamp -> date
let ts = Value::Timestamp(Timestamp::from_str("2000-01-01 08:00:01").unwrap());
let date = ConcreteDataType::date_datatype().cast(ts).unwrap();
let date = ConcreteDataType::date_datatype().try_cast(ts).unwrap();
assert_eq!(date, Value::Date(Date::from_str("2000-01-01").unwrap()));

// this case bind with local timezone.
let ts = Value::Timestamp(Timestamp::from_str("2000-01-02 07:59:59").unwrap());
let date = ConcreteDataType::date_datatype().cast(ts).unwrap();
let date = ConcreteDataType::date_datatype().try_cast(ts).unwrap();
assert_eq!(date, Value::Date(Date::from_str("2000-01-01").unwrap()));

// Int32 -> date
let val = Value::Int32(0);
let date = ConcreteDataType::date_datatype().cast(val).unwrap();
let date = ConcreteDataType::date_datatype().try_cast(val).unwrap();
assert_eq!(date, Value::Date(Date::from_str("1970-01-01").unwrap()));

let val = Value::Int32(19614);
let date = ConcreteDataType::date_datatype().cast(val).unwrap();
let date = ConcreteDataType::date_datatype().try_cast(val).unwrap();
assert_eq!(date, Value::Date(Date::from_str("2023-09-14").unwrap()));

// String -> date
let s = Value::String(StringBytes::from("1970-02-12"));
let date = ConcreteDataType::date_datatype().cast(s).unwrap();
let date = ConcreteDataType::date_datatype().try_cast(s).unwrap();
assert_eq!(date, Value::Date(Date::from_str("1970-02-12").unwrap()));
}
}
8 changes: 4 additions & 4 deletions src/datatypes/src/types/datetime_type.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ impl DataType for DateTimeType {
false
}

fn cast(&self, from: Value) -> Option<Value> {
fn try_cast(&self, from: Value) -> Option<Value> {
match from {
Value::Int64(v) => Some(Value::DateTime(DateTime::from(v))),
Value::Timestamp(v) => v.to_chrono_datetime().map(|d| Value::DateTime(d.into())),
Expand Down Expand Up @@ -113,21 +113,21 @@ mod tests {
fn test_datetime_cast() {
// cast from Int64
let val = Value::Int64(1000);
let dt = ConcreteDataType::datetime_datatype().cast(val).unwrap();
let dt = ConcreteDataType::datetime_datatype().try_cast(val).unwrap();
assert_eq!(dt, Value::DateTime(DateTime::from(1000)));

// cast from String
std::env::set_var("TZ", "Asia/Shanghai");
let val = Value::String("1970-01-01 00:00:00+0800".into());
let dt = ConcreteDataType::datetime_datatype().cast(val).unwrap();
let dt = ConcreteDataType::datetime_datatype().try_cast(val).unwrap();
assert_eq!(
dt,
Value::DateTime(DateTime::from_str("1970-01-01 00:00:00+0800").unwrap())
);

// cast from Timestamp
let val = Value::Timestamp(Timestamp::from_str("2020-09-08 21:42:29.042+0800").unwrap());
let dt = ConcreteDataType::datetime_datatype().cast(val).unwrap();
let dt = ConcreteDataType::datetime_datatype().try_cast(val).unwrap();
assert_eq!(
dt,
Value::DateTime(DateTime::from_str("2020-09-08 21:42:29+0800").unwrap())
Expand Down
2 changes: 1 addition & 1 deletion src/datatypes/src/types/dictionary_type.rs
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ impl DataType for DictionaryType {
false
}

fn cast(&self, _: Value) -> Option<Value> {
fn try_cast(&self, _: Value) -> Option<Value> {
unimplemented!()
}
}
2 changes: 1 addition & 1 deletion src/datatypes/src/types/interval_type.rs
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ macro_rules! impl_data_type_for_interval {
false
}

fn cast(&self, _: Value) -> Option<Value> {
fn try_cast(&self, _: Value) -> Option<Value> {
unimplemented!("interval type cast not implemented yet")
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/datatypes/src/types/list_type.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ impl DataType for ListType {
false
}

fn cast(&self, from: Value) -> Option<Value> {
fn try_cast(&self, from: Value) -> Option<Value> {
match from {
Value::List(v) => Some(Value::List(v)),
_ => None,
Expand Down
2 changes: 1 addition & 1 deletion src/datatypes/src/types/null_type.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ impl DataType for NullType {
}

// Unconditional cast other type to Value::Null
fn cast(&self, _from: Value) -> Option<Value> {
fn try_cast(&self, _from: Value) -> Option<Value> {
Some(Value::Null)
}
}
8 changes: 4 additions & 4 deletions src/datatypes/src/types/primitive_type.rs
Original file line number Diff line number Diff line change
Expand Up @@ -275,7 +275,7 @@ macro_rules! define_non_timestamp_primitive {
false
}

fn cast(&self, from: Value) -> Option<Value> {
fn try_cast(&self, from: Value) -> Option<Value> {
match from {
Value::Boolean(v) => bool_to_numeric(v).map(Value::$TypeId),
Value::String(v) => v.as_utf8().parse::<$Native>().map(|val| Value::from(val)).ok(),
Expand Down Expand Up @@ -362,7 +362,7 @@ impl DataType for Int64Type {
true
}

fn cast(&self, from: Value) -> Option<Value> {
fn try_cast(&self, from: Value) -> Option<Value> {
match from {
Value::Boolean(v) => bool_to_numeric(v).map(Value::Int64),
Value::Int8(v) => num::cast::cast(v).map(Value::Int64),
Expand Down Expand Up @@ -420,7 +420,7 @@ impl DataType for Int32Type {
false
}

fn cast(&self, from: Value) -> Option<Value> {
fn try_cast(&self, from: Value) -> Option<Value> {
match from {
Value::Boolean(v) => bool_to_numeric(v).map(Value::Int32),
Value::Int8(v) => num::cast::cast(v).map(Value::Int32),
Expand Down Expand Up @@ -494,7 +494,7 @@ mod tests {
macro_rules! assert_primitive_cast {
($value: expr, $datatype:expr, $expected: expr) => {
let val = $value;
let b = $datatype.cast(val).unwrap();
let b = $datatype.try_cast(val).unwrap();
assert_eq!(b, $expected);
};
}
Expand Down
2 changes: 1 addition & 1 deletion src/datatypes/src/types/string_type.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ impl DataType for StringType {
false
}

fn cast(&self, from: Value) -> Option<Value> {
fn try_cast(&self, from: Value) -> Option<Value> {
if from.logical_type_id() == self.logical_type_id() {
return Some(from);
}
Expand Down
20 changes: 11 additions & 9 deletions src/datatypes/src/types/time_type.rs
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ macro_rules! impl_data_type_for_time {
false
}

fn cast(&self, from: Value) -> Option<Value> {
fn try_cast(&self, from: Value) -> Option<Value> {
match from {
Value::$TargetType(v) => Some(Value::Time(Time::new(v as i64, TimeUnit::$unit))),
Value::Time(v) => v.convert_to(TimeUnit::$unit).map(Value::Time),
Expand Down Expand Up @@ -230,44 +230,46 @@ mod tests {
fn test_time_cast() {
// Int32 -> TimeSecondType
let val = Value::Int32(1000);
let time = ConcreteDataType::time_second_datatype().cast(val).unwrap();
let time = ConcreteDataType::time_second_datatype()
.try_cast(val)
.unwrap();
assert_eq!(time, Value::Time(Time::new_second(1000)));

// Int32 -> TimeMillisecondType
let val = Value::Int32(2000);
let time = ConcreteDataType::time_millisecond_datatype()
.cast(val)
.try_cast(val)
.unwrap();
assert_eq!(time, Value::Time(Time::new_millisecond(2000)));

// Int64 -> TimeMicrosecondType
let val = Value::Int64(3000);
let time = ConcreteDataType::time_microsecond_datatype()
.cast(val)
.try_cast(val)
.unwrap();
assert_eq!(time, Value::Time(Time::new_microsecond(3000)));

// Int64 -> TimeNanosecondType
let val = Value::Int64(4000);
let time = ConcreteDataType::time_nanosecond_datatype()
.cast(val)
.try_cast(val)
.unwrap();
assert_eq!(time, Value::Time(Time::new_nanosecond(4000)));

// Other situations will return None, such as Int64 -> TimeSecondType or
// Int32 -> TimeMicrosecondType etc.
let val = Value::Int64(123);
let time = ConcreteDataType::time_second_datatype().cast(val);
let time = ConcreteDataType::time_second_datatype().try_cast(val);
assert_eq!(time, None);

let val = Value::Int32(123);
let time = ConcreteDataType::time_microsecond_datatype().cast(val);
let time = ConcreteDataType::time_microsecond_datatype().try_cast(val);
assert_eq!(time, None);

// TimeSecond -> TimeMicroSecond
let second = Value::Time(Time::new_second(2023));
let microsecond = ConcreteDataType::time_microsecond_datatype()
.cast(second)
.try_cast(second)
.unwrap();
assert_eq!(
microsecond,
Expand All @@ -276,7 +278,7 @@ mod tests {

// test overflow
let second = Value::Time(Time::new_second(i64::MAX));
let microsecond = ConcreteDataType::time_microsecond_datatype().cast(second);
let microsecond = ConcreteDataType::time_microsecond_datatype().try_cast(second);
assert_eq!(microsecond, None);
}
}
14 changes: 7 additions & 7 deletions src/datatypes/src/types/timestamp_type.rs
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ macro_rules! impl_data_type_for_timestamp {
true
}

fn cast(&self, from: Value)-> Option<Value>{
fn try_cast(&self, from: Value)-> Option<Value>{
match from {
Value::Timestamp(v) => v.convert_to(TimeUnit::$unit).map(Value::Timestamp),
Value::String(v) => Timestamp::from_str(v.as_utf8()).map(Value::Timestamp).ok(),
Expand Down Expand Up @@ -237,39 +237,39 @@ mod tests {
// String -> TimestampSecond
let s = Value::String("2021-01-01 01:02:03".to_string().into());
let ts = ConcreteDataType::timestamp_second_datatype()
.cast(s)
.try_cast(s)
.unwrap();
assert_eq!(ts, Value::Timestamp(Timestamp::new_second(1609434123)));
// String cast failed
let s = Value::String("12345".to_string().into());
let ts = ConcreteDataType::timestamp_second_datatype().cast(s);
let ts = ConcreteDataType::timestamp_second_datatype().try_cast(s);
assert_eq!(ts, None);

let n = Value::Int64(1694589525);
// Int64 -> TimestampSecond
let ts = ConcreteDataType::timestamp_second_datatype()
.cast(n)
.try_cast(n)
.unwrap();
assert_eq!(ts, Value::Timestamp(Timestamp::new_second(1694589525)));

// Datetime -> TimestampSecond
let dt = Value::DateTime(DateTime::from(1234567));
let ts = ConcreteDataType::timestamp_second_datatype()
.cast(dt)
.try_cast(dt)
.unwrap();
assert_eq!(ts, Value::Timestamp(Timestamp::new_second(1234567)));

// Date -> TimestampMillisecond
let d = Value::Date(Date::from_str("1970-01-01").unwrap());
let ts = ConcreteDataType::timestamp_millisecond_datatype()
.cast(d)
.try_cast(d)
.unwrap();
assert_eq!(ts, Value::Timestamp(Timestamp::new_millisecond(0)));

// TimestampSecond -> TimestampMicrosecond
let second = Value::Timestamp(Timestamp::new_second(123));
let microsecond = ConcreteDataType::timestamp_microsecond_datatype()
.cast(second)
.try_cast(second)
.unwrap();
assert_eq!(
microsecond,
Expand Down

0 comments on commit 9b08af3

Please sign in to comment.