-
Notifications
You must be signed in to change notification settings - Fork 253
Expand file tree
/
Copy pathob_vector_index_util.cpp
More file actions
6646 lines (6413 loc) · 327 KB
/
Copy pathob_vector_index_util.cpp
File metadata and controls
6646 lines (6413 loc) · 327 KB
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
/*
* Copyright (c) 2025 OceanBase.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#define USING_LOG_PREFIX SHARE
#include "ob_vector_index_util.h"
#include "storage/vector_index/ob_vector_index_sched_job_utils.h"
#include "sql/engine/expr/ob_array_expr_utils.h"
#include "sql/engine/ob_exec_context.h"
#include "share/ob_vec_index_builder_util.h"
#include "share/ob_fts_index_builder_util.h"
#include "share/vector_index/ob_plugin_vector_index_util.h"
#include "share/vector_index/ob_plugin_vector_index_utils.h"
#include "share/vector_index/ob_plugin_vector_index_service.h"
#include "share/vector_index/ob_plugin_vector_index_adaptor.h"
#include "share/allocator/ob_shared_memory_allocator_mgr.h"
#include "lib/roaringbitmap/ob_rb_memory_mgr.h"
#include "lib/file/ob_string_util.h"
#include "sql/engine/expr/ob_array_cast.h"
#include "sql/engine/expr/ob_expr_ai/ob_ai_func_utils.h"
namespace oceanbase
{
using namespace sql;
using namespace common;
namespace share
{
/*
Expect index_param_str to be an uppercase string
*/
int ObVectorIndexUtil::parser_params_from_string(
const ObString &index_param_str, ObVectorIndexType index_type, ObVectorIndexParam ¶m, const bool set_default)
{
int ret = OB_SUCCESS;
ObString tmp_param_str = index_param_str;
ObArray<ObString> tmp_param_strs;
param.reset();
if (tmp_param_str.empty()) {
ret = OB_ERR_UNEXPECTED;
LOG_WARN("unexpected vector index param, is empty", K(ret));
} else if (OB_FAIL(split_on(tmp_param_str, ',', tmp_param_strs))) {
LOG_WARN("fail to split func expr", K(ret), K(tmp_param_str));
} else if (index_type != ObVectorIndexType::VIT_SPIV_INDEX && tmp_param_strs.count() < 2) { // at lease two params(distance, type) should be set
ret = OB_INVALID_ARGUMENT;
LOG_WARN("invalid vector index param count", K(tmp_param_strs.count()));
} else if (index_type == ObVectorIndexType::VIT_SPIV_INDEX && tmp_param_strs.count() != 1) {
ret = OB_INVALID_ARGUMENT;
LOG_WARN("invalid vector index param count", K(tmp_param_strs.count()));
} else {
const int64_t default_m_value = 16;
const int64_t default_ef_construction_value = 200;
const int64_t default_ef_search_value = 64;
const int64_t default_nlist_value = 128;
const int64_t default_sample_per_nlist_value = 256;
const int64_t default_nbits_value = 8;
const int64_t default_sync_interval_value = 10;
for (int64_t i = 0; OB_SUCC(ret) && i < tmp_param_strs.count(); ++i) {
ObString one_tmp_param_str = tmp_param_strs.at(i).trim();
ObArray<ObString> one_tmp_param_strs;
if (OB_FAIL(split_on(one_tmp_param_str, '=', one_tmp_param_strs))) {
LOG_WARN("fail to split one param str", K(ret), K(one_tmp_param_str));
} else if (one_tmp_param_strs.count() != 2) {
ret = OB_INVALID_ARGUMENT;
LOG_WARN("invalid vector index one param pair count", K(one_tmp_param_strs.count()));
} else {
ObString new_param_name = one_tmp_param_strs.at(0).trim();
ObString new_param_value = one_tmp_param_strs.at(1).trim();
if (new_param_name == "DISTANCE") {
if (new_param_value == "INNER_PRODUCT") {
param.dist_algorithm_ = ObVectorIndexDistAlgorithm::VIDA_IP;
} else if (new_param_value == "L2") {
param.dist_algorithm_ = ObVectorIndexDistAlgorithm::VIDA_L2;
} else if (new_param_value == "COSINE") {
param.dist_algorithm_ = ObVectorIndexDistAlgorithm::VIDA_COS;
} else {
ret = OB_NOT_SUPPORTED;
LOG_WARN("not support vector index dist algorithm", K(ret), K(new_param_value));
}
} else if (new_param_name == "LIB") {
if (new_param_value == "VSAG") {
param.lib_ = ObVectorIndexAlgorithmLib::VIAL_VSAG;
} else if (new_param_value == "OB") {
param.lib_ = ObVectorIndexAlgorithmLib::VIAL_OB;
} else {
ret = OB_NOT_SUPPORTED;
LOG_WARN("not support vector index lib", K(ret), K(new_param_value));
}
} else if (new_param_name == "TYPE") {
if (new_param_value == "HNSW") {
param.type_ = ObVectorIndexAlgorithmType::VIAT_HNSW;
} else if (new_param_value == "HNSW_SQ") {
param.type_ = ObVectorIndexAlgorithmType::VIAT_HNSW_SQ;
} else if (new_param_value == "IVF_FLAT") {
param.type_ = ObVectorIndexAlgorithmType::VIAT_IVF_FLAT;
} else if (new_param_value == "IVF_SQ8") {
param.type_ = ObVectorIndexAlgorithmType::VIAT_IVF_SQ8;
} else if (new_param_value == "IVF_PQ") {
param.type_ = ObVectorIndexAlgorithmType::VIAT_IVF_PQ;
} else if (new_param_value == "HNSW_BQ") {
param.type_ = ObVectorIndexAlgorithmType::VIAT_HNSW_BQ;
} else if (new_param_value == "SINDI") {
param.type_ = ObVectorIndexAlgorithmType::VIAT_IPIVF;
} else {
ret = OB_NOT_SUPPORTED;
LOG_WARN("not support vector index type", K(ret), K(new_param_value));
}
} else if (new_param_name == "M") { // here must be ivf_pq or hnsw index
int64_t int_value = 0;
if (OB_FAIL(ObSchemaUtils::str_to_int(new_param_value, int_value))) {
LOG_WARN("fail to str_to_int", K(ret), K(new_param_value));
} else if (ObVectorIndexType::VIT_HNSW_INDEX == index_type) {
if (int_value >= 5 && int_value <= 128) {
param.m_ = int_value;
} else {
ret = OB_NOT_SUPPORTED;
LOG_WARN("not support vector index m value", K(ret), K(int_value), K(new_param_value));
}
} else if (ObVectorIndexType::VIT_IVF_INDEX == index_type) {
if (int_value >= 1 && int_value <= 65536) {
param.m_ = int_value;
} else {
ret = OB_NOT_SUPPORTED;
LOG_WARN("not support vector index m value", K(ret), K(int_value), K(new_param_value));
}
}
} else if (new_param_name == "EF_CONSTRUCTION") {
int64_t int_value = 0;
if (OB_FAIL(ObSchemaUtils::str_to_int(new_param_value, int_value))) {
LOG_WARN("fail to str_to_int", K(ret), K(new_param_value));
} else if (int_value >= 5 && int_value <= 1000) {
param.ef_construction_ = int_value;
} else {
ret = OB_NOT_SUPPORTED;
LOG_WARN("not support vector index ef_construction value", K(ret), K(int_value), K(new_param_value));
}
} else if (new_param_name == "EF_SEARCH") {
int64_t int_value = 0;
if (OB_FAIL(ObSchemaUtils::str_to_int(new_param_value, int_value))) {
LOG_WARN("fail to str_to_int", K(ret), K(new_param_value));
} else if (int_value >= 1 && int_value <= 160000) {
param.ef_search_ = int_value;
} else {
ret = OB_NOT_SUPPORTED;
LOG_WARN("not support vector index ef_search value", K(ret), K(int_value), K(new_param_value));
}
} else if (new_param_name == "NLIST") {
int64_t int_value = 0;
if (OB_FAIL(ObSchemaUtils::str_to_int(new_param_value, int_value))) {
LOG_WARN("fail to str_to_int", K(ret), K(new_param_value));
} else if (int_value >= 1 && int_value <= 65536) {
param.nlist_ = int_value;
} else {
ret = OB_NOT_SUPPORTED;
LOG_WARN("not support vector index nlist value", K(ret), K(int_value), K(new_param_value));
}
} else if (new_param_name == "SAMPLE_PER_NLIST") {
int64_t int_value = 0;
if (OB_FAIL(ObSchemaUtils::str_to_int(new_param_value, int_value))) {
LOG_WARN("fail to str_to_int", K(ret), K(new_param_value));
} else if (int_value >= 1 && int_value <= INT64_MAX) {
param.sample_per_nlist_ = int_value;
} else {
ret = OB_NOT_SUPPORTED;
LOG_WARN("not support vector index sample_per_nlist value", K(ret), K(int_value), K(new_param_value));
}
} else if (new_param_name == "EXTRA_INFO_MAX_SIZE") {
int64_t int_value = 0;
bool is_int = false;
if (OB_FAIL(is_int_val(new_param_value, is_int)) || !is_int) {
ret = OB_NOT_SUPPORTED;
LOG_WARN("not support vector index extra_info_max_size value", K(ret), K(new_param_value));
} else if (OB_FAIL(ObSchemaUtils::str_to_int(new_param_value, int_value))) {
LOG_WARN("fail to str_to_int", K(ret), K(new_param_value));
} else if (ObVectorIndexType::VIT_HNSW_INDEX == index_type) {
if (int_value >= 0 && int_value <= INT64_MAX) {
param.extra_info_max_size_ = int_value;
} else {
ret = OB_NOT_SUPPORTED;
LOG_WARN("not support vector index extra_info_max_size value", K(ret), K(int_value), K(new_param_value));
}
}
} else if (new_param_name == "EXTRA_INFO_ACTUAL_SIZE") {
int64_t int_value = 0;
if (OB_FAIL(ObSchemaUtils::str_to_int(new_param_value, int_value))) {
LOG_WARN("fail to str_to_int", K(ret), K(new_param_value));
} else if (ObVectorIndexType::VIT_HNSW_INDEX == index_type) {
if (int_value >= 0 && int_value <= INT64_MAX) {
param.extra_info_actual_size_ = int_value;
} else {
ret = OB_NOT_SUPPORTED;
LOG_WARN("not support vector index extra_info_actual_size value", K(ret), K(int_value), K(new_param_value));
}
}
} else if (new_param_name == "REFINE_TYPE") {
if (new_param_value == "FP32") {
param.refine_type_ = 0;
} else if (new_param_value == "SQ8") {
param.refine_type_ = 1;
} else {
ret = OB_INVALID_ARGUMENT;
LOG_WARN("invalid vector index param", K(ret), K(new_param_name), K(new_param_value));
}
} else if (new_param_name == "BQ_BITS_QUERY") {
int64_t int_value = 0;
if (OB_FAIL(ObSchemaUtils::str_to_int(new_param_value, int_value))) {
LOG_WARN("fail to str_to_int", K(ret), K(new_param_value));
} else if (int_value != 0 && int_value != 4 && int_value != 32) {
ret = OB_NOT_SUPPORTED;
LOG_WARN("not support vector index bq_bits_query value", K(ret), K(int_value), K(new_param_value));
} else {
param.bq_bits_query_ = int_value;
}
} else if (new_param_name == "REFINE_K") {
int err = 0;
char *endptr = NULL;
double out_val = ObCharset::strntod(new_param_value.ptr(), new_param_value.length(), &endptr, &err);
if (err != 0 || (new_param_value.ptr() + new_param_value.length()) != endptr) {
ret = OB_DATA_OUT_OF_RANGE;
LOG_WARN("fail to cast string to double", K(ret), K(new_param_value), K(err), KP(endptr));
} else if (out_val < 1.0 || out_val > 1000) {
ret = OB_NOT_SUPPORTED;
LOG_WARN("not support vector index refine_k value", K(ret), K(out_val), K(new_param_value));
} else {
param.refine_k_ = out_val;
}
} else if (new_param_name == "BQ_USE_FHT") {
if (new_param_value == "FALSE") {
param.bq_use_fht_ = false;
} else if (new_param_value == "TRUE") {
param.bq_use_fht_ = true;
} else {
ret = OB_NOT_SUPPORTED;
LOG_WARN("not support vector index bq_use_fht value", K(ret), K(new_param_name), K(new_param_value));
}
} else if (new_param_name == "NBITS") {
int64_t int_value = 0;
if (OB_FAIL(ObSchemaUtils::str_to_int(new_param_value, int_value))) {
LOG_WARN("fail to str_to_int", K(ret), K(new_param_value));
} else if (ObVectorIndexType::VIT_IVF_INDEX == index_type) {
if (int_value >= 1 && int_value <= 24) {
param.nbits_ = int_value;
} else {
ret = OB_NOT_SUPPORTED;
LOG_WARN("not support vector index nbits value", K(ret), K(int_value), K(new_param_value));
}
}
} else if (new_param_name == "PRUNE") {
if (new_param_value == "FALSE") {
param.prune_ = false;
} else if (new_param_value == "TRUE") {
param.prune_ = true;
} else {
ret = OB_NOT_SUPPORTED;
LOG_WARN("not support vector index prune value", K(ret), K(new_param_name), K(new_param_value));
}
} else if (new_param_name == "REFINE") {
if (new_param_value == "FALSE") {
param.refine_ = false;
} else if (new_param_value == "TRUE") {
param.refine_ = true;
} else {
ret = OB_NOT_SUPPORTED;
LOG_WARN("not support vector index refine value", K(ret), K(new_param_name), K(new_param_value));
}
} else if (new_param_name == "DROP_RATIO_BUILD") {
int err = 0;
char *endptr = NULL;
double out_val = ObCharset::strntod(new_param_value.ptr(), new_param_value.length(), &endptr, &err);
if (err != 0 || (new_param_value.ptr() + new_param_value.length()) != endptr) {
ret = OB_DATA_OUT_OF_RANGE;
LOG_WARN("fail to cast string to double", K(ret), K(new_param_value), K(err), KP(endptr));
} else if (out_val < 0.0 || out_val > 0.9) {
ret = OB_NOT_SUPPORTED;
LOG_WARN("not support vector index drop_ratio_build value", K(ret), K(out_val), K(new_param_value));
} else {
param.ob_sparse_drop_ratio_build_ = out_val;
}
} else if (new_param_name == "DROP_RATIO_SEARCH") {
int err = 0;
char *endptr = NULL;
double out_val = ObCharset::strntod(new_param_value.ptr(), new_param_value.length(), &endptr, &err);
if (err != 0 || (new_param_value.ptr() + new_param_value.length()) != endptr) {
ret = OB_DATA_OUT_OF_RANGE;
LOG_WARN("fail to cast string to double", K(ret), K(new_param_value), K(err), KP(endptr));
} else if (out_val < 0.0 || out_val > 0.9) {
ret = OB_NOT_SUPPORTED;
LOG_WARN("not support vector index drop_ratio_search value", K(ret), K(out_val), K(new_param_value));
} else {
param.ob_sparse_drop_ratio_search_ = out_val;
}
} else if (new_param_name == "MODEL") {
if (new_param_value.empty()) {
ret = OB_INVALID_ARGUMENT;
LOG_WARN("model value cannot be empty", K(ret), K(new_param_value));
} else if (new_param_value.length() >= OB_MAX_ENDPOINT_LENGTH) {
ret = OB_INVALID_ARGUMENT;
LOG_WARN("model value too long", K(ret), K(new_param_value.length()), K(OB_MAX_ENDPOINT_LENGTH));
} else {
MEMCPY(param.endpoint_, new_param_value.ptr(), new_param_value.length());
param.endpoint_[new_param_value.length()] = '\0';
}
} else if (new_param_name == "DIM") {
int64_t int_value = 0;
if (OB_FAIL(ObSchemaUtils::str_to_int(new_param_value, int_value))) {
LOG_WARN("fail to str_to_int", K(ret), K(new_param_value));
} else if (int_value <= 0) {
ret = OB_INVALID_ARGUMENT;
LOG_WARN("dim value must be positive", K(ret), K(int_value), K(new_param_value));
} else if (int_value > MAX_DIM_LIMITED) {
ret = OB_INVALID_ARGUMENT;
LOG_WARN("dim value exceeds maximum limit", K(ret), K(int_value), K(MAX_DIM_LIMITED), K(new_param_value));
} else {
param.dim_ = int_value;
}
} else if (new_param_name == "SYNC_INTERVAL") {
int64_t seconds = 0;
if (OB_FAIL(parse_time_string_to_seconds(new_param_value, seconds))) {
ret = OB_INVALID_ARGUMENT;
LOG_WARN("sync_interval must be a time string (e.g., '10s', '10h', '10d')", K(ret), K(new_param_value));
} else if (seconds < 0) {
ret = OB_INVALID_ARGUMENT;
LOG_WARN("sync_interval time value must be positive", K(ret), K(seconds), K(new_param_value));
} else if (param.sync_interval_value_ == 0) {
// just skip the set
} else {
param.sync_interval_value_ = seconds;
}
} else if (new_param_name == "SYNC_MODE") {
if (new_param_value.empty()) {
ret = OB_INVALID_ARGUMENT;
LOG_WARN("sync_mode value cannot be empty", K(ret), K(new_param_value));
} else if (new_param_value == "IMMEDIATE") {
param.sync_interval_type_ = ObVectorIndexSyncIntervalType::VSIT_IMMEDIATE;
param.sync_interval_value_ = 0;
} else if (new_param_value == "MANUAL") {
param.sync_interval_type_ = ObVectorIndexSyncIntervalType::VSIT_MANUAL;
param.sync_interval_value_ = 0;
} else if (new_param_value == "ASYNC") {
param.sync_interval_type_ = ObVectorIndexSyncIntervalType::VSIT_NUMERIC;
param.sync_mode_async_ = true;
} else {
ret = OB_INVALID_ARGUMENT;
LOG_WARN("sync_mode value is invalid", K(ret), K(new_param_value));
}
} else if (new_param_name == "SIMILARITY") {
int err = 0;
char *endptr = NULL;
double out_val = ObCharset::strntod(new_param_value.ptr(), new_param_value.length(), &endptr, &err);
if (err != 0 || (new_param_value.ptr() + new_param_value.length()) != endptr) {
ret = OB_DATA_OUT_OF_RANGE;
LOG_WARN("fail to cast string to double", K(ret), K(new_param_value), K(err), KP(endptr));
} else if (out_val < 1.0 || out_val > 1e6) {
ret = OB_NOT_SUPPORTED;
LOG_WARN("not support vector index refine_k value", K(ret), K(out_val), K(new_param_value));
} else {
param.similarity_threshold_ = out_val;
}
} else {
ret = OB_INVALID_ARGUMENT;
LOG_WARN("invalid vector index param name", K(ret), K(new_param_name));
}
if (index_type == ObVectorIndexType::VIT_SPIV_INDEX) {
param.type_ = ObVectorIndexAlgorithmType::VIAT_SPIV;
}
}
}
if (OB_SUCC(ret) && set_default) { // if vector param is not set, use default
if (index_type == ObVectorIndexType::VIT_HNSW_INDEX) {
if (param.m_ == 0) {
param.m_ = default_m_value;
}
if (param.ef_construction_ == 0) {
param.ef_construction_ = default_ef_construction_value;
}
if (param.ef_search_ == 0) {
param.ef_search_ = default_ef_search_value;
}
if (param.lib_ == ObVectorIndexAlgorithmLib::VIAL_MAX) {
param.lib_ = ObVectorIndexAlgorithmLib::VIAL_VSAG;
}
if (param.extra_info_actual_size_ > 0 && param.type_ == ObVectorIndexAlgorithmType::VIAT_HNSW) {
param.type_ = ObVectorIndexAlgorithmType::VIAT_HGRAPH;
}
if (param.type_ == ObVectorIndexAlgorithmType::VIAT_IPIVF && param.dist_algorithm_ == VIDA_MAX) {
param.dist_algorithm_ = VIDA_IP;
}
if (param.sync_interval_value_ == 0) {
param.sync_interval_value_ = default_sync_interval_value;
}
if (param.sync_interval_type_ == ObVectorIndexSyncIntervalType::VSIT_MAX) {
param.sync_interval_type_ = ObVectorIndexSyncIntervalType::VSIT_NUMERIC;
}
} else if (index_type == ObVectorIndexType::VIT_IVF_INDEX) {
if (param.nlist_ == 0) {
param.nlist_ = default_nlist_value;
}
if (param.sample_per_nlist_ == 0) {
param.sample_per_nlist_ = default_sample_per_nlist_value;
}
if (param.lib_ == ObVectorIndexAlgorithmLib::VIAL_MAX) {
param.lib_ = ObVectorIndexAlgorithmLib::VIAL_OB;
}
if (param.nbits_ == 0) {
param.nbits_ = default_nbits_value;
}
} else if (index_type == ObVectorIndexType::VIT_SPIV_INDEX) {
if (param.dist_algorithm_ == VIDA_MAX) {
param.dist_algorithm_ = VIDA_IP;
}
} else {
ret = OB_NOT_SUPPORTED;
LOG_WARN("not support vector index type", K(ret), K(index_type));
}
param.dim_ = 0; // TODO@xiajin: fill dim
}
LOG_DEBUG("parser vector index param", K(ret), K(index_param_str), K(param));
}
return ret;
}
bool ObVectorIndexUtil::is_sync_mode_async(const ObString &index_params)
{
return is_sync_mode_async(index_params, false /* is_hnsw_heap_table */);
}
bool ObVectorIndexUtil::is_sync_mode_async(const ObString &index_params, bool is_hnsw_heap_table)
{
bool is_async = false;
// SYNC_MODE is only valid for HNSW+heap_table: ASYNC or IMMEDIATE.
// For hybrid/IVF: SYNC_INTERVAL/MANUAL are used, not SYNC_MODE.
ObCollationType calc_cs_type = CS_TYPE_UTF8MB4_GENERAL_CI;
uint32_t immediate_pos = ObCharset::locate(calc_cs_type,
index_params.ptr(),
index_params.length(),
"SYNC_MODE=IMMEDIATE",
18,
1);
if (immediate_pos > 0) {
is_async = false; // explicitly sync
} else {
uint32_t async_pos = ObCharset::locate(calc_cs_type,
index_params.ptr(),
index_params.length(),
"SYNC_MODE=ASYNC",
14,
1);
if (async_pos > 0) {
is_async = true; // explicitly async
} else if (is_hnsw_heap_table) {
// SYNC_MODE not specified: for HNSW+heap default is ASYNC (index_id table may have empty params)
is_async = true;
}
}
return is_async;
}
int ObVectorIndexUtil::parse_time_string_to_seconds(const ObString &time_str, int64_t &seconds)
{
int ret = OB_SUCCESS;
seconds = 0;
if (time_str.empty()) {
ret = OB_INVALID_ARGUMENT;
LOG_WARN("time string is empty", K(ret));
} else {
const char *ptr = time_str.ptr();
int64_t len = time_str.length();
if (len < 2) {
ret = OB_INVALID_ARGUMENT;
LOG_WARN("time string too short", K(ret), K(time_str));
} else {
char unit = ptr[len - 1];
ObString number_str;
number_str.assign_ptr(ptr, static_cast<int32_t>(len - 1));
int64_t number = 0;
if (OB_FAIL(ObSchemaUtils::str_to_int(number_str, number))) {
ret = OB_INVALID_ARGUMENT;
LOG_WARN("invalid number in time string", K(ret), K(number_str), K(time_str));
} else if (number <= 0) {
ret = OB_INVALID_ARGUMENT;
LOG_WARN("time value must be positive", K(ret), K(number), K(time_str));
} else {
switch (unit) {
case 's':
case 'S':
seconds = number;
break;
case 'm':
case 'M':
seconds = number * 60;
break;
case 'h':
case 'H':
seconds = number * 3600;
break;
case 'd':
case 'D':
seconds = number * 86400;
break;
default:
ret = OB_INVALID_ARGUMENT;
LOG_WARN("unsupported time unit", K(ret), K(unit), K(time_str));
break;
}
}
}
}
return ret;
}
// embedding result is string like "[0.181664,0.013905,0.628127]", trans to float array binary
int ObVectorIndexUtil::cast_vector_array_str_to_float_array_binary(ObIAllocator &allocator,
const ObString &vector_array_str,
int64_t dim,
ObString &output_vec)
{
int ret = OB_SUCCESS;
if (vector_array_str.empty()) {
ret = OB_INVALID_ARGUMENT;
LOG_WARN("vector_array_str is empty", K(ret));
} else {
ObCollectionBasicType elem_type;
elem_type.type_id_ = ObNestedType::OB_BASIC_TYPE;
elem_type.basic_meta_.set_obj_type(ObFloatType);
ObCollectionArrayType vector_type(allocator);
vector_type.type_id_ = ObNestedType::OB_VECTOR_TYPE;
vector_type.element_type_ = &elem_type;
vector_type.dim_cnt_ = dim;
ObIArrayType *vector_array = nullptr;
ObVectorF32Data vector_data;
ObArrayData<float> arr_data(allocator);
vector_data.set_array_data(&arr_data);
vector_data.set_element_type(ObFloatType);
vector_array = &vector_data;
ObString vector_array_str_copy = vector_array_str;
if (OB_FAIL(ObArrayCastUtils::string_cast_vector(allocator, vector_array_str_copy, vector_array, &vector_type, false))) {
LOG_WARN("failed to parse vector string", K(ret), K(vector_array_str));
} else {
// After parsing, call init() to sync data from data_container_ to data_ pointer
if (OB_FAIL(vector_data.init())) {
LOG_WARN("failed to init vector data", K(ret));
} else {
ObString temp_vec;
temp_vec.assign_ptr(vector_data.get_data(), vector_data.get_raw_binary_len());
int64_t real_dim = vector_data.get_raw_binary_len() / sizeof(float);
if (real_dim != dim) {
ret = OB_ERR_INVALID_VECTOR_DIM;
LOG_WARN("invalid array size", K(ret), K(dim), K(real_dim));
LOG_USER_ERROR(OB_ERR_INVALID_VECTOR_DIM, dim, real_dim);
} else if (OB_FAIL(ob_write_string(allocator, temp_vec, output_vec))) {
LOG_WARN("failed to write string", K(ret));
}
}
}
}
return ret;
}
int ObVectorIndexUtil::get_vector_from_vector_array_string(ObIAllocator &allocator,
const ObString &vector_array_str,
const ObString ¶m_str,
ObString &output_vec)
{
int ret = OB_SUCCESS;
ObVectorIndexParam param;
ObVectorIndexType index_type = ObVectorIndexType::VIT_HNSW_INDEX;
if (OB_FAIL(parser_params_from_string(param_str, index_type, param, false))) {
LOG_WARN("parse vector index param failed", K(ret), K(param_str));
} else if (OB_FAIL(cast_vector_array_str_to_float_array_binary(allocator, vector_array_str, param.dim_, output_vec))) {
LOG_WARN("cast vector array str to float binary failed", K(ret));
}
return ret;
}
int ObVectorIndexUtil::get_vector_from_text_by_embedding(ObIAllocator &allocator,
const ObString &query_text,
const ObString ¶m_str,
ObString &output_vec)
{
int ret = OB_SUCCESS;
ObVectorIndexParam param;
ObVectorIndexType index_type = ObVectorIndexType::VIT_HNSW_INDEX;
ObString result;
if (OB_FAIL(parser_params_from_string(param_str, index_type, param, false))) {
LOG_WARN("parse vector index param failed", K(ret), K(param_str));
} else if (strlen(param.endpoint_) == 0) {
LOG_WARN("wrong vector index param endpoint", K(ret), K(param_str));
} else {
ObString endpoint_str(param.endpoint_);
ObAIFuncExprInfo *ai_fun_info = nullptr;
omt::ObAiServiceGuard ai_service_guard;
omt::ObTenantAiService *ai_service = MTL(omt::ObTenantAiService*);
const share::ObAiModelEndpointInfo *endpoint_info = nullptr;
if (OB_FAIL(ObAIFuncUtils::get_ai_func_info(allocator, endpoint_str, ai_fun_info))) {
LOG_WARN("failed to get ai fun info", K(ret), K(param_str));
} else if (OB_ISNULL(ai_service)) {
ret = OB_ERR_UNEXPECTED;
LOG_WARN("ai service is null", K(ret));
} else if (OB_FAIL(ai_service->get_ai_service_guard(ai_service_guard))) {
LOG_WARN("failed to get ai service guard", K(ret));
} else if (OB_FAIL(ai_service_guard.get_ai_endpoint_by_ai_model_name(endpoint_str, endpoint_info))) {
LOG_WARN("failed to get endpoint info", K(ret), K(endpoint_str));
} else {
int64_t dim = param.dim_;
ObJsonInt *dim_json = nullptr;
ObJsonObject *config = nullptr;
ObAIFuncModel model(allocator, *ai_fun_info, *endpoint_info);
ObString query_text_copy = query_text; // Create a non-const copy
if (OB_FAIL(ObAIFuncJsonUtils::get_json_object(allocator, config))) {
LOG_WARN("fail to get json object", K(ret));
} else if (OB_FAIL(ObAIFuncJsonUtils::get_json_int(allocator, dim, dim_json))) {
LOG_WARN("fail to get json int", K(ret));
} else if (OB_FAIL(config->add("dimensions", dim_json))) {
LOG_WARN("fail to add dimensions", K(ret));
} else if (OB_FAIL(model.call_dense_embedding(query_text_copy, config, result))) {
LOG_WARN("fail to call embedding", K(ret));
}
}
}
if (OB_FAIL(ret)) {
// do nothing
} else if (result.empty()) {
ret = OB_INVALID_ARGUMENT;
LOG_WARN("embedding result is empty", K(ret));
} else {
// embedding result is like "[0.181664,0.013905,0.628127]", trans to float array binary
ObCollectionBasicType elem_type;
elem_type.type_id_ = ObNestedType::OB_BASIC_TYPE;
elem_type.basic_meta_.set_obj_type(ObFloatType);
ObCollectionArrayType vector_type(allocator);
vector_type.type_id_ = ObNestedType::OB_VECTOR_TYPE;
vector_type.element_type_ = &elem_type;
vector_type.dim_cnt_ = param.dim_;
ObIArrayType *vector_array = nullptr;
ObVectorF32Data vector_data;
ObArrayData<float> arr_data(allocator);
vector_data.set_array_data(&arr_data);
vector_data.set_element_type(ObFloatType);
vector_array = &vector_data;
// Use string_cast_vector to parse the vector string
if (OB_FAIL(ObArrayCastUtils::string_cast_vector(allocator, result, vector_array, &vector_type, false))) {
LOG_WARN("failed to parse vector string", K(ret), K(result));
} else {
// After parsing, call init() to sync data from data_container_ to data_ pointer
if (OB_FAIL(vector_data.init())) {
LOG_WARN("failed to init vector data", K(ret));
}
int32_t binary_len = vector_data.get_raw_binary_len();
output_vec.assign_ptr(vector_data.get_data(), binary_len);
}
}
return ret;
}
int ObVectorIndexParam::build_search_param(const ObVectorIndexParam &index_param,
const ObVectorIndexQueryParam &query_param,
ObVectorIndexParam &search_param)
{
int ret = OB_SUCCESS;
if (OB_FAIL(search_param.assign(index_param))) {
LOG_WARN("fail to parser params from string", K(ret), K(index_param));
} else {
if (query_param.is_set_ef_search_) {
search_param.ef_search_ = query_param.ef_search_;
}
if (query_param.is_set_refine_k_) {
if (ObVectorIndexAlgorithmType::VIAT_HNSW_BQ != index_param.type_ && ObVectorIndexAlgorithmType::VIAT_IPIVF != index_param.type_) {
ret = OB_NOT_SUPPORTED;
LOG_WARN("refine_k is not support parameter for current index", K(ret));
LOG_USER_ERROR(OB_NOT_SUPPORTED, "refine_k parameter for current index is");
} else {
search_param.refine_k_ = query_param.refine_k_;
}
}
if (OB_FAIL(ret)) {
} else if (query_param.is_set_drop_ratio_search_) {
if (ObVectorIndexAlgorithmType::VIAT_IPIVF != index_param.type_) {
ret = OB_NOT_SUPPORTED;
LOG_WARN("drop_ratio_search is not support parameter for current index", K(ret), K(index_param.type_));
LOG_USER_ERROR(OB_NOT_SUPPORTED, "drop_ratio_search parameter for current index is");
} else {
search_param.ob_sparse_drop_ratio_search_ = query_param.ob_sparse_drop_ratio_search_;
}
}
if (query_param.is_set_similarity_threshold_) {
if (index_param.type_ == ObVectorIndexAlgorithmType::VIAT_SPIV) {
ret = OB_NOT_SUPPORTED;
LOG_WARN("similarity is not support parameter for current index", K(ret));
LOG_USER_ERROR(OB_NOT_SUPPORTED, "similarity parameter for current index is");
} else if (index_param.dist_algorithm_ == VIDA_IP) {
ret = OB_NOT_SUPPORTED;
LOG_WARN("similarity is not supported for inner_product distance", K(ret));
LOG_USER_ERROR(OB_NOT_SUPPORTED, "similarity parameter for inner_product distance is");
} else {
search_param.similarity_threshold_ = query_param.similarity_threshold_;
}
}
LOG_TRACE("vector param", K(index_param), K(query_param), K(search_param));
}
return ret;
}
int ObVectorIndexUtil::resolve_query_param(
const ParseNode *param_list_node,
ObVectorIndexQueryParam ¶m)
{
int ret = OB_SUCCESS;
const ParseNode *param_node = nullptr;
for (int32_t i = 0; OB_SUCC(ret) && i < param_list_node->num_child_; i+=2) {
if (i + 1 >= param_list_node->num_child_) {
ret = OB_ERR_UNEXPECTED;
LOG_WARN("num_child is incorrect", K(ret), K(i), K(param_node->num_child_), K(param_node->type_));
} else {
ObString param_name;
const ParseNode *key_node = param_list_node->children_[i];
const ParseNode *value_node = param_list_node->children_[i + 1];
param_name.assign_ptr(key_node->str_value_, static_cast<int32_t>(key_node->str_len_));
if (param_name.case_compare("EF_SEARCH") == 0) {
if (param.is_set_ef_search_) {
ret = OB_ERR_PARAM_DUPLICATE;
LOG_WARN("duplicate ef_search param", K(ret), K(i));
} else if (value_node->type_ != T_INT && value_node->type_ != T_NUMBER) {
ret = OB_INVALID_ARGUMENT;
LOG_WARN("invalid query param", K(ret), K(i), K(param_name), K(value_node->type_));
} else if (! (value_node->value_ >= 1 && value_node->value_ <= 160000)) {
ret = OB_INVALID_ARGUMENT;
LOG_WARN("invalid query param", K(ret), K(i), K(param_name), K(value_node->type_), K(value_node->value_));
} else {
param.ef_search_ = value_node->value_;
param.is_set_ef_search_ = 1;
}
} else if (param_name.case_compare("REFINE_K") == 0) {
int err = 0;
char *endptr = NULL;
ObString value_str(static_cast<int32_t>(value_node->str_len_), value_node->str_value_);
double out_val = 0;
if (param.is_set_refine_k_) {
ret = OB_ERR_PARAM_DUPLICATE;
LOG_WARN("duplicate refine_k param", K(ret), K(i));
} else if (OB_FALSE_IT(out_val = ObCharset::strntod(value_str.ptr(), value_str.length(), &endptr, &err))) {
} else if (err != 0 || (value_str.ptr() + value_str.length()) != endptr) {
ret = OB_DATA_OUT_OF_RANGE;
LOG_WARN("fail to cast string to double", K(ret), K(value_str), K(err), KP(endptr));
} else if (out_val < 1.0 || out_val > 1000) {
ret = OB_INVALID_ARGUMENT;
LOG_WARN("invalid vector index refine_k value", K(ret), K(out_val), K(value_str));
LOG_USER_ERROR(OB_INVALID_ARGUMENT, "vector index refine_k value");
} else {
param.refine_k_ = out_val;
param.is_set_refine_k_ = 1;
}
} else if (param_name.case_compare("DROP_RATIO_SEARCH") == 0) {
int err = 0;
char *endptr = NULL;
ObString value_str(static_cast<int32_t>(value_node->str_len_), value_node->str_value_);
double out_val = 0;
if (param.is_set_drop_ratio_search_) {
ret = OB_ERR_PARAM_DUPLICATE;
LOG_WARN("duplicate drop_ratio_search param", K(ret), K(i));
} else if (OB_FALSE_IT(out_val = ObCharset::strntod(value_str.ptr(), value_str.length(), &endptr, &err))) {
} else if (err != 0 || (value_str.ptr() + value_str.length()) != endptr) {
ret = OB_DATA_OUT_OF_RANGE;
LOG_WARN("fail to cast string to double", K(ret), K(value_str), K(err), KP(endptr));
} else if (out_val < 0 || out_val > 0.9) {
ret = OB_INVALID_ARGUMENT;
LOG_WARN("invalid vector index drop_ratio_search value", K(ret), K(out_val), K(value_str));
LOG_USER_ERROR(OB_INVALID_ARGUMENT, "vector index drop_ratio_search value");
} else {
param.ob_sparse_drop_ratio_search_ = out_val;
param.is_set_drop_ratio_search_ = 1;
}
} else if (param_name.case_compare("SIMILARITY") == 0) {
int err = 0;
char *endptr = NULL;
ObString value_str(static_cast<int32_t>(value_node->str_len_), value_node->str_value_);
double out_val = 0;
if (param.is_set_similarity_threshold_) {
ret = OB_ERR_PARAM_DUPLICATE;
LOG_WARN("duplicate similarity param", K(ret), K(i));
} else if (OB_FALSE_IT(out_val = ObCharset::strntod(value_str.ptr(), value_str.length(), &endptr, &err))) {
} else if (err != 0 || (value_str.ptr() + value_str.length()) != endptr) {
ret = OB_DATA_OUT_OF_RANGE;
LOG_WARN("fail to cast string to double", K(ret), K(value_str), K(err), KP(endptr));
} else if (out_val < 0.0 || out_val > 1.0) {
ret = OB_INVALID_ARGUMENT;
LOG_WARN("invalid vector index similarity value", K(ret), K(out_val), K(value_str));
} else {
param.similarity_threshold_ = out_val;
param.is_set_similarity_threshold_ = 1;
}
} else {
ret = OB_INVALID_ARGUMENT;
LOG_WARN("invalid query param", K(ret), K(i), K(param_name));
}
}
}
return ret;
}
int ObVectorIndexUtil::is_int_val(const ObString &str, bool &is_int)
{
int ret = OB_SUCCESS;
is_int = false;
char buf[OB_MAX_BIT_LENGTH];
if (str.empty()) {
ret = OB_INVALID_ARGUMENT;
LOG_WARN("invalid argument", K(ret), K(str));
} else {
int n = snprintf(buf, OB_MAX_BIT_LENGTH, "%.*s", str.length(), str.ptr());
if (n < 0 || n >= OB_MAX_BIT_LENGTH) {
ret = OB_BUF_NOT_ENOUGH;
LOG_WARN("id_buf is not long enough", K(ret), K(n), LITERAL_K(OB_MAX_BIT_LENGTH));
} else {
is_int = ::obsys::ObStringUtil::is_int(buf);
}
}
return ret;
}
int ObVectorIndexUtil::filter_index_param(const ObString &index_param_str, const char *to_filter, char *filtered_param_str, int32_t &res_len)
{
int ret = OB_SUCCESS;
ObString tmp_param_str = index_param_str;
ObArray<ObString> tmp_param_strs;
if (tmp_param_str.empty() ||OB_ISNULL(filtered_param_str) ||OB_ISNULL(to_filter)) {
ret = OB_ERR_UNEXPECTED;
LOG_WARN("unexpected empty", K(ret), K(tmp_param_str), K(filtered_param_str), K(to_filter));
} else if (OB_FAIL(split_on(tmp_param_str, ',', tmp_param_strs))) {
LOG_WARN("fail to split func expr", K(ret), K(tmp_param_str));
} else if (tmp_param_strs.count() < 2) { // at lease two params(distance, type) should be set
ret = OB_ERR_UNEXPECTED;
LOG_WARN("unexpected vector index param count", K(tmp_param_strs.count()));
} else {
bool first_item = true;
int64_t print_pos = 0;
for (int64_t i = 0; OB_SUCC(ret) && i < tmp_param_strs.count(); ++i) {
ObString one_tmp_param_str = tmp_param_strs.at(i).trim();
ObArray<ObString> one_tmp_param_strs;
if (OB_FAIL(split_on(one_tmp_param_str, '=', one_tmp_param_strs))) {
LOG_WARN("fail to split one param str", K(ret), K(one_tmp_param_str));
} else if (one_tmp_param_strs.count() != 2) {
ret = OB_ERR_UNEXPECTED;
LOG_WARN("unexpected vector index one param pair count", K(one_tmp_param_strs.count()));
} else {
ObString new_param_name = one_tmp_param_strs.at(0).trim();
ObString new_param_value = one_tmp_param_strs.at(1).trim();
// filter EXTRA_INFO_ACTUAL_SIZE
if (new_param_name != to_filter) {
if (OB_FAIL(databuff_printf(filtered_param_str, OB_MAX_TABLE_NAME_LENGTH, print_pos,
first_item ? "%.*s=%.*s" : ", %.*s=%.*s", new_param_name.length(),
new_param_name.ptr(), new_param_value.length(), new_param_value.ptr()))) {
LOG_WARN("fail to databuff printf", K(ret), K(print_pos), K(new_param_name), K(new_param_value));
} else {
first_item = false;
}
} else {
LOG_INFO("do not print: ", K(new_param_name), K(new_param_value));
}
}
}
res_len = print_pos;
}
return ret;
}
int ObVectorIndexUtil::print_index_param(const ObTableSchema &table_schema, char *buf, const int64_t &buf_len,
int64_t &pos)
{
int ret = OB_SUCCESS;
bool has_extra_param = false;
const ObString &index_param_str = table_schema.get_index_params();
if (share::schema::is_vec_hnsw_index(table_schema.get_index_type())) {
const char *to_filter = "EXTRA_INFO_ACTUAL_SIZE";
const char *position = strstr(index_param_str.ptr(), to_filter);
if (position != NULL) {
has_extra_param = true;
char print_params_str[OB_MAX_TABLE_NAME_LENGTH];
int32_t len = 0;
if (OB_FAIL(filter_index_param(index_param_str, to_filter, print_params_str, len))) {
LOG_WARN("fail to filter index param", K(ret), K(index_param_str));
} else if (OB_FAIL(databuff_printf(buf, buf_len, pos, "WITH (%.*s) ", len, print_params_str))) {
LOG_WARN("print WITH vector index param failed", K(ret), K(print_params_str));
}
}
}
if (OB_FAIL(ret)) {
} else if (index_param_str.empty() || has_extra_param) {
// skip
} else if (OB_FAIL(databuff_printf(buf, buf_len, pos, "WITH (%.*s) ", index_param_str.length(),
index_param_str.ptr()))) {
LOG_WARN("print WITH vector index param failed", K(ret), K(index_param_str));
}
return ret;
}
int ObVectorIndexUtil::construct_rebuild_index_param(
const ObTableSchema &data_table_schema, const ObString &old_index_params, ObString &new_index_params, ObIAllocator *allocator)
{
int ret = OB_SUCCESS;
ObVectorIndexParam old_vec_param;
ObVectorIndexParam new_vec_param;
ObString old_index_param_str;
const bool set_default = false;
if (old_index_params.empty() || new_index_params.empty() || OB_ISNULL(allocator)) {
ret = OB_INVALID_ARGUMENT;
LOG_WARN("invalid params", K(ret), K(old_index_params), K(new_index_params), KP(allocator));
} else if (OB_FAIL(ob_simple_low_to_up(*allocator, old_index_params, old_index_param_str))) {
LOG_WARN("string low to up failed", K(ret), K(old_index_params));
} else if (OB_FAIL(ob_simple_low_to_up(*allocator, new_index_params, new_index_params))) {
LOG_WARN("string low to up failed", K(ret), K(new_index_params));
} else if (OB_FAIL(parser_params_from_string(old_index_param_str, ObVectorIndexType::VIT_HNSW_INDEX, old_vec_param, set_default))) {
LOG_WARN("fail to parse old index parsm", K(ret), K(old_index_param_str));
} else if (OB_FAIL(parser_params_from_string(new_index_params, ObVectorIndexType::VIT_HNSW_INDEX, new_vec_param, set_default))) {
LOG_WARN("fail to parse new index parsm", K(ret), K(new_index_params));
} else {
bool new_distance_is_set = false;
bool new_type_is_set = false;
char not_set_params_str[OB_MAX_TABLE_NAME_LENGTH];
int64_t pos = 0;
// set distance
if (new_vec_param.dist_algorithm_ != ObVectorIndexDistAlgorithm::VIDA_MAX) {
// distance is reset, check new set is same as old, not support rebuild to new distance algorithm
if (new_vec_param.dist_algorithm_ != old_vec_param.dist_algorithm_) {
ret = OB_NOT_SUPPORTED;
LOG_WARN("distance must be same in rebuild now", K(ret), K(new_vec_param), K(old_vec_param));
} else {
new_distance_is_set = true;
}
} else {
ObString tmp_distance;
if (old_vec_param.dist_algorithm_ == ObVectorIndexDistAlgorithm::VIDA_L2) {
tmp_distance = ObString("L2");
} else if (old_vec_param.dist_algorithm_ == ObVectorIndexDistAlgorithm::VIDA_IP) {
tmp_distance = ObString("IP");
} else if (old_vec_param.dist_algorithm_ == ObVectorIndexDistAlgorithm::VIDA_COS) {
tmp_distance = ObString("COSINE");
} else {
ret = OB_ERR_UNEXPECTED;
LOG_WARN("unexpected old vec param distance algorithm", K(ret), K(old_vec_param.dist_algorithm_));
}
if (OB_FAIL(ret)) {
} else if (OB_FAIL(databuff_printf(not_set_params_str, OB_MAX_TABLE_NAME_LENGTH, pos,
", DISTANCE=%.*s", tmp_distance.length(), tmp_distance.ptr()))) {
LOG_WARN("fail to databuff printf", K(ret), K(pos), K(tmp_distance));
}
}
// set lib
if (OB_FAIL(ret)) {
} else if (new_vec_param.lib_ != ObVectorIndexAlgorithmLib::VIAL_MAX) {
// lib is reset, check new set is same as old, not support rebuild to new lib
if (new_vec_param.lib_ != old_vec_param.lib_) {
ret = OB_NOT_SUPPORTED;
LOG_WARN("lib must be same in rebuild now", K(ret), K(new_vec_param), K(old_vec_param));
}
} else {
ObString tmp_lib;
if (old_vec_param.lib_ == ObVectorIndexAlgorithmLib::VIAL_VSAG) {
tmp_lib = ObString("VSAG");
} else if (old_vec_param.lib_ == ObVectorIndexAlgorithmLib::VIAL_OB) {
tmp_lib = ObString("OB");
} else {
ret = OB_ERR_UNEXPECTED;
LOG_WARN("unexpected old vec param lib", K(ret), K(old_vec_param.lib_));
}
if (OB_FAIL(ret)) {
} else if (OB_FAIL(databuff_printf(not_set_params_str, OB_MAX_TABLE_NAME_LENGTH, pos,
", LIB=%.*s", tmp_lib.length(), tmp_lib.ptr()))) {
LOG_WARN("fail to databuff printf", K(ret), K(pos), K(tmp_lib));
}
}
// set type
if (OB_FAIL(ret)) {