Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@

#### Bug Fixes

- Fixed a bug when reading xml with custom schema, result include element attributes when column is not `StructType` type.

#### Improvements

### Snowpark pandas API Updates
Expand Down
8 changes: 7 additions & 1 deletion src/snowflake/snowpark/_internal/xml_reader.py
Original file line number Diff line number Diff line change
Expand Up @@ -346,16 +346,22 @@ def get_text(element: ET.Element) -> Optional[str]:
for child in children:
tag = child.tag
child_result_template = None
child_exclude_attributes = exclude_attributes
if result_template is not None:
# skip if not in custom schema
if tag.lower() not in norm_name_to_ori_name:
continue
tag = norm_name_to_ori_name[tag.lower()]
child_result_template = result_template[tag]
# result_template is not None means custom schema is present
# child_result_template is None in this case means that
# child schema not treated as struct type, thus element attributes should be excluded
if child_result_template is None:
child_exclude_attributes = True
child_dict = element_to_dict_or_str(
child,
attribute_prefix=attribute_prefix,
exclude_attributes=exclude_attributes,
exclude_attributes=child_exclude_attributes,
value_tag=value_tag,
null_value=null_value,
ignore_surrounding_whitespace=ignore_surrounding_whitespace,
Expand Down
56 changes: 56 additions & 0 deletions tests/integ/test_xml_reader_row_tag.py
Original file line number Diff line number Diff line change
Expand Up @@ -691,3 +691,59 @@ def test_user_schema_without_rowtag(session):
ValueError, match="When reading XML with user schema, rowtag must be set."
):
session.read.schema(user_schema).xml(f"@{tmp_stage_name}/{test_file_books_xml}")


def test_value_tag_custom_schema(session):
test_schema1 = StructType(
[
StructField("num", StringType(), True),
StructField("str1", StringType(), True),
StructField("str2", StringType(), True),
StructField(
"str3",
StructType(
[
StructField(
"_VALUE", StringType(), True
), # element text (because str3 has an attribute)
StructField(
"_id", StringType(), True
), # attribute id (default attributePrefix is "_")
]
),
True,
),
]
)
row_tag = "test"
df = (
session.read.option("rowTag", row_tag)
.schema(test_schema1)
.xml(f"@{tmp_stage_name}/{test_file_null_value_xml}")
)
Utils.check_answer(
df,
[
Row(
num="1",
str1="NULL",
str2=None,
str3='{\n "_VALUE": "xxx",\n "_id": "empty"\n}',
)
],
)

test_schema2 = StructType(
[
StructField("num", StringType(), True),
StructField("str1", StringType(), True),
StructField("str2", StringType(), True),
StructField("str3", StringType(), True),
]
)
df = (
session.read.option("rowTag", row_tag)
.schema(test_schema2)
.xml(f"@{tmp_stage_name}/{test_file_null_value_xml}")
)
Utils.check_answer(df, [Row(num="1", str1="NULL", str2=None, str3="xxx")])
66 changes: 66 additions & 0 deletions tests/unit/test_xml_reader.py
Original file line number Diff line number Diff line change
Expand Up @@ -925,3 +925,69 @@ def test_schema_string_to_result_dict_and_struct_type(session):
"description": None,
"map_type": None,
}


def test_user_schema_value_tag():
xml_string = """
<test>
<num>1</num>
<str1>NULL</str1>
<str2></str2>
<str3 id="empty">xxx</str3>
</test>
"""

user_schema = StructType(
[
StructField("num", StringType(), True),
StructField("str1", StringType(), True),
StructField("str2", StringType(), True),
StructField(
"str3",
StructType(
[
StructField(
"_VALUE", StringType(), True
), # element text (because str3 has an attribute)
StructField(
"_id", StringType(), True
), # attribute id (default attributePrefix is "_")
]
),
True,
),
]
)

result_template = struct_type_to_result_template(user_schema)

element = ET.fromstring(xml_string)
res = element_to_dict_or_str(element, result_template=result_template)
assert result_template == {
"NUM": None,
"STR1": None,
"STR2": None,
"STR3": {"_VALUE": None, "_ID": None},
}
assert res == {
"NUM": "1",
"STR1": "NULL",
"STR2": None,
"STR3": {"_VALUE": "xxx", "_ID": "empty"},
}

user_schema = StructType(
[
StructField("num", StringType(), True),
StructField("str1", StringType(), True),
StructField("str2", StringType(), True),
StructField("str3", StringType(), True),
]
)

result_template = struct_type_to_result_template(user_schema)

element = ET.fromstring(xml_string)
res = element_to_dict_or_str(element, result_template=result_template)
assert result_template == {"NUM": None, "STR1": None, "STR2": None, "STR3": None}
assert res == {"NUM": "1", "STR1": "NULL", "STR2": None, "STR3": "xxx"}