Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtp (Exim 4.84_2) (envelope-from ) id 1cDX31-0002l4-Um for pgsql-bugs@arkaria.postgresql.org; Sun, 04 Dec 2016 13:41:44 +0000 Received: from localhost ([127.0.0.1] helo=postgresql.org) by malur.postgresql.org with smtp (Exim 4.84_2) (envelope-from ) id 1cDX31-00075B-A3 for pgsql-bugs@arkaria.postgresql.org; Sun, 04 Dec 2016 13:41:43 +0000 Received: from makus.postgresql.org ([2001:4800:1501:1::229]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.84_2) (envelope-from ) id 1cDX2M-0006MI-8q for pgsql-bugs@postgresql.org; Sun, 04 Dec 2016 13:41:02 +0000 Received: from smtprelay0003.b.hostedemail.com ([64.98.42.3] helo=smtprelay.b.hostedemail.com) by makus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.84_2) (envelope-from ) id 1cDX2F-0005G0-Tm for pgsql-bugs@postgresql.org; Sun, 04 Dec 2016 13:41:01 +0000 Received: from filter.hostedemail.com (10.5.19.248.rfc1918.com [10.5.19.248]) by smtprelay06.b.hostedemail.com (Postfix) with ESMTP id A601B8D247; Sun, 4 Dec 2016 13:40:54 +0000 (UTC) X-Session-Marker: 616C76686572726540616C76682E6E6F2D69702E6F7267 X-Spam-Summary: 50,0,0,,d41d8cd98f00b204,alvherre@alvh.no-ip.org,:::,RULES_HIT:41:152:355:379:599:800:871:967:973:982:988:989:1260:1311:1312:1313:1314:1345:1359:1437:1515:1516:1518:1519:1534:1541:1575:1594:1595:1596:1711:1730:1747:1764:1777:1792:2393:2525:2560:2563:2682:2685:2859:2933:2937:2939:2942:2945:2947:2951:2954:3022:3138:3139:3140:3141:3142:3352:3865:3866:3867:3872:3934:3936:3938:3941:3944:3947:3950:3953:3956:3959:4250:4321:4605:5007:6261:6748:7281:7514:7909:8957:9025:9121:9165:10004:10394:10400:10848:11026:11232:11604:11658:11783:11914:12043:12050:12555:12740:13138:13231:13439:13846:13891:13894:13895:14181:14658:14721:21080:21433:30003:30022:30054,0,RBL:none,CacheIP:none,Bayesian:0.5,0.5,0.5,Netcheck:none,DomainCache:0,MSF:not bulk,SPF:fn,MSBL:0,DNSBL:none,Custom_rules:0:0:0,LFtime:3,LUA_SUMMARY:none X-HE-Tag: mass68_7c46d44cb5710 X-Filterd-Recvd-Size: 3177 Received: from alvin.alvh.no-ip.org (unknown [181.43.80.152]) (Authenticated sender: alvherre@alvh.no-ip.org) by omf11.b.hostedemail.com (Postfix) with ESMTPA; Sun, 4 Dec 2016 13:40:53 +0000 (UTC) Received: by alvin.alvh.no-ip.org (Postfix, from userid 1000) id 1D08E1902; Sun, 4 Dec 2016 10:40:50 -0300 (CLST) Date: Sun, 4 Dec 2016 10:40:50 -0300 From: Alvaro Herrera To: abelisto@gmail.com Cc: pgsql-bugs@postgresql.org Subject: Re: BUG #14446: make_date with negative year Message-ID: <20161204134050.vsfjbwub6u6jljax@alvherre.pgsql> MIME-Version: 1.0 Content-Type: multipart/mixed; boundary="ablmacoc2ocsbqmn" Content-Disposition: inline Content-Transfer-Encoding: 8bit In-Reply-To: <20161204055939.4361.33460@wrigleys.postgresql.org> User-Agent: NeoMutt/20161104 (1.7.1) X-Pg-Spam-Score: -1.9 (-) List-Archive: List-Help: List-ID: List-Owner: List-Post: List-Subscribe: List-Unsubscribe: X-Mailing-List: pgsql-bugs Precedence: bulk Sender: pgsql-bugs-owner@postgresql.org --ablmacoc2ocsbqmn Content-Type: text/plain; charset=utf-8 Content-Disposition: inline Content-Transfer-Encoding: 8bit abelisto@gmail.com wrote: > Bug reference: 14446 > Logged by: Andy Abelisto > Email address: abelisto@gmail.com > PostgreSQL version: 9.5.5 > Operating system: Linux Mint > Description: > > with t(x) as (values('1111-11-11 BC'::date)) select make_date(extract(year > from x)::int, extract(month from x)::int, extract(day from x)::int) from > t; > ERROR: date field value out of range: -1111-11-11 make_date() has: /* * Note: we'll reject zero or negative year values. Perhaps negatives * should be allowed to represent BC years? */ dterr = ValidateDate(DTK_DATE_M, false, false, false, &tm); With the attached patch it becomes (needs docs, tests): with t(x) as (values('1111-11-11 BC'::date), ('00001-11-11 bc'), ('00001-11-11'), ('1111-11-11') ) select make_date(extract(year from x)::int, extract(month from x)::int, extract(day from x)::int) from t; make_date ─────────────── 1111-11-11 BC 0001-11-11 BC 0001-11-11 1111-11-11 (4 filas) -- Álvaro Herrera https://www.2ndQuadrant.com/ PostgreSQL Development, 24x7 Support, Remote DBA, Training & Services --ablmacoc2ocsbqmn Content-Type: text/plain; charset=us-ascii Content-Disposition: attachment; filename="make_date_bc.patch" diff --git a/src/backend/utils/adt/date.c b/src/backend/utils/adt/date.c index bc7d190..149991e 100644 --- a/src/backend/utils/adt/date.c +++ b/src/backend/utils/adt/date.c @@ -252,16 +252,20 @@ make_date(PG_FUNCTION_ARGS) struct pg_tm tm; DateADT date; int dterr; + bool bc = false; tm.tm_year = PG_GETARG_INT32(0); tm.tm_mon = PG_GETARG_INT32(1); tm.tm_mday = PG_GETARG_INT32(2); - /* - * Note: we'll reject zero or negative year values. Perhaps negatives - * should be allowed to represent BC years? - */ - dterr = ValidateDate(DTK_DATE_M, false, false, false, &tm); + /* Handle negative years as BC */ + if (tm.tm_year < 0) + { + bc = true; + tm.tm_year = -tm.tm_year; + } + + dterr = ValidateDate(DTK_DATE_M, false, false, bc, &tm); if (dterr != 0) ereport(ERROR, --ablmacoc2ocsbqmn Content-Type: text/plain Content-Disposition: inline Content-Transfer-Encoding: 8bit MIME-Version: 1.0 -- Sent via pgsql-bugs mailing list (pgsql-bugs@postgresql.org) To make changes to your subscription: http://www.postgresql.org/mailpref/pgsql-bugs --ablmacoc2ocsbqmn--