Skip to content

Commit 42c33e5

Browse files
committed
feat: add PyO3 Python API
1 parent 1f941aa commit 42c33e5

File tree

7 files changed

+560
-2
lines changed

7 files changed

+560
-2
lines changed

.github/workflows/ci.yml

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,3 +147,23 @@ jobs:
147147

148148
- name: Run native examples
149149
run: make native-examples
150+
# 7
151+
python-tests:
152+
name: Python bindings tests
153+
runs-on: ubuntu-latest
154+
steps:
155+
- uses: actions/checkout@v2
156+
157+
- name: Install stable toolchain
158+
uses: actions-rs/toolchain@v1
159+
with:
160+
toolchain: stable
161+
override: true
162+
163+
- name: Setup Python
164+
uses: actions/setup-python@v5
165+
with:
166+
python-version: "3.12"
167+
168+
- name: Run python binding tests
169+
run: make test-python

Cargo.lock

Lines changed: 94 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ default = ["macros"]
2828
macros = []
2929
net = ["dep:pgwire", "dep:async-trait", "dep:clap", "dep:env_logger", "dep:futures", "dep:log", "dep:tokio"]
3030
pprof = ["pprof/criterion", "pprof/flamegraph"]
31+
python = ["dep:pyo3"]
3132

3233
[[bench]]
3334
name = "query_bench"
@@ -48,6 +49,7 @@ itertools = { version = "0.12" }
4849
ordered-float = { version = "4", features = ["serde"] }
4950
paste = { version = "1" }
5051
parking_lot = { version = "0.12", features = ["arc_lock"] }
52+
pyo3 = { version = "0.23", features = ["auto-initialize"], optional = true }
5153
recursive = { version = "0.1" }
5254
regex = { version = "1" }
5355
rust_decimal = { version = "1" }

Makefile

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,16 @@ CARGO ?= cargo
33
WASM_PACK ?= wasm-pack
44
SQLLOGIC_PATH ?= tests/slt/**/*.slt
55

6-
.PHONY: test test-wasm test-slt test-all wasm-build check tpcc tpcc-dual cargo-check build wasm-examples native-examples fmt clippy
6+
.PHONY: test test-python test-wasm test-slt test-all wasm-build check tpcc tpcc-dual cargo-check build wasm-examples native-examples fmt clippy
77

88
## Run default Rust tests in the current environment (non-WASM).
99
test:
1010
$(CARGO) test --all
1111

12+
## Run Python binding API tests implemented with pyo3.
13+
test-python:
14+
$(CARGO) test --features python test_python_
15+
1216
## Perform a `cargo check` across the workspace.
1317
cargo-check:
1418
$(CARGO) check
@@ -30,7 +34,7 @@ test-slt:
3034
$(CARGO) run -p sqllogictest-test -- --path '$(SQLLOGIC_PATH)'
3135

3236
## Convenience target to run every suite in sequence.
33-
test-all: test test-wasm test-slt
37+
test-all: test test-wasm test-slt test-python
3438

3539
## Run formatting (check mode) across the workspace.
3640
fmt:

README.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,20 @@ console.log(rows.map((r) => r.values.map((v) => v.Int32 ?? v)));
5252
```
5353
- In Node.js, provide a small `localStorage` shim if you enable statistics-related features (see `examples/wasm_index_usage.test.mjs`).
5454
55+
## Python (PyO3)
56+
- Enable bindings with Cargo feature `python`.
57+
- Constructor is explicit: `Database(path)`; in-memory usage is `Database.in_memory()`.
58+
- Minimal usage:
59+
```python
60+
import kite_sql
61+
62+
db = kite_sql.Database.in_memory()
63+
db.execute("create table demo(id int primary key, v int)")
64+
db.execute("insert into demo values (1, 2), (2, 4)")
65+
rows = db.run("select * from demo").rows()
66+
print([row["values"] for row in rows])
67+
```
68+
5569
## Examples
5670
5771
```rust

src/lib.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,8 @@ mod optimizer;
115115
pub mod parser;
116116
pub mod paths;
117117
pub mod planner;
118+
#[cfg(all(not(target_arch = "wasm32"), feature = "python"))]
119+
pub mod python;
118120
pub mod serdes;
119121
pub mod storage;
120122
pub mod types;

0 commit comments

Comments
 (0)