Skip to content

Support empty string handling for date and time datatypes #582

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

Draft
wants to merge 1 commit into
base: BABEL_5_X_DEV__PG_17_X
Choose a base branch
from

Conversation

manisha-deshpande
Copy link
Contributor

@manisha-deshpande manisha-deshpande commented Jun 2, 2025

Add TSQL-compatible handling of empty string ('') for date and time types, defaulting to '1900-01-01' for date and time, similar to datetime, datetime2, datetimeoffset, smalldatetime.

Task: BABEL-3433

Description

In SQL Server, when an empty string ('') is cast to date or time datatype, it should be return a default value - '1900-01-01' for date and '00:00:00.0000000' for time. Currently, Babelfish raises a syntax error for these cases. This change improves TSQL compatibility by matching SQL Server's behavior for empty string handling in date and time datatypes, while maintaining existing behavior for non-empty inputs.

1> create table bug(dt date null, tm time(3) null);
2> insert into bug values ('', '');
go
Msg 33557097, Level 16, State 1, Server BABELFISH, Line 1
invalid input syntax for type date: ""
Msg 33557097, Level 16, State 1, Server BABELFISH, Line 1
invalid input syntax for type time: ""

Changes:

  • Modified date_in() and time_in() functions in PostgreSQL engine to check for empty input string before ParseDateTime() and if in TSQL dialect, prepare date and time default values adjusting their time struct and converting to respective return type Datums (by calling date2j and tm2time respectively).
    • This approach mirrors the empty string handling logic in the babelfish_common package for datetime, datetime2, datetimeoffset, and smalldatetime datatypes through the use of initializeToDefaultDatetime().
  • Added comprehensive test coverage in the Babelfish JDBC test suite to verify empty string handling across different scenarios including direct casts, table insert operation, type conversions, and formatting functions.

Issues Resolved

BABEL-3433: Support for empty input string handling in date and time datatypes

Check List

  • Commits are signed per the DCO using --signoff

By submitting this pull request, I confirm that my contribution is under the terms of the PostgreSQL license, and grant any person obtaining a copy of the contribution permission to relicense all or a portion of my contribution to the PostgreSQL License solely to contribute all or a portion of my contribution to the PostgreSQL open source project.

For more information on following Developer Certificate of Origin and signing off your commits, please check here.

Add TSQL-compatible handling of empty string ('') for date and time
types, defaulting to '1900-01-01' for date and '00:00:00.0000000' for
time, similar to datetime, datetime2, datetimeoffset, smalldatetime.

Task: BABEL-3433
Signed-off-by: Manisha Deshpande <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant