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: parse var len of decimal for parquet statistic #837

Merged
merged 3 commits into from
Jan 3, 2025

Conversation

ZENOTME
Copy link
Contributor

@ZENOTME ZENOTME commented Dec 24, 2024

I find that the decimal encode for parquet statistic will be byte with var len. Not necessary with 16 len. So we need to extend it when the len smaller than 16.

@ZENOTME
Copy link
Contributor Author

ZENOTME commented Dec 24, 2024

cc @Xuanwo @liurenjie1024

precision: 38,
scale: 0
},
PrimitiveLiteral::Int128(99999999999999999999999999999999999999_i128)
Copy link
Member

Choose a reason for hiding this comment

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

Hi, I assume the limitation of rust_decimal is that it only supports up to 28 digits of precision. Values of Decimal larger than Decimal::MAX or smaller than Decimal::MIN are not supported.

The best we can do here is raising an error if users trying to read or write Decimal larger than 28.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

But from the definition of iceberg spec, the precision of decimal can be 38. So I think for here we can't prevent user write decimal with precision larger than 28 but smaller than 38. https://iceberg.apache.org/spec/#primitive-types

Copy link
Member

Choose a reason for hiding this comment

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

Yep, that is exactly the issue I raised here #669

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Actually, according to my understanding, our decimal is not restricted by rust_decimal.🤔 Internally iceberg have its own decimal representation.

For here, we just provide a way let the user pass a rust_decimal and convert it into our decimal representation. If the rust_decimal is that it only supports up to 28 digits of precision, when user want to more precision, they can pass other format to convert it into our decimal representation. Or as #669, we can provide a way to convert other decimal which support more precision to our decimal representation.

@ZENOTME
Copy link
Contributor Author

ZENOTME commented Dec 30, 2024

Hi @liurenjie1024 @Xuanwo , is there other question about this PR?

@ZENOTME ZENOTME requested a review from Xuanwo January 2, 2025 07:13
@Xuanwo
Copy link
Member

Xuanwo commented Jan 2, 2025

Hi @liurenjie1024 @Xuanwo , is there other question about this PR?

Hi, I don't quite understand what issue this PR is trying to address. The test added in this PR is incorrect, as we can't support such a case unless we use other libraries to handle decimals. I'm also concerned about the root cause of this issue. Is it related to the limitations of our decimal library?

@ZENOTME
Copy link
Contributor Author

ZENOTME commented Jan 2, 2025

Hi @liurenjie1024 @Xuanwo , is there other question about this PR?

Hi, I don't quite understand what issue this PR is trying to address. The test added in this PR is incorrect, as we can't support such a case unless we use other libraries to handle decimals. I'm also concerned about the root cause of this issue. Is it related to the limitations of our decimal library?

Actually, this PR is used to fix case like: 1.1, 2.2, so it's not related the limitations of our decimal library.

The test added in this PR is incorrect, as we can't support such a case unless we use other libraries to handle decimals. I'm also concerned about the root cause of this issue. Is it related to the limitations of our decimal library?

Let me remove the incorrect the wrong case now. I think I should reuse a decimal library which can support 38 precision and after that we can add the test case back.

@Xuanwo Xuanwo requested review from Fokko and liurenjie1024 January 2, 2025 07:53
@Xuanwo
Copy link
Member

Xuanwo commented Jan 2, 2025

Thank you, @ZENOTME, for the clarification. Tagging @liurenjie1024 and @Fokko to review this again.

@@ -741,7 +766,7 @@ macro_rules! get_parquet_stat_as_datum {
};
Some(Datum::new(
primitive_type.clone(),
PrimitiveLiteral::Int128(i128::from_be_bytes(bytes.try_into()?)),
PrimitiveLiteral::Int128(i128::from_be_bytes(extend_to_i128_big_endian(bytes.into())?)),
Copy link
Member

Choose a reason for hiding this comment

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

I revisited the code and found that the root cause might be that we didn't handle the decimal bytes data correctly. By design, Iceberg only keeps the smallest bits of the decimal. To decode it properly, we may need to follow the same logic outlined here:

PrimitiveType::Decimal { .. } => {
let unscaled_value = BigInt::from_signed_bytes_be(bytes);
PrimitiveLiteral::Int128(unscaled_value.to_i128().ok_or_else(|| {
Error::new(
ErrorKind::DataInvalid,
format!("Can't convert bytes to i128: {:?}", bytes),
)
})?)
}
};

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thanks! Nice catch

@ZENOTME ZENOTME requested a review from Xuanwo January 2, 2025 10:16
Copy link
Contributor

@liurenjie1024 liurenjie1024 left a comment

Choose a reason for hiding this comment

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

Thanks @ZENOTME for fixing this, LGTM!

@Xuanwo
Copy link
Member

Xuanwo commented Jan 3, 2025

Thank you @ZENOTME for fixing this and thank you @liurenjie1024 for the review, let's merge!

@Xuanwo Xuanwo merged commit 1c632b8 into apache:main Jan 3, 2025
16 checks passed
@ZENOTME ZENOTME deleted the fix_decimal branch January 4, 2025 14:55
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants