From e957f2ba08612b031638d2243c0bf20e9b780c0b Mon Sep 17 00:00:00 2001 From: Diego Imbert Date: Mon, 1 Dec 2025 12:23:12 +0100 Subject: [PATCH] infer_sql_type in python client --- python-client/wmill/wmill/client.py | 30 +++++++++++++++++++++++++---- 1 file changed, 26 insertions(+), 4 deletions(-) diff --git a/python-client/wmill/wmill/client.py b/python-client/wmill/wmill/client.py index 79066ae032..806147326f 100644 --- a/python-client/wmill/wmill/client.py +++ b/python-client/wmill/wmill/client.py @@ -1573,11 +1573,11 @@ def username_to_email(username: str) -> str: @init_global_client -def datatable(name: str = "main"): +def datatable(name: str = "main") -> DataTableClient: return _client.datatable(name) @init_global_client -def ducklake(name: str = "main"): +def ducklake(name: str = "main") -> DucklakeClient: return _client.ducklake(name) def task(*args, **kwargs): @@ -1704,11 +1704,33 @@ class DucklakeClient: args_def = "" for key, value in kwargs.items(): args_dict[key] = value - args_def += f"-- ${key}\n" + args_def += f"-- ${key} ({infer_sql_type(value)})\n" attach = f"ATTACH 'ducklake://{self.name}' AS dl;USE dl;\n" sql = args_def + attach + sql return self.client.run_inline_script_preview( content=sql, language="duckdb", args=args_dict, - ) \ No newline at end of file + ) + +def infer_sql_type(value) -> str: + """ + DuckDB executor requires explicit argument types at declaration + These types exist in both DuckDB and Postgres + Check that the types exist if you plan to extend this function for other SQL engines. + """ + if isinstance(value, bool): + # Check bool before int since bool is a subclass of int in Python + return "BOOLEAN" + elif isinstance(value, int): + return "BIGINT" + elif isinstance(value, float): + return "DOUBLE PRECISION" + elif value is None: + return "TEXT" + elif isinstance(value, str): + return "TEXT" + elif isinstance(value, dict) or isinstance(value, list): + return "JSON" + else: + return "TEXT"