Skip to content

Commit

Permalink
remove repeated codes.
Browse files Browse the repository at this point in the history
  • Loading branch information
Rachelint committed Jul 17, 2024
1 parent effccc1 commit a2d493c
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 83 deletions.
121 changes: 40 additions & 81 deletions arrow-cast/src/display.rs
Original file line number Diff line number Diff line change
Expand Up @@ -662,17 +662,17 @@ impl<'a> DisplayIndex for &'a PrimitiveArray<IntervalYearMonthType> {
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 mut prefix = "";

if value.days != 0 {
write!(f, "{} days", value.days)?;
first_part = false;
write!(f, "{prefix}{} days", value.days)?;
prefix = " ";
}

if value.milliseconds != 0 {
let millis_fmt = MillisecondsFormatter {
milliseconds: value.milliseconds,
first_part,
prefix,
};

f.write_fmt(format_args!("{millis_fmt}"))?;
Expand All @@ -685,26 +685,22 @@ impl<'a> DisplayIndex for &'a PrimitiveArray<IntervalDayTimeType> {
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;
let mut prefix = "";

if value.months != 0 {
write!(f, "{} mons", value.months)?;
first_part = false;
write!(f, "{prefix}{} mons", value.months)?;
prefix = " ";
}

if value.days != 0 {
if first_part {
write!(f, "{} days", value.days)?;
first_part = false;
} else {
write!(f, " {} days", value.days)?;
}
write!(f, "{prefix}{} days", value.days)?;
prefix = " ";
}

if value.nanoseconds != 0 {
let nano_fmt = NanosecondsFormatter {
nanoseconds: value.nanoseconds,
first_part,
prefix,
};
f.write_fmt(format_args!("{nano_fmt}"))?;
}
Expand All @@ -713,14 +709,14 @@ impl<'a> DisplayIndex for &'a PrimitiveArray<IntervalMonthDayNanoType> {
}
}

struct NanosecondsFormatter {
struct NanosecondsFormatter<'a> {
nanoseconds: i64,
first_part: bool,
prefix: &'a str,
}

impl Display for NanosecondsFormatter {
impl<'a> Display for NanosecondsFormatter<'a> {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
let mut first_part = self.first_part;
let mut prefix = self.prefix;

let secs = self.nanoseconds / 1_000_000_000;
let mins = secs / 60;
Expand All @@ -732,57 +728,38 @@ impl Display for NanosecondsFormatter {
let nanoseconds = self.nanoseconds % 1_000_000_000;

if hours != 0 {
if first_part {
write!(f, "{} hours", hours)?;
first_part = false;
} else {
write!(f, " {} hours", hours)?;
}
write!(f, "{prefix}{} hours", hours)?;
prefix = " ";
}

if mins != 0 {
if first_part {
write!(f, "{} mins", mins)?;
first_part = false;
} else {
write!(f, " {} mins", mins)?;
}
write!(f, "{prefix}{} mins", mins)?;
prefix = " ";
}

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()
)?;
}
write!(
f,
"{prefix}{}{}.{:09} secs",
secs_sign,
secs.abs(),
nanoseconds.abs()
)?;
}

Ok(())
}
}

struct MillisecondsFormatter {
struct MillisecondsFormatter<'a> {
milliseconds: i32,
first_part: bool,
prefix: &'a str,
}

impl Display for MillisecondsFormatter {
impl<'a> Display for MillisecondsFormatter<'a> {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
let mut first_part = self.first_part;
let mut prefix = self.prefix;

let secs = self.milliseconds / 1_000;
let mins = secs / 60;
Expand All @@ -794,21 +771,13 @@ impl Display for MillisecondsFormatter {
let milliseconds = self.milliseconds % 1_000;

if hours != 0 {
if first_part {
write!(f, "{} hours", hours,)?;
first_part = false;
} else {
write!(f, " {} hours", hours,)?;
}
write!(f, "{prefix}{} hours", hours,)?;
prefix = " ";
}

if mins != 0 {
if first_part {
write!(f, "{} mins", mins,)?;
first_part = false;
} else {
write!(f, " {} mins", mins,)?;
}
write!(f, "{prefix}{} mins", mins,)?;
prefix = " ";
}

if secs != 0 || milliseconds != 0 {
Expand All @@ -818,23 +787,13 @@ impl Display for MillisecondsFormatter {
""
};

if first_part {
write!(
f,
"{}{}.{:03} secs",
secs_sign,
secs.abs(),
milliseconds.abs()
)?;
} else {
write!(
f,
" {}{}.{:03} secs",
secs_sign,
secs.abs(),
milliseconds.abs()
)?;
}
write!(
f,
"{prefix}{}{}.{:03} secs",
secs_sign,
secs.abs(),
milliseconds.abs()
)?;
}

Ok(())
Expand Down
2 changes: 1 addition & 1 deletion parquet-testing
Submodule parquet-testing updated 32 files
+6 −368 data/README.md
+0 −1 data/_KEY_MATERIAL_FOR_external_key_material_java.parquet.encrypted.json
+ data/bloom_filter.xxhash.bin
+ data/byte_stream_split.zstd.parquet
+ data/byte_stream_split_extended.gzip.parquet
+ data/concatenated_gzip_members.parquet
+ data/data_index_bloom_encoding_with_length.parquet
+ data/datapage_v1-corrupt-checksum.parquet
+ data/datapage_v1-snappy-compressed-checksum.parquet
+ data/datapage_v1-uncompressed-checksum.parquet
+ data/delta_length_byte_array.parquet
+ data/encrypt_columns_and_footer.parquet.encrypted
+ data/encrypt_columns_and_footer_aad.parquet.encrypted
+ data/encrypt_columns_and_footer_ctr.parquet.encrypted
+ data/encrypt_columns_and_footer_disable_aad_storage.parquet.encrypted
+ data/encrypt_columns_plaintext_footer.parquet.encrypted
+ data/external_key_material_java.parquet.encrypted
+0 −73 data/fixed_length_byte_array.md
+ data/fixed_length_byte_array.parquet
+ data/float16_nonzeros_and_nans.parquet
+ data/float16_zeros_and_nans.parquet
+ data/incorrect_map_schema.parquet
+0 −73 data/int32_with_null_pages.md
+ data/int32_with_null_pages.parquet
+ data/large_string_map.brotli.parquet
+ data/nan_in_stats.parquet
+ data/overflow_i16_page_cnt.parquet
+ data/plain-dict-uncompressed-checksum.parquet
+ data/rle-dict-snappy-checksum.parquet
+ data/rle-dict-uncompressed-corrupt-checksum.parquet
+ data/rle_boolean_encoding.parquet
+ data/uniform_encryption.parquet.encrypted
1 change: 1 addition & 0 deletions rust-toolchain
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
nightly-2024-07-05

0 comments on commit a2d493c

Please sign in to comment.