Skip to content
Draft
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
23 changes: 9 additions & 14 deletions pysus/api/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -820,14 +820,13 @@ def read_parquet(

def get_columns(path: Path) -> set[tuple[str, str]]:
"""Return the schema of a Parquet file as (name, type) pairs."""
result = duckdb.execute(f"SELECT * FROM '{path}' LIMIT 0")
result = duckdb.execute(
"SELECT * FROM read_parquet($1) LIMIT 0", [[str(path)]]
)
return {(col[0], str(col[1])) for col in result.description}

if len(paths) == 1:
query = f"SELECT * FROM '{paths[0]}'"
else:
paths_str = ", ".join(f"'{p}'" for p in paths)
query = f"SELECT * FROM read_parquet([{paths_str}])"
params = [[str(p) for p in paths]]
query = "SELECT * FROM read_parquet($1)"

schemas = [get_columns(p) for p in paths]
common_columns = set.intersection(*schemas) if schemas else set()
Expand All @@ -847,22 +846,18 @@ def get_columns(path: Path) -> set[tuple[str, str]]:
if not common_columns:
return duckdb.execute("SELECT * WHERE 1=0")
cols = ", ".join(f'"{c[0]}"' for c in sorted(common_columns))
paths_str = ", ".join(f"'{p}'" for p in paths)
query = f"SELECT {cols} FROM read_parquet([{paths_str}])"
query = f"SELECT {cols} FROM read_parquet($1)"

else:
paths_str = ", ".join(f"'{p}'" for p in paths)
query = (
f"SELECT * FROM read_parquet([{paths_str}], union_by_name=True)"
)
query = "SELECT * FROM read_parquet($1, union_by_name=True)"

if sql:
if sql.upper().startswith("SELECT"):
query = sql.replace("FROM t", f"FROM ({query}) AS t")
else:
query = f"SELECT {sql} FROM ({query}) AS t"

base = duckdb.execute(query)
base = duckdb.execute(query, params)

if not add_dv:
return base
Expand Down Expand Up @@ -890,4 +885,4 @@ def get_columns(path: Path) -> set[tuple[str, str]]:
for c in base.description
]
query = f"SELECT {', '.join(selects)} FROM ({query}) AS _t"
return duckdb.execute(query)
return duckdb.execute(query, params)