Skip to content

Commit 6aba81d

Browse files
saumitrpfacebook-github-bot
authored andcommitted
Add support for HAVING clause with vector searches
Summary: As titled Differential Revision: D55833702 fbshipit-source-id: d966281
1 parent 40f4f32 commit 6aba81d

File tree

3 files changed

+22
-9
lines changed

3 files changed

+22
-9
lines changed

mysql-test/suite/fb_vectordb/r/vectordb_orderby.result

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -50,23 +50,23 @@ id select_type table partitions type possible_keys key key_len ref rows filtered
5050
1 SIMPLE t1 NULL ALL NULL NULL NULL NULL 1 100.00 Using filesort
5151

5252

53-
3. Verify that the optimizer does not choose the vector index without
53+
3. Verify that the optimizer DOES choose the vector index EVEN without
5454
the LIMIT clause for the above 3 cases
5555

5656
Case A. SELECT *
5757
explain select * from t1 order by FB_VECTOR_L2(vector1, '[2, 1, 1]');
5858
id select_type table partitions type possible_keys key key_len ref rows filtered Extra
59-
1 SIMPLE t1 NULL ALL NULL NULL NULL NULL 1 100.00 Using filesort
59+
1 SIMPLE t1 NULL index NULL vector_key_1 65536 NULL 1 100.00 NULL
6060

6161
Case B. SELECT <vector index field>
6262
explain select vector1 from t1 order by FB_VECTOR_L2(vector1, '[2, 1, 1]');
6363
id select_type table partitions type possible_keys key key_len ref rows filtered Extra
64-
1 SIMPLE t1 NULL ALL NULL NULL NULL NULL 1 100.00 Using filesort
64+
1 SIMPLE t1 NULL index NULL vector_key_1 65536 NULL 1 100.00 NULL
6565

6666
Case C. SELECT <primary key field>
6767
explain select id from t1 order by FB_VECTOR_L2(vector1, '[2, 1, 1]');
6868
id select_type table partitions type possible_keys key key_len ref rows filtered Extra
69-
1 SIMPLE t1 NULL ALL NULL NULL NULL NULL 1 100.00 Using filesort
69+
1 SIMPLE t1 NULL index NULL vector_key_1 65536 NULL 1 100.00 NULL
7070

7171

7272
4. Verify that the optimizer chooses the vector index for an ORDER BY with
@@ -126,25 +126,34 @@ id select_type table partitions type possible_keys key key_len ref rows filtered
126126
1 SIMPLE t1 NULL ALL NULL NULL NULL NULL 1 100.00 Using filesort
127127

128128

129-
6. Other scenarios - Vector dist fn as part of SELECT fields
129+
7. Other scenarios - Vector dist fn as part of SELECT fields
130130

131131
Case A. SELECT *
132132
explain select *, FB_VECTOR_L2(vector1, '[2, 1, 1]') as dis from t1 order by dis limit 2;
133133
id select_type table partitions type possible_keys key key_len ref rows filtered Extra
134134
1 SIMPLE t1 NULL index NULL vector_key_1 65536 NULL 1 100.00 NULL
135+
explain select *, FB_VECTOR_L2(vector1, '[2, 1, 1]') as dis from t1 having dis < 7 order by dis limit 2;
136+
id select_type table partitions type possible_keys key key_len ref rows filtered Extra
137+
1 SIMPLE t1 NULL index NULL vector_key_1 65536 NULL 1 100.00 Using where
135138

136139
Case B. SELECT <vector index field>
137140
explain select vector1, FB_VECTOR_L2(vector1, '[2, 1, 1]') as dis from t1 order by dis limit 2;
138141
id select_type table partitions type possible_keys key key_len ref rows filtered Extra
139142
1 SIMPLE t1 NULL index NULL vector_key_1 65536 NULL 1 100.00 NULL
143+
explain select vector1, FB_VECTOR_L2(vector1, '[2, 1, 1]') as dis from t1 having dis < 7 order by dis limit 2;
144+
id select_type table partitions type possible_keys key key_len ref rows filtered Extra
145+
1 SIMPLE t1 NULL index NULL vector_key_1 65536 NULL 1 100.00 Using where
140146

