Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: support nvl2 function #9364

Merged
merged 2 commits into from
Feb 28, 2024
Merged

feat: support nvl2 function #9364

merged 2 commits into from
Feb 28, 2024

Conversation

guojidan
Copy link
Contributor

Which issue does this PR close?

Closes #9363.

Rationale for this change

What changes are included in this PR?

add nvl2 function

Are these changes tested?

Are there any user-facing changes?

@github-actions github-actions bot added the sqllogictest SQL Logic Tests (.slt) label Feb 27, 2024
@guojidan
Copy link
Contributor Author

if this pr is merged, we can rewrite nvl/ifnull function to nvl2, like:
nvl(expr1, expr2) -> nvl2(expr1, expr1, expr2)

🤔

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.

Thank you @guojidan

I left some suggestions on how this code could potentially be clearned up but I think this PR is well tested and well documented so it could also be merged as is

/// Currently supported types by the nvl/ifnull function.
/// The order of these types correspond to the order on which coercion applies
/// This should thus be from least informative to most informative
static SUPPORTED_NVL2_TYPES: &[DataType] = &[
Copy link
Contributor

Choose a reason for hiding this comment

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

I don't think we need this list of data types, do we? Why wouldn't NVL2 support any data type? Its logic isn't specific to the type (only if the argument is null).

It seems like we could remove the list from nvl as well

Copy link
Contributor Author

Choose a reason for hiding this comment

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

maybe Signature::variadic_equal(Volatility::Immutable) more suitable? don't need specific type 🤔

use datafusion_common::{Result, ScalarValue};

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

Choose a reason for hiding this comment

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

Do these tests add any additional coverage over what is in the nvl2.slt file? I feel like they are the same test logic encoded twice (as in it seems like any bug would cause both the slt and the rust tests to fail)

If they are the same, I think we should remove the rust based tests and stay with slt

Copy link
Contributor Author

Choose a reason for hiding this comment

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

yes, This is indeed two similar tests, I will remove rust based tests

;

# Arrays tests
query I
Copy link
Contributor

Choose a reason for hiding this comment

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

I would recommend adding an ORDER BY to these queries to ensure the order is the same as listed in the tests

for example

SELECT NVL2(int_field, 2, 3) FROM test ORDER BY more_ints;

@alamb
Copy link
Contributor

alamb commented Feb 27, 2024

if this pr is merged, we can rewrite nvl/ifnull function to nvl2, like: nvl(expr1, expr2) -> nvl2(expr1, expr1, expr2)

🤔

I think this would be a great idea.

@alamb
Copy link
Contributor

alamb commented Feb 28, 2024

Thanks again @guojidan

@alamb alamb merged commit 5f90ead into apache:main Feb 28, 2024
24 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
sqllogictest SQL Logic Tests (.slt)
Projects
None yet
Development

Successfully merging this pull request may close these issues.

feature: support NVL2 function
2 participants