Skip to content

Commit

Permalink
fix: parse i64 to different kinds of timestamp (GreptimeTeam#2440)
Browse files Browse the repository at this point in the history
* feat: support i64 to multi-timestamp.

* chore: fmt
  • Loading branch information
QuenKar authored and paomian committed Oct 19, 2023
1 parent fcf6f9b commit fe23858
Showing 1 changed file with 24 additions and 6 deletions.
30 changes: 24 additions & 6 deletions src/sql/src/statements.rs
Original file line number Diff line number Diff line change
Expand Up @@ -141,10 +141,15 @@ macro_rules! parse_number_to_value {
Ok(Value::$Type($Target::from(n)))
},
)+
_ => ParseSqlValueSnafu {
msg: format!("Fail to parse number {}, invalid column type: {:?}",
$n, $data_type
)}.fail(),
ConcreteDataType::Timestamp(t) => {
let n = parse_sql_number::<i64>($n)?;
Ok(Value::Timestamp(Timestamp::new(n, t.unit())))
},

_ => ParseSqlValueSnafu {
msg: format!("Fail to parse number {}, invalid column type: {:?}",
$n, $data_type
)}.fail(),
}
}
}
Expand All @@ -163,8 +168,7 @@ pub fn sql_number_to_value(data_type: &ConcreteDataType, n: &str) -> Result<Valu
(Int32, i32, i32),
(Int64, i64, i64),
(Float64, f64, OrderedF64),
(Float32, f32, OrderedF32),
(Timestamp, i64, Timestamp)
(Float32, f32, OrderedF32)
)
// TODO(hl): also Date/DateTime
}
Expand Down Expand Up @@ -548,6 +552,20 @@ mod tests {
let v = sql_number_to_value(&ConcreteDataType::int32_datatype(), "999").unwrap();
assert_eq!(Value::Int32(999), v);

let v = sql_number_to_value(
&ConcreteDataType::timestamp_nanosecond_datatype(),
"1073741821",
)
.unwrap();
assert_eq!(Value::Timestamp(Timestamp::new_nanosecond(1073741821)), v);

let v = sql_number_to_value(
&ConcreteDataType::timestamp_millisecond_datatype(),
"999999",
)
.unwrap();
assert_eq!(Value::Timestamp(Timestamp::new_millisecond(999999)), v);

let v = sql_number_to_value(&ConcreteDataType::string_datatype(), "999");
assert!(v.is_err(), "parse value error is: {v:?}");
}
Expand Down

0 comments on commit fe23858

Please sign in to comment.