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

Configurable Duration Display #4581

Merged
merged 3 commits into from
Jul 30, 2023
Merged

Conversation

tustvold
Copy link
Contributor

Which issue does this PR close?

Closes #4580
Closes #4554

Rationale for this change

What changes are included in this PR?

Are there any user-facing changes?

@github-actions github-actions bot added the arrow Changes to the arrow crate label Jul 29, 2023
true => {
write!(
$f,
concat!("{} days {} hours {} mins -{}.{:0", $scale, "} secs"),
Copy link
Contributor Author

Choose a reason for hiding this comment

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

I opted to not include years and months as durations can never store these quantities and so including them seemed redundant

/// * Human Readable - "198 days 16 hours 34 mins 15.407810000 secs"
///
/// Defaults to true
pub const fn with_iso8601_duration_format(self, iso8601: bool) -> Self {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

I could see us also adding an option to print intervals as ISO8601

Copy link
Contributor

Choose a reason for hiding this comment

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

Rather than a bool here, an enum that would allow more easily adding different formats in the future without changing the API.

Perhaps something like

enum DurationFormat {
  /// Human Readable - "198 days 16 hours 34 mins 15.407810000 secs"
  Human,
  /// ISO 8601 - "P198DT72932.972880S"
  ISO8601,
}

... 
  pub const with_duration_format(mut self, format: DurationFormat) {
  ...
  }

@@ -8959,4 +8959,14 @@ mod tests {
assert_eq!(formatted.value(0).to_string(), "[[1], [2], [3]]");
assert_eq!(formatted.value(1).to_string(), "[[4], [null], [6]]");
}

const CAST_OPTIONS: CastOptions<'static> = CastOptions {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This will let us define constant CastOptions and FormatOptions within DataFusion to override the way it formats durations

assert_eq!(iso[1], "-PT0.000000001S");
assert_eq!(non_iso[1], "0 days 0 hours 0 mins -0.000000001 secs");
assert_eq!(iso[2], "PT0.000001S");
assert_eq!(non_iso[2], "0 days 0 hours 0 mins 0.000001000 secs");
Copy link
Contributor Author

Choose a reason for hiding this comment

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

I was a little torn on whether to print trailing 0s, we currently do for intervals and don't for timestamps. As we are trying to emulate the former, I opted to follow that convention

Copy link
Contributor

@alamb alamb left a comment

Choose a reason for hiding this comment

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

Thank you @tustvold -- this looks good to me. I had one comment on the API design (enum vs bool) that I think is worth considering but otherwise 🚢 🇮🇹

@@ -866,6 +951,15 @@ pub fn lexical_to_string<N: lexical_core::ToLexical>(n: N) -> String {
mod tests {
use super::*;

const TEST_CONST_OPTIONS: FormatOptions<'static> = FormatOptions::new()
Copy link
Contributor

Choose a reason for hiding this comment

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

I think a comment explaining the rationale of the test (so it isn't inadvertently remove) would be helpful

Suggested change
const TEST_CONST_OPTIONS: FormatOptions<'static> = FormatOptions::new()
// Test to verify options can be constant. See
// https://github.com/apache/arrow-rs/issues/4580
const TEST_CONST_OPTIONS: FormatOptions<'static> = FormatOptions::new()

/// * Human Readable - "198 days 16 hours 34 mins 15.407810000 secs"
///
/// Defaults to true
pub const fn with_iso8601_duration_format(self, iso8601: bool) -> Self {
Copy link
Contributor

Choose a reason for hiding this comment

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

Rather than a bool here, an enum that would allow more easily adding different formats in the future without changing the API.

Perhaps something like

enum DurationFormat {
  /// Human Readable - "198 days 16 hours 34 mins 15.407810000 secs"
  Human,
  /// ISO 8601 - "P198DT72932.972880S"
  ISO8601,
}

... 
  pub const with_duration_format(mut self, format: DurationFormat) {
  ...
  }

@tustvold tustvold merged commit fb926a4 into apache:master Jul 30, 2023
25 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
arrow Changes to the arrow crate
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Use FormatOptions in Const Contexts Human Readable Duration Display
2 participants