Skip to content
Open
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
5 changes: 3 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
node_modules

*.so
/lib/chdb_bun.dylib
/lib/libchdb.so
*.dylib
/lib/chdb.h
/lib/chdb.hpp
/lib/index.js
/lib/index.d.ts
15 changes: 7 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ Experimental [chDB](https://github.com/chdb-io/chdb) FFI bindings for the [bun r

- experimental, unstable, subject to changes
- requires [`libchdb`](https://github.com/chdb-io/chdb) on the system
- requires `gcc` or `clang`

#### Build binding
```bash
Expand All @@ -25,23 +24,23 @@ bun run example.ts
import { query } from 'chdb-bun';

// Query (ephemeral)
var result = query("SELECT version()", "CSV");
console.log(result); // 23.10.1.1
var result = query("SELECT version(), 'Hello chDB', chdb()", "CSV");
console.log(result); // "25.5.2.1","Hello chDB","3.6.0"
```

#### Session.Query(query, *format)
```javascript
import { Session } from 'chdb-bun';
const sess = new Session('./chdb-bun-tmp');
const session = new Session('./chdb-bun-tmp');

// Query Session (persistent)
sess.query("CREATE FUNCTION IF NOT EXISTS hello AS () -> 'Hello chDB'", "CSV");
var result = sess.query("SELECT hello()", "CSV");
console.log(result);
session.query("CREATE FUNCTION IF NOT EXISTS hello AS () -> 'chDB'", "CSV");
var result = session.query("SELECT hello()", "CSV");
console.log(result); //"chDB"

// Before cleanup, you can find the database files in `./chdb-bun-tmp`

sess.cleanup(); // cleanup session, this will delete the database
session.cleanup(); // cleanup session, this will delete the database
```

<br>
25 changes: 25 additions & 0 deletions bun.lock
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
{
"lockfileVersion": 1,
"workspaces": {
"": {
"name": "chdb-bun",
"devDependencies": {
"bun-types": "^1.0.19",
"typescript": "^5.3.3",
},
},
},
"packages": {
"@types/node": ["@types/node@24.7.2", "", { "dependencies": { "undici-types": "~7.14.0" } }, "sha512-/NbVmcGTP+lj5oa4yiYxxeBjRivKQ5Ns1eSZeB99ExsEQ6rX5XYU1Zy/gGxY/ilqtD4Etx9mKyrPxZRetiahhA=="],

"@types/react": ["@types/react@19.2.2", "", { "dependencies": { "csstype": "^3.0.2" } }, "sha512-6mDvHUFSjyT2B2yeNx2nUgMxh9LtOWvkhIU3uePn2I2oyNymUAX1NIsdgviM4CH+JSrp2D2hsMvJOkxY+0wNRA=="],

"bun-types": ["bun-types@1.3.0", "", { "dependencies": { "@types/node": "*" }, "peerDependencies": { "@types/react": "^19" } }, "sha512-u8X0thhx+yJ0KmkxuEo9HAtdfgCBaM/aI9K90VQcQioAmkVp3SG3FkwWGibUFz3WdXAdcsqOcbU40lK7tbHdkQ=="],

"csstype": ["csstype@3.1.3", "", {}, "sha512-M1uQkMl8rQK/szD0LNhtqxIPLpimGm8sOBwU7lLnCpSbTyY3yeU1Vc7l4KT5zT4s/yOxHH5O7tIuuLOCnLADRw=="],

"typescript": ["typescript@5.9.3", "", { "bin": { "tsc": "bin/tsc", "tsserver": "bin/tsserver" } }, "sha512-jl1vZzPDinLr9eUt3J/t7V6FgNEw9QjvBPdysz9KfQDD41fQrC2Y4vKQdiaUpFT4bXlb1RHhLpp8wtm6M5TgSw=="],

"undici-types": ["undici-types@7.14.0", "", {}, "sha512-QQiYxHuyZ9gQUIrmPo3IA+hUl4KYk8uSA7cHrcKd/l3p1OTpZcM0Tbp9x7FAtXdAYhlasd60ncPpgu6ihG6TOA=="],
}
}
Binary file removed bun.lockb
Binary file not shown.
18 changes: 0 additions & 18 deletions example.deno.ts

This file was deleted.

4 changes: 2 additions & 2 deletions example.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@ var result;

// Test standalone query
result = query("SELECT version(), 'Hello chDB', chdb()", "CSV");
console.log(result);
console.log(result); // "25.5.2.1","Hello chDB","3.6.0"

// Test session query
session.query("CREATE FUNCTION IF NOT EXISTS hello AS () -> 'chDB'", "CSV");
result = session.query("SELECT hello()", "CSV");
console.log(result);
console.log(result); //"chDB"

// Clean up the session
session.cleanup();
54 changes: 36 additions & 18 deletions index.ts
Original file line number Diff line number Diff line change
@@ -1,25 +1,37 @@
import { dlopen, FFIType } from "bun:ffi";
import { rmdirSync, mkdtempSync } from 'fs';
import { cc, FFIType, CString } from "bun:ffi";
import { rmdirSync, mkdtempSync } from "fs";
import source from "./lib/chdb_bun.c" with { type: "file" };

const path = `chdb_bun.so`;

const { symbols: chdb } = dlopen(path, {
Query: {
args: [FFIType.cstring, FFIType.cstring],
returns: FFIType.cstring,
},
QuerySession: {
args: [FFIType.cstring, FFIType.cstring, FFIType.cstring],
returns: FFIType.cstring,
},
});
const { symbols: chdb } = cc({
source,
library: ["chdb"],
flags: ["-L./lib"],
symbols: {
Query: {
args: [FFIType.cstring, FFIType.cstring],
returns: FFIType.ptr,
},
QuerySession: {
args: [FFIType.cstring, FFIType.cstring, FFIType.cstring],
returns: FFIType.ptr,
},
free_cstr: {
args: [FFIType.ptr],
returns: FFIType.void,
}
}
});

// Standalone exported query function
export function query(query: string, format: string = "CSV") {
if (!query) {
return "";
if (!query) return "";
const resultPtr = chdb.Query(Buffer.from(query + "\0"), Buffer.from(format + "\0"));
if (resultPtr === null) return "";
try {
return new CString(resultPtr).toString();
} finally {
chdb.free_cstr(resultPtr);
}
return chdb.Query(Buffer.from(query + "\0"), Buffer.from(format + "\0"));
}

// Session class with path handling
Expand All @@ -29,11 +41,17 @@ class Session {

query(query: string, format: string = "CSV") {
if (!query) return "";
return chdb.QuerySession(
const resultPtr = chdb.QuerySession(
Buffer.from(query + "\0"),
Buffer.from(format + "\0"),
Buffer.from(this.path + "\0")
);
if (resultPtr === null) return "";
try {
return new CString(resultPtr).toString();
} finally {
chdb.free_cstr(resultPtr);
}
}

constructor(path: string = "") {
Expand Down
10 changes: 0 additions & 10 deletions lib/README.md

This file was deleted.

13 changes: 0 additions & 13 deletions lib/build.sh

This file was deleted.

28 changes: 0 additions & 28 deletions lib/chdb.h

This file was deleted.

87 changes: 72 additions & 15 deletions lib/chdb_bun.c
Original file line number Diff line number Diff line change
@@ -1,17 +1,28 @@
#include "chdb.h"
#include "chdb_bun.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define MAX_FORMAT_LENGTH 64
#define MAX_PATH_LENGTH 4096
#define MAX_ARG_COUNT 6
#define QUERY_PREFIX "--query="
#define FORMAT_PREFIX "--output-format="
#define PATH_PREFIX "--path="

// Utility function to construct argument string
void construct_arg(char *dest, const char *prefix, const char *value,
size_t dest_size) {
snprintf(dest, dest_size, "%s%s", prefix, value);
// Returns 0 on success, -1 if truncated
int construct_arg(char *dest, const char *prefix, const char *value,
size_t dest_size) {
if (dest == NULL || prefix == NULL || value == NULL) {
return -1;
}
int written = snprintf(dest, dest_size, "%s%s", prefix, value);
// Check if output was truncated
if (written < 0 || (size_t)written >= dest_size) {
return -1;
}
return 0;
}

// Generalized query function
Expand All @@ -20,27 +31,50 @@ char *general_query(int argc, char *args[]) {

if (result == NULL) {
return NULL;
} else {
return result->buf;
}

char *buf_copy = malloc(result->len + 1);
if (buf_copy == NULL) {
free_result(result);
return NULL;
}
memcpy(buf_copy, result->buf, result->len);
buf_copy[result->len] = '\0';

// Free the original result
free_result(result);

return buf_copy;
}

// Query function without session
char *Query(const char *query, const char *format) {
if (query == NULL || format == NULL) {
return NULL;
}

char dataFormat[MAX_FORMAT_LENGTH];
char *dataQuery;
char *args[MAX_ARG_COUNT] = {"clickhouse", "--multiquery", NULL, NULL};
int argc = 4;

construct_arg(dataFormat, "--output-format=", format, MAX_FORMAT_LENGTH);
// Validate format string length
if (construct_arg(dataFormat, FORMAT_PREFIX, format, MAX_FORMAT_LENGTH) != 0) {
return NULL;
}
args[2] = dataFormat;

dataQuery = (char *)malloc(strlen(query) + strlen("--query=") + 1);
// Allocate memory for query argument
size_t query_arg_len = strlen(query) + strlen(QUERY_PREFIX) + 1;
dataQuery = malloc(query_arg_len);
if (dataQuery == NULL) {
return NULL;
}
construct_arg(dataQuery, "--query=", query,
strlen(query) + strlen("--query=") + 1);

if (construct_arg(dataQuery, QUERY_PREFIX, query, query_arg_len) != 0) {
free(dataQuery);
return NULL;
}
args[3] = dataQuery;

char *result = general_query(argc, args);
Expand All @@ -51,27 +85,50 @@ char *Query(const char *query, const char *format) {
// QuerySession function will save the session to the path
// queries with same path will use the same session
char *QuerySession(const char *query, const char *format, const char *path) {
if (query == NULL || format == NULL || path == NULL) {
return NULL;
}

char dataFormat[MAX_FORMAT_LENGTH];
char dataPath[MAX_PATH_LENGTH];
char *dataQuery;
char *args[MAX_ARG_COUNT] = {"clickhouse", "--multiquery", NULL, NULL, NULL};
int argc = 5;

construct_arg(dataFormat, "--output-format=", format, MAX_FORMAT_LENGTH);
// Validate format string length
if (construct_arg(dataFormat, FORMAT_PREFIX, format, MAX_FORMAT_LENGTH) != 0) {
return NULL;
}
args[2] = dataFormat;

dataQuery = (char *)malloc(strlen(query) + strlen("--query=") + 1);
// Allocate memory for query argument
size_t query_arg_len = strlen(query) + strlen(QUERY_PREFIX) + 1;
dataQuery = malloc(query_arg_len);
if (dataQuery == NULL) {
return NULL;
}
construct_arg(dataQuery, "--query=", query,
strlen(query) + strlen("--query=") + 1);

if (construct_arg(dataQuery, QUERY_PREFIX, query, query_arg_len) != 0) {
free(dataQuery);
return NULL;
}
args[3] = dataQuery;

construct_arg(dataPath, "--path=", path, MAX_PATH_LENGTH);
// Validate path string length
if (construct_arg(dataPath, PATH_PREFIX, path, MAX_PATH_LENGTH) != 0) {
free(dataQuery);
return NULL;
}
args[4] = dataPath;

char *result = general_query(argc, args);
free(dataQuery);
return result;
}

// Free function for FFI callers to clean up query results
void free_cstr(char *result) {
if (result != NULL) {
free(result);
}
}
4 changes: 0 additions & 4 deletions lib/chdb_bun.h

This file was deleted.

Loading