Skip to content

Commit c5f63d9

Browse files
feat: Cache schema owners to optimize table ingestion
Co-authored-by: yourton.ma <yourton.ma@gmail.com>
1 parent 8a7b08b commit c5f63d9

File tree

1 file changed

+15
-21
lines changed

1 file changed

+15
-21
lines changed

ingestion/src/metadata/ingestion/source/database/database_service.py

Lines changed: 15 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
"""
1414
import traceback
1515
from abc import ABC, abstractmethod
16-
from typing import Any, Iterable, List, Optional, Set, Tuple
16+
from typing import Any, Dict, Iterable, List, Optional, Set, Tuple
1717

1818
from pydantic import BaseModel, Field
1919
from sqlalchemy.engine import Inspector
@@ -225,6 +225,8 @@ class DatabaseServiceSource(
225225
stored_procedure_source_state: Set = set()
226226
database_entity_source_state: Set = set()
227227
schema_entity_source_state: Set = set()
228+
# Cache for schema owners to avoid repeated API calls during table ingestion
229+
schema_owner_cache: Dict[str, Optional[str]] = {}
228230
# Big union of types we want to fetch dynamically
229231
service_connection: DatabaseConnection.model_fields["config"].annotation
230232

@@ -654,6 +656,9 @@ def get_schema_owner_ref(self, schema_name: str) -> Optional[EntityReferenceList
654656
parent_owner=parent_owner,
655657
)
656658
if owner_ref:
659+
# Cache the resolved owner for table inheritance
660+
if owner_ref.root:
661+
self.schema_owner_cache[schema_fqn] = owner_ref.root[0].name
657662
return owner_ref
658663

659664
except Exception as exc:
@@ -680,31 +685,20 @@ def get_owner_ref(self, table_name: str) -> Optional[EntityReferenceList]:
680685
try:
681686
parent_owner = None
682687

683-
# Get parent owner from schema entity
684-
# Since schema entity is not stored in context, we fetch it from the API
688+
# Get parent owner from cached schema owner
689+
# The schema owner was cached when the schema was processed
685690
if (
686691
hasattr(self.source_config, "ownerConfig")
687692
and self.source_config.ownerConfig
688693
):
689-
schema_fqn = fqn.build(
690-
metadata=self.metadata,
691-
entity_type=DatabaseSchema,
692-
service_name=self.context.get().database_service,
693-
database_name=self.context.get().database,
694-
schema_name=self.context.get().database_schema,
695-
)
694+
schema_fqn = f"{self.context.get().database}.{self.context.get().database_schema}"
696695

697-
# Fetch schema entity to get its resolved owner
698-
try:
699-
schema_entity = self.metadata.get_by_name(
700-
entity=DatabaseSchema,
701-
fqn=schema_fqn,
702-
fields=["owners"],
703-
)
704-
if schema_entity and schema_entity.owners and schema_entity.owners.root:
705-
parent_owner = schema_entity.owners.root[0].name
706-
except Exception as exc:
707-
logger.debug(f"Could not fetch schema entity for owner inheritance: {exc}")
696+
# Use cached schema owner if available
697+
parent_owner = self.schema_owner_cache.get(schema_fqn)
698+
699+
logger.debug(
700+
f"Table '{table_name}': Using cached schema owner from '{schema_fqn}': {parent_owner}"
701+
)
708702

709703
table_fqn = f"{self.context.get().database}.{self.context.get().database_schema}.{table_name}"
710704

0 commit comments

Comments
 (0)