Skip to content

Commit c7a660c

Browse files
Support empty string handling for date and time datatypes
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]>
1 parent ed82740 commit c7a660c

File tree

1 file changed

+25
-0
lines changed

1 file changed

+25
-0
lines changed

src/backend/utils/adt/date.c

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
#include "miscadmin.h"
3030
#include "nodes/supportnodes.h"
3131
#include "parser/scansup.h"
32+
#include "parser/parser.h"
3233
#include "utils/array.h"
3334
#include "utils/builtins.h"
3435
#include "utils/date.h"
@@ -127,6 +128,19 @@ date_in(PG_FUNCTION_ARGS)
127128
char workbuf[MAXDATELEN + 1];
128129
DateTimeErrorExtra extra;
129130

131+
/*
132+
* Set input to default '1900-01-01' if empty string encountered
133+
*/
134+
if ((*str == '\0') && (sql_dialect == SQL_DIALECT_TSQL))
135+
{
136+
tm->tm_year = 1900;
137+
tm->tm_mon = 1;
138+
tm->tm_mday = 1;
139+
140+
date = date2j(tm->tm_year, tm->tm_mon, tm->tm_mday) - POSTGRES_EPOCH_JDATE;
141+
PG_RETURN_DATEADT(date);
142+
}
143+
130144
dterr = ParseDateTime(str, workbuf, sizeof(workbuf),
131145
field, ftype, MAXDATEFIELDS, &nf);
132146
if (dterr == 0)
@@ -1392,6 +1406,17 @@ time_in(PG_FUNCTION_ARGS)
13921406
int ftype[MAXDATEFIELDS];
13931407
DateTimeErrorExtra extra;
13941408

1409+
/*
1410+
* Set input to default '00:00:00.0000000' if empty string encountered
1411+
*/
1412+
if ((*str == '\0') && (sql_dialect == SQL_DIALECT_TSQL))
1413+
{
1414+
tm->tm_hour = tm->tm_min = tm->tm_sec = 0;
1415+
tm2time(tm, 0, &result);
1416+
AdjustTimeForTypmod(&result, typmod);
1417+
PG_RETURN_TIMEADT(result);
1418+
}
1419+
13951420
dterr = ParseDateTime(str, workbuf, sizeof(workbuf),
13961421
field, ftype, MAXDATEFIELDS, &nf);
13971422
if (dterr == 0)

0 commit comments

Comments
 (0)