You are viewing a plain text version of this content. The canonical link for it is here.
Posted to github@arrow.apache.org by "Rachelint (via GitHub)" <gi...@apache.org> on 2024/07/05 11:11:10 UTC

[PR] Make interval more pretty [arrow-rs]

Rachelint opened a new pull request, #6006:
URL: https://github.com/apache/arrow-rs/pull/6006

   # Which issue does this PR close?
   
   <!--
   We generally require a GitHub issue to be filed for all bug fixes and enhancements and this helps us generate change logs for our releases. You can link an issue to this PR using the GitHub syntax. For example `Closes #123` indicates that this PR will close issue #123.
   -->
   
   Closes #.
   
   # Rationale for this change
    
   <!--
   Why are you proposing this change? If this is already explained clearly in the issue then this section is not needed.
   Explaining clearly why changes are proposed helps reviewers understand your changes and offer better suggestions for fixes.
   -->
   
   # What changes are included in this PR?
   
   <!--
   There is no need to duplicate the description in the issue here but it is sometimes worth providing a summary of the individual changes in this PR.
   -->
   
   # Are there any user-facing changes?
   
   
   <!--
   If there are user-facing changes then we may require documentation to be updated before approving the PR.
   -->
   
   <!---
   If there are any breaking changes to public APIs, please add the `breaking change` label.
   -->
   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] Make display of interval types more pretty [arrow-rs]

Posted by "alamb (via GitHub)" <gi...@apache.org>.
alamb commented on PR #6006:
URL: https://github.com/apache/arrow-rs/pull/6006#issuecomment-2212429775

   Thanks @Rachelint  -- this looks great. I think now that we have merged https://github.com/apache/arrow-rs/pull/6012 (thanks again BTW!) this PR could be merged / rebased and CI would pass


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] Make display of interval types more pretty [arrow-rs]

Posted by "Rachelint (via GitHub)" <gi...@apache.org>.
Rachelint commented on PR #6006:
URL: https://github.com/apache/arrow-rs/pull/6006#issuecomment-2212431688

   > Thanks @Rachelint -- this looks great. I think now that we have merged #6012 (thanks again BTW!) this PR could be merged / rebased and CI would pass
   
   Oh, forget to push yesterday... Have updated now.


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] Make display of interval types more pretty [arrow-rs]

Posted by "Rachelint (via GitHub)" <gi...@apache.org>.
Rachelint commented on PR #6006:
URL: https://github.com/apache/arrow-rs/pull/6006#issuecomment-2233707024

   > Per discussion on #6050 I am going to merge this branch to the `53.0.0-dev` feature branch, where we are collecting PRs with breaking changes before `master` opens
   
   Oh, sorry.. forget to push the code about eliminating the repeated codes, I make a new pr to make it.


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] Make display of interval types more pretty [arrow-rs]

Posted by "alamb (via GitHub)" <gi...@apache.org>.
alamb commented on PR #6006:
URL: https://github.com/apache/arrow-rs/pull/6006#issuecomment-2231905219

   Per discussion on https://github.com/apache/arrow-rs/issues/6050 I am going to merge this branch to the `53.0.0-dev` feature branch, where we are collecting PRs with breaking changes before `master` opens


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] Make display of interval types more pretty [arrow-rs]

Posted by "alamb (via GitHub)" <gi...@apache.org>.
alamb commented on PR #6006:
URL: https://github.com/apache/arrow-rs/pull/6006#issuecomment-2234099091

   Thank you @Rachelint 


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] Make display of interval types more pretty [arrow-rs]

Posted by "alamb (via GitHub)" <gi...@apache.org>.
alamb commented on code in PR #6006:
URL: https://github.com/apache/arrow-rs/pull/6006#discussion_r1667746254


