Исходный код СУБД RuDB — свободной СУБД с открытым исходным кодом на базе PostgreSQL
rudb
2 days ago bd039d1e92813358c5c5a1e346b49074baf513f2
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
/* skip test if not UTF8 server encoding */
SELECT getdatabaseencoding() <> 'UTF8' AS skip_test \gset
\if :skip_test
\quit
\endif
\getenv libdir PG_LIBDIR
\getenv dlsuffix PG_DLSUFFIX
\set regresslib :libdir '/regress' :dlsuffix
CREATE FUNCTION test_bytea_to_text(bytea) RETURNS text
    AS :'regresslib' LANGUAGE C STRICT;
CREATE FUNCTION test_text_to_bytea(text) RETURNS bytea
    AS :'regresslib' LANGUAGE C STRICT;
CREATE FUNCTION test_mblen_func(text, text, text, int) RETURNS int
    AS :'regresslib' LANGUAGE C STRICT;
CREATE FUNCTION test_text_to_wchars(text, text) RETURNS int[]
    AS :'regresslib' LANGUAGE C STRICT;
CREATE FUNCTION test_wchars_to_text(text, int[]) RETURNS text
    AS :'regresslib' LANGUAGE C STRICT;
CREATE FUNCTION test_valid_server_encoding(text) RETURNS boolean
    AS :'regresslib' LANGUAGE C STRICT;
CREATE TABLE regress_encoding(good text, truncated text, with_nul text, truncated_with_nul text);
INSERT INTO regress_encoding
VALUES ('café',
        'caf' || test_bytea_to_text('\xc3'),
        'café' || test_bytea_to_text('\x00') || 'dcba',
        'caf' || test_bytea_to_text('\xc300') || 'dcba');
SELECT good, truncated, with_nul FROM regress_encoding;
 good | truncated | with_nul 
------+-----------+----------
 café | caf       | café
(1 row)
 
SELECT length(good) FROM regress_encoding;
 length 
--------
      4
(1 row)
 
SELECT substring(good, 3, 1) FROM regress_encoding;
 substring 
-----------
 f
(1 row)
 
SELECT substring(good, 4, 1) FROM regress_encoding;
 substring 
-----------
 é
(1 row)
 
SELECT regexp_replace(good, '^caf(.)$', '\1') FROM regress_encoding;
 regexp_replace 
----------------
 é
(1 row)
 
SELECT reverse(good) FROM regress_encoding;
 reverse 
---------
 éfac
(1 row)
 
-- invalid short mb character = error
SELECT length(truncated) FROM regress_encoding;
ERROR:  invalid byte sequence for encoding "UTF8": 0xc3
SELECT substring(truncated, 1, 3) FROM regress_encoding;
 substring 
-----------
 caf
(1 row)
 
SELECT substring(truncated, 1, 4) FROM regress_encoding;
ERROR:  invalid byte sequence for encoding "UTF8": 0xc3
SELECT reverse(truncated) FROM regress_encoding;
ERROR:  invalid byte sequence for encoding "UTF8": 0xc3
-- invalid short mb character = silently dropped
SELECT regexp_replace(truncated, '^caf(.)$', '\1') FROM regress_encoding;
 regexp_replace 
----------------
 caf
(1 row)
 
-- PostgreSQL doesn't allow strings to contain NUL.  If a corrupted string
-- contains NUL at a character boundary position, some functions treat it as a
-- character while others treat it as a terminator, as implementation details.
-- NUL = terminator
SELECT length(with_nul) FROM regress_encoding;
 length 
--------
      4
(1 row)
 
SELECT substring(with_nul, 3, 1) FROM regress_encoding;
 substring 
-----------
 f
(1 row)
 
SELECT substring(with_nul, 4, 1) FROM regress_encoding;
 substring 
-----------
 é
(1 row)
 
SELECT substring(with_nul, 5, 1) FROM regress_encoding;
 substring 
-----------
 
(1 row)
 
SELECT convert_to(substring(with_nul, 5, 1), 'UTF8') FROM regress_encoding;
 convert_to 
------------
 \x
(1 row)
 
SELECT regexp_replace(with_nul, '^caf(.)$', '\1') FROM regress_encoding;
 regexp_replace 
----------------
 é
(1 row)
 
-- NUL = character
SELECT with_nul, reverse(with_nul), reverse(reverse(with_nul)) FROM regress_encoding;
 with_nul | reverse | reverse 
----------+---------+---------
 café     | abcd    | café
