Skip to content

Commit b07fb41

Browse files
committed
chore: add new API to chdb.h
1 parent ffc395f commit b07fb41

File tree

6 files changed

+145
-17
lines changed

6 files changed

+145
-17
lines changed

programs/local/LocalChdb.h

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,22 @@ class local_result_wrapper
125125
}
126126
return result->bytes_read;
127127
}
128+
size_t storage_rows_read()
129+
{
130+
if (result == nullptr)
131+
{
132+
return 0;
133+
}
134+
return result->storage_rows_read;
135+
}
136+
size_t storage_bytes_read()
137+
{
138+
if (result == nullptr)
139+
{
140+
return 0;
141+
}
142+
return result->storage_bytes_read;
143+
}
128144
double elapsed()
129145
{
130146
if (result == nullptr)
@@ -166,6 +182,8 @@ class query_result
166182
size_t size() { return result_wrapper->size(); }
167183
size_t rows_read() { return result_wrapper->rows_read(); }
168184
size_t bytes_read() { return result_wrapper->bytes_read(); }
185+
size_t storgae_rows_read() { return result_wrapper->storage_rows_read(); }
186+
size_t storage_bytes_read() { return result_wrapper->storage_bytes_read(); }
169187
double elapsed() { return result_wrapper->elapsed(); }
170188
bool has_error() { return result_wrapper->has_error(); }
171189
py::str error_message() { return result_wrapper->error_message(); }

programs/local/LocalServer.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1289,6 +1289,8 @@ static local_result_v2 * createMaterializedLocalQueryResult(DB::LocalServer * se
12891289
}
12901290
result->rows_read = server->getProcessedRows();
12911291
result->bytes_read = server->getProcessedBytes();
1292+
result->storage_rows_read = server->getStorgaeRowsRead();
1293+
result->storage_bytes_read = server->getStorageBytesRead();
12921294
result->elapsed = server->getElapsedTime();
12931295
}
12941296
}

programs/local/chdb.h

Lines changed: 104 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,19 @@
11
#pragma once
22

33
#ifdef __cplusplus
4-
# include <cstddef>
5-
# include <cstdint>
4+
#include <cstddef>
5+
#include <cstdint>
66
extern "C" {
77
#else
8-
# include <stdbool.h>
9-
# include <stddef.h>
10-
# include <stdint.h>
8+
#include <stdbool.h>
9+
#include <stddef.h>
10+
#include <stdint.h>
1111
#endif
1212

1313
#define CHDB_EXPORT __attribute__((visibility("default")))
14+
15+
#ifndef CHDB_NO_DEPRECATED
16+
// WARNING: The following structs are deprecated and will be removed in a future version.
1417
struct local_result
1518
{
1619
char * buf;
@@ -45,12 +48,6 @@ struct local_result_v2
4548
};
4649
#endif
4750

48-
CHDB_EXPORT struct local_result * query_stable(int argc, char ** argv);
49-
CHDB_EXPORT void free_result(struct local_result * result);
50-
51-
CHDB_EXPORT struct local_result_v2 * query_stable_v2(int argc, char ** argv);
52-
CHDB_EXPORT void free_result_v2(struct local_result_v2 * result);
53-
5451
/**
5552
* Connection structure for chDB
5653
* Contains server instance, connection state, and query processing queue
@@ -62,10 +59,37 @@ struct chdb_conn
6259
void * queue; /* Query processing queue */
6360
};
6461

62+
#endif
63+
64+
// Opaque handle for query results.
65+
// Internal data structure managed by chDB implementation.
66+
// Users should only interact through API functions.
67+
typedef struct {
68+
void * internal_data;
69+
} chdb_result;
70+
71+
// Connection handle wrapping database session state.
72+
// Internal data structure managed by chDB implementation.
73+
// Users should only interact through API functions.
74+
typedef struct _chdb_connection {
75+
void * internal_data;
76+
} * chdb_connection;
77+
78+
// Opaque handle for streaming query results.
79+
// Internal data structure managed by chDB implementation.
80+
// Users should only interact through API functions.
6581
typedef struct {
6682
void * internal_data;
6783
} chdb_streaming_result;
6884

85+
#ifndef CHDB_NO_DEPRECATED
86+
// WARNING: The following interfaces are deprecated and will be removed in a future version.
87+
CHDB_EXPORT struct local_result * query_stable(int argc, char ** argv);
88+
CHDB_EXPORT void free_result(struct local_result * result);
89+
90+
CHDB_EXPORT struct local_result_v2 * query_stable_v2(int argc, char ** argv);
91+
CHDB_EXPORT void free_result_v2(struct local_result_v2 * result);
92+
6993
/**
7094
* Creates a new chDB connection.
7195
* Only one active connection is allowed per process.
@@ -110,12 +134,67 @@ CHDB_EXPORT struct local_result_v2 * query_conn(struct chdb_conn * conn, const c
110134
CHDB_EXPORT chdb_streaming_result * query_conn_streaming(struct chdb_conn * conn, const char * query, const char * format);
111135

112136
/**
113-
* Retrieves error message from streaming result.
114-
* @brief Gets error message associated with streaming query execution
137+
* Fetches next chunk of streaming results.
138+
* @brief Iterates through streaming query results
139+
* @param conn Active connection handle
115140
* @param result Streaming result handle from query_conn_streaming()
116-
* @return Null-terminated error message string, or NULL if no error occurred
141+
* @return Materialized result chunk with data
142+
* @note Returns empty result when stream ends
117143
*/
118-
CHDB_EXPORT const char * chdb_streaming_result_error(chdb_streaming_result * result);
144+
CHDB_EXPORT struct local_result_v2 * chdb_streaming_fetch_result(struct chdb_conn * conn, chdb_streaming_result * result);
145+
146+
/**
147+
* Cancels ongoing streaming query.
148+
* @brief Aborts streaming query execution and cleans up resources
149+
* @param conn Active connection handle
150+
* @param result Streaming result handle to cancel
151+
*/
152+
CHDB_EXPORT void chdb_streaming_cancel_query(struct chdb_conn * conn, chdb_streaming_result * result);
153+
154+
#endif
155+
156+
/**
157+
* Creates a new chDB connection.
158+
* Only one active connection is allowed per process.
159+
* Creating a new connection with different path requires closing existing connection.
160+
*
161+
* @param argc Number of command-line arguments
162+
* @param argv Command-line arguments array (--path=<db_path> to specify database location)
163+
* @return Pointer to connection pointer, or NULL on failure
164+
* @note Default path is ":memory:" if not specified
165+
*/
166+
CHDB_EXPORT chdb_connection * chdb_connect(int argc, char ** argv);
167+
168+
/**
169+
* Closes an existing chDB connection and cleans up resources.
170+
* Thread-safe function that handles connection shutdown and cleanup.
171+
*
172+
* @param conn Pointer to connection pointer to close
173+
*/
174+
CHDB_EXPORT void chdb_close_conn(chdb_connection * conn);
175+
176+
/**
177+
* Executes a query on the given connection.
178+
* Thread-safe function that handles query execution in a separate thread.
179+
*
180+
* @param conn Connection to execute query on
181+
* @param query SQL query string to execute
182+
* @param format Output format string (e.g., "CSV", default format)
183+
* @return Query result structure containing output or error message
184+
* @note Returns error result if connection is invalid or closed
185+
*/
186+
CHDB_EXPORT chdb_result * chdb_query(chdb_connection conn, const char * query, const char * format);
187+
188+
/**
189+
* Executes a streaming query on the given connection.
190+
* @brief Initializes streaming query execution and returns result handle
191+
* @param conn Connection to execute query on
192+
* @param query SQL query string to execute
193+
* @param format Output format string (e.g. "CSV", default format)
194+
* @return Streaming result handle containing query state or error message
195+
* @note Returns error result if connection is invalid or closed
196+
*/
197+
CHDB_EXPORT chdb_streaming_result * chdb_stream_query(chdb_connection conn, const char * query, const char * format);
119198

120199
/**
121200
* Fetches next chunk of streaming results.
@@ -125,15 +204,23 @@ CHDB_EXPORT const char * chdb_streaming_result_error(chdb_streaming_result * res
125204
* @return Materialized result chunk with data
126205
* @note Returns empty result when stream ends
127206
*/
128-
CHDB_EXPORT struct local_result_v2 * chdb_streaming_fetch_result(struct chdb_conn * conn, chdb_streaming_result * result);
207+
CHDB_EXPORT chdb_result * chdb_stream_fetch_result(chdb_connection conn, chdb_streaming_result * result);
129208

