Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 7 additions & 5 deletions programs/local/FormatHelper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,22 @@

#include <algorithm>
#include <cctype>
#include <base/types.h>

namespace CHDB {

static bool is_json_supported = true;

void SetCurrentFormat(const char * format)
void SetCurrentFormat(const char * format, size_t format_len)
{
if (format)
{
String lowerFormat = format;
std::transform(lowerFormat.begin(), lowerFormat.end(), lowerFormat.begin(), ::tolower);
String lower_format{format, format_len};
std::transform(lower_format.begin(), lower_format.end(), lower_format.begin(), ::tolower);

is_json_supported = !(lowerFormat == "arrow" || lowerFormat == "parquet" || lowerFormat == "arrowstream"
|| lowerFormat == "protobuf" || lowerFormat == "protobuflist" || lowerFormat == "protobufsingle");
is_json_supported
= !(lower_format == "arrow" || lower_format == "parquet" || lower_format == "arrowstream" || lower_format == "protobuf"
|| lower_format == "protobuflist" || lower_format == "protobufsingle");

return;
}
Expand Down
2 changes: 1 addition & 1 deletion programs/local/FormatHelper.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

namespace CHDB {

void SetCurrentFormat(const char * format);
void SetCurrentFormat(const char * format, size_t format_len);

bool isJSONSupported();

Expand Down
25 changes: 14 additions & 11 deletions programs/local/LocalChdb.cpp
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
#include "LocalChdb.h"
#include "chdb.h"
#include "chdb-internal.h"
#include <cstring>
#include "PythonImporter.h"
#include "PythonTableCache.h"
#include "StoragePython.h"
#include "chdb-internal.h"
#include "chdb.h"

#include <pybind11/pybind11.h>
#include <pybind11/detail/non_limited_api.h>
Expand All @@ -13,6 +14,8 @@
namespace py = pybind11;

extern bool inside_main = true;
const static char * CURSOR_DEFAULT_FORMAT = "JSONCompactEachRowWithNamesAndTypes";
const static size_t CURSOR_DEFAULT_FORMAT_LEN = strlen(CURSOR_DEFAULT_FORMAT);

chdb_result * queryToBuffer(
const std::string & queryStr,
Expand Down Expand Up @@ -256,14 +259,14 @@ query_result * connection_wrapper::query(const std::string & query_str, const st
CHDB::PythonTableCache::findQueryableObjFromQuery(query_str);

py::gil_scoped_release release;
auto * result = chdb_query(*conn, query_str.c_str(), format.c_str());
auto * result = chdb_query_n(*conn, query_str.data(), query_str.size(), format.data(), format.size());
if (chdb_result_length(result))
{
LOG_DEBUG(getLogger("CHDB"), "Empty result returned for query: {}", query_str);
}

auto * error_msg = chdb_result_error(result);
if (error_msg)
auto error_msg = CHDB::chdb_result_error_string(result);
if (!error_msg.empty())
{
std::string msg_copy(error_msg);
chdb_destroy_query_result(result);
Expand All @@ -277,9 +280,9 @@ streaming_query_result * connection_wrapper::send_query(const std::string & quer
CHDB::PythonTableCache::findQueryableObjFromQuery(query_str);

py::gil_scoped_release release;
auto * result = chdb_stream_query(*conn, query_str.c_str(), format.c_str());
auto * error_msg = chdb_result_error(result);
if (error_msg)
auto * result = chdb_stream_query_n(*conn, query_str.data(), query_str.size(), format.data(), format.size());
auto error_msg = CHDB::chdb_result_error_string(result);
if (!error_msg.empty())
{
std::string msg_copy(error_msg);
chdb_destroy_query_result(result);
Expand All @@ -301,8 +304,8 @@ query_result * connection_wrapper::streaming_fetch_result(streaming_query_result
if (chdb_result_length(result) == 0)
LOG_DEBUG(getLogger("CHDB"), "Empty result returned for streaming query");

auto * error_msg = chdb_result_error(result);
if (error_msg)
const auto error_msg = CHDB::chdb_result_error_string(result);
if (!error_msg.empty())
{
std::string msg_copy(error_msg);
chdb_destroy_query_result(result);
Expand All @@ -329,7 +332,7 @@ void cursor_wrapper::execute(const std::string & query_str)

// Use JSONCompactEachRowWithNamesAndTypes format for better type support
py::gil_scoped_release release;
current_result = chdb_query(conn->get_conn(), query_str.c_str(), "JSONCompactEachRowWithNamesAndTypes");
current_result = chdb_query_n(conn->get_conn(), query_str.data(), query_str.size(), CURSOR_DEFAULT_FORMAT, CURSOR_DEFAULT_FORMAT_LEN);
}


Expand Down
17 changes: 17 additions & 0 deletions programs/local/chdb-internal.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,13 @@ struct MaterializedQueryRequest : QueryRequestBase
std::string query;
std::string format;

MaterializedQueryRequest() = default;
MaterializedQueryRequest(const char * q, size_t q_len, const char * f, size_t f_len)
: query(q, q_len)
, format(f, f_len)
{
}

bool isStreaming() const override { return false; }
};

Expand All @@ -37,6 +44,13 @@ struct StreamingInitRequest : QueryRequestBase
std::string query;
std::string format;

StreamingInitRequest() = default;
StreamingInitRequest(const char * q, size_t q_len, const char * f, size_t f_len)
: query(q, q_len)
, format(f, f_len)
{
}

bool isStreaming() const override { return true; }
};

Expand Down Expand Up @@ -76,4 +90,7 @@ void chdbCleanupConnection();

void cancelStreamQuery(DB::LocalServer * server, void * stream_result);

const std::string & chdb_result_error_string(chdb_result * result);

const std::string & chdb_streaming_result_error_string(chdb_streaming_result * result);
}
Loading
Loading