-
Notifications
You must be signed in to change notification settings - Fork 122
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
SNOW-1748140: Modify schema_expression to be structured type aware. #2659
Merged
sfc-gh-jrose
merged 9 commits into
main
from
jrose_snow_1748140_schema_expression_for_struct_types
Nov 25, 2024
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
548c5e6
SNOW-1748140: Modify schema_expression to be structured type aware.
sfc-gh-jrose b3195f6
test fix
sfc-gh-jrose e417cc8
Merge branch 'main' into jrose_snow_1748140_schema_expression_for_str…
sfc-gh-jrose 13f1381
Merge branch 'main' into jrose_snow_1748140_schema_expression_for_str…
sfc-gh-jrose 69b511b
Merge branch 'main' into jrose_snow_1748140_schema_expression_for_str…
sfc-gh-jrose 0256a33
Review Feedback
sfc-gh-jrose 8a40b80
Merge branch 'main' into jrose_snow_1748140_schema_expression_for_str…
sfc-gh-jrose 4c99937
Add nested example to test.
sfc-gh-jrose 4824f2b
coverage
sfc-gh-jrose 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
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 |
---|---|---|
|
@@ -601,7 +601,7 @@ def test_iceberg_nested_fields( | |
|
||
|
||
@pytest.mark.skip( | ||
reason="SNOW-1748140: Need to handle structured types in datatype_mapper" | ||
reason="SNOW-1819531: Error in _contains_external_cte_ref when analyzing lqb" | ||
) | ||
def test_struct_dtype_iceberg_lqb( | ||
structured_type_session, local_testing_mode, structured_type_support | ||
|
@@ -970,3 +970,129 @@ def test_structured_type_print_schema( | |
df._format_schema(1, translate_columns={'"MAP"': '"map"'}) | ||
== 'root\n |-- "map": MapType (nullable = True)' | ||
) | ||
|
||
|
||
@pytest.mark.skipif( | ||
"config.getoption('local_testing_mode', default=False)", | ||
reason="local testing does not fully support structured types yet.", | ||
) | ||
def test_structured_type_schema_expression( | ||
structured_type_session, local_testing_mode, structured_type_support | ||
): | ||
if not structured_type_support: | ||
pytest.skip("Test requires structured type support.") | ||
|
||
table_name = f"snowpark_schema_expresion_test_{uuid.uuid4().hex[:5]}".upper() | ||
non_null_table_name = ( | ||
f"snowpark_schema_expresion_nonnull_test_{uuid.uuid4().hex[:5]}".upper() | ||
) | ||
nested_table_name = ( | ||
f"snowpark_schema_expresion_nested_test_{uuid.uuid4().hex[:5]}".upper() | ||
) | ||
|
||
expected_schema = StructType( | ||
[ | ||
StructField( | ||
"MAP", | ||
MapType(StringType(), DoubleType(), structured=True), | ||
nullable=True, | ||
), | ||
StructField("ARR", ArrayType(DoubleType(), structured=True), nullable=True), | ||
StructField( | ||
"OBJ", | ||
StructType( | ||
[ | ||
StructField("FIELD1", StringType(), nullable=True), | ||
StructField("FIELD2", DoubleType(), nullable=True), | ||
], | ||
structured=True, | ||
), | ||
nullable=True, | ||
), | ||
] | ||
) | ||
|
||
expected_non_null_schema = StructType( | ||
[ | ||
StructField( | ||
"MAP", | ||
MapType(StringType(), DoubleType(), structured=True), | ||
nullable=False, | ||
), | ||
StructField( | ||
"ARR", ArrayType(DoubleType(), structured=True), nullable=False | ||
), | ||
StructField( | ||
"OBJ", | ||
StructType( | ||
[ | ||
StructField("FIELD1", StringType(), nullable=False), | ||
StructField("FIELD2", DoubleType(), nullable=False), | ||
], | ||
structured=True, | ||
), | ||
nullable=False, | ||
), | ||
] | ||
) | ||
|
||
expected_nested_schema = StructType( | ||
[ | ||
StructField( | ||
"MAP", | ||
MapType( | ||
StringType(), | ||
StructType( | ||
[StructField("ARR", ArrayType(DoubleType(), structured=True))], | ||
structured=True, | ||
), | ||
structured=True, | ||
), | ||
) | ||
] | ||
) | ||
|
||
try: | ||
# SNOW-1819428: Nullability doesn't seem to be respected when creating | ||
# a structured type dataframe so use a table instead. | ||
structured_type_session.sql( | ||
f"create table {table_name} (MAP MAP(VARCHAR, DOUBLE), ARR ARRAY(DOUBLE), " | ||
"OBJ OBJECT(FIELD1 VARCHAR, FIELD2 DOUBLE))" | ||
).collect() | ||
structured_type_session.sql( | ||
f"create table {non_null_table_name} (MAP MAP(VARCHAR, DOUBLE) NOT NULL, " | ||
"ARR ARRAY(DOUBLE) NOT NULL, OBJ OBJECT(FIELD1 VARCHAR NOT NULL, FIELD2 " | ||
"DOUBLE NOT NULL) NOT NULL)" | ||
).collect() | ||
structured_type_session.sql( | ||
f"create table {nested_table_name} (MAP MAP(VARCHAR, OBJECT(ARR ARRAY(DOUBLE))))" | ||
).collect() | ||
|
||
table = structured_type_session.table(table_name) | ||
non_null_table = structured_type_session.table(non_null_table_name) | ||
nested_table = structured_type_session.table(nested_table_name) | ||
|
||
assert table.schema == expected_schema | ||
assert non_null_table.schema == expected_non_null_schema | ||
assert nested_table.schema == expected_nested_schema | ||
|
||
# Dataframe.union forces a schema_expression call | ||
assert table.union(table).schema == expected_schema | ||
# Functions used in schema generation don't respect nested nullability so compare query string instead | ||
non_null_union = non_null_table.union(non_null_table) | ||
assert non_null_union._plan.schema_query == ( | ||
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 also create a test case for nested array and object? like 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. Added |
||
"( SELECT object_construct_keep_null('a' :: STRING (16777216), 0 :: DOUBLE) :: " | ||
'MAP(STRING(16777216), DOUBLE) AS "MAP", to_array(0 :: DOUBLE) :: ARRAY(DOUBLE) AS "ARR",' | ||
" object_construct_keep_null('FIELD1', 'a' :: STRING (16777216), 'FIELD2', 0 :: " | ||
'DOUBLE) :: OBJECT(FIELD1 STRING(16777216), FIELD2 DOUBLE) AS "OBJ") UNION ( SELECT ' | ||
"object_construct_keep_null('a' :: STRING (16777216), 0 :: DOUBLE) :: " | ||
'MAP(STRING(16777216), DOUBLE) AS "MAP", to_array(0 :: DOUBLE) :: ARRAY(DOUBLE) AS "ARR", ' | ||
"object_construct_keep_null('FIELD1', 'a' :: STRING (16777216), 'FIELD2', 0 :: " | ||
'DOUBLE) :: OBJECT(FIELD1 STRING(16777216), FIELD2 DOUBLE) AS "OBJ")' | ||
) | ||
|
||
assert nested_table.union(nested_table).schema == expected_nested_schema | ||
finally: | ||
Utils.drop_table(structured_type_session, table_name) | ||
Utils.drop_table(structured_type_session, non_null_table_name) | ||
Utils.drop_table(structured_type_session, nested_table_name) |
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.
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.
should we determine whether keeping null values based on
is_nullable
?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.
If we don't keep nulls and either the key or value gets evaluated to a
NULL ::
type statement then that field would be dropped from the schema altogether. For this reason I think we always want nulls.