141147
Case C. SELECT <primary key field>
142148
explain select id, FB_VECTOR_L2(vector1, '[2, 1, 1]') as dis from t1 order by dis limit 2;
143149
id select_type table partitions type possible_keys key key_len ref rows filtered Extra
144150
1 SIMPLE t1 NULL index NULL vector_key_1 65536 NULL 1 100.00 NULL
151+
explain select id, FB_VECTOR_L2(vector1, '[2, 1, 1]') as dis from t1 having dis < 7 order by dis limit 2;
152+
id select_type table partitions type possible_keys key key_len ref rows filtered Extra
153+
1 SIMPLE t1 NULL index NULL vector_key_1 65536 NULL 1 100.00 Using where
145154

146155

147-
7. Test ORDER BY and LIMIT pushdown to MyRocks by looking at output of Iterators
156+
8. Test ORDER BY and LIMIT pushdown to MyRocks by looking at output of Iterators
148157

149158
Case A. SELECT *
150159
select * from t1 order by FB_VECTOR_L2(vector1, '[1, 2, 3]') limit 2;

mysql-test/suite/fb_vectordb/t/vectordb_orderby.test

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ explain select id from t1 order by FB_VECTOR_L2('[2, 1, 1]', vector1) limit 2;
5757

5858
--echo
5959
--echo
60-
--echo 3. Verify that the optimizer does not choose the vector index without
60+
--echo 3. Verify that the optimizer DOES choose the vector index EVEN without
6161
--echo the LIMIT clause for the above 3 cases
6262
--echo
6363

@@ -137,28 +137,31 @@ explain select id from t1 order by FB_VECTOR_L2(vector1, vector1) limit 2;
137137

138138
--echo
139139
--echo
140-
--echo 6. Other scenarios - Vector dist fn as part of SELECT fields
140+
--echo 7. Other scenarios - Vector dist fn as part of SELECT fields
141141
--echo
142142

143143
--echo Case A. SELECT *
144144

145145
explain select *, FB_VECTOR_L2(vector1, '[2, 1, 1]') as dis from t1 order by dis limit 2;
146+
explain select *, FB_VECTOR_L2(vector1, '[2, 1, 1]') as dis from t1 having dis < 7 order by dis limit 2;
146147

147148
--echo
148149
--echo Case B. SELECT <vector index field>
149150

150151
explain select vector1, FB_VECTOR_L2(vector1, '[2, 1, 1]') as dis from t1 order by dis limit 2;
152+
explain select vector1, FB_VECTOR_L2(vector1, '[2, 1, 1]') as dis from t1 having dis < 7 order by dis limit 2;
151153

152154
--echo
153155
--echo Case C. SELECT <primary key field>
154156

155157
explain select id, FB_VECTOR_L2(vector1, '[2, 1, 1]') as dis from t1 order by dis limit 2;
158+
explain select id, FB_VECTOR_L2(vector1, '[2, 1, 1]') as dis from t1 having dis < 7 order by dis limit 2;
156159

157160
enable_warnings;
158161

159162
--echo
160163
--echo
161-
--echo 7. Test ORDER BY and LIMIT pushdown to MyRocks by looking at output of Iterators
164+
--echo 8. Test ORDER BY and LIMIT pushdown to MyRocks by looking at output of Iterators
162165
--echo
163166

164167
--echo Case A. SELECT *

sql/sql_select.cc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5071,6 +5071,7 @@ bool test_if_cheaper_ordering(const JOIN_TAB *tab, ORDER_with_src *order,
50715071
queries too.
50725072
*/
50735073
if (is_covering || select_limit != HA_POS_ERROR ||
5074+
(table->file->index_supports_vector_scan(order->order, nr)) ||
50745075
(ref_key < 0 && (group || table->force_index_order))) {
50755076
rec_per_key_t rec_per_key;
50765077
KEY *keyinfo = table->key_info + nr;

0 commit comments

Comments
 (0)