Skip to content

Conversation

Weijun-H
Copy link
Member

@Weijun-H Weijun-H commented Oct 4, 2025

Which issue does this PR close?

Rationale for this change

What changes are included in this PR?

  • Supports Decimal256 values for log

Are these changes tested?

Yes

Are there any user-facing changes?

@github-actions github-actions bot added sqllogictest SQL Logic Tests (.slt) functions Changes to functions implementation labels Oct 4, 2025
@Weijun-H Weijun-H requested review from Jefffrey and Copilot October 4, 2025 15:03
Copy link
Contributor

@Copilot Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull Request Overview

This PR implements support for the log function with Decimal256 values that are larger than i128. Previously, the function would return a "Not yet implemented" error for large Decimal256 values.

  • Adds decimal256_to_i256 utility function for handling Decimal256 scaling
  • Implements proper Decimal256 logarithm calculation with fallback to f64 approximation for very large values
  • Updates test cases to verify the new functionality works correctly

Reviewed Changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated 3 comments.

File Description
datafusion/functions/src/utils.rs Adds decimal256_to_i256 utility function for converting Decimal256 values to unscaled i256
datafusion/functions/src/math/log.rs Implements full Decimal256 support in logarithm functions with i256_to_f64 conversion helper
datafusion/sqllogictest/test_files/decimal.slt Updates test cases to verify Decimal256 logarithm functionality instead of expecting errors

Tip: Customize your code reviews with copilot-instructions.md. Create the file or learn how to get started.

Comment on lines +187 to +190
// For larger values, use string conversion (less efficient but more accurate)
// Parse the string representation
let value_str = value.to_string();
value_str.parse::<f64>().unwrap_or(f64::INFINITY)
Copy link
Preview

Copilot AI Oct 4, 2025

Choose a reason for hiding this comment

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

String conversion and parsing is inefficient for i256 to f64 conversion. Consider implementing a more direct conversion method using bit manipulation or splitting the i256 into manageable chunks.

Suggested change
// For larger values, use string conversion (less efficient but more accurate)
// Parse the string representation
let value_str = value.to_string();
value_str.parse::<f64>().unwrap_or(f64::INFINITY)
// For larger values, convert using bit manipulation
// i256 is represented as 4 u64 limbs (little-endian)
let limbs = value.0;
let mut abs = [0u64; 4];
let negative = value.is_negative();
if negative {
// Two's complement: invert and add 1
let mut carry = true;
for (i, limb) in limbs.iter().enumerate() {
let inv = !limb;
let (res, overflow) = if carry {
inv.overflowing_add(1)
} else {
(inv, false)
};
abs[i] = res;
carry = overflow;
}
} else {
abs.copy_from_slice(&limbs);
}
// Combine limbs into f64
let mut result = 0f64;
for i in (0..4).rev() {
result = result * (1u64 << 64) as f64 + abs[i] as f64;
}
if negative {
-result
} else {
result
}

Copilot uses AI. Check for mistakes.

// For larger values, use string conversion (less efficient but more accurate)
// Parse the string representation
let value_str = value.to_string();
value_str.parse::<f64>().unwrap_or(f64::INFINITY)
Copy link
Preview

Copilot AI Oct 4, 2025

Choose a reason for hiding this comment

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

Using f64::INFINITY as a fallback for parse errors could mask legitimate parsing failures and produce incorrect logarithm results. Consider returning an error or using a more appropriate fallback value.

Copilot uses AI. Check for mistakes.

@Weijun-H Weijun-H marked this pull request as draft October 4, 2025 15:39
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
functions Changes to functions implementation sqllogictest SQL Logic Tests (.slt)
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Native decimal 256 bit support for log
1 participant