mirror of
https://github.com/bytedance/deer-flow.git
synced 2026-11-27 17:00:37 +00:00
fix(skills): import the literal CSV path during data analysis (#8703)
Co-authored-by: hehe764 <243333382+hehe764@users.noreply.github.com>
This commit is contained in:
parent
0890e94e84
commit
312b6fc1ec
@ -56,6 +56,8 @@ and leading-zero identifiers while importing cell values into DuckDB. See the
|
||||
[data-analysis setup instructions](../../skills/public/data-analysis/SKILL.md)
|
||||
for the additional dependencies. Legacy `.xls` and CSV input do not require
|
||||
DuckDB extensions and can be analyzed offline with an empty extension cache.
|
||||
CSV paths passed to `--files` select individual files. For example, quote
|
||||
`'orders[1].csv'` in the shell to import that file even beside `orders1.csv`.
|
||||
The `.xlsx` reader continues to use the spatial extension. Legacy workbooks that
|
||||
cannot be opened are reported and skipped while other input files continue;
|
||||
analysis fails if no tables can be loaded.
|
||||
|
||||
@ -132,7 +132,7 @@ NULL END AS ratio`. Finite numbers, nulls, and text such as `'NaN'` remain valid
|
||||
|
||||
| Parameter | Required | Description |
|
||||
|-----------|----------|-------------|
|
||||
| `--files` | Yes | Space-separated paths to Excel/CSV files |
|
||||
| `--files` | Yes | Space-separated paths to Excel/CSV files. Quote CSV paths such as `'orders[1].csv'` so the shell passes the literal name. |
|
||||
| `--action` | Yes | One of: `inspect`, `query`, `summary` |
|
||||
| `--sql` | For `query` | SQL query to execute |
|
||||
| `--table` | For `summary` | Table/sheet name to summarize |
|
||||
|
||||
@ -6,6 +6,7 @@ Supports schema inspection, SQL queries, statistical summaries, and result expor
|
||||
"""
|
||||
|
||||
import argparse
|
||||
import glob
|
||||
import hashlib
|
||||
import json
|
||||
import logging
|
||||
@ -49,7 +50,11 @@ def compute_files_hash(files: list[str]) -> str:
|
||||
name = os.fsencode(os.path.basename(file_path))
|
||||
hasher.update(len(name).to_bytes(8, "big"))
|
||||
hasher.update(name)
|
||||
if os.path.splitext(file_path)[1].lower() == ".xls":
|
||||
extension = os.path.splitext(file_path)[1].lower()
|
||||
if extension == ".csv":
|
||||
# Older generations may contain rows from a glob-matched neighbor.
|
||||
hasher.update(b"literal-csv-path\0")
|
||||
elif extension == ".xls":
|
||||
# Rebuild legacy caches that might have dropped a mixed-type sheet.
|
||||
hasher.update(b"legacy-xls-v2\0")
|
||||
file_hasher = hashlib.sha256()
|
||||
@ -359,6 +364,29 @@ def _load_legacy_excel(
|
||||
logger.warning(f" Failed to load sheet '{sheet_name}': {e}")
|
||||
|
||||
|
||||
@contextmanager
|
||||
def _csv_source(file_path: str):
|
||||
"""Give DuckDB one local file, even when its name looks like a glob."""
|
||||
path = str(Path(file_path).absolute())
|
||||
pattern = glob.escape(path)
|
||||
if pattern == path:
|
||||
yield path
|
||||
elif int(duckdb.__version__.split(".")[0]) >= 2:
|
||||
# DuckDB accepts a list of explicit file entries without expanding globs.
|
||||
yield [{"filename": path}]
|
||||
elif os.name == "nt" or "\\" not in path:
|
||||
yield pattern
|
||||
else:
|
||||
# Older glob readers split even POSIX filenames on literal backslashes.
|
||||
with tempfile.TemporaryDirectory() as directory:
|
||||
alias = Path(directory) / "input.csv"
|
||||
try:
|
||||
alias.symlink_to(path)
|
||||
except OSError:
|
||||
shutil.copyfile(path, alias)
|
||||
yield glob.escape(str(alias))
|
||||
|
||||
|
||||
def _load_csv(
|
||||
con: duckdb.DuckDBPyConnection, file_path: str, table_map: dict[str, str]
|
||||
) -> None:
|
||||
@ -367,13 +395,11 @@ def _load_csv(
|
||||
alias, table_name = _allocate_table_names(base_name, table_map)
|
||||
|
||||
try:
|
||||
con.execute(
|
||||
f"""
|
||||
CREATE TABLE {_quote_identifier(table_name)} AS
|
||||
SELECT * FROM read_csv_auto(?)
|
||||
""",
|
||||
[file_path],
|
||||
)
|
||||
with _csv_source(file_path) as source:
|
||||
con.execute(
|
||||
f"CREATE TABLE {_quote_identifier(table_name)} AS SELECT * FROM read_csv_auto(?)",
|
||||
[source],
|
||||
)
|
||||
table_map[alias] = table_name
|
||||
row_count = con.execute(
|
||||
f"SELECT COUNT(*) FROM {_quote_identifier(table_name)}"
|
||||
|
||||
@ -270,6 +270,38 @@ def test_csv_paths_are_bound_as_values(analysis, tmp_path):
|
||||
assert con.execute("SELECT value FROM sales_2026").fetchall() == [(7,)]
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
"requested, neighbor",
|
||||
[
|
||||
("orders[1].csv", "orders1.csv"),
|
||||
("batch[1]/orders.csv", "batch1/orders.csv"),
|
||||
pytest.param(
|
||||
"orders*?.csv",
|
||||
"orders12.csv",
|
||||
marks=pytest.mark.skipif(
|
||||
os.name == "nt", reason="invalid Windows filename"
|
||||
),
|
||||
),
|
||||
pytest.param(
|
||||
r"west\orders[1].csv",
|
||||
"west/orders1.csv",
|
||||
marks=pytest.mark.skipif(
|
||||
os.name == "nt", reason="backslash is a separator"
|
||||
),
|
||||
),
|
||||
],
|
||||
)
|
||||
def test_csv_import_reads_only_the_named_file(analysis, tmp_path, requested, neighbor):
|
||||
selected = tmp_path / requested
|
||||
other = tmp_path / neighbor
|
||||
csv_file(selected.parent, selected.name, 20)
|
||||
csv_file(other.parent, other.name, 900)
|
||||
with duckdb.connect() as con:
|
||||
tables = analysis["load_files"](con, [str(selected)])
|
||||
table = tables[selected.stem]
|
||||
assert con.execute(f'SELECT value FROM "{table}"').fetchall() == [(20,)]
|
||||
|
||||
|
||||
def test_inspect_and_summary_handle_quoted_headers(analysis, tmp_path):
|
||||
import csv
|
||||
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user