textlike under the LIKE operator for char(n)

From: Kohei KaiGai <kaigai(at)kaigai(dot)gr(dot)jp>
To: PgHacker <pgsql-hackers(at)postgresql(dot)org>
Subject: textlike under the LIKE operator for char(n)
Date: 2016-05-06 13:58:00
Message-ID: CADyhKSWPosAgqLbaZB6sDEmFQNW8oKZkuPL9=qBz=q=8D_fygw@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

Hi,

I found a mysterious behavior when we use LIKE operator on char(n) data type.

postgres=# select 'abcd'::char(20) LIKE 'ab%cd';
?column?
----------
f
(1 row)

postgres=# select 'abcd'::char(4) LIKE 'ab%cd';
?column?
----------
t
(1 row)

LIKE operator (that is eventually processed by textlike) considers the
padding space of char(n) data type as a part of string.

On the other hands, equal operator ignores the padding space when it
compares two strings.

postgres=# select 'abcd'::char(20) = 'abcd';
?column?
----------
t
(1 row)

postgres=# select 'abcd'::char(4) = 'abcd';
?column?
----------
t
(1 row)

The LIKE operator on char(n) data type is implemented by textlike().

at pg_proc.h:
DATA(insert OID = 1631 ( bpcharlike PGNSP PGUID 12 1 0 0 0 f f
f f t f i s 2 0 16 "1042 25" _null_ _null_ _null_ _null_ _null_
textlike _null_ _null_ _null_ ));

It calls GenericMatchText() with length of the target string,
calculated by VARSIZE_ANY_EXHDR, however, it includes the padding
space.
It seems to me bcTruelen() gives the correct length for char(n) data
types, instead of this macro.

Is this behavior as expected? or, bug?

Thanks,
--
KaiGai Kohei <kaigai(at)kaigai(dot)gr(dot)jp>

Responses

Browse pgsql-hackers by date

  From Date Subject
Next Message Mithun Cy 2016-05-06 14:13:52 Perf Benchmarking and regression.
Previous Message Daniel Verite 2016-05-06 13:45:00 Re: \crosstabview fixes