-
Notifications
You must be signed in to change notification settings - Fork 1.2k
[Variant] Add Variant::Time primitive and cast logic #8114
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
Merged
Merged
Changes from 3 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
803e115
[Variant] Add Variant::Time primitive and cast logic
klion26 16b2cad
fix style
klion26 8a522b6
update test
klion26 1051ef1
Merge remote-tracking branch 'apache/main' into variant_time
alamb dfba596
Merge remote-tracking branch 'apache/main' into variant_time
alamb afbcb4b
Only install cargo-msrv if needed
alamb 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
Submodule parquet-testing
updated
14 files
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
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 |
|---|---|---|
|
|
@@ -18,11 +18,11 @@ | |
| //! Module for converting Variant data to JSON format | ||
| use arrow_schema::ArrowError; | ||
| use base64::{engine::general_purpose, Engine as _}; | ||
| use chrono::Timelike; | ||
| use parquet_variant::{Variant, VariantList, VariantObject}; | ||
| use serde_json::Value; | ||
| use std::io::Write; | ||
|
|
||
| use parquet_variant::{Variant, VariantList, VariantObject}; | ||
|
|
||
| // Format string constants to avoid duplication and reduce errors | ||
| const DATE_FORMAT: &str = "%Y-%m-%d"; | ||
| const TIMESTAMP_NTZ_FORMAT: &str = "%Y-%m-%dT%H:%M:%S%.6f"; | ||
|
|
@@ -40,6 +40,19 @@ fn format_binary_base64(bytes: &[u8]) -> String { | |
| general_purpose::STANDARD.encode(bytes) | ||
| } | ||
|
|
||
| fn format_time_ntz_str(time: &chrono::NaiveTime) -> String { | ||
| let base = time.format("%H:%M:%S").to_string(); | ||
| let micros = time.nanosecond() / 1000; | ||
| match micros { | ||
| 0 => format!("{}.{}", base, 0), | ||
| _ => { | ||
| let micros_str = format!("{:06}", micros); | ||
| let micros_str_trimmed = micros_str.trim_matches('0'); | ||
| format!("{}.{}", base, micros_str_trimmed) | ||
| } | ||
| } | ||
| } | ||
|
|
||
| /// | ||
| /// This function writes JSON directly to any type that implements [`Write`], | ||
| /// making it efficient for streaming or when you want to control the output destination. | ||
|
|
@@ -110,6 +123,7 @@ pub fn variant_to_json(json_buffer: &mut impl Write, variant: &Variant) -> Resul | |
| Variant::TimestampNtzMicros(ts) => { | ||
| write!(json_buffer, "\"{}\"", format_timestamp_ntz_string(ts))? | ||
| } | ||
| Variant::Time(time) => write!(json_buffer, "\"{}\"", format_time_ntz_str(time))?, | ||
| Variant::Binary(bytes) => { | ||
| // Encode binary as base64 string | ||
| let base64_str = format_binary_base64(bytes); | ||
|
|
@@ -348,6 +362,7 @@ pub fn variant_to_json_value(variant: &Variant) -> Result<Value, ArrowError> { | |
| Variant::Date(date) => Ok(Value::String(format_date_string(date))), | ||
| Variant::TimestampMicros(ts) => Ok(Value::String(ts.to_rfc3339())), | ||
| Variant::TimestampNtzMicros(ts) => Ok(Value::String(format_timestamp_ntz_string(ts))), | ||
| Variant::Time(time) => Ok(Value::String(format_time_ntz_str(time))), | ||
| Variant::Binary(bytes) => Ok(Value::String(format_binary_base64(bytes))), | ||
| Variant::String(s) => Ok(Value::String(s.to_string())), | ||
| Variant::ShortString(s) => Ok(Value::String(s.to_string())), | ||
|
|
@@ -371,7 +386,7 @@ pub fn variant_to_json_value(variant: &Variant) -> Result<Value, ArrowError> { | |
| #[cfg(test)] | ||
| mod tests { | ||
| use super::*; | ||
| use chrono::{DateTime, NaiveDate, Utc}; | ||
| use chrono::{DateTime, NaiveDate, NaiveTime, Utc}; | ||
| use parquet_variant::{VariantDecimal16, VariantDecimal4, VariantDecimal8}; | ||
|
|
||
| #[test] | ||
|
|
@@ -457,6 +472,18 @@ mod tests { | |
| Ok(()) | ||
| } | ||
|
|
||
| #[test] | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ❤️ |
||
| fn test_time_to_json() -> Result<(), ArrowError> { | ||
| let naive_time = NaiveTime::from_num_seconds_from_midnight_opt(12345, 123460708).unwrap(); | ||
| let variant = Variant::Time(naive_time); | ||
| let json = variant_to_json_string(&variant)?; | ||
| assert_eq!("\"03:25:45.12346\"", json); | ||
|
|
||
| let json_value = variant_to_json_value(&variant)?; | ||
| assert!(matches!(json_value, Value::String(_))); | ||
| Ok(()) | ||
| } | ||
|
|
||
| #[test] | ||
| fn test_binary_to_json() -> Result<(), ArrowError> { | ||
| let binary_data = b"Hello, World!"; | ||
|
|
||
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.
this logic looks good to me -- thank you