Исходный код СУБД 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
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
-- DBMS_VECTOR: CREATE_CREDENTIAL / DROP_CREDENTIAL.
-- Oracle-faithful: params carry the secret directly (username+password,
-- access_token, or the OCI user_ocid/tenancy_ocid/private_key/fingerprint
-- form; Oracle if/elsif precedence, pls:1290-1362); they are stored verbatim
-- in SYS.vector_credentials and redacted at the SQL boundary.
\! mkdir -p /tmp/kilo/dbms_vector
-- happy path: access_token plus free-form passthrough params
CALL dbms_vector_create_credential('openai_cred', '{"access_token": "sk-test-0600", "provider": "openai", "comment": "main credential"}');
SELECT credential_name, params FROM "SYS".all_vector_credentials ORDER BY credential_name;
 credential_name |                                   params                                    
-----------------+-----------------------------------------------------------------------------
 openai_cred     | {"comment": "main credential", "provider": "openai", "access_token": "***"}
(1 row)
 
SELECT dbms_vector_get_credential('openai_cred') ->> 'access_token' AS access_token;
 access_token 
--------------
 ***
(1 row)
 
SELECT dbms_vector_get_credential('openai_cred') ->> 'provider' AS provider;
 provider 
----------
 openai
(1 row)
 
-- the table itself keeps the raw secret (superuser only)
SELECT params ->> 'access_token' AS raw_access_token FROM "SYS".vector_credentials WHERE credential_name = 'openai_cred';
 raw_access_token 
------------------
 sk-test-0600
(1 row)
 
-- username/password and OCI forms are stored the same way
CALL dbms_vector_create_credential('userpass', '{"username": "u", "password": "p"}');
CALL dbms_vector_create_credential('oci', '{"user_ocid": "ocid1.user..x", "tenancy_ocid": "ocid1.tenancy..x", "private_key": "k", "fingerprint": "aa:bb", "compartment_ocid": "ocid1.comp..x"}');
SELECT credential_name,
       params ->> 'username' AS username,
       params ->> 'password' AS password,
       params ->> 'private_key' AS private_key,
       params ->> 'fingerprint' AS fingerprint
  FROM "SYS".all_vector_credentials
 WHERE credential_name IN ('userpass', 'oci')
 ORDER BY credential_name;
 credential_name | username | password | private_key | fingerprint 
-----------------+----------+----------+-------------+-------------
 oci             |          |          | ***         | aa:bb
 userpass        | u        | ***      |             | 
(2 rows)
 
-- duplicate name
CALL dbms_vector_create_credential('openai_cred', '{"access_token": "sk-test-0600"}');
ERROR:  credential openai_cred already exists
-- params validation
CALL dbms_vector_create_credential('null_params', NULL);
ERROR:  params must be a JSON object with authentication parameters
CALL dbms_vector_create_credential('non_object', '["not", "an", "object"]');
ERROR:  params must be a JSON object
CALL dbms_vector_create_credential('empty_token', '{"access_token": ""}');
ERROR:  invalid value for ACCESS_TOKEN parameter
DETAIL:  Oracle error: ORA-20003 (EXCP_GU_ERR).
CALL dbms_vector_create_credential('non_string_token', '{"access_token": 42}');
ERROR:  invalid value for ACCESS_TOKEN parameter
DETAIL:  Oracle error: ORA-20003 (EXCP_GU_ERR).
CALL dbms_vector_create_credential('empty_password', '{"username": "u", "password": ""}');
ERROR:  invalid value for PASSWORD parameter
DETAIL:  Oracle error: ORA-20003 (EXCP_GU_ERR).
CALL dbms_vector_create_credential('non_string_fingerprint', '{"user_ocid": "ocid1.user..x", "tenancy_ocid": "ocid1.tenancy..x", "private_key": "k", "fingerprint": 42}');
ERROR:  invalid value for FINGERPRINT parameter
DETAIL:  Oracle error: ORA-20003 (EXCP_GU_ERR).
CALL dbms_vector_create_credential('no_form', '{"provider": "openai"}');
ERROR:  params must specify one of: username+password, access_token, or user_ocid+tenancy_ocid+private_key+fingerprint
CALL dbms_vector_create_credential('no_form_empty', '{}');
ERROR:  params must specify one of: username+password, access_token, or user_ocid+tenancy_ocid+private_key+fingerprint
CALL dbms_vector_create_credential('half_userpass', '{"username": "u"}');
ERROR:  username and password must be specified together
CALL dbms_vector_create_credential('partial_oci', '{"user_ocid": "ocid1.user..x", "tenancy_ocid": "ocid1.tenancy..x", "private_key": "k"}');
ERROR:  params must specify one of: username+password, access_token, or user_ocid+tenancy_ocid+private_key+fingerprint
-- name validation
CALL dbms_vector_create_credential('bad"name', '{"access_token": "sk-test-0600"}');
ERROR:  invalid credential name: bad"name
CALL dbms_vector_create_credential(NULL, NULL);
ERROR:  credential name must be specified
CALL dbms_vector_drop_credential(NULL);
ERROR:  credential name must be specified
-- qualified names: a superuser may manage a foreign owner
CALL dbms_vector_create_credential('other_user.shared_cred', '{"access_token": "sk-test-0600"}');
SELECT dbms_vector_get_credential('other_user.shared_cred') ->> 'access_token' AS access_token;
 access_token 
--------------
 ***
(1 row)
 
CALL dbms_vector_drop_credential('other_user.shared_cred');
-- a non-superuser cannot touch the credential catalog at all
CREATE ROLE dbms_vector_user LOGIN;
\set VERBOSITY sqlstate
SET ROLE dbms_vector_user;
CALL dbms_vector_create_credential('own_cred', '{"access_token": "sk-test-0600"}');
ERROR:  42501
SELECT dbms_vector_get_credential('openai_cred');
ERROR:  42501
\set VERBOSITY default
RESET ROLE;
DROP ROLE dbms_vector_user;
-- get_credential of a missing credential
SELECT dbms_vector_get_credential('no_such_cred');
ERROR:  credential no_such_cred does not exist
DETAIL:  Oracle error: ORA-20001 (EXCP_CRED_NOT_EXIST).
-- drop
CALL dbms_vector_drop_credential('userpass');
CALL dbms_vector_drop_credential('oci');
CALL dbms_vector_drop_credential('openai_cred');
CALL dbms_vector_drop_credential('openai_cred');
ERROR:  credential openai_cred does not exist
DETAIL:  Oracle error: ORA-20001 (EXCP_CRED_NOT_EXIST).
CALL dbms_vector_drop_credential('never_existed');
ERROR:  credential never_existed does not exist
DETAIL:  Oracle error: ORA-20001 (EXCP_CRED_NOT_EXIST).
SELECT credential_name FROM "SYS".all_vector_credentials ORDER BY 1;
 credential_name 
-----------------
(0 rows)
 
-- ===========================================================================
-- LOAD_ONNX_MODEL / DROP_ONNX_MODEL (catalog storage; on builds with the
-- ONNX runtime the model bytes are validated at LOAD time, so the fake
-- blobs below are then rejected - the tests assert flavor-consistent
-- behavior through outcome tables instead of absolute messages).
-- Flavor probe: 1 on --with-onnxruntime builds, 0 otherwise (not printed).
SELECT dbms_vector_onnx_enabled()::int AS onnx \gset
\! mkdir -p /tmp/kilo/dbms_vector/models
\! printf '\010\006\022\004test' > /tmp/kilo/dbms_vector/models/model.onnx
\! printf '\010\006' > /tmp/rudb_dbms_vector_smoke.onnx
-- Load/drop outcomes per flavor: 'loaded'/'dropped' without the runtime,
-- 'rejected' with it (real protobuf validation at LOAD).
CREATE TEMP TABLE dbms_vector_outcomes(op text, outcome text);
CREATE TEMP TABLE dbms_vector_expected(op text, want text);
INSERT INTO dbms_vector_expected VALUES
    ('load_doc_model',      CASE WHEN :'onnx'::int = 1 THEN 'rejected' ELSE 'loaded' END),
    ('load_defmeta_model',  CASE WHEN :'onnx'::int = 1 THEN 'rejected' ELSE 'loaded' END),
    ('duplicate_doc_model', 'rejected'),
    ('load_dir_model',      CASE WHEN :'onnx'::int = 1 THEN 'rejected' ELSE 'loaded' END),
    ('load_tempdir_model',  CASE WHEN :'onnx'::int = 1 THEN 'rejected' ELSE 'loaded' END),
    ('drop_doc_model',      CASE WHEN :'onnx'::int = 1 THEN 'rejected' ELSE 'dropped' END),
    ('drop_defmeta_model',  CASE WHEN :'onnx'::int = 1 THEN 'rejected' ELSE 'dropped' END),
    ('drop_dir_model',      CASE WHEN :'onnx'::int = 1 THEN 'rejected' ELSE 'dropped' END),
    ('drop_tempdir_model',  CASE WHEN :'onnx'::int = 1 THEN 'rejected' ELSE 'dropped' END),
    ('superuser_foreign_owner', CASE WHEN :'onnx'::int = 1 THEN 'rejected' ELSE 'managed' END);
-- BLOB overload: explicit metadata
DO $$
BEGIN
    CALL dbms_vector_load_onnx_model('doc_model', '\x0806120474657374'::bytea, '{"function": "embedding", "source": "unit-test"}');
    INSERT INTO dbms_vector_outcomes VALUES ('load_doc_model', 'loaded');
EXCEPTION WHEN OTHERS THEN
    INSERT INTO dbms_vector_outcomes VALUES ('load_doc_model', 'rejected');
END $$;
-- BLOB overload: default metadata (NULL)
DO $$
BEGIN
    CALL dbms_vector_load_onnx_model('defmeta_model', '\x0806'::bytea, NULL);
    INSERT INTO dbms_vector_outcomes VALUES ('load_defmeta_model', 'loaded');
EXCEPTION WHEN OTHERS THEN
    INSERT INTO dbms_vector_outcomes VALUES ('load_defmeta_model', 'rejected');
END $$;
SELECT count(*) = CASE WHEN :'onnx'::int = 1 THEN 0 ELSE 2 END AS stored_models FROM "SYS".all_vector_models;
 stored_models 
---------------
 t
(1 row)
 
SELECT count(*) = CASE WHEN :'onnx'::int = 1 THEN 0 ELSE 1 END AS doc_model_metadata FROM "SYS".all_vector_models WHERE model_name = 'doc_model' AND metadata ->> 'source' = 'unit-test';
 doc_model_metadata 
--------------------
 t
(1 row)
 
-- duplicate model: rejected on both flavors (duplicate error without the
-- runtime, validation error with it)
DO $$
BEGIN
    CALL dbms_vector_load_onnx_model('doc_model', '\x0806'::bytea, NULL);
    INSERT INTO dbms_vector_outcomes VALUES ('duplicate_doc_model', 'loaded');
EXCEPTION WHEN OTHERS THEN
    INSERT INTO dbms_vector_outcomes VALUES ('duplicate_doc_model', 'rejected');
END $$;
-- model data validation
CALL dbms_vector_load_onnx_model('empty_model', '\x'::bytea, NULL);
ERROR:  ONNX model data must not be empty
DETAIL:  Oracle error: ORA-20003 (EXCP_GU_ERR).
CALL dbms_vector_load_onnx_model('null_model', NULL, NULL);
ERROR:  ONNX model data must be specified
DETAIL:  Oracle error: ORA-20003 (EXCP_GU_ERR).
CALL dbms_vector_load_onnx_model(NULL, '\x0806'::bytea, NULL);
ERROR:  model name must be specified
-- metadata must be a JSON object
CALL dbms_vector_load_onnx_model('badmeta_model', '\x0806'::bytea, '[1, 2]');
ERROR:  metadata must be a JSON object
-- name validation
CALL dbms_vector_load_onnx_model('bad"name', '\x0806'::bytea, NULL);
ERROR:  invalid model name: bad"name
CALL dbms_vector_drop_onnx_model(NULL, false);
ERROR:  model name must be specified
-- directory overload: absolute path
DO $$
BEGIN
    CALL dbms_vector_load_onnx_model_dir('/tmp/kilo/dbms_vector/models', 'model.onnx', 'dir_model', '{"source": "directory"}');
    INSERT INTO dbms_vector_outcomes VALUES ('load_dir_model', 'loaded');
EXCEPTION WHEN OTHERS THEN
    INSERT INTO dbms_vector_outcomes VALUES ('load_dir_model', 'rejected');
END $$;
SELECT count(*) = CASE WHEN :'onnx'::int = 1 THEN 0 ELSE 1 END AS dir_model_metadata FROM "SYS".all_vector_models WHERE model_name = 'dir_model' AND metadata ->> 'source' = 'directory';
 dir_model_metadata 
--------------------
 t
(1 row)
 
-- directory overload: TEMP_DIR alias (BFILE manager resolution)
DO $$
BEGIN
    CALL dbms_vector_load_onnx_model_dir('TEMP_DIR', 'rudb_dbms_vector_smoke.onnx', 'tempdir_model', NULL);
    INSERT INTO dbms_vector_outcomes VALUES ('load_tempdir_model', 'loaded');
EXCEPTION WHEN OTHERS THEN
    INSERT INTO dbms_vector_outcomes VALUES ('load_tempdir_model', 'rejected');
END $$;
SELECT count(*) = CASE WHEN :'onnx'::int = 1 THEN 0 ELSE 1 END AS tempdir_model_bytes FROM "SYS".all_vector_models WHERE model_name = 'tempdir_model' AND octet_length(model_data) = 2;
 tempdir_model_bytes 
---------------------
 t
(1 row)
 
-- qualified name: a superuser may manage a foreign owner (the load itself
-- is rejected by validation on runtime builds)
DO $$
BEGIN
    CALL dbms_vector_load_onnx_model('other_user.shared_model', '\x0806'::bytea, NULL);
    CALL dbms_vector_drop_onnx_model('other_user.shared_model', false);
    INSERT INTO dbms_vector_outcomes VALUES ('superuser_foreign_owner', 'managed');
EXCEPTION WHEN OTHERS THEN
    INSERT INTO dbms_vector_outcomes VALUES ('superuser_foreign_owner', 'rejected');
END $$;
-- directory overload errors
CALL dbms_vector_load_onnx_model_dir('/tmp/kilo/dbms_vector/models', 'no_such.onnx', 'missing_model', NULL);
ERROR:  model file not found on the server: /tmp/kilo/dbms_vector/models/no_such.onnx
DETAIL:  Oracle error: ORA-20003 (EXCP_GU_ERR).
CALL dbms_vector_load_onnx_model_dir('/tmp/kilo/dbms_vector/../models', 'model.onnx', 'dotdot_model', NULL);
ERROR:  model file path must not be empty or contain '..': /tmp/kilo/dbms_vector/../models/model.onnx
CALL dbms_vector_load_onnx_model_dir('TEMP_DIR', '../secret.onnx', 'esc_model', NULL);
ERROR:  invalid file name: ../secret.onnx
DETAIL:  Oracle error: ORA-20003 (EXCP_GU_ERR).
CALL dbms_vector_load_onnx_model_dir('NO_SUCH_DIR', 'model.onnx', 'alias_model', NULL);
ERROR:  non-existent directory or file
DETAIL:  ORA-22285: non-existent directory or file for FILEOPEN operation
CALL dbms_vector_load_onnx_model_dir(NULL, 'model.onnx', 'nulldir_model', NULL);
ERROR:  directory must be specified
CALL dbms_vector_load_onnx_model_dir('/tmp/kilo/dbms_vector/models', NULL, 'nullfile_model', NULL);
ERROR:  file name must be specified
CALL dbms_vector_load_onnx_model_dir('/tmp/kilo/dbms_vector', 'models', 'notfile_model', NULL);
ERROR:  model file is not a regular file: /tmp/kilo/dbms_vector/models
DETAIL:  Oracle error: ORA-20003 (EXCP_GU_ERR).
-- drop (doc_model was either loaded or rejected by validation above, so the
-- second drop misses it on both flavors)
DO $$
BEGIN
    CALL dbms_vector_drop_onnx_model('doc_model', false);
    INSERT INTO dbms_vector_outcomes VALUES ('drop_doc_model', 'dropped');
EXCEPTION WHEN OTHERS THEN
    INSERT INTO dbms_vector_outcomes VALUES ('drop_doc_model', 'rejected');
END $$;
CALL dbms_vector_drop_onnx_model('doc_model', false);
ERROR:  model doc_model does not exist
DETAIL:  Oracle error: ORA-20003 (EXCP_GU_ERR).
CALL dbms_vector_drop_onnx_model('never_existed', true);
ERROR:  model never_existed does not exist
DETAIL:  Oracle error: ORA-20003 (EXCP_GU_ERR).
-- the internal helper is not exposed in pg_catalog; the flavor probe is
SELECT count(*) FROM pg_proc WHERE proname = 'dbms_vector_get_onnx_model';
 count 
-------
     0
(1 row)
 
SELECT count(*) FROM pg_proc WHERE proname = 'dbms_vector_onnx_enabled';
 count 
-------
     1
(1 row)
 
SELECT dbms_vector_onnx_enabled() = (:'onnx'::int = 1) AS probe_consistent;
 probe_consistent 
------------------
 t
(1 row)
 
DO $$
BEGIN
    CALL dbms_vector_drop_onnx_model('defmeta_model', false);
    INSERT INTO dbms_vector_outcomes VALUES ('drop_defmeta_model', 'dropped');
EXCEPTION WHEN OTHERS THEN
    INSERT INTO dbms_vector_outcomes VALUES ('drop_defmeta_model', 'rejected');
END $$;
DO $$
BEGIN
    CALL dbms_vector_drop_onnx_model('dir_model', false);
    INSERT INTO dbms_vector_outcomes VALUES ('drop_dir_model', 'dropped');
EXCEPTION WHEN OTHERS THEN
    INSERT INTO dbms_vector_outcomes VALUES ('drop_dir_model', 'rejected');
END $$;
DO $$
BEGIN
    CALL dbms_vector_drop_onnx_model('tempdir_model', false);
    INSERT INTO dbms_vector_outcomes VALUES ('drop_tempdir_model', 'dropped');
EXCEPTION WHEN OTHERS THEN
    INSERT INTO dbms_vector_outcomes VALUES ('drop_tempdir_model', 'rejected');
END $$;
SELECT model_name FROM "SYS".all_vector_models ORDER BY 1;
 model_name 
------------
(0 rows)
 
SELECT count(*) = 10 AND bool_and(o.outcome = e.want) AS outcomes_match_flavor
FROM dbms_vector_outcomes o JOIN dbms_vector_expected e USING (op);
 outcomes_match_flavor 
-----------------------
 t
(1 row)
 
-- ===========================================================================
-- UTL_TO_EMBEDDING (REST core, OpenAI-compatible provider).
\! rm -f /tmp/dbms_vector_fixture.pid /tmp/dbms_vector_fixture.log /tmp/kilo/dbms_vector/fixture_requests.log
\! python3 "$PG_ABS_SRCDIR/dbms_vector_fixture.py" > /tmp/dbms_vector_fixture.log 2>&1 & echo $! > /tmp/dbms_vector_fixture.pid
\! for attempt in $(seq 1 50); do python3 -c "import socket; s = socket.create_connection(('127.0.0.1', 18180), 1); s.close()" >/dev/null 2>&1 && exit 0; sleep 0.1; done; echo "DBMS_VECTOR fixture startup failed" >&2; exit 1
CALL dbms_vector_create_credential('openai_cred', '{"access_token": "sk-test-0600"}');
CALL dbms_vector_create_credential('wrongkey_cred', '{"access_token": "wrong-key"}');
CALL dbms_vector_create_credential('userpass_cred', '{"username": "u", "password": "p"}');
CALL dbms_vector_create_credential('oci_cred', '{"user_ocid": "ocid1.user..x", "tenancy_ocid": "ocid1.tenancy..x", "private_key": "k", "fingerprint": "aa:bb"}');
CALL dbms_vector_create_credential('mixed_cred', '{"username": "u", "password": "p", "access_token": "sk-test-0600"}');
-- happy path: passthrough keys (encoding_format, user) travel to the provider
SELECT dbms_vector_utl_to_embedding('hello'::clob, '{"provider":"openai","url":"http://127.0.0.1:18180/v1/embeddings","model":"text-embedding-test","credential_name":"openai_cred","encoding_format":"float","user":"tester"}');
 dbms_vector_utl_to_embedding 
------------------------------
 [5,1,0.25]
(1 row)
 
-- provider name is case-insensitive; oracle keys are not forwarded
SELECT dbms_vector_utl_to_embedding('hi'::clob, '{"provider":"OPENAI","url":"http://127.0.0.1:18180/v1/embeddings","model":"text-embedding-test","credential_name":"openai_cred"}');
 dbms_vector_utl_to_embedding 
------------------------------
 [2,1,0.25]
(1 row)
 
-- the result is a genuine vector
SELECT '[5,1,0.25]'::vector <-> dbms_vector_utl_to_embedding('hello'::clob, '{"provider":"openai","url":"http://127.0.0.1:18180/v1/embeddings","model":"text-embedding-test","credential_name":"openai_cred"}');
 ?column? 
----------
        0
(1 row)
 
-- the fixture saw the api key from the stored credential and the exact body keys
\! cat /tmp/kilo/dbms_vector/fixture_requests.log
embeddings key=sk-test-0600 model=text-embedding-test keys=encoding_format,input,model,user
embeddings key=sk-test-0600 model=text-embedding-test keys=input,model
embeddings key=sk-test-0600 model=text-embedding-test keys=input,model
-- provider 'database' (the default): errors on both flavors - the
-- deferred-inference error without the runtime, the missing MODEL parameter
-- (or the missing model row) with it
CREATE TEMP TABLE dbms_vector_db_provider(op text, outcome text);
DO $$
BEGIN
    PERFORM dbms_vector_utl_to_embedding('hello'::clob, NULL);
    INSERT INTO dbms_vector_db_provider VALUES ('embedding_default', 'succeeded');
EXCEPTION WHEN OTHERS THEN
    INSERT INTO dbms_vector_db_provider VALUES ('embedding_default', 'rejected');
END $$;
DO $$
BEGIN
    PERFORM dbms_vector_utl_to_embedding('hello'::clob, '{"provider":"database"}');
    INSERT INTO dbms_vector_db_provider VALUES ('embedding_database', 'succeeded');
EXCEPTION WHEN OTHERS THEN
    INSERT INTO dbms_vector_db_provider VALUES ('embedding_database', 'rejected');
END $$;
-- with a model name the call reaches the model catalog and misses it on
-- both flavors (deferred inference / model does not exist)
DO $$
BEGIN
    PERFORM dbms_vector_utl_to_embedding('hello'::clob, '{"provider":"database","model":"no_such_model"}');
    INSERT INTO dbms_vector_db_provider VALUES ('embedding_db_model', 'succeeded');
EXCEPTION WHEN OTHERS THEN
    INSERT INTO dbms_vector_db_provider VALUES ('embedding_db_model', 'rejected');
END $$;
SELECT bool_and(outcome = 'rejected') AS db_provider_rejected FROM dbms_vector_db_provider;
 db_provider_rejected 
----------------------
 t
(1 row)
 
-- unknown / invalid provider
SELECT dbms_vector_utl_to_embedding('hello'::clob, '{"provider":"cohere"}');
ERROR:  An unsupported provider was specified: cohere
DETAIL:  Oracle error: ORA-20003 (EXCP_GU_ERR).
HINT:  Supported providers: database (local ONNX), openai.
SELECT dbms_vector_utl_to_embedding('hello'::clob, '{"provider":42}');
ERROR:  invalid value for PROVIDER parameter
DETAIL:  Oracle error: ORA-20003 (EXCP_GU_ERR).
SELECT dbms_vector_utl_to_embedding('hello'::clob, '["not", "an", "object"]');
ERROR:  params must be a JSON object
DETAIL:  Oracle error: ORA-20003 (EXCP_GU_ERR).
-- data is required
SELECT dbms_vector_utl_to_embedding(NULL, NULL);
ERROR:  data must be specified
DETAIL:  Oracle error: ORA-20003 (EXCP_GU_ERR).
-- CHECK_REST_PARAMS parity: url / model / credential_name are required
SELECT dbms_vector_utl_to_embedding('hello'::clob, '{"provider":"openai","model":"text-embedding-test","credential_name":"openai_cred"}');
ERROR:  missing URL parameter
DETAIL:  Oracle error: ORA-20003 (EXCP_GU_ERR).
SELECT dbms_vector_utl_to_embedding('hello'::clob, '{"provider":"openai","url":"http://127.0.0.1:18180/v1/embeddings","credential_name":"openai_cred"}');
ERROR:  missing MODEL parameter
DETAIL:  Oracle error: ORA-20003 (EXCP_GU_ERR).
SELECT dbms_vector_utl_to_embedding('hello'::clob, '{"provider":"openai","url":"http://127.0.0.1:18180/v1/embeddings","model":"text-embedding-test"}');
ERROR:  missing CREDENTIAL_NAME parameter
DETAIL:  Oracle error: ORA-20003 (EXCP_GU_ERR).
-- an empty value counts as missing (Oracle '' IS NULL)
SELECT dbms_vector_utl_to_embedding('hello'::clob, '{"provider":"openai","url":"","model":"text-embedding-test","credential_name":"openai_cred"}');
ERROR:  missing URL parameter
DETAIL:  Oracle error: ORA-20003 (EXCP_GU_ERR).
-- unknown credential
SELECT dbms_vector_utl_to_embedding('hello'::clob, '{"provider":"openai","url":"http://127.0.0.1:18180/v1/embeddings","model":"text-embedding-test","credential_name":"no_such_cred"}');
ERROR:  credential no_such_cred does not exist
DETAIL:  Oracle error: ORA-20001 (EXCP_CRED_NOT_EXIST).
-- transfer_timeout validation
SELECT dbms_vector_utl_to_embedding('hello'::clob, '{"provider":"openai","url":"http://127.0.0.1:18180/v1/embeddings","model":"text-embedding-test","credential_name":"openai_cred","transfer_timeout":-1}');
ERROR:  invalid value for transfer_timeout parameter
DETAIL:  Oracle error: ORA-29225.
SELECT dbms_vector_utl_to_embedding('hello'::clob, '{"provider":"openai","url":"http://127.0.0.1:18180/v1/embeddings","model":"text-embedding-test","credential_name":"openai_cred","transfer_timeout":"ten"}');
ERROR:  invalid value for transfer_timeout parameter
DETAIL:  Oracle error: ORA-29225.
-- wrong api key: provider answers 401, $.error.message is extracted
SELECT dbms_vector_utl_to_embedding('hello'::clob, '{"provider":"openai","url":"http://127.0.0.1:18180/v1/embeddings","model":"text-embedding-test","credential_name":"wrongkey_cred"}');
ERROR:  provider returned an error: Invalid API key provided
DETAIL:  Oracle error: ORA-20002 (EXCP_PROVIDER_ERR).
-- username/password credentials have no bearer key (Oracle providers always
-- send 'Bearer ' || KEY, pls:2253; this port raises a clear error instead)
SELECT dbms_vector_utl_to_embedding('hello'::clob, '{"provider":"openai","url":"http://127.0.0.1:18180/v1/embeddings","model":"text-embedding-test","credential_name":"userpass_cred"}');
ERROR:  username/password credentials cannot be used with the openai provider
DETAIL:  Oracle providers always send the stored key as a bearer token (pls:2253); a username/password credential has no key.
-- mixed params: userpass wins (Oracle if/elsif precedence, pls:1290)
SELECT dbms_vector_utl_to_embedding('hello'::clob, '{"provider":"openai","url":"http://127.0.0.1:18180/v1/embeddings","model":"text-embedding-test","credential_name":"mixed_cred"}');
ERROR:  username/password credentials cannot be used with the openai provider
DETAIL:  Oracle providers always send the stored key as a bearer token (pls:2253); a username/password credential has no key.
-- OCI credentials require request signing (deferred, pls:2529-2594)
SELECT dbms_vector_utl_to_embedding('hello'::clob, '{"provider":"openai","url":"http://127.0.0.1:18180/v1/embeddings","model":"text-embedding-test","credential_name":"oci_cred"}');
ERROR:  OCI request signing credentials are not supported by the REST provider
HINT:  Use an access_token credential with provider 'openai'.
-- HTTP 400 with $.error.message
SELECT dbms_vector_utl_to_embedding('hello'::clob, '{"provider":"openai","url":"http://127.0.0.1:18180/v1/bad_request","model":"text-embedding-test","credential_name":"openai_cred"}');
ERROR:  provider returned an error: Invalid request: unknown model
DETAIL:  Oracle error: ORA-20002 (EXCP_PROVIDER_ERR).
-- HTTP 500 with a non-JSON body: the raw body is the message
SELECT dbms_vector_utl_to_embedding('hello'::clob, '{"provider":"openai","url":"http://127.0.0.1:18180/v1/server_error","model":"text-embedding-test","credential_name":"openai_cred"}');
ERROR:  provider returned an error: upstream exploded
DETAIL:  Oracle error: ORA-20002 (EXCP_PROVIDER_ERR).
-- 200 without data: $.message / $.error / $.error[0] extraction
SELECT dbms_vector_utl_to_embedding('hello'::clob, '{"provider":"openai","url":"http://127.0.0.1:18180/v1/message_error","model":"text-embedding-test","credential_name":"openai_cred"}');
ERROR:  provider returned an error: model overloaded, retry later
DETAIL:  Oracle error: ORA-20002 (EXCP_PROVIDER_ERR).
SELECT dbms_vector_utl_to_embedding('hello'::clob, '{"provider":"openai","url":"http://127.0.0.1:18180/v1/plain_error","model":"text-embedding-test","credential_name":"openai_cred"}');
ERROR:  provider returned an error: quota exceeded for this key
DETAIL:  Oracle error: ORA-20002 (EXCP_PROVIDER_ERR).
SELECT dbms_vector_utl_to_embedding('hello'::clob, '{"provider":"openai","url":"http://127.0.0.1:18180/v1/array_error","model":"text-embedding-test","credential_name":"openai_cred"}');
ERROR:  provider returned an error: bad input
DETAIL:  Oracle error: ORA-20002 (EXCP_PROVIDER_ERR).
-- transfer_timeout applies to the whole request (fixture endpoint sleeps 3 s)
SELECT dbms_vector_utl_to_embedding('hello'::clob, '{"provider":"openai","url":"http://127.0.0.1:18180/v1/slow","model":"text-embedding-test","credential_name":"openai_cred","transfer_timeout":1}');
ERROR:  provider request failed: Timeout was reached
-- ===========================================================================
-- UTL_TO_EMBEDDINGS (CLOB and VECTOR_ARRAY_T overloads).
\! rm -f /tmp/kilo/dbms_vector/fixture_requests.log
-- CLOB overload: wrapped as the single chunk {"chunk_id":"1","chunk_data":...}
SELECT dbms_vector_utl_to_embeddings('hello'::clob, '{"provider":"openai","url":"http://127.0.0.1:18180/v1/embeddings","model":"text-embedding-test","credential_name":"openai_cred"}');
                             dbms_vector_utl_to_embeddings                              
