Исходный код СУБД RuDB — свободной СУБД с открытым исходным кодом на базе PostgreSQL
rudb
2026-06-19 0cfc87bd32822e5ba22d8c2b1a26fa60f63ad7fa
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
SET client_min_messages = warning;
-- DBMS_JSON.BUILD_DATAGUIDE
SELECT EXISTS (
    SELECT 1
    FROM jsonb_array_elements(dbms_json_build_dataguide('{"id": 10, "name": "alice"}'::jsonb)) elem
    WHERE elem->>'path' = '$.id' AND elem->>'type' = 'number'
) AS has_id_path,
EXISTS (
    SELECT 1
    FROM jsonb_array_elements(dbms_json_build_dataguide('{"id": 10, "name": "alice"}'::jsonb)) elem
    WHERE elem->>'path' = '$.name' AND elem->>'type' = 'string'
) AS has_name_path;
 has_id_path | has_name_path 
-------------+---------------
 t           | t
(1 row)
 
SELECT EXISTS (
    SELECT 1
    FROM jsonb_array_elements(dbms_json_build_dataguide('{"info": {"score": 42}}'::jsonb)) elem
    WHERE elem->>'path' = '$.info.score'
) AS has_nested_score_path;
 has_nested_score_path 
-----------------------
 t
(1 row)
 
SELECT dbms_json_build_dataguide('[1,2,3]'::jsonb);
ERROR:  DBMS_JSON.BUILD_DATAGUIDE expects root JSON object
-- DBMS_JSON.GET_INDEX_DATAGUIDE and RENAME_COLUMN metadata
DROP TABLE IF EXISTS dbms_json_metadata;
DROP TABLE IF EXISTS t_json_guide;
CREATE TABLE t_json_guide
(
    j1 jsonb
);
INSERT INTO t_json_guide
VALUES ('{"id": 10, "name": "alice"}'),
       ('{"id": 20, "name": "bob"}');
SELECT dbms_json_get_index_dataguide('t_json_guide', 'j1', 1) LIKE '%$.id%' AS has_id,
       dbms_json_get_index_dataguide('t_json_guide', 'j1', 1) LIKE '%$.name%' AS has_name;
 has_id | has_name 
--------+----------
 t      | t
(1 row)
 
SELECT dbms_json_rename_column('t_json_guide', 'j1', '$.name', 2, 'preferred_name');
 dbms_json_rename_column 
-------------------------
 
(1 row)
 
SELECT dbms_json_get_index_dataguide('t_json_guide', 'j1', 1) LIKE '%preferred_name%' AS has_preferred_name;
 has_preferred_name 
--------------------
 t
(1 row)
 
SELECT position(E'\n' in dbms_json_get_index_dataguide('t_json_guide', 'j1', 1, 1)::text) > 0 AS pretty_has_newline;
 pretty_has_newline 
--------------------
 t
(1 row)
 
SELECT dbms_json_get_index_dataguide('t_json_guide', 'j1', 99);
ERROR:  DBMS_JSON.GET_INDEX_DATAGUIDE invalid format
DETAIL:  format=99
-- DBMS_JSON.GET_VIEW_SQL
DROP SCHEMA IF EXISTS "HR" CASCADE;
CREATE SCHEMA "HR";
SET search_path TO "HR", public;
CREATE TABLE "HR"."T1" ("J1" jsonb);
INSERT INTO "HR"."T1"("J1") VALUES ('{"id":1,"a":10,"b":"txt","info":[{"val":5}]}'::jsonb);
SELECT dbms_json_get_view_sql('V1', 'T1', 'J1',
  '{"type":"object","properties":{"id":{"type":"number","o:length":4,"o:preferred_column_name":"id"}}}') LIKE
  'CREATE OR REPLACE VIEW "HR".v1 AS SELECT JT.%JSON_TABLE%' AS get_view_sql_has_create;
 get_view_sql_has_create 
-------------------------
 t
(1 row)
 
SELECT dbms_json_get_view_sql(NULL, 'T1', 'J1',
  '{"type":"object","properties":{"id":{"type":"number","o:length":4,"o:preferred_column_name":"id"}}}') LIKE
  'SELECT JT.%JSON_TABLE%' AS get_view_sql_select_only;
 get_view_sql_select_only 
--------------------------
 t
(1 row)
 
SELECT dbms_json_get_view_sql('V_EMPTY', 'T1', 'J1',
  '{"type":"object","properties":{}}') = '' AS get_view_sql_empty;
 get_view_sql_empty 
