chore: Resolves remaining comments in #168 (#175)

* fix: Rename current_timestamp to current_time_millis, fix resolution

Fix current_timestamp returns seconds resolution, also add a test for
this method

* chore: Use slice of array instead of Vec

Save some heap allocations

* test: Compare std and chrono timestamp

The original test always success even the current_time_millis returns in
seconds resolution

* chore: Store current time in gmt_created/gmt_modified
This commit is contained in:
evenyag
2022-08-17 12:11:08 +08:00
committed by GitHub
parent a1c4921933
commit 6d23118aa0
3 changed files with 36 additions and 8 deletions

View File

@@ -5,6 +5,7 @@ use std::sync::Arc;
use common_query::logical_plan::Expr;
use common_recordbatch::SendableRecordBatchStream;
use common_telemetry::debug;
use common_time::util;
use datatypes::prelude::{ConcreteDataType, ScalarVector};
use datatypes::schema::{ColumnSchema, Schema, SchemaRef};
use datatypes::vectors::{BinaryVector, Int64Vector, UInt8Vector};
@@ -152,7 +153,7 @@ pub fn build_table_insert_request(full_table_name: String, table_id: TableId) ->
let mut columns_values = HashMap::with_capacity(6);
columns_values.insert(
"entry_type".to_string(),
Arc::new(UInt8Vector::from_vec(vec![EntryType::Table as u8])) as _,
Arc::new(UInt8Vector::from_slice(&[EntryType::Table as u8])) as _,
);
columns_values.insert(
@@ -163,7 +164,7 @@ pub fn build_table_insert_request(full_table_name: String, table_id: TableId) ->
// Timestamp in key part is intentionally left to 0
columns_values.insert(
"timestamp".to_string(),
Arc::new(Int64Vector::from_vec(vec![0])) as _,
Arc::new(Int64Vector::from_slice(&[0])) as _,
);
columns_values.insert(
@@ -177,12 +178,12 @@ pub fn build_table_insert_request(full_table_name: String, table_id: TableId) ->
columns_values.insert(
"gmt_created".to_string(),
Arc::new(Int64Vector::from_vec(vec![0])) as _,
Arc::new(Int64Vector::from_slice(&[util::current_time_millis()])) as _,
);
columns_values.insert(
"gmt_modified".to_string(),
Arc::new(Int64Vector::from_vec(vec![0])) as _,
Arc::new(Int64Vector::from_slice(&[util::current_time_millis()])) as _,
);
InsertRequest {

View File

@@ -6,4 +6,4 @@ edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
chrono = "0.4"
chrono = "0.4"

View File

@@ -1,4 +1,31 @@
/// Calculates the time duration since UNIX_EPOCH in milliseconds.
pub fn current_timestamp() -> i64 {
chrono::Utc::now().timestamp()
/// Returns the time duration since UNIX_EPOCH in milliseconds.
pub fn current_time_millis() -> i64 {
chrono::Utc::now().timestamp_millis()
}
#[cfg(test)]
mod tests {
use std::time::{self, SystemTime};
use chrono::{Datelike, TimeZone, Timelike};
use super::*;
#[test]
fn test_current_time_millis() {
let now = current_time_millis();
let millis_from_std = SystemTime::now()
.duration_since(time::UNIX_EPOCH)
.unwrap()
.as_millis() as i64;
let datetime_now = chrono::Utc.timestamp_millis(now);
let datetime_std = chrono::Utc.timestamp_millis(millis_from_std);
assert_eq!(datetime_std.year(), datetime_now.year());
assert_eq!(datetime_std.month(), datetime_now.month());
assert_eq!(datetime_std.day(), datetime_now.day());
assert_eq!(datetime_std.hour(), datetime_now.hour());
assert_eq!(datetime_std.minute(), datetime_now.minute());
}
}