feat: timestamp column support i64 (#325)

* feat: align_bucket support i64 and timestamp values

* feat: add Int64 to timestamp

* feat: support query i64 timestamp vector

* test: fix failling tests

* refactor: simplify some code

* fix: CR comments and add insert and query test for i64 timestamp column
This commit is contained in:
Lei, Huang
2022-10-28 18:39:11 +08:00
committed by GitHub
parent 3e8d9b421c
commit 81716d622e
8 changed files with 241 additions and 58 deletions

View File

@@ -21,24 +21,6 @@ impl TimestampMillis {
TimestampMillis(ms)
}
/// Returns the timestamp aligned by `bucket_duration` in milliseconds or
/// `None` if overflow occurred.
///
/// # Panics
/// Panics if `bucket_duration <= 0`.
pub fn align_by_bucket(self, bucket_duration: i64) -> Option<TimestampMillis> {
assert!(bucket_duration > 0);
let ts = if self.0 >= 0 {
self.0
} else {
// `bucket_duration > 0` implies `bucket_duration - 1` won't overflow.
self.0.checked_sub(bucket_duration - 1)?
};
Some(TimestampMillis(ts / bucket_duration * bucket_duration))
}
/// Returns the timestamp value as i64.
pub fn as_i64(&self) -> i64 {
self.0
@@ -51,6 +33,12 @@ impl From<i64> for TimestampMillis {
}
}
impl From<TimestampMillis> for i64 {
fn from(ts: TimestampMillis) -> Self {
ts.0
}
}
impl PartialEq<i64> for TimestampMillis {
fn eq(&self, other: &i64) -> bool {
self.0 == *other
@@ -75,6 +63,25 @@ impl PartialOrd<TimestampMillis> for i64 {
}
}
pub trait BucketAligned {
/// Returns the timestamp aligned by `bucket_duration` in milliseconds or
/// `None` if overflow occurred.
///
/// # Panics
/// Panics if `bucket_duration <= 0`.
fn align_by_bucket(self, bucket_duration: i64) -> Option<TimestampMillis>;
}
impl<T: Into<i64>> BucketAligned for T {
fn align_by_bucket(self, bucket_duration: i64) -> Option<TimestampMillis> {
assert!(bucket_duration > 0);
self.into()
.checked_div_euclid(bucket_duration)
.and_then(|val| val.checked_mul(bucket_duration))
.map(TimestampMillis)
}
}
#[cfg(test)]
mod tests {
use super::*;