Skip to content

Commit 4b5feb9

Browse files
authored
SNOW-2098847: Do not use "scoped temporary" stage for XML reader in stored procedure (#3354)
1 parent 1973988 commit 4b5feb9

File tree

2 files changed

+33
-5
lines changed

2 files changed

+33
-5
lines changed

src/snowflake/snowpark/dataframe_reader.py

Lines changed: 33 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,11 +54,14 @@
5454
convert_sf_to_sp_type,
5555
convert_sp_to_sf_type,
5656
)
57+
from snowflake.snowpark._internal.udf_utils import get_types_from_type_hints
5758
from snowflake.snowpark._internal.utils import (
59+
STAGE_PREFIX,
5860
XML_ROW_TAG_STRING,
5961
XML_ROW_DATA_COLUMN_NAME,
6062
XML_READER_FILE_PATH,
6163
XML_READER_API_SIGNATURE,
64+
XML_READER_SQL_COMMENT,
6265
INFER_SCHEMA_FORMAT_TYPES,
6366
SNOWFLAKE_PATH_PREFIXES,
6467
TempObjectType,
@@ -70,6 +73,7 @@
7073
private_preview,
7174
random_name_for_temp_object,
7275
warning,
76+
is_in_stored_procedure,
7377
)
7478
from snowflake.snowpark.column import METADATA_COLUMN_TYPES, Column, _to_col_if_str
7579
from snowflake.snowpark.dataframe import DataFrame
@@ -1106,13 +1110,40 @@ def _read_semi_structured_file(self, path: str, format: str) -> DataFrame:
11061110
"rowTag",
11071111
"rowTag for reading XML file is in private preview since 1.31.0. Do not use it in production.",
11081112
)
1113+
1114+
if is_in_stored_procedure(): # pragma: no cover
1115+
# create a temp stage for udtf import files
1116+
# we have to use "temp" object instead of "scoped temp" object in stored procedure
1117+
# so we need to upload the file to the temp stage first to use register_from_file
1118+
temp_stage = random_name_for_temp_object(TempObjectType.STAGE)
1119+
sql_create_temp_stage = f"create temp stage if not exists {temp_stage} {XML_READER_SQL_COMMENT}"
1120+
self._session.sql(sql_create_temp_stage, _emit_ast=False).collect(
1121+
_emit_ast=False
1122+
)
1123+
self._session._conn.upload_file(
1124+
XML_READER_FILE_PATH,
1125+
temp_stage,
1126+
compress_data=False,
1127+
overwrite=True,
1128+
skip_upload_on_content_match=True,
1129+
)
1130+
python_file_path = f"{STAGE_PREFIX}{temp_stage}/{os.path.basename(XML_READER_FILE_PATH)}"
1131+
else:
1132+
python_file_path = XML_READER_FILE_PATH
1133+
1134+
# create udtf
1135+
handler_name = "XMLReader"
1136+
_, input_types = get_types_from_type_hints(
1137+
(XML_READER_FILE_PATH, handler_name), TempObjectType.TABLE_FUNCTION
1138+
)
11091139
output_schema = StructType(
11101140
[StructField(XML_ROW_DATA_COLUMN_NAME, VariantType(), True)]
11111141
)
11121142
xml_reader_udtf = self._session.udtf.register_from_file(
1113-
XML_READER_FILE_PATH,
1114-
"XMLReader",
1143+
python_file_path,
1144+
handler_name,
11151145
output_schema=output_schema,
1146+
input_types=input_types,
11161147
packages=["snowflake-snowpark-python"],
11171148
replace=True,
11181149
)

tests/integ/scala/test_dataframe_reader_suite.py

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -136,9 +136,6 @@ def get_df_from_reader_and_file_format(reader, file_format):
136136

137137
@pytest.fixture(scope="module", autouse=True)
138138
def setup(session, resources_path, local_testing_mode):
139-
# TODO SNOW-2098847: remove this workaround after fixing the issue
140-
session._use_scoped_temp_objects = False
141-
142139
test_files = TestFiles(resources_path)
143140
if not local_testing_mode:
144141
Utils.create_stage(session, tmp_stage_name1, is_temporary=True)

0 commit comments

Comments
 (0)