From b399b377ac5fc61c02956474ae4d5928448fb851 Mon Sep 17 00:00:00 2001 From: Peter Allen Webb Date: Thu, 2 Feb 2023 10:28:46 -0500 Subject: [PATCH] ct-2018: optimize BaseRelaton.matches() --- core/dbt/adapters/base/relation.py | 39 ++++++++++-------------------- 1 file changed, 13 insertions(+), 26 deletions(-) diff --git a/core/dbt/adapters/base/relation.py b/core/dbt/adapters/base/relation.py index 13f64c01742..036ee0bbe34 100644 --- a/core/dbt/adapters/base/relation.py +++ b/core/dbt/adapters/base/relation.py @@ -12,7 +12,6 @@ Path, ) from dbt.exceptions import ( - ApproximateMatchError, DbtInternalError, MultipleDatabasesNotAllowedError, ) @@ -77,36 +76,24 @@ def matches( schema: Optional[str] = None, identifier: Optional[str] = None, ) -> bool: - search = filter_null_values( - { - ComponentName.Database: database, - ComponentName.Schema: schema, - ComponentName.Identifier: identifier, - } - ) - if not search: - # nothing was passed in - raise dbt.exceptions.DbtRuntimeError( - "Tried to match relation, but no search path was passed!" - ) + if identifier is not None and not self._is_exactish_match( + ComponentName.Identifier, identifier + ): + return False - exact_match = True - approximate_match = True + if schema is not None and not self._is_exactish_match(ComponentName.Schema, schema): + return False - for k, v in search.items(): - if not self._is_exactish_match(k, v): - exact_match = False - if str(self.path.get_lowered_part(k)).strip(self.quote_character) != v.lower().strip( - self.quote_character - ): - approximate_match = False # type: ignore[union-attr] + if database is not None and not self._is_exactish_match(ComponentName.Database, database): + return False - if approximate_match and not exact_match: - target = self.create(database=database, schema=schema, identifier=identifier) - raise ApproximateMatchError(target, self) + if database is None and schema is None and identifier is None: + raise dbt.exceptions.DbtRuntimeError( + "Tried to match relation, but no search path was passed!" + ) - return exact_match + return True def replace_path(self, **kwargs): return self.replace(path=self.path.replace(**kwargs))