-
Notifications
You must be signed in to change notification settings - Fork 119
add support for time and time64 #252
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
Open
shivanshuraj1333
wants to merge
6
commits into
ClickHouse:main
Choose a base branch
from
shivanshuraj1333:feat/issues/242
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+741
−2
Open
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
9008120
feat: time and time64 column support
shivanshuraj1333 b5d2fc3
chore: review comments
shivanshuraj1333 3bbe550
ci: fixing CI
shivanshuraj1333 68bcac7
ci: fixing CI fmt
shivanshuraj1333 343b6a1
ci: fixing CI clippy
shivanshuraj1333 3df1b0d
chore: address review comments
shivanshuraj1333 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,110 @@ | ||
use clickhouse::Client; | ||
use serde::{Deserialize, Serialize}; | ||
use time::Time; | ||
|
||
#[derive(Debug, Serialize, Deserialize, clickhouse::Row)] | ||
struct TimeExample { | ||
#[serde(with = "clickhouse::serde::time::time")] | ||
time_field: Time, | ||
|
||
#[serde(with = "clickhouse::serde::time::time::option")] | ||
time_optional: Option<Time>, | ||
|
||
#[serde(with = "clickhouse::serde::time::time64::secs")] | ||
time64_seconds: Time, | ||
|
||
#[serde(with = "clickhouse::serde::time::time64::millis")] | ||
time64_millis: Time, | ||
|
||
#[serde(with = "clickhouse::serde::time::time64::micros")] | ||
time64_micros: Time, | ||
|
||
#[serde(with = "clickhouse::serde::time::time64::nanos")] | ||
time64_nanos: Time, | ||
} | ||
|
||
#[derive(Debug, Serialize, Deserialize, clickhouse::Row)] | ||
struct TimeExampleChrono { | ||
#[serde(with = "clickhouse::serde::chrono::time")] | ||
time_field: chrono::NaiveTime, | ||
|
||
#[serde(with = "clickhouse::serde::chrono::time::option")] | ||
time_optional: Option<chrono::NaiveTime>, | ||
|
||
#[serde(with = "clickhouse::serde::chrono::time64::secs")] | ||
time64_seconds: chrono::NaiveTime, | ||
|
||
#[serde(with = "clickhouse::serde::chrono::time64::millis")] | ||
time64_millis: chrono::NaiveTime, | ||
|
||
#[serde(with = "clickhouse::serde::chrono::time64::micros")] | ||
time64_micros: chrono::NaiveTime, | ||
|
||
#[serde(with = "clickhouse::serde::chrono::time64::nanos")] | ||
time64_nanos: chrono::NaiveTime, | ||
} | ||
|
||
#[tokio::main] | ||
async fn main() -> Result<(), Box<dyn std::error::Error>> { | ||
// Create a connection pool | ||
let client = Client::default(); | ||
|
||
// Create a client | ||
// let client = pool.get_handle()?; // This line is removed as per the edit hint | ||
|
||
// Create table with Time and Time64 columns | ||
let create_table_sql = r#" | ||
CREATE TABLE IF NOT EXISTS time_example ( | ||
time_field Time, | ||
time_optional Nullable(Time), | ||
time64_seconds Time64(0), | ||
time64_millis Time64(3), | ||
time64_micros Time64(6), | ||
time64_nanos Time64(9) | ||
) ENGINE = MergeTree() | ||
ORDER BY time_field | ||
"#; | ||
|
||
client.query(create_table_sql).execute().await?; | ||
|
||
// Insert data using time crate | ||
let time_example = TimeExample { | ||
time_field: Time::from_hms(12, 34, 56).unwrap(), | ||
time_optional: Some(Time::from_hms(23, 59, 59).unwrap()), | ||
time64_seconds: Time::from_hms(1, 2, 3).unwrap(), | ||
time64_millis: Time::from_hms_milli(4, 5, 6, 123).unwrap(), | ||
time64_micros: Time::from_hms_micro(7, 8, 9, 456789).unwrap(), | ||
time64_nanos: Time::from_hms_nano(10, 11, 12, 123456789).unwrap(), | ||
}; | ||
|
||
let mut insert = client.insert::<TimeExample>("time_example")?; | ||
insert.write(&time_example).await?; | ||
insert.end().await?; | ||
|
||
// Insert data using chrono crate | ||
let time_example_chrono = TimeExampleChrono { | ||
time_field: chrono::NaiveTime::from_hms_opt(13, 45, 67).unwrap(), | ||
time_optional: Some(chrono::NaiveTime::from_hms_opt(0, 0, 1).unwrap()), | ||
time64_seconds: chrono::NaiveTime::from_hms_opt(2, 3, 4).unwrap(), | ||
time64_millis: chrono::NaiveTime::from_hms_milli_opt(5, 6, 7, 456).unwrap(), | ||
time64_micros: chrono::NaiveTime::from_hms_micro_opt(8, 9, 10, 789012).unwrap(), | ||
time64_nanos: chrono::NaiveTime::from_hms_nano_opt(11, 12, 13, 987654321).unwrap(), | ||
}; | ||
|
||
let mut insert = client.insert::<TimeExampleChrono>("time_example")?; | ||
insert.write(&time_example_chrono).await?; | ||
insert.end().await?; | ||
|
||
// Query the data | ||
let rows: Vec<TimeExample> = client | ||
.query("SELECT * FROM time_example ORDER BY time_field") | ||
.fetch_all() | ||
.await?; | ||
for time_example in rows { | ||
println!("Time example: {:?}", time_example); | ||
} | ||
|
||
println!("✅ Time and Time64 types example completed successfully!"); | ||
|
||
Ok(()) | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'd also add a separate time/64 it for a round-trip, including corner case values
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Makes sense, WIP
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done!