Skip to content

Conversation

@Jefffrey
Copy link
Contributor

@Jefffrey Jefffrey commented Nov 7, 2025

Ensure the right value gets casted if it is a scalar (currently we only cast if it is an array).

Remove unused arm for casting left value.

@github-actions github-actions bot added the functions Changes to functions implementation label Nov 7, 2025
{
Ok(match right {
let left = left.as_primitive::<L>();
let right = right.cast_to(&R::DATA_TYPE, None)?;
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This right cast wasn't applied if it was a scalar, so cast the columnarvalue itself to align both arms

Copy link
Contributor

Choose a reason for hiding this comment

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

I wonder why we aren't also casting left as well up here (it was only done in the branch below 🤔 )

let right_array = right_casted.as_primitive::<R>();

// Types are compatible even they are decimals with different scale or precision
let result = if PrimitiveArray::<L>::is_compatible(&L::DATA_TYPE) {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This is_compatible check would always be true since we're essentially comparing L generic with itself; this seemed like a mistake so I'm removing it

Copy link
Contributor

Choose a reason for hiding this comment

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

The documentation of https://docs.rs/arrow/latest/arrow/array/struct.PrimitiveArray.html#method.is_compatible says

This is equivalent to data_type == T::DATA_TYPE, however ignores timestamp timezones and decimal precision and scale

So maybe it is something related to timezones?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It's possible it was meant to check compatibility of the left array with L generic type itself; but given no tests failed I'm not sure if the branch would be reached anyway 🤔

Copy link
Contributor

Choose a reason for hiding this comment

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

The change looks ok. Seem like I intended to check extra casting to the right array's type, but placed an always-true check. Given we tend to use coercing (like in #18032) instead of specifying exact types, it wouldn't be required.

@Jefffrey Jefffrey marked this pull request as ready for review November 7, 2025 06:55
Copy link
Contributor

@alamb alamb left a comment

Choose a reason for hiding this comment

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

The code looks better to me and all the tests are passing so I think it is good to go from my perspective 👍

let right_array = right_casted.as_primitive::<R>();

// Types are compatible even they are decimals with different scale or precision
let result = if PrimitiveArray::<L>::is_compatible(&L::DATA_TYPE) {
Copy link
Contributor

Choose a reason for hiding this comment

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

The documentation of https://docs.rs/arrow/latest/arrow/array/struct.PrimitiveArray.html#method.is_compatible says

This is equivalent to data_type == T::DATA_TYPE, however ignores timestamp timezones and decimal precision and scale

So maybe it is something related to timezones?

{
Ok(match right {
let left = left.as_primitive::<L>();
let right = right.cast_to(&R::DATA_TYPE, None)?;
Copy link
Contributor

Choose a reason for hiding this comment

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

I wonder why we aren't also casting left as well up here (it was only done in the branch below 🤔 )

@Jefffrey Jefffrey added this pull request to the merge queue Nov 8, 2025
Merged via the queue into apache:main with commit 8259b35 Nov 8, 2025
28 checks passed
@Jefffrey Jefffrey deleted the refactor-calculate_binary_math branch November 8, 2025 00:19
hsiang-c pushed a commit to hsiang-c/datafusion that referenced this pull request Nov 9, 2025
…pache#18525)

Ensure the `right` value gets casted if it is a scalar (currently we
only cast if it is an array).

Remove unused arm for casting left value.
codetyri0n pushed a commit to codetyri0n/datafusion that referenced this pull request Nov 11, 2025
…pache#18525)

Ensure the `right` value gets casted if it is a scalar (currently we
only cast if it is an array).

Remove unused arm for casting left value.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

functions Changes to functions implementation

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants