PolarDB-for-PostgreSQL

Форк
0
352 строки · 15.1 Кб
1
CREATE EXTENSION hstore;
2

3
-- Check whether any of our opclasses fail amvalidate
4
SELECT amname, opcname
5
FROM pg_opclass opc LEFT JOIN pg_am am ON am.oid = opcmethod
6
WHERE opc.oid >= 16384 AND NOT amvalidate(opc.oid);
7

8
set escape_string_warning=off;
9

10
--hstore;
11

12
select ''::hstore;
13
select 'a=>b'::hstore;
14
select ' a=>b'::hstore;
15
select 'a =>b'::hstore;
16
select 'a=>b '::hstore;
17
select 'a=> b'::hstore;
18
select '"a"=>"b"'::hstore;
19
select ' "a"=>"b"'::hstore;
20
select '"a" =>"b"'::hstore;
21
select '"a"=>"b" '::hstore;
22
select '"a"=> "b"'::hstore;
23
select 'aa=>bb'::hstore;
24
select ' aa=>bb'::hstore;
25
select 'aa =>bb'::hstore;
26
select 'aa=>bb '::hstore;
27
select 'aa=> bb'::hstore;
28
select '"aa"=>"bb"'::hstore;
29
select ' "aa"=>"bb"'::hstore;
30
select '"aa" =>"bb"'::hstore;
31
select '"aa"=>"bb" '::hstore;
32
select '"aa"=> "bb"'::hstore;
33

34
select 'aa=>bb, cc=>dd'::hstore;
35
select 'aa=>bb , cc=>dd'::hstore;
36
select 'aa=>bb ,cc=>dd'::hstore;
37
select 'aa=>bb, "cc"=>dd'::hstore;
38
select 'aa=>bb , "cc"=>dd'::hstore;
39
select 'aa=>bb ,"cc"=>dd'::hstore;
40
select 'aa=>"bb", cc=>dd'::hstore;
41
select 'aa=>"bb" , cc=>dd'::hstore;
42
select 'aa=>"bb" ,cc=>dd'::hstore;
43

44
select 'aa=>null'::hstore;
45
select 'aa=>NuLl'::hstore;
46
select 'aa=>"NuLl"'::hstore;
47

48
select e'\\=a=>q=w'::hstore;
49
select e'"=a"=>q\\=w'::hstore;
50
select e'"\\"a"=>q>w'::hstore;
51
select e'\\"a=>q"w'::hstore;
52

53
select ''::hstore;
54
select '	'::hstore;
55

56
-- -> operator
57

58
select 'aa=>b, c=>d , b=>16'::hstore->'c';
59
select 'aa=>b, c=>d , b=>16'::hstore->'b';
60
select 'aa=>b, c=>d , b=>16'::hstore->'aa';
61
select ('aa=>b, c=>d , b=>16'::hstore->'gg') is null;
62
select ('aa=>NULL, c=>d , b=>16'::hstore->'aa') is null;
63
select ('aa=>"NULL", c=>d , b=>16'::hstore->'aa') is null;
64

65
-- -> array operator
66

67
select 'aa=>"NULL", c=>d , b=>16'::hstore -> ARRAY['aa','c'];
68
select 'aa=>"NULL", c=>d , b=>16'::hstore -> ARRAY['c','aa'];
69
select 'aa=>NULL, c=>d , b=>16'::hstore -> ARRAY['aa','c',null];
70
select 'aa=>1, c=>3, b=>2, d=>4'::hstore -> ARRAY[['b','d'],['aa','c']];
71

72
-- exists/defined
73

74
select exist('a=>NULL, b=>qq', 'a');
75
select exist('a=>NULL, b=>qq', 'b');
76
select exist('a=>NULL, b=>qq', 'c');
77
select exist('a=>"NULL", b=>qq', 'a');
78
select defined('a=>NULL, b=>qq', 'a');
79
select defined('a=>NULL, b=>qq', 'b');
80
select defined('a=>NULL, b=>qq', 'c');
81
select defined('a=>"NULL", b=>qq', 'a');
82
select hstore 'a=>NULL, b=>qq' ? 'a';
83
select hstore 'a=>NULL, b=>qq' ? 'b';
84
select hstore 'a=>NULL, b=>qq' ? 'c';
85
select hstore 'a=>"NULL", b=>qq' ? 'a';
86
select hstore 'a=>NULL, b=>qq' ?| ARRAY['a','b'];
87
select hstore 'a=>NULL, b=>qq' ?| ARRAY['b','a'];
88
select hstore 'a=>NULL, b=>qq' ?| ARRAY['c','a'];
89
select hstore 'a=>NULL, b=>qq' ?| ARRAY['c','d'];
90
select hstore 'a=>NULL, b=>qq' ?| '{}'::text[];
91
select hstore 'a=>NULL, b=>qq' ?& ARRAY['a','b'];
92
select hstore 'a=>NULL, b=>qq' ?& ARRAY['b','a'];
93
select hstore 'a=>NULL, b=>qq' ?& ARRAY['c','a'];
94
select hstore 'a=>NULL, b=>qq' ?& ARRAY['c','d'];
95
select hstore 'a=>NULL, b=>qq' ?& '{}'::text[];
96

97
-- delete
98

99
select delete('a=>1 , b=>2, c=>3'::hstore, 'a');
100
select delete('a=>null , b=>2, c=>3'::hstore, 'a');
101
select delete('a=>1 , b=>2, c=>3'::hstore, 'b');
102
select delete('a=>1 , b=>2, c=>3'::hstore, 'c');
103
select delete('a=>1 , b=>2, c=>3'::hstore, 'd');
104
select 'a=>1 , b=>2, c=>3'::hstore - 'a'::text;
105
select 'a=>null , b=>2, c=>3'::hstore - 'a'::text;
106
select 'a=>1 , b=>2, c=>3'::hstore - 'b'::text;
107
select 'a=>1 , b=>2, c=>3'::hstore - 'c'::text;
108
select 'a=>1 , b=>2, c=>3'::hstore - 'd'::text;
109
select pg_column_size('a=>1 , b=>2, c=>3'::hstore - 'b'::text)
110
         = pg_column_size('a=>1, b=>2'::hstore);
111

112
-- delete (array)
113

114
select delete('a=>1 , b=>2, c=>3'::hstore, ARRAY['d','e']);
115
select delete('a=>1 , b=>2, c=>3'::hstore, ARRAY['d','b']);
116
select delete('a=>1 , b=>2, c=>3'::hstore, ARRAY['a','c']);
117
select delete('a=>1 , b=>2, c=>3'::hstore, ARRAY[['b'],['c'],['a']]);
118
select delete('a=>1 , b=>2, c=>3'::hstore, '{}'::text[]);
119
select 'a=>1 , b=>2, c=>3'::hstore - ARRAY['d','e'];
120
select 'a=>1 , b=>2, c=>3'::hstore - ARRAY['d','b'];
121
select 'a=>1 , b=>2, c=>3'::hstore - ARRAY['a','c'];
122
select 'a=>1 , b=>2, c=>3'::hstore - ARRAY[['b'],['c'],['a']];
123
select 'a=>1 , b=>2, c=>3'::hstore - '{}'::text[];
124
select pg_column_size('a=>1 , b=>2, c=>3'::hstore - ARRAY['a','c'])
125
         = pg_column_size('b=>2'::hstore);
126
select pg_column_size('a=>1 , b=>2, c=>3'::hstore - '{}'::text[])
127
         = pg_column_size('a=>1, b=>2, c=>3'::hstore);
128

129
-- delete (hstore)
130

131
select delete('aa=>1 , b=>2, c=>3'::hstore, 'aa=>4, b=>2'::hstore);
132
select delete('aa=>1 , b=>2, c=>3'::hstore, 'aa=>NULL, c=>3'::hstore);
133
select delete('aa=>1 , b=>2, c=>3'::hstore, 'aa=>1, b=>2, c=>3'::hstore);
134
select delete('aa=>1 , b=>2, c=>3'::hstore, 'b=>2'::hstore);
135
select delete('aa=>1 , b=>2, c=>3'::hstore, ''::hstore);
136
select 'aa=>1 , b=>2, c=>3'::hstore - 'aa=>4, b=>2'::hstore;
137
select 'aa=>1 , b=>2, c=>3'::hstore - 'aa=>NULL, c=>3'::hstore;
138
select 'aa=>1 , b=>2, c=>3'::hstore - 'aa=>1, b=>2, c=>3'::hstore;
139
select 'aa=>1 , b=>2, c=>3'::hstore - 'b=>2'::hstore;
140
select 'aa=>1 , b=>2, c=>3'::hstore - ''::hstore;
141
select pg_column_size('a=>1 , b=>2, c=>3'::hstore - 'b=>2'::hstore)
142
         = pg_column_size('a=>1, c=>3'::hstore);
143
select pg_column_size('a=>1 , b=>2, c=>3'::hstore - ''::hstore)
144
         = pg_column_size('a=>1, b=>2, c=>3'::hstore);
145

146
-- ||
147
select 'aa=>1 , b=>2, cq=>3'::hstore || 'cq=>l, b=>g, fg=>f';
148
select 'aa=>1 , b=>2, cq=>3'::hstore || 'aq=>l';
149
select 'aa=>1 , b=>2, cq=>3'::hstore || 'aa=>l';
150
select 'aa=>1 , b=>2, cq=>3'::hstore || '';
151
select ''::hstore || 'cq=>l, b=>g, fg=>f';
152
select pg_column_size(''::hstore || ''::hstore) = pg_column_size(''::hstore);
153
select pg_column_size('aa=>1'::hstore || 'b=>2'::hstore)
154
         = pg_column_size('aa=>1, b=>2'::hstore);
155
select pg_column_size('aa=>1, b=>2'::hstore || ''::hstore)
156
         = pg_column_size('aa=>1, b=>2'::hstore);
157
select pg_column_size(''::hstore || 'aa=>1, b=>2'::hstore)
158
         = pg_column_size('aa=>1, b=>2'::hstore);
159

160
-- hstore(text,text)
161
select 'a=>g, b=>c'::hstore || hstore('asd', 'gf');
162
select 'a=>g, b=>c'::hstore || hstore('b', 'gf');
163
select 'a=>g, b=>c'::hstore || hstore('b', 'NULL');
164
select 'a=>g, b=>c'::hstore || hstore('b', NULL);
165
select ('a=>g, b=>c'::hstore || hstore(NULL, 'b')) is null;
166
select pg_column_size(hstore('b', 'gf'))
167
         = pg_column_size('b=>gf'::hstore);
168
select pg_column_size('a=>g, b=>c'::hstore || hstore('b', 'gf'))
169
         = pg_column_size('a=>g, b=>gf'::hstore);
170

171
-- slice()
172
select slice(hstore 'aa=>1, b=>2, c=>3', ARRAY['g','h','i']);
173
select slice(hstore 'aa=>1, b=>2, c=>3', ARRAY['c','b']);
174
select slice(hstore 'aa=>1, b=>2, c=>3', ARRAY['aa','b']);
175
select slice(hstore 'aa=>1, b=>2, c=>3', ARRAY['c','b','aa']);
176
select pg_column_size(slice(hstore 'aa=>1, b=>2, c=>3', ARRAY['c','b']))
177
         = pg_column_size('b=>2, c=>3'::hstore);
178
select pg_column_size(slice(hstore 'aa=>1, b=>2, c=>3', ARRAY['c','b','aa']))
179
         = pg_column_size('aa=>1, b=>2, c=>3'::hstore);
