Skip to content

Commit 7184786

Browse files
committed
Infer JSON parameters in Python binding
1 parent 83101a1 commit 7184786

1 file changed

Lines changed: 18 additions & 29 deletions

File tree

src_cpp/py_connection.cpp

Lines changed: 18 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -393,14 +393,6 @@ static LogicalType pyLogicalType(const py::handle& val) {
393393
}
394394
return LogicalType::DECIMAL(precision, -exponent);
395395
} else if (py::isinstance<py::str>(val)) {
396-
auto strVal = py::cast<std::string>(val);
397-
if (!strVal.empty() && (strVal.front() == '{' || strVal.front() == '[')) {
398-
auto jsonModule = py::module_::import("json");
399-
try {
400-
auto parsed = jsonModule.attr("loads")(val);
401-
return pyLogicalType(parsed);
402-
} catch (...) {}
403-
}
404396
return LogicalType::STRING();
405397
} else if (py::isinstance<py::bytes>(val)) {
406398
return LogicalType::BLOB();
@@ -517,6 +509,9 @@ static bool tryCastToMap(py::dict& dict, LogicalType& result) {
517509
}
518510

519511
static LogicalType pyLogicalTypeFromParameter(const py::handle& val) {
512+
if (py::isinstance<py::str>(val)) {
513+
return LogicalType::STRING();
514+
}
520515
if (py::isinstance<py::dict>(val)) {
521516
auto dict = py::reinterpret_borrow<py::dict>(val);
522517
LogicalType resultType;
@@ -603,18 +598,9 @@ Value PyConnection::transformPythonValueAs(const py::handle& val, const LogicalT
603598
}
604599
case LogicalTypeID::STRING:
605600
if (py::isinstance<py::str>(val)) {
606-
auto strVal = val.cast<std::string>();
607-
if (!strVal.empty() && (strVal.front() == '{' || strVal.front() == '[')) {
608-
auto jsonModule = py::module_::import("json");
609-
try {
610-
auto parsed = jsonModule.attr("loads")(val);
611-
return transformPythonValue(parsed);
612-
} catch (...) {}
613-
}
614-
return Value::createValue<std::string>(strVal);
615-
} else {
616-
return Value::createValue<std::string>(py::str(val));
601+
return Value::createValue<std::string>(val.cast<std::string>());
617602
}
603+
return Value::createValue<std::string>(py::str(val));
618604
case LogicalTypeID::BLOB: {
619605
auto bytes = py::cast<py::bytes>(val);
620606
const char* data = PyBytes_AsString(bytes.ptr());
@@ -727,16 +713,6 @@ Value PyConnection::transformPythonValueAs(const py::handle& val, const LogicalT
727713

728714
Value PyConnection::transformPythonValueFromParameterAs(const py::handle& val,
729715
const LogicalType& type) {
730-
if (py::isinstance<py::str>(val)) {
731-
auto strVal = py::cast<std::string>(val);
732-
if (!strVal.empty() && (strVal.front() == '{' || strVal.front() == '[')) {
733-
auto jsonModule = py::module_::import("json");
734-
try {
735-
auto parsed = jsonModule.attr("loads")(val);
736-
return transformPythonValueFromParameter(parsed);
737-
} catch (...) {}
738-
}
739-
}
740716
switch (type.getLogicalTypeID()) {
741717
case LogicalTypeID::LIST: {
742718
if (ListType::getChildType(type).getLogicalTypeID() == LogicalTypeID::JSON) {
@@ -785,6 +761,9 @@ Value PyConnection::transformPythonValueFromParameterAs(const py::handle& val,
785761
return Value(type.copy(), std::move(children));
786762
}
787763
case LogicalTypeID::JSON: {
764+
if (py::isinstance<py::str>(val)) {
765+
return Value::createValue<std::string>(py::cast<std::string>(val));
766+
}
788767
auto jsonStr = pythonObjectToJsonString(val);
789768
return Value::createValue<std::string>(jsonStr);
790769
}
@@ -802,6 +781,16 @@ Value PyConnection::transformPythonValue(const py::handle& val) {
802781
}
803782

804783
Value PyConnection::transformPythonValueFromParameter(const py::handle& val) {
784+
if (py::isinstance<py::str>(val)) {
785+
auto strVal = py::cast<std::string>(val);
786+
if (!strVal.empty() && (strVal.front() == '{' || strVal.front() == '[')) {
787+
auto jsonModule = py::module_::import("json");
788+
try {
789+
auto parsed = jsonModule.attr("loads")(val);
790+
return transformPythonValueFromParameter(parsed);
791+
} catch (...) {}
792+
}
793+
}
805794
auto type = pyLogicalTypeFromParameter(val);
806795
return transformPythonValueFromParameterAs(val, type);
807796
}

0 commit comments

Comments
 (0)