-
Notifications
You must be signed in to change notification settings - Fork 56
feat: Jinja/dbt template preprocessing for SQL analysis tools #67
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from 2 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
1edb214
feat: add Jinja/dbt template preprocessing for SQL analysis tools
anandgupta42 591565e
test: add true E2E tests for Jinja preprocessor via subprocess
anandgupta42 3667ded
fix: add parentheses to resolve `||`/`??` operator precedence in `eng…
anandgupta42 7a9fdb8
fix: address PR review feedback from Sentry bot
anandgupta42 f29af30
fix: improve Jinja preprocessing robustness
anandgupta42 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
103 changes: 103 additions & 0 deletions
103
packages/altimate-code/src/tool/sql-preprocess-jinja.ts
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,103 @@ | ||
| import z from "zod" | ||
| import { Tool } from "./tool" | ||
| import { Bridge } from "../bridge/client" | ||
| import type { SqlPreprocessJinjaResult } from "../bridge/protocol" | ||
|
|
||
| export const SqlPreprocessJinjaTool = Tool.define("sql_preprocess_jinja", { | ||
| description: | ||
| "Preprocess Jinja/dbt template syntax in SQL before analysis. Stubs common dbt macros like {{ ref() }}, {{ source() }}, {{ config() }}, {{ var() }}, {{ this }}, and Jinja block tags ({% if %}, {% for %}) into plain SQL that downstream tools can parse. Use this when SQL analysis tools fail on dbt-templated SQL.", | ||
| parameters: z.object({ | ||
| sql: z.string().describe("SQL with Jinja/dbt template syntax to preprocess"), | ||
| }), | ||
| async execute(args, ctx) { | ||
| try { | ||
| const result = await Bridge.call("sql.preprocess_jinja", { | ||
| sql: args.sql, | ||
| }) | ||
|
|
||
| if (!result.was_preprocessed) { | ||
| return { | ||
| title: "Preprocess Jinja: no templates found", | ||
| metadata: { | ||
| success: true as boolean, | ||
| was_preprocessed: false as boolean, | ||
| refs: [] as string[], | ||
| sources: [] as string[], | ||
| variables: [] as string[], | ||
| }, | ||
| output: "No Jinja templates detected in the SQL. The input is already plain SQL.", | ||
| } | ||
| } | ||
|
|
||
| return { | ||
| title: `Preprocess Jinja: ${formatSummary(result)}`, | ||
| metadata: { | ||
| success: result.success, | ||
| was_preprocessed: result.was_preprocessed, | ||
| refs: result.refs_found, | ||
| sources: result.sources_found, | ||
| variables: result.variables_found, | ||
| }, | ||
| output: formatResult(result), | ||
| } | ||
| } catch (e) { | ||
| const msg = e instanceof Error ? e.message : String(e) | ||
| return { | ||
| title: "Preprocess Jinja: ERROR", | ||
| metadata: { | ||
| success: false as boolean, | ||
| was_preprocessed: false as boolean, | ||
| refs: [] as string[], | ||
| sources: [] as string[], | ||
| variables: [] as string[], | ||
| }, | ||
| output: `Failed to preprocess Jinja: ${msg}\n\nEnsure the Python bridge is running and altimate-engine is installed.`, | ||
| } | ||
| } | ||
| }, | ||
| }) | ||
|
|
||
| function formatSummary(result: SqlPreprocessJinjaResult): string { | ||
| const parts: string[] = [] | ||
| if (result.refs_found.length > 0) parts.push(`${result.refs_found.length} ref(s)`) | ||
| if (result.sources_found.length > 0) parts.push(`${result.sources_found.length} source(s)`) | ||
| if (result.variables_found.length > 0) parts.push(`${result.variables_found.length} var(s)`) | ||
| return parts.length > 0 ? parts.join(", ") : "templates removed" | ||
| } | ||
|
|
||
| function formatResult(result: SqlPreprocessJinjaResult): string { | ||
| const lines: string[] = [] | ||
|
|
||
| lines.push("=== Preprocessed SQL ===") | ||
| lines.push(result.preprocessed_sql) | ||
| lines.push("") | ||
|
|
||
| if (result.refs_found.length > 0) { | ||
| lines.push(`Models referenced (ref): ${result.refs_found.join(", ")}`) | ||
| } | ||
| if (result.sources_found.length > 0) { | ||
| lines.push(`Sources referenced: ${result.sources_found.join(", ")}`) | ||
| } | ||
| if (result.variables_found.length > 0) { | ||
| lines.push(`Variables used (var): ${result.variables_found.join(", ")}`) | ||
| } | ||
| if (result.macros_removed.length > 0) { | ||
| lines.push(`Macros removed: ${result.macros_removed.join(", ")}`) | ||
| } | ||
|
|
||
| if (result.warnings.length > 0) { | ||
| lines.push("") | ||
| lines.push("=== Warnings ===") | ||
| for (const w of result.warnings) { | ||
| lines.push(` ! ${w}`) | ||
| } | ||
| } | ||
|
|
||
| lines.push("") | ||
| lines.push( | ||
| "Note: Jinja templates were stubbed with placeholder values. " + | ||
| "Analysis results on this SQL are approximate.", | ||
| ) | ||
|
|
||
| return lines.join("\n") | ||
| } |
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
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
Oops, something went wrong.
Oops, something went wrong.
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.
This comment was marked as outdated.
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.