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.
File:
sqlmesh/core/snapshot/definition.pyFunctions:
to_table_mappingLines:
1994 - 2003Issue with
to_table_mappingIssue is really in
sqlmesh/core/renderer.pyin_resolve_tablesthat we touched in Issue 1. The function callsto_table_mappingfor 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
Note:
lru_cacheon the function directly does not work — snapshots is an unhashable iterable of objects.