feat: compile a query to warehouse SQL without executing it#30
Merged
Conversation
Add Query.compile() -> str, returning the SQL Lightdash would run for a query
without executing it or fetching rows. Useful for inspecting/debugging a query
or running it directly against the warehouse (bigframes, dbt, pipelines) when
you need more rows than the query API returns.
- Reuses the existing _build_payload(), POSTed to the v1
`/explores/{explore}/compileQuery` endpoint, returning `results.query`.
- The v1 endpoint is stricter than the v2 query endpoint: it requires
`additionalMetrics` and an `id` on every filter group/rule (v2 injects ids
server-side). `_inject_filter_ids()` adds them; no-filter queries send `{}`.
Verified live end-to-end: compile with a filter returns correct SQL with a
WHERE clause. 7 unit tests (fake client) + an env-gated acceptance test.
Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
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.
Closes #5
What
Adds
Query.compile() -> str— returns the warehouse SQL Lightdash would run for a query, without executing it or fetching any rows.Why
The original request: get compiled SQL to run elsewhere (the issue's bigframes example). It also complements #19 — the query API caps at
query.maxLimit(100k), but.compile()gives you the SQL to run directly against your warehouse with no row cap, for bigframes / dbt / pipelines:How
_build_payload(), POSTed to the v1POST /projects/{uuid}/explores/{explore}/compileQueryendpoint (the same one the UI's "View SQL" uses), returningresults.query.additionalMetricsand anidon every filter group/rule (v2 injects ids server-side)._inject_filter_ids()adds them; no-filter queries send{}(the shape v1 accepts). The 422 that surfaced this is captured in the implementation comment.to_sql()etc.) — idiomatic Python favours one name;.compile()matches Lightdash's own terminology, and it's documented in the SDK guide.Verified
Live, end-to-end through the SDK — compile with a filter (exercising the id injection):
Tests
tests/test_compile.py(7, fake client): returns SQL, POSTs to the compile endpoint,additionalMetricspresent, no-filter sends{}, filter groups/rules get ids, limit flows through unbounded, requires a client. Plus an env-gated live acceptance test (test_compile_query). Full unit suite green (138).🤖 Generated with Claude Code