Исходный код СУБД 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
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
--
-- CREATE_OPERATOR
--
CREATE OPERATOR ## (
   leftarg = path,
   rightarg = path,
   function = path_inter,
   commutator = ##
);
CREATE OPERATOR @#@ (
   rightarg = int8,     -- prefix
   procedure = factorial
);
CREATE OPERATOR #%# (
   leftarg = int8,      -- fail, postfix is no longer supported
   procedure = factorial
);
ERROR:  operator right argument type must be specified
DETAIL:  Postfix operators are not supported.
-- Test operator created above
SELECT @#@ 24;
         ?column?         
--------------------------
 620448401733239439360000
(1 row)
 
-- Test error cases
select @@##@@ 24;  -- no such operator
ERROR:  operator does not exist: @@##@@ integer
LINE 1: select @@##@@ 24;
               ^
DETAIL:  There is no operator of that name.
set search_path = pg_catalog;
select @#@ 24;  -- wrong schema
ERROR:  operator does not exist: @#@ integer
LINE 1: select @#@ 24;
               ^
DETAIL:  An operator of that name exists, but it is not in the search_path.
reset search_path;
select @#@ 24.0;  -- wrong data type
ERROR:  operator does not exist: @#@ numeric
LINE 1: select @#@ 24.0;
               ^
DETAIL:  No operator of that name accepts the given argument type.
HINT:  You might need to add an explicit type cast.
-- Test comments
COMMENT ON OPERATOR ###### (NONE, int4) IS 'bad prefix';
ERROR:  operator does not exist: ###### integer
COMMENT ON OPERATOR ###### (int4, NONE) IS 'bad postfix';
ERROR:  postfix operators are not supported
COMMENT ON OPERATOR ###### (int4, int8) IS 'bad infix';
ERROR:  operator does not exist: integer ###### bigint
-- Check that DROP on a nonexistent op behaves sanely, too
DROP OPERATOR ###### (NONE, int4);
ERROR:  operator does not exist: ###### integer
DROP OPERATOR ###### (int4, NONE);
ERROR:  postfix operators are not supported
DROP OPERATOR ###### (int4, int8);
ERROR:  operator does not exist: integer ###### bigint
-- => is disallowed as an operator name now
CREATE OPERATOR => (
   rightarg = int8,
   procedure = factorial
);
ERROR:  syntax error at or near "=>"
LINE 1: CREATE OPERATOR => (
                        ^
-- lexing of <=, >=, <>, != has a number of edge cases
-- (=> is tested elsewhere)
-- this is legal because ! is not allowed in sql ops
CREATE OPERATOR !=- (
   rightarg = int8,
   procedure = factorial
);
SELECT !=- 10;
 ?column? 
----------
  3628800
(1 row)
 
-- postfix operators don't work anymore
SELECT 10 !=-;
ERROR:  syntax error at or near ";"
LINE 1: SELECT 10 !=-;
                     ^
-- make sure lexer returns != as <> even in edge cases
SELECT 2 !=/**/ 1, 2 !=/**/ 2;
 ?column? | ?column? 
----------+----------
 t        | f
(1 row)
 
SELECT 2 !=-- comment to be removed by psql
  1;
 ?column? 
----------
 t
(1 row)
 
DO $$ -- use DO to protect -- from psql
  declare r boolean;
  begin
    execute $e$ select 2 !=-- comment
      1 $e$ into r;
    raise info 'r = %', r;
  end;
$$;
INFO:  r = t
-- check that <= etc. followed by more operator characters are returned
-- as the correct token with correct precedence
SELECT true<>-1 BETWEEN 1 AND 1;  -- BETWEEN has prec. above <> but below Op
 ?column? 
----------
 t
(1 row)
 
SELECT false<>/**/1 BETWEEN 1 AND 1;
 ?column? 
----------
 t
(1 row)
 
SELECT false<=-1 BETWEEN 1 AND 1;
 ?column? 
----------
 t
(1 row)
 
SELECT false>=-1 BETWEEN 1 AND 1;
 ?column? 
----------
 t
(1 row)
 
SELECT 2<=/**/3, 3>=/**/2, 2<>/**/3;
 ?column? | ?column? | ?column? 
----------+----------+----------
 t        | t        | t
(1 row)
 
SELECT 3<=/**/2, 2>=/**/3, 2<>/**/2;
 ?column? | ?column? | ?column? 
----------+----------+----------
 f        | f        | f
(1 row)
 
-- Should fail. CREATE OPERATOR requires USAGE on SCHEMA
BEGIN TRANSACTION;
CREATE ROLE regress_rol_op1;
CREATE SCHEMA schema_op1;
GRANT USAGE ON SCHEMA schema_op1 TO PUBLIC;
REVOKE USAGE ON SCHEMA schema_op1 FROM regress_rol_op1;
SET ROLE regress_rol_op1;
CREATE OPERATOR schema_op1.#*# (
   rightarg = int8,
   procedure = factorial
);
ERROR:  permission denied for schema schema_op1
ROLLBACK;
-- Should fail. SETOF type functions not allowed as argument (testing leftarg)
BEGIN TRANSACTION;
CREATE OPERATOR #*# (
   leftarg = SETOF int8,
   procedure = factorial
);
ERROR:  SETOF type not allowed for operator argument
ROLLBACK;
-- Should fail. SETOF type functions not allowed as argument (testing rightarg)
BEGIN TRANSACTION;
CREATE OPERATOR #*# (
   rightarg = SETOF int8,
   procedure = factorial
);
ERROR:  SETOF type not allowed for operator argument
ROLLBACK;
-- Should work. Sample text-book case
BEGIN TRANSACTION;
CREATE OR REPLACE FUNCTION fn_op2(boolean, boolean)
RETURNS boolean AS $$
    SELECT NULL::BOOLEAN;
$$ LANGUAGE sql IMMUTABLE;
CREATE OPERATOR ===== (
    LEFTARG = boolean,
    RIGHTARG = boolean,
    PROCEDURE = fn_op2,
    COMMUTATOR = =====,
    NEGATOR = !====,
    RESTRICT = contsel,
    JOIN = contjoinsel,
    SORT1, SORT2, LTCMP, GTCMP, HASHES, MERGES
);
-- Verify that oprcom/oprnegate links are set up correctly
SELECT oprname, oprcode, oprresult::regtype,
       oprcom::regoper, oprnegate::regoper
FROM pg_operator WHERE oprname IN ('=====', '!====') ORDER BY 1;
 oprname | oprcode | oprresult | oprcom | oprnegate 
---------+---------+-----------+--------+-----------
 !====   | -       | -         | 0      | =====
 =====   | fn_op2  | boolean   | =====  | !====
(2 rows)
 
-- Now fill in the shell operator
CREATE OPERATOR !==== (
    LEFTARG = boolean,
    RIGHTARG = boolean,
    PROCEDURE = fn_op2,
    COMMUTATOR = !====
    -- notice we leave NEGATOR to be inferred from the prior command
);
-- Verify that oprcom/oprnegate links are still set up correctly
SELECT oprname, oprcode, oprresult::regtype,
       oprcom::regoper, oprnegate::regoper
FROM pg_operator WHERE oprname IN ('=====', '!====') ORDER BY 1;
 oprname | oprcode | oprresult | oprcom | oprnegate 
---------+---------+-----------+--------+-----------
 !====   | fn_op2  | boolean   | !====  | =====
 =====   | fn_op2  | boolean   | =====  | !====
(2 rows)
 
ROLLBACK;
-- Should fail. Invalid attribute
CREATE OPERATOR #@%# (
   rightarg = int8,
   procedure = factorial,
   invalid_att = int8
);
WARNING:  operator attribute "invalid_att" not recognized
-- Should fail. At least rightarg should be mandatorily specified
CREATE OPERATOR #@%# (
   procedure = factorial
);
ERROR:  operator argument types must be specified
-- Should fail. Procedure should be mandatorily specified
CREATE OPERATOR #@%# (
   rightarg = int8
);
ERROR:  operator function must be specified
-- Should fail. CREATE OPERATOR requires USAGE on TYPE
BEGIN TRANSACTION;
CREATE ROLE regress_rol_op3;
CREATE TYPE type_op3 AS ENUM ('new', 'open', 'closed');
CREATE FUNCTION fn_op3(type_op3, int8)
RETURNS int8 AS $$
    SELECT NULL::int8;