(1 row)
 
-- If a corrupted string contains NUL in the tail bytes of a multibyte
-- character (invalid in all encodings), it is considered part of the
-- character for length purposes.  An error will only be raised in code paths
-- that convert or verify encodings.
SELECT length(truncated_with_nul) FROM regress_encoding;
 length 
--------
      8
(1 row)
 
SELECT substring(truncated_with_nul, 3, 1) FROM regress_encoding;
 substring 
-----------
 f
(1 row)
 
SELECT substring(truncated_with_nul, 4, 1) FROM regress_encoding;
 substring 
-----------
 
(1 row)
 
SELECT convert_to(substring(truncated_with_nul, 4, 1), 'UTF8') FROM regress_encoding;
ERROR:  invalid byte sequence for encoding "UTF8": 0xc3 0x00
SELECT substring(truncated_with_nul, 5, 1) FROM regress_encoding;
 substring 
-----------
 d
(1 row)
 
SELECT regexp_replace(truncated_with_nul, '^caf(.)dcba$', '\1') = test_bytea_to_text('\xc300') FROM regress_encoding;
 ?column? 
----------
 t
(1 row)
 
SELECT reverse(truncated_with_nul) FROM regress_encoding;
 reverse 
---------
 abcd
(1 row)
 
-- unbounded: sequence would overrun the string!
SELECT test_mblen_func('pg_mblen_unbounded', 'UTF8', truncated, 3)
FROM regress_encoding;
 test_mblen_func 
-----------------
               2
(1 row)
 
-- condition detected when using the length/range variants
SELECT test_mblen_func('pg_mblen_with_len', 'UTF8', truncated, 3)
FROM regress_encoding;
ERROR:  invalid byte sequence for encoding "UTF8": 0xc3
SELECT test_mblen_func('pg_mblen_range', 'UTF8', truncated, 3)
FROM regress_encoding;
ERROR:  invalid byte sequence for encoding "UTF8": 0xc3
-- unbounded: sequence would overrun the string, if the terminator were really
-- the end of it
SELECT test_mblen_func('pg_mblen_unbounded', 'UTF8', truncated_with_nul, 3)
FROM regress_encoding;
 test_mblen_func 
-----------------
               2
(1 row)
 
SELECT test_mblen_func('pg_encoding_mblen', 'GB18030', truncated_with_nul, 3)
FROM regress_encoding;
 test_mblen_func 
-----------------
               2
(1 row)
 
-- condition detected when using the cstr variants
SELECT test_mblen_func('pg_mblen_cstr', 'UTF8', truncated_with_nul, 3)
FROM regress_encoding;
ERROR:  invalid byte sequence for encoding "UTF8": 0xc3
DROP TABLE regress_encoding;
-- mb<->wchar conversions
CREATE FUNCTION test_encoding(encoding text, description text, input bytea)
RETURNS VOID LANGUAGE plpgsql AS
$$
DECLARE
    prefix text;
    len int;
    wchars int[];
    round_trip bytea;
    result text;
BEGIN
    prefix := rpad(encoding || ' ' || description || ':', 28);
 
    -- XXX could also test validation, length functions and include client
    -- only encodings with these test cases
 
    IF test_valid_server_encoding(encoding) THEN
        wchars := test_text_to_wchars(encoding, test_bytea_to_text(input));
        round_trip = test_text_to_bytea(test_wchars_to_text(encoding, wchars));
        if input = round_trip then
            result := 'OK';
        elsif length(input) > length(round_trip) and round_trip = substr(input, 1, length(round_trip)) then
            result := 'truncated';
        else
            result := 'failed';
        end if;
        RAISE NOTICE '% % -> % -> % = %', prefix, input, wchars, round_trip, result;
    END IF;
