Skip to content

refactor: share scalar/array broadcast in math_funcs via values_to_arrays - #7726

Open
talz-a wants to merge 7 commits into
apache:mainfrom
talz-a:refactor-math-broadcast
Open

talz-a wants to merge 7 commits into
apache:mainfrom
talz-a:refactor-math-broadcast

Conversation

@talz-a

@talz-a talz-a commented Nov 21, 2026 •

Copy link
Copy Markdown

Which issue does this PR close?

Closes #6982.

Rationale for this change

As stated in the issue, these four call sites all duplicate code to turn a pair of ColumnarValues into ArrayRefs. We want to minimize duplicate code for potential future changes to how scalar broadcasting works.

What changes are included in this PR?

  • checked_arithmetic_internal, spark_decimal_div_internal, checked_float_modulo and
    WideDecimalBinaryExpr::evaluate now call ColumnarValue::values_to_arrays
    instead of their own match.
  • Unused ArrayRef imports are removed.
  • Used values_to_arrays rather than adding a broadcast_pair helper, because it already
    expands two scalars into length-1 arrays (array_len.unwrap_or(1)), which is what the old
    match did.
  • The match in ansi_arithmetic_kernel is left as is: it wraps scalars in arrow's Scalar so the
    kernel can use its scalar fast path, and values_to_arrays would materialize them into full arrays.
  • The one difference: two arrays of different lengths now return an error from
    values_to_arrays straight away, instead of failing later.

How are these changes tested?

No new tests are added, as existing tests cover the behaviour. Tests run with the following commands are green:

  • cargo test -p datafusion-comet-spark-expr
  • cargo clippy -p datafusion-comet-spark-expr --all-targets -- -D warnings
  • cargo fmt --all -- --check

@talz-a
talz-a marked this pull request as ready for review November 21, 2026 00:38

@zhangfengcdt zhangfengcdt left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Thanks for the refactoring and it is nice and clean! I found no issues.

@comphead comphead left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Behavior is identical on all four call sites. In DataFusion 55.2.0, ColumnarValue::values_to_arrays expands scalars to the array length and uses a length of 1 when every input is a scalar, which is what the removed matches did with to_array(). The scalar-in, scalar-out handling is untouched because checked_float_modulo and WideDecimalBinaryExpr::evaluate decide it from the original inputs before or after the call. checked_arithmetic_internal and spark_decimal_div_internal already returned arrays for two scalars. The only change is that two arrays of different lengths now fail up front with an internal error. This PR conflicts textually with #7811 in wide_decimal_binary_expr.rs (git merge-tree reports a content conflict near the left_arr and _p1 lines), which is easy to resolve by whichever PR lands second. I did not run any tests.

@comphead comphead left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

thanks @talz-a, the PR goes into right direction, please check some nits!

@talz-a
talz-a requested a review from comphead December 2, 2026 00:42
@talz-a
talz-a force-pushed the refactor-math-broadcast branch from 9f29ee4 to de6d883 Compare December 2, 2026 01:52
@github-actions github-actions Bot added enhancement New feature or request area:expressions Expression evaluation labels Dec 4, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area:expressions Expression evaluation enhancement New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Replace duplicated scalar/array broadcast match in math_funcs with a shared helper

3 participants