Skip to content

Conversation

@kosiew
Copy link
Contributor

@kosiew kosiew commented Feb 27, 2025

Which issue does this PR close?

Rationale for this change

Currently, when passing a NULL value to to_char, it returns an empty string instead of NULL. This behavior differs from PostgreSQL, Spark, DuckDb, which returns NULL when given a NULL input. Aligning with common practise ensures consistency and expected behavior for users relying on SQL standards.

What changes are included in this PR?

Modify to_char function implementation to return NULL when the input is NULL.

Update relevant test cases to validate the correct behavior.

Ensure consistent behavior across different data types.

Are these changes tested?

Yes, test cases have been added to check the NULL behavior of to_char.

Existing test cases have been updated where necessary.

Verified that the changes do not introduce regressions.

Are there any user-facing changes?

Users who previously received an empty string when calling to_char(NULL, format) will now receive NULL, aligning with PostgreSQL behavior.

No breaking changes to public APIs, but behavior will be corrected to match expected SQL standards.

@github-actions github-actions bot added sqllogictest SQL Logic Tests (.slt) functions Changes to functions implementation labels Feb 27, 2025
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.

Makes sense to me - thank you @kosiew

cc @Omega359

@Omega359
Copy link
Contributor

LGTM

}

#[test]
fn test_to_char_input_none_scalar() {
Copy link
Contributor

Choose a reason for hiding this comment

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

we don't need rust test for this function, we can add it to 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.

Thanks for spotting this.
Removed.

@alamb
Copy link
Contributor

alamb commented Feb 28, 2025

Thanks @kosiew and @jayzhan211

@alamb alamb merged commit 8bc889f into apache:main Feb 28, 2025
24 checks passed
kosiew added a commit to kosiew/datafusion that referenced this pull request Mar 3, 2025
kosiew added a commit to kosiew/datafusion that referenced this pull request Mar 3, 2025
alamb pushed a commit that referenced this pull request Mar 5, 2025
…#14970)

* revert _to_char_scalar to before #14908

* Amend formatted loop - handle null

* revert _to_char_array to before #14908

* add slt test
danila-b pushed a commit to danila-b/datafusion that referenced this pull request Mar 8, 2025
…apache#14970)

* revert _to_char_scalar to before apache#14908

* Amend formatted loop - handle null

* revert _to_char_array to before apache#14908

* add slt test
@kosiew kosiew deleted the to-char branch August 9, 2025 03:49
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

functions Changes to functions implementation sqllogictest SQL Logic Tests (.slt)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Fix the null handling for to_char function

4 participants