END;
$$;
-- No validation is done on the encoding itself, just the length to avoid
-- overruns, so some of the byte sequences below are bogus.  They cover
-- all code branches, server encodings only for now.
CREATE TABLE encoding_tests (encoding text, description text, input bytea);
INSERT INTO encoding_tests VALUES
    -- LATIN1, other single-byte encodings
    ('LATIN1', 'ASCII',    'a'),
    ('LATIN1', 'extended', '\xe9'),
    -- EUC_JP, EUC_JIS_2004, EUR_KR (for the purposes of wchar conversion):
    -- 2 8e (CS2, not used by EUR_KR but arbitrarily considered to have EUC_JP length)
    -- 3 8f (CS3, not used by EUR_KR but arbitrarily considered to have EUC_JP length)
    -- 2 80..ff (CS1)
    ('EUC_JP', 'ASCII',      'a'),
    ('EUC_JP', 'CS1, short', '\x80'),
    ('EUC_JP', 'CS1',        '\x8002'),
    ('EUC_JP', 'CS2, short', '\x8e'),
    ('EUC_JP', 'CS2',        '\x8e02'),
    ('EUC_JP', 'CS3, short', '\x8f'),
    ('EUC_JP', 'CS3, short', '\x8f02'),
    ('EUC_JP', 'CS3',        '\x8f0203'),
    -- EUC_CN
    -- 3 8e (CS2, not used but arbitrarily considered to have length 3)
    -- 3 8f (CS3, not used but arbitrarily considered to have length 3)
    -- 2 80..ff (CS1)
    ('EUC_CN', 'ASCII',      'a'),
    ('EUC_CN', 'CS1, short', '\x80'),
    ('EUC_CN', 'CS1',        '\x8002'),
    ('EUC_CN', 'CS2, short', '\x8e'),
    ('EUC_CN', 'CS2, short', '\x8e02'),
    ('EUC_CN', 'CS2',        '\x8e0203'),
    ('EUC_CN', 'CS3, short', '\x8f'),
    ('EUC_CN', 'CS3, short', '\x8f02'),
    ('EUC_CN', 'CS3',        '\x8f0203'),
    -- EUC_TW:
    -- 4 8e (CS2)
    -- 3 8f (CS3, not used but arbitrarily considered to have length 3)
    -- 2 80..ff (CS1)
    ('EUC_TW', 'ASCII',      'a'),
    ('EUC_TW', 'CS1, short', '\x80'),
    ('EUC_TW', 'CS1',        '\x8002'),
    ('EUC_TW', 'CS2, short', '\x8e'),
    ('EUC_TW', 'CS2, short', '\x8e02'),
    ('EUC_TW', 'CS2, short', '\x8e0203'),
    ('EUC_TW', 'CS2',        '\x8e020304'),
    ('EUC_TW', 'CS3, short', '\x8f'),
    ('EUC_TW', 'CS3, short', '\x8f02'),
    ('EUC_TW', 'CS3',        '\x8f0203'),
    -- UTF8
    -- 2 c0..df
    -- 3 e0..ef
    -- 4 f0..f7 (but maximum real codepoint U+10ffff has f4)
    -- 5 f8..fb (not supported)
    -- 6 fc..fd (not supported)
    ('UTF8',   'ASCII',               'a'),
    ('UTF8',   '2 byte, short',       '\xdf'),
    ('UTF8',   '2 byte',              '\xdf82'),
    ('UTF8',   '3 byte, short',       '\xef'),
    ('UTF8',   '3 byte, short',       '\xef82'),
    ('UTF8',   '3 byte',              '\xef8283'),
    ('UTF8',   '4 byte, short',       '\xf7'),
    ('UTF8',   '4 byte, short',       '\xf782'),
    ('UTF8',   '4 byte, short',       '\xf78283'),
    ('UTF8',   '4 byte',              '\xf7828384'),
    ('UTF8',   '5 byte, unsupported', '\xfb'),
    ('UTF8',   '5 byte, unsupported', '\xfb82'),
    ('UTF8',   '5 byte, unsupported', '\xfb8283'),
    ('UTF8',   '5 byte, unsupported', '\xfb828384'),
    ('UTF8',   '5 byte, unsupported', '\xfb82838485'),
    ('UTF8',   '6 byte, unsupported', '\xfd'),
    ('UTF8',   '6 byte, unsupported', '\xfd82'),
    ('UTF8',   '6 byte, unsupported', '\xfd8283'),
    ('UTF8',   '6 byte, unsupported', '\xfd828384'),
    ('UTF8',   '6 byte, unsupported', '\xfd82838485'),
    ('UTF8',   '6 byte, unsupported', '\xfd8283848586');