--------------------
 t
(1 row)
 
SELECT dbms_json_get_view_sql('V_ERR', 'T1', 'J1',
  '{"type":"object","properties":{"f1":{"type":"number","o:length":4,"o:preferred_column_name":"col"},"f2":{"type":"number","o:length":4,"o:preferred_column_name":"col"}}}',
  resolvenameconflicts => false);
ERROR:  invalid data guide: column name conflict
DETAIL:  column_name=col
RESET search_path;
DROP SCHEMA "HR" CASCADE;
-- DBMS_JSON.CREATE_VIEW and CREATE_VIEW_ON_PATH
DROP VIEW IF EXISTS v_json_simple;
DROP VIEW IF EXISTS v_json_path_auto;
DROP VIEW IF EXISTS v_json_path_renamed;
DROP VIEW IF EXISTS customer_profile_view;
DROP TABLE IF EXISTS t_json_view;
DROP TABLE IF EXISTS t_json_path;
DROP TABLE IF EXISTS customers;
DROP TABLE IF EXISTS dbms_json_metadata;
CREATE TABLE t_json_view
(
    j1 jsonb
);
INSERT INTO t_json_view
VALUES ('{"id": 10, "name": "alice", "info": {"score": 42}}');
SELECT dbms_json_create_view(
    'v_json_simple',
    't_json_view',
    'j1',
    '[{"path":"$.id","type":"number","column":"id"},{"path":"$.name","type":"string","column":"name"}]'::jsonb
);
 dbms_json_create_view 
-----------------------
 
(1 row)
 
SELECT to_regclass('v_json_simple') AS created_view;
 created_view  
---------------
 v_json_simple
(1 row)
 
TABLE v_json_simple;
 id | name  
----+-------
 10 | alice
(1 row)
 
CREATE TABLE customers
(
    customer_json jsonb
);
INSERT INTO customers
VALUES ('{"customer": {"id": 101, "name": "Alice", "city": "Moscow"}}');
SELECT dbms_json_create_view(
    'customer_profile_view',
    'customers',
    'customer_json',
    '[{"path":"$.customer.id","type":"number","column":"customer_id"},
      {"path":"$.customer.name","type":"string","column":"customer_name"},
      {"path":"$.customer.city","type":"string","column":"customer_city"}]'::jsonb,
    path => '$.customer'
);
 dbms_json_create_view 
-----------------------
 
(1 row)
 
TABLE customer_profile_view;
 customer_id | customer_name | customer_city 
-------------+---------------+---------------
         101 | Alice         | Moscow
(1 row)
 
CREATE TABLE t_json_path
(
    j1 jsonb
);
INSERT INTO t_json_path
VALUES ('{"info": {"score": 42, "label": "gold"}}');
SELECT dbms_json_create_view_on_path('v_json_path_auto', 't_json_path', 'j1', '$.info');
 dbms_json_create_view_on_path 
-------------------------------
 
(1 row)
 
TABLE v_json_path_auto;
 label | score 
-------+-------
 gold  |    42
(1 row)
 
SELECT dbms_json_rename_column('t_json_path', 'j1', '$.info.label', 2, 'preferred_label');
 dbms_json_rename_column 
-------------------------
 
(1 row)
 
SELECT dbms_json_create_view_on_path('v_json_path_renamed', 't_json_path', 'j1', '$.info');
 dbms_json_create_view_on_path 
-------------------------------
 
(1 row)
 
SELECT column_name
FROM information_schema.columns
WHERE table_name = 'v_json_path_renamed'
ORDER BY ordinal_position;
   column_name   
-----------------
 preferred_label
 score
(2 rows)
 
TABLE v_json_path_renamed;
 preferred_label | score 
-----------------+-------
 gold            |    42
(1 row)
 
SELECT dbms_json_create_view('v_json_bad_resource', 't_json_view', 'j1',
    '[{"path":"$.id","type":"number","column":"id"}]'::jsonb,
    '/tmp/resource');
ERROR:  DBMS_JSON.CREATE_VIEW resourcepath is not supported
DETAIL:  resourcepath must be NULL
SELECT dbms_json_create_view2(
    'v_json_service',
    't_json_view',
    'j1',
    '[{"path":"$.id","type":"number","column":"id"},{"path":"$.name","type":"string","column":"name"}]'::jsonb,
    NULL::varchar2,
    false,
    1,
    '$'::varchar2,
    true,
    NULL::varchar2,
    false,
    0::int4
);
 dbms_json_create_view2 