----------------------------------------------------------------------------------------
 {"{\"embed_id\": \"1\", \"embed_data\": \"hello\", \"embed_vector\": \"[5,1,0.25]\"}"}
(1 row)
 
-- the envelope carries embed_id / embed_data / embed_vector
SELECT (dbms_vector_utl_to_embeddings('hello'::clob, '{"provider":"openai","url":"http://127.0.0.1:18180/v1/embeddings","model":"text-embedding-test","credential_name":"openai_cred"}'))[1]::jsonb ->> 'embed_id';
 ?column? 
----------
 1
(1 row)
 
SELECT (dbms_vector_utl_to_embeddings('hello'::clob, '{"provider":"openai","url":"http://127.0.0.1:18180/v1/embeddings","model":"text-embedding-test","credential_name":"openai_cred"}'))[1]::jsonb ->> 'embed_data';
 ?column? 
----------
 hello
(1 row)
 
-- embed_vector is castable to vector
SELECT ((dbms_vector_utl_to_embeddings('hello'::clob, '{"provider":"openai","url":"http://127.0.0.1:18180/v1/embeddings","model":"text-embedding-test","credential_name":"openai_cred"}'))[1]::jsonb ->> 'embed_vector')::vector <-> '[5,1,0.25]'::vector;
 ?column? 
----------
        0
(1 row)
 
-- array overload: batch_size 2 splits 3 chunks into 2 requests (fixture log)
SELECT dbms_vector_utl_to_embeddings(array['{"chunk_id":"1","chunk_data":"aaaa"}','{"chunk_id":"2","chunk_data":"b"}','{"chunk_id":"3","chunk_data":"cc"}']::text[], '{"provider":"openai","url":"http://127.0.0.1:18180/v1/embeddings","model":"text-embedding-test","credential_name":"openai_cred","batch_size":2}');
                                                                                                              dbms_vector_utl_to_embeddings                                                                                                               
----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
 {"{\"embed_id\": \"1\", \"embed_data\": \"aaaa\", \"embed_vector\": \"[4,1,0.25]\"}","{\"embed_id\": \"2\", \"embed_data\": \"b\", \"embed_vector\": \"[1,2,0.25]\"}","{\"embed_id\": \"3\", \"embed_data\": \"cc\", \"embed_vector\": \"[2,1,0.25]\"}"}
(1 row)
 
\! cat /tmp/kilo/dbms_vector/fixture_requests.log
embeddings key=sk-test-0600 model=text-embedding-test keys=input,model
embeddings key=sk-test-0600 model=text-embedding-test keys=input,model
embeddings key=sk-test-0600 model=text-embedding-test keys=input,model
embeddings key=sk-test-0600 model=text-embedding-test keys=input,model
embeddings key=sk-test-0600 model=text-embedding-test keys=input,model
embeddings key=sk-test-0600 model=text-embedding-test keys=input,model
-- batch_size 1: one request per chunk
\! rm -f /tmp/kilo/dbms_vector/fixture_requests.log
SELECT array_length(dbms_vector_utl_to_embeddings(array['{"chunk_id":"1","chunk_data":"aaaa"}','{"chunk_id":"2","chunk_data":"b"}']::text[], '{"provider":"openai","url":"http://127.0.0.1:18180/v1/embeddings","model":"text-embedding-test","credential_name":"openai_cred","batch_size":1}'), 1);
 array_length 
--------------
            2
(1 row)
 
\! cat /tmp/kilo/dbms_vector/fixture_requests.log
embeddings key=sk-test-0600 model=text-embedding-test keys=input,model
embeddings key=sk-test-0600 model=text-embedding-test keys=input,model
-- chunk_id is passed through verbatim (numeric stays numeric)
SELECT (dbms_vector_utl_to_embeddings(array['{"chunk_id":42,"chunk_data":"b"}']::text[], '{"provider":"openai","url":"http://127.0.0.1:18180/v1/embeddings","model":"text-embedding-test","credential_name":"openai_cred"}'))[1]::jsonb -> 'embed_id';
 ?column? 
----------
 42
(1 row)
 
-- missing chunk_id becomes JSON null
SELECT (dbms_vector_utl_to_embeddings(array['{"chunk_data":"b"}']::text[], '{"provider":"openai","url":"http://127.0.0.1:18180/v1/embeddings","model":"text-embedding-test","credential_name":"openai_cred"}'))[1]::jsonb -> 'embed_id';
 ?column? 
----------
 null
(1 row)
 
-- empty input array
SELECT dbms_vector_utl_to_embeddings('{}'::text[], '{"provider":"openai","url":"http://127.0.0.1:18180/v1/embeddings","model":"text-embedding-test","credential_name":"openai_cred"}');
 dbms_vector_utl_to_embeddings 
-------------------------------
 {}
(1 row)
 
-- batch_size validation
SELECT dbms_vector_utl_to_embeddings(array['{"chunk_id":"1","chunk_data":"b"}']::text[], '{"provider":"openai","url":"http://127.0.0.1:18180/v1/embeddings","model":"text-embedding-test","credential_name":"openai_cred","batch_size":0}');
ERROR:  invalid value for BATCH_SIZE parameter
DETAIL:  Oracle error: ORA-20003 (EXCP_GU_ERR).
SELECT dbms_vector_utl_to_embeddings(array['{"chunk_id":"1","chunk_data":"b"}']::text[], '{"provider":"openai","url":"http://127.0.0.1:18180/v1/embeddings","model":"text-embedding-test","credential_name":"openai_cred","batch_size":"ten"}');
ERROR:  invalid value for BATCH_SIZE parameter
DETAIL:  Oracle error: ORA-20003 (EXCP_GU_ERR).
-- provider dispatch parity with UTL_TO_EMBEDDING (the 'database' cases
-- error on both flavors and are recorded in dbms_vector_db_provider)
DO $$
BEGIN
    PERFORM dbms_vector_utl_to_embeddings('hello'::clob, NULL);
    INSERT INTO dbms_vector_db_provider VALUES ('embeddings_default', 'succeeded');
EXCEPTION WHEN OTHERS THEN
    INSERT INTO dbms_vector_db_provider VALUES ('embeddings_default', 'rejected');
END $$;
DO $$
BEGIN
    PERFORM dbms_vector_utl_to_embeddings('hello'::clob, '{"provider":"database","model":"no_such_model"}');
    INSERT INTO dbms_vector_db_provider VALUES ('embeddings_db_model', 'succeeded');
EXCEPTION WHEN OTHERS THEN
    INSERT INTO dbms_vector_db_provider VALUES ('embeddings_db_model', 'rejected');
END $$;
SELECT (bool_and(outcome = 'rejected') AND count(*) = 5) AS db_provider_all_rejected FROM dbms_vector_db_provider;
 db_provider_all_rejected 
--------------------------
 t
(1 row)
 
SELECT dbms_vector_utl_to_embeddings('hello'::clob, '{"provider":"cohere"}');
ERROR:  An unsupported provider was specified: cohere
DETAIL:  Oracle error: ORA-20003 (EXCP_GU_ERR).
HINT:  Supported providers: database (local ONNX), openai.
SELECT dbms_vector_utl_to_embeddings(array['{"chunk_id":"1","chunk_data":"b"}']::text[], '{"provider":"cohere"}');
ERROR:  An unsupported provider was specified: cohere
DETAIL:  Oracle error: ORA-20003 (EXCP_GU_ERR).
HINT:  Supported providers: database (local ONNX), openai.
-- data is required
SELECT dbms_vector_utl_to_embeddings(NULL, NULL);
ERROR:  data must be specified
DETAIL:  Oracle error: ORA-20003 (EXCP_GU_ERR).
SELECT dbms_vector_utl_to_embeddings(NULL::text[], NULL);
ERROR:  data must be specified
DETAIL:  Oracle error: ORA-20003 (EXCP_GU_ERR).
-- element validation
SELECT dbms_vector_utl_to_embeddings(array['not json']::text[], '{"provider":"openai","url":"http://127.0.0.1:18180/v1/embeddings","model":"text-embedding-test","credential_name":"openai_cred"}');
ERROR:  data(1) is not a valid JSON document
DETAIL:  Oracle error: ORA-20003 (EXCP_GU_ERR).
SELECT dbms_vector_utl_to_embeddings(array['[1, 2]']::text[], '{"provider":"openai","url":"http://127.0.0.1:18180/v1/embeddings","model":"text-embedding-test","credential_name":"openai_cred"}');
ERROR:  data(1) must be a JSON object with chunk_data
DETAIL:  Oracle error: ORA-20003 (EXCP_GU_ERR).
SELECT dbms_vector_utl_to_embeddings(array['{"chunk_id":"1"}']::text[], '{"provider":"openai","url":"http://127.0.0.1:18180/v1/embeddings","model":"text-embedding-test","credential_name":"openai_cred"}');
ERROR:  data(1) is missing a string chunk_data
DETAIL:  Oracle error: ORA-20003 (EXCP_GU_ERR).
SELECT dbms_vector_utl_to_embeddings(array['{"chunk_data":42}']::text[], '{"provider":"openai","url":"http://127.0.0.1:18180/v1/embeddings","model":"text-embedding-test","credential_name":"openai_cred"}');
ERROR:  data(1) is missing a string chunk_data
DETAIL:  Oracle error: ORA-20003 (EXCP_GU_ERR).
SELECT dbms_vector_utl_to_embeddings(array[NULL]::text[], '{"provider":"openai","url":"http://127.0.0.1:18180/v1/embeddings","model":"text-embedding-test","credential_name":"openai_cred"}');
ERROR:  data(1) must not be NULL
DETAIL:  Oracle error: ORA-20003 (EXCP_GU_ERR).
-- CHECK_REST_PARAMS parity on the batch path
SELECT dbms_vector_utl_to_embeddings(array['{"chunk_id":"1","chunk_data":"b"}']::text[], '{"provider":"openai","model":"text-embedding-test","credential_name":"openai_cred"}');
ERROR:  missing URL parameter
DETAIL:  Oracle error: ORA-20003 (EXCP_GU_ERR).
-- provider errors keep the UTL_TO_EMBEDDING formatting on the batch path
SELECT dbms_vector_utl_to_embeddings(array['{"chunk_id":"1","chunk_data":"b"}']::text[], '{"provider":"openai","url":"http://127.0.0.1:18180/v1/bad_request","model":"text-embedding-test","credential_name":"openai_cred"}');
ERROR:  provider returned an error: Invalid request: unknown model
DETAIL:  Oracle error: ORA-20002 (EXCP_PROVIDER_ERR).
CALL dbms_vector_drop_credential('openai_cred');
CALL dbms_vector_drop_credential('wrongkey_cred');
CALL dbms_vector_drop_credential('userpass_cred');
CALL dbms_vector_drop_credential('oci_cred');
CALL dbms_vector_drop_credential('mixed_cred');
-- ===========================================================================
-- QUERY (dynamic SQL over the in-core vector distance operators; rows are
-- inserted in shuffled order so the result order comes from the distance
-- ORDER BY, not from storage).
CREATE TABLE dbms_vector_query_tab (id int, t text, v vector(3));
INSERT INTO dbms_vector_query_tab VALUES
    (3, 'c', '[1,4,0]'),
    (1, 'a', '[0,1,2]'),
    (5, 'e', '[6,1,0]'),
    (2, 'b', '[3,0,1]'),
    (4, 'd', '[2,2,4]');
CREATE INDEX dbms_vector_query_tab_id_idx ON dbms_vector_query_tab (id);
-- default metric EUCLIDEAN: whole row as JSON, nearest first
SELECT dbms_vector_query('dbms_vector_query_tab', 'v', '[1,1,1]'::vector, 3) =
       '[{"t":"a","v":"[0,1,2]","id":1},{"t":"b","v":"[3,0,1]","id":2},{"t":"c","v":"[1,4,0]","id":3}]'::jsonb
       AS euclidean_top3;
 euclidean_top3 
----------------
 t
(1 row)
 
-- the text overload shares the vector path
SELECT dbms_vector_query('dbms_vector_query_tab', 'v', '[1,1,1]'::text, 5) =
       dbms_vector_query('dbms_vector_query_tab', 'v', '[1,1,1]'::vector, 5)
       AS text_overload_ok;
 text_overload_ok 
------------------
 t
(1 row)
 
-- metric matrix (ids aggregated in result order)
SELECT string_agg(x ->> 'id', ',' ORDER BY ord) AS euclidean
FROM jsonb_array_elements(dbms_vector_query('dbms_vector_query_tab', 'v', '[1,1,1]'::vector, 5)) WITH ORDINALITY AS q(x, ord);
 euclidean 
-----------
 1,2,3,4,5
(1 row)
 
SELECT string_agg(x ->> 'id', ',' ORDER BY ord) AS cosine
FROM jsonb_array_elements(dbms_vector_query('dbms_vector_query_tab', 'v', '[1,1,1]'::vector, 5, NULL, NULL, 'COSINE')) WITH ORDINALITY AS q(x, ord);
  cosine   
-----------
 4,1,2,3,5
(1 row)
 
SELECT string_agg(x ->> 'id', ',' ORDER BY ord) AS dot
FROM jsonb_array_elements(dbms_vector_query('dbms_vector_query_tab', 'v', '[1,1,1]'::vector, 5, NULL, NULL, 'DOT')) WITH ORDINALITY AS q(x, ord);
    dot    
-----------
 4,5,3,2,1
(1 row)
 
SELECT string_agg(x ->> 'id', ',' ORDER BY ord) AS manhattan
FROM jsonb_array_elements(dbms_vector_query('dbms_vector_query_tab', 'v', '[1,1,1]'::vector, 5, NULL, NULL, 'MANHATTAN')) WITH ORDINALITY AS q(x, ord);
 manhattan 
-----------
 1,2,3,4,5
(1 row)
 
SELECT string_agg(x ->> 'id', ',' ORDER BY ord) AS l2sq
FROM jsonb_array_elements(dbms_vector_query('dbms_vector_query_tab', 'v', '[1,1,1]'::vector, 5, NULL, NULL, 'EUCLIDEAN_SQUARED')) WITH ORDINALITY AS q(x, ord);
   l2sq    
-----------
 1,2,3,4,5
(1 row)
 
-- the metric name is case-insensitive
SELECT string_agg(x ->> 'id', ',' ORDER BY ord) AS cos_lower
FROM jsonb_array_elements(dbms_vector_query('dbms_vector_query_tab', 'v', '[1,1,1]'::vector, 5, NULL, NULL, 'cosine')) WITH ORDINALITY AS q(x, ord);
 cos_lower 
-----------
 4,1,2,3,5
(1 row)
 
-- top_k
SELECT string_agg(x ->> 'id', ',' ORDER BY ord) AS top3
FROM jsonb_array_elements(dbms_vector_query('dbms_vector_query_tab', 'v', '[1,1,1]'::vector, 3)) WITH ORDINALITY AS q(x, ord);
 top3  
-------
 1,2,3
(1 row)
 
SELECT dbms_vector_query('dbms_vector_query_tab', 'v', '[1,1,1]'::vector, 0) = '[]'::jsonb AS empty;
 empty 
-------
 t
(1 row)
 
SELECT string_agg(x ->> 'id', ',' ORDER BY ord) AS top100
FROM jsonb_array_elements(dbms_vector_query('dbms_vector_query_tab', 'v', '[1,1,1]'::vector, 100)) WITH ORDINALITY AS q(x, ord);
  top100   
-----------
 1,2,3,4,5
(1 row)
 
-- vec_proj_cols selects specific columns instead of the whole row
SELECT dbms_vector_query('dbms_vector_query_tab', 'v', '[1,1,1]'::vector, 3, '["id", "t"]') =
       '[{"t":"a","id":1},{"t":"b","id":2},{"t":"c","id":3}]'::jsonb AS projected;
 projected 
-----------
 t
(1 row)
 
-- an empty vec_proj_cols behaves like NULL (whole row)
SELECT dbms_vector_query('dbms_vector_query_tab', 'v', '[1,1,1]'::vector, 1, '[]') =
       '[{"t":"a","v":"[0,1,2]","id":1}]'::jsonb AS empty_proj;
 empty_proj 
------------
 t
(1 row)
 
-- use_index = false forces an exact scan
SELECT string_agg(x ->> 'id', ',' ORDER BY ord) AS exactscan
FROM jsonb_array_elements(dbms_vector_query('dbms_vector_query_tab', 'v', '[1,1,1]'::vector, 5, NULL, NULL, 'EUCLIDEAN', false)) WITH ORDINALITY AS q(x, ord);
 exactscan 
-----------
 1,2,3,4,5
(1 row)
 
-- idx_parameters map to hnsw.ef_search / ivfflat.probes around the query
SELECT string_agg(x ->> 'id', ',' ORDER BY ord) AS efsearch
FROM jsonb_array_elements(dbms_vector_query('dbms_vector_query_tab', 'v', '[1,1,1]'::vector, 5, NULL, NULL, 'EUCLIDEAN', true, 95, '{"efsearch": 40}')) WITH ORDINALITY AS q(x, ord);
 efsearch  
-----------
 1,2,3,4,5
(1 row)
 
SELECT string_agg(x ->> 'id', ',' ORDER BY ord) AS probes
FROM jsonb_array_elements(dbms_vector_query('dbms_vector_query_tab', 'v', '[1,1,1]'::vector, 5, NULL, NULL, 'EUCLIDEAN', true, 95, '{"neighbor partition probes": 2}')) WITH ORDINALITY AS q(x, ord);
  probes   
-----------
 1,2,3,4,5
(1 row)
 
SELECT string_agg(x ->> 'id', ',' ORDER BY ord) AS gucs_both
FROM jsonb_array_elements(dbms_vector_query('dbms_vector_query_tab', 'v', '[1,1,1]'::vector, 5, NULL, NULL, 'EUCLIDEAN', true, 95, '{"efsearch": 20, "neighbor partition probes": 1}')) WITH ORDINALITY AS q(x, ord);
 gucs_both 
-----------
 1,2,3,4,5
(1 row)
 
-- accuracy is accepted (and ignored) for Oracle signature parity
SELECT string_agg(x ->> 'id', ',' ORDER BY ord) AS accuracy
FROM jsonb_array_elements(dbms_vector_query('dbms_vector_query_tab', 'v', '[1,1,1]'::vector, 5, NULL, NULL, 'EUCLIDEAN', true, 80)) WITH ORDINALITY AS q(x, ord);
 accuracy  
-----------
 1,2,3,4,5
(1 row)
 
-- idx_name is validated against the table (warning only, no hint forced)
SELECT string_agg(x ->> 'id', ',' ORDER BY ord) AS idx_ok
FROM jsonb_array_elements(dbms_vector_query('dbms_vector_query_tab', 'v', '[1,1,1]'::vector, 5, NULL, 'dbms_vector_query_tab_id_idx')) WITH ORDINALITY AS q(x, ord);
  idx_ok   
-----------
 1,2,3,4,5
(1 row)
 
SELECT string_agg(x ->> 'id', ',' ORDER BY ord) AS idx_warn
FROM jsonb_array_elements(dbms_vector_query('dbms_vector_query_tab', 'v', '[1,1,1]'::vector, 5, NULL, 'no_such_idx')) WITH ORDINALITY AS q(x, ord);
WARNING:  index no_such_idx does not exist on table dbms_vector_query_tab
 idx_warn  
-----------
 1,2,3,4,5
(1 row)
 
-- invalid metric (message parity with pls:857-864)
SELECT dbms_vector_query('dbms_vector_query_tab', 'v', '[1,1,1]'::vector, 3, NULL, NULL, 'CHEBYSHEV');
ERROR:  Distance metric - CHEBYSHEV - is not a valid option
DETAIL:  Oracle error: ORA-20003 (EXCP_GU_ERR).
-- HAMMING exists only for bit vectors (out of scope for this port)
SELECT dbms_vector_query('dbms_vector_query_tab', 'v', '[1,1,1]'::vector, 3, NULL, NULL, 'HAMMING');
ERROR:  Distance metric - HAMMING - is supported only for bit vectors
HINT:  Bit vector distances are outside the scope of this port.
-- required arguments
SELECT dbms_vector_query(NULL, 'v', '[1,1,1]'::vector, 3);
ERROR:  table name must be specified
SELECT dbms_vector_query('dbms_vector_query_tab', NULL, '[1,1,1]'::vector, 3);
ERROR:  vector column name must be specified
SELECT dbms_vector_query('dbms_vector_query_tab', 'v', NULL::vector, 3);
ERROR:  query vector must be specified
DETAIL:  Oracle error: ORA-20003 (EXCP_GU_ERR).
SELECT dbms_vector_query('dbms_vector_query_tab', 'v', NULL::text, 3);
ERROR:  query vector must be specified
DETAIL:  Oracle error: ORA-20003 (EXCP_GU_ERR).
SELECT dbms_vector_query('dbms_vector_query_tab', 'v', '[1,1,1]'::vector, NULL);
ERROR:  top_k must be specified
DETAIL:  Oracle error: ORA-20003 (EXCP_GU_ERR).
-- identifiers must be simple SQL names (DBMS_ASSERT.SIMPLE_SQL_NAME parity)
SELECT dbms_vector_query('public.dbms_vector_query_tab', 'v', '[1,1,1]'::vector, 3);
ERROR:  invalid table name: public.dbms_vector_query_tab
SELECT dbms_vector_query('dbms_vector_query_tab', 'bad-name', '[1,1,1]'::vector, 3);
ERROR:  invalid vector column name: bad-name
-- the text overload validates the vector text through vector_in
SELECT dbms_vector_query('dbms_vector_query_tab', 'v', 'nope', 3);
ERROR:  invalid input syntax for type vector: "nope"
DETAIL:  Vector contents must start with "[".
-- vec_proj_cols must be an array of column name strings
SELECT dbms_vector_query('dbms_vector_query_tab', 'v', '[1,1,1]'::vector, 3, '{"id": 1}');
ERROR:  vec_proj_cols must be a JSON array of column names
DETAIL:  Oracle error: ORA-20003 (EXCP_GU_ERR).
SELECT dbms_vector_query('dbms_vector_query_tab', 'v', '[1,1,1]'::vector, 3, '[42]');
ERROR:  vec_proj_cols(1) must be a column name string
DETAIL:  Oracle error: ORA-20003 (EXCP_GU_ERR).
SELECT dbms_vector_query('dbms_vector_query_tab', 'v', '[1,1,1]'::vector, 3, '["bad-name"]');
ERROR:  invalid projection column name: bad-name
-- query vector dimensions must match the column
SELECT dbms_vector_query('dbms_vector_query_tab', 'v', '[1,1]'::vector, 3);
ERROR:  different vector dimensions 3 and 2
CONTEXT:  SQL statement "SELECT to_jsonb(t.*) FROM dbms_vector_query_tab t ORDER BY t.v <-> $1 LIMIT $2"
-- top_k must be non-negative
SELECT dbms_vector_query('dbms_vector_query_tab', 'v', '[1,1,1]'::vector, -1);
ERROR:  LIMIT must not be negative
CONTEXT:  SQL statement "SELECT to_jsonb(t.*) FROM dbms_vector_query_tab t ORDER BY t.v <-> $1 LIMIT $2"
-- idx_parameters must be an object of numbers >= 1
SELECT dbms_vector_query('dbms_vector_query_tab', 'v', '[1,1,1]'::vector, 3, NULL, NULL, 'EUCLIDEAN', true, 95, '[]');
ERROR:  idx_parameters must be a JSON object
DETAIL:  Oracle error: ORA-20003 (EXCP_GU_ERR).
SELECT dbms_vector_query('dbms_vector_query_tab', 'v', '[1,1,1]'::vector, 3, NULL, NULL, 'EUCLIDEAN', true, 95, '{"efsearch": 0}');
ERROR:  invalid value for EFSEARCH parameter
DETAIL:  Oracle error: ORA-20003 (EXCP_GU_ERR).
SELECT dbms_vector_query('dbms_vector_query_tab', 'v', '[1,1,1]'::vector, 3, NULL, NULL, 'EUCLIDEAN', true, 95, '{"efsearch": "big"}');
ERROR:  invalid value for EFSEARCH parameter
DETAIL:  Oracle error: ORA-20003 (EXCP_GU_ERR).
SELECT dbms_vector_query('dbms_vector_query_tab', 'v', '[1,1,1]'::vector, 3, NULL, NULL, 'EUCLIDEAN', true, 95, '{"neighbor partition probes": "many"}');
ERROR:  invalid value for NEIGHBOR PARTITION PROBES parameter
DETAIL:  Oracle error: ORA-20003 (EXCP_GU_ERR).
DROP TABLE dbms_vector_query_tab;
-- ===========================================================================
-- UTL_TO_CHUNKS (facade over the shared chunker core in dbms_vector_text.cpp;
-- the string overloads need an explicit cast: clob vs varchar2)
-- default params: one chunk per document, detail fields null without extended
SELECT dbms_vector_utl_to_chunks('one two three'::clob);
                                  dbms_vector_utl_to_chunks                                   
----------------------------------------------------------------------------------------------
 [{"chunk_id": 1, "chunk_data": "one two three", "chunk_length": null, "chunk_offset": null}]
(1 row)
 
SELECT dbms_vector_utl_to_chunks('one two three'::clob, NULL::jsonb);
                                  dbms_vector_utl_to_chunks                                   
----------------------------------------------------------------------------------------------
 [{"chunk_id": 1, "chunk_data": "one two three", "chunk_length": null, "chunk_offset": null}]
(1 row)
 
-- extended fills chunk_offset / chunk_length; fixed windows with overlap
SELECT dbms_vector_utl_to_chunks('abcdefghijklmnopqrstuvwxyz'::clob,
                                 '{"by": "chars", "max": 10, "overlap": 3, "split": "none", "extended": true}'::jsonb);
                                                                                                                                                          dbms_vector_utl_to_chunks                                                                                                                                                           
----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
 [{"chunk_id": 1, "chunk_data": "abcdefghij", "chunk_length": 10, "chunk_offset": 1}, {"chunk_id": 2, "chunk_data": "hijklmnopq", "chunk_length": 10, "chunk_offset": 8}, {"chunk_id": 3, "chunk_data": "opqrstuvwx", "chunk_length": 10, "chunk_offset": 15}, {"chunk_id": 4, "chunk_data": "vwxyz", "chunk_length": 5, "chunk_offset": 22}]
(1 row)
 
-- the varchar2 overload chunks identically
SELECT dbms_vector_utl_to_chunks('aa bb cc'::varchar2,
                                 '{"by": "words", "max": 2, "overlap": 1, "split": "none", "extended": true}'::jsonb);
                                                                   dbms_vector_utl_to_chunks                                                                   
---------------------------------------------------------------------------------------------------------------------------------------------------------------
 [{"chunk_id": 1, "chunk_data": "aa bb ", "chunk_length": 6, "chunk_offset": 1}, {"chunk_id": 2, "chunk_data": "bb cc", "chunk_length": 5, "chunk_offset": 4}]
(1 row)
 
-- by words counts words, not characters
SELECT jsonb_array_length(dbms_vector_utl_to_chunks('w1 w2 w3 w4 w5 w6 w7'::clob,
                                                    '{"by": "words", "max": 3, "split": "none"}'::jsonb)::jsonb);
 jsonb_array_length 
--------------------
                  3
(1 row)
 
-- empty input yields an empty document
SELECT dbms_vector_utl_to_chunks(''::clob);
 dbms_vector_utl_to_chunks 
---------------------------
 []
(1 row)
 
-- split by newline: segments pack while they fit max
SELECT dbms_vector_utl_to_chunks(E'aaa bbb\nccc ddd\neee'::clob,
                                 '{"max": 12, "split": "newline", "extended": true}'::jsonb);
                                                                        dbms_vector_utl_to_chunks                                                                         
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------
 [{"chunk_id": 1, "chunk_data": "aaa bbb\n", "chunk_length": 8, "chunk_offset": 1}, {"chunk_id": 2, "chunk_data": "ccc ddd\neee", "chunk_length": 11, "chunk_offset": 9}]
(1 row)
 
-- split by blankline keeps the blank line with the preceding paragraph
SELECT dbms_vector_utl_to_chunks(E'para one\npara two\n\npara three'::clob,
                                 '{"split": "blankline", "max": 20, "extended": true}'::jsonb);
                                                                               dbms_vector_utl_to_chunks                                                                               
---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
 [{"chunk_id": 1, "chunk_data": "para one\npara two\n\n", "chunk_length": 19, "chunk_offset": 1}, {"chunk_id": 2, "chunk_data": "para three", "chunk_length": 10, "chunk_offset": 20}]
(1 row)
 
-- split by sentence cuts after terminator + whitespace; decimals do not cut
SELECT dbms_vector_utl_to_chunks('First sentence. Second one? Third!'::clob,
                                 '{"split": "sentence", "max": 17, "extended": true}'::jsonb);
                                                                                                                     dbms_vector_utl_to_chunks                                                                                                                     
-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
 [{"chunk_id": 1, "chunk_data": "First sentence. ", "chunk_length": 16, "chunk_offset": 1}, {"chunk_id": 2, "chunk_data": "Second one? ", "chunk_length": 12, "chunk_offset": 17}, {"chunk_id": 3, "chunk_data": "Third!", "chunk_length": 6, "chunk_offset": 29}]
(1 row)
 
SELECT jsonb_array_length(dbms_vector_utl_to_chunks('3.14 is a number'::clob,
                                                    '{"split": "sentence"}'::jsonb)::jsonb);
 jsonb_array_length 
--------------------
                  1
(1 row)
 
-- split by custom keeps the delimiter at the end of the segment it closes
SELECT dbms_vector_utl_to_chunks('rule one:rule two;rule three'::clob,
                                 '{"split": "custom", "custom_list": [":", ";"], "max": 10, "extended": true}'::jsonb);
                                                                                                                 dbms_vector_utl_to_chunks                                                                                                                  
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
 [{"chunk_id": 1, "chunk_data": "rule one:", "chunk_length": 9, "chunk_offset": 1}, {"chunk_id": 2, "chunk_data": "rule two;", "chunk_length": 9, "chunk_offset": 10}, {"chunk_id": 3, "chunk_data": "rule three", "chunk_length": 10, "chunk_offset": 19}]
(1 row)
 
-- recursive split (default): blank lines, then newlines, then word boundaries
SELECT dbms_vector_utl_to_chunks(E'aaa bbb ccc\nddd eee\nfff ggg'::clob,
                                 '{"by": "words", "max": 2, "extended": true}'::jsonb);
                                                                                                                                                      dbms_vector_utl_to_chunks                                                                                                                                                      
-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
 [{"chunk_id": 1, "chunk_data": "aaa bbb ", "chunk_length": 8, "chunk_offset": 1}, {"chunk_id": 2, "chunk_data": "ccc\n", "chunk_length": 4, "chunk_offset": 9}, {"chunk_id": 3, "chunk_data": "ddd eee\n", "chunk_length": 8, "chunk_offset": 13}, {"chunk_id": 4, "chunk_data": "fff ggg", "chunk_length": 7, "chunk_offset": 21}]
(1 row)
 
-- normalize all: widechar to ASCII, punctuation dropped, whitespace collapsed
SELECT dbms_vector_utl_to_chunks('  Hello,   world!  '::clob,
                                 '{"normalize": "all", "extended": true}'::jsonb);
                               dbms_vector_utl_to_chunks                               
---------------------------------------------------------------------------------------
 [{"chunk_id": 1, "chunk_data": "Hello world", "chunk_length": 11, "chunk_offset": 1}]
(1 row)
 
SELECT dbms_vector_utl_to_chunks('A B C'::clob,
                                 '{"normalize": "options", "norm_options": ["widechar", "punctuation"], "extended": true}'::jsonb);
                           dbms_vector_utl_to_chunks                            
--------------------------------------------------------------------------------
 [{"chunk_id": 1, "chunk_data": "A B C", "chunk_length": 5, "chunk_offset": 1}]
(1 row)
 
-- language is accepted (single generic tokenizer) and ignored
SELECT jsonb_array_length(dbms_vector_utl_to_chunks('hello brave world'::clob,
                                                    '{"language": "ENGLISH"}'::jsonb)::jsonb);
 jsonb_array_length 
--------------------
                  1
(1 row)
 
-- param validation (Oracle parity messages)
SELECT dbms_vector_utl_to_chunks('x'::clob, '{"by": "bytes"}'::jsonb);
ERROR:  invalid value bytes for BY parameter
DETAIL:  Oracle error: ORA-20003 (EXCP_GU_ERR).
SELECT dbms_vector_utl_to_chunks('x'::clob, '{"max": "big"}'::jsonb);
ERROR:  invalid value for MAX parameter
DETAIL:  Oracle error: ORA-20003 (EXCP_GU_ERR).
SELECT dbms_vector_utl_to_chunks('x'::clob, '{"max": 0}'::jsonb);
ERROR:  invalid value for MAX parameter
DETAIL:  Oracle error: ORA-20003 (EXCP_GU_ERR).
SELECT dbms_vector_utl_to_chunks('x'::clob, '{"overlap": -1}'::jsonb);
ERROR:  invalid value for OVERLAP parameter
DETAIL:  Oracle error: ORA-20003 (EXCP_GU_ERR).
SELECT dbms_vector_utl_to_chunks('x'::clob, '{"max": 5, "overlap": 5}'::jsonb);
ERROR:  invalid value for OVERLAP parameter
DETAIL:  Oracle error: ORA-20003 (EXCP_GU_ERR).
SELECT dbms_vector_utl_to_chunks('x'::clob, '{"split": "everywhere"}'::jsonb);
ERROR:  invalid value everywhere for SPLIT parameter
DETAIL:  Oracle error: ORA-20003 (EXCP_GU_ERR).
SELECT dbms_vector_utl_to_chunks('x'::clob, '{"split": "custom"}'::jsonb);
ERROR:  missing CUSTOM_LIST parameter
DETAIL:  Oracle error: ORA-20003 (EXCP_GU_ERR).
SELECT dbms_vector_utl_to_chunks('x'::clob, '{"split": "custom", "custom_list": []}'::jsonb);
ERROR:  missing CUSTOM_LIST parameter
DETAIL:  Oracle error: ORA-20003 (EXCP_GU_ERR).
SELECT dbms_vector_utl_to_chunks('x'::clob, '{"split": "custom", "custom_list": [42]}'::jsonb);
ERROR:  invalid value for CUSTOM_LIST parameter
DETAIL:  Oracle error: ORA-20003 (EXCP_GU_ERR).
SELECT dbms_vector_utl_to_chunks('x'::clob, '{"by": "vocabulary"}'::jsonb);
ERROR:  missing VOCABULARY parameter
DETAIL:  Oracle error: ORA-20003 (EXCP_GU_ERR).
SELECT dbms_vector_utl_to_chunks('x'::clob, '{"by": "vocabulary", "vocabulary": "my_vocab"}'::jsonb);
ERROR:  chunking by vocabulary is not supported in this port
DETAIL:  Vocabulary storage is not ported; use "by": "chars" or "by": "words".
SELECT dbms_vector_utl_to_chunks('x'::clob, '{"normalize": "sometimes"}'::jsonb);
ERROR:  invalid value sometimes for NORMALIZE parameter
DETAIL:  Oracle error: ORA-20003 (EXCP_GU_ERR).
SELECT dbms_vector_utl_to_chunks('x'::clob, '{"normalize": "options"}'::jsonb);
ERROR:  missing NORM_OPTIONS parameter
DETAIL:  Oracle error: ORA-20003 (EXCP_GU_ERR).
SELECT dbms_vector_utl_to_chunks('x'::clob, '{"normalize": "options", "norm_options": ["shouting"]}'::jsonb);
ERROR:  invalid value shouting for NORM_OPTIONS parameter
DETAIL:  Oracle error: ORA-20003 (EXCP_GU_ERR).
SELECT dbms_vector_utl_to_chunks('x'::clob, '{"extended": "yes"}'::jsonb);
ERROR:  invalid value for EXTENDED parameter
DETAIL:  Oracle error: ORA-20003 (EXCP_GU_ERR).
SELECT dbms_vector_utl_to_chunks('x'::clob, '[]'::jsonb);
ERROR:  params must be a JSON object
DETAIL:  Oracle error: ORA-20003 (EXCP_GU_ERR).
SELECT dbms_vector_utl_to_chunks(NULL::clob);
ERROR:  data must be specified
DETAIL:  Oracle error: ORA-20003 (EXCP_GU_ERR).
-- ===========================================================================
-- UTL_TO_GENERATE_TEXT (facade over the shared generate-text core in
-- dbms_vector_text.cpp; the fixture speaks the OpenAI protocol)
CALL dbms_vector_create_credential('openai_gen', '{"access_token": "sk-test-0600"}');
CALL dbms_vector_create_credential('wrongkey_gen', '{"access_token": "wrong-key"}');
-- start from a clean fixture log
\! rm -f /tmp/kilo/dbms_vector/fixture_requests.log
-- chat shape: system + user messages, answer from $.choices[0].message.content
SELECT dbms_vector_utl_to_generate_text('tell me about vectors'::clob,
    '{"provider":"openai","url":"http://127.0.0.1:18180/v1/chat/completions","model":"gpt-test","credential_name":"openai_gen","temperature":0.7,"user":"tester"}');
 dbms_vector_utl_to_generate_text 
----------------------------------
 reply to: tell me about vectors
(1 row)
 
-- legacy shape: url contains v1/completions, prompt body, answer from $.choices[0].text
SELECT dbms_vector_utl_to_generate_text('once upon a time'::clob,
    '{"provider":"openai","url":"http://127.0.0.1:18180/v1/completions","model":"gpt-test","credential_name":"openai_gen"}');
 dbms_vector_utl_to_generate_text 
----------------------------------
 completion of: once upon a time
(1 row)
 
