Skip to content

2 - Avoid recreating full table mapping for every render #6018

Description

@cmgoffena13

File: sqlmesh/core/snapshot/definition.py
Functions: to_table_mapping
Lines: 1994 - 2003

Issue with to_table_mapping

Issue is really in sqlmesh/core/renderer.py in _resolve_tables that we touched in Issue 1. The function calls to_table_mapping for every expression, doing a comprehension every single time. We need to cache the mapping so renders just grab the cache instead of recreating it.

Example AI Code

_TO_TABLE_MAPPING_CACHE: dict[tuple, dict[str, str]] = {}
def to_table_mapping(
    snapshots: t.Iterable[Snapshot], deployability_index: t.Optional[DeployabilityIndex]
) -> t.Dict[str, str]:
    snapshots = list(snapshots)
    deployability_index = deployability_index or DeployabilityIndex.all_deployable()
    key = (
        deployability_index.indexed_ids,
        deployability_index.is_opposite_index,
        deployability_index.representative_shared_version_ids,
        tuple((s.snapshot_id, s.version, s.dev_version) for s in snapshots),
    )
    cached = _TO_TABLE_MAPPING_CACHE.get(key)
    if cached is None:
        cached = {
            snapshot.name: snapshot.table_name(deployability_index.is_deployable(snapshot))
            for snapshot in snapshots
            if snapshot.version and not snapshot.is_embedded and snapshot.is_model
        }
        _TO_TABLE_MAPPING_CACHE[key] = cached
    return dict(cached)

Note: lru_cache on the function directly does not work — snapshots is an unhashable iterable of objects.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions