pg.ddx.io  pgsql-hackers@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Artur Zakirov <a.zakirov@postgrespro.ru>
To: Andreas 'ads' Scherbaum <adsmail@wars-nicht.de>
To: Joshua D. Drake <jd@commandprompt.com>
To: Peter Eisentraut <peter.eisentraut@2ndquadrant.com>
To: Albe Laurenz <laurenz.albe@wien.gv.at>
To: Andreas Karlsson *EXTERN* <andreas@proxel.se>
To: Pavel Stehule <pavel.stehule@gmail.com>
Cc: Jaime Casanova <jaime.casanova@2ndquadrant.com>
Cc: Euler Taveira <euler@timbira.com.br>
Cc: Pg Hackers <pgsql-hackers@postgresql.org>
Subject: Re: to_date_valid()
Date: Mon, 15 Aug 2016 11:24:07 +0300
Message-ID: <95738e12-6ed6-daf5-9dcf-6336072e6b15@postgrespro.ru> (raw)
In-Reply-To: <e9617e30-1b47-54ec-b978-2c84c6420b12@wars-nicht.de>
References: <57786490.9010201@wars-nicht.de>
	<dcdebc73-ffdd-13a3-d6cd-73e1944a066b@timbira.com.br>
	<CAJGNTeO75ZPm3qDk9rze7coGyvCZyWm68iEFY3WOYb5w=7OdBg@mail.gmail.com>
	<5778EA94.1020706@wars-nicht.de>
	<af97b147-3542-7a55-b651-e0ecae0c8c00@proxel.se>
	<CAFj8pRDHaeVdwHtZQPXKCSaeAQ_2_jeXHpPCssiOdFYs7UoUTw@mail.gmail.com>
	<2bc6bcef-cada-1ac7-f9a2-2f6128ca4c3c@proxel.se>
	<A737B7A37273E048B164557ADEF4A58B5386992E@ntex2010i.host.magwien.gv.at>
	<b28373f4-7e37-3bc4-25a5-65aef4006cf0@2ndquadrant.com>
	<579823DF.3080903@commandprompt.com>
	<e9617e30-1b47-54ec-b978-2c84c6420b12@wars-nicht.de>
List-Unsubscribe: <mailto:majordomo@postgresql.org?body=unsub%20pgsql-hackers>

On 14.08.2016 01:52, Andreas 'ads' Scherbaum wrote:
>
> Attached is a patch to "do the right thing". The verification is in
> "to_date()" now, the extra function is removed. Regression tests are
> updated - two or three of them returned a wrong date before, and still
> passed. They fail now. Documentation is also updated.
>
>
> Regards,
>

Is it right and "true" way to validate date by extra transforming and 
comparison?

Maybe validate date by using ValidateDate(). Attached sample patch.

-- 
Artur Zakirov
Postgres Professional: http://www.postgrespro.com
Russian Postgres Company

-- 
Sent via pgsql-hackers mailing list (pgsql-hackers@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-hackers

Attachments:

to-date-valid.patch (text/x-patch, 2.8K) download diff --git a/src/backend/utils/adt/formatting.c b/src/backend/utils/adt/formatting.c index bbd97dc..c0048c9 100644 --- a/src/backend/utils/adt/formatting.c +++ b/src/backend/utils/adt/formatting.c @@ -3563,7 +3563,9 @@ do_to_timestamp(text *date_txt, text *fmt, { FormatNode *format; TmFromChar tmfc; - int fmt_len; + int fmt_len, + fmask = 0; /* Bit mask for ValidateDate() */ + char *date_str = NULL; ZERO_tmfc(&tmfc); ZERO_tm(tm); @@ -3574,7 +3576,6 @@ do_to_timestamp(text *date_txt, text *fmt, if (fmt_len) { char *fmt_str; - char *date_str; bool incache; fmt_str = text_to_cstring(fmt); @@ -3630,7 +3631,6 @@ do_to_timestamp(text *date_txt, text *fmt, DCH_from_char(format, date_str, &tmfc); - pfree(date_str); pfree(fmt_str); if (!incache) pfree(format); @@ -3706,6 +3706,7 @@ do_to_timestamp(text *date_txt, text *fmt, if (tmfc.bc && tm->tm_year > 0) tm->tm_year = -(tm->tm_year - 1); } + fmask |= DTK_M(YEAR); } else if (tmfc.cc) /* use first year of century */ { @@ -3717,10 +3718,14 @@ do_to_timestamp(text *date_txt, text *fmt, else /* +1 because year == 599 is 600 BC */ tm->tm_year = tmfc.cc * 100 + 1; + fmask |= DTK_M(YEAR); } if (tmfc.j) + { j2date(tmfc.j, &tm->tm_year, &tm->tm_mon, &tm->tm_mday); + fmask |= DTK_DATE_M; + } if (tmfc.ww) { @@ -3734,6 +3739,7 @@ do_to_timestamp(text *date_txt, text *fmt, isoweekdate2date(tmfc.ww, tmfc.d, &tm->tm_year, &tm->tm_mon, &tm->tm_mday); else isoweek2date(tmfc.ww, &tm->tm_year, &tm->tm_mon, &tm->tm_mday); + fmask |= DTK_DATE_M; } else tmfc.ddd = (tmfc.ww - 1) * 7 + 1; @@ -3744,11 +3750,17 @@ do_to_timestamp(text *date_txt, text *fmt, if (tmfc.d) tm->tm_wday = tmfc.d - 1; /* convert to native numbering */ if (tmfc.dd) + { tm->tm_mday = tmfc.dd; + fmask |= DTK_M(DAY); + } if (tmfc.ddd) tm->tm_yday = tmfc.ddd; if (tmfc.mm) + { tm->tm_mon = tmfc.mm; + fmask |= DTK_M(MONTH); + } if (tmfc.ddd && (tm->tm_mon <= 1 || tm->tm_mday <= 1)) { @@ -3771,6 +3783,7 @@ do_to_timestamp(text *date_txt, text *fmt, j0 = isoweek2j(tm->tm_year, 1) - 1; j2date(j0 + tmfc.ddd, &tm->tm_year, &tm->tm_mon, &tm->tm_mday); + fmask |= DTK_DATE_M; } else { @@ -3793,9 +3806,24 @@ do_to_timestamp(text *date_txt, text *fmt, if (tm->tm_mday <= 1) tm->tm_mday = tmfc.ddd - y[i - 1]; + + fmask |= DTK_M(MONTH) | DTK_M(DAY); } } + /* Validate date with bit mask received above */ + if (fmask != 0 && date_str) + { + if (ValidateDate(fmask, false, false, false, tm) != 0) + ereport(ERROR, + (errcode(ERRCODE_DATETIME_FIELD_OVERFLOW), + errmsg("date/time field value out of range: \"%s\"", + date_str))); + } + + if (date_str) + pfree(date_str); + #ifdef HAVE_INT64_TIMESTAMP if (tmfc.ms) *fsec += tmfc.ms * 1000;

view thread (37+ messages)  latest in thread

Message-ID: <95738e12-6ed6-daf5-9dcf-6336072e6b15@postgrespro.ru>
Permalink:  ../95738e12-6ed6-daf5-9dcf-6336072e6b15@postgrespro.ru/
Also on:    postgresql.org/message-id/95738e12-6ed6-daf5-9dcf-6336072e6b15@postgrespro.ru

 ·  · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients from your mail client; the link fills in
  the headers below: reply via email
  Some clients, webmail especially, drop In-Reply-To and References from
  a link, and the reply then starts a new thread: use the mbox below.

  To: pgsql-hackers@postgresql.org
  Cc: a.zakirov@postgrespro.ru, adsmail@wars-nicht.de, jd@commandprompt.com, peter.eisentraut@2ndquadrant.com, laurenz.albe@wien.gv.at, andreas@proxel.se, pavel.stehule@gmail.com, jaime.casanova@2ndquadrant.com, euler@timbira.com.br
  Subject: Re: to_date_valid()
  In-Reply-To: <95738e12-6ed6-daf5-9dcf-6336072e6b15@postgrespro.ru>
  References: <57786490.9010201@wars-nicht.de> <dcdebc73-ffdd-13a3-d6cd-73e1944a066b@timbira.com.br> <CAJGNTeO75ZPm3qDk9rze7coGyvCZyWm68iEFY3WOYb5w=7OdBg@mail.gmail.com> <5778EA94.1020706@wars-nicht.de> <af97b147-3542-7a55-b651-e0ecae0c8c00@proxel.se> <CAFj8pRDHaeVdwHtZQPXKCSaeAQ_2_jeXHpPCssiOdFYs7UoUTw@mail.gmail.com> <2bc6bcef-cada-1ac7-f9a2-2f6128ca4c3c@proxel.se> <A737B7A37273E048B164557ADEF4A58B5386992E@ntex2010i.host.magwien.gv.at> <b28373f4-7e37-3bc4-25a5-65aef4006cf0@2ndquadrant.com> <579823DF.3080903@commandprompt.com> <e9617e30-1b47-54ec-b978-2c84c6420b12@wars-nicht.de> <95738e12-6ed6-daf5-9dcf-6336072e6b15@postgrespro.ru>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox