22#include " LocalServer.h"
33#include " chdb.h"
44#include " PythonImporter.h"
5+ #include " PythonTableCache.h"
56#include " TableFunctionPython.h"
67
78#include < mutex>
8- #include " Common/logger_useful.h"
9- #include < Common/re2.h>
10- #include " pybind11/gil.h"
11- #include " pybind11/pytypes.h"
9+ #include < Common/logger_useful.h>
1210
1311namespace py = pybind11;
1412
1513extern bool inside_main = true ;
1614
17- // Global storage for Python Table Engine queriable object
18- extern py::handle global_query_obj;
19-
20- // Find the queriable object in the Python environment
21- // return nullptr if no Python obj is referenced in query string
22- // return py::none if the obj referenced not found
23- // return the Python object if found
24- // The object name is extracted from the query string, must referenced by
25- // Python(var_name) or Python('var_name') or python("var_name") or python('var_name')
26- // such as:
27- // - `SELECT * FROM Python('PyReader')`
28- // - `SELECT * FROM Python(PyReader_instance)`
29- // - `SELECT * FROM Python(some_var_with_type_pandas_DataFrame_or_pyarrow_Table)`
30- // The object can be any thing that Python Table supported, like PyReader, pandas DataFrame, or PyArrow Table
31- // The object should be in the global or local scope
32- py::handle findQueryableObjFromQuery (const std::string & query_str)
33- {
34- // Extract the object name from the query string
35- std::string var_name;
36-
37- // RE2 pattern to match Python()/python() patterns with single/double quotes or no quotes
38- static const RE2 pattern (R"( [Pp]ython\s*\(\s*(?:['"]([^'"]+)['"]|([a-zA-Z_][a-zA-Z0-9_]*))\s*\))" );
39-
40- re2::StringPiece input (query_str);
41- std::string quoted_match, unquoted_match;
42-
43- // Try to match and extract the groups
44- if (RE2::PartialMatch (query_str, pattern, "ed_match, &unquoted_match))
45- {
46- // If quoted string was matched
47- if (!quoted_match.empty ())
48- {
49- var_name = quoted_match;
50- }
51- // If unquoted identifier was matched
52- else if (!unquoted_match.empty ())
53- {
54- var_name = unquoted_match;
55- }
56- }
57-
58- if (var_name.empty ())
59- {
60- return nullptr ;
61- }
62-
63- // Find the object in the Python environment
64- return DB::findQueryableObj (var_name);
65- }
66-
6715local_result_v2 * queryToBuffer (
6816 const std::string & queryStr,
6917 const std::string & output_format = " CSV" ,
@@ -303,7 +251,7 @@ void connection_wrapper::commit()
303251
304252query_result * connection_wrapper::query (const std::string & query_str, const std::string & format)
305253{
306- global_query_obj = findQueryableObjFromQuery (query_str);
254+ CHDB::PythonTableCache:: findQueryableObjFromQuery (query_str);
307255
308256 py::gil_scoped_release release;
309257 auto * result = query_conn (*conn, query_str.c_str (), format.c_str ());
@@ -322,7 +270,7 @@ query_result * connection_wrapper::query(const std::string & query_str, const st
322270
323271streaming_query_result * connection_wrapper::send_query (const std::string & query_str, const std::string & format)
324272{
325- global_query_obj = findQueryableObjFromQuery (query_str);
273+ CHDB::PythonTableCache:: findQueryableObjFromQuery (query_str);
326274
327275 py::gil_scoped_release release;
328276 auto * result = query_conn_streaming (*conn, query_str.c_str (), format.c_str ());
@@ -372,7 +320,7 @@ void connection_wrapper::streaming_cancel_query(streaming_query_result * streami
372320void cursor_wrapper::execute (const std::string & query_str)
373321{
374322 release_result ();
375- global_query_obj = findQueryableObjFromQuery (query_str);
323+ CHDB::PythonTableCache:: findQueryableObjFromQuery (query_str);
376324
377325 // Use JSONCompactEachRowWithNamesAndTypes format for better type support
378326 py::gil_scoped_release release;
@@ -545,6 +493,7 @@ PYBIND11_MODULE(_chdb, m)
545493 auto destroy_import_cache = []()
546494 {
547495 DB::LocalServer::cleanupConnection ();
496+ CHDB::PythonTableCache::clear ();
548497 CHDB::PythonImporter::destroy ();
549498 };
550499 m.add_object (" _destroy_import_cache" , py::capsule (destroy_import_cache));
0 commit comments