180

181
-- array input
182
select '{}'::text[]::hstore;
183
select ARRAY['a','g','b','h','asd']::hstore;
184
select ARRAY['a','g','b','h','asd','i']::hstore;
185
select ARRAY[['a','g'],['b','h'],['asd','i']]::hstore;
186
select ARRAY[['a','g','b'],['h','asd','i']]::hstore;
187
select ARRAY[[['a','g'],['b','h'],['asd','i']]]::hstore;
188
select hstore('{}'::text[]);
189
select hstore(ARRAY['a','g','b','h','asd']);
190
select hstore(ARRAY['a','g','b','h','asd','i']);
191
select hstore(ARRAY[['a','g'],['b','h'],['asd','i']]);
192
select hstore(ARRAY[['a','g','b'],['h','asd','i']]);
193
select hstore(ARRAY[[['a','g'],['b','h'],['asd','i']]]);
194
select hstore('[0:5]={a,g,b,h,asd,i}'::text[]);
195
select hstore('[0:2][1:2]={{a,g},{b,h},{asd,i}}'::text[]);
196

197
-- pairs of arrays
198
select hstore(ARRAY['a','b','asd'], ARRAY['g','h','i']);
199
select hstore(ARRAY['a','b','asd'], ARRAY['g','h',NULL]);
200
select hstore(ARRAY['z','y','x'], ARRAY['1','2','3']);
201
select hstore(ARRAY['aaa','bb','c','d'], ARRAY[null::text,null,null,null]);
202
select hstore(ARRAY['aaa','bb','c','d'], null);
203
select quote_literal(hstore('{}'::text[], '{}'::text[]));
204
select quote_literal(hstore('{}'::text[], null));
205
select hstore(ARRAY['a'], '{}'::text[]);  -- error
206
select hstore('{}'::text[], ARRAY['a']);  -- error
207
select pg_column_size(hstore(ARRAY['a','b','asd'], ARRAY['g','h','i']))
208
         = pg_column_size('a=>g, b=>h, asd=>i'::hstore);
209

210
-- records
211
select hstore(v) from (values (1, 'foo', 1.2, 3::float8)) v(a,b,c,d);
212
create domain hstestdom1 as integer not null default 0;
213
create table testhstore0 (a integer, b text, c numeric, d float8);
214
create table testhstore1 (a integer, b text, c numeric, d float8, e hstestdom1);
215
insert into testhstore0 values (1, 'foo', 1.2, 3::float8);
216
insert into testhstore1 values (1, 'foo', 1.2, 3::float8);
217
select hstore(v) from testhstore1 v;
218
select hstore(null::testhstore0);
219
select hstore(null::testhstore1);
220
select pg_column_size(hstore(v))
221
         = pg_column_size('a=>1, b=>"foo", c=>"1.2", d=>"3", e=>"0"'::hstore)
222
  from testhstore1 v;
223
select populate_record(v, hstore('c', '3.45')) from testhstore1 v;
224
select populate_record(v, hstore('d', '3.45')) from testhstore1 v;
225
select populate_record(v, hstore('e', '123')) from testhstore1 v;
226
select populate_record(v, hstore('e', null)) from testhstore1 v;
227
select populate_record(v, hstore('c', null)) from testhstore1 v;
228
select populate_record(v, hstore('b', 'foo') || hstore('a', '123')) from testhstore1 v;
229
select populate_record(v, hstore('b', 'foo') || hstore('e', null)) from testhstore0 v;
230
select populate_record(v, hstore('b', 'foo') || hstore('e', null)) from testhstore1 v;
231
select populate_record(v, '') from testhstore0 v;
232
select populate_record(v, '') from testhstore1 v;
233
select populate_record(null::testhstore1, hstore('c', '3.45') || hstore('a', '123'));
234
select populate_record(null::testhstore1, hstore('c', '3.45') || hstore('e', '123'));
235
select populate_record(null::testhstore0, '');
236
select populate_record(null::testhstore1, '');
237
select v #= hstore('c', '3.45') from testhstore1 v;
238
select v #= hstore('d', '3.45') from testhstore1 v;
239
select v #= hstore('e', '123') from testhstore1 v;
240
select v #= hstore('c', null) from testhstore1 v;
241
select v #= hstore('e', null) from testhstore0 v;
242
select v #= hstore('e', null) from testhstore1 v;
243
select v #= (hstore('b', 'foo') || hstore('a', '123')) from testhstore1 v;
244
select v #= (hstore('b', 'foo') || hstore('e', '123')) from testhstore1 v;
245
select v #= hstore '' from testhstore0 v;
246
select v #= hstore '' from testhstore1 v;
247
select null::testhstore1 #= (hstore('c', '3.45') || hstore('a', '123'));
248
select null::testhstore1 #= (hstore('c', '3.45') || hstore('e', '123'));
249
select null::testhstore0 #= hstore '';
250
select null::testhstore1 #= hstore '';
251
select v #= h from testhstore1 v, (values (hstore 'a=>123',1),('b=>foo,c=>3.21',2),('a=>null',3),('e=>123',4),('f=>blah',5)) x(h,i) order by i;
252

253
-- keys/values
254
select akeys('aa=>1 , b=>2, cq=>3'::hstore || 'cq=>l, b=>g, fg=>f');
255
select akeys('""=>1');
256
select akeys('');
257
select avals('aa=>1 , b=>2, cq=>3'::hstore || 'cq=>l, b=>g, fg=>f');
258
select avals('aa=>1 , b=>2, cq=>3'::hstore || 'cq=>l, b=>g, fg=>NULL');
259
select avals('""=>1');
260
select avals('');
261

262
select hstore_to_array('aa=>1, cq=>l, b=>g, fg=>NULL'::hstore);
263
select %% 'aa=>1, cq=>l, b=>g, fg=>NULL';
264

265
select hstore_to_matrix('aa=>1, cq=>l, b=>g, fg=>NULL'::hstore);
266
select %# 'aa=>1, cq=>l, b=>g, fg=>NULL';
267

268
select * from skeys('aa=>1 , b=>2, cq=>3'::hstore || 'cq=>l, b=>g, fg=>f');
269
select * from skeys('""=>1');
270
select * from skeys('');
271
select * from svals('aa=>1 , b=>2, cq=>3'::hstore || 'cq=>l, b=>g, fg=>f');
272
select *, svals is null from svals('aa=>1 , b=>2, cq=>3'::hstore || 'cq=>l, b=>g, fg=>NULL');
273
select * from svals('""=>1');
274
select * from svals('');
275

276
select * from each('aaa=>bq, b=>NULL, ""=>1 ');
277

278
-- @>
279
select 'a=>b, b=>1, c=>NULL'::hstore @> 'a=>b';
280
select 'a=>b, b=>1, c=>NULL'::hstore @> 'a=>b, c=>NULL';
281
select 'a=>b, b=>1, c=>NULL'::hstore @> 'a=>b, g=>NULL';
282
select 'a=>b, b=>1, c=>NULL'::hstore @> 'g=>NULL';
283
select 'a=>b, b=>1, c=>NULL'::hstore @> 'a=>c';
284
select 'a=>b, b=>1, c=>NULL'::hstore @> 'a=>b';
285
select 'a=>b, b=>1, c=>NULL'::hstore @> 'a=>b, c=>q';
286

287
CREATE TABLE testhstore (h hstore);
288
\copy testhstore from 'data/hstore.data'
289

