-
Notifications
You must be signed in to change notification settings - Fork 185
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
made table index type logic Synapse specific through destination adapter
- Loading branch information
Jorrit Sandbrink
committed
Jan 26, 2024
1 parent
014543a
commit 7868ca6
Showing
15 changed files
with
117 additions
and
82 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
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
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
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,50 @@ | ||
from typing import Any, Literal, Set, get_args, Final | ||
|
||
from dlt.extract import DltResource, resource as make_resource | ||
from dlt.extract.typing import TTableHintTemplate | ||
from dlt.extract.hints import TResourceHints | ||
from dlt.destinations.utils import ensure_resource | ||
|
||
TTableIndexType = Literal["heap", "clustered_columnstore_index"] | ||
""" | ||
Table [index type](https://learn.microsoft.com/en-us/azure/synapse-analytics/sql-data-warehouse/sql-data-warehouse-tables-index) used when creating the Synapse table. | ||
This regards indexes specified at the table level, not the column level. | ||
""" | ||
TABLE_INDEX_TYPES: Set[TTableIndexType] = set(get_args(TTableIndexType)) | ||
|
||
TABLE_INDEX_TYPE_HINT: Literal["x-table-index-type"] = "x-table-index-type" | ||
|
||
|
||
def synapse_adapter(data: Any, table_index_type: TTableIndexType = None) -> DltResource: | ||
"""Prepares data for the Synapse destination by specifying which table index | ||
type should be used. | ||
Args: | ||
data (Any): The data to be transformed. It can be raw data or an instance | ||
of DltResource. If raw data, the function wraps it into a DltResource | ||
object. | ||
table_index_type (TTableIndexType, optional): The table index type used when creating | ||
the Synapse table. | ||
Returns: | ||
DltResource: A resource with applied Synapse-specific hints. | ||
Raises: | ||
ValueError: If input for `table_index_type` is invalid. | ||
Examples: | ||
>>> data = [{"name": "Anush", "description": "Integrations Hacker"}] | ||
>>> synapse_adapter(data, table_index_type="clustered_columnstore_index") | ||
[DltResource with hints applied] | ||
""" | ||
resource = ensure_resource(data) | ||
|
||
if table_index_type is not None: | ||
if table_index_type not in TABLE_INDEX_TYPES: | ||
allowed_types = ", ".join(TABLE_INDEX_TYPES) | ||
raise ValueError( | ||
f"Table index type {table_index_type} is invalid. Allowed table index" | ||
f" types are: {allowed_types}." | ||
) | ||
resource._hints[TABLE_INDEX_TYPE_HINT] = table_index_type # type: ignore[typeddict-unknown-key] | ||
return resource |
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,16 @@ | ||
from typing import Any | ||
|
||
from dlt.extract import DltResource, resource as make_resource | ||
|
||
|
||
def ensure_resource(data: Any) -> DltResource: | ||
"""Wraps `data` in a DltResource if it's not a DltResource already.""" | ||
resource: DltResource | ||
if not isinstance(data, DltResource): | ||
resource_name: str = None | ||
if not hasattr(data, "__name__"): | ||
resource_name = "content" | ||
resource = make_resource(data, name=resource_name) | ||
else: | ||
resource = data | ||
return resource |
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
Oops, something went wrong.