-
Notifications
You must be signed in to change notification settings - Fork 0
19926: fix: increase ROUND decimal precision to prevent overflow truncation #201
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
base: main
Are you sure you want to change the base?
Conversation
WalkthroughThe changes modify the decimal rounding implementation to include precision-aware validation. The round function is updated to increase decimal precision by 1 after rounding to accommodate potential carry, with validation applied across all decimal types (Decimal32, Decimal64, Decimal128, Decimal256). Dedicated validation helper functions are introduced for each decimal type. The return type logic is extended to map decimal inputs to higher-precision types. Both scalar and columnar execution paths are updated to apply this increased precision and validation. Test expectations are updated to reflect the new precision values, and new test cases are added for carry-over rounding scenarios and overflow handling. ✨ Finishing touches
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
Summary of ChangesHello @martin-augment, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed! This pull request addresses an issue where the Highlights
Using Gemini Code AssistThe full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips. Invoking Gemini You can request assistance from Gemini at any point by creating a comment using either
Customization To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a Limitations & Feedback Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here. You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension. Footnotes
|
PR Review: Fix ROUND decimal precision overflowSummaryThis PR addresses issue apache#19926 by increasing the precision of decimal values returned from the ROUND function to prevent overflow truncation. The fix handles cases where rounding can cause a carry-over that requires an additional digit (e.g., 999.9 → 1000.0). ✅ Code Quality & Best PracticesStrengths:
Suggestions:
🐛 Potential Bugs & IssuesCritical - Bounds checking: let max = MAX_DECIMAL32_FOR_EACH_PRECISION[precision as usize];While this should be safe because
Recommendation: Add a debug assertion or explicit bounds check: debug_assert!(
(precision as usize) < MAX_DECIMAL32_FOR_EACH_PRECISION.len(),
"Precision {} exceeds array bounds",
precision
);Minor - Potential information loss: let new_precision = (*precision + 1).min(DECIMAL128_MAX_PRECISION);However, this means values at max precision that would overflow are only caught at runtime via validation. The test case correctly verifies this produces an error (scalar.slt:975-976), which is good defensive behavior. ⚡ Performance ConsiderationsPositive:
Potential concern:
Analysis: This is likely acceptable because:
🔒 Security ConcernsNo significant security issues identified. The validation functions properly prevent:
The error messages don't leak sensitive information and are appropriate for debugging. 🧪 Test CoverageWell covered:
Potential gaps:
Recommendation: Add at least one test for columnar operations to ensure the validation logic works correctly in 📝 Overall AssessmentVerdict: Approve with minor suggestions This is a well-implemented fix that:
Before merging:
The code is production-ready, and the suggestions are for future maintainability rather than correctness concerns. Nice work! The fix properly handles decimal precision overflow while maintaining consistency with established database semantics. 🎉 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Code Review
This pull request correctly addresses the decimal overflow issue in the ROUND function by increasing the precision. The logic seems sound, and the added tests cover the fix and edge cases well. My main feedback is on improving code maintainability by reducing duplication. There are several places where almost identical code is used for Decimal32, Decimal64, Decimal128, and Decimal256. Using macros would make this code much cleaner and easier to maintain.
| /// Validate that an i32 (Decimal32) value fits within the specified precision. | ||
| /// Uses Arrow's pre-defined MAX/MIN_DECIMAL32_FOR_EACH_PRECISION constants. | ||
| fn validate_decimal32_precision(value: i32, precision: u8) -> Result<i32, ArrowError> { | ||
| let max = MAX_DECIMAL32_FOR_EACH_PRECISION[precision as usize]; | ||
| let min = MIN_DECIMAL32_FOR_EACH_PRECISION[precision as usize]; | ||
| if value > max || value < min { | ||
| return Err(ArrowError::ComputeError(format!( | ||
| "Decimal overflow: rounded value exceeds precision {precision}" | ||
| ))); | ||
| } | ||
| Ok(value) | ||
| } | ||
|
|
||
| fn validate_decimal64_precision(value: i64, precision: u8) -> Result<i64, ArrowError> { | ||
| let max = MAX_DECIMAL64_FOR_EACH_PRECISION[precision as usize]; | ||
| let min = MIN_DECIMAL64_FOR_EACH_PRECISION[precision as usize]; | ||
| if value > max || value < min { | ||
| return Err(ArrowError::ComputeError(format!( | ||
| "Decimal overflow: rounded value exceeds precision {precision}" | ||
| ))); | ||
| } | ||
| Ok(value) | ||
| } | ||
|
|
||
| fn validate_decimal128_precision(value: i128, precision: u8) -> Result<i128, ArrowError> { | ||
| let max = MAX_DECIMAL128_FOR_EACH_PRECISION[precision as usize]; | ||
| let min = MIN_DECIMAL128_FOR_EACH_PRECISION[precision as usize]; | ||
| if value > max || value < min { | ||
| return Err(ArrowError::ComputeError(format!( | ||
| "Decimal overflow: rounded value exceeds precision {precision}" | ||
| ))); | ||
| } | ||
| Ok(value) | ||
| } | ||
|
|
||
| fn validate_decimal256_precision( | ||
| value: arrow::datatypes::i256, | ||
| precision: u8, | ||
| ) -> Result<arrow::datatypes::i256, ArrowError> { | ||
| let max = MAX_DECIMAL256_FOR_EACH_PRECISION[precision as usize]; | ||
| let min = MIN_DECIMAL256_FOR_EACH_PRECISION[precision as usize]; | ||
| if value > max || value < min { | ||
| return Err(ArrowError::ComputeError(format!( | ||
| "Decimal overflow: rounded value exceeds precision {precision}" | ||
| ))); | ||
| } | ||
| Ok(value) | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
These validate_decimal..._precision functions are identical apart from the types. This code duplication can be eliminated by using a macro, which would make the code more maintainable.
macro_rules! define_validate_decimal_precision {
($name:ident, $ty:ty, $max_const:ident, $min_const:ident) => {
fn $name(value: $ty, precision: u8) -> Result<$ty, ArrowError> {
let max = $max_const[precision as usize];
let min = $min_const[precision as usize];
if value > max || value < min {
return Err(ArrowError::ComputeError(format!(
"Decimal overflow: rounded value exceeds precision {precision}"
)));
}
Ok(value)
}
};
}
define_validate_decimal_precision!(
validate_decimal32_precision,
i32,
MAX_DECIMAL32_FOR_EACH_PRECISION,
MIN_DECIMAL32_FOR_EACH_PRECISION
);
define_validate_decimal_precision!(
validate_decimal64_precision,
i64,
MAX_DECIMAL64_FOR_EACH_PRECISION,
MIN_DECIMAL64_FOR_EACH_PRECISION
);
define_validate_decimal_precision!(
validate_decimal128_precision,
i128,
MAX_DECIMAL128_FOR_EACH_PRECISION,
MIN_DECIMAL128_FOR_EACH_PRECISION
);
define_validate_decimal_precision!(
validate_decimal256_precision,
arrow::datatypes::i256,
MAX_DECIMAL256_FOR_EACH_PRECISION,
MIN_DECIMAL256_FOR_EACH_PRECISION
);There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
value:good-to-have; category:bug; feedback:The Gemini AI reviewer is correct! By using a macro the code duplication could be reduced a lot. Prevents higher maintenance cost in the future for all the duplicated code.
🤖 Augment PR SummarySummary: This PR adjusts the behavior of the SQL Changes:
Technical Notes: The precision bump is intended to match behavior seen in systems like PostgreSQL where rounding can increase digits before the decimal point; values that still exceed the (possibly capped) precision now error rather than silently truncating. 🤖 Was this summary useful? React with 👍 or 👎 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Review completed. No suggestions at this time.
Comment augment review to trigger a new review at any time.
19926: To review by AI