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

Fix decimal repr in parquet schema printer #721

Merged
merged 1 commit into from
Aug 31, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 22 additions & 5 deletions parquet/src/schema/printer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -249,13 +249,13 @@ fn print_logical_and_converted(
ConvertedType::NONE => format!(""),
decimal @ ConvertedType::DECIMAL => {
// For decimal type we should print precision and scale if they
// are > 0, e.g. DECIMAL(9, 2) -
// are > 0, e.g. DECIMAL(9,2) -
// DECIMAL(9) - DECIMAL
let precision_scale = match (precision, scale) {
(p, s) if p > 0 && s > 0 => {
format!("{}, {}", p, s)
format!("({},{})", p, s)
}
(p, 0) if p > 0 => format!("{}", p),
(p, 0) if p > 0 => format!("({})", p),
_ => format!(""),
};
format!("{}{}", decimal, precision_scale)
Expand Down Expand Up @@ -292,8 +292,8 @@ impl<'a> Printer<'a> {
let logical_type_str = print_logical_and_converted(
&basic_info.logical_type(),
basic_info.converted_type(),
scale,
precision,
scale,
);
if logical_type_str.is_empty() {
write!(
Expand Down Expand Up @@ -588,7 +588,10 @@ mod tests {

#[inline]
fn decimal_length_from_precision(precision: usize) -> i32 {
(10.0_f64.powi(precision as i32).log2() / 8.0).ceil() as i32
let max_val = 10.0_f64.powi(precision as i32) - 1.0;
let bits_unsigned = max_val.log2().ceil();
let bits_signed = bits_unsigned + 1.0;
(bits_signed / 8.0).ceil() as i32
}

#[test]
Expand Down Expand Up @@ -630,6 +633,20 @@ mod tests {
.unwrap(),
"REPEATED FIXED_LEN_BYTE_ARRAY (14) decimal (DECIMAL(32,20));",
),
(
Type::primitive_type_builder(
"decimal",
PhysicalType::FIXED_LEN_BYTE_ARRAY,
)
.with_converted_type(ConvertedType::DECIMAL)
.with_precision(19)
.with_scale(4)
.with_length(decimal_length_from_precision(19))
.with_repetition(Repetition::OPTIONAL)
.build()
.unwrap(),
"OPTIONAL FIXED_LEN_BYTE_ARRAY (9) decimal (DECIMAL(19,4));",
Copy link
Contributor

Choose a reason for hiding this comment

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

In case anyone is interested, I double checked that test fails without the code change in this PR (unsurprisingly):


---- schema::printer::tests::test_print_flba_logical_types stdout ----
thread 'schema::printer::tests::test_print_flba_logical_types' panicked at 'called `Result::unwrap()` on an `Err` value: General("Cannot represent FIXED_LEN_BYTE_ARRAY as DECIMAL with length 8 and precision 19. The max precision can only be 18")', parquet/src/schema/printer.rs:644:18

),
];

types_and_strings.into_iter().for_each(|(field, expected)| {
Expand Down