From 2ccd9c30c94cf2b7e3094f486e1080f779f0f861 Mon Sep 17 00:00:00 2001 From: Arun Sharma Date: Wed, 22 Apr 2026 08:59:57 -0700 Subject: [PATCH] Infer JSON parameters in Python binding --- src_cpp/py_connection.cpp | 47 +++++++++++++++------------------------ 1 file changed, 18 insertions(+), 29 deletions(-) diff --git a/src_cpp/py_connection.cpp b/src_cpp/py_connection.cpp index 6488abd..9a6bc3f 100644 --- a/src_cpp/py_connection.cpp +++ b/src_cpp/py_connection.cpp @@ -393,14 +393,6 @@ static LogicalType pyLogicalType(const py::handle& val) { } return LogicalType::DECIMAL(precision, -exponent); } else if (py::isinstance(val)) { - auto strVal = py::cast(val); - if (!strVal.empty() && (strVal.front() == '{' || strVal.front() == '[')) { - auto jsonModule = py::module_::import("json"); - try { - auto parsed = jsonModule.attr("loads")(val); - return pyLogicalType(parsed); - } catch (...) {} - } return LogicalType::STRING(); } else if (py::isinstance(val)) { return LogicalType::BLOB(); @@ -517,6 +509,9 @@ static bool tryCastToMap(py::dict& dict, LogicalType& result) { } static LogicalType pyLogicalTypeFromParameter(const py::handle& val) { + if (py::isinstance(val)) { + return LogicalType::STRING(); + } if (py::isinstance(val)) { auto dict = py::reinterpret_borrow(val); LogicalType resultType; @@ -603,18 +598,9 @@ Value PyConnection::transformPythonValueAs(const py::handle& val, const LogicalT } case LogicalTypeID::STRING: if (py::isinstance(val)) { - auto strVal = val.cast(); - if (!strVal.empty() && (strVal.front() == '{' || strVal.front() == '[')) { - auto jsonModule = py::module_::import("json"); - try { - auto parsed = jsonModule.attr("loads")(val); - return transformPythonValue(parsed); - } catch (...) {} - } - return Value::createValue(strVal); - } else { - return Value::createValue(py::str(val)); + return Value::createValue(val.cast()); } + return Value::createValue(py::str(val)); case LogicalTypeID::BLOB: { auto bytes = py::cast(val); const char* data = PyBytes_AsString(bytes.ptr()); @@ -727,16 +713,6 @@ Value PyConnection::transformPythonValueAs(const py::handle& val, const LogicalT Value PyConnection::transformPythonValueFromParameterAs(const py::handle& val, const LogicalType& type) { - if (py::isinstance(val)) { - auto strVal = py::cast(val); - if (!strVal.empty() && (strVal.front() == '{' || strVal.front() == '[')) { - auto jsonModule = py::module_::import("json"); - try { - auto parsed = jsonModule.attr("loads")(val); - return transformPythonValueFromParameter(parsed); - } catch (...) {} - } - } switch (type.getLogicalTypeID()) { case LogicalTypeID::LIST: { if (ListType::getChildType(type).getLogicalTypeID() == LogicalTypeID::JSON) { @@ -785,6 +761,9 @@ Value PyConnection::transformPythonValueFromParameterAs(const py::handle& val, return Value(type.copy(), std::move(children)); } case LogicalTypeID::JSON: { + if (py::isinstance(val)) { + return Value::createValue(py::cast(val)); + } auto jsonStr = pythonObjectToJsonString(val); return Value::createValue(jsonStr); } @@ -802,6 +781,16 @@ Value PyConnection::transformPythonValue(const py::handle& val) { } Value PyConnection::transformPythonValueFromParameter(const py::handle& val) { + if (py::isinstance(val)) { + auto strVal = py::cast(val); + if (!strVal.empty() && (strVal.front() == '{' || strVal.front() == '[')) { + auto jsonModule = py::module_::import("json"); + try { + auto parsed = jsonModule.attr("loads")(val); + return transformPythonValueFromParameter(parsed); + } catch (...) {} + } + } auto type = pyLogicalTypeFromParameter(val); return transformPythonValueFromParameterAs(val, type); }