$$ LANGUAGE sql IMMUTABLE;
REVOKE USAGE ON TYPE type_op3 FROM regress_rol_op3;
REVOKE USAGE ON TYPE type_op3 FROM PUBLIC;  -- Need to do this so that regress_rol_op3 is not allowed USAGE via PUBLIC
SET ROLE regress_rol_op3;
CREATE OPERATOR #*# (
   leftarg = type_op3,
   rightarg = int8,
   procedure = fn_op3
);
ERROR:  permission denied for type type_op3
ROLLBACK;
-- Should fail. CREATE OPERATOR requires USAGE on TYPE (need to check separately for rightarg)
BEGIN TRANSACTION;
CREATE ROLE regress_rol_op4;
CREATE TYPE type_op4 AS ENUM ('new', 'open', 'closed');
CREATE FUNCTION fn_op4(int8, type_op4)
RETURNS int8 AS $$
    SELECT NULL::int8;
$$ LANGUAGE sql IMMUTABLE;
REVOKE USAGE ON TYPE type_op4 FROM regress_rol_op4;
REVOKE USAGE ON TYPE type_op4 FROM PUBLIC;  -- Need to do this so that regress_rol_op3 is not allowed USAGE via PUBLIC
SET ROLE regress_rol_op4;
CREATE OPERATOR #*# (
   leftarg = int8,
   rightarg = type_op4,
   procedure = fn_op4
);
ERROR:  permission denied for type type_op4
ROLLBACK;
-- Should fail. CREATE OPERATOR requires EXECUTE on function
BEGIN TRANSACTION;
CREATE ROLE regress_rol_op5;
CREATE TYPE type_op5 AS ENUM ('new', 'open', 'closed');
CREATE FUNCTION fn_op5(int8, int8)
RETURNS int8 AS $$
    SELECT NULL::int8;
$$ LANGUAGE sql IMMUTABLE;
REVOKE EXECUTE ON FUNCTION fn_op5(int8, int8) FROM regress_rol_op5;
REVOKE EXECUTE ON FUNCTION fn_op5(int8, int8) FROM PUBLIC;-- Need to do this so that regress_rol_op3 is not allowed EXECUTE via PUBLIC
SET ROLE regress_rol_op5;
CREATE OPERATOR #*# (
   leftarg = int8,
   rightarg = int8,
   procedure = fn_op5
);
ERROR:  permission denied for function fn_op5
ROLLBACK;
-- Should fail. CREATE OPERATOR requires USAGE on return TYPE
BEGIN TRANSACTION;
CREATE ROLE regress_rol_op6;
CREATE TYPE type_op6 AS ENUM ('new', 'open', 'closed');
CREATE FUNCTION fn_op6(int8, int8)
RETURNS type_op6 AS $$
    SELECT NULL::type_op6;
