This is an automated email from the ASF dual-hosted git repository.

zeroshade pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/arrow-go.git


The following commit(s) were added to refs/heads/main by this push:
     new 46603e72 fix(arrow/compute): promote dictionary decimal inputs before 
dispatch (#1662)
46603e72 is described below

commit 46603e7236ab74453127c190c9942f1eb95848a4
Author: Nathan Chandrasekar <[email protected]>
AuthorDate: Mon Nov 23 13:26:35 2026 -0500

    fix(arrow/compute): promote dictionary decimal inputs before dispatch 
(#1662)
    
    ### Rationale for this change
    
    When one or both inputs are dictionary-encoded decimals, comparison and
    arithmetic functions skip the decimal scale promotion. For example, with
    a dictionary<int8, decimal128(3, 0)> array `[1]` and a decimal128(3, 1)
    array `[1.0]` (dictionary or plain), `equal` returns false, `less`
    returns true, `add` returns 11 and `divide` returns 0. The same values
    without dictionary encoding give true, false, 2.0 and 1.0000. The same
    happens for decimal256.
    
    The cause is the order in `DispatchBest`: `compareFunction` and
    `arithmeticFunction` run the decimal promotion (`castBinaryDecimalArgs`
    / `checkDecimals`) before `ensureDictionaryDecoded`, so the promotion
    sees dictionary types, does nothing, and the kernels then run on the
    unscaled values.
    
    ### What changes are included in this PR?
    
    `ensureDictionaryDecoded` is now called before the decimal promotion in
    `compareFunction.DispatchBest` and `arithmeticFunction.DispatchBest`, so
    dictionary value types take part in it like plain decimal inputs.
    
    ### Are these changes tested?
    
    Yes. `TestBinaryArithmetic/TestDictionaryPromotion` checks `equal`,
    `less`, `add` and `divide` for decimal128 and decimal256 with
    dictionary/dictionary, dictionary/plain and plain/dictionary inputs
    `["1", null, "2"]` (scale 0) and `["1.0", null, "0.5"]` (scale 1),
    including the result types. All 24 cases fail without the fix and pass
    with it; the `./arrow/compute/...` and decimal package tests pass.
    
    ### Are there any user-facing changes?
    
    Yes. Comparisons and arithmetic on dictionary-encoded decimals now
    return the same values and decimal result types as on the decoded
    inputs.
---
 arrow/compute/arithmetic.go      |  4 ++--
 arrow/compute/arithmetic_test.go | 47 ++++++++++++++++++++++++++++++++++++++++
 arrow/compute/scalar_compare.go  |  3 ++-
 3 files changed, 51 insertions(+), 3 deletions(-)

diff --git a/arrow/compute/arithmetic.go b/arrow/compute/arithmetic.go
index fcf3cc26..2a0d1ce2 100644
--- a/arrow/compute/arithmetic.go
+++ b/arrow/compute/arithmetic.go
@@ -114,6 +114,8 @@ func (fn *arithmeticFunction) DispatchBest(vals 
...arrow.DataType) (exec.Kernel,
                return nil, err
        }
 
+       ensureDictionaryDecoded(vals...)
+
        if err := fn.checkDecimals(vals...); err != nil {
                return nil, err
        }
@@ -122,8 +124,6 @@ func (fn *arithmeticFunction) DispatchBest(vals 
...arrow.DataType) (exec.Kernel,
                return kn, nil
        }
 
-       ensureDictionaryDecoded(vals...)
-
        // only promote types for binary funcs
        if len(vals) == 2 {
                replaceNullWithOtherType(vals...)
diff --git a/arrow/compute/arithmetic_test.go b/arrow/compute/arithmetic_test.go
index 1ca688cf..a588ce07 100644
--- a/arrow/compute/arithmetic_test.go
+++ b/arrow/compute/arithmetic_test.go
@@ -814,6 +814,53 @@ type DecimalBinaryArithmeticSuite struct {
        DecimalArithmeticSuite
 }
 
+func (ds *DecimalBinaryArithmeticSuite) TestDictionaryPromotion() {
+       for _, id := range []arrow.Type{arrow.DECIMAL128, arrow.DECIMAL256} {
+               ds.Run(id.String(), func() {
+                       leftType, _ := arrow.NewDecimalType(id, 3, 0)
+                       rightType, _ := arrow.NewDecimalType(id, 3, 1)
+                       addType, _ := arrow.NewDecimalType(id, 5, 1)
+                       divideType, _ := arrow.NewDecimalType(id, 8, 4)
+                       leftDict := &arrow.DictionaryType{IndexType: 
arrow.PrimitiveTypes.Int8, ValueType: leftType}
+                       rightDict := &arrow.DictionaryType{IndexType: 
arrow.PrimitiveTypes.Int8, ValueType: rightType}
+
+                       for _, inputs := range []struct {
+                               name        string
+                               left, right arrow.DataType
+                       }{
+                               {"dictionary_dictionary", leftDict, rightDict},
+                               {"dictionary_plain", leftDict, rightType},
+                               {"plain_dictionary", leftType, rightDict},
+                       } {
+                               ds.Run(inputs.name, func() {
+                                       left := ds.getArr(inputs.left, `["1", 
null, "2"]`)
+                                       defer left.Release()
+                                       right := ds.getArr(inputs.right, 
`["1.0", null, "0.5"]`)
+                                       defer right.Release()
+
+                                       for _, tc := range []struct {
+                                               fn       string
+                                               typ      arrow.DataType
+                                               expected string
+                                       }{
+                                               {"equal", 
arrow.FixedWidthTypes.Boolean, `[true, null, false]`},
+                                               {"less", 
arrow.FixedWidthTypes.Boolean, `[false, null, false]`},
+                                               {"add", addType, `["2.0", null, 
"2.5"]`},
+                                               {"divide", divideType, 
`["1.0000", null, "4.0000"]`},
+                                       } {
+                                               ds.Run(tc.fn, func() {
+                                                       expected := 
ds.getArr(tc.typ, tc.expected)
+                                                       defer expected.Release()
+                                                       
checkScalarBinary(ds.T(), tc.fn, compute.NewDatumWithoutOwning(left),
+                                                               
compute.NewDatumWithoutOwning(right), compute.NewDatumWithoutOwning(expected), 
nil)
+                                               })
+                                       }
+                               })
+                       }
+               })
+       }
+}
+
 func (ds *DecimalBinaryArithmeticSuite) TestDispatchBest() {
        // decimal, floating point
        ds.Run("dec/floatingpoint", func() {
diff --git a/arrow/compute/scalar_compare.go b/arrow/compute/scalar_compare.go
index 21c0b414..2c160d8f 100644
--- a/arrow/compute/scalar_compare.go
+++ b/arrow/compute/scalar_compare.go
@@ -42,6 +42,8 @@ func (fn *compareFunction) DispatchBest(vals 
...arrow.DataType) (exec.Kernel, er
                return nil, err
        }
 
+       ensureDictionaryDecoded(vals...)
+
        if hasDecimal(vals...) {
                if err := castBinaryDecimalArgs(decPromoteAdd, vals...); err != 
nil {
                        return nil, err
@@ -55,7 +57,6 @@ func (fn *compareFunction) DispatchBest(vals 
...arrow.DataType) (exec.Kernel, er
                return kn, nil
        }
 
-       ensureDictionaryDecoded(vals...)
        ensureNoExtensionType(vals...)
        replaceNullWithOtherType(vals...)
        if err := validateTimestampTimezones(vals...); err != nil {

Reply via email to