130209
/**
131210
* Cancels ongoing streaming query.
132211
* @brief Aborts streaming query execution and cleans up resources
133212
* @param conn Active connection handle
134213
* @param result Streaming result handle to cancel
135214
*/
136-
CHDB_EXPORT void chdb_streaming_cancel_query(struct chdb_conn * conn, chdb_streaming_result * result);
215+
CHDB_EXPORT void chdb_stream_cancel_query(chdb_connection conn, chdb_streaming_result * result);
216+
217+
/**
218+
* Retrieves error message from streaming result.
219+
* @brief Gets error message associated with streaming query execution
220+
* @param result Streaming result handle from query_conn_streaming()
221+
* @return Null-terminated error message string, or NULL if no error occurred
222+
*/
223+
CHDB_EXPORT const char * chdb_streaming_result_error(chdb_streaming_result * result);
137224

138225
/**
139226
* Releases resources associated with streaming result.

src/Client/ClientBase.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,9 @@ class ClientBase
127127
return result;
128128
}
129129

130+
size_t getStorgaeRowsRead() const { return connection->getCHDBProgress().read_rows; }
131+
size_t getStorageBytesRead() const { return connection->getCHDBProgress().read_bytes; }
132+
130133
size_t getProcessedRows() const { return processed_rows; }
131134
size_t getProcessedBytes() const { return processed_bytes; }
132135
double getElapsedTime() const { return progress_indication.elapsedSeconds(); }

src/Client/LocalConnection.cpp

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,8 @@ LocalConnection::LocalConnection(ContextPtr context_, ReadBuffer * in_, bool sen
4343
/// Authenticate and create a context to execute queries.
4444
session.authenticate("default", "", Poco::Net::SocketAddress{});
4545
session.makeSessionContext();
46+
47+
send_progress = false;
4648
}
4749

4850
LocalConnection::~LocalConnection()
@@ -77,6 +79,11 @@ void LocalConnection::updateProgress(const Progress & value)
7779
state->progress.incrementPiecewiseAtomically(value);
7880
}
7981

82+
void LocalConnection::updateCHDBProgress(const Progress & value)
83+
{
84+
chdb_progress.incrementPiecewiseAtomically(value);
85+
}
86+
8087
void LocalConnection::sendProfileEvents()
8188
{
8289
Block profile_block;
@@ -113,6 +120,11 @@ void LocalConnection::sendQuery(
113120
query_context->setProgressCallback([this] (const Progress & value) { this->updateProgress(value); });
114121
query_context->setFileProgressCallback([this](const FileProgress & value) { this->updateProgress(Progress(value)); });
115122
}
123+
else
124+
{
125+
query_context->setProgressCallback([this] (const Progress & value) { this->updateCHDBProgress(value); });
126+
query_context->setFileProgressCallback([this](const FileProgress & value) { this->updateCHDBProgress(Progress(value)); });
127+
}
116128

117129
/// Switch the database to the desired one (set by the USE query)
118130
/// but don't attempt to do it if we are already in that database.
@@ -125,6 +137,7 @@ void LocalConnection::sendQuery(
125137

126138
state.reset();
127139
state.emplace();
140+
chdb_progress.reset();
128141

129142
state->query_id = query_id;
130143
state->query = query;

src/Client/LocalConnection.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,7 @@ class LocalConnection : public IServerConnection, WithContext
142142

143143
void setThrottler(const ThrottlerPtr &) override {}
144144

145+
const Progress & getCHDBProgress() const { return chdb_progress; }
145146
#if USE_PYTHON
146147
void resetQueryContext();
147148
#endif
@@ -153,6 +154,8 @@ class LocalConnection : public IServerConnection, WithContext
153154

154155
void updateProgress(const Progress & value);
155156

157+
void updateCHDBProgress(const Progress & value);
158+
156159
void sendProfileEvents();
157160

158161
/// Returns true on executor timeout, meaning a retryable error.
@@ -170,6 +173,8 @@ class LocalConnection : public IServerConnection, WithContext
170173

171174
std::optional<LocalQueryState> state;
172175

176+
Progress chdb_progress;
177+
173178
/// Last "server" packet.
174179
std::optional<UInt64> next_packet_type;
175180

0 commit comments

Comments
 (0)