-
Notifications
You must be signed in to change notification settings - Fork 146
SNOW-3266242: Support TRY_CAST with user-provided schema in DataFrameReader #4138
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 all commits
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 |
|---|---|---|
|
|
@@ -797,6 +797,7 @@ def csv(self, path: str, _emit_ast: bool = True) -> DataFrame: | |
| self._file_type = "CSV" | ||
|
|
||
| schema_to_cast, transformations = None, None | ||
| use_user_schema = False | ||
|
|
||
| if not self._user_schema: | ||
| if not self._infer_schema: | ||
|
|
@@ -833,7 +834,19 @@ def csv(self, path: str, _emit_ast: bool = True) -> DataFrame: | |
| transformations = [] | ||
| else: | ||
| self._cur_options["INFER_SCHEMA"] = False | ||
| schema = self._user_schema._to_attributes() | ||
| try_cast = self._cur_options.get("TRY_CAST", False) | ||
| if try_cast: | ||
| ( | ||
| schema, | ||
| schema_to_cast, | ||
| transformations, | ||
| ) = self._get_schema_from_csv_user_input( | ||
|
Contributor
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. can you check with @sfc-gh-skumbham to understand the scope? is this requirement just for csv?
Collaborator
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. I have confirmed with him that this PR's scope is limited to CSV |
||
| self._user_schema, | ||
| try_cast, | ||
| ) | ||
| use_user_schema = True | ||
| else: | ||
| schema = self._user_schema._to_attributes() | ||
|
|
||
| metadata_project, metadata_schema = self._get_metadata_project_and_schema() | ||
|
|
||
|
|
@@ -859,6 +872,7 @@ def csv(self, path: str, _emit_ast: bool = True) -> DataFrame: | |
| transformations=transformations, | ||
| metadata_project=metadata_project, | ||
| metadata_schema=metadata_schema, | ||
| use_user_schema=use_user_schema, | ||
| ), | ||
| analyzer=self._session._analyzer, | ||
| ), | ||
|
|
@@ -879,6 +893,7 @@ def csv(self, path: str, _emit_ast: bool = True) -> DataFrame: | |
| transformations=transformations, | ||
| metadata_project=metadata_project, | ||
| metadata_schema=metadata_schema, | ||
| use_user_schema=use_user_schema, | ||
| ), | ||
| _ast_stmt=stmt, | ||
| _emit_ast=_emit_ast, | ||
|
|
@@ -1387,6 +1402,37 @@ def _infer_schema_for_file_format( | |
|
|
||
| return new_schema, schema_to_cast, read_file_transformations, None | ||
|
|
||
| def _get_schema_from_csv_user_input( | ||
| self, user_schema: StructType, try_cast: bool | ||
| ) -> Tuple[List, List, List]: | ||
| """ | ||
| This function accept a user input structtype and return schemas needed for reading CSV file. | ||
| CSV files are processed differently than semi-structured file so need a different helper function. | ||
| """ | ||
| schema_to_cast = [] | ||
| transformations = [] | ||
| new_schema = [] | ||
| for index, field in enumerate(user_schema.fields, start=1): | ||
| new_schema.append( | ||
| Attribute( | ||
| field.column_identifier.quoted_name, | ||
| field.datatype, | ||
| field.nullable, | ||
| ) | ||
| ) | ||
| sf_type = convert_sp_to_sf_type(field.datatype) | ||
| source_column = f"${index}" | ||
| identifier = ( | ||
| f"TRY_CAST({source_column} AS {sf_type})" | ||
| if try_cast | ||
| else f"{source_column}::{sf_type}" | ||
| ) | ||
| schema_to_cast.append((identifier, field.name)) | ||
| transformations.append(sql_expr(identifier)) | ||
|
|
||
| read_file_transformations = [t._expression.sql for t in transformations] | ||
| return new_schema, schema_to_cast, read_file_transformations | ||
|
|
||
| def _get_schema_from_user_input( | ||
| self, user_schema: StructType | ||
| ) -> Tuple[List, List, List]: | ||
|
|
||
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.
why we need to introduce this new var?
was there a bug before?
Uh oh!
There was an error while loading. Please reload this page.
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.
technically there is a bug before,
in the analyzer: https://github.com/snowflakedb/snowpark-python/blob/main/src/snowflake/snowpark/_internal/analyzer/snowflake_plan.py#L1960
we need this bool value to actually apply try_cast to the result, otherwise try_cast is ignored