You are viewing a plain text version of this content. The canonical link for it is here.
Posted to github@datafusion.apache.org by "kumarUjjawal (via GitHub)" <gi...@apache.org> on 2025/12/23 07:12:40 UTC

[PR] Decimal128 support for statistical aggregations [datafusion]

kumarUjjawal opened a new pull request, #19463:
URL: https://github.com/apache/datafusion/pull/19463

   ## Which issue does this PR close?
   
   <!--
   We generally require a GitHub issue to be filed for all bug fixes and enhancements and this helps us generate change logs for our releases. You can link an issue to this PR using the GitHub syntax. For example `Closes #123` indicates that this PR will close issue #123.
   -->
   
   - Closes #3572 and #3481
   
   ## Rationale for this change
   
   - Statistical aggregates like covar_*, corr, and regr_* rejected Decimal128 (and some mixed numeric inputs)
   
   <!--
    Why are you proposing this change? If this is already explained clearly in the issue then this section is not needed.
    Explaining clearly why changes are proposed helps reviewers understand your changes and offer better suggestions for fixes.  
   -->
   
   ## What changes are included in this PR?
   
    - Broadened statistical aggregate signatures (covar_*, corr, regr_*) to accept numeric/decimal inputs via `TypeSignature::Coercible`
     - Updated ordered-set percentile aggregates to accept decimal percentiles by coercing them to `Float64`
   
   <!--
   There is no need to duplicate the description in the issue here but it is sometimes worth providing a summary of the individual changes in this PR.
   -->
   
   ## Are these changes tested?
   
   - Added sqllogictest coverage for decimals: NULL handling, decimal(38,0) boundary, and mixed decimal/float coercion cases.
   
   <!--
   We typically require tests for all PRs in order to:
   1. Prevent the code from being accidentally broken by subsequent changes
   2. Serve as another way to document the expected behavior of the code
   
   If tests are not included in your PR, please explain why (for example, are they covered by existing tests)?
   -->
   
   ## Are there any user-facing changes?
   
   <!--
   If there are user-facing changes then we may require documentation to be updated before approving the PR.
   -->
   
   <!--
   If there are any breaking changes to public APIs, please add the `api change` label.
   -->
   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For additional commands, e-mail: github-help@datafusion.apache.org


Re: [PR] Decimal128 support for statistical aggregations [datafusion]

Posted by "Jefffrey (via GitHub)" <gi...@apache.org>.
Jefffrey commented on code in PR #19463:
URL: https://github.com/apache/datafusion/pull/19463#discussion_r2643680653