SELECT COUNT(test_encoding(encoding, description, input)) > 0
FROM encoding_tests;
NOTICE:  LATIN1 ASCII:                \x61 -> {97} -> \x61 = OK
NOTICE:  LATIN1 extended:             \xe9 -> {233} -> \xe9 = OK
NOTICE:  EUC_JP ASCII:                \x61 -> {97} -> \x61 = OK
NOTICE:  EUC_JP CS1, short:           \x80 -> {} -> \x = truncated
NOTICE:  EUC_JP CS1:                  \x8002 -> {32770} -> \x8002 = OK
NOTICE:  EUC_JP CS2, short:           \x8e -> {} -> \x = truncated
NOTICE:  EUC_JP CS2:                  \x8e02 -> {36354} -> \x8e02 = OK
NOTICE:  EUC_JP CS3, short:           \x8f -> {} -> \x = truncated
NOTICE:  EUC_JP CS3, short:           \x8f02 -> {} -> \x = truncated
NOTICE:  EUC_JP CS3:                  \x8f0203 -> {9372163} -> \x8f0203 = OK
NOTICE:  EUC_CN ASCII:                \x61 -> {97} -> \x61 = OK
NOTICE:  EUC_CN CS1, short:           \x80 -> {} -> \x = truncated
NOTICE:  EUC_CN CS1:                  \x8002 -> {32770} -> \x8002 = OK
NOTICE:  EUC_CN CS2, short:           \x8e -> {} -> \x = truncated
NOTICE:  EUC_CN CS2, short:           \x8e02 -> {} -> \x = truncated
NOTICE:  EUC_CN CS2:                  \x8e0203 -> {9306627} -> \x8e0203 = OK
NOTICE:  EUC_CN CS3, short:           \x8f -> {} -> \x = truncated
NOTICE:  EUC_CN CS3, short:           \x8f02 -> {} -> \x = truncated
NOTICE:  EUC_CN CS3:                  \x8f0203 -> {9372163} -> \x8f0203 = OK
NOTICE:  EUC_TW ASCII:                \x61 -> {97} -> \x61 = OK
NOTICE:  EUC_TW CS1, short:           \x80 -> {} -> \x = truncated
NOTICE:  EUC_TW CS1:                  \x8002 -> {32770} -> \x8002 = OK
NOTICE:  EUC_TW CS2, short:           \x8e -> {} -> \x = truncated
NOTICE:  EUC_TW CS2, short:           \x8e02 -> {} -> \x = truncated
NOTICE:  EUC_TW CS2, short:           \x8e0203 -> {} -> \x = truncated
NOTICE:  EUC_TW CS2:                  \x8e020304 -> {-1912470780} -> \x8e020304 = OK
NOTICE:  EUC_TW CS3, short:           \x8f -> {} -> \x = truncated
NOTICE:  EUC_TW CS3, short:           \x8f02 -> {} -> \x = truncated
NOTICE:  EUC_TW CS3:                  \x8f0203 -> {9372163} -> \x8f0203 = OK
NOTICE:  UTF8 ASCII:                  \x61 -> {97} -> \x61 = OK
NOTICE:  UTF8 2 byte, short:          \xdf -> {} -> \x = truncated
NOTICE:  UTF8 2 byte:                 \xdf82 -> {1986} -> \xdf82 = OK
NOTICE:  UTF8 3 byte, short:          \xef -> {} -> \x = truncated
NOTICE:  UTF8 3 byte, short:          \xef82 -> {} -> \x = truncated
NOTICE:  UTF8 3 byte:                 \xef8283 -> {61571} -> \xef8283 = OK
NOTICE:  UTF8 4 byte, short:          \xf7 -> {} -> \x = truncated
NOTICE:  UTF8 4 byte, short:          \xf782 -> {} -> \x = truncated
NOTICE:  UTF8 4 byte, short:          \xf78283 -> {} -> \x = truncated
NOTICE:  UTF8 4 byte:                 \xf7828384 -> {1843396} -> \xf7828384 = OK
NOTICE:  UTF8 5 byte, unsupported:    \xfb -> {251} -> \xc3bb = failed
NOTICE:  UTF8 5 byte, unsupported:    \xfb82 -> {251,130} -> \xc3bbc282 = failed
NOTICE:  UTF8 5 byte, unsupported:    \xfb8283 -> {251,130,131} -> \xc3bbc282c283 = failed
NOTICE:  UTF8 5 byte, unsupported:    \xfb828384 -> {251,130,131,132} -> \xc3bbc282c283c284 = failed
NOTICE:  UTF8 5 byte, unsupported:    \xfb82838485 -> {251,130,131,132,133} -> \xc3bbc282c283c284c285 = failed
NOTICE:  UTF8 6 byte, unsupported:    \xfd -> {253} -> \xc3bd = failed
NOTICE:  UTF8 6 byte, unsupported:    \xfd82 -> {253,130} -> \xc3bdc282 = failed
NOTICE:  UTF8 6 byte, unsupported:    \xfd8283 -> {253,130,131} -> \xc3bdc282c283 = failed
NOTICE:  UTF8 6 byte, unsupported:    \xfd828384 -> {253,130,131,132} -> \xc3bdc282c283c284 = failed
NOTICE:  UTF8 6 byte, unsupported:    \xfd82838485 -> {253,130,131,132,133} -> \xc3bdc282c283c284c285 = failed
NOTICE:  UTF8 6 byte, unsupported:    \xfd8283848586 -> {253,130,131,132,133,134} -> \xc3bdc282c283c284c285c286 = failed
 ?column? 
