-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Fixes #27950: [Datalake] JSON columns incorrectly typed as STRING for empty dict values #27951
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
base: main
Are you sure you want to change the base?
Changes from 1 commit
c14e05b
f17df88
afd7898
a0f0f01
6ab38c8
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -226,7 +226,7 @@ | |
|
|
||
| @staticmethod | ||
| def _get_data_frame( | ||
| data_frame: Union[List["DataFrame"], "DataFrame"], # noqa: F821, UP006 | ||
|
Check warning on line 229 in ingestion/src/metadata/utils/datalake/datalake_utils.py
|
||
| sample: bool, | ||
| shuffle: bool, # noqa: F821, RUF100 | ||
| ): | ||
|
|
@@ -282,7 +282,7 @@ | |
| return self._get_columns(self.data_frame) | ||
|
|
||
| @classmethod | ||
| def _get_columns(cls, data_frame: "DataFrame"): # noqa: F821 | ||
|
Check failure on line 285 in ingestion/src/metadata/utils/datalake/datalake_utils.py
|
||
| """ | ||
| method to process column details. | ||
|
|
||
|
|
@@ -334,14 +334,19 @@ | |
| """ | ||
| data_type = None # default to string | ||
| try: | ||
| if data_frame[column_name].dtypes.name == "object" and any(data_frame[column_name].dropna().values): | ||
| if data_frame[column_name].dtypes.name == "object" and len(data_frame[column_name].dropna()) > 0: | ||
| try: | ||
| # Safely evaluate the input string | ||
| df_row_val_list = data_frame[column_name].dropna().values[:1000] | ||
| parsed_object_datatype_list = [] | ||
| for df_row_val in df_row_val_list: | ||
| try: | ||
| parsed_object_datatype_list.append(type(ast.literal_eval(str(df_row_val))).__name__.lower()) | ||
| if isinstance(df_row_val, (dict, list)): | ||
| parsed_object_datatype_list.append(type(df_row_val).__name__.lower()) | ||
| else: | ||
| parsed_object_datatype_list.append( | ||
| type(ast.literal_eval(str(df_row_val))).__name__.lower() | ||
| ) | ||
|
Comment on lines
+369
to
+378
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. pre-existing behavior, out of scope for this PR (which targets the empty-dict typing bug). Worth a follow-up issue. |
||
| except (ValueError, SyntaxError): | ||
| # we try to parse the value as a datetime, if it fails, we fallback to string | ||
| # as literal_eval will fail for string | ||
|
|
@@ -354,11 +359,11 @@ | |
| if not str(df_row_val).isnumeric(): | ||
| # check if the row value is time | ||
| try: | ||
| datetime.strptime(df_row_val, "%H:%M:%S").time() | ||
|
Check failure on line 362 in ingestion/src/metadata/utils/datalake/datalake_utils.py
|
||
| dtype_ = "timedelta[ns]" | ||
| except (ValueError, TypeError): | ||
| # check if the row value is date / time / datetime | ||
| type(parse(df_row_val)).__name__.lower() | ||
|
Check failure on line 366 in ingestion/src/metadata/utils/datalake/datalake_utils.py
|
||
| dtype_ = "datetime64[ns]" | ||
| parsed_object_datatype_list.append(dtype_) | ||
| except (ParserError, TypeError): | ||
|
|
@@ -394,7 +399,7 @@ | |
| return data_type or DataType.STRING | ||
|
|
||
| @classmethod | ||
| def unique_json_structure(cls, dicts: List[Dict]) -> Dict: # noqa: UP006 | ||
|
Check failure on line 402 in ingestion/src/metadata/utils/datalake/datalake_utils.py
|
||
| """Given a sample of `n` json objects, return a json object that represents the unique | ||
| structure of all `n` objects. Note that the type of the key will be that of | ||
| the last object seen in the sample. | ||
|
|
@@ -459,16 +464,23 @@ | |
| from pandas import Series # pylint: disable=import-outside-toplevel # noqa: PLC0415 | ||
|
|
||
| json_column = cast(Series, json_column) # noqa: TC006 | ||
| try: | ||
| json_column = json_column.apply(json.loads) | ||
| except TypeError as exc: | ||
| # if values are not strings, we will assume they are already json objects | ||
| # based on the read class logic | ||
| logger.debug( | ||
| f"TypeError while parsing JSON column children: {exc}. Assuming values are already JSON objects." | ||
| ) | ||
| json_structure = cls.unique_json_structure(json_column.values.tolist()) | ||
|
|
||
| dict_values = [] | ||
| for value in json_column.dropna().values: | ||
| if isinstance(value, dict): | ||
| dict_values.append(value) | ||
| elif isinstance(value, str): | ||
| try: | ||
| parsed = json.loads(value) | ||
| if isinstance(parsed, dict): | ||
| dict_values.append(parsed) | ||
| except (TypeError, json.JSONDecodeError): | ||
| pass | ||
|
|
||
| if not dict_values: | ||
| return [] | ||
|
|
||
| json_structure = cls.unique_json_structure(dict_values) | ||
| return cls.construct_json_column_children(json_structure) | ||
|
|
||
| @classmethod | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,37 @@ | ||
| { | ||
| "metadata": { | ||
| "dbt_schema_version": "https://schemas.getdbt.com/dbt/catalog/v1.json", | ||
| "dbt_version": "1.5.0", | ||
| "generated_at": "2024-01-01T00:00:00.000000Z", | ||
| "invocation_id": "abc-123", | ||
| "env": {} | ||
| }, | ||
| "nodes": { | ||
| "model.my_project.customers": { | ||
| "metadata": {"type": "VIEW", "schema": "public", "name": "customers"}, | ||
| "columns": { | ||
| "customer_id": {"type": "integer", "index": 1, "name": "customer_id"}, | ||
| "name": {"type": "text", "index": 2, "name": "name"} | ||
| }, | ||
| "stats": {}, | ||
| "unique_id": "model.my_project.customers" | ||
| }, | ||
| "model.my_project.orders": { | ||
| "metadata": {"type": "VIEW", "schema": "public", "name": "orders"}, | ||
| "columns": { | ||
| "order_id": {"type": "integer", "index": 1, "name": "order_id"} | ||
| }, | ||
| "stats": {}, | ||
| "unique_id": "model.my_project.orders" | ||
| } | ||
| }, | ||
| "sources": { | ||
| "source.my_project.raw.customers": { | ||
| "metadata": {"type": "TABLE", "schema": "raw", "name": "customers"}, | ||
| "columns": {}, | ||
| "stats": {}, | ||
| "unique_id": "source.my_project.raw.customers" | ||
| } | ||
| }, | ||
| "errors": null | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,42 @@ | ||
| { | ||
| "metadata": { | ||
| "dbt_schema_version": "https://schemas.getdbt.com/dbt/manifest/v11/manifest.json", | ||
| "dbt_version": "1.5.0", | ||
| "generated_at": "2024-01-01T00:00:00.000000Z", | ||
| "invocation_id": "abc-123", | ||
| "env": {}, | ||
| "project_name": "my_project", | ||
| "project_id": "xyz", | ||
| "adapter_type": "postgres", | ||
| "credential_id": null, | ||
| "profile_name": "my_profile" | ||
| }, | ||
| "nodes": { | ||
| "model.my_project.customers": { | ||
| "name": "customers", | ||
| "description": "Customer records", | ||
| "unique_id": "model.my_project.customers", | ||
| "fqn": ["my_project", "customers"] | ||
| } | ||
| }, | ||
| "sources": { | ||
| "source.my_project.raw.customers": { | ||
| "name": "customers", | ||
| "description": "Raw customer data", | ||
| "unique_id": "source.my_project.raw.customers" | ||
| } | ||
| }, | ||
| "macros": {}, | ||
| "docs": {}, | ||
| "exposures": {}, | ||
| "metrics": {}, | ||
| "groups": {}, | ||
| "selectors": {}, | ||
| "disabled": {}, | ||
| "parent_map": {}, | ||
| "child_map": {}, | ||
| "group_map": {}, | ||
| "saved_queries": {}, | ||
| "semantic_models": {}, | ||
| "unit_tests": {} | ||
| } |
Uh oh!
There was an error while loading. Please reload this page.