Support empty string handling for date and time datatypes#582
Support empty string handling for date and time datatypes#582manisha-deshpande wants to merge 1 commit into
Conversation
0865c8e to
c7a660c
Compare
| /* | ||
| * Set input to default '1900-01-01' if empty string encountered | ||
| */ | ||
| if ((*str == '\0') && (sql_dialect == SQL_DIALECT_TSQL)) |
There was a problem hiding this comment.
We always put dialect check / hook check as the first thing in the condition.
There was a problem hiding this comment.
Flipped the order. Tests passed locally. It is required to rerun the extensions-side PR tests?
| /* | ||
| * Set input to default '1900-01-01' if empty string encountered | ||
| */ | ||
| if (sql_dialect == SQL_DIALECT_TSQL && (*str == '\0')) |
There was a problem hiding this comment.
This condition would not effectively check for empty string consisting of whitespaces. For eg:
select CAST(' ' as date)
We may consider trimming these spaces first.
There was a problem hiding this comment.
Good point. Considered this, but left it for now to remain consistent with the other date types (datetime2, datetime, datetimeoffset) that don't check or trim blank strings and return a syntax error:
select CAST(' ' as datetimeoffset)
invalid input syntax for type timestamp with time zone: " "
It might be good to expand the scope from just date/time datatypes and add the whitespace trimming logic for the other date types as well. Shall I proceed with those changes in this pr itself?
There was a problem hiding this comment.
Can we check the effort to fix other date/time datatypes as well. I think we should fix all of them if the effort is reasonable.
There was a problem hiding this comment.
We should also consider fixing this for CASTs from string datatypes to Date/Time.
There was a problem hiding this comment.
Agreed. Can you help me understand why the sql_dialect check fails for CAST(CAST('' AS VARCHAR) AS DATE/TIME)
- sql_dialect=1 when
date_inis invoked implicitly - sql_dialect=0 when
varchar2dateinvokesdate_in
There was a problem hiding this comment.
dialect is changed to PG when executing non-tsql functions. In this case varchar2date.
I think we will need to fix this in two places instead of date_in()
tsql_coerce_string_literal_hookfor direct constants casted to datetime types.- In cast functions while casting from other datatypes
There was a problem hiding this comment.
Yes, it's Better to have these changes in extension itself as tanzeel mentioned, we can have handlings at two seperate places for string literals and casts respectively.
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.
Addressing PR comments: Moving code changes to extension side
Task: BABEL-3433
Signed-off-by: Manisha Deshpande <mmdeshp@amazon.com>
4bd73cc to
67ff8f3
Compare
|
Fix moved to extension side: babelfish-for-postgresql/babelfish_extensions#3807 |
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
dateortimedatatype, 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.Changes:
date_in()andtime_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 callingdate2jandtm2timerespectively).initializeToDefaultDatetime().Issues Resolved
BABEL-3433: Support for empty input string handling in date and time datatypes
Check List
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.