@@ -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 } ');""" )
0 commit comments