Skip to content

Commit

Permalink
feat(core): drop default value for write disposition (#1057)
Browse files Browse the repository at this point in the history
* feat(core): drop default value for write disposition

* don't use default value in apply_hints

* applies default write disposition in empty apply hints

---------

Co-authored-by: Marcin Rudolf <[email protected]>
  • Loading branch information
IlyaFaer and rudolfix authored Mar 12, 2024
1 parent ee5db59 commit e622300
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 4 deletions.
4 changes: 3 additions & 1 deletion dlt/common/schema/schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,9 @@ class Schema:
_imported_version_hash: str # version hash of recently imported schema
_schema_description: str # optional schema description
_schema_tables: TSchemaTables
_settings: TSchemaSettings # schema settings to hold default hints, preferred types and other settings
_settings: (
TSchemaSettings # schema settings to hold default hints, preferred types and other settings
)

# list of preferred types: map regex on columns into types
_compiled_preferred_types: List[Tuple[REPattern, TDataType]]
Expand Down
5 changes: 4 additions & 1 deletion dlt/extract/hints.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,8 @@ def make_hints(
)
if not table_name:
new_template.pop("name")
if not write_disposition and "write_disposition" in new_template:
new_template.pop("write_disposition")
# remember original columns
if columns is not None:
new_template["original_columns"] = columns
Expand Down Expand Up @@ -197,10 +199,11 @@ def apply_hints(
"""
if not self._hints:
# if there is no template yet, create and set a new one.
default_wd = None if parent_table_name else DEFAULT_WRITE_DISPOSITION
t = make_hints(
table_name,
parent_table_name,
write_disposition,
write_disposition or default_wd,
columns,
primary_key,
merge_key,
Expand Down
8 changes: 8 additions & 0 deletions tests/extract/test_extract.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,14 @@ def table_name_with_lambda(_range):
assert "table_name_with_lambda" not in schema.tables


def test_make_hints_default() -> None:
hints = make_hints()
assert hints == {"columns": {}}

hints = make_hints(write_disposition=None)
assert hints == {"columns": {}}


def test_extract_hints_mark(extract_step: Extract) -> None:
@dlt.resource
def with_table_hints():
Expand Down
15 changes: 13 additions & 2 deletions tests/extract/test_sources.py
Original file line number Diff line number Diff line change
Expand Up @@ -1326,9 +1326,11 @@ def empty_gen():

empty_r = empty()
with pytest.raises(InconsistentTableTemplate):
empty_r.apply_hints(parent_table_name=lambda ev: ev["p"])
empty_r.apply_hints(parent_table_name=lambda ev: ev["p"], write_disposition=None)

empty_r.apply_hints(table_name=lambda ev: ev["t"], parent_table_name=lambda ev: ev["p"])
empty_r.apply_hints(
table_name=lambda ev: ev["t"], parent_table_name=lambda ev: ev["p"], write_disposition=None
)
assert empty_r._table_name_hint_fun is not None
assert empty_r._table_has_other_dynamic_hints is True

Expand Down Expand Up @@ -1360,6 +1362,15 @@ def empty_gen():
assert table["columns"]["tags"] == {"name": "tags"}


def test_resource_no_template() -> None:
empty = DltResource.from_data([1, 2, 3], name="table")
assert empty.write_disposition == "append"
assert empty.compute_table_schema()["write_disposition"] == "append"
empty.apply_hints()
assert empty.write_disposition == "append"
assert empty.compute_table_schema()["write_disposition"] == "append"


def test_selected_pipes_with_duplicates():
def input_gen():
yield from [1, 2, 3]
Expand Down

0 comments on commit e622300

Please sign in to comment.