Skip to content

Commit 722811e

Browse files
committed
Unified chdb.query with udf_path support
1 parent 0401915 commit 722811e

File tree

2 files changed

+30
-28
lines changed

2 files changed

+30
-28
lines changed

chdb/__init__.py

Lines changed: 4 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -48,21 +48,11 @@ def to_df(r):
4848

4949

5050
# wrap _chdb functions
51-
def query(sql, output_format="CSV"):
51+
def query(sql, output_format="CSV", path=None, udf_path=None):
5252
lower_output_format = output_format.lower()
5353
if lower_output_format == "dataframe":
54-
return to_df(_chdb.query(sql, "Arrow"))
54+
return to_df(_chdb.query(sql, "Arrow", path=path, udf_path=udf_path))
5555
elif lower_output_format == 'arrowtable':
56-
return to_arrowTable(_chdb.query(sql, "Arrow"))
56+
return to_arrowTable(_chdb.query(sql, "Arrow", path=path, udf_path=udf_path))
5757
else:
58-
return _chdb.query(sql, output_format)
59-
60-
61-
def query_stateful(sql, output_format="CSV", path=None):
62-
lower_output_format = output_format.lower()
63-
if lower_output_format == "dataframe":
64-
return to_df(_chdb.query_stateful(sql, "Arrow", path))
65-
elif lower_output_format == 'arrowtable':
66-
return to_arrowTable(_chdb.query_stateful(sql, "Arrow", path))
67-
else:
68-
return _chdb.query_stateful(sql, output_format, path)
58+
return _chdb.query(sql, output_format, path=path, udf_path=udf_path)

programs/local/LocalChdb.cpp

Lines changed: 26 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,12 @@
66
extern 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

Comments
 (0)