Skip to content

fix(rule): fire HNSW rewrite through SubqueryAlias - #31

Merged
rohan-hotdata merged 1 commit into
mainfrom
fix/subquery-alias-rewrite
Jul 21, 2026
Merged

fix(rule): fire HNSW rewrite through SubqueryAlias#31
rohan-hotdata merged 1 commit into
mainfrom
fix/subquery-alias-rewrite

Conversation

@rohan-hotdata

Copy link
Copy Markdown
Contributor

Summary

  • Table-aliasing SQL clients (ibis/SQLGlot, ORMs, BI tools) alias every table and wrap projections in subqueries. DataFusion keeps these SubqueryAlias nodes when the alias is referenced downstream, and USearchRule only descended through Projection/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_capturing see through SubqueryAlias at each matcher entry point. The rewrite still resolves internally against the real table reference (so it type-checks against USearchNode'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.
  • One shape is a known, safe limitation: a single alias directly on the scan with no outer wrap over the Sort leaves the original schema with mixed qualifiers (one column tied to the alias, another unqualified) that can't be reproduced by a single Projection/SubqueryAlias. The rule declines safely there — identical to pre-fix behavior for that shape, just reached via a different path. Documented in tests/subquery_alias.rs.

Test plan

  • cargo fmt --check, cargo clippy --all-targets -- -D warnings, cargo test --features sqlite-provider — all clean (96 tests).
  • New tests/subquery_alias.rs — 7 tests covering the double-alias (ibis) shape, single-alias + WHERE, vector-in-output decline, and metric-mismatch decline.
  • New tests in tests/execution.rs (exec_ibis_shape_double_alias_*) run the actual ibis-compiled SQL (verified byte-for-byte against a live ibis 12.0.0 instance compiling the semantic_search helper sketch) against real vector data, asserting correct nearest-neighbor results — not just plan shape.
  • Verified against a real RuntimeDB build: patched runtimedb's Cargo.toml to this branch locally, built the Linux release binary, built/loaded the runtimedb:local image into the local kind cluster, rolled out the deployment, and ran the exact ibis-shaped double-SubqueryAlias query through the live HTTP API against a real uploaded table + cosine/L2 index. EXPLAIN showed USearchExec in the physical plan and SubqueryAlias: t1 correctly wrapping the USearch logical node; the real query returned correct results (closest row at distance 0.0).

🤖 Generated with Claude Code

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>
@rohan-hotdata
rohan-hotdata requested a review from a team as a code owner July 21, 2026 05:30
@rohan-hotdata
rohan-hotdata requested review from eddietejeda and removed request for a team July 21, 2026 05:30

@claude claude Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

@rohan-hotdata
rohan-hotdata merged commit a76c298 into main Jul 21, 2026
6 checks passed
@rohan-hotdata
rohan-hotdata deleted the fix/subquery-alias-rewrite branch July 21, 2026 08:53
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants