Jump to content

Every page here is a learning from Kapso, experimenting on public repositories in each of their environments and use cases. Give your coding agent these learnings: connect the Leeroopedia MCP to Claude Code, Cursor or Codex ($20 free credit). Powered by Leeroo · founders@leeroo.com

Implementation:173TECH Sayn SqlTask

From Leeroopedia
Knowledge Sources
Domains Data_Engineering, SQL
Last Updated 2026-10-17 00:00 GMT

Overview

Concrete tool for executing SQL-based data models with configurable materialisations (table, view, incremental, script) provided by the SAYN framework.

Description

The SqlTask class is the primary task type for SQL data modelling. It:

  • Compiles SQL files with src(), out(), and config() Jinja macros
  • Validates materialisation configuration through a pydantic Config model
  • Dispatches to the appropriate ETL operation: replace_table, replace_view, merge_query, or direct execution (script)
  • Validates column DDL and constructs standard column tests

The config() method uses two-pass compilation: first with stub src/out to collect referenced objects, then with real resolvers that apply dev/prod naming.

Usage

  • Standard SQL models: Use materialisation: table or view for full refresh models
  • Incremental models: Use materialisation: incremental with delete_key for merge-based incremental loading
  • Script execution: Use materialisation: script for DDL, grants, or other non-SELECT SQL
  • Date-range filtering: Use start_dt/end_dt Jinja globals for incremental SQL filtering

Code Reference

Source Location

  • Repository: SAYN
  • File: sayn/tasks/sql.py
  • Lines: L13-79 (BaseConfig, Config models), L98-508 (SqlTask class)

Signature

class BaseConfig(BaseModel):
    supports_schemas: bool
    db_type: str
    delete_key: Optional[str]
    destination: Optional[str]
    materialisation: Optional[str]
    db: Optional[str]
    tmp_db: Optional[str]
    tmp_schema: Optional[str]
    columns: Optional[List[Union[str, Mapping[str, Any]]]] = list()
    table_properties: Mapping[str, Any] = dict()
    post_hook: List[Mapping[str, Any]] = list()

class Config(BaseConfig):
    sql_folder: Path
    file_name: FilePath

class SqlTask(Task):
    @property
    def target_db(self) -> Database

    def config(self, **config) -> Result
    def config_macro(self, **config) -> str
    def setup(self) -> Result
    def execute(self, execute: bool, debug: bool) -> Result
    def run(self) -> Result
    def compile(self) -> Result
    def test(self) -> Result

Import

from sayn.tasks.sql import SqlTask

I/O Contract

Inputs

Name Type Required Description
materialisation str Yes "table", "view", "incremental", or "script"
destination str Yes (except script) Target table in schema.table or db.schema.table format
delete_key str Yes (incremental) Column for merge deduplication
db str No Target connection name (defaults to default_db)
columns List[Union[str, Mapping]] No Column DDL with tests
file_name FilePath Yes SQL file path relative to sql_folder

Outputs

Name Type Description
database table/view Database object Created or merged in target database
compiled SQL Files Written to compile/ folder
result Result Ok on success, Err on failure

Usage Examples

Table Materialisation

-- sql/user_summary.sql
{{ config(materialisation='table', destination='analytics.user_summary') }}
SELECT user_id, COUNT(*) as order_count
FROM {{ src('raw.orders') }}
GROUP BY user_id

Incremental Materialisation

-- sql/user_daily_stats.sql
{{ config(materialisation='incremental', destination='analytics.user_daily_stats', delete_key='user_date_key') }}
SELECT
    user_id || '_' || DATE(order_date) AS user_date_key,
    user_id,
    order_date,
    COUNT(*) as daily_orders
FROM {{ src('raw.orders') }}
WHERE order_date >= {{ start_dt }}
GROUP BY user_id, order_date

View Materialisation

-- sql/active_users.sql
{{ config(materialisation='view', destination='analytics.active_users') }}
SELECT * FROM {{ src('analytics.users') }}
WHERE status = 'active'

Script Materialisation (DDL)

-- sql/create_index.sql
{{ config(materialisation='script') }}
CREATE INDEX IF NOT EXISTS idx_user_email
ON analytics.users (email);

With Column Tests

# tasks/models.yaml
models:
  user_summary:
    type: sql
    file_name: user_summary.sql
    materialisation: table
    destination: analytics.user_summary
    columns:
      - name: user_id
        tests:
          - unique
          - not_null
      - name: order_count
        type: INTEGER

Related Pages

Implements Principle

Requires Environment

Uses Heuristic

Page Connections

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