Jump to content

Connect Leeroopedia MCP: Equip your AI agents to search best practices, build plans, verify code, diagnose failures, and look up hyperparameter defaults.

Implementation:TobikoData Sqlmesh PlanBuilder Set Start End

From Leeroopedia


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

Overview

Concrete methods for adjusting backfill time ranges in deployment plans, provided by the PlanBuilder class.

Description

The PlanBuilder.set_start and PlanBuilder.set_end methods allow programmatic or interactive modification of backfill time boundaries after a plan is initially created. These methods update the plan's start and end timestamps, mark that these values were explicitly overridden, and invalidate the cached plan to trigger recalculation of backfill intervals and costs.

These methods enable the interactive plan workflow where users review automatically calculated backfill ranges and adjust them based on cost, time, or business constraints. The methods return self to support method chaining. Setting new boundaries causes the plan to be rebuilt on next access, recalculating which intervals need processing for each model.

Usage

Call these methods during plan review to adjust backfill ranges before application. Use them in interactive CLI workflows where users modify suggested dates, in automated scripts that apply business-specific backfill policies, or when implementing custom backfill strategies based on data volume or warehouse costs. These methods are particularly useful for limiting expensive full backfills to shorter recent time windows.

Code Reference

Source Location

  • Repository: sqlmesh
  • File: sqlmesh/core/plan/builder.py
  • Class: PlanBuilder
  • Methods: set_start (line 227), set_end (line 233)

Signature

def set_start(self, new_start: TimeLike) -> PlanBuilder:
    """Sets the start date for backfilling.

    Args:
        new_start: The new start date/time.

    Returns:
        Self for method chaining.
    """

def set_end(self, new_end: TimeLike) -> PlanBuilder:
    """Sets the end date for backfilling.

    Args:
        new_end: The new end date/time.

    Returns:
        Self for method chaining.
    """

Import

from sqlmesh.core.context import Context

context = Context(paths="project")
plan_builder = context.plan_builder("prod")

I/O Contract

Inputs

Name Type Required Description
new_start TimeLike Yes New start boundary (date string, datetime, or relative like "-1 month")
new_end TimeLike Yes New end boundary (date string, datetime, or relative like "now")

Outputs

Name Type Description
self PlanBuilder The PlanBuilder instance for method chaining

Usage Examples

Basic Usage

from sqlmesh.core.context import Context

context = Context(paths="project")

# Get plan builder
plan_builder = context.plan_builder("prod")

# Adjust backfill range
plan_builder.set_start("2024-01-01").set_end("2024-01-31")

# Build plan with new ranges
plan = plan_builder.build()

Interactive Adjustment

from sqlmesh.core.context import Context

context = Context(paths="project")
plan_builder = context.plan_builder("prod")

# Review initial plan
initial_plan = plan_builder.build()
print(f"Initial backfill cost estimate: {initial_plan.backfill_cost}")

# Adjust to reduce costs
plan_builder.set_start("-7 days")  # Only last week instead of full history

# Rebuild with adjusted range
final_plan = plan_builder.build()
print(f"Adjusted backfill cost estimate: {final_plan.backfill_cost}")

context.apply(final_plan)

Relative Time Ranges

from sqlmesh.core.context import Context

context = Context(paths="project")
plan_builder = context.plan_builder("dev_environment")

# Use relative time expressions
plan_builder.set_start("-1 month").set_end("now")

plan = plan_builder.build()
context.apply(plan)

Cost Optimization

from sqlmesh.core.context import Context
from datetime import datetime

context = Context(paths="project")
plan_builder = context.plan_builder("prod")

# Start with default range
initial_plan = plan_builder.build()
estimated_days = (initial_plan.end - initial_plan.start).days

# If too expensive, limit to recent data
if estimated_days > 90:
    print(f"Full backfill would process {estimated_days} days, limiting to 30")
    plan_builder.set_start("-30 days")

plan = plan_builder.build()
print(f"Final range: {plan.start} to {plan.end}")
context.apply(plan)

Per-Environment Strategies

from sqlmesh.core.context import Context

context = Context(paths="project")

def create_and_apply_plan(env_name, start, end):
    plan_builder = context.plan_builder(env_name)
    plan_builder.set_start(start).set_end(end)
    plan = plan_builder.build()
    context.apply(plan)
    return plan

# Development: minimal backfill
dev_plan = create_and_apply_plan("dev", start="-7 days", end="now")

# Staging: moderate backfill
staging_plan = create_and_apply_plan("staging", start="-30 days", end="now")

# Production: full backfill when needed
prod_plan = create_and_apply_plan("prod", start="2024-01-01", end="now")

Related Pages

Implements Principle

Page Connections

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