$$ LANGUAGE sql IMMUTABLE;
REVOKE USAGE ON TYPE type_op6 FROM regress_rol_op6;
REVOKE USAGE ON TYPE type_op6 FROM PUBLIC;  -- Need to do this so that regress_rol_op3 is not allowed USAGE via PUBLIC
SET ROLE regress_rol_op6;
CREATE OPERATOR #*# (
   leftarg = int8,
   rightarg = int8,
   procedure = fn_op6
);
ERROR:  permission denied for type type_op6
ROLLBACK;
-- Should fail. An operator cannot be its own negator.
BEGIN TRANSACTION;
CREATE OPERATOR === (
    leftarg = integer,
    rightarg = integer,
    procedure = int4eq,
    negator = ===
);
ERROR:  operator cannot be its own negator
ROLLBACK;
-- Should fail. An operator cannot be its own negator. Here we check that
-- this error is detected when replacing a shell operator.
BEGIN TRANSACTION;
-- create a shell operator for ===!!! by referencing it as a commutator
CREATE OPERATOR === (
    leftarg = integer,
    rightarg = integer,
    procedure = int4eq,
    commutator = ===!!!
);
CREATE OPERATOR ===!!! (
    leftarg = integer,
    rightarg = integer,
    procedure = int4ne,
    negator = ===!!!
);
ERROR:  operator cannot be its own negator
ROLLBACK;
-- Should fail. A commutator operator must return the same datatype.
BEGIN TRANSACTION;
CREATE OPERATOR === (
    leftarg = integer,
    rightarg = integer,
    procedure = int4eq,
    commutator = <<
);
ERROR:  commutator operator << does not return the same data type as operator ===
ROLLBACK;
-- The same, detected when replacing a shell operator.
BEGIN TRANSACTION;
-- create a shell operator for ===!!! by referencing it as a commutator
CREATE OPERATOR === (
    leftarg = integer,
    rightarg = integer,
    procedure = int4eq,
    commutator = ===!!!
);
CREATE OPERATOR ===!!! (
    leftarg = integer,
    rightarg = integer,
    procedure = int4pl
);
ERROR:  commutator operator === does not return the same data type as operator ===!!!
ROLLBACK;
-- Should fail. A negator operator must return the same datatype.
BEGIN TRANSACTION;
CREATE OPERATOR === (
    leftarg = integer,
    rightarg = integer,
    procedure = int4eq,
    negator = <<
);
ERROR:  negator operator << does not return the same data type as operator ===
ROLLBACK;
-- The same, detected when replacing a shell operator.
-- (We actually get a must-be-boolean error, because that's detected first,
-- but the important point is to reject this.)
BEGIN TRANSACTION;
-- create a shell operator for ===!!! by referencing it as a negator
CREATE OPERATOR === (
    leftarg = integer,
    rightarg = integer,
    procedure = int4eq,
    negator = ===!!!
);
CREATE OPERATOR ===!!! (
    leftarg = integer,
    rightarg = integer,
    procedure = int4pl
);
ERROR:  only boolean operators can have negators
ROLLBACK;
-- test that we can't use part of an existing commutator or negator pair
-- as a commutator or negator
CREATE OPERATOR === (
    leftarg = integer,
    rightarg = integer,
    procedure = int4eq,
    commutator = =
);
ERROR:  commutator operator = is already the commutator of operator =
CREATE OPERATOR === (
    leftarg = integer,
    rightarg = integer,
    procedure = int4eq,
    negator = <>
);
ERROR:  negator operator <> is already the negator of operator =
-- Test that we can't change a shell operator's existing commutator link
BEGIN TRANSACTION;
-- create a shell operator for ===!!! by referencing it as a commutator
CREATE OPERATOR === (
    leftarg = integer,
    rightarg = integer,
    procedure = int4eq,
    commutator = ===!!!
);
CREATE OPERATOR ===!!! (
    leftarg = integer,
    rightarg = integer,
    procedure = int4eq,
    commutator = <<
);
ERROR:  commutator operator === is already the commutator of operator ===!!!
ROLLBACK;
-- Test that we can't change a shell operator's existing negator link
BEGIN TRANSACTION;
-- create a shell operator for ===!!! by referencing it as a negator
CREATE OPERATOR === (
    leftarg = integer,
    rightarg = integer,
    procedure = int4eq,
    negator = ===!!!
);
CREATE OPERATOR ===!!! (
    leftarg = integer,
    rightarg = integer,
    procedure = int4eq,
    negator = <<
);
ERROR:  negator operator === is already the negator of operator ===!!!
ROLLBACK;
-- invalid: non-lowercase quoted identifiers
CREATE OPERATOR ===
(
    "Leftarg" = box,
    "Rightarg" = box,
    "Procedure" = area_equal_function,
    "Commutator" = ===,
    "Negator" = !==,
    "Restrict" = area_restriction_function,
    "Join" = area_join_function,
    "Hashes",
    "Merges"
);
WARNING:  operator attribute "Leftarg" not recognized
WARNING:  operator attribute "Rightarg" not recognized
WARNING:  operator attribute "Procedure" not recognized
WARNING:  operator attribute "Commutator" not recognized
WARNING:  operator attribute "Negator" not recognized
WARNING:  operator attribute "Restrict" not recognized
WARNING:  operator attribute "Join" not recognized
WARNING:  operator attribute "Hashes" not recognized
WARNING:  operator attribute "Merges" not recognized
ERROR:  operator function must be specified