A Zig language binding for chDB, an embedded SQL query engine that brings ClickHouse to your application.
zigg provides a simple and idiomatic Zig interface to chDB's C API. It enables you to execute SQL queries directly from Zig code without managing external database servers.
- Zig 0.13.0 or later
- libchdb (pre-built or compiled)
zig buildzig build runzig build testconst std = @import("std");
const chdb = @import("chdb.zig");
pub fn main() !void {
const allocator = std.heap.c_allocator;
// Execute a simple query
var result = try chdb.query("SELECT 1 as x", allocator);
defer result.deinit();
// Access result data
std.debug.print("Result: {s}\n", .{result.buf[0..result.len]});
std.debug.print("Rows read: {d}\n", .{result.rows_read});
std.debug.print("Elapsed: {d}ms\n", .{result.elapsed * 1000});
}Executes a SQL query and returns the result.
Parameters:
sql: SQL query stringallocator: Memory allocator for temporary string allocations
Returns:
QueryResulton successChdbError.QueryFailedif the query fails
Example:
var result = try chdb.query("SELECT * FROM system.tables LIMIT 1", allocator);
defer result.deinit();pub const QueryResult = struct {
buf: [*]u8, // Result buffer pointer
len: usize, // Result buffer length
elapsed: f64, // Query execution time (seconds)
rows_read: u64, // Number of rows read
bytes_read: u64, // Number of bytes read
error_message: ?[*:0]u8, // Error message (if any)
_internal: ?*c.struct_local_result_v2,
pub fn deinit(self: *QueryResult) void
};Fields:
buf: Pointer to the result data bufferlen: Length of the result data in byteselapsed: Query execution time in secondsrows_read: Total rows read during query executionbytes_read: Total bytes read during query executionerror_message: Error message if query failed (null if successful)
.
├── build.zig # Build configuration
├── build.zig.zon # Build dependencies
├── libchdb/
│ ├── chdb.h # chDB C header
│ └── libchdb.so # Pre-built chDB library (not included in the repo, should be downloaded locally)
├── src/
│ ├── chdb.zig # Main binding module
│ ├── main.zig # Example application
│ └── root.zig # Library root
├── test/
│ └── test_basic.zig # Basic tests
└── README.md
zig build -Doptimize=Debugzig build -Doptimize=ReleaseFastzig build testEnsure libchdb.so is in the libchdb/ directory or adjust the path in build.zig:
exe.addObjectFile(b.path("path/to/libchdb.so"));Check the query syntax and ensure chDB supports the SQL dialect. See chDB documentation.
Contributions are welcome! Please open an issue or submit a pull request.