Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(examples): Introduce DuckDB C example #22

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
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
6 changes: 6 additions & 0 deletions examples/duckdb-0.9-c/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
/rootfs/
/rootfs.cpio
/run-qemu*
/run-fc*
/kraft-run-*
/fc*.json
39 changes: 39 additions & 0 deletions examples/duckdb-0.9-c/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
FROM --platform=linux/x86_64 debian:bookworm AS build

RUN set -xe; \
apt -yqq update; \
apt -yqq install \
wget \
ca-certificates \
build-essential \
unzip \
;

ARG DUCKDB_VERSION=0.9.1

WORKDIR /src

RUN set -xe; \
wget https://github.com/duckdb/duckdb/releases/download/v${DUCKDB_VERSION}/libduckdb-linux-amd64.zip; \
unzip -q libduckdb-linux-amd64.zip; \
mv libduckdb.so /usr/local/lib/; \
ldconfig /usr/local/lib/libduckdb.so \
;

COPY ./main.c /src/main.c

RUN set -xe; \
gcc -Wall -I. -o /main /src/main.c -lduckdb

FROM scratch

COPY --from=build /main /main
COPY --from=build /usr/local/lib/libduckdb.so /usr/local/lib/libduckdb.so
COPY --from=build /lib/x86_64-linux-gnu/libc.so.6 /lib/x86_64-linux-gnu/libc.so.6
COPY --from=build /lib/x86_64-linux-gnu/libgcc_s.so.1 /lib/x86_64-linux-gnu/libgcc_s.so.1
COPY --from=build /lib/x86_64-linux-gnu/libpthread.so.0 /lib/x86_64-linux-gnu/libpthread.so.0
COPY --from=build /lib/x86_64-linux-gnu/libdl.so.2 /lib/x86_64-linux-gnu/libdl.so.2
COPY --from=build /lib/x86_64-linux-gnu/libstdc++.so.6 /lib/x86_64-linux-gnu/libstdc++.so.6
COPY --from=build /lib/x86_64-linux-gnu/libm.so.6 /lib/x86_64-linux-gnu/libm.so.6
COPY --from=build /lib64/ld-linux-x86-64.so.2 /lib64/ld-linux-x86-64.so.2
COPY --from=build /etc/ld.so.cache /etc/ld.so.cache
9 changes: 9 additions & 0 deletions examples/duckdb-0.9-c/Kraftfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
spec: v0.6

name: duckdb-c

runtime: index.unikraft.io/unikraft.org/base:latest

rootfs: ./Dockerfile

cmd: ["/main"]
3 changes: 3 additions & 0 deletions examples/duckdb-0.9-c/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
IMAGE_NAME = unikraft-duckdb-c

include ../../utils/bincompat/docker.Makefile
24 changes: 24 additions & 0 deletions examples/duckdb-0.9-c/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# DuckDB C Example

This directory contains a simple C example using the [DuckDB](https://duckdb.org) library running with Unikraft in binary compatibility mode.
The C program is extracted from [DuckDB examples](https://github.com/duckdb/duckdb/blob/main/examples/embedded-c/main.c) and it creates and uses a test database.

Follow the instructions in the common `../README.md` file to set up and configure the application.

## Quick Run

Use `kraft` to run the image:

```console
kraft run -M 128M
```

Once executed, it will print out information from the created database:

```console
```
Comment on lines +18 to +19
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add the output here.


## Scripted Run

Use the scripted runs, detailed in the common `../README.md`.
Running the scripts will print out information ame as above.
3 changes: 3 additions & 0 deletions examples/duckdb-0.9-c/config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
networking: False
accel: True
memory: 256
52 changes: 52 additions & 0 deletions examples/duckdb-0.9-c/main.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
/* https://github.com/duckdb/duckdb/blob/main/examples/embedded-c/main.c */

#include "duckdb.h"
#include <stdio.h>

int main() {
duckdb_database db = NULL;
duckdb_connection con = NULL;
duckdb_result result;

if (duckdb_open(NULL, &db) == DuckDBError) {
fprintf(stderr, "Failed to open database\n");
goto cleanup;
}
if (duckdb_connect(db, &con) == DuckDBError) {
fprintf(stderr, "Failed to open connection\n");
goto cleanup;
}
if (duckdb_query(con, "CREATE TABLE integers(i INTEGER, j INTEGER);", NULL) == DuckDBError) {
fprintf(stderr, "Failed to query database\n");
goto cleanup;
}
if (duckdb_query(con, "INSERT INTO integers VALUES (3, 4), (5, 6), (7, NULL);", NULL) == DuckDBError) {
fprintf(stderr, "Failed to query database\n");
goto cleanup;
}
if (duckdb_query(con, "SELECT * FROM integers", &result) == DuckDBError) {
fprintf(stderr, "Failed to query database\n");
goto cleanup;
}
// print the names of the result
idx_t row_count = duckdb_row_count(&result);
idx_t column_count = duckdb_column_count(&result);
for (size_t i = 0; i < column_count; i++) {
printf("%s ", duckdb_column_name(&result, i));
}
printf("\n");
// print the data of the result
for (size_t row_idx = 0; row_idx < row_count; row_idx++) {
for (size_t col_idx = 0; col_idx < column_count; col_idx++) {
char *val = duckdb_value_varchar(&result, col_idx, row_idx);
printf("%s ", val);
duckdb_free(val);
}
printf("\n");
}
// duckdb_print_result(result);
cleanup:
duckdb_destroy_result(&result);
duckdb_disconnect(&con);
duckdb_close(&db);
}