----------
 t
(1 row)
 
-- substring fetches a slice of a toasted value; unused tail of that slice is
-- an incomplete char (bug #19406)
CREATE TABLE toast_3b_utf8 (c text);
INSERT INTO toast_3b_utf8 VALUES (repeat(U&'\2026', 4000));
SELECT SUBSTRING(c FROM 1 FOR 1) FROM toast_3b_utf8;
 substring 
-----------
 …
(1 row)
 
SELECT SUBSTRING(c FROM 4001 FOR 1) FROM toast_3b_utf8;
 substring 
-----------
 
(1 row)
 
-- diagnose incomplete char iff within the substring
UPDATE toast_3b_utf8 SET c = c || test_bytea_to_text('\xe280');
SELECT SUBSTRING(c FROM 4000 FOR 1) FROM toast_3b_utf8;
 substring 
-----------
 …
(1 row)
 
SELECT SUBSTRING(c FROM 4001 FOR 1) FROM toast_3b_utf8;
ERROR:  invalid byte sequence for encoding "UTF8": 0xe2 0x80
-- substring needing last byte of its slice_size
ALTER TABLE toast_3b_utf8 RENAME TO toast_4b_utf8;
UPDATE toast_4b_utf8 SET c = repeat(U&'\+01F680', 3000);
SELECT SUBSTRING(c FROM 3000 FOR 1) FROM toast_4b_utf8;
 substring 
-----------
 🚀
(1 row)
 
-- ascii() and multibyte characters
SELECT ascii(test_bytea_to_text('\xc3'));
ERROR:  invalid byte sequence for encoding "UTF8"
SELECT ascii(test_bytea_to_text('\xe2'));
ERROR:  invalid byte sequence for encoding "UTF8"
SELECT ascii(test_bytea_to_text('\xe282'));
ERROR:  invalid byte sequence for encoding "UTF8"
SELECT ascii(test_bytea_to_text('\xf0'));
ERROR:  invalid byte sequence for encoding "UTF8"
SELECT ascii(test_bytea_to_text('\xf09f'));
ERROR:  invalid byte sequence for encoding "UTF8"
SELECT ascii(test_bytea_to_text('\xf09f98'));
ERROR:  invalid byte sequence for encoding "UTF8"
-- invalid continuation byte
SELECT ascii(test_bytea_to_text('\xc3ff'));
ERROR:  invalid byte sequence for encoding "UTF8"
-- invalid leading byte
SELECT ascii(test_bytea_to_text('\x80'));
ERROR:  invalid byte sequence for encoding "UTF8"
DROP TABLE encoding_tests;
DROP TABLE toast_4b_utf8;
DROP FUNCTION test_encoding;
DROP FUNCTION test_wchars_to_text;
DROP FUNCTION test_text_to_wchars;
DROP FUNCTION test_valid_server_encoding;
DROP FUNCTION test_mblen_func;
DROP FUNCTION test_bytea_to_text;
DROP FUNCTION test_text_to_bytea;
-- substring slow path: multi-byte escape char vs. multi-byte pattern char.
SELECT SUBSTRING('a' SIMILAR U&'\00AC' ESCAPE U&'\00A7');
 substring 
-----------
 
(1 row)
 
-- Levenshtein distance metric: exercise character length cache.
SELECT U&"real\00A7_name" FROM (select 1) AS x(real_name);
ERROR:  column "real§_name" does not exist
LINE 1: SELECT U&"real\00A7_name" FROM (select 1) AS x(real_name);
               ^
HINT:  Perhaps you meant to reference the column "x.real_name".
-- JSON errcontext: truncate long data.
SELECT repeat(U&'\00A7', 30)::json;
ERROR:  invalid input syntax for type json
DETAIL:  Token "§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§" is invalid.
CONTEXT:  JSON data, line 1: ...§§§§§§§§§§§§§§§§§§§§§§§§