Jump to content

Connect SuperML | Leeroopedia MCP: Equip your AI agents with best practices, code verification, and debugging knowledge. Powered by Leeroo — building Organizational Superintelligence. Contact us at founders@leeroo.com.

Principle:TobikoData Sqlmesh Macro And Jinja Translation

From Leeroopedia


Knowledge Sources
Domains Data_Engineering, Dbt_Migration
Last Updated 2026-02-07 00:00 GMT

Overview

Translating dbt's Jinja macro system and templating expressions into SQLMesh's macro environment while preserving functionality.

Description

Macro and Jinja translation is critical for dbt migration because dbt projects heavily rely on Jinja templating for SQL generation, code reusability, and dynamic behavior. This process involves creating a compatible Jinja environment within SQLMesh that can execute dbt macros, including built-in dbt functions (ref, source, var, config) and custom project macros. The translation must maintain dbt's package-scoped macro namespacing while integrating with SQLMesh's model rendering system.

The translation handles complex scenarios like macro dependencies (macros calling other macros), package-qualified macro references, variable interpolation, and dbt's special adapter methods. It also bridges the gap between dbt's adapter dispatch system (which selects macro implementations based on database type) and SQLMesh's engine adapter architecture.

Usage

Use macro and Jinja translation when:

  • Loading dbt projects with custom macros into SQLMesh
  • Converting dbt models that use Jinja templating
  • Executing dbt's built-in functions (ref, source, var) in SQLMesh
  • Supporting dbt packages with macro dependencies
  • Maintaining compatibility with dbt's adapter-specific macros

Theoretical Basis

The macro translation system operates through several interconnected mechanisms:

1. Jinja Environment Construction:

  * Create SQLMesh JinjaMacroRegistry with dbt compatibility layer
  * Set root package name to project name for macro resolution
  * Configure top-level packages list (always includes "dbt")
  * Build Jinja2 environment with dbt-compatible globals and filters

2. Macro Registration:

  * Parse macro definitions from SQL files in macros/ directory
  * Extract macro signatures (name, arguments, defaults)
  * Store macros with package qualification (package.macro_name)
  * Handle macro overrides (project macros override package macros)

3. Built-in Function Translation:

  * Implement ref() to resolve model references to SQLMesh model names
  * Implement source() to resolve source references to external tables
  * Implement var() to access project and CLI variables with defaults
  * Implement config() to apply model-level configuration
  * Implement is_incremental() to detect incremental model runs

4. Package Namespace Management:

  * Maintain separate macro dictionaries per package
  * Support package-qualified references (package_name.macro_name)
  * Allow unqualified references to resolve in package order
  * Handle dbt_packages/ dependencies with proper namespacing

5. Variable Rendering Pipeline:

  * First pass: render variables that reference other variables
  * Handle circular dependencies with appropriate error messages
  * Support var() calls within variable definitions
  * Cache rendered variables per package context

6. Context Isolation:

  * Create package-specific contexts for variable scoping
  * Set top_level_packages to include current package for macro visibility
  * Preserve project-level context for cross-package references
  * Allow context copying for isolated rendering

The translation preserves dbt's macro semantics while executing within SQLMesh's infrastructure. For example, when a dbt model calls ref('other_model'), the translation layer resolves this to the fully-qualified SQLMesh model name, handling package prefixes and version suffixes automatically.

Critical considerations:

  • Macro expansion timing: macros expand during model loading, not execution
  • Variable resolution: variables render before macro expansion
  • Adapter dispatch: SQLMesh selects macro implementations based on target dialect
  • Package precedence: project macros override dependency macros

Related Pages

Implemented By

Page Connections

Double-click a node to navigate. Hold to expand connections.
Principle
Implementation
Heuristic
Environment