-
Notifications
You must be signed in to change notification settings - Fork 16
implement DataSet[Schema].from_dataframe(...) #420
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Draft
nanne-aben
wants to merge
11
commits into
main
Choose a base branch
from
from_dataset
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
c42cc5b
implement DataSet[Schema].from_dataframe(...)
nanne-aben f194028
update
nanne-aben f4425ae
update
nanne-aben a3e6c47
remove test notebook
nanne-aben 331678f
change interface to include register_to_schema()
nanne-aben 6b8202f
Merge branch 'main' into from_dataset
nanne-aben e96ab1a
update
nanne-aben 7c56566
fix full-name typo
nanne-aben aa57381
add functionality to generate schemas with external_name fields in a …
nanne-aben 9a9ac29
nested structtypes
nanne-aben 0f01c99
update
nanne-aben File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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,123 @@ | ||
| """Helper functions to rename columns from their external name (defined in | ||
| `ColumnMeta(external_name=...)`) to their internal name.""" | ||
|
|
||
| from typing import Optional, Type | ||
|
|
||
| from pyspark.sql import Column, DataFrame | ||
| from pyspark.sql.functions import col, lit, struct, when | ||
| from pyspark.sql.types import StructField, StructType | ||
|
|
||
| from typedspark._schema.schema import Schema | ||
|
|
||
|
|
||
| def rename_columns(df: DataFrame, schema: Type[Schema]) -> DataFrame: | ||
| """Helper functions to rename columns from their external name (defined in | ||
| `ColumnMeta(external_name=...)`) to their internal name (as used in the Schema).""" | ||
| for field in schema.get_structtype().fields: | ||
| internal_name = field.name | ||
|
|
||
| if field.metadata and "external_name" in field.metadata: | ||
| external_name = field.metadata["external_name"] | ||
| df = df.withColumnRenamed(external_name, internal_name) | ||
|
|
||
| if isinstance(field.dataType, StructType): | ||
| structtype = _create_renamed_structtype(field.dataType, internal_name) | ||
| df = df.withColumn(internal_name, structtype) | ||
|
|
||
| return df | ||
|
|
||
|
|
||
| def rename_columns_2(df: DataFrame, schema: Type[Schema]) -> DataFrame: | ||
| """Helper functions to rename columns from their internal name (as used in the | ||
| Schema) to their external name (defined in `ColumnMeta(external_name=...)`).""" | ||
| for field in schema.get_structtype().fields: | ||
| internal_name = field.name | ||
|
|
||
| if field.metadata and "external_name" in field.metadata: | ||
| external_name = field.metadata["external_name"] | ||
| df = df.withColumnRenamed(internal_name, external_name) # swap | ||
|
|
||
| if isinstance(field.dataType, StructType): | ||
| structtype = _create_renamed_structtype_2(field.dataType, internal_name) | ||
| df = df.withColumn(external_name, structtype) # swap | ||
|
|
||
| return df | ||
|
|
||
|
|
||
| def _create_renamed_structtype( | ||
| schema: StructType, | ||
| parent: str, | ||
| full_parent_path: Optional[str] = None, | ||
| ) -> Column: | ||
| if not full_parent_path: | ||
| full_parent_path = f"`{parent}`" | ||
|
|
||
| mapping = [] | ||
| for field in schema.fields: | ||
| external_name = _get_updated_parent_path(full_parent_path, field) | ||
|
|
||
| if isinstance(field.dataType, StructType): | ||
| mapping += [ | ||
| _create_renamed_structtype( | ||
| field.dataType, | ||
| parent=field.name, | ||
| full_parent_path=external_name, | ||
| ) | ||
| ] | ||
| else: | ||
| mapping += [col(external_name).alias(field.name)] | ||
|
|
||
| return _produce_nested_structtype(mapping, parent, full_parent_path) | ||
|
|
||
|
|
||
| def _create_renamed_structtype_2( | ||
| schema: StructType, | ||
| parent: str, | ||
| full_parent_path: Optional[str] = None, | ||
| ) -> Column: | ||
| if not full_parent_path: | ||
| full_parent_path = f"`{parent}`" | ||
|
|
||
| mapping = [] | ||
| for field in schema.fields: | ||
| internal_name = field.name | ||
| external_name = field.metadata.get("external_name", internal_name) | ||
|
|
||
| updated_parent_path = _get_updated_parent_path_2(full_parent_path, internal_name) # swap | ||
|
|
||
| if isinstance(field.dataType, StructType): | ||
| mapping += [ | ||
| _create_renamed_structtype( | ||
| field.dataType, | ||
| parent=external_name, # swap | ||
| full_parent_path=updated_parent_path, | ||
| ) | ||
| ] | ||
| else: | ||
| mapping += [col(updated_parent_path).alias(external_name)] # swap | ||
|
|
||
| return _produce_nested_structtype(mapping, parent, full_parent_path) | ||
|
|
||
|
|
||
| def _get_updated_parent_path(full_parent_path: str, field: StructField) -> str: | ||
| external_name = field.metadata.get("external_name", field.name) | ||
| return f"{full_parent_path}.`{external_name}`" | ||
|
|
||
|
|
||
| def _get_updated_parent_path_2(full_parent_path: str, field: str) -> str: | ||
| return f"{full_parent_path}.`{field}`" | ||
|
|
||
|
|
||
| def _produce_nested_structtype( | ||
| mapping: list[Column], | ||
| parent: str, | ||
| full_parent_path: str, | ||
| ) -> Column: | ||
| return ( | ||
| when( | ||
| col(full_parent_path).isNotNull(), | ||
| struct(*mapping), | ||
| ) | ||
| .otherwise(lit(None)) | ||
| .alias(parent) | ||
| ) |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.