Skip to content

Commit d5cf2fd

Browse files
committed
SNOW-3385961: improve INFER_SCHEMA zero-row FileNotFoundError message
When INFER_SCHEMA returns zero rows, the reader used to raise: FileNotFoundError: Given path: '{path}' could not be found or is empty. Zero rows can also mean file-format options silently filtered every row/header (e.g. PARSE_HEADER on a file with a leading blank line, SKIP_HEADER exceeding row count, or ON_ERROR=CONTINUE dropping bad rows). The old message sends users chasing a path-not-found issue that isn't real. Keep the exception type (FileNotFoundError) for back-compat, but rewrite the message to surface both possibilities and append any applied PARSE_HEADER / SKIP_HEADER / ON_ERROR values that are already in scope (no extra round-trip). Update the existing integ assertions in test_filepath_not_exist_or_empty to match the new wording and to access the message via str(ex_info.value) so the full (longer) message is compared.
1 parent 0694dae commit d5cf2fd

2 files changed

Lines changed: 33 additions & 5 deletions

File tree

src/snowflake/snowpark/dataframe_reader.py

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1338,8 +1338,31 @@ def _infer_schema_for_file_format(
13381338
raise e
13391339

13401340
if len(results) == 0:
1341+
# Zero rows from INFER_SCHEMA can mean the path is empty/missing,
1342+
# or that file format options silently filtered every row/header
1343+
# (e.g. PARSE_HEADER on a file with a leading blank line,
1344+
# SKIP_HEADER exceeding row count, or ON_ERROR=CONTINUE dropping
1345+
# bad rows). Surface both possibilities so callers don't chase a
1346+
# phantom path-not-found issue when the real cause is a format
1347+
# option.
1348+
option_hints = []
1349+
for key in ("PARSE_HEADER", "SKIP_HEADER", "ON_ERROR"):
1350+
if key in format_type_options:
1351+
option_hints.append(f"{key}={format_type_options[key]}")
1352+
elif key in infer_schema_options:
1353+
option_hints.append(f"{key}={infer_schema_options[key]}")
1354+
hint_suffix = (
1355+
f" Applied file format options: {', '.join(option_hints)}."
1356+
if option_hints
1357+
else ""
1358+
)
13411359
raise FileNotFoundError(
1342-
f"Given path: '{path}' could not be found or is empty."
1360+
f"Given path: '{path}' returned no results from INFER_SCHEMA. "
1361+
"The path may be empty or missing, or file format options may "
1362+
"have filtered every row/header (e.g. PARSE_HEADER on a file "
1363+
"with a leading blank line, SKIP_HEADER exceeding row count, "
1364+
"or ON_ERROR=CONTINUE silently dropping bad rows). Check the "
1365+
"file contents and file format options." + hint_suffix
13431366
)
13441367
new_schema = []
13451368
schema_to_cast = []

tests/integ/scala/test_dataframe_reader_suite.py

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2119,18 +2119,23 @@ def test_filepath_not_exist_or_empty(session):
21192119
session.read.option("PARSE_HEADER", True).option("INFER_SCHEMA", True).csv(
21202120
empty_file_path
21212121
)
2122-
assert f"Given path: '{empty_file_path}' could not be found or is empty." in str(
2123-
ex_info
2122+
assert (
2123+
f"Given path: '{empty_file_path}' returned no results from INFER_SCHEMA."
2124+
in str(ex_info.value)
21242125
)
2126+
# Message should mention file format options so users don't chase a phantom
2127+
# path-not-found when the real cause is PARSE_HEADER/SKIP_HEADER/ON_ERROR.
2128+
assert "file format options" in str(ex_info.value)
21252129

21262130
with pytest.raises(FileNotFoundError) as ex_info:
21272131
session.read.option("PARSE_HEADER", True).option("INFER_SCHEMA", True).csv(
21282132
not_exist_file_path
21292133
)
21302134
assert (
2131-
f"Given path: '{not_exist_file_path}' could not be found or is empty."
2132-
in str(ex_info)
2135+
f"Given path: '{not_exist_file_path}' returned no results from INFER_SCHEMA."
2136+
in str(ex_info.value)
21332137
)
2138+
assert "file format options" in str(ex_info.value)
21342139

21352140

21362141
@pytest.mark.skipif(

0 commit comments

Comments
 (0)