From: Tatsuhito Kasahara <kasahara.tatsuhito@oss.ntt.co.jp>
To: pgsql-bugs@postgresql.org
Subject: make_greater_string() does not return a string in some cases
Date: Mon, 21 Jun 2010 19:02:03 +0900
Message-ID: <4C1F389B.1010606@oss.ntt.co.jp> (raw)
Hi !
make_greater_string() does not return a string when some UTF8 strings
set to str_const.
# Especially UTF8 strings which contains 'BF' in last byte.
Because make_greater_string() only try incrementing the last byte of
the string, and not try same test for upper bytes.
Therefore, some queries which contains "LIKE '<contains 'BF' in last byte>%'"
can not perform (Btree's) index-scan.
# Or may be nearly full-index-scan.
# See follwing example.
===============================================================================
'$B@>(B' (Japanese Letter) : 0xE8A5BF
[client : UTF8 $B"N(B server : EUC_JP]
=# EXPLAIN ANALYZE SELECT * FROM test2 WHERE name LIKE '$B@>(B%';
QUERY PLAN
------------------------------------------------------------------------------------------------------------------
Index Scan using test2_name on test2 (cost=0.00..8.28 rows=1 width=3) (actual time=0.077..0.078 rows=1 loops=1)
Index Cond: ((name >= '$B@>(B'::text) AND (name < '$B@?(B'::text)) <-- Index-scan is chosen
Filter: (name ~~ '$B@>(B%'::text)
Total runtime: 0.110 ms
(4 rows)
[client : UTF8 $B"N(B server : UTF8]
=# EXPLAIN ANALYZE SELECT * FROM test2 WHERE name LIKE '$B@>(B%';
QUERY PLAN
----------------------------------------------------------------------------------------------------
Seq Scan on test2 (cost=0.00..1693.01 rows=1 width=4) (actual time=22.598..22.599 rows=1 loops=1)
Filter: (name ~~ '$B@>(B%'::text) <-- Seq-scan is chosen !
Total runtime: 22.626 ms
(3 rows)
===============================================================================
Attached patch solve above problem.
Best regards,
--
NTT OSS Center
Tatsuhito Kasahara
Attachments:make_greater_string.patch (text/plain, 1.2K) downloaddiff --git a/src/backend/utils/adt/selfuncs.c b/src/backend/utils/adt/selfuncs.cindex fc3c5b0..fdf58cf 100644
*** a/src/backend/utils/adt/selfuncs.c
--- b/src/backend/utils/adt/selfuncs.c
*************** make_greater_string(const Const *str_con
*** 5542,5552 ****
*lastchar = savelastchar;
/*
! * Truncate off the last character, which might be more than 1 byte,
! * depending on the character encoding.
*/
if (datatype != BYTEAOID && pg_database_encoding_max_length() > 1)
! len = pg_mbcliplen(workstr, len, len - 1);
else
len -= 1;
--- 5542,5567 ----
*lastchar = savelastchar;
/*
! * Increment the previous character, or truncate off the last character,
! * which might be more than 1 byte, depending on the character encoding.
*/
if (datatype != BYTEAOID && pg_database_encoding_max_length() > 1)
! {
! int i;
! int cliplen = pg_mbcliplen(workstr, len, len - 1);
!
! for (i = len - 1; i > cliplen; i--)
! {
! if ((unsigned char) workstr[i] < (unsigned char) 255)
! {
! workstr[i]++;
! memset(workstr + i + 1, 1 /* or 0? */, len - i);
! break;
! }
! }
! if (i <= cliplen)
! len = cliplen;
! }
else
len -= 1;
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-bugs@postgresql.org
Cc: kasahara.tatsuhito@oss.ntt.co.jp
Subject: Re: make_greater_string() does not return a string in some cases
In-Reply-To: <4C1F389B.1010606@oss.ntt.co.jp>
References: <4C1F389B.1010606@oss.ntt.co.jp>
* 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