66extern bool inside_main = true ;
77
88
9- local_result * queryToBuffer (const std::string & queryStr, const std::string & format = " CSV" , const std::string & path = {})
9+ local_result * queryToBuffer (
10+ const std::string & queryStr, const std::string & format = " CSV" , const std::string & path = {}, const std::string & udfPath = {})
1011{
11- std::vector<std::string> argv = {" clickhouse" , " --multiquery" };
12+ std::vector<std::string> argv = {" clickhouse" , " --" , " -- multiquery" };
1213
13- // if format is "Debug" or "debug", then we will add --verbose and --log-level=trace to argv
14+ // If format is "Debug" or "debug", then we will add ` --verbose` and ` --log-level=trace` to argv
1415 if (format == " Debug" || format == " debug" )
1516 {
1617 argv.push_back (" --verbose" );
@@ -24,7 +25,16 @@ local_result * queryToBuffer(const std::string & queryStr, const std::string & f
2425 argv.push_back (" --output-format=" + format);
2526 }
2627
27- if (!path.empty ())
28+ // If udfPath is not empty, then we will add `--user_scripts_path` and `--user_defined_executable_functions_config` to argv
29+ // the path should be a one time thing, so the caller should take care of the temporary files deletion
30+ if (!udfPath.empty ())
31+ {
32+ argv.push_back (" --user_scripts_path=" + udfPath);
33+ argv.push_back (" --user_defined_executable_functions_config=" + udfPath + " /*.xml" );
34+ }
35+
36+ // If path is not empty, then we will add `--path` to argv. This is used for chdb.Session to support stateful query
37+ if (!path.empty ())
2838 {
2939 // Add path string
3040 argv.push_back (" --path=" + path);
@@ -42,14 +52,10 @@ local_result * queryToBuffer(const std::string & queryStr, const std::string & f
4252
4353// Pybind11 will take over the ownership of the `query_result` object
4454// using smart ptr will cause early free of the object
45- query_result * query (const std::string & queryStr, const std::string & format = " CSV" )
55+ query_result *
56+ query (const std::string & queryStr, const std::string & format = " CSV" , const std::string & path = {}, const std::string & udfPath = {})
4657{
47- return new query_result (queryToBuffer (queryStr, format));
48- }
49-
50- query_result * query_stateful (const std::string & queryStr, const std::string & format = " CSV" , const std::string & path = {})
51- {
52- return new query_result (queryToBuffer (queryStr, format, path));
58+ return new query_result (queryToBuffer (queryStr, format, path, udfPath));
5359}
5460
5561// The `query_result` and `memoryview_wrapper` will hold `local_result_wrapper` with shared_ptr
@@ -132,9 +138,15 @@ PYBIND11_MODULE(_chdb, m)
132138 .def (" get_memview" , &query_result::get_memview);
133139
134140
135- m.def (" query" , &query, " Stateless query Clickhouse and return a query_result object" );
136-
137- m.def (" query_stateful" , &query_stateful, " Stateful query Clickhouse and return a query_result object" );
141+ m.def (
142+ " query" ,
143+ &query,
144+ py::arg (" queryStr" ),
145+ py::arg (" format" ) = " CSV" ,
146+ py::kw_only (),
147+ py::arg (" path" ) = " " ,
148+ py::arg (" udf_path" ) = " " ,
149+ " Query chDB and return a query_result object" );
138150}
139151
140152#endif // PY_TEST_MAIN
0 commit comments