Skip to content

Commit

Permalink
use naming convention when resolving nested tables
Browse files Browse the repository at this point in the history
  • Loading branch information
zilto committed Jan 29, 2025
1 parent 7f499f0 commit e51bd25
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 11 deletions.
4 changes: 2 additions & 2 deletions dlt/extract/extractors.py
Original file line number Diff line number Diff line change
Expand Up @@ -232,8 +232,8 @@ def _compute_table(
) -> List[TTableSchema]:
"""Computes a schema for a new or dynamic table and normalizes identifiers"""
return [
utils.normalize_table_identifiers(tbl, self.schema.naming)
for tbl in resource.compute_table_chain(items, meta)
utils.normalize_table_identifiers(tbl, self.naming)
for tbl in resource.compute_table_chain(self.naming, items, meta)
]

def _compute_and_update_table(
Expand Down
55 changes: 46 additions & 9 deletions dlt/extract/hints.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,8 @@
from dlt.common.typing import TDataItem, TColumnNames
from dlt.common.time import ensure_pendulum_datetime
from dlt.common.utils import clone_dict_nested
from dlt.common.normalizers.json.relational import DataItemNormalizer
from dlt.common.normalizers.naming import NamingConvention
from dlt.common.schema.utils import normalize_table_identifiers
from dlt.common.validation import validate_dict_ignoring_xkeys
from dlt.extract.exceptions import (
DataItemRequiredForDynamicTableHints,
Expand Down Expand Up @@ -288,7 +289,12 @@ def compute_table_schema(self, item: TDataItem = None, meta: Any = None) -> TTab

return table_schema

def compute_table_chain(self, item: TDataItem = None, meta: Any = None) -> List[TTableSchema]:
def compute_table_chain(
self,
naming: NamingConvention,
item: TDataItem = None,
meta: Any = None
) -> List[TTableSchema]:
"""Compute the table schema based on the current and all nested hints.
Nested hints are resolved recursively.
"""
Expand All @@ -299,21 +305,52 @@ def compute_table_chain(self, item: TDataItem = None, meta: Any = None) -> List[
root_table_name = root_table["name"]

result = [root_table]
path_to_name: Dict[Tuple[str, ...], str] = {(root_table_name,): root_table_name}
path_to_name_map: Dict[Tuple[str, ...], str] = {(root_table_name,): root_table_name}
for path, instance in self._walk_nested_hints():
full_path = (root_table_name,) + path
table = instance.compute_table_schema(item, meta)
#breakpoint()

if not table.get("name"):
table["name"] = "__".join(full_path) # TODO: naming convention
# generate name from current path and store {path: name}
current_path = (root_table_name, *path)
if table.get("name") is None:
table["name"] = naming.make_path(*current_path)
path_to_name_map[current_path] = table["name"]

# get parent name from the stored mapping {path: name}
parent_path = current_path[:-1]
parent_name = path_to_name_map.get(parent_path)
if parent_name is None:
raise KeyError(
f"Can't find parent table for path `{current_path}",
"This is an internal error, please report on GitHub."
)

path_to_name[full_path] = table["name"]
parent_name = path_to_name[full_path[:-1]]
table["parent"] = parent_name
result.append(table)

return result

# path_segments: Dict[Tuple[str, ...], str] = {(root_table_name,): root_table_name}
# for path, instance in self._walk_nested_hints():
# table = instance.compute_table_schema(item, meta)
# current_path = (root_table_name,) + path

# # generate table name according to naming convention if not explicitly set
# if table.get("name") is None:
# table["name"] = naming.make_path(path_segments[current_path])

# path_segments[current_path] = table["name"]

# # table = normalize_table_identifiers(table, naming)


# # full_path = (root_table_name,) + path
# # path_to_name[full_path] = table["name"]
# # parent_name = path_to_name[full_path[:-1]]
# # table["parent"] = parent_name

# result.append(table)
# # if not table.get("name"):
# # table["name"] = "__".join(full_path) # TODO: naming convention

def apply_hints(
self,
Expand Down

0 comments on commit e51bd25

Please sign in to comment.