Skip to content

Conversation

@kyle-mccarthy
Copy link
Contributor

Which issue does this PR close?

Closes #6155

Rationale for this change

Currently, scalars cannot appear as the LHS arg in arithmetic operations on temporal values. This updates the relevant logic to accept scalars as the first argument.

What changes are included in this PR?

Updates the temporal arithmetic operations to accept scalars as the LHS arg.

Are these changes tested?

A test has been added for this new case.

Are there any user-facing changes?

No

@github-actions github-actions bot added the physical-expr Changes to the physical-expr crates label May 3, 2023
@kyle-mccarthy kyle-mccarthy marked this pull request as ready for review May 3, 2023 07:19
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 perfect! Thank you @kyle-mccarthy 👏

In terms of improving the test coverage, I think we should use a sqllogictest instead of using rust tests (though I realize you were simply following the model of existing tests)

The sqllogictest is here:
https://github.com/apache/arrow-datafusion/tree/main/datafusion/core/tests/sqllogictests

While working on a suggestion for you, I couldn't find existing sql level arithmetic tests, so I wrote some in: #6201

Thus I suggest you remove the tests from this PR, and we can use the test sin #6201 to verify your fix

(ColumnarValue::Array(array_lhs), ColumnarValue::Scalar(array_rhs)) => {
resolve_temporal_op_scalar(&array_lhs, sign, &array_rhs)
}
// This function evaluates operations between a scalar value and an array of temporal
Copy link
Contributor

Choose a reason for hiding this comment

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

👍

}

#[test]
fn test_subtract_array_from_scalar() -> Result<()> {
Copy link
Contributor

Choose a reason for hiding this comment

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

This is a neat test but I think we may be able to write it more concisely and get better end to end coverage by using sqllogictest. I will explain how to do that in the review comments

// array of timestamps (i.e. `now() - some_column`).
(ColumnarValue::Scalar(scalar_lhs), ColumnarValue::Array(array_rhs)) => {
let array_lhs = scalar_lhs.to_array_of_size(array_rhs.len());
Ok(ColumnarValue::Array(resolve_temporal_op(
Copy link
Contributor

@berkaysynnada berkaysynnada May 3, 2023

Choose a reason for hiding this comment

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

The purpose of the resolve_temporal_op is actually to work with real array types. Otherwise, would we need specialized handlers for scalar types? I think this case can be handled in two ways:

  1. resolve_temporal_op_scalar is extended with a commute parameter. This pattern is applied in impl_op_arithmetic. The same functions should be able to handle Scalar op Array and Array op Scalar.
  2. A similar resolve_temporal_op_scalar function can be added with replaced parameters of Array and Scalar.

Thank you for working on this issue. I can assist further if needed.

Copy link
Contributor

Choose a reason for hiding this comment

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

@berkaysynnada are you suggesting we should change this PR? Or can it be merged as is?

I am not quite sure what action your comment is suggesting

Copy link
Contributor

Choose a reason for hiding this comment

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

@berkaysynnada are you suggesting we should change this PR? Or can it be merged as is?

I am not quite sure what action your comment is suggesting

There is an operation creating an array from the scalar value. My suggestions intended to prevent this. I think we can merge this PR, I can update that part with a quick PR in the following days.

@kyle-mccarthy
Copy link
Contributor Author

@alamb thanks for the review! I removed the test from this PR and noted in the commit that an equivalent test will be added in #6201

@alamb
Copy link
Contributor

alamb commented May 4, 2023

Thanks @kyle-mccarthy -- I took the liberty of merging up from main and fixing a clippy issue. I'll then rebase #6201 on this one 👍

@alamb alamb merged commit 62127df into apache:main May 4, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

physical-expr Changes to the physical-expr crates

Projects

None yet

Development

Successfully merging this pull request may close these issues.

arithmetic operations on timestamps fail when now() is used as the LHS argument

3 participants