##########
datafusion/functions-aggregate/src/correlation.rs:
##########
@@ -85,8 +86,11 @@ impl Correlation {
     /// Create a new CORR aggregate function
     pub fn new() -> Self {
         Self {
-            signature: Signature::exact(
-                vec![DataType::Float64, DataType::Float64],
+            signature: Signature::coercible(
+                vec![
+                    Coercion::new_exact(TypeSignatureClass::Numeric),
+                    Coercion::new_exact(TypeSignatureClass::Numeric),
+                ],

Review Comment:
   Yes that is a good course of action



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For additional commands, e-mail: github-help@datafusion.apache.org


Re: [PR] Decimal128 support for statistical aggregations [datafusion]

Posted by "Jefffrey (via GitHub)" <gi...@apache.org>.
Jefffrey commented on code in PR #19463:
URL: https://github.com/apache/datafusion/pull/19463#discussion_r2642434464


##########
datafusion/functions-aggregate/src/correlation.rs:
##########
@@ -85,8 +86,11 @@ impl Correlation {
     /// Create a new CORR aggregate function
     pub fn new() -> Self {
         Self {
-            signature: Signature::exact(
-                vec![DataType::Float64, DataType::Float64],
+            signature: Signature::coercible(
+                vec![
+                    Coercion::new_exact(TypeSignatureClass::Numeric),
+                    Coercion::new_exact(TypeSignatureClass::Numeric),
+                ],

Review Comment:
   I am not sure of this approach of accepting all numeric types at the signature level only to cast it internally 🤔 



##########
datafusion/functions-aggregate/src/approx_percentile_cont.rs:
##########
@@ -133,18 +133,30 @@ impl Default for ApproxPercentileCont {
 impl ApproxPercentileCont {
     /// Create a new [`ApproxPercentileCont`] aggregate function.
     pub fn new() -> Self {
-        let mut variants = Vec::with_capacity(NUMERICS.len() * (INTEGERS.len() + 1));
-        // Accept any numeric value paired with a float64 percentile
-        for num in NUMERICS {
-            variants.push(TypeSignature::Exact(vec![num.clone(), DataType::Float64]));
-            // Additionally accept an integer number of centroids for T-Digest
-            for int in INTEGERS {
-                variants.push(TypeSignature::Exact(vec![
-                    num.clone(),
-                    DataType::Float64,
-                    int.clone(),
-                ]))
-            }
+        let percentile_coercion = Coercion::new_implicit(
+            TypeSignatureClass::Native(logical_float64()),
+            vec![
+                TypeSignatureClass::Integer,
+                TypeSignatureClass::Float,
+                TypeSignatureClass::Decimal,
+            ],
+            NativeType::Float64,
+        );
+
+        // Value must be numeric (excluding Decimal) as the accumulator currently only
+        // supports integers and floats. Percentile may be any numeric literal that can
+        // be coerced to Float64 (to support parse_float_as_decimal).
+        let mut variants = Vec::with_capacity(4);
+        for value_class in [TypeSignatureClass::Integer, TypeSignatureClass::Float] {
+            variants.push(TypeSignature::Coercible(vec![
+                Coercion::new_exact(value_class.clone()),
+                percentile_coercion.clone(),
+            ]));
+            variants.push(TypeSignature::Coercible(vec![
+                Coercion::new_exact(value_class),
+                percentile_coercion.clone(),

Review Comment:
   I'm hesitant to accept a signature like this, since I don't like the look of this and it would need to be refactored anyway



##########
datafusion/expr/src/type_coercion/functions.rs:
##########
@@ -673,12 +673,12 @@ fn get_valid_types(
                         default_casted_type.default_cast_for(current_type)?;
                     new_types.push(casted_type);
                 } else {
-                    return internal_err!(
-                        "Expect {} but received NativeType::{}, DataType: {}",
-                        param.desired_type(),
-                        current_native_type,
-                        current_type
-                    );
+                    // No valid coercion for this signature given the current type.
+                    // Return an empty set so the higher-level signature matching
+                    // logic can continue checking other variants (e.g. `OneOf`)
+                    // and/or produce a consistent "Failed to coerce arguments"
+                    // error message.
+                    return Ok(vec![]);

Review Comment:
   We should not be changing this here; I think we need more consideration before changing this



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For additional commands, e-mail: github-help@datafusion.apache.org


Re: [PR] Decimal128 support for statistical aggregations [datafusion]

Posted by "kumarUjjawal (via GitHub)" <gi...@apache.org>.
kumarUjjawal commented on code in PR #19463:
URL: https://github.com/apache/datafusion/pull/19463#discussion_r2643673442


##########
datafusion/functions-aggregate/src/correlation.rs:
##########
@@ -85,8 +86,11 @@ impl Correlation {
     /// Create a new CORR aggregate function
     pub fn new() -> Self {
         Self {
-            signature: Signature::exact(
-                vec![DataType::Float64, DataType::Float64],
+            signature: Signature::coercible(
+                vec![
+                    Coercion::new_exact(TypeSignatureClass::Numeric),
+                    Coercion::new_exact(TypeSignatureClass::Numeric),
+                ],

Review Comment:
   Should I reduce this PR to just variance/stddev as a starting point and move the cast to the signature level instead of doing it internally? We can change others in the follow up PRs? If you have other directions in mind, let me know.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For additional commands, e-mail: github-help@datafusion.apache.org


Re: [PR] Decimal128 support for statistical aggregations [datafusion]

Posted by "kumarUjjawal (via GitHub)" <gi...@apache.org>.
kumarUjjawal commented on PR #19463:
URL: https://github.com/apache/datafusion/pull/19463#issuecomment-3687601864

   I going to make this as draft for now till I have better ways to handle the decimal support for the statistical aggregations.


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For additional commands, e-mail: github-help@datafusion.apache.org


Re: [PR] Decimal128 support for statistical aggregations [datafusion]

Posted by "kumarUjjawal (via GitHub)" <gi...@apache.org>.
kumarUjjawal commented on PR #19463:
URL: https://github.com/apache/datafusion/pull/19463#issuecomment-3685753889

   @Jefffrey need your feedback, whether I should update the slt tests according to the code changes or keep the tests as it is and make changes to my code?


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For additional commands, e-mail: github-help@datafusion.apache.org


Re: [PR] Decimal128 support for statistical aggregations [datafusion]

Posted by "kumarUjjawal (via GitHub)" <gi...@apache.org>.
kumarUjjawal closed pull request #19463: Decimal128 support for statistical aggregations
URL: https://github.com/apache/datafusion/pull/19463


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For additional commands, e-mail: github-help@datafusion.apache.org


Re: [PR] Decimal128 support for statistical aggregations [datafusion]

Posted by "Jefffrey (via GitHub)" <gi...@apache.org>.
Jefffrey commented on code in PR #19463:
URL: https://github.com/apache/datafusion/pull/19463#discussion_r2643654286


##########
datafusion/functions-aggregate/src/correlation.rs:
##########
@@ -85,8 +86,11 @@ impl Correlation {
     /// Create a new CORR aggregate function
     pub fn new() -> Self {
         Self {
-            signature: Signature::exact(
-                vec![DataType::Float64, DataType::Float64],
+            signature: Signature::coercible(
+                vec![
+                    Coercion::new_exact(TypeSignatureClass::Numeric),
+                    Coercion::new_exact(TypeSignatureClass::Numeric),
+                ],

Review Comment:
   I'm not a fan of the approach to do the cast internally in the function implementation, especially if that can be represented at the signature level instead.
   
   I feel we should take a more targeted approach, one by one, instead of trying all these functions together, as they seem to handle things differently in subtle ways.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For additional commands, e-mail: github-help@datafusion.apache.org


Re: [PR] Decimal128 support for statistical aggregations [datafusion]

Posted by "kumarUjjawal (via GitHub)" <gi...@apache.org>.
kumarUjjawal commented on code in PR #19463:
URL: https://github.com/apache/datafusion/pull/19463#discussion_r2643717874


##########
datafusion/functions-aggregate/src/correlation.rs:
##########
@@ -85,8 +86,11 @@ impl Correlation {
     /// Create a new CORR aggregate function
     pub fn new() -> Self {
         Self {
-            signature: Signature::exact(
-                vec![DataType::Float64, DataType::Float64],
+            signature: Signature::coercible(
+                vec![
+                    Coercion::new_exact(TypeSignatureClass::Numeric),
+                    Coercion::new_exact(TypeSignatureClass::Numeric),
+                ],

Review Comment:
   Great!



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For additional commands, e-mail: github-help@datafusion.apache.org


Re: [PR] Decimal128 support for statistical aggregations [datafusion]

Posted by "kumarUjjawal (via GitHub)" <gi...@apache.org>.
kumarUjjawal commented on code in PR #19463:
URL: https://github.com/apache/datafusion/pull/19463#discussion_r2642562896


##########
datafusion/functions-aggregate/src/correlation.rs:
##########
@@ -85,8 +86,11 @@ impl Correlation {
     /// Create a new CORR aggregate function
     pub fn new() -> Self {
         Self {
-            signature: Signature::exact(
-                vec![DataType::Float64, DataType::Float64],
+            signature: Signature::coercible(
+                vec![
+                    Coercion::new_exact(TypeSignatureClass::Numeric),
+                    Coercion::new_exact(TypeSignatureClass::Numeric),
+                ],

Review Comment:
    I was thinking that the current NUMERICS list explicitly excludes Decimals (see 
   [aggregates.rs](https://github.com/apache/datafusion/blob/main/datafusion/expr-common/src/type_coercion/aggregates.rs#L25-L34)). These functions already cast to Float64 internally (https://github.com/apache/datafusion/blob/33ac70dd6d634da040cc34abd414425b176a2b99/datafusion/functions-aggregate/src/variance.rs#L333) 
   
   and return Float64, so Decimals can be handled the same way integers are.
   



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For additional commands, e-mail: github-help@datafusion.apache.org


Re: [PR] Decimal128 support for statistical aggregations [datafusion]

Posted by "kumarUjjawal (via GitHub)" <gi...@apache.org>.
kumarUjjawal commented on code in PR #19463:
URL: https://github.com/apache/datafusion/pull/19463#discussion_r2642584198


##########
datafusion/functions-aggregate/src/correlation.rs:
##########
@@ -85,8 +86,11 @@ impl Correlation {
     /// Create a new CORR aggregate function
     pub fn new() -> Self {
         Self {
-            signature: Signature::exact(
-                vec![DataType::Float64, DataType::Float64],
+            signature: Signature::coercible(
+                vec![
+                    Coercion::new_exact(TypeSignatureClass::Numeric),
+                    Coercion::new_exact(TypeSignatureClass::Numeric),
+                ],

Review Comment:
   can we use ```signature: Signature::numeric(2, Volatility::Immutable)``` instead of the current?



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For additional commands, e-mail: github-help@datafusion.apache.org


Re: [PR] Decimal128 support for statistical aggregations [datafusion]

Posted by "kumarUjjawal (via GitHub)" <gi...@apache.org>.
kumarUjjawal commented on code in PR #19463:
URL: https://github.com/apache/datafusion/pull/19463#discussion_r2642562896


##########
datafusion/functions-aggregate/src/correlation.rs:
##########
@@ -85,8 +86,11 @@ impl Correlation {
     /// Create a new CORR aggregate function
     pub fn new() -> Self {
         Self {
-            signature: Signature::exact(
-                vec![DataType::Float64, DataType::Float64],
+            signature: Signature::coercible(
+                vec![
+                    Coercion::new_exact(TypeSignatureClass::Numeric),
+                    Coercion::new_exact(TypeSignatureClass::Numeric),
+                ],

Review Comment:
    I was thinking that the current NUMERICS list explicitly excludes Decimals (
   [aggregates.rs](https://github.com/apache/datafusion/blob/main/datafusion/expr-common/src/type_coercion/aggregates.rs#L25-L34)). These functions already cast to Float64 internally (https://github.com/apache/datafusion/blob/33ac70dd6d634da040cc34abd414425b176a2b99/datafusion/functions-aggregate/src/variance.rs#L333) 
   
   and return Float64, so Decimals can be handled the same way integers are.
   



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For additional commands, e-mail: github-help@datafusion.apache.org


Re: [PR] Decimal128 support for statistical aggregations [datafusion]

Posted by "github-actions[bot] (via GitHub)" <gi...@apache.org>.
github-actions[bot] commented on PR #19463:
URL: https://github.com/apache/datafusion/pull/19463#issuecomment-3939932148

   Thank you for your contribution. Unfortunately, this pull request is stale because it has been open 60 days with no activity. Please remove the stale label or comment or this will be closed in 7 days.


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For additional commands, e-mail: github-help@datafusion.apache.org


Re: [PR] Decimal128 support for statistical aggregations [datafusion]

Posted by "kumarUjjawal (via GitHub)" <gi...@apache.org>.
kumarUjjawal commented on PR #19463:
URL: https://github.com/apache/datafusion/pull/19463#issuecomment-4325315161

   Closing this I am working on another pr for this.


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For additional commands, e-mail: github-help@datafusion.apache.org