11from __future__ import annotations
22
33import abc
4+ import concurrent .futures
45import glob
56import itertools
67import linecache
78import multiprocessing as mp
89import os
910import re
1011import typing as t
12+ import concurrent
1113from collections import Counter , defaultdict
1214from dataclasses import dataclass
1315from pathlib import Path
14- from concurrent .futures import ProcessPoolExecutor , as_completed
1516
1617from sqlglot .errors import SqlglotError
1718from sqlglot import exp
@@ -310,7 +311,10 @@ def _load_external_models(
310311 # external models with no explicit gateway defined form the base set
311312 for model in external_models :
312313 if model .gateway is None :
313- models [model .fqn ] = model
314+ try :
315+ models [model .fqn ] = model
316+ except Exception as ex :
317+ raise ConfigError (f"Failed to add model: { model .fqn } \n \n { ex } " )
314318
315319 # however, if there is a gateway defined, gateway-specific models take precedence
316320 if gateway :
@@ -463,20 +467,15 @@ def _load_models(
463467 audits into a Dict and creates the dag
464468 """
465469 cache = SqlMeshLoader ._Cache (self , self .config_path )
466- import time
467470
468- now = time .time ()
469471 sql_models = self ._load_sql_models (macros , jinja_macros , audits , signals , cache , gateway )
470- print ("sql models" , time .time () - now )
471- now = time .time ()
472472 external_models = self ._load_external_models (audits , cache , gateway )
473- print ("external models" , time .time () - now )
474473 python_models = self ._load_python_models (macros , jinja_macros , audits , signals )
475474
476475 all_model_names = list (sql_models ) + list (external_models ) + list (python_models )
477476 duplicates = [name for name , count in Counter (all_model_names ).items () if count > 1 ]
478477 if duplicates :
479- raise ValueError (f"Duplicate model name(s) found: { ', ' .join (duplicates )} ." )
478+ raise ConfigError (f"Duplicate model name(s) found: { ', ' .join (duplicates )} ." )
480479
481480 return UniqueKeyDict ("models" , ** sql_models , ** external_models , ** python_models )
482481
@@ -491,8 +490,7 @@ def _load_sql_models(
491490 ) -> UniqueKeyDict [str , Model ]:
492491 """Loads the sql models into a Dict"""
493492 models : UniqueKeyDict [str , Model ] = UniqueKeyDict ("models" )
494-
495- paths = set ()
493+ paths : t .Set [Path ] = set ()
496494
497495 for path in self ._glob_paths (
498496 self .config_path / c .MODELS ,
@@ -507,14 +505,11 @@ def _load_sql_models(
507505
508506 for path in paths .copy ():
509507 cached_models = cache .get (path )
510-
511508 if cached_models :
512509 paths .remove (path )
513-
514510 for model in cached_models :
515- models [model .fqn ] = model
516-
517- error = False
511+ if model .enabled :
512+ models [model .fqn ] = model
518513
519514 if paths :
520515 defaults = dict (
@@ -535,31 +530,31 @@ def _load_sql_models(
535530 default_catalog_per_gateway = self .context .default_catalog_per_gateway ,
536531 )
537532
538- with ProcessPoolExecutor (
533+ errors : t .List [str ] = []
534+ with concurrent .futures .ProcessPoolExecutor (
539535 mp_context = mp .get_context ("fork" ),
540536 initializer = _init_model_defaults ,
541537 initargs = (self .config , gateway , defaults , cache ),
542538 max_workers = c .MAX_FORK_WORKERS ,
543539 ) as pool :
544- for fut in as_completed (pool .submit (load_sql_models , path ) for path in paths ):
540+ futures_to_paths = {pool .submit (load_sql_models , path ): path for path in paths }
541+ for fut , path in futures_to_paths .items ():
545542 try :
546- path , loaded = fut .result ()
547-
543+ _ , loaded = fut .result ()
548544 if loaded :
549545 for model in loaded :
550- model ._path = path
551- models [model .fqn ] = model
546+ if model .enabled :
547+ model ._path = path
548+ models [model .fqn ] = model
552549 else :
553550 for model in cache .get (path ):
554- models [model .fqn ] = model
551+ if model .enabled :
552+ models [model .fqn ] = model
555553 except Exception as ex :
556- self ._console .log_error (
557- f"Failed to load model definition at '{ path } '.\n { ex } "
558- )
559- error = True
554+ errors .append (f"Failed to load model definition at '{ path } '.\n \n { ex } " )
560555
561- if error :
562- raise ConfigError ("Failed to load models" )
556+ if errors :
557+ raise ConfigError (f "Failed to load models\n \n { ' \n ' . join ( errors ) } " )
563558
564559 return models
565560
0 commit comments