Skip to content

Commit

Permalink
feat: impl cast_with_opt
Browse files Browse the repository at this point in the history
  • Loading branch information
QuenKar committed Sep 18, 2023
1 parent 9b08af3 commit 18f1038
Show file tree
Hide file tree
Showing 5 changed files with 233 additions and 221 deletions.
10 changes: 10 additions & 0 deletions src/common/time/src/date.rs
Original file line number Diff line number Diff line change
Expand Up @@ -136,4 +136,14 @@ mod tests {
let d: Date = 42.into();
assert_eq!(42, d.val());
}

#[test]
fn test_to_secs() {
let d = Date::from_str("1970-01-01").unwrap();
assert_eq!(d.to_secs(), 0);
let d = Date::from_str("1970-01-02").unwrap();
assert_eq!(d.to_secs(), 24 * 3600);
let d = Date::from_str("1970-01-03").unwrap();
assert_eq!(d.to_secs(), 2 * 24 * 3600);
}
}
22 changes: 21 additions & 1 deletion src/datatypes/src/data_type.rs
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,10 @@ impl ConcreteDataType {
matches!(self, ConcreteDataType::Boolean(_))
}

pub fn is_string(&self) -> bool {
matches!(self, ConcreteDataType::String(_))
}

pub fn is_stringifiable(&self) -> bool {
matches!(
self,
Expand Down Expand Up @@ -151,6 +155,22 @@ impl ConcreteDataType {
)
}

pub fn is_numeric(&self) -> bool {
matches!(
self,
ConcreteDataType::Int8(_)
| ConcreteDataType::Int16(_)
| ConcreteDataType::Int32(_)
| ConcreteDataType::Int64(_)
| ConcreteDataType::UInt8(_)
| ConcreteDataType::UInt16(_)
| ConcreteDataType::UInt32(_)
| ConcreteDataType::UInt64(_)
| ConcreteDataType::Float32(_)
| ConcreteDataType::Float64(_)
)
}

pub fn numerics() -> Vec<ConcreteDataType> {
vec![
ConcreteDataType::int8_datatype(),
Expand Down Expand Up @@ -406,7 +426,7 @@ pub trait DataType: std::fmt::Debug + Send + Sync {
/// use it as a timestamp.
fn is_timestamp_compatible(&self) -> bool;

/// Casts the value to this DataType.
/// Casts the value to specific DataType.
/// Return None if cast failed.
fn try_cast(&self, from: Value) -> Option<Value>;
}
Expand Down
1 change: 1 addition & 0 deletions src/datatypes/src/types/binary_type.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ impl DataType for BinaryType {
fn try_cast(&self, from: Value) -> Option<Value> {
match from {
Value::Binary(v) => Some(Value::Binary(v)),
Value::String(v) => Some(Value::Binary(Bytes::from(v.as_utf8().as_bytes()))),
_ => None,
}
}
Expand Down
Loading

0 comments on commit 18f1038

Please sign in to comment.