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(types): support ns timestamp #19827

Open
wants to merge 9 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
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
26 changes: 26 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions src/common/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ humantime = "2.1"
hytra = { workspace = true }
itertools = { workspace = true }
itoa = "1.0"
jiff = "0.1.15"
jsonbb = { workspace = true }
lru = { workspace = true }
memcomparable = { version = "0.2", features = ["decimal"] }
Expand Down
62 changes: 32 additions & 30 deletions src/common/src/types/datetime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -168,29 +168,18 @@ impl FromStr for Timestamp {
type Err = InvalidParamsError;

fn from_str(s: &str) -> Result<Self> {
if let Ok(res) = speedate::DateTime::parse_str_rfc3339(s) {
if res.time.tz_offset.is_some() {
return Err(ErrorKind::ParseTimestamp.into());
}
Ok(Date::from_ymd_uncheck(
res.date.year as i32,
res.date.month as u32,
res.date.day as u32,
)
.and_hms_micro_uncheck(
res.time.hour as u32,
res.time.minute as u32,
res.time.second as u32,
res.time.microsecond,
))
} else {
let res =
speedate::Date::parse_str_rfc3339(s).map_err(|_| ErrorKind::ParseTimestamp)?;
Ok(
Date::from_ymd_uncheck(res.year as i32, res.month as u32, res.day as u32)
.and_hms_micro_uncheck(0, 0, 0, 0),
)
}
let dt = s
.parse::<jiff::civil::DateTime>()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why do we need to introduce yet another dependency in addition to chrono and speedate?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Because chrono can't be parsed, and speedate is only parsed at the us level

.map_err(|_| ErrorKind::ParseTimestamp)?;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Error message of ParseTimestamp shall be updated from up to 6 digits to up to 9 digits.

Ok(
Date::from_ymd_uncheck(dt.year() as i32, dt.month() as u32, dt.day() as u32)
.and_hms_nano_uncheck(
dt.hour() as u32,
dt.minute() as u32,
dt.second() as u32,
dt.subsec_nanosecond() as u32,
),
)
}
}

Expand Down Expand Up @@ -422,6 +411,13 @@ impl Date {
.and_time(Time::from_hms_micro_uncheck(hour, min, sec, micro).0),
)
}

pub fn and_hms_nano_uncheck(self, hour: u32, min: u32, sec: u32, nano: u32) -> Timestamp {
Timestamp::new(
self.0
.and_time(Time::from_hms_nano_uncheck(hour, min, sec, nano).0),
)
}
}

impl Time {
Expand Down Expand Up @@ -495,18 +491,24 @@ impl Timestamp {
}

pub fn from_protobuf(cur: &mut Cursor<&[u8]>) -> ArrayResult<Timestamp> {
let micros = cur
let secs = cur
xxhZs marked this conversation as resolved.
Show resolved Hide resolved
.read_i64::<BigEndian>()
.context("failed to read i64 from Timestamp buffer")?;
.context("failed to read i64 from Time buffer")?;
let nsecs = cur
.read_u32::<BigEndian>()
.context("failed to read u32 from Time buffer")?;

Ok(Timestamp::with_micros(micros)?)
Ok(Timestamp::with_secs_nsecs(secs, nsecs)?)
}

/// Although `Timestamp` takes 12 bytes, we drop 4 bytes in protobuf encoding.
pub fn to_protobuf<T: Write>(self, output: &mut T) -> ArrayResult<usize> {
output
.write(&(self.0.and_utc().timestamp_micros()).to_be_bytes())
.map_err(Into::into)
let timestamp_size = output
.write(&(self.0.and_utc().timestamp()).to_be_bytes())
.map_err(Into::<ArrayError>::into)?;
let timestamp_subsec_nanos_size = output
.write(&(self.0.and_utc().timestamp_subsec_nanos()).to_be_bytes())
.map_err(Into::<ArrayError>::into)?;
Ok(timestamp_subsec_nanos_size + timestamp_size)
xxhZs marked this conversation as resolved.
Show resolved Hide resolved
}

pub fn get_timestamp_nanos(&self) -> i64 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -524,7 +524,7 @@ impl HummockVersion {
.clone_from(&group_construct.table_ids);
self.levels.insert(*compaction_group_id, new_levels);
let member_table_ids = if group_construct.version
>= CompatibilityVersion::NoMemberTableIds as _
>= CompatibilityVersion::NoMemberTableIds as i32
xiangjinwu marked this conversation as resolved.
Show resolved Hide resolved
{
self.state_table_info
.compaction_group_member_table_ids(*compaction_group_id)
Expand All @@ -537,7 +537,8 @@ impl HummockVersion {
BTreeSet::from_iter(group_construct.table_ids.clone())
};

if group_construct.version >= CompatibilityVersion::SplitGroupByTableId as _
if group_construct.version
>= CompatibilityVersion::SplitGroupByTableId as i32
{
let split_key = if group_construct.split_key.is_some() {
Some(Bytes::from(group_construct.split_key.clone().unwrap()))
Expand Down
Loading