-
Notifications
You must be signed in to change notification settings - Fork 300
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
Annotated StructuredDataset: support nested_types
#2252
Merged
pingsutw
merged 11 commits into
flyteorg:master
from
austin362667:austin362667/structured_datasets
Apr 30, 2024
Merged
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
f69f7a3
StructuredDataset: add recursive `flatten_dict()`
austin362667 15600ff
add `levels_wf` as integration test
austin362667 bf892f7
add structured_dataset unit tests
austin362667 774c16e
make `kwtypes()` only accepts named args
austin362667 601939c
lint
austin362667 facdeb8
remove tabulate
austin362667 ff6b58f
refine `flatten_dict()` recursion
austin362667 153f709
resolve conflicts
austin362667 eaac66c
nit
pingsutw 98d7902
nit
pingsutw a6e469a
clean up
austin362667 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 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
179 changes: 179 additions & 0 deletions
179
...ytekit/unit/types/structured_dataset/test_structured_dataset_workflow_with_nested_type.py
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,179 @@ | ||
from dataclasses import dataclass | ||
|
||
import pyarrow as pa | ||
import pytest | ||
from typing_extensions import Annotated | ||
|
||
from flytekit import FlyteContextManager, StructuredDataset, kwtypes, task, workflow | ||
|
||
pd = pytest.importorskip("pandas") | ||
|
||
PANDAS_PATH = FlyteContextManager.current_context().file_access.get_random_local_directory() | ||
NUMPY_PATH = FlyteContextManager.current_context().file_access.get_random_local_directory() | ||
BQ_PATH = "bq://flyte-dataset:flyte.table" | ||
|
||
|
||
data = [ | ||
{ | ||
"company": "XYZ pvt ltd", | ||
"location": "London", | ||
"info": {"president": "Rakesh Kapoor", "contacts": {"email": "[email protected]", "tel": "9876543210"}}, | ||
}, | ||
{ | ||
"company": "ABC pvt ltd", | ||
"location": "USA", | ||
"info": {"president": "Kapoor Rakesh", "contacts": {"email": "[email protected]", "tel": "0123456789"}}, | ||
}, | ||
] | ||
|
||
|
||
@dataclass | ||
class ContactsField: | ||
email: str | ||
tel: str | ||
|
||
|
||
@dataclass | ||
class InfoField: | ||
president: str | ||
contacts: ContactsField | ||
|
||
|
||
@dataclass | ||
class CompanyField: | ||
location: str | ||
info: InfoField | ||
company: str | ||
|
||
|
||
MyArgDataset = Annotated[StructuredDataset, kwtypes(company=str)] | ||
MyDictDataset = Annotated[StructuredDataset, kwtypes(info={"contacts": {"tel": str}})] | ||
MyDictListDataset = Annotated[StructuredDataset, kwtypes(info={"contacts": {"tel": str, "email": str}})] | ||
MyTopDataClassDataset = Annotated[StructuredDataset, CompanyField] | ||
MyTopDictDataset = Annotated[StructuredDataset, {"company": str, "location": str}] | ||
MySecondDataClassDataset = Annotated[StructuredDataset, kwtypes(info=InfoField)] | ||
MyNestedDataClassDataset = Annotated[StructuredDataset, kwtypes(info=kwtypes(contacts=ContactsField))] | ||
|
||
|
||
@task() | ||
def create_pd_table() -> StructuredDataset: | ||
df = pd.json_normalize(data, max_level=0) | ||
print("original dataframe: \n", df) | ||
|
||
return StructuredDataset(dataframe=df, uri=PANDAS_PATH) | ||
|
||
|
||
@task() | ||
def create_bq_table() -> StructuredDataset: | ||
df = pd.json_normalize(data, max_level=0) | ||
print("original dataframe: \n", df) | ||
|
||
# Enable one of GCP `uri` below if you want. You can replace `uri` with your own google cloud endpoints. | ||
return StructuredDataset(dataframe=df, uri=BQ_PATH) | ||
|
||
|
||
@task() | ||
def create_np_table() -> StructuredDataset: | ||
df = pd.json_normalize(data, max_level=0) | ||
print("original dataframe: \n", df) | ||
|
||
return StructuredDataset(dataframe=df, uri=NUMPY_PATH) | ||
|
||
|
||
@task() | ||
def create_ar_table() -> StructuredDataset: | ||
df = pa.Table.from_pandas(pd.json_normalize(data, max_level=0)) | ||
print("original dataframe: \n", df) | ||
|
||
return StructuredDataset( | ||
dataframe=df, | ||
) | ||
|
||
|
||
@task() | ||
def print_table_by_arg(sd: MyArgDataset) -> pd.DataFrame: | ||
t = sd.open(pd.DataFrame).all() | ||
print("MyArgDataset dataframe: \n", t) | ||
return t | ||
|
||
|
||
@task() | ||
def print_table_by_dict(sd: MyDictDataset) -> pd.DataFrame: | ||
t = sd.open(pd.DataFrame).all() | ||
print("MyDictDataset dataframe: \n", t) | ||
return t | ||
|
||
|
||
@task() | ||
def print_table_by_list_dict(sd: MyDictListDataset) -> pd.DataFrame: | ||
t = sd.open(pd.DataFrame).all() | ||
print("MyDictListDataset dataframe: \n", t) | ||
return t | ||
|
||
|
||
@task() | ||
def print_table_by_top_dataclass(sd: MyTopDataClassDataset) -> pd.DataFrame: | ||
t = sd.open(pd.DataFrame).all() | ||
print("MyTopDataClassDataset dataframe: \n", t) | ||
return t | ||
|
||
|
||
@task() | ||
def print_table_by_top_dict(sd: MyTopDictDataset) -> pd.DataFrame: | ||
t = sd.open(pd.DataFrame).all() | ||
print("MyTopDictDataset dataframe: \n", t) | ||
return t | ||
|
||
|
||
@task() | ||
def print_table_by_second_dataclass(sd: MySecondDataClassDataset) -> pd.DataFrame: | ||
t = sd.open(pd.DataFrame).all() | ||
print("MySecondDataClassDataset dataframe: \n", t) | ||
return t | ||
|
||
|
||
@task() | ||
def print_table_by_nested_dataclass(sd: MyNestedDataClassDataset) -> pd.DataFrame: | ||
t = sd.open(pd.DataFrame).all() | ||
print("MyNestedDataClassDataset dataframe: \n", t) | ||
return t | ||
|
||
|
||
@workflow | ||
def wf(): | ||
pd_sd = create_pd_table() | ||
print_table_by_arg(sd=pd_sd) | ||
print_table_by_dict(sd=pd_sd) | ||
print_table_by_list_dict(sd=pd_sd) | ||
print_table_by_top_dataclass(sd=pd_sd) | ||
print_table_by_top_dict(sd=pd_sd) | ||
print_table_by_second_dataclass(sd=pd_sd) | ||
print_table_by_nested_dataclass(sd=pd_sd) | ||
bq_sd = create_pd_table() | ||
print_table_by_arg(sd=bq_sd) | ||
print_table_by_dict(sd=bq_sd) | ||
print_table_by_list_dict(sd=bq_sd) | ||
print_table_by_top_dataclass(sd=bq_sd) | ||
print_table_by_top_dict(sd=bq_sd) | ||
print_table_by_second_dataclass(sd=bq_sd) | ||
print_table_by_nested_dataclass(sd=bq_sd) | ||
np_sd = create_pd_table() | ||
print_table_by_arg(sd=np_sd) | ||
print_table_by_dict(sd=np_sd) | ||
print_table_by_list_dict(sd=np_sd) | ||
print_table_by_top_dataclass(sd=np_sd) | ||
print_table_by_top_dict(sd=np_sd) | ||
print_table_by_second_dataclass(sd=np_sd) | ||
print_table_by_nested_dataclass(sd=np_sd) | ||
ar_sd = create_pd_table() | ||
print_table_by_arg(sd=ar_sd) | ||
print_table_by_dict(sd=ar_sd) | ||
print_table_by_list_dict(sd=ar_sd) | ||
print_table_by_top_dataclass(sd=ar_sd) | ||
print_table_by_top_dict(sd=ar_sd) | ||
print_table_by_second_dataclass(sd=ar_sd) | ||
print_table_by_nested_dataclass(sd=ar_sd) | ||
|
||
|
||
def test_structured_dataset_wf(): | ||
wf() |
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Perhaps add more
nested
dataframes to cover extreme test cases.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could we just add your example to the unit test? we can add a new file (test_structured_dataset_workflow_with_nested_type.py) to
tests/flytekit/unit/types/structured_dataset
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
done