-
Notifications
You must be signed in to change notification settings - Fork 203
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
first version of ibis dataset base transformations
- Loading branch information
Showing
5 changed files
with
148 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
from typing import Callable, Literal, Union, Any, Generator, List, TYPE_CHECKING, Iterable | ||
|
||
from dataclasses import dataclass | ||
from functools import wraps | ||
|
||
from dlt.common.destination.reference import SupportsReadableDataset, SupportsReadableRelation | ||
|
||
|
||
TTransformationMaterialization = Literal["table", "view"] | ||
TTransformationWriteDisposition = Literal["replace", "append"] | ||
|
||
TTransformationFunc = Callable[[SupportsReadableDataset], SupportsReadableRelation] | ||
|
||
TTransformationGroupFunc = Callable[[], List[TTransformationFunc]] | ||
|
||
|
||
def transformation( | ||
table_name: str, | ||
materialization: TTransformationMaterialization = "table", | ||
write_disposition: TTransformationWriteDisposition = "replace", | ||
) -> Callable[[TTransformationFunc], TTransformationFunc]: | ||
def decorator(func: TTransformationFunc) -> TTransformationFunc: | ||
@wraps(func) | ||
def wrapper(*args: Any, **kwargs: Any) -> SupportsReadableRelation: | ||
return func(*args, **kwargs) | ||
|
||
# save the arguments to the function | ||
wrapper.__transformation_args__ = { # type: ignore | ||
"table_name": table_name, | ||
"materialization": materialization, | ||
"write_disposition": write_disposition, | ||
} | ||
|
||
return wrapper | ||
|
||
return decorator | ||
|
||
|
||
def transformation_group( | ||
name: str, | ||
) -> Callable[[TTransformationGroupFunc], TTransformationGroupFunc]: | ||
def decorator(func: TTransformationGroupFunc) -> TTransformationGroupFunc: | ||
@wraps(func) | ||
def wrapper(*args: Any, **kwargs: Any) -> List[TTransformationFunc]: | ||
return func(*args, **kwargs) | ||
|
||
func.__transformation_group_args__ = { # type: ignore | ||
"name": name, | ||
} | ||
return wrapper | ||
|
||
return decorator | ||
|
||
|
||
def run_transformations( | ||
dataset: SupportsReadableDataset, | ||
transformations: Union[TTransformationFunc, List[TTransformationFunc]], | ||
) -> None: | ||
if not isinstance(transformations, Iterable): | ||
transformations = [transformations] | ||
|
||
# TODO: fix typing | ||
with dataset.sql_client as client: # type: ignore | ||
for transformation in transformations: | ||
# get transformation settings | ||
table_name = transformation.__transformation_args__["table_name"] # type: ignore | ||
materialization = transformation.__transformation_args__["materialization"] # type: ignore | ||
write_disposition = transformation.__transformation_args__["write_disposition"] # type: ignore | ||
table_name = client.make_qualified_table_name(table_name) | ||
|
||
# get relation from transformation | ||
relation = transformation(dataset) | ||
if not isinstance(relation, SupportsReadableRelation): | ||
raise ValueError( | ||
f"Transformation {transformation.__name__} did not return a ReadableRelation" | ||
) | ||
|
||
# materialize result | ||
select_clause = relation.query | ||
|
||
if write_disposition == "replace": | ||
client.execute( | ||
f"CREATE OR REPLACE {materialization} {table_name} AS {select_clause}" | ||
) | ||
elif write_disposition == "append" and materialization == "table": | ||
try: | ||
client.execute(f"INSERT INTO {table_name} {select_clause}") | ||
except Exception: | ||
client.execute(f"CREATE TABLE {table_name} AS {select_clause}") | ||
else: | ||
raise ValueError( | ||
f"Write disposition {write_disposition} is not supported for " | ||
f"materialization {materialization}" | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
import dlt | ||
|
||
from dlt.common.destination.reference import SupportsReadableDataset, SupportsReadableRelation | ||
|
||
from functools import reduce | ||
|
||
|
||
def test_simple_transformation() -> None: | ||
# load some stuff into items table | ||
|
||
@dlt.resource(table_name="items") | ||
def items_resource(): | ||
for i in range(10): | ||
yield {"id": i, "value": i * 2} | ||
|
||
p = dlt.pipeline("test_pipeline", destination="duckdb", dataset_name="test_dataset") | ||
p.run(items_resource) | ||
|
||
print(p.dataset().items.df()) | ||
|
||
@dlt.transformation(table_name="quadrupled_items") | ||
def simple_transformation(dataset: SupportsReadableDataset) -> SupportsReadableRelation: | ||
items_table = dataset.items | ||
return items_table.mutate(quadruple_id=items_table.id * 4).select("id", "quadruple_id") | ||
|
||
@dlt.transformation(table_name="aggregated_items") | ||
def aggregate_transformation(dataset: SupportsReadableDataset) -> SupportsReadableRelation: | ||
items_table = dataset.items | ||
return items_table.aggregate(sum_id=items_table.id.sum(), value_sum=items_table.value.sum()) | ||
|
||
# we run two transformations | ||
p.transform([simple_transformation, aggregate_transformation]) | ||
|
||
# check table with quadrupled ids | ||
assert list(p.dataset().quadrupled_items.df()["quadruple_id"]) == [i * 4 for i in range(10)] | ||
|
||
# check aggregated table for both fields | ||
assert p.dataset().aggregated_items.fetchone()[0] == reduce(lambda a, b: a + b, range(10)) | ||
assert p.dataset().aggregated_items.fetchone()[1] == (reduce(lambda a, b: a + b, range(10)) * 2) |