------------------------
 
(1 row)
 
SELECT to_regclass('v_json_service') AS created_service_view;
 created_service_view 
----------------------
 v_json_service
(1 row)
 
TABLE v_json_service;
 id | name  
----+-------
 10 | alice
(1 row)
 
SELECT dbms_json_create_view_on_path('v_json_path_freq', 't_json_path', 'j1', '$.info', 1);
ERROR:  DBMS_JSON.CREATE_VIEW_ON_PATH frequency is not supported in simplified mode
DETAIL:  frequency=1
SELECT dbms_json_create_view_on_path('v_json_path_empty', 't_json_path', 'j1', '$.missing');
ERROR:  DBMS_JSON.CREATE_VIEW_ON_PATH found no scalar fields
DETAIL:  path=$.missing
-- DBMS_JSON.ADD_VIRTUAL_COLUMNS and DROP_VIRTUAL_COLUMNS
DROP TABLE IF EXISTS t_json_vc;
CREATE TABLE t_json_vc
(
    j1 jsonb
);
INSERT INTO t_json_vc
VALUES ('{"id": 10, "name": "alice"}');
SELECT dbms_json_add_virtual_columns(
    't_json_vc',
    'j1',
    '[{"path":"$.id","type":"number","column":"id"},{"path":"$.name","type":"string","column":"name"}]'::jsonb
);
 dbms_json_add_virtual_columns 
-------------------------------
 
(1 row)
 
SELECT column_name
FROM information_schema.columns
WHERE table_name = 't_json_vc'
ORDER BY ordinal_position;
 column_name 
-------------
 j1
 id
 name
(3 rows)
 
TABLE t_json_vc;
             j1              | id | name  
-----------------------------+----+-------
 {"id": 10, "name": "alice"} | 10 | alice
(1 row)
 
SELECT dbms_json_drop_virtual_columns('t_json_vc', 'j1');
 dbms_json_drop_virtual_columns 
--------------------------------
 
(1 row)
 
SELECT column_name
FROM information_schema.columns
WHERE table_name = 't_json_vc'
ORDER BY ordinal_position;
 column_name 
-------------
 j1
(1 row)
 
SELECT dbms_json_add_virtual_columns(
    't_json_vc',
    'j1',
    '[{"path":"$.a","type":"number","column":"dup"},{"path":"$.b","type":"number","column":"dup"}]'::jsonb,
    false
);
ERROR:  invalid data guide: column name conflict
DETAIL:  column_name=dup
-- DBMS_JSON.JSON_TYPE_CONVERTIBLE_CHECK
DROP TABLE IF EXISTS status_json_check;
DROP TABLE IF EXISTS t_json_convert;
CREATE TABLE t_json_convert
(
    j1 jsonb
);
INSERT INTO t_json_convert
VALUES ('{"id": 10}'),
       ('{"id": 20}'),
       (NULL);
SELECT dbms_json_json_type_convertible_check('public', 't_json_convert', 'j1', 'status_json_check');
 dbms_json_json_type_convertible_check 
---------------------------------------
 
(1 row)
 
SELECT to_regclass('public.status_json_check') AS created_status_table;
 created_status_table 
----------------------
 status_json_check
(1 row)
 
SELECT count(*) AS checked_rows, bool_and(is_convertible) AS all_convertible
FROM status_json_check;
 checked_rows | all_convertible 
--------------+-----------------
            2 | t
(1 row)
 
SELECT dbms_json_json_type_convertible_check('public', 't_json_convert', 'j1', 'status_json_check', false, true);
 dbms_json_json_type_convertible_check 
---------------------------------------
 
(1 row)
 
SELECT count(*) AS checked_rows_after_append
FROM status_json_check;
 checked_rows_after_append 
---------------------------
                         4
(1 row)
 
-- Cleanup
DROP VIEW IF EXISTS v_json_simple;
DROP VIEW IF EXISTS v_json_service;
DROP VIEW IF EXISTS v_json_path_auto;
DROP VIEW IF EXISTS v_json_path_renamed;
DROP VIEW IF EXISTS customer_profile_view;
DROP TABLE IF EXISTS t_json_view;
DROP TABLE IF EXISTS t_json_path;
DROP TABLE IF EXISTS customers;
DROP TABLE IF EXISTS t_json_guide;
DROP TABLE IF EXISTS t_json_vc;
DROP TABLE IF EXISTS t_json_convert;
DROP TABLE IF EXISTS status_json_check;
DROP TABLE IF EXISTS dbms_json_metadata;