Skip to content

Commit a0a5e44

Browse files
committed
Use names instead of ids to identify table
1 parent b41ab10 commit a0a5e44

19 files changed

+170
-184
lines changed

.devcontainer/devcontainer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
"features": {
66
"ghcr.io/devcontainers/features/docker-in-docker:2": {}
77
},
8-
"postStartCommand": "git config devcontainers-theme.show-dirty 1",
8+
"postCreateCommand": "bash .devcontainer/postCreateCommand.sh",
99
"customizations": {
1010
"vscode": {
1111
"extensions": [

.devcontainer/postCreateCommand.sh

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
git config devcontainers-theme.show-dirty 1
2+
3+
cat >> ~/.bashrc <<EOF
4+
function pgmooncake_server() {
5+
sudo rm -rf /var/lib/postgresql
6+
sudo install -d -o vscode -g vscode /var/lib/postgresql
7+
mkdir -p /var/lib/postgresql/data
8+
docker run --name mooncake --rm -e POSTGRES_PASSWORD=password -v /var/lib/postgresql/data:/var/lib/postgresql/data --user "$(id -u):$(id -g)" mooncakelabs/pg_mooncake
9+
}
10+
11+
function pgmooncake_client() {
12+
docker exec -it mooncake psql -U postgres
13+
}
14+
EOF

README.md

Lines changed: 22 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -1,90 +1,42 @@
1-
# Mooncake
1+
# duckdb_mooncake
22

3-
This repository is based on https://github.com/duckdb/extension-template, check it out if you want to build and ship your own DuckDB extension.
3+
duckdb_mooncake is a DuckDB extension to read Iceberg tables written by [moonlink][moonlink-link] in real time.
44

5-
---
5+
## Installation
66

7-
This extension, Mooncake, allow you to ... <extension_goal>.
8-
9-
10-
## Building
11-
### Managing dependencies
12-
DuckDB extensions uses VCPKG for dependency management. Enabling VCPKG is very simple: follow the [installation instructions](https://vcpkg.io/en/getting-started) or just run the following:
13-
```shell
14-
git clone https://github.com/Microsoft/vcpkg.git
15-
./vcpkg/bootstrap-vcpkg.sh
16-
export VCPKG_TOOLCHAIN_PATH=`pwd`/vcpkg/scripts/buildsystems/vcpkg.cmake
7+
duckdb_mooncake can be installed using the `INSTALL` command:
8+
```sql
9+
INSTALL duckdb_mooncake FROM community;
1710
```
18-
Note: VCPKG is only required for extensions that want to rely on it for dependency management. If you want to develop an extension without dependencies, or want to do your own dependency management, just skip this step. Note that the example extension uses VCPKG to build with a dependency for instructive purposes, so when skipping this step the build may not work without removing the dependency.
1911

20-
### Build steps
21-
Now to build the extension, run:
22-
```sh
23-
make
24-
```
25-
The main binaries that will be built are:
26-
```sh
27-
./build/release/duckdb
28-
./build/release/test/unittest
29-
./build/release/extension/mooncake/mooncake.duckdb_extension
30-
```
31-
- `duckdb` is the binary for the duckdb shell with the extension code automatically loaded.
32-
- `unittest` is the test runner of duckdb. Again, the extension is already linked into the binary.
33-
- `mooncake.duckdb_extension` is the loadable binary as it would be distributed.
12+
## Usage
3413

35-
## Running the extension
36-
To run the extension code, simply start the shell with `./build/release/duckdb`.
14+
Mooncake databases can be attached using the `ATTACH` command, after which tables can be queried using standard SQL.
3715

38-
Now we can use the features from the extension directly in DuckDB:
39-
```
40-
D ATTACH DATABASE 'mooncake' (TYPE mooncake, URI '/var/lib/postgresql/data/pg_mooncake/moonlink.sock');
41-
D USE mooncake;
42-
D SELECT * FROM "5.16401";
16+
The example below attaches to the moonlink database `'postgres'`, from a moonlink instance listening at `'/var/lib/postgresql/data/pg_mooncake/moonlink.sock'`. This moonlink instance comes prepopulated with a table named `public.c`:
17+
```sql
18+
D ATTACH DATABASE 'mooncake' (TYPE mooncake, URI '/var/lib/postgresql/data/pg_mooncake/moonlink.sock', DATABASE 'postgres');
19+
D SELECT * FROM mooncake.public.c;
4320
┌───────┬─────────┐
44-
a b
21+
id val
4522
│ int32 │ varchar
4623
├───────┼─────────┤
47-
│ 1 │ a │
48-
│ 2 │ b │
49-
│ 3 │ c │
24+
1 │ Hello │
25+
2 │ World │
5026
└───────┴─────────┘
5127
```
5228

53-
## Running the tests
54-
Different tests can be created for DuckDB extensions. The primary way of testing DuckDB extensions should be the SQL tests in `./test/sql`. These SQL tests can be run using:
55-
```sh
56-
make test
57-
```
58-
59-
### Installing the deployed binaries
60-
To install your extension binaries from S3, you will need to do two things. Firstly, DuckDB should be launched with the
61-
`allow_unsigned_extensions` option set to true. How to set this will depend on the client you're using. Some examples:
29+
## Building
6230

63-
CLI:
64-
```shell
65-
duckdb -unsigned
31+
To build, type:
6632
```
67-
68-
Python:
69-
```python
70-
con = duckdb.connect(':memory:', config={'allow_unsigned_extensions' : 'true'})
33+
git submodule update --init --recursive
34+
GEN=ninja make
7135
```
7236

73-
NodeJS:
74-
```js
75-
db = new duckdb.Database(':memory:', {"allow_unsigned_extensions": "true"});
37+
To run, run the bundled `duckdb` shell:
7638
```
77-
78-
Secondly, you will need to set the repository endpoint in DuckDB to the HTTP url of your bucket + version of the extension
79-
you want to install. To do this run the following SQL query in DuckDB:
80-
```sql
81-
SET custom_extension_repository='bucket.s3.eu-west-1.amazonaws.com/<your_extension_name>/latest';
39+
./build/release/duckdb
8240
```
83-
Note that the `/latest` path will allow you to install the latest extension version available for your current version of
84-
DuckDB. To specify a specific version, you can pass the version instead.
8541

86-
After running these steps, you can install and load your extension using the regular INSTALL/LOAD commands in DuckDB:
87-
```sql
88-
INSTALL mooncake
89-
LOAD mooncake
90-
```
42+
[moonlink-link]: https://github.com/Mooncake-Labs/moonlink

moonlink_ffi/Cargo.lock

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

moonlink_ffi/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,4 @@ crate-type = ["staticlib"]
88

99
[dependencies]
1010
moonlink_rpc.git = "https://github.com/Mooncake-labs/moonlink.git"
11-
tokio = { version = "1.46", features = ["net"] }
11+
tokio = { version = "1.47", features = ["net"] }

moonlink_ffi/src/lib.rs

Lines changed: 25 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,7 @@ pub struct Void {
2727

2828
#[no_mangle]
2929
pub extern "C" fn moonlink_connect(uri: *const c_char) -> Result<*mut Stream> {
30-
let uri = unsafe { CStr::from_ptr(uri) };
31-
let uri = uri.to_str().expect("uri should be convertible to &str");
30+
let uri = ptr_to_str(uri);
3231
block_on(Stream::connect(uri))
3332
.map(|stream| Box::into_raw(Box::new(stream)))
3433
.into()
@@ -53,39 +52,54 @@ pub extern "C" fn moonlink_drop_stream(stream: *mut Stream) {
5352
#[no_mangle]
5453
pub extern "C" fn moonlink_get_table_schema(
5554
stream: *mut Stream,
56-
database_id: u32,
57-
table_id: u32,
55+
database: *const c_char,
56+
schema: *const c_char,
57+
table: *const c_char,
5858
) -> Result<*mut Data> {
5959
let stream = unsafe { &mut *stream };
60-
block_on(get_table_schema(stream, database_id, table_id))
60+
let database = ptr_to_str(database).to_owned();
61+
let table = format!("{}.{}", ptr_to_str(schema), ptr_to_str(table));
62+
block_on(get_table_schema(stream, database, table))
6163
.map(|schema| Box::into_raw(Box::new(schema.into())))
6264
.into()
6365
}
6466

6567
#[no_mangle]
6668
pub extern "C" fn moonlink_scan_table_begin(
6769
stream: *mut Stream,
68-
database_id: u32,
69-
table_id: u32,
70+
database: *const c_char,
71+
schema: *const c_char,
72+
table: *const c_char,
73+
lsn: u64,
7074
) -> Result<*mut Data> {
7175
let stream = unsafe { &mut *stream };
72-
block_on(scan_table_begin(stream, database_id, table_id, 0))
76+
let database = ptr_to_str(database).to_owned();
77+
let table = format!("{}.{}", ptr_to_str(schema), ptr_to_str(table));
78+
block_on(scan_table_begin(stream, database, table, lsn))
7379
.map(|metadata| Box::into_raw(Box::new(metadata.into())))
7480
.into()
7581
}
7682

7783
#[no_mangle]
7884
pub extern "C" fn moonlink_scan_table_end(
7985
stream: *mut Stream,
80-
database_id: u32,
81-
table_id: u32,
86+
database: *const c_char,
87+
schema: *const c_char,
88+
table: *const c_char,
8289
) -> Result<Void> {
8390
let stream = unsafe { &mut *stream };
84-
block_on(scan_table_end(stream, database_id, table_id))
91+
let database = ptr_to_str(database).to_owned();
92+
let table = format!("{}.{}", ptr_to_str(schema), ptr_to_str(table));
93+
block_on(scan_table_end(stream, database, table))
8594
.map(|unit| unit.into())
8695
.into()
8796
}
8897

98+
fn ptr_to_str(ptr: *const c_char) -> &'static str {
99+
let cstr = unsafe { CStr::from_ptr(ptr) };
100+
cstr.to_str().expect("DuckDB string should be valid UTF-8")
101+
}
102+
89103
fn block_on<F: Future>(future: F) -> F::Output {
90104
static RUNTIME: LazyLock<Runtime> = LazyLock::new(|| {
91105
Builder::new_current_thread()

0 commit comments

Comments
 (0)