Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
M
MariaDB
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Analytics
Analytics
CI / CD
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
nexedi
MariaDB
Commits
518f9cb7
Commit
518f9cb7
authored
Aug 31, 2024
by
Sergei Golubchik
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
if we require Eigen, we can as well use it everywhere
it's measurably faster even in items
parent
78bd693a
Changes
4
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
180 additions
and
179 deletions
+180
-179
mysql-test/main/vector.result
mysql-test/main/vector.result
+138
-138
mysql-test/main/vector_innodb.result
mysql-test/main/vector_innodb.result
+20
-20
sql/item_vectorfunc.cc
sql/item_vectorfunc.cc
+20
-0
sql/item_vectorfunc.h
sql/item_vectorfunc.h
+2
-21
No files found.
mysql-test/main/vector.result
View file @
518f9cb7
...
...
@@ -108,14 +108,14 @@ id hex(v)
flush tables;
select id,vec_distance_euclidean(v, x'B047263c9f87233fcfd27e3eae493e3f0329f43e') d from t1 order by d limit 3;
id d
9 0.4719976
290006591
10 0.506901
1044450041
3 0.5865673
124650332
9 0.4719976
1867523193
10 0.506901
0853767395
3 0.5865673
422813416
select id,vec_distance_euclidean(x'b047263C9f87233fcfd27e3eae493e3f0329f43e', v) d from t1 order by d limit 3;
id d
9 0.4719976
290006591
10 0.506901
1044450041
3 0.5865673
124650332
9 0.4719976
1867523193
10 0.506901
0853767395
3 0.5865673
422813416
select id>0,vec_distance_euclidean(v, NULL) d from t1 order by d limit 3;
id>0 d
1 NULL
...
...
@@ -138,149 +138,149 @@ id1 id2 vec_distance_euclidean(t1.v, t2.v)
8 8 0
9 9 0
10 10 0
7 10 0.352090
10323904116
10 7 0.352090
10323904116
1 7 0.5572673
3272485
5
7 1 0.5572673
3272485
5
2 3 0.606512
8837978769
3 2 0.606512
8837978769
1 3 0.612823
8020507096
3 1 0.612823
8020507096
5 8 0.6219995
745138945
8 5 0.6219995
745138945
3 10 0.6523185
66254781
6
10 3 0.6523185
66254781
6
9 10 0.67326813
62788765
10 9 0.67326813
62788765
3 7 0.6799892
416547949
7 3 0.6799892
416547949
3 9 0.6820752
294088018
9 3 0.6820752
294088018
2 10 0.6916305
331777215
10 2 0.6916305
331777215
2 9 0.6966650
510789955
9 2 0.6966650
510789955
3 6 0.7102823
580937639
6 3 0.7102823
580937639
2 7 0.7120217
58066697
1
7 2 0.7120217
58066697
1
2 6 0.735161
8106552689
6 2 0.735161
8106552689
1 10 0.73868644
91588024
10 1 0.73868644
91588024
4 6 0.7784357
824370262
6 4 0.7784357
824370262
4 8 0.7795837
407361241
8 4 0.7795837
407361241
4 5 0.8132007
346697969
5 4 0.8132007
346697969
2 4 0.8260925
223296488
4 2 0.8260925
223296488
5 10 0.8286488
93276529
9
10 5 0.8286488
93276529
9
5 9 0.8769351
33306076
8
9 5 0.8769351
33306076
8
1 6 0.8861410
875047832
6 1 0.8861410
875047832
3 5 0.922420
1772876247
5 3 0.922420
1772876247
4 7 0.934791624
6876117
7 4 0.934791624
6876117
7 9 0.9364253
40768525
7
9 7 0.9364253
40768525
7
3 4 0.9757105
842688992
4 3 0.9757105
842688992
1 2 0.98102724
3943351
4
2 1 0.98102724
3943351
4
1 4 0.9965475
544626712
4 1 0.9965475
544626712
5 7 0.9976863
778073342
7 5 0.9976863
778073342
4 10 1.01093459
44029724
10 4 1.01093459
44029724
1 5 1.0208359
400987237
5 1 1.0208359
400987237
6 7 1.0221332
668982412
7 6 1.0221332
668982412
2 5 1.0507693
16594881
5 2 1.0507693
16594881
6 8 1.1034203
81318026
8 6 1.1034203
81318026
3 8 1.1170300
826294572
8 3 1.1170300
826294572
6 10 1.1523451
990991307
10 6 1.1523451
990991307
1 9 1.1637750
565139302
9 1 1.1637750
565139302
2 8 1.173657
1017573874
8 2 1.173657
1017573874
4 9 1.174689
3942711878
9 4 1.174689
3942711878
1 8 1.19099
59973982214
8 1 1.19099
59973982214
8 10 1.2093596
17652948
10 8 1.2093596
17652948
6 9 1.21452987
3940304
9 6 1.21452987
3940304
5 6 1.2272785
0650139
5
6 5 1.2272785
0650139
5
8 9 1.2575258
643523053
9 8 1.2575258
643523053
7 8 1.288239
696195716
8 7 1.288239
696195716
7 10 0.352090
09051322937
10 7 0.352090
09051322937
1 7 0.5572673
08235168
5
7 1 0.5572673
08235168
5
2 3 0.606512
9041671753
3 2 0.606512
9041671753
1 3 0.612823
7843513489
3 1 0.612823
7843513489
5 8 0.6219995
617866516
8 5 0.6219995
617866516
3 10 0.6523185
372352
6
10 3 0.6523185
372352
6
9 10 0.67326813
93623352
10 9 0.67326813
93623352
3 7 0.6799892
18711853
7 3 0.6799892
18711853
3 9 0.6820752
024650574
9 3 0.6820752
024650574
2 10 0.6916305
422782898
10 2 0.6916305
422782898
2 9 0.6966650
485992432
9 2 0.6966650
485992432
3 6 0.7102823
853492737
6 3 0.7102823
853492737
2 7 0.7120217
68093109
1
7 2 0.7120217
68093109
1
2 6 0.735161
7813110352
6 2 0.735161
7813110352
1 10 0.73868644
23751831
10 1 0.73868644
23751831
4 6 0.7784357
666969299
6 4 0.7784357
666969299
4 8 0.7795837
52155304
8 4 0.7795837
52155304
4 5 0.8132007
122039795
5 4 0.8132007
122039795
2 4 0.8260925
41217804
4 2 0.8260925
41217804
5 10 0.8286488
65222930
9
10 5 0.8286488
65222930
9
5 9 0.8769351
24397277
8
9 5 0.8769351
24397277
8
1 6 0.8861410
617828369
6 1 0.8861410
617828369
3 5 0.922420
2036857605
5 3 0.922420
2036857605
4 7 0.934791624
546051
7 4 0.934791624
546051
7 9 0.9364253
28254699
7
9 7 0.9364253
28254699
7
3 4 0.9757105
708122253
4 3 0.9757105
708122253
1 2 0.98102724
5521545
4
2 1 0.98102724
5521545
4
1 4 0.9965475
797653198
4 1 0.9965475
797653198
5 7 0.9976863
861083984
7 5 0.9976863
861083984
4 10 1.01093459
1293335
10 4 1.01093459
1293335
1 5 1.0208359
956741333
5 1 1.0208359
956741333
6 7 1.0221332
31163025
7 6 1.0221332
31163025
2 5 1.0507693
29071045
5 2 1.0507693
29071045
6 8 1.1034203
76777649
8 6 1.1034203
76777649
3 8 1.1170300
245285034
8 3 1.1170300
245285034
6 10 1.1523451
805114746
10 6 1.1523451
805114746
1 9 1.1637750
86402893
9 1 1.1637750
86402893
2 8 1.173657
0596694946
8 2 1.173657
0596694946
4 9 1.174689
4121170044
9 4 1.174689
4121170044
1 8 1.19099
60508346558
8 1 1.19099
60508346558
8 10 1.2093596
458435059
10 8 1.2093596
458435059
6 9 1.21452987
19406128
9 6 1.21452987
19406128
5 6 1.2272785
90202331
5
6 5 1.2272785
90202331
5
8 9 1.2575258
016586304
9 8 1.2575258
016586304
7 8 1.288239
598274231
8 7 1.288239
598274231
flush session status;
select id,vec_distance_euclidean(v, x'B047263c9f87233fcfd27e3eae493e3f0329f43e') d from t1 order by d limit 3;
id d
9 0.4719976
290006591
10 0.506901
1044450041
3 0.5865673
124650332
9 0.4719976
1867523193
10 0.506901
0853767395
3 0.5865673
422813416
show status like 'handler_read_rnd_next';
Variable_name Value
Handler_read_rnd_next 0
select id,vec_distance_euclidean(v, x'B047263c9f87233fcfd27e3eae493e3f0329f43e') d from t1 use index () order by d limit 3;
id d
9 0.4719976
290006591
10 0.506901
1044450041
3 0.5865673
124650332
9 0.4719976
1867523193
10 0.506901
0853767395
3 0.5865673
422813416
show status like 'handler_read_rnd_next';
Variable_name Value
Handler_read_rnd_next 11
flush session status;
select id,vec_distance_cosine(v, x'B047263c9f87233fcfd27e3eae493e3f0329f43e') d from t1 order by d limit 3;
id d
10 0.0590554
6376032378
9 0.06546
887818344715
3 0.1075028
2439505232
10 0.0590554
47578430176
9 0.06546
902656555176
3 0.1075028
7771224976
show status like 'handler_read_rnd_next';
Variable_name Value
Handler_read_rnd_next 11
delete from t1 where v = x'7b713f3e5258323f80d1113d673b2b3f66e3583f';
select id,vec_distance_euclidean(v, x'B047263C9f87233fcfd27e3eae493e3f0329f43e') d from t1 order by d limit 3;
id d
10 0.506901
1044450041
3 0.5865673
124650332
7 0.73444646
9721486
7
10 0.506901
0853767395
3 0.5865673
422813416
7 0.73444646
5969085
7
insert t1 (v) values (x'7b713f3e5258323f80d1113d673b2b3f66e3583f');
select id,vec_distance_euclidean(v, x'b047263c9F87233fcfd27e3eae493e3f0329f43e') d from t1 order by d limit 3;
id d
11 0.4719976
290006591
10 0.506901
1044450041
3 0.5865673
124650332
11 0.4719976
1867523193
10 0.506901
0853767395
3 0.5865673
422813416
select id,vec_distance_euclidean(v, x'B047263c9F87233fcfd27e3eae493e3f0329f43e') d from t1 order by d limit 5;
id d
11 0.4719976
290006591
10 0.506901
1044450041
3 0.5865673
124650332
7 0.73444646
9721486
7
5 0.7671033
52904271
2
11 0.4719976
1867523193
10 0.506901
0853767395
3 0.5865673
422813416
7 0.73444646
5969085
7
5 0.7671033
14399719
2
update t1 set v=x'76EDFC3E4B57243F10F8423FB158713F020BAA3E' where v=x'6CA1D43E9DF91B3FE580DA3E1C247D3F147CF33E';
select id,vec_distance_euclidean(v, x'B047263C9F87233fcfd27e3eae493e3f0329f43e') d from t1 order by d limit 5;
id d
11 0.4719976
290006591
3 0.5865673
124650332
7 0.73444646
9721486
7
10 0.7468367
19209219
5 0.7671033
52904271
2
11 0.4719976
1867523193
3 0.5865673
422813416
7 0.73444646
5969085
7
10 0.7468367
218971252
5 0.7671033
14399719
2
delete from t1;
insert t1 (v) values (x'e360d63ebe554f3fcdbc523f4522193f5236083d'),
(x'f511303f72224a3fdd05fe3eb22a133ffae86a3f'),
...
...
@@ -294,11 +294,11 @@ insert t1 (v) values (x'e360d63ebe554f3fcdbc523f4522193f5236083d'),
(x'6ca1d43e9df91b3fe580da3e1c247d3f147cf33e');
select id,vec_distance_euclidean(v, x'b047263c9f87233Fcfd27e3eae493e3f0329f43e') d from t1 order by d limit 5;
id d
20 0.4719976
290006591
21 0.506901
1044450041
14 0.5865673
124650332
18 0.73444646
9721486
7
16 0.7671033
52904271
2
20 0.4719976
1867523193
21 0.506901
0853767395
14 0.5865673
422813416
18 0.73444646
5969085
7
16 0.7671033
14399719
2
insert t1 (v) values ('');
ERROR 22007: Incorrect vector value: '...' for column `test`.`t1`.`v` at row 1
insert t1 (v) values (x'1234');
...
...
@@ -426,32 +426,32 @@ insert t1 (v) values (x'e360d63ebe554f3fcdbc523f4522193f5236083d'),
(x'6ca1d43e9df91b3fe580da3e1c247d3f147cf33e');
select id,vec_distance_cosine(v, x'B047263c9f87233fcfd27e3eae493e3f0329f43e') d from t1 order by d limit 3;
id d
10 0.0590554
6376032378
9 0.06546
887818344715
3 0.1075028
2439505232
10 0.0590554
47578430176
9 0.06546
902656555176
3 0.1075028
7771224976
flush session status;
select id,vec_distance_cosine(v, x'B047263c9f87233fcfd27e3eae493e3f0329f43e') d from t1 order by d limit 3;
id d
10 0.0590554
6376032378
9 0.06546
887818344715
3 0.1075028
2439505232
10 0.0590554
47578430176
9 0.06546
902656555176
3 0.1075028
7771224976
show status like 'handler_read_rnd_next';
Variable_name Value
Handler_read_rnd_next 0
select id,vec_distance_cosine(v, x'B047263c9f87233fcfd27e3eae493e3f0329f43e') d from t1 use index () order by d limit 3;
id d
10 0.0590554
6376032378
9 0.06546
887818344715
3 0.1075028
2439505232
10 0.0590554
47578430176
9 0.06546
902656555176
3 0.1075028
7771224976
show status like 'handler_read_rnd_next';
Variable_name Value
Handler_read_rnd_next 11
flush session status;
select id,vec_distance_euclidean(v, x'B047263c9f87233fcfd27e3eae493e3f0329f43e') d from t1 order by d limit 3;
id d
9 0.4719976
290006591
10 0.506901
1044450041
3 0.5865673
124650332
9 0.4719976
1867523193
10 0.506901
0853767395
3 0.5865673
422813416
show status like 'handler_read_rnd_next';
Variable_name Value
Handler_read_rnd_next 11
...
...
mysql-test/main/vector_innodb.result
View file @
518f9cb7
...
...
@@ -40,18 +40,18 @@ delete from t1 where id=7;
rollback;
select id,vec_distance_euclidean(v, x'c923e33dc0da313fe7c7983e526b3d3fde63963e6eaf3a3f27fa133fe27a583f') d from t1 order by d limit 5;
id d
2 0.8781474
260354732
10 0.88562083
47761952
30 1.0162643
974895857
7 1.026397
313888122
5 1.030816
1006949719
2 0.8781474
828720093
10 0.88562083
24432373
30 1.0162643
194198608
7 1.026397
2282409668
5 1.030816
0781860352
select id,vec_distance_euclidean(v, x'754b5f3ea2312b3fc169f43e4604883e1d20173e8dd7443f421b703fb11e0d3e') d from t1 order by d limit 5;
id d
2 0.9426904
171992334
33 0.9477554
826856
30 1.1114054
27702547
10 1.1186302
8629234
3
8 1.140573
3350751739
2 0.9426904
320716858
33 0.9477554
559707642
30 1.1114054
918289185
10 1.1186302
9003143
3
8 1.140573
263168335
create table t2 (id int auto_increment primary key, v blob not null, vector index (v)) engine=innodb;
insert t2 (v) values
(x'45cf153f830a313f7a0a113fb1ff533f47a1533fcf9e6e3f'),
...
...
@@ -73,16 +73,16 @@ insert t2 values
commit;
select id,vec_distance_euclidean(v, x'1f4d053f7056493f937da03dd8c97a3f220cbb3c926c1c3facca213ec0618a3e') d from t1 order by d limit 5;
id d
6 0.9309383
181777582
5 0.970630466
2574956
30 0.981444
92002831
50 1.079862
635421575
2 1.090713
8991979892
6 0.9309383
034706116
5 0.970630466
9380188
30 0.981444
8952674866
50 1.079862
5946044922
2 1.090713
9778137207
select id,vec_distance_euclidean(v, x'f618663f256be73e62cd453f8bcdbf3e16ae503c3858313f') d from t2 order by d limit 5;
id d
21 0.435591
80321379337
20 0.6435053
022072372
6 0.6942000
623336242
2 0.7971622
099055623
9 0.829858
9136476077
21 0.435591
78709983826
20 0.6435053
34854126
6 0.6942000
389099121
2 0.7971622
347831726
9 0.829858
8991165161
drop table t1, t2;
sql/item_vectorfunc.cc
View file @
518f9cb7
...
...
@@ -215,3 +215,23 @@ String *Item_func_vec_fromtext::val_str(String *buf)
null_value
=
true
;
return
nullptr
;
}
#include <eigen3/Eigen/Dense>
using
namespace
Eigen
;
double
Item_func_vec_distance_euclidean
::
calc_distance
(
float
*
data1
,
float
*
data2
,
size_t
d_len
)
{
Map
<
VectorXf
>
v1
(
data1
,
d_len
);
Map
<
VectorXf
>
v2
(
data2
,
d_len
);
return
(
v1
-
v2
).
norm
();
}
double
Item_func_vec_distance_cosine
::
calc_distance
(
float
*
data1
,
float
*
data2
,
size_t
d_len
)
{
Map
<
VectorXf
>
v1
(
data1
,
d_len
);
Map
<
VectorXf
>
v2
(
data2
,
d_len
);
return
1.0
f
-
v1
.
dot
(
v2
)
/
v1
.
norm
()
/
v2
.
norm
();
}
sql/item_vectorfunc.h
View file @
518f9cb7
...
...
@@ -61,16 +61,7 @@ class Item_func_vec_distance_common: public Item_real_func
class
Item_func_vec_distance_euclidean
:
public
Item_func_vec_distance_common
{
double
calc_distance
(
float
*
v1
,
float
*
v2
,
size_t
v_len
)
override
{
double
d
=
0
;
for
(
size_t
i
=
0
;
i
<
v_len
;
i
++
,
v1
++
,
v2
++
)
{
float
dist
=
*
v1
-
*
v2
;
d
+=
dist
*
dist
;
}
return
sqrt
(
d
);
}
double
calc_distance
(
float
*
v1
,
float
*
v2
,
size_t
v_len
)
override
;
public:
Item_func_vec_distance_euclidean
(
THD
*
thd
,
Item
*
a
,
Item
*
b
)
...
...
@@ -87,17 +78,7 @@ class Item_func_vec_distance_euclidean: public Item_func_vec_distance_common
class
Item_func_vec_distance_cosine
:
public
Item_func_vec_distance_common
{
double
calc_distance
(
float
*
v1
,
float
*
v2
,
size_t
v_len
)
override
{
double
dotp
=
0
,
abs1
=
0
,
abs2
=
0
;
for
(
size_t
i
=
0
;
i
<
v_len
;
i
++
,
v1
++
,
v2
++
)
{
abs1
+=
*
v1
*
*
v1
;
abs2
+=
*
v2
*
*
v2
;
dotp
+=
*
v1
*
*
v2
;
}
return
1
-
dotp
/
sqrt
(
abs1
*
abs2
);
}
double
calc_distance
(
float
*
v1
,
float
*
v2
,
size_t
v_len
)
override
;
public:
Item_func_vec_distance_cosine
(
THD
*
thd
,
Item
*
a
,
Item
*
b
)
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment