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,11 +311,18 @@ 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 :
314+ < << << << HEAD
313315 if model .fqn in models :
314316 self ._raise_failed_to_load_model_error (
315317 path , f"Duplicate external model name: '{ model .name } '."
316318 )
317319 models [model .fqn ] = model
320+ == == == =
321+ try :
322+ models [model .fqn ] = model
323+ except Exception as ex :
324+ raise ConfigError (f"Failed to add model: { model .fqn } \n \n { ex } " )
325+ >> >> >> > 80487e4 f (add mock executor ; fix loader ; adapt unit tests )
318326
319327 # however, if there is a gateway defined, gateway-specific models take precedence
320328 if gateway :
@@ -473,20 +481,15 @@ def _load_models(
473481 audits into a Dict and creates the dag
474482 """
475483 cache = SqlMeshLoader ._Cache (self , self .config_path )
476- import time
477484
478- now = time .time ()
479485 sql_models = self ._load_sql_models (macros , jinja_macros , audits , signals , cache , gateway )
480- print ("sql models" , time .time () - now )
481- now = time .time ()
482486 external_models = self ._load_external_models (audits , cache , gateway )
483- print ("external models" , time .time () - now )
484487 python_models = self ._load_python_models (macros , jinja_macros , audits , signals )
485488
486489 all_model_names = list (sql_models ) + list (external_models ) + list (python_models )
487490 duplicates = [name for name , count in Counter (all_model_names ).items () if count > 1 ]
488491 if duplicates :
489- raise ValueError (f"Duplicate model name(s) found: { ', ' .join (duplicates )} ." )
492+ raise ConfigError (f"Duplicate model name(s) found: { ', ' .join (duplicates )} ." )
490493
491494 return UniqueKeyDict ("models" , ** sql_models , ** external_models , ** python_models )
492495
@@ -501,8 +504,7 @@ def _load_sql_models(
501504 ) -> UniqueKeyDict [str , Model ]:
502505 """Loads the sql models into a Dict"""
503506 models : UniqueKeyDict [str , Model ] = UniqueKeyDict ("models" )
504-
505- paths = set ()
507+ paths : t .Set [Path ] = set ()
506508
507509 for path in self ._glob_paths (
508510 self .config_path / c .MODELS ,
@@ -517,14 +519,11 @@ def _load_sql_models(
517519
518520 for path in paths .copy ():
519521 cached_models = cache .get (path )
520-
521522 if cached_models :
522523 paths .remove (path )
523-
524524 for model in cached_models :
525- models [model .fqn ] = model
526-
527- error = False
525+ if model .enabled :
526+ models [model .fqn ] = model
528527
529528 if paths :
530529 defaults = dict (
@@ -545,31 +544,31 @@ def _load_sql_models(
545544 default_catalog_per_gateway = self .context .default_catalog_per_gateway ,
546545 )
547546
548- with ProcessPoolExecutor (
547+ errors : t .List [str ] = []
548+ with concurrent .futures .ProcessPoolExecutor (
549549 mp_context = mp .get_context ("fork" ),
550550 initializer = _init_model_defaults ,
551551 initargs = (self .config , gateway , defaults , cache ),
552552 max_workers = c .MAX_FORK_WORKERS ,
553553 ) as pool :
554- for fut in as_completed (pool .submit (load_sql_models , path ) for path in paths ):
554+ futures_to_paths = {pool .submit (load_sql_models , path ): path for path in paths }
555+ for fut , path in futures_to_paths .items ():
555556 try :
556- path , loaded = fut .result ()
557-
557+ _ , loaded = fut .result ()
558558 if loaded :
559559 for model in loaded :
560- model ._path = path
561- models [model .fqn ] = model
560+ if model .enabled :
561+ model ._path = path
562+ models [model .fqn ] = model
562563 else :
563564 for model in cache .get (path ):
564- models [model .fqn ] = model
565+ if model .enabled :
566+ models [model .fqn ] = model
565567 except Exception as ex :
566- self ._console .log_error (
567- f"Failed to load model definition at '{ path } '.\n { ex } "
568- )
569- error = True
568+ errors .append (f"Failed to load model definition at '{ path } '.\n \n { ex } " )
570569
571- if error :
572- raise ConfigError ("Failed to load models" )
570+ if errors :
571+ raise ConfigError (f "Failed to load models\n \n { ' \n ' . join ( errors ) } " )
573572
574573 return models
575574
0 commit comments