-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathoptimizer_rule.rs
More file actions
421 lines (375 loc) · 18.1 KB
/
optimizer_rule.rs
File metadata and controls
421 lines (375 loc) · 18.1 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
// tests/usearch_optimizer_rule.rs — Tests for USearchRule metric mismatch guard
// and sort-direction check.
//
// Setup: a synthetic "items" table (id: UInt64, vector: FixedSizeList<f32, 4>)
// is registered in USearchRegistry with a specific MetricKind and in the
// DataFusion SessionContext via HashKeyProvider (which also implements
// TableProvider, so no separate MemTable is needed).
//
// Tests only call into_optimized_plan() — physical planning never runs, so we
// don't need a USearchQueryPlanner or a populated index.
//
// Assertions: USearchNode present in the optimized plan ↔ rule fired.
use std::sync::Arc;
use arrow_schema::{DataType, Field, Schema};
use datafusion::logical_expr::LogicalPlan;
use datafusion::prelude::SessionContext;
use usearch::{Index, IndexOptions, MetricKind, ScalarKind};
use datafusion_vector_search_ext::{HashKeyProvider, USearchNode, USearchRegistry, register_all};
// ── Schema ────────────────────────────────────────────────────────────────────
fn items_schema() -> Arc<Schema> {
Arc::new(Schema::new(vec![
Field::new("id", DataType::UInt64, false),
Field::new(
"vector",
DataType::FixedSizeList(Arc::new(Field::new("item", DataType::Float32, true)), 4),
false,
),
]))
}
// ── Helpers ───────────────────────────────────────────────────────────────────
fn make_index(metric: MetricKind) -> Arc<Index> {
let options = IndexOptions {
dimensions: 4,
metric,
quantization: ScalarKind::F32,
..Default::default()
};
Arc::new(Index::new(&options).expect("usearch Index::new failed"))
}
/// Build a SessionContext with a USearch index registered under "items" using
/// the given MetricKind. Uses a plain SessionContext — no USearchQueryPlanner
/// needed because we only inspect the optimized logical plan (never .collect()).
async fn make_ctx(metric: MetricKind) -> SessionContext {
let schema = items_schema();
// Empty provider — no rows needed; we only test the optimizer rule.
let provider = Arc::new(
HashKeyProvider::try_new(schema.clone(), vec![], "id")
.expect("HashKeyProvider::try_new failed"),
);
let reg = USearchRegistry::new();
reg.add(
"items::vector",
make_index(metric),
provider.clone(),
provider.clone(),
"id",
metric,
ScalarKind::F32,
)
.expect("USearchRegistry::add failed");
let registry = reg.into_arc();
let ctx = SessionContext::default();
register_all(&ctx, registry).expect("register_all failed");
// HashKeyProvider also implements TableProvider, so register it directly
// for SQL column-name resolution — no separate MemTable needed.
ctx.register_table("items", provider)
.expect("register_table failed");
ctx
}
async fn optimized_plan(ctx: &SessionContext, sql: &str) -> LogicalPlan {
ctx.sql(sql)
.await
.unwrap_or_else(|e| panic!("SQL parse/analysis failed: {e}\nSQL: {sql}"))
.into_optimized_plan()
.unwrap_or_else(|e| panic!("Optimization failed: {e}\nSQL: {sql}"))
}
fn contains_usearch_node(plan: &LogicalPlan) -> bool {
if let LogicalPlan::Extension(ext) = plan
&& ext.node.as_any().downcast_ref::<USearchNode>().is_some()
{
return true;
}
plan.inputs()
.iter()
.any(|child| contains_usearch_node(child))
}
const Q: &str = "ARRAY[0.1, 0.2, 0.3, 0.4]";
// ═══════════════════════════════════════════════════════════════════════════════
// MATCHING METRIC — rule must fire
// ═══════════════════════════════════════════════════════════════════════════════
#[tokio::test]
async fn test_l2sq_index_l2_udf_rewrites() {
let ctx = make_ctx(MetricKind::L2sq).await;
let sql = format!("SELECT id, l2_distance(vector, {Q}) AS d FROM items ORDER BY d ASC LIMIT 5");
let plan = optimized_plan(&ctx, &sql).await;
assert!(
contains_usearch_node(&plan),
"L2sq index + l2_distance ASC → rule must fire"
);
}
#[tokio::test]
async fn test_cos_index_cosine_udf_rewrites() {
let ctx = make_ctx(MetricKind::Cos).await;
let sql =
format!("SELECT id, cosine_distance(vector, {Q}) AS d FROM items ORDER BY d ASC LIMIT 5");
let plan = optimized_plan(&ctx, &sql).await;
assert!(
contains_usearch_node(&plan),
"Cos index + cosine_distance ASC → rule must fire"
);
}
#[tokio::test]
async fn test_ip_index_negative_dot_udf_rewrites() {
let ctx = make_ctx(MetricKind::IP).await;
let sql = format!(
"SELECT id, negative_dot_product(vector, {Q}) AS d FROM items ORDER BY d ASC LIMIT 5"
);
let plan = optimized_plan(&ctx, &sql).await;
assert!(
contains_usearch_node(&plan),
"IP index + negative_dot_product ASC → rule must fire"
);
}
// ═══════════════════════════════════════════════════════════════════════════════
// MISMATCHING METRIC — rule must NOT fire; query falls back to exact scan
// ═══════════════════════════════════════════════════════════════════════════════
#[tokio::test]
async fn test_l2sq_index_cosine_udf_no_rewrite() {
let ctx = make_ctx(MetricKind::L2sq).await;
let sql =
format!("SELECT id, cosine_distance(vector, {Q}) AS d FROM items ORDER BY d ASC LIMIT 5");
let plan = optimized_plan(&ctx, &sql).await;
assert!(
!contains_usearch_node(&plan),
"L2sq index + cosine_distance → metric mismatch, rule must not fire"
);
}
#[tokio::test]
async fn test_l2sq_index_negative_dot_udf_no_rewrite() {
let ctx = make_ctx(MetricKind::L2sq).await;
let sql = format!(
"SELECT id, negative_dot_product(vector, {Q}) AS d FROM items ORDER BY d ASC LIMIT 5"
);
let plan = optimized_plan(&ctx, &sql).await;
assert!(
!contains_usearch_node(&plan),
"L2sq index + negative_dot_product → metric mismatch, rule must not fire"
);
}
#[tokio::test]
async fn test_cos_index_l2_udf_no_rewrite() {
let ctx = make_ctx(MetricKind::Cos).await;
let sql = format!("SELECT id, l2_distance(vector, {Q}) AS d FROM items ORDER BY d ASC LIMIT 5");
let plan = optimized_plan(&ctx, &sql).await;
assert!(
!contains_usearch_node(&plan),
"Cos index + l2_distance → metric mismatch, rule must not fire"
);
}
#[tokio::test]
async fn test_cos_index_negative_dot_udf_no_rewrite() {
let ctx = make_ctx(MetricKind::Cos).await;
let sql = format!(
"SELECT id, negative_dot_product(vector, {Q}) AS d FROM items ORDER BY d ASC LIMIT 5"
);
let plan = optimized_plan(&ctx, &sql).await;
assert!(
!contains_usearch_node(&plan),
"Cos index + negative_dot_product → metric mismatch, rule must not fire"
);
}
#[tokio::test]
async fn test_ip_index_l2_udf_no_rewrite() {
let ctx = make_ctx(MetricKind::IP).await;
let sql = format!("SELECT id, l2_distance(vector, {Q}) AS d FROM items ORDER BY d ASC LIMIT 5");
let plan = optimized_plan(&ctx, &sql).await;
assert!(
!contains_usearch_node(&plan),
"IP index + l2_distance → metric mismatch, rule must not fire"
);
}
#[tokio::test]
async fn test_ip_index_cosine_udf_no_rewrite() {
let ctx = make_ctx(MetricKind::IP).await;
let sql =
format!("SELECT id, cosine_distance(vector, {Q}) AS d FROM items ORDER BY d ASC LIMIT 5");
let plan = optimized_plan(&ctx, &sql).await;
assert!(
!contains_usearch_node(&plan),
"IP index + cosine_distance → metric mismatch, rule must not fire"
);
}
// ═══════════════════════════════════════════════════════════════════════════════
// SELECT * (no Projection node) — rule must fire
// ═══════════════════════════════════════════════════════════════════════════════
/// DataFusion 51 omits the Projection node for SELECT * queries.
/// The rule must match Sort→TableScan directly (no Projection between them).
#[tokio::test]
async fn test_select_star_l2_rewrites() {
let ctx = make_ctx(MetricKind::L2sq).await;
let sql = format!("SELECT * FROM items ORDER BY l2_distance(vector, {Q}) ASC LIMIT 5");
let plan = optimized_plan(&ctx, &sql).await;
assert!(
contains_usearch_node(&plan),
"SELECT * + l2_distance ASC (no Projection) → rule must fire\nPlan: {plan:?}"
);
}
#[tokio::test]
async fn test_select_star_cosine_rewrites() {
let ctx = make_ctx(MetricKind::Cos).await;
let sql = format!("SELECT * FROM items ORDER BY cosine_distance(vector, {Q}) ASC LIMIT 5");
let plan = optimized_plan(&ctx, &sql).await;
assert!(
contains_usearch_node(&plan),
"SELECT * + cosine_distance ASC (no Projection) → rule must fire\nPlan: {plan:?}"
);
}
// ═══════════════════════════════════════════════════════════════════════════════
// DESC SORT — rule must NOT fire even when metric matches
// ═══════════════════════════════════════════════════════════════════════════════
/// ORDER BY distance DESC asks for the *least* similar vectors — the ANN index
/// cannot answer that efficiently. The guard must reject it.
#[tokio::test]
async fn test_desc_sort_no_rewrite() {
let ctx = make_ctx(MetricKind::L2sq).await;
let sql =
format!("SELECT id, l2_distance(vector, {Q}) AS d FROM items ORDER BY d DESC LIMIT 5");
let plan = optimized_plan(&ctx, &sql).await;
assert!(
!contains_usearch_node(&plan),
"l2_distance DESC (metric matches but wrong direction) → rule must not fire"
);
}
// ═══════════════════════════════════════════════════════════════════════════════
// ORDER BY UDF directly — no distance alias in SELECT
// ═══════════════════════════════════════════════════════════════════════════════
/// SELECT projects only non-distance columns; ORDER BY uses the UDF expression
/// directly. The rule must still match because find_distance_info resolves the
/// sort expression without requiring it to appear in the projection list.
#[tokio::test]
async fn test_order_by_udf_direct_no_dist_in_select_rewrites() {
let ctx = make_ctx(MetricKind::L2sq).await;
let sql = format!("SELECT id FROM items ORDER BY l2_distance(vector, {Q}) ASC LIMIT 5");
let plan = optimized_plan(&ctx, &sql).await;
assert!(
contains_usearch_node(&plan),
"ORDER BY UDF (dist not projected) → rule must fire\nPlan: {plan:?}"
);
}
// ═══════════════════════════════════════════════════════════════════════════════
// WHERE clause — filter predicate absorbed into USearchNode
// ═══════════════════════════════════════════════════════════════════════════════
#[tokio::test]
async fn test_where_clause_absorbed_rewrites() {
let ctx = make_ctx(MetricKind::L2sq).await;
let sql =
format!("SELECT id FROM items WHERE id > 10 ORDER BY l2_distance(vector, {Q}) ASC LIMIT 5");
let plan = optimized_plan(&ctx, &sql).await;
assert!(
contains_usearch_node(&plan),
"WHERE clause → Filter(TableScan) absorbed into USearchNode, rule must fire\nPlan: {plan:?}"
);
}
#[tokio::test]
async fn test_where_clause_with_alias_order_by_rewrites() {
let ctx = make_ctx(MetricKind::L2sq).await;
let sql = format!(
"SELECT id, l2_distance(vector, {Q}) AS dist FROM items WHERE id > 5 ORDER BY dist ASC LIMIT 3"
);
let plan = optimized_plan(&ctx, &sql).await;
assert!(
contains_usearch_node(&plan),
"WHERE clause + alias ORDER BY → rule must fire\nPlan: {plan:?}"
);
}
// ═══════════════════════════════════════════════════════════════════════════════
// No LIMIT — rule must NOT fire (k is unknown)
// ═══════════════════════════════════════════════════════════════════════════════
#[tokio::test]
async fn test_no_limit_no_rewrite() {
let ctx = make_ctx(MetricKind::L2sq).await;
let sql = format!("SELECT id FROM items ORDER BY l2_distance(vector, {Q}) ASC");
let plan = optimized_plan(&ctx, &sql).await;
assert!(
!contains_usearch_node(&plan),
"no LIMIT → k is unknown, rule must not fire\nPlan: {plan:?}"
);
}
// ═══════════════════════════════════════════════════════════════════════════════
// Fully-qualified table reference (catalog.schema.table)
// ═══════════════════════════════════════════════════════════════════════════════
//
// When SQL uses `datafusion.public.items`, the TableScan carries a Full
// TableReference. Before the schema-qualifier fix, USearchNode's DFSchema was
// always built with a Bare reference, causing a post-optimizer schema mismatch
// whenever the query projected the alias into the SELECT list.
/// Register index under the fully-qualified key matching `datafusion.public.items`.
async fn make_ctx_qualified(metric: MetricKind) -> SessionContext {
let schema = items_schema();
let provider = Arc::new(
HashKeyProvider::try_new(schema.clone(), vec![], "id")
.expect("HashKeyProvider::try_new failed"),
);
let reg = USearchRegistry::new();
// Key must match table_ref_to_str(Full{catalog,schema,table}) + "::" + col.
reg.add(
"datafusion::public::items::vector",
make_index(metric),
provider.clone(),
provider.clone(),
"id",
metric,
ScalarKind::F32,
)
.expect("USearchRegistry::add failed");
let registry = reg.into_arc();
let ctx = SessionContext::default();
register_all(&ctx, registry).expect("register_all failed");
ctx.register_table("items", provider)
.expect("register_table failed");
ctx
}
/// ORDER BY UDF inline, fully-qualified table — rule must fire.
#[tokio::test]
async fn test_qualified_ref_order_by_udf_rewrites() {
let ctx = make_ctx_qualified(MetricKind::L2sq).await;
let sql = format!(
"SELECT id FROM datafusion.public.items ORDER BY l2_distance(vector, {Q}) ASC LIMIT 5"
);
let plan = optimized_plan(&ctx, &sql).await;
assert!(
contains_usearch_node(&plan),
"qualified ref + ORDER BY UDF → rule must fire\nPlan: {plan:?}"
);
}
/// ORDER BY alias, fully-qualified table — previously crashed with schema
/// qualifier mismatch. This is the regression test for that bug.
#[tokio::test]
async fn test_qualified_ref_order_by_alias_rewrites() {
let ctx = make_ctx_qualified(MetricKind::L2sq).await;
let sql = format!(
"SELECT id, l2_distance(vector, {Q}) AS dist FROM datafusion.public.items ORDER BY dist ASC LIMIT 5"
);
let plan = optimized_plan(&ctx, &sql).await;
assert!(
contains_usearch_node(&plan),
"qualified ref + ORDER BY alias → schema qualifier must be preserved, rule must fire\nPlan: {plan:?}"
);
}
/// SELECT * with fully-qualified table — rule must fire.
#[tokio::test]
async fn test_qualified_ref_select_star_rewrites() {
let ctx = make_ctx_qualified(MetricKind::L2sq).await;
let sql = format!(
"SELECT * FROM datafusion.public.items ORDER BY l2_distance(vector, {Q}) ASC LIMIT 5"
);
let plan = optimized_plan(&ctx, &sql).await;
assert!(
contains_usearch_node(&plan),
"qualified ref + SELECT * → rule must fire\nPlan: {plan:?}"
);
}
/// WHERE clause with fully-qualified table — filter absorbed, rule must fire.
#[tokio::test]
async fn test_qualified_ref_where_clause_rewrites() {
let ctx = make_ctx_qualified(MetricKind::L2sq).await;
let sql = format!(
"SELECT id FROM datafusion.public.items WHERE id > 0 ORDER BY l2_distance(vector, {Q}) ASC LIMIT 5"
);
let plan = optimized_plan(&ctx, &sql).await;
assert!(
contains_usearch_node(&plan),
"qualified ref + WHERE → filter absorbed, rule must fire\nPlan: {plan:?}"
);
}