Skip to content

Commit 5a4cd47

Browse files
committed
feat: cache external models and others for faster loading
1 parent c4e1f02 commit 5a4cd47

2 files changed

Lines changed: 31 additions & 23 deletions

File tree

sqlmesh/core/loader.py

Lines changed: 30 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@
2323
from sqlmesh.core.metric import Metric, MetricMeta, expand_metrics, load_metric_ddl
2424
from sqlmesh.core.model import (
2525
Model,
26-
ExternalModel,
2726
ModelCache,
2827
SeedModel,
2928
create_external_model,
@@ -208,32 +207,41 @@ def _load_external_models(
208207
if external_models_path.exists() and external_models_path.is_dir():
209208
paths_to_load.extend(self._glob_paths(external_models_path, extension=".yaml"))
210209

210+
cache = SqlMeshLoader._Cache(self, self.config_path)
211+
212+
def _load() -> t.List[Model]:
213+
try:
214+
with open(path, "r", encoding="utf-8") as file:
215+
return [
216+
create_external_model(
217+
defaults=self.config.model_defaults.dict(),
218+
path=path,
219+
project=self.config.project,
220+
audit_definitions=audits,
221+
**{
222+
"dialect": self.config.model_defaults.dialect,
223+
"default_catalog": self.context.default_catalog,
224+
**row,
225+
},
226+
)
227+
for row in YAML().load(file.read())
228+
]
229+
except Exception as ex:
230+
raise ConfigError(f"Failed to load model definition at '{path}'.\n{ex}")
231+
211232
for path in paths_to_load:
212233
self._track_file(path)
213234

214-
with open(path, "r", encoding="utf-8") as file:
215-
external_models: t.List[ExternalModel] = []
216-
for row in YAML().load(file.read()):
217-
model = create_external_model(
218-
defaults=self.config.model_defaults.dict(),
219-
path=path,
220-
project=self.config.project,
221-
audit_definitions=audits,
222-
**{
223-
"dialect": self.config.model_defaults.dialect,
224-
"default_catalog": self.context.default_catalog,
225-
**row,
226-
},
227-
)
228-
external_models.append(model)
229-
230-
# external models with no explicit gateway defined form the base set
231-
for model in (e for e in external_models if e.gateway is None):
235+
external_models = cache.get_or_load_models(path, _load)
236+
# external models with no explicit gateway defined form the base set
237+
for model in external_models:
238+
if model.gateway is None:
232239
models[model.fqn] = model
233240

234-
# however, if there is a gateway defined, gateway-specific models take precedence
235-
if gateway:
236-
for model in (e for e in external_models if e.gateway == gateway):
241+
# however, if there is a gateway defined, gateway-specific models take precedence
242+
if gateway:
243+
for model in external_models:
244+
if model.gateway == gateway:
237245
models.update({model.fqn: model})
238246

239247
return models

sqlmesh/core/model/cache.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ def get_or_load(
5959
return cache_entry
6060

6161
models = loader()
62-
if isinstance(models, list) and isinstance(seq_get(models, 0), SqlModel):
62+
if isinstance(models, list):
6363
# make sure we preload full_depends_on
6464
for model in models:
6565
model.full_depends_on

0 commit comments

Comments
 (0)