Skip to content

feat(ir): add a query-DAG JSON exporter + interactive viewer - #134

Closed
zzylol wants to merge 3 commits into
mainfrom
query-dag-viewer
Closed

zzylol wants to merge 3 commits into
mainfrom
query-dag-viewer

Conversation

@zzylol

@zzylol zzylol commented Jul 15, 2026

Copy link
Copy Markdown
Contributor

Summary

Closes #133 (a query DAG visualization for manual code review/debugging, common query pattern analysis, and — later — CSE analysis, in the spirit of Hamna's bgp-query-dag-explorer).

  • crates/ir/src/dag_export.rs — flattens QueryExpr into a generic node/edge graph (exhaustive match over every variant), with a bottom-up structural hash per node so identical subtrees across queries can be spotted cheaply.
  • crates/lower/examples/dag_export.rs — lowers one or more --sql/--promql queries and prints a WorkloadGraph as JSON (cargo run -p asap-lower --example dag_export -- --sql "..." --name q1 > dag.json).
  • tools/dag-viewer/ — a self-contained, offline HTML viewer (vendored Cytoscape.js + dagre, MIT-licensed, no CDN) with drag-and-drop JSON loading, per-query tabs, a node-detail inspector, and shared-subtree highlighting.

QueryExpr already fully derived Serialize, so the exporter is a thin flattening layer, not a new data model. serde_json moved from a dev-dependency to a real dependency of asap-ir since the exporter needs it at runtime.

Explicitly out of scope (see doc comments / the viewer's README): the shared-subtree highlighting is a client-side structural-hash proxy, not real CSE — asap_plan::cse::dedupe_subtrees/CseWorkloadPlan isn't wired into any end-to-end multi-root planning pipeline today (no caller outside its own unit tests). Wiring that up is a separate, larger effort worth its own issue; once it lands, this viewer is a natural place to render its bindings/Ref output as real converging DAG edges instead of this proxy.

Test plan

  • cargo test -p asap-ir — new dag_export unit tests (leaf, chain, n-ary Merge, hash-equality/inequality across identical/differing subtrees) pass; full existing suite still green (41 passed).
  • cargo build/test/clippy --workspace and cargo fmt --check all clean.
  • Drove the viewer end-to-end in a real Chromium instance (Playwright): loaded a 3-query export, confirmed tab switching, node-click → detail panel, and that the shared Scan node between two queries is correctly outlined and cross-referenced.

🤖 Generated with Claude Code

Flatten QueryExpr into a generic node/edge graph with a bottom-up
structural hash per node, add a CLI example to lower SQL/PromQL and
dump the graph as JSON, and add a self-contained offline HTML viewer
(vendored Cytoscape.js + dagre) for manual IR review/debugging and
cross-query pattern/shared-subtree inspection.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
@zzylol

zzylol commented Jul 15, 2026

Copy link
Copy Markdown
Contributor Author

@milindsrivastava1997 FYI. I haven't checked how it works lol

@zzylol zzylol changed the title Add a query-DAG JSON exporter + interactive viewer feat(ir): add a query-DAG JSON exporter + interactive viewer Jul 15, 2026
zzylol and others added 2 commits August 12, 2026 11:51
Aggregate::by (GroupKeys) was renamed to Aggregate::reduction
(Reduction) on main after this branch forked, breaking dag_export's
exhaustive match. Update the build() arm and the chain_preserves_shape
unit test fixture to match; no behavior change beyond the JSON detail
key ("by" -> "reduction").

Verified: cargo build --workspace, cargo test -p asap-ir (5 dag_export
tests pass), cargo clippy -p asap-ir -p asap-lower --all-targets clean,
and a real cargo run -p asap-lower --example dag_export invocation.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
@zzylol

zzylol commented Aug 12, 2026

Copy link
Copy Markdown
Contributor Author

Rebased on current `main` and fixed a compile break: `Aggregate::by` (`GroupKeys`) was renamed to `Aggregate::reduction` (`Reduction`) in #165/#166 after this branch forked, so `dag_export.rs`'s exhaustive match no longer compiled. Updated the `build()` arm and the `chain_preserves_shape_and_child_links` test fixture accordingly (JSON detail key: `"by"` → `"reduction"`).

Verified after the fix: `cargo build --workspace`, `cargo test -p asap-ir` (all 5 `dag_export` tests pass), `cargo clippy -p asap-ir -p asap-lower --all-targets` clean, and a real `cargo run -p asap-lower --example dag_export -- --promql "topk(5, rate(http_requests_total[5m]))"` run producing correct JSON. No other gaps found — the rest of the match is exhaustive against the current `QueryExpr`, and the viewer renders `detail` generically so it needed no changes.

🤖 Generated with Claude Code

zzylol added a commit that referenced this pull request Aug 12, 2026
…by a stale squash-merge (#177)

PR #176's squash-merge (via gh pr merge) used a stale cached PR head on
GitHub's side and landed the pre-#134-merge content: the README still
had the topk_ir/{:#?} section, and none of #134's actual files
(crates/ir/src/dag_export.rs, crates/lower/examples/dag_export.rs,
tools/dag-viewer/) made it into main.

This restores exactly the tree that was actually reviewed and verified
(build/test/clippy clean, dag_export CLI run end-to-end) before the
merge: the query-DAG JSON exporter + offline HTML viewer (#134, with
the Aggregate::by -> reduction compile fix), and the README's
'Visualize the IR' section pointing at it instead of topk_ir.

Co-authored-by: Claude Sonnet 5 <noreply@anthropic.com>
@zzylol

zzylol commented Aug 12, 2026

Copy link
Copy Markdown
Contributor Author

Closing as superseded — this content is already on `main` (landed via #177 after #176's squash-merge initially dropped it due to a stale-cache issue on GitHub's merge endpoint; #177 restored it, see that PR for the full story). Verified: `crates/ir/src/dag_export.rs`, `crates/lower/examples/dag_export.rs`, and `tools/dag-viewer/` are all present and building on current `main`, including the `Aggregate::by` → `reduction` fix from this branch. The only remaining diff between this branch and `main` is that `main` additionally has the README pointing at `dag_export`/`tools/dag-viewer` (from #176/#177) — merging this branch as-is would actually remove that doc section, so closing rather than merging.

🤖 Generated with Claude Code

@zzylol zzylol closed this Aug 12, 2026
@zzylol
zzylol deleted the query-dag-viewer branch August 12, 2026 18:36
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.

Consider a query DAG visualization tool (like Hamna's bgp-query-dag-explorer) for the IR

1 participant