diff --git a/tools/python_api/src_cpp/include/cached_import/py_cached_modules.h b/tools/python_api/src_cpp/include/cached_import/py_cached_modules.h index 40813dbef34..1ed04afa5ee 100644 --- a/tools/python_api/src_cpp/include/cached_import/py_cached_modules.h +++ b/tools/python_api/src_cpp/include/cached_import/py_cached_modules.h @@ -93,6 +93,9 @@ class PandasCachedItem : public PythonCachedItem { }; class PolarsCachedItem : public PythonCachedItem { +public: + static constexpr const char* name_ = "polars"; + public: PolarsCachedItem() : PythonCachedItem("polars"), DataFrame("DataFrame", this) {} diff --git a/tools/python_api/src_cpp/pyarrow/pyarrow_scan.cpp b/tools/python_api/src_cpp/pyarrow/pyarrow_scan.cpp index c481f91a1d1..0b5c42b91d9 100644 --- a/tools/python_api/src_cpp/pyarrow/pyarrow_scan.cpp +++ b/tools/python_api/src_cpp/pyarrow/pyarrow_scan.cpp @@ -35,6 +35,12 @@ PyArrowScanConfig::PyArrowScanConfig(const std::unordered_map +static bool moduleIsLoaded() { + auto dict = pybind11::module_::import("sys").attr("modules"); + return dict.contains(py::str(T::name_)); +} + static std::unique_ptr bindFunc(main::ClientContext* /*context*/, ScanTableFuncBindInput* input) { // TODO: This binding step could use some drastic improvements. @@ -45,7 +51,8 @@ static std::unique_ptr bindFunc(main::ClientContext reinterpret_cast(input->inputs[0].getValue()))); if (PyConnection::isPandasDataframe(table)) { table = importCache->pyarrow.lib.Table.from_pandas()(table); - } else if (py::isinstance(table, importCache->polars.DataFrame())) { + } else if (moduleIsLoaded() && + py::isinstance(table, importCache->polars.DataFrame())) { table = table.attr("to_arrow")(); } std::vector returnTypes;