pg_compatibility: char varchar compute length of characters insted of bytes

This commit is contained in:
wuyuechuan
2020-12-09 20:02:57 +08:00
parent f6f81afce9
commit b351b798f1
4 changed files with 95 additions and 3 deletions

View File

@ -647,7 +647,11 @@ Datum bpcharlen(PG_FUNCTION_ARGS)
int len;
/* get number of bytes, ignoring trailing spaces */
len = VARSIZE_ANY_EXHDR(arg);
if (DB_IS_CMPT(DB_CMPT_PG)) {
len = bcTruelen(arg);
} else {
len = VARSIZE_ANY_EXHDR(arg);
}
/* in multibyte encoding, convert to number of characters */
if (pg_database_encoding_max_length() != 1) {

View File

@ -877,7 +877,11 @@ int pg_mbcharcliplen(const char* mbstr, int len, int limit)
}
while (len > 0 && *mbstr) {
l = pg_mblen(mbstr);
nch += l;
if (DB_IS_CMPT(DB_CMPT_PG)) {
nch++;
} else {
nch += l;
}
if (nch > limit) {
break;
}

View File

@ -43,3 +43,67 @@ select concat_ws(null,'ABCDE', 2, null, 22);
(1 row)
--char、varchar test
create table char_test(a char(10),b varchar(10));
insert into char_test values('零一二三四五六七八九','零一二三四五六七八九');
insert into char_test values('零1二3四5六7八9','零1二3四5六7八9');
insert into char_test values('零1二3四5六7八9','零1二3四5六7八90');
ERROR: value too long for type character varying(10)
CONTEXT: referenced column: b
insert into char_test values('零1二3四5六7八90','零1二3四5六7八9');
ERROR: value too long for type character(10)
CONTEXT: referenced column: a
insert into char_test values('零0','零1二3');
insert into char_test values('','');
insert into char_test values(null,null);
insert into char_test values('0','0');
select length(a),length(b) from char_test;
length | length
--------+--------
10 | 10
10 | 10
2 | 4
0 | 0
|
1 | 1
(6 rows)
select lengthb(a),lengthb(b) from char_test;
lengthb | lengthb
---------+---------
30 | 30
20 | 20
10 | 8
10 | 0
|
10 | 1
(6 rows)
select bit_length(a),bit_length(b) from char_test;
bit_length | bit_length
------------+------------
240 | 240
160 | 160
32 | 64
0 | 0
|
8 | 8
(6 rows)
create index a on char_test(a);
create index b on char_test(b);
set enable_seqscan to off;
select * from char_test where a = '零0';
a | b
-----------+--------
零0 | 零1二3
(1 row)
select * from char_test where b = '零1二3';
a | b
-----------+--------
零0 | 零1二3
(1 row)

View File

@ -17,4 +17,24 @@ select ''::int;
select concat_ws('','ABCDE', 2, null, 22);
select concat_ws(null,'ABCDE', 2, null, 22);
select concat_ws(null,'ABCDE', 2, null, 22);
--charvarchar test
create table char_test(a char(10),b varchar(10));
insert into char_test values('零一二三四五六七八九','零一二三四五六七八九');
insert into char_test values('零1二3四5六7八9','零1二3四5六7八9');
insert into char_test values('零1二3四5六7八9','零1二3四5六7八90');
insert into char_test values('零1二3四5六7八90','零1二3四5六7八9');
insert into char_test values('零0','零1二3');
insert into char_test values('','');
insert into char_test values(null,null);
insert into char_test values('0','0');
select length(a),length(b) from char_test;
select lengthb(a),lengthb(b) from char_test;
select bit_length(a),bit_length(b) from char_test;
create index a on char_test(a);
create index b on char_test(b);
set enable_seqscan to off;
select * from char_test where a = '零0';
select * from char_test where b = '零1二3';