-- the fixture saw the bearer key, the model, the exact body keys and the
-- prompt (reserved keys stripped, passthrough keys travel)
\! cat /tmp/kilo/dbms_vector/fixture_requests.log
chat key=sk-test-0600 model=gpt-test keys=messages,model,temperature,user prompt=tell me about vectors
completions key=sk-test-0600 model=gpt-test keys=model,prompt prompt=once upon a time
\! rm -f /tmp/kilo/dbms_vector/fixture_requests.log
-- provider is required (Oracle message parity, pls:2975)
SELECT dbms_vector_utl_to_generate_text('hi'::clob);
ERROR:  You did not specify a value for the parameter provider
DETAIL:  Oracle error: ORA-20003 (EXCP_GU_ERR).
SELECT dbms_vector_utl_to_generate_text('hi'::clob, NULL::jsonb);
ERROR:  You did not specify a value for the parameter provider
DETAIL:  Oracle error: ORA-20003 (EXCP_GU_ERR).
SELECT dbms_vector_utl_to_generate_text('hi'::clob, '{"provider":""}'::jsonb);
ERROR:  You did not specify a value for the parameter provider
DETAIL:  Oracle error: ORA-20003 (EXCP_GU_ERR).
-- unsupported provider (pls:2972)
SELECT dbms_vector_utl_to_generate_text('hi'::clob, '{"provider":"cohere"}'::jsonb);
ERROR:  An unsupported provider was specified: cohere
DETAIL:  Oracle error: ORA-20003 (EXCP_GU_ERR).
HINT:  Supported providers: openai.
-- params must be a JSON object
SELECT dbms_vector_utl_to_generate_text('hi'::clob, '["not", "an", "object"]'::jsonb);
ERROR:  params must be a JSON object
DETAIL:  Oracle error: ORA-20003 (EXCP_GU_ERR).
-- data is required
SELECT dbms_vector_utl_to_generate_text(NULL::clob);
ERROR:  data must be specified
DETAIL:  Oracle error: ORA-20003 (EXCP_GU_ERR).
-- CHECK_REST_PARAMS parity: url / model / credential_name are required
SELECT dbms_vector_utl_to_generate_text('hi'::clob, '{"provider":"openai","model":"gpt-test","credential_name":"openai_gen"}');
ERROR:  missing URL parameter
DETAIL:  Oracle error: ORA-20003 (EXCP_GU_ERR).
SELECT dbms_vector_utl_to_generate_text('hi'::clob, '{"provider":"openai","url":"http://127.0.0.1:18180/v1/chat/completions","credential_name":"openai_gen"}');
ERROR:  missing MODEL parameter
DETAIL:  Oracle error: ORA-20003 (EXCP_GU_ERR).
SELECT dbms_vector_utl_to_generate_text('hi'::clob, '{"provider":"openai","url":"http://127.0.0.1:18180/v1/chat/completions","model":"gpt-test"}');
ERROR:  missing CREDENTIAL_NAME parameter
DETAIL:  Oracle error: ORA-20003 (EXCP_GU_ERR).
-- unknown credential (ORA-20001 parity)
SELECT dbms_vector_utl_to_generate_text('hi'::clob, '{"provider":"openai","url":"http://127.0.0.1:18180/v1/chat/completions","model":"gpt-test","credential_name":"no_such_cred"}');
ERROR:  credential no_such_cred does not exist
DETAIL:  Oracle error: ORA-20001 (EXCP_CRED_NOT_EXIST).
-- wrong api key: provider answers 401, $.error.message is extracted
SELECT dbms_vector_utl_to_generate_text('hi'::clob, '{"provider":"openai","url":"http://127.0.0.1:18180/v1/chat/completions","model":"gpt-test","credential_name":"wrongkey_gen"}');
ERROR:  The provider returned an error: Invalid API key provided
DETAIL:  Oracle error: ORA-20002 (EXCP_PROVIDER_ERR).
-- HTTP 400 with $.error.message
SELECT dbms_vector_utl_to_generate_text('hi'::clob, '{"provider":"openai","url":"http://127.0.0.1:18180/v1/bad_request","model":"gpt-test","credential_name":"openai_gen"}');
ERROR:  The provider returned an error: Invalid request: unknown model
DETAIL:  Oracle error: ORA-20002 (EXCP_PROVIDER_ERR).
-- HTTP 500 with a non-JSON body: the raw body is the message
SELECT dbms_vector_utl_to_generate_text('hi'::clob, '{"provider":"openai","url":"http://127.0.0.1:18180/v1/server_error","model":"gpt-test","credential_name":"openai_gen"}');
ERROR:  The provider returned an error: upstream exploded
DETAIL:  Oracle error: ORA-20002 (EXCP_PROVIDER_ERR).
-- 200 without choices: $.message extraction
SELECT dbms_vector_utl_to_generate_text('hi'::clob, '{"provider":"openai","url":"http://127.0.0.1:18180/v1/message_error","model":"gpt-test","credential_name":"openai_gen"}');
ERROR:  The provider returned an error: model overloaded, retry later
DETAIL:  Oracle error: ORA-20002 (EXCP_PROVIDER_ERR).
-- transfer_timeout applies to the whole request (fixture endpoint sleeps 3 s)
SELECT dbms_vector_utl_to_generate_text('hi'::clob, '{"provider":"openai","url":"http://127.0.0.1:18180/v1/slow","model":"gpt-test","credential_name":"openai_gen","transfer_timeout":1}');
ERROR:  provider request failed: Timeout was reached
CALL dbms_vector_drop_credential('openai_gen');
CALL dbms_vector_drop_credential('wrongkey_gen');
-- ===========================================================================
-- CREATE_INDEX (generated DDL over the shared index mapping)
CREATE TABLE dbms_vector_create_index_tab (id int, v vector(3), payload text);
INSERT INTO dbms_vector_create_index_tab VALUES (1, '[1,2,3]', 'one'), (2, '[3,2,1]', 'two'), (3, '[0,0,1]', 'three');
-- hnsw with reloptions; the index name is uppercased (Oracle parity)
CALL dbms_vector_create_index('vec_hnsw_idx', 'dbms_vector_create_index_tab', 'v', NULL, 'LOCAL', 'INMEMORY NEIGHBOR GRAPH', 'EUCLIDEAN', 95, '{"type":"HNSW","neighbors":8,"efConstruction":48}'::jsonb, 4, NULL);
SELECT a.amname, oc.opcname, c.reloptions FROM pg_class c JOIN pg_am a ON a.oid = c.relam JOIN pg_index i ON i.indexrelid = c.oid JOIN pg_opclass oc ON oc.oid = i.indclass[0] WHERE c.relname = 'vec_hnsw_idx';
 amname |    opcname    |        reloptions        
--------+---------------+--------------------------
 hnsw   | vector_l2_ops | {m=8,ef_construction=48}
(1 row)
 
-- ivfflat with lists; NULL partitioning is the LOCAL default; COSINE maps to vector_cosine_ops
CALL dbms_vector_create_index('vec_ivf_idx', 'dbms_vector_create_index_tab', 'v', NULL, NULL, 'NEIGHBOR PARTITIONS', 'COSINE', 95, '{"type":"IVF","partitions":5}'::jsonb, 4, NULL);
NOTICE:  ivfflat index created with little data
DETAIL:  This will cause low recall.
HINT:  Drop the index until the table has more data.
SELECT a.amname, oc.opcname, c.reloptions FROM pg_class c JOIN pg_am a ON a.oid = c.relam JOIN pg_index i ON i.indexrelid = c.oid JOIN pg_opclass oc ON oc.oid = i.indclass[0] WHERE c.relname = 'vec_ivf_idx';
 amname  |      opcname      | reloptions 
---------+-------------------+------------
 ivfflat | vector_cosine_ops | {lists=5}
(1 row)
 
-- accuracy is accepted and logged only; DOT maps to vector_ip_ops
CALL dbms_vector_create_index('vec_acc_idx', 'dbms_vector_create_index_tab', 'v', NULL, 'LOCAL', 'INMEMORY NEIGHBOR GRAPH', 'DOT', 80, '{"type":"HNSW","neighbors":8}'::jsonb, 4, NULL);
SELECT a.amname, oc.opcname, c.reloptions FROM pg_class c JOIN pg_am a ON a.oid = c.relam JOIN pg_index i ON i.indexrelid = c.oid JOIN pg_opclass oc ON oc.oid = i.indclass[0] WHERE c.relname = 'vec_acc_idx';
 amname |    opcname    | reloptions 
--------+---------------+------------
 hnsw   | vector_ip_ops | {m=8}
(1 row)
 
