Skip to content

Commit 2e99df5

Browse files
committed
Pass csv parameters during duckdb connection
1 parent e636ede commit 2e99df5

2 files changed

Lines changed: 26 additions & 8 deletions

File tree

datacontract/engines/soda/connections/duckdb.py

Lines changed: 23 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -42,14 +42,29 @@ def get_duckdb_connection(data_contract, server, run: Run):
4242
elif server.format == "csv":
4343
columns = to_csv_types(model)
4444
run.log_info("Using columns: " + str(columns))
45-
if columns is None:
46-
con.sql(
47-
f"""CREATE VIEW "{model_name}" AS SELECT * FROM read_csv('{model_path}', hive_partitioning=1);"""
48-
)
49-
else:
50-
con.sql(
51-
f"""CREATE VIEW "{model_name}" AS SELECT * FROM read_csv('{model_path}', hive_partitioning=1, columns={columns});"""
52-
)
45+
46+
# Start with the required parameter.
47+
params = ["hive_partitioning=1"]
48+
49+
# Loop over optional CSV parameters.
50+
for param in ("delimiter", "header", "escape", "encoding"):
51+
value = getattr(server, param)
52+
if value is not None:
53+
# Wrap string values in quotes.
54+
if isinstance(value, str):
55+
params.append(f"{param}='{value}'")
56+
else:
57+
params.append(f"{param}={value}")
58+
59+
# Add columns if they exist.
60+
if columns is not None:
61+
params.append(f"columns={columns}")
62+
63+
# Build the parameter string.
64+
params_str = ", ".join(params)
65+
66+
# Create the view with the assembled parameters.
67+
con.sql(f"""CREATE VIEW "{model_name}" AS SELECT * FROM read_csv('{model_path}', {params_str});""")
5368
elif server.format == "delta":
5469
con.sql("update extensions;") # Make sure we have the latest delta extension
5570
con.sql(f"""CREATE VIEW "{model_name}" AS SELECT * FROM delta_scan('{model_path}');""")

datacontract/model/data_contract_specification.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,9 @@ class Server(pyd.BaseModel):
5858
dataset: str | None = None
5959
path: str | None = None
6060
delimiter: str | None = None
61+
header: bool | None = None
62+
escape: str | None = None
63+
encoding: str | None = None
6164
endpointUrl: str | None = None
6265
location: str | None = None
6366
account: str | None = None

0 commit comments

Comments
 (0)