290
select count(*) from testhstore where h @> 'wait=>NULL';
291
select count(*) from testhstore where h @> 'wait=>CC';
292
select count(*) from testhstore where h @> 'wait=>CC, public=>t';
293
select count(*) from testhstore where h ? 'public';
294
select count(*) from testhstore where h ?| ARRAY['public','disabled'];
295
select count(*) from testhstore where h ?& ARRAY['public','disabled'];
296

297
create index hidx on testhstore using gist(h);
298
set enable_seqscan=off;
299

300
select count(*) from testhstore where h @> 'wait=>NULL';
301
select count(*) from testhstore where h @> 'wait=>CC';
302
select count(*) from testhstore where h @> 'wait=>CC, public=>t';
303
select count(*) from testhstore where h ? 'public';
304
select count(*) from testhstore where h ?| ARRAY['public','disabled'];
305
select count(*) from testhstore where h ?& ARRAY['public','disabled'];
306

307
drop index hidx;
308
create index hidx on testhstore using gin (h);
309
set enable_seqscan=off;
310

311
select count(*) from testhstore where h @> 'wait=>NULL';
312
select count(*) from testhstore where h @> 'wait=>CC';
313
select count(*) from testhstore where h @> 'wait=>CC, public=>t';
314
select count(*) from testhstore where h ? 'public';
315
select count(*) from testhstore where h ?| ARRAY['public','disabled'];
316
select count(*) from testhstore where h ?& ARRAY['public','disabled'];
317

318
select count(*) from (select (each(h)).key from testhstore) as wow ;
319
select key, count(*) from (select (each(h)).key from testhstore) as wow group by key order by count desc, key;
320

321
-- sort/hash
322
select count(distinct h) from testhstore;
323
set enable_hashagg = false;
324
select count(*) from (select h from (select * from testhstore union all select * from testhstore) hs group by h) hs2;
325
set enable_hashagg = true;
326
set enable_sort = false;
327
select count(*) from (select h from (select * from testhstore union all select * from testhstore) hs group by h) hs2;
328
select distinct * from (values (hstore '' || ''),('')) v(h);
329
set enable_sort = true;
330

331
-- btree
332
drop index hidx;
333
create index hidx on testhstore using btree (h);
334
set enable_seqscan=off;
335

336
select count(*) from testhstore where h #># 'p=>1';
337
select count(*) from testhstore where h = 'pos=>98, line=>371, node=>CBA, indexed=>t';
338

339
-- json and jsonb
340
select hstore_to_json('"a key" =>1, b => t, c => null, d=> 12345, e => 012345, f=> 1.234, g=> 2.345e+4');
341
select cast( hstore  '"a key" =>1, b => t, c => null, d=> 12345, e => 012345, f=> 1.234, g=> 2.345e+4' as json);
342
select hstore_to_json_loose('"a key" =>1, b => t, c => null, d=> 12345, e => 012345, f=> 1.234, g=> 2.345e+4, h=> "2016-01-01"');
343

344
select hstore_to_jsonb('"a key" =>1, b => t, c => null, d=> 12345, e => 012345, f=> 1.234, g=> 2.345e+4');
345
select cast( hstore  '"a key" =>1, b => t, c => null, d=> 12345, e => 012345, f=> 1.234, g=> 2.345e+4' as jsonb);
346
select hstore_to_jsonb_loose('"a key" =>1, b => t, c => null, d=> 12345, e => 012345, f=> 1.234, g=> 2.345e+4, h=> "2016-01-01"');
347

348
create table test_json_agg (f1 text, f2 hstore);
349
insert into test_json_agg values ('rec1','"a key" =>1, b => t, c => null, d=> 12345, e => 012345, f=> 1.234, g=> 2.345e+4'),
350
       ('rec2','"a key" =>2, b => f, c => "null", d=> -12345, e => 012345.6, f=> -1.234, g=> 0.345e-4');
351
select json_agg(q) from test_json_agg q;
352
select json_agg(q) from (select f1, hstore_to_json_loose(f2) as f2 from test_json_agg) q;
353

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.