##########
arrow-cast/src/display.rs:
##########
@@ -654,73 +654,189 @@ impl<'a> DisplayIndex for &'a PrimitiveArray<IntervalYearMonthType> {
         let years = (interval / 12_f64).floor();
         let month = interval - (years * 12_f64);
 
-        write!(
-            f,
-            "{years} years {month} mons 0 days 0 hours 0 mins 0.00 secs",
-        )?;
+        write!(f, "{years} years {month} mons",)?;
         Ok(())
     }
 }
 
 impl<'a> DisplayIndex for &'a PrimitiveArray<IntervalDayTimeType> {
     fn write(&self, idx: usize, f: &mut dyn Write) -> FormatResult {
         let value = self.value(idx);
+        let mut first_part = true;
 
-        let secs = value.milliseconds / 1_000;
+        if value.days != 0 {
+            write!(f, "{} days", value.days)?;
+            first_part = false;
+        }
+
+        if value.milliseconds != 0 {
+            let millis_fmt = MillisecondsFormatter {
+                milliseconds: value.milliseconds,
+                first_part,
+            };
+
+            f.write_fmt(format_args!("{millis_fmt}"))?;
+        }
+
+        Ok(())
+    }
+}
+
+impl<'a> DisplayIndex for &'a PrimitiveArray<IntervalMonthDayNanoType> {
+    fn write(&self, idx: usize, f: &mut dyn Write) -> FormatResult {
+        let value = self.value(idx);
+        let mut first_part = true;
+
+        if value.months != 0 {
+            write!(f, "{} mons", value.months)?;
+            first_part = false;
+        }
+
+        if value.days != 0 {
+            if first_part {
+                write!(f, "{} days", value.days)?;
+                first_part = false;
+            } else {
+                write!(f, " {} days", value.days)?;
+            }
+        }
+
+        if value.nanoseconds != 0 {
+            let nano_fmt = NanosecondsFormatter {
+                nanoseconds: value.nanoseconds,
+                first_part,
+            };
+            f.write_fmt(format_args!("{nano_fmt}"))?;
+        }
+
+        Ok(())
+    }
+}
+
+struct NanosecondsFormatter {
+    nanoseconds: i64,
+    first_part: bool,
+}
+
+impl Display for NanosecondsFormatter {
+    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
+        let mut first_part = self.first_part;
+
+        let secs = self.nanoseconds / 1_000_000_000;
         let mins = secs / 60;
         let hours = mins / 60;
 
         let secs = secs - (mins * 60);
         let mins = mins - (hours * 60);
 
-        let milliseconds = value.milliseconds % 1_000;
+        let nanoseconds = self.nanoseconds % 1_000_000_000;
 
-        let secs_sign = if secs < 0 || milliseconds < 0 {
-            "-"
-        } else {
-            ""
-        };
+        if hours != 0 {
+            if first_part {
+                write!(f, "{} hours", hours)?;
+                first_part = false;
+            } else {
+                write!(f, " {} hours", hours)?;
+            }
+        }
+
+        if mins != 0 {
+            if first_part {
+                write!(f, "{} mins", mins)?;
+                first_part = false;
+            } else {
+                write!(f, " {} mins", mins)?;
+            }
+        }
+
+        if secs != 0 || nanoseconds != 0 {
+            let secs_sign = if secs < 0 || nanoseconds < 0 { "-" } else { "" };
+
+            if first_part {
+                write!(
+                    f,
+                    "{}{}.{:09} secs",
+                    secs_sign,
+                    secs.abs(),
+                    nanoseconds.abs()
+                )?;
+            } else {
+                write!(
+                    f,
+                    " {}{}.{:09} secs",
+                    secs_sign,
+                    secs.abs(),
+                    nanoseconds.abs()
+                )?;
+            }

Review Comment:
   You could probably avoid some repetition here if with a constant str. Something like 
   
   ```suggestion
               let prefix = if first_part { " " } else { "" }
               
               write!(
                   f,
                   "{prefix}{}{}.{:09} secs",
                   secs_sign,
                   secs.abs(),
                   nanoseconds.abs()
               )      
   ```
   
   However that might be slightly slower  now it has to check `prefix`. You could possibly even use a field like `prefix: &'static str` instead of `bool first_part` if you wanted to make it even more concise. 
   
   A similar comment applies to the other clauses
   
   I don't think this is required, I just wanted to mention it



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] Make display of interval types more pretty [arrow-rs]

Posted by "alamb (via GitHub)" <gi...@apache.org>.
alamb merged PR #6006:
URL: https://github.com/apache/arrow-rs/pull/6006


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] Make display of interval types more pretty [arrow-rs]

Posted by "alamb (via GitHub)" <gi...@apache.org>.
alamb commented on PR #6006:
URL: https://github.com/apache/arrow-rs/pull/6006#issuecomment-2212540635

   Marking as waiting on next major release. @Rachelint  feel free to make changes as well.


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org