-- parameter validation (Oracle parity messages)
CALL dbms_vector_create_index('bad_org_idx', 'dbms_vector_create_index_tab', 'v', NULL, 'LOCAL', 'SPATIAL', 'EUCLIDEAN', 95, '{"type":"HNSW"}'::jsonb, 4, NULL);
ERROR:  Organization - SPATIAL - is not a valid option
DETAIL:  Oracle error: ORA-20003 (EXCP_GU_ERR).
CALL dbms_vector_create_index('bad_part_idx', 'dbms_vector_create_index_tab', 'v', NULL, 'GLOBAL', 'INMEMORY NEIGHBOR GRAPH', 'EUCLIDEAN', 95, '{"type":"HNSW"}'::jsonb, 4, NULL);
ERROR:  Partitioning scheme - GLOBAL - is not a valid option
DETAIL:  RuDB has no Oracle index partitioning; only LOCAL (the default) is supported.
CALL dbms_vector_create_index('bad_metric_idx', 'dbms_vector_create_index_tab', 'v', NULL, 'LOCAL', 'INMEMORY NEIGHBOR GRAPH', 'CHEBYSHEV', 95, '{"type":"HNSW"}'::jsonb, 4, NULL);
ERROR:  Distance metric - CHEBYSHEV - is not a valid option
DETAIL:  Oracle error: ORA-20003 (EXCP_GU_ERR).
CALL dbms_vector_create_index('hamming_idx', 'dbms_vector_create_index_tab', 'v', NULL, 'LOCAL', 'INMEMORY NEIGHBOR GRAPH', 'HAMMING', 95, '{"type":"HNSW"}'::jsonb, 4, NULL);
ERROR:  Distance metric - HAMMING - is supported only for bit vectors
HINT:  Bit vector distances are outside the scope of this port.
CALL dbms_vector_create_index('bad_type_idx', 'dbms_vector_create_index_tab', 'v', NULL, 'LOCAL', 'INMEMORY NEIGHBOR GRAPH', 'EUCLIDEAN', 95, '{"type":"XYZ"}'::jsonb, 4, NULL);
ERROR:  Index type - XYZ - is not a valid option
DETAIL:  Oracle error: ORA-20003 (EXCP_GU_ERR).
CALL dbms_vector_create_index('mismatch_idx', 'dbms_vector_create_index_tab', 'v', NULL, 'LOCAL', 'INMEMORY NEIGHBOR GRAPH', 'EUCLIDEAN', 95, '{"type":"IVF","partitions":5}'::jsonb, 4, NULL);
ERROR:  Index type - IVF - does not match the organization INMEMORY NEIGHBOR GRAPH
DETAIL:  Oracle error: ORA-20003 (EXCP_GU_ERR).
CALL dbms_vector_create_index('bad_neighbors_idx', 'dbms_vector_create_index_tab', 'v', NULL, 'LOCAL', 'INMEMORY NEIGHBOR GRAPH', 'EUCLIDEAN', 95, '{"type":"HNSW","neighbors":"eight"}'::jsonb, 4, NULL);
ERROR:  invalid value for NEIGHBORS parameter
DETAIL:  Oracle error: ORA-20003 (EXCP_GU_ERR).
CALL dbms_vector_create_index('bad_params_idx', 'dbms_vector_create_index_tab', 'v', NULL, 'LOCAL', 'INMEMORY NEIGHBOR GRAPH', 'EUCLIDEAN', 95, '["not", "an", "object"]'::jsonb, 4, NULL);
ERROR:  idx_parameters must be a JSON object
DETAIL:  Oracle error: ORA-20003 (EXCP_GU_ERR).
-- required parameters are enforced in the C body (the SQL defaults hide them)
CALL dbms_vector_create_index('bad_null_org', 'dbms_vector_create_index_tab', 'v', NULL, 'LOCAL', NULL, 'EUCLIDEAN', 95, '{"type":"HNSW"}'::jsonb, 4, NULL);
ERROR:  idx_organization must be specified
DETAIL:  Oracle error: ORA-20003 (EXCP_GU_ERR).
CALL dbms_vector_create_index('bad_null_params', 'dbms_vector_create_index_tab', 'v', NULL, 'LOCAL', 'INMEMORY NEIGHBOR GRAPH', 'EUCLIDEAN', 95, NULL::jsonb, 4, NULL);
ERROR:  idx_parameters must be specified
DETAIL:  Oracle error: ORA-20003 (EXCP_GU_ERR).
-- identifiers are simple SQL names (DBMS_ASSERT.SIMPLE_SQL_NAME parity)
CALL dbms_vector_create_index('bad', 'public.dbms_vector_create_index_tab', 'v', NULL, 'LOCAL', 'INMEMORY NEIGHBOR GRAPH', 'EUCLIDEAN', 95, '{"type":"HNSW"}'::jsonb, 4, NULL);
ERROR:  invalid table name: public.dbms_vector_create_index_tab
CALL dbms_vector_create_index('bad', 'dbms_vector_create_index_tab', 'bad-name', NULL, 'LOCAL', 'INMEMORY NEIGHBOR GRAPH', 'EUCLIDEAN', 95, '{"type":"HNSW"}'::jsonb, 4, NULL);
ERROR:  invalid vector column name: bad-name
-- generated DDL failures surface the engine error when there is no fallback
CALL dbms_vector_create_index('bad', 'dbms_vector_no_tab', 'v', NULL, 'LOCAL', 'INMEMORY NEIGHBOR GRAPH', 'EUCLIDEAN', 95, '{"type":"HNSW"}'::jsonb, 4, NULL);
ERROR:  relation "dbms_vector_no_tab" does not exist
CONTEXT:  SQL statement "CREATE INDEX BAD ON dbms_vector_no_tab USING hnsw (v vector_l2_ops)"
CALL dbms_vector_create_index('bad', 'dbms_vector_create_index_tab', 'nope', NULL, 'LOCAL', 'INMEMORY NEIGHBOR GRAPH', 'EUCLIDEAN', 95, '{"type":"HNSW"}'::jsonb, 4, NULL);
ERROR:  column "nope" does not exist
LINE 1: ...X BAD ON dbms_vector_create_index_tab USING hnsw (nope vecto...
                                                             ^
QUERY:  CREATE INDEX BAD ON dbms_vector_create_index_tab USING hnsw (nope vector_l2_ops)
-- INCLUDE is not supported by the vector access methods; without a
-- fallback the engine error surfaces
CALL dbms_vector_create_index('vec_inc_idx', 'dbms_vector_create_index_tab', 'v', 'payload', 'LOCAL', 'INMEMORY NEIGHBOR GRAPH', 'EUCLIDEAN', 95, '{"type":"HNSW"}'::jsonb, 4, NULL);
ERROR:  access method "hnsw" does not support included columns
CONTEXT:  SQL statement "CREATE INDEX VEC_INC_IDX ON dbms_vector_create_index_tab USING hnsw (v vector_l2_ops) INCLUDE (payload)"
-- with idx_ddl the escape hatch takes over (Oracle pls:659-666)
CALL dbms_vector_create_index('vec_inc_idx', 'dbms_vector_create_index_tab', 'nope', 'payload', 'LOCAL', 'INMEMORY NEIGHBOR GRAPH', 'EUCLIDEAN', 95, '{"type":"HNSW"}'::jsonb, 4, 'CREATE INDEX vec_inc_fallback_idx ON dbms_vector_create_index_tab USING hnsw (v vector_l2_ops)');
SELECT count(*) AS fallback_created FROM pg_class WHERE relname = 'vec_inc_fallback_idx';
 fallback_created 
------------------
                1
(1 row)
 
DROP INDEX vec_hnsw_idx;
DROP INDEX vec_ivf_idx;
DROP INDEX vec_acc_idx;
DROP INDEX vec_inc_fallback_idx;
DROP TABLE dbms_vector_create_index_tab;
-- ===========================================================================
-- REBUILD_INDEX (three-branch parity)
CREATE TABLE dbms_vector_rebuild_tab (id int, v vector(3));
INSERT INTO dbms_vector_rebuild_tab VALUES (1, '[1,2,3]'), (2, '[3,2,1]');
CALL dbms_vector_create_index('vec_rebuild_idx', 'dbms_vector_rebuild_tab', 'v', NULL, 'LOCAL', 'INMEMORY NEIGHBOR GRAPH', 'EUCLIDEAN', 95, '{"type":"HNSW","neighbors":8,"efConstruction":48}'::jsonb, 4, NULL);
-- branch (c): no overrides re-executes the stored DDL verbatim
CALL dbms_vector_rebuild_index('vec_rebuild_idx');
SELECT a.amname, oc.opcname, c.reloptions, i.indisvalid FROM pg_class c JOIN pg_am a ON a.oid = c.relam JOIN pg_index i ON i.indexrelid = c.oid JOIN pg_opclass oc ON oc.oid = i.indclass[0] WHERE c.relname = 'vec_rebuild_idx';
 amname |    opcname    |        reloptions        | indisvalid 
--------+---------------+--------------------------+------------
 hnsw   | vector_l2_ops | {m=8,ef_construction=48} | t
(1 row)
 
-- branch (b) parameters override: patched reloptions are visible
CALL dbms_vector_rebuild_index('vec_rebuild_idx', NULL, NULL, NULL, 'LOCAL', NULL, 'EUCLIDEAN', 95, '{"type":"HNSW","neighbors":16,"efConstruction":64}'::jsonb, 4);
SELECT a.amname, oc.opcname, c.reloptions, i.indisvalid FROM pg_class c JOIN pg_am a ON a.oid = c.relam JOIN pg_index i ON i.indexrelid = c.oid JOIN pg_opclass oc ON oc.oid = i.indclass[0] WHERE c.relname = 'vec_rebuild_idx';
 amname |    opcname    |        reloptions         | indisvalid 
--------+---------------+---------------------------+------------
 hnsw   | vector_l2_ops | {m=16,ef_construction=64} | t
(1 row)
 
-- branch (b) metric override rewrites the operator class, keeping the keys
CALL dbms_vector_rebuild_index('vec_rebuild_idx', NULL, NULL, NULL, 'LOCAL', NULL, 'COSINE', 95, NULL::jsonb, 4);
SELECT a.amname, oc.opcname, c.reloptions, i.indisvalid FROM pg_class c JOIN pg_am a ON a.oid = c.relam JOIN pg_index i ON i.indexrelid = c.oid JOIN pg_opclass oc ON oc.oid = i.indclass[0] WHERE c.relname = 'vec_rebuild_idx';
 amname |      opcname      |        reloptions         | indisvalid 
--------+-------------------+---------------------------+------------
 hnsw   | vector_cosine_ops | {m=16,ef_construction=64} | t
(1 row)
 
-- branch (b) organization override switches the engine (hnsw -> ivfflat)
CALL dbms_vector_rebuild_index('vec_rebuild_idx', NULL, NULL, NULL, 'LOCAL', 'NEIGHBOR PARTITIONS', 'EUCLIDEAN', 95, '{"type":"IVF","partitions":4}'::jsonb, 4);
NOTICE:  ivfflat index created with little data
DETAIL:  This will cause low recall.
HINT:  Drop the index until the table has more data.
SELECT a.amname, oc.opcname, c.reloptions, i.indisvalid FROM pg_class c JOIN pg_am a ON a.oid = c.relam JOIN pg_index i ON i.indexrelid = c.oid JOIN pg_opclass oc ON oc.oid = i.indclass[0] WHERE c.relname = 'vec_rebuild_idx';
 amname  |      opcname      | reloptions | indisvalid 
---------+-------------------+------------+------------
 ivfflat | vector_cosine_ops | {lists=4}  | t
(1 row)
 
-- branch (b) parallel_creation alone still triggers the rebuild
CALL dbms_vector_rebuild_index('vec_rebuild_idx', NULL, NULL, NULL, 'LOCAL', NULL, 'EUCLIDEAN', 95, NULL::jsonb, 8);
NOTICE:  ivfflat index created with little data
DETAIL:  This will cause low recall.
HINT:  Drop the index until the table has more data.
SELECT a.amname, oc.opcname, c.reloptions, i.indisvalid FROM pg_class c JOIN pg_am a ON a.oid = c.relam JOIN pg_index i ON i.indexrelid = c.oid JOIN pg_opclass oc ON oc.oid = i.indclass[0] WHERE c.relname = 'vec_rebuild_idx';
 amname  |      opcname      | reloptions | indisvalid 
---------+-------------------+------------+------------
 ivfflat | vector_cosine_ops | {lists=4}  | t
(1 row)
 
-- branch (a): full parameters drop + re-run the CREATE_INDEX core
CALL dbms_vector_rebuild_index('vec_rebuild_idx', 'dbms_vector_rebuild_tab', 'v', NULL, 'LOCAL', 'INMEMORY NEIGHBOR GRAPH', 'DOT', 90, '{"type":"HNSW","neighbors":12}'::jsonb, 4);
SELECT a.amname, oc.opcname, c.reloptions, i.indisvalid FROM pg_class c JOIN pg_am a ON a.oid = c.relam JOIN pg_index i ON i.indexrelid = c.oid JOIN pg_opclass oc ON oc.oid = i.indclass[0] WHERE c.relname = 'vec_rebuild_idx';
 amname |    opcname    | reloptions | indisvalid 
--------+---------------+------------+------------
 hnsw   | vector_ip_ops | {m=12}     | t
(1 row)
 
-- branch (a) with a failing generated DDL: the stored-DDL fallback of the
-- CREATE_INDEX core recreates the previous state
CALL dbms_vector_rebuild_index('vec_rebuild_idx', 'dbms_vector_rebuild_tab', 'nope', NULL, 'LOCAL', 'INMEMORY NEIGHBOR GRAPH', 'DOT', 95, '{"type":"HNSW","neighbors":12}'::jsonb, 4);
SELECT a.amname, oc.opcname, c.reloptions, i.indisvalid FROM pg_class c JOIN pg_am a ON a.oid = c.relam JOIN pg_index i ON i.indexrelid = c.oid JOIN pg_opclass oc ON oc.oid = i.indclass[0] WHERE c.relname = 'vec_rebuild_idx';
 amname |    opcname    | reloptions | indisvalid 
--------+---------------+------------+------------
 hnsw   | vector_ip_ops | {m=12}     | t
(1 row)
 
-- nonexistent index
CALL dbms_vector_rebuild_index('no_such_rebuild_idx');
ERROR:  index NO_SUCH_REBUILD_IDX does not exist
DETAIL:  Oracle error: ORA-20003 (EXCP_GU_ERR).
-- non-vector index
CREATE INDEX vec_rebuild_btree ON dbms_vector_rebuild_tab (id);
CALL dbms_vector_rebuild_index('vec_rebuild_btree');
ERROR:  index VEC_REBUILD_BTREE is not a vector index
DETAIL:  Oracle error: ORA-20003 (EXCP_GU_ERR).
-- validation parity on the overrides (nothing is dropped on failure)
CALL dbms_vector_rebuild_index('vec_rebuild_idx', NULL, NULL, NULL, 'LOCAL', 'NEIGHBOR PARTITIONS', 'HAMMING', 95, '{"type":"IVF","partitions":4}'::jsonb, 4);
ERROR:  Distance metric - HAMMING - is supported only for bit vectors
HINT:  Bit vector distances are outside the scope of this port.
SELECT i.indisvalid FROM pg_index i JOIN pg_class c ON c.oid = i.indexrelid WHERE c.relname = 'vec_rebuild_idx';
 indisvalid 
------------
 t
(1 row)
 
DROP INDEX vec_rebuild_idx;
DROP INDEX vec_rebuild_btree;
DROP TABLE dbms_vector_rebuild_tab;
-- ===========================================================================
-- GET_INDEX_STATUS (state derivation + lookup errors)
CREATE TABLE dbms_vector_status_tab (id int, v vector(3));
INSERT INTO dbms_vector_status_tab VALUES (1, '[1,2,3]'), (2, '[3,2,1]');
CALL dbms_vector_create_index('vec_status_idx', 'dbms_vector_status_tab', 'v', NULL, 'LOCAL', 'INMEMORY NEIGHBOR GRAPH', 'EUCLIDEAN', 95, '{"type":"HNSW","neighbors":8}'::jsonb, 4, NULL);
-- valid index reports COMPLETED; the name folds case-insensitively
SELECT dbms_vector_get_index_status('vec_status_idx');
                     dbms_vector_get_index_status                     
----------------------------------------------------------------------
 {"am": "hnsw", "state": "COMPLETED", "index_name": "vec_status_idx"}
(1 row)
 
SELECT dbms_vector_get_index_status('VEC_STATUS_IDX');
                     dbms_vector_get_index_status                     
----------------------------------------------------------------------
 {"am": "hnsw", "state": "COMPLETED", "index_name": "vec_status_idx"}
(1 row)
 
-- !indisvalid reports FAILED (direct catalog poke as superuser)
UPDATE pg_index SET indisvalid = false WHERE indexrelid = 'vec_status_idx'::regclass;
SELECT dbms_vector_get_index_status('vec_status_idx');
                   dbms_vector_get_index_status                    
-------------------------------------------------------------------
 {"am": "hnsw", "state": "FAILED", "index_name": "vec_status_idx"}
(1 row)
 
UPDATE pg_index SET indisvalid = true WHERE indexrelid = 'vec_status_idx'::regclass;
-- missing index / non-vector index / NULL name
SELECT dbms_vector_get_index_status('no_such_status_idx');
ERROR:  index NO_SUCH_STATUS_IDX does not exist
DETAIL:  Oracle error: ORA-20003 (EXCP_GU_ERR).
CREATE INDEX vec_status_btree ON dbms_vector_status_tab (id);
SELECT dbms_vector_get_index_status('vec_status_btree');
ERROR:  index VEC_STATUS_BTREE is not a vector index
DETAIL:  Oracle error: ORA-20003 (EXCP_GU_ERR).
SELECT dbms_vector_get_index_status(NULL);
ERROR:  index name must be specified
DROP INDEX vec_status_idx;
DROP INDEX vec_status_btree;
DROP TABLE dbms_vector_status_tab;
-- ===========================================================================
-- INDEX_VECTOR_MEMORY_ADVISOR (estimate formulas; before-create mode)
CREATE TABLE dbms_vector_adv_tab (id int, v vector(3));
INSERT INTO dbms_vector_adv_tab VALUES (1, '[1,2,3]'), (2, '[3,2,1]');
ANALYZE dbms_vector_adv_tab;
-- hnsw m=8 over dim 3: per row 3*8 + 8*24 = 216 bytes; 2 rows
CALL dbms_vector_create_index('vec_adv_idx', 'dbms_vector_adv_tab', 'v', NULL, 'LOCAL', 'INMEMORY NEIGHBOR GRAPH', 'EUCLIDEAN', 95, '{"type":"HNSW","neighbors":8}'::jsonb, 4, NULL);
SELECT dbms_vector_index_vector_memory_advisor('vec_adv_idx', NULL);
               dbms_vector_index_vector_memory_advisor               
---------------------------------------------------------------------
 {"bytes_per_row": 216, "rows_estimated": 2, "estimated_bytes": 432}
(1 row)
 
-- the estimate is monotonic in m (neighbors 16 doubles the link layer)
CALL dbms_vector_create_index('vec_adv2_idx', 'dbms_vector_adv_tab', 'v', NULL, 'LOCAL', 'INMEMORY NEIGHBOR GRAPH', 'EUCLIDEAN', 95, '{"type":"HNSW","neighbors":16}'::jsonb, 4, NULL);
SELECT dbms_vector_index_vector_memory_advisor('vec_adv2_idx', NULL);
               dbms_vector_index_vector_memory_advisor               
---------------------------------------------------------------------
 {"bytes_per_row": 408, "rows_estimated": 2, "estimated_bytes": 816}
(1 row)
 
-- ivfflat lists=4: vectors (12/row) plus 4 centroids of 12 bytes
CALL dbms_vector_create_index('vec_adv_ivf_idx', 'dbms_vector_adv_tab', 'v', NULL, 'LOCAL', 'NEIGHBOR PARTITIONS', 'EUCLIDEAN', 95, '{"type":"IVF","partitions":4}'::jsonb, 4, NULL);
NOTICE:  ivfflat index created with little data
DETAIL:  This will cause low recall.
HINT:  Drop the index until the table has more data.
SELECT dbms_vector_index_vector_memory_advisor('vec_adv_ivf_idx', NULL);
              dbms_vector_index_vector_memory_advisor              
-------------------------------------------------------------------
 {"bytes_per_row": 12, "rows_estimated": 2, "estimated_bytes": 72}
(1 row)
 
-- before-create mode: table without an index, hnsw with the default m=16
CREATE TABLE dbms_vector_adv2_tab (id int, v vector(3));
INSERT INTO dbms_vector_adv2_tab VALUES (1, '[1,1,1]'), (2, '[2,2,2]'), (3, '[3,3,3]');
ANALYZE dbms_vector_adv2_tab;
SELECT dbms_vector_index_vector_memory_advisor(NULL, 'dbms_vector_adv2_tab');
               dbms_vector_index_vector_memory_advisor                
----------------------------------------------------------------------
 {"bytes_per_row": 408, "rows_estimated": 3, "estimated_bytes": 1224}
(1 row)
 
-- an unresolved idx_name with a table is also the before-create mode
SELECT dbms_vector_index_vector_memory_advisor('never_created_idx', 'dbms_vector_adv2_tab');
               dbms_vector_index_vector_memory_advisor                
----------------------------------------------------------------------
 {"bytes_per_row": 408, "rows_estimated": 3, "estimated_bytes": 1224}
(1 row)
 
-- error paths
SELECT dbms_vector_index_vector_memory_advisor('never_created_idx', NULL);
ERROR:  index NEVER_CREATED_IDX does not exist
DETAIL:  Oracle error: ORA-20003 (EXCP_GU_ERR).
CREATE TABLE dbms_vector_adv_plain (id int, payload text);
SELECT dbms_vector_index_vector_memory_advisor(NULL, 'dbms_vector_adv_plain');
ERROR:  table dbms_vector_adv_plain has no vector column
DETAIL:  Oracle error: ORA-20003 (EXCP_GU_ERR).
SELECT dbms_vector_index_vector_memory_advisor(NULL, 'dbms_vector_no_tab');
ERROR:  table dbms_vector_no_tab does not exist
DETAIL:  Oracle error: ORA-20003 (EXCP_GU_ERR).
SELECT dbms_vector_index_vector_memory_advisor(NULL, NULL);
ERROR:  idx_name or table_name must be specified
DETAIL:  Oracle error: ORA-20003 (EXCP_GU_ERR).
DROP INDEX vec_adv_idx;
DROP INDEX vec_adv2_idx;
DROP INDEX vec_adv_ivf_idx;
DROP TABLE dbms_vector_adv_tab;
DROP TABLE dbms_vector_adv2_tab;
DROP TABLE dbms_vector_adv_plain;
-- ===========================================================================
-- ENABLE/DISABLE_CHECKPOINT (stage 1: persisted state, no AM behavior)
CREATE TABLE dbms_vector_ckpt_tab (id int, v vector(3));
INSERT INTO dbms_vector_ckpt_tab VALUES (1, '[1,2,3]'), (2, '[3,2,1]');
CALL dbms_vector_create_index('vec_ckpt_idx', 'dbms_vector_ckpt_tab', 'v', NULL, 'LOCAL', 'INMEMORY NEIGHBOR GRAPH', 'EUCLIDEAN', 95, '{"type":"HNSW","neighbors":8}'::jsonb, 4, NULL);
-- per-index disable: SYS row plus the reloption mirror
CALL dbms_vector_disable_checkpoint('vec_ckpt_idx');
SELECT idx_schema, idx_name, enabled FROM "SYS".vector_index_checkpointing WHERE idx_name = 'vec_ckpt_idx';
 idx_schema |   idx_name   | enabled 
------------+--------------+---------
 public     | vec_ckpt_idx | f
(1 row)
 
SELECT reloptions FROM pg_class WHERE relname = 'vec_ckpt_idx';
       reloptions        
-------------------------
 {m=8,checkpointing=off}
(1 row)
 
-- enable back
CALL dbms_vector_enable_checkpoint('vec_ckpt_idx');
SELECT idx_schema, idx_name, enabled FROM "SYS".vector_index_checkpointing WHERE idx_name = 'vec_ckpt_idx';
 idx_schema |   idx_name   | enabled 
------------+--------------+---------
 public     | vec_ckpt_idx | t
(1 row)
 
SELECT reloptions FROM pg_class WHERE relname = 'vec_ckpt_idx';
       reloptions       
------------------------
 {m=8,checkpointing=on}
(1 row)
 
-- an invalid index must not crash the toggle
UPDATE pg_index SET indisvalid = false WHERE indexrelid = 'vec_ckpt_idx'::regclass;
CALL dbms_vector_disable_checkpoint('vec_ckpt_idx');
SELECT idx_name, enabled FROM "SYS".vector_index_checkpointing WHERE idx_name = 'vec_ckpt_idx';
   idx_name   | enabled 
--------------+---------
 vec_ckpt_idx | f
(1 row)
 
UPDATE pg_index SET indisvalid = true WHERE indexrelid = 'vec_ckpt_idx'::regclass;
CALL dbms_vector_enable_checkpoint('vec_ckpt_idx');
-- cluster-wide toggle: the sentinel row
CALL dbms_vector_disable_checkpoint(NULL);
SELECT idx_schema, idx_name, enabled FROM "SYS".vector_index_checkpointing WHERE idx_schema = '*';
 idx_schema | idx_name | enabled 
------------+----------+---------
 *          | *        | f
(1 row)
 
CALL dbms_vector_enable_checkpoint(NULL);
SELECT idx_schema, idx_name, enabled FROM "SYS".vector_index_checkpointing WHERE idx_schema = '*';
 idx_schema | idx_name | enabled 
------------+----------+---------
 *          | *        | t
(1 row)
 
-- unknown index
CALL dbms_vector_enable_checkpoint('no_such_ckpt_idx');
ERROR:  index NO_SUCH_CKPT_IDX does not exist
DETAIL:  Oracle error: ORA-20003 (EXCP_GU_ERR).
-- permission checks: a non-owner non-superuser is rejected
CREATE ROLE dbms_vector_ckpt_stranger;
SET ROLE dbms_vector_ckpt_stranger;
CALL dbms_vector_enable_checkpoint('vec_ckpt_idx');
ERROR:  permission denied for index VEC_CKPT_IDX
HINT:  Only the index owner or a superuser may toggle its checkpointing state.
CALL dbms_vector_disable_checkpoint(NULL);
ERROR:  permission denied to change the cluster-wide checkpointing state
DETAIL:  Only superusers may toggle the cluster-wide state.
RESET ROLE;
-- the build still completes with checkpointing disabled (stage 1 is a
-- behavioral no-op beyond state)
CALL dbms_vector_disable_checkpoint('vec_ckpt_idx');
INSERT INTO dbms_vector_ckpt_tab VALUES (3, '[0,1,0]');
DROP INDEX vec_ckpt_idx;
CALL dbms_vector_create_index('vec_ckpt_idx', 'dbms_vector_ckpt_tab', 'v', NULL, 'LOCAL', 'INMEMORY NEIGHBOR GRAPH', 'EUCLIDEAN', 95, '{"type":"HNSW","neighbors":8}'::jsonb, 4, NULL);
SELECT i.indisvalid FROM pg_index i JOIN pg_class c ON c.oid = i.indexrelid WHERE c.relname = 'vec_ckpt_idx';
 indisvalid 
------------
 t
(1 row)
 
DROP INDEX vec_ckpt_idx;
DROP TABLE dbms_vector_ckpt_tab;
DELETE FROM "SYS".vector_index_checkpointing;
DROP ROLE dbms_vector_ckpt_stranger;
-- ===========================================================================
-- INMEMORY_ONNX_MODEL (per-backend ONNX session cache pin; flavor-gated
-- outcomes like the LOAD/DROP section above)
-- missing model: flavor-independent (the catalog lookup raises first)
CALL dbms_vector_inmemory_onnx_model('never_loaded_pin_model');
ERROR:  model never_loaded_pin_model does not exist
DETAIL:  Oracle error: ORA-20003 (EXCP_GU_ERR).
CALL dbms_vector_inmemory_onnx_model(NULL);
ERROR:  model name must be specified
DETAIL:  Oracle error: ORA-20003 (EXCP_GU_ERR).
CREATE TEMP TABLE dbms_vector_pin_outcomes(op text, outcome text);
CREATE TEMP TABLE dbms_vector_pin_expected(op text, want text);
INSERT INTO dbms_vector_pin_expected VALUES
    ('load_pin_model',       CASE WHEN :'onnx'::int = 1 THEN 'rejected' ELSE 'loaded' END),
    ('pin_pin_model',        'rejected'),
    ('pin_pin_model_params', 'rejected'),
    ('stranger_pin_model',   'rejected'),
    ('cleanup_drop_model',   CASE WHEN :'onnx'::int = 1 THEN 'rejected' ELSE 'dropped' END);
DO $$
BEGIN
    CALL dbms_vector_load_onnx_model('pin_model', '\x0806'::bytea, NULL);
    INSERT INTO dbms_vector_pin_outcomes VALUES ('load_pin_model', 'loaded');
EXCEPTION WHEN OTHERS THEN
    INSERT INTO dbms_vector_pin_outcomes VALUES ('load_pin_model', 'rejected');
END $$;
-- pin: rejected on both flavors (no runtime vs no stored model)
DO $$
BEGIN
    CALL dbms_vector_inmemory_onnx_model('pin_model');
    INSERT INTO dbms_vector_pin_outcomes VALUES ('pin_pin_model', 'pinned');
EXCEPTION WHEN OTHERS THEN
    INSERT INTO dbms_vector_pin_outcomes VALUES ('pin_pin_model', 'rejected');
END $$;
-- the params argument is accepted and ignored (same outcome)
DO $$
BEGIN
    CALL dbms_vector_inmemory_onnx_model('pin_model', '{"anything": 1}'::jsonb);
    INSERT INTO dbms_vector_pin_outcomes VALUES ('pin_pin_model_params', 'pinned');
EXCEPTION WHEN OTHERS THEN
    INSERT INTO dbms_vector_pin_outcomes VALUES ('pin_pin_model_params', 'rejected');
END $$;
-- a non-owner is rejected by the catalog lookup on both flavors
CREATE ROLE dbms_vector_pin_stranger;
GRANT INSERT, SELECT ON dbms_vector_pin_outcomes TO dbms_vector_pin_stranger;
SET ROLE dbms_vector_pin_stranger;
DO $$
BEGIN
    CALL dbms_vector_inmemory_onnx_model('pin_model');
    INSERT INTO dbms_vector_pin_outcomes VALUES ('stranger_pin_model', 'pinned');
EXCEPTION WHEN OTHERS THEN
    INSERT INTO dbms_vector_pin_outcomes VALUES ('stranger_pin_model', 'rejected');
END $$;
RESET ROLE;
REVOKE INSERT, SELECT ON dbms_vector_pin_outcomes FROM dbms_vector_pin_stranger;
DROP ROLE dbms_vector_pin_stranger;
DO $$
BEGIN
    CALL dbms_vector_drop_onnx_model('pin_model', false);
    INSERT INTO dbms_vector_pin_outcomes VALUES ('cleanup_drop_model', 'dropped');
EXCEPTION WHEN OTHERS THEN
    INSERT INTO dbms_vector_pin_outcomes VALUES ('cleanup_drop_model', 'rejected');
END $$;
SELECT bool_and(o.outcome = e.want) AS pin_matrix_ok
  FROM dbms_vector_pin_outcomes o
  JOIN dbms_vector_pin_expected e USING (op);
 pin_matrix_ok 
---------------
 t
(1 row)
 
-- ===========================================================================
-- Index lifecycle: create -> status -> advisor -> rebuild -> checkpoint
CREATE TABLE dbms_vector_lifecycle_tab (id int, v vector(3), payload text);
INSERT INTO dbms_vector_lifecycle_tab VALUES
    (1, '[1,2,3]', 'one'), (2, '[3,2,1]', 'two'), (3, '[0,0,1]', 'three');
ANALYZE dbms_vector_lifecycle_tab;
CALL dbms_vector_create_index('vec_lifecycle_idx', 'dbms_vector_lifecycle_tab', 'v', NULL, 'LOCAL', 'INMEMORY NEIGHBOR GRAPH', 'EUCLIDEAN', 95, '{"type":"HNSW","neighbors":8,"efConstruction":48}'::jsonb, 4, NULL);
SELECT a.amname, oc.opcname, c.reloptions,
       (dbms_vector_get_index_status('vec_lifecycle_idx') ->> 'state') AS state,
       (dbms_vector_index_vector_memory_advisor('vec_lifecycle_idx') ->> 'estimated_bytes') AS est_bytes
  FROM pg_class c
  JOIN pg_am a ON a.oid = c.relam
  JOIN pg_index i ON i.indexrelid = c.oid
  JOIN pg_opclass oc ON oc.oid = i.indclass[0]
 WHERE c.relname = 'vec_lifecycle_idx';
 amname |    opcname    |        reloptions        |   state   | est_bytes 
--------+---------------+--------------------------+-----------+-----------
 hnsw   | vector_l2_ops | {m=8,ef_construction=48} | COMPLETED | 648
(1 row)
 
CALL dbms_vector_rebuild_index('vec_lifecycle_idx', NULL, NULL, NULL, 'LOCAL', 'NEIGHBOR PARTITIONS', 'EUCLIDEAN', 95, '{"type":"IVF","partitions":4}'::jsonb, 4);
NOTICE:  ivfflat index created with little data
DETAIL:  This will cause low recall.
HINT:  Drop the index until the table has more data.
SELECT a.amname, oc.opcname, c.reloptions,
       (dbms_vector_get_index_status('vec_lifecycle_idx') ->> 'state') AS state,
       (dbms_vector_index_vector_memory_advisor('vec_lifecycle_idx') ->> 'estimated_bytes') AS est_bytes
  FROM pg_class c
  JOIN pg_am a ON a.oid = c.relam
  JOIN pg_index i ON i.indexrelid = c.oid
  JOIN pg_opclass oc ON oc.oid = i.indclass[0]
 WHERE c.relname = 'vec_lifecycle_idx';
 amname  |    opcname    | reloptions |   state   | est_bytes 
---------+---------------+------------+-----------+-----------
 ivfflat | vector_l2_ops | {lists=4}  | COMPLETED | 84
(1 row)
 
CALL dbms_vector_disable_checkpoint('vec_lifecycle_idx');
CALL dbms_vector_rebuild_index('vec_lifecycle_idx');
NOTICE:  ivfflat index created with little data
DETAIL:  This will cause low recall.
HINT:  Drop the index until the table has more data.
SELECT (dbms_vector_get_index_status('vec_lifecycle_idx') ->> 'state') AS state,
       c.reloptions
  FROM pg_class c
 WHERE c.relname = 'vec_lifecycle_idx';
   state   |         reloptions          
-----------+-----------------------------
 COMPLETED | {lists=4,checkpointing=off}
(1 row)
 
CALL dbms_vector_enable_checkpoint('vec_lifecycle_idx');
SELECT enabled FROM "SYS".vector_index_checkpointing WHERE idx_name = 'vec_lifecycle_idx';
 enabled 
---------
 t
(1 row)
 
DROP INDEX vec_lifecycle_idx;
DROP TABLE dbms_vector_lifecycle_tab;
DELETE FROM "SYS".vector_index_checkpointing;
\! test ! -f /tmp/dbms_vector_fixture.pid || kill "$(cat /tmp/dbms_vector_fixture.pid)" 2>/dev/null || true; rm -f /tmp/dbms_vector_fixture.pid /tmp/dbms_vector_fixture.log
\! rm -rf /tmp/kilo/dbms_vector
\! rm -f /tmp/rudb_dbms_vector_smoke.onnx