fix(rule): fire HNSW rewrite through SubqueryAlias - #31
Merged
Conversation
Table-aliasing SQL clients (ibis/SQLGlot, ORMs, BI tools) alias every table and wrap projections in subqueries. DataFusion does not eliminate these SubqueryAlias nodes when the alias is referenced downstream, and USearchRule only descended through Projection/Filter, so it silently fell back to brute-force search for any aliased query. - peel_alias/peel_alias_capturing see through SubqueryAlias at each matcher entry point, and capture the outermost alias name. - The rewrite still resolves internally against the real table reference (so it type-checks against USearchNode's own schema), then re-wraps in the same SubqueryAlias so the exposed schema matches the original qualifiers exactly, with a schema-equality safety net that declines rather than emit a mismatched plan DataFusion would reject. - A single alias directly on the scan with no outer wrap leaves the original schema with mixed qualifiers that can't be reproduced this way; the rule declines safely there, same as pre-fix behavior. Verified against the actual ibis-compiled SQL (byte-for-byte via a live ibis 12.0.0 instance) and against a real RuntimeDB build/deployment with real vector data. Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
There was a problem hiding this comment.
Reviewed the SubqueryAlias-peeling logic in src/rule.rs. The design is sound: peeling sees through alias chains at each matcher entry point, the rewrite resolves against the real table reference, re-wraps in the captured outer alias, and the trailing schema-equality check declines cleanly for any shape it can't reproduce exactly (no invalid plan reaches DataFusion's invariant check). Verified the filter-predicate qualifier is a non-issue since the planner strips qualifiers before compiling physical filters, and that the rewritten plan can't re-fire. Tests cover both the fire and decline paths, including real execution against ibis-compiled SQL.
anoop-narang
approved these changes
Jul 21, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
SubqueryAliasnodes when the alias is referenced downstream, andUSearchRuleonly descended throughProjection/Filter— so it silently declined and fell back to brute-force search for any aliased query, including ibis's actual generated SQL.peel_alias/peel_alias_capturingsee throughSubqueryAliasat each matcher entry point. The rewrite still resolves internally against the real table reference (so it type-checks againstUSearchNode's own schema), then re-wraps the result in the same outer alias so the exposed schema matches the original qualifiers exactly. A trailing schema-equality check declines cleanly (no rewrite, exact fallback) rather than emit a plan DataFusion's post-optimizer invariant check would reject.Sortleaves the original schema with mixed qualifiers (one column tied to the alias, another unqualified) that can't be reproduced by a singleProjection/SubqueryAlias. The rule declines safely there — identical to pre-fix behavior for that shape, just reached via a different path. Documented intests/subquery_alias.rs.Test plan
cargo fmt --check,cargo clippy --all-targets -- -D warnings,cargo test --features sqlite-provider— all clean (96 tests).tests/subquery_alias.rs— 7 tests covering the double-alias (ibis) shape, single-alias + WHERE, vector-in-output decline, and metric-mismatch decline.tests/execution.rs(exec_ibis_shape_double_alias_*) run the actual ibis-compiled SQL (verified byte-for-byte against a liveibis12.0.0 instance compiling thesemantic_searchhelper sketch) against real vector data, asserting correct nearest-neighbor results — not just plan shape.runtimedb'sCargo.tomlto this branch locally, built the Linux release binary, built/loaded theruntimedb:localimage into the local kind cluster, rolled out the deployment, and ran the exact ibis-shaped double-SubqueryAliasquery through the live HTTP API against a real uploaded table + cosine/L2 index.EXPLAINshowedUSearchExecin the physical plan andSubqueryAlias: t1correctly wrapping theUSearchlogical node; the real query returned correct results (closest row at distance0.0).🤖 Generated with Claude Code