Re: date_part/extract parse curiosity

From: Japin Li <japinli(at)hotmail(dot)com>
To: Erik Rijkers <er(at)xs4all(dot)nl>
Cc: PostgreSQL-development <pgsql-hackers(at)postgresql(dot)org>, pgsql-hackers(at)lists(dot)postgresql(dot)org
Subject: Re: date_part/extract parse curiosity
Date: 2022-10-20 13:57:34
Message-ID: MEYP282MB166938973B6061473482A262B62A9@MEYP282MB1669.AUSP282.PROD.OUTLOOK.COM
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-hackers


On Thu, 20 Oct 2022 at 20:45, Erik Rijkers <er(at)xs4all(dot)nl> wrote:
> Hi,
>
> I noticed that
> select date_part('millennium', now()); --> 3
>
> will execute also, unperturbed, in this form:
> select date_part('millennium xxxxx', now()); --> 3
>
> By the same token
>
> select extract(millennium from now()) --> 3
> select extract(millenniumxxxxxxxxx from now()) --> 3
>
> This laxness occurs in all releases, and with 'millennium',
> 'millisecond', and 'microsecond' (at least).
>
> Even though it's not likely to cause much real-life headaches, and I
> hesitate to call it a real bug, perhaps it would be better if it could
> be a bit stricter.
>

According to the documentation [1], the extract() only has some field names,
however, the code use strncmp() to compare the units and tokens.

int
DecodeUnits(int field, char *lowtoken, int *val)
{
int type;
const datetkn *tp;

tp = deltacache[field];
/* use strncmp so that we match truncated tokens */ <---- here
if (tp == NULL || strncmp(lowtoken, tp->token, TOKMAXLEN) != 0)
{
tp = datebsearch(lowtoken, deltatktbl, szdeltatktbl);
}
if (tp == NULL)
{
type = UNKNOWN_FIELD;
*val = 0;
}
else
{
deltacache[field] = tp;
type = tp->type;
*val = tp->value;
}

return type;
}

This is convenient for field names such as millennium and millenniums,
however it also valid for millenniumxxxxxxxxxxxx, which is looks strange.

Maybe we should document this. I'd be inclined to change the code to
match the certain valid field names.

Any thoughts?

[1] https://www.postgresql.org/docs/current/functions-datetime.html#FUNCTIONS-DATETIME-EXTRACT

--
Regrads,
Japin Li.
ChengDu WenWu Information Technology Co.,Ltd.

In response to

Responses

Browse pgsql-hackers by date

  From Date Subject
Next Message Tom Lane 2022-10-20 14:12:05 Re: date_part/extract parse curiosity
Previous Message Robert Haas 2022-10-20 13:47:38 Re: Logical WAL sender unresponsive during decoding commit