Skip to content

Commit

Permalink
use the cached version of the relation to ensure we have column metadata
Browse files Browse the repository at this point in the history
  • Loading branch information
mikealfare committed Dec 19, 2023
1 parent 3afd659 commit f28c181
Showing 1 changed file with 13 additions and 5 deletions.
18 changes: 13 additions & 5 deletions dbt/adapters/spark/impl.py
Original file line number Diff line number Diff line change
Expand Up @@ -408,22 +408,30 @@ def _get_one_catalog(
) -> agate.Table:
if len(schemas) != 1:
raise dbt.exceptions.CompilationError(
f"Expected only one schema in spark _get_one_catalog, found " f"{schemas}"
f"Expected only one schema in spark _get_one_catalog, found {schemas}"
)
relations = self.list_relations(information_schema.database, schemas.pop())
return self._get_one_catalog_by_relations(information_schema, relations, manifest)
return self._get_relation_metadata(relations)

def _get_one_catalog_by_relations(
self,
information_schema: InformationSchema,
relations: List[BaseRelation],
manifest: Manifest,
) -> agate.Table:
columns: List[Dict[str, Any]] = []
if len(relations) == 0:
schemas = {r.schema for r in relations}
identifiers = {r.identifier for r in relations}
if len(schemas) != 1:
raise dbt.exceptions.CompilationError(
"Expected at least one relation in spark _get_one_catalog_by_relations, found None"
f"Expected only one schema in spark _get_one_catalog_by_relations, found {schemas}"
)
# we need to use the cache to get all the column metadata
all_relations = self.cache.get_relations(information_schema.database, schemas.pop())
target_relations = [r for r in all_relations if r.identifier in identifiers]
return self._get_relation_metadata(target_relations)

def _get_relation_metadata(self, relations: List[BaseRelation]) -> agate.Table:
columns: List[Dict[str, Any]] = []
for relation in relations:
logger.debug(f"Getting table schema for relation {relation}")
columns.extend(self._get_columns_for_catalog(relation))
Expand Down

0 comments on commit f28c181

Please sign in to comment.