Skip to content

Commit

Permalink
Merge pull request #11 from sbrunk/use-magic
Browse files Browse the repository at this point in the history
Use the Mojo package manager Magic for the project and add CI tests
  • Loading branch information
sbrunk authored Oct 7, 2024
2 parents 280ec8b + 74c1740 commit c6c9985
Show file tree
Hide file tree
Showing 9 changed files with 1,671 additions and 32 deletions.
2 changes: 2 additions & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# GitHub syntax highlighting
magic.lock linguist-language=YAML linguist-generated=true
35 changes: 35 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
name: Run tests

on: ["push"]

jobs:
build:
strategy:
matrix:
include:
- { target: linux-64, os: ubuntu-22.04 }
- { target: osx-arm64, os: macos-14 }
fail-fast: false

runs-on: ${{ matrix.os }}
timeout-minutes: 5

defaults:
run:
shell: bash

steps:
- name: Checkout repo
uses: actions/checkout@v4

- name: Install magic
run: |
curl -ssL https://magic.modular.com | bash
- name: Execute tests
run: |
source $HOME/.bash_profile
magic run mojo run example.mojo
magic run test
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -165,4 +165,8 @@ cython_debug/
.pixi
*.egg-info
# magic environments
.magic/

.vscode/
# magic environments
.magic
29 changes: 12 additions & 17 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,16 @@ from duckdb import *
var con = DuckDB.connect(":memory:")
_ = con.execute(
"""
_ = con.execute("""
SET autoinstall_known_extensions=1;
SET autoload_known_extensions=1;
CREATE TABLE train_services AS
FROM 's3://duckdb-blobs/train_services.parquet';
"""
)
var result = con.execute(
"""
var result = con.execute("""
-- Get the top-3 busiest train stations
SELECT station_name, count(*) AS num_services
FROM train_services
Expand All @@ -45,21 +46,15 @@ for row in range(len(result)):

## Installation

1. [Install Mojo](https://docs.modular.com/mojo/manual/get-started#1-install-mojo). Currently nightly >= `2024.7.1105` is required, so install or update the nightly version: `modular install nightly/mojo`
2. Download the DuckDB C/C++ library from the [installation](https://duckdb.org/docs/installation/?version=stable&environment=cplusplus) page.
3. Extract `libduckdb.so` (Linux) or `libduckdb.dylib` (macOS) to the project directory.
4. Set library path:
```shell
export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:$(realpath .) # Linux
export DYLD_FALLBACK_LIBRARY_PATH=$(realpath .) # macOS
```
5. Run
``` shell
mojo example.mojo
```
Currently, you'll need to checkout the source. We'll publish a Conda package soon to make it easier to use from another Mojo project.

1. [Install the Magic package manager for Mojo](https://docs.modular.com/mojo/manual/get-started#1-install-mojo).
2. Checkout this repo
3. Run `magic shell`
4. Run `mojo example.mojo`

### Run Tests

```shell
mojo test -I .
magic run test
```
27 changes: 17 additions & 10 deletions duckdb/_c_api/c_api.mojo
Original file line number Diff line number Diff line change
Expand Up @@ -384,15 +384,18 @@ fn get_libname() -> StringLiteral:
return "libduckdb.so"


@value
struct LibDuckDB:
var lib: DLHandle

fn __init__(inout self, path: String = get_libname()):
self.lib = DLHandle(path)

fn __del__(owned self):
self.lib.close()
@staticmethod
fn destroy(inout existing):
existing.lib.close()

fn __copyinit__(inout self, existing: Self):
self.lib = existing.lib

# ===--------------------------------------------------------------------===#
# Open/Connect
Expand Down Expand Up @@ -1136,11 +1139,13 @@ struct LibDuckDB:
* type: The child type of list type to create.
* returns: The logical type.
"""
return self.lib.get_function[fn (duckdb_logical_type) -> duckdb_logical_type](
"duckdb_create_list_type"
)(type)
return self.lib.get_function[
fn (duckdb_logical_type) -> duckdb_logical_type
]("duckdb_create_list_type")(type)

fn duckdb_create_array_type(self, type: duckdb_logical_type, array_size: idx_t) -> duckdb_logical_type:
fn duckdb_create_array_type(
self, type: duckdb_logical_type, array_size: idx_t
) -> duckdb_logical_type:
"""Creates an array type from its child type.
The resulting type should be destroyed with `duckdb_destroy_logical_type`.
Expand All @@ -1152,7 +1157,9 @@ struct LibDuckDB:
"duckdb_create_array_type"
)(type, array_size)

fn duckdb_create_map_type(self, key_type: duckdb_logical_type, value_type: duckdb_logical_type) -> duckdb_logical_type:
fn duckdb_create_map_type(
self, key_type: duckdb_logical_type, value_type: duckdb_logical_type
) -> duckdb_logical_type:
"""Creates a map type from its key type and value type.
The resulting type should be destroyed with `duckdb_destroy_logical_type`.
Expand Down Expand Up @@ -1183,7 +1190,8 @@ struct LibDuckDB:
self,
member_types: UnsafePointer[duckdb_logical_type],
member_names: UnsafePointer[UnsafePointer[C_char]],
member_count: idx_t) -> duckdb_logical_type:
member_count: idx_t,
) -> duckdb_logical_type:
"""Creates a STRUCT type from the passed member name and type arrays.
The resulting type should be destroyed with `duckdb_destroy_logical_type`.
Expand Down Expand Up @@ -1268,7 +1276,6 @@ struct LibDuckDB:
fn (duckdb_logical_type) -> duckdb_logical_type
]("duckdb_map_type_value_type")(type)


# fn duckdb_struct_type_child_count TODO
# fn duckdb_struct_type_child_name TODO
# fn duckdb_struct_type_child_type TODO
Expand Down
7 changes: 4 additions & 3 deletions duckdb/_c_api/libduckdb.mojo
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
from duckdb._c_api.c_api import LibDuckDB
from sys.ffi import _get_global


fn _init_global(ignored: UnsafePointer[NoneType]) -> UnsafePointer[NoneType]:
var ptr = UnsafePointer[LibDuckDB].alloc(1)
ptr[] = LibDuckDB()
return ptr.bitcast[NoneType]()


fn _destroy_global(duckdb: UnsafePointer[NoneType]):
# var p = duckdb.bitcast[LibDuckDB]()
# LibDuckDB.destroy(p[])
var p = duckdb.bitcast[LibDuckDB]()
LibDuckDB.destroy(p[])
duckdb.free()


Expand All @@ -33,4 +34,4 @@ struct _DuckDBInterfaceImpl:


fn _impl() -> LibDuckDB:
return _get_global_duckdb_itf().libDuckDB()
return _get_global_duckdb_itf().libDuckDB()
6 changes: 4 additions & 2 deletions example.mojo
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,10 @@ from duckdb import *
def main():
var con = DuckDB.connect(":memory:")

_ = con.execute(
"""
_ = con.execute("""
SET autoinstall_known_extensions=1;
SET autoload_known_extensions=1;
CREATE TABLE train_services AS
FROM 's3://duckdb-blobs/train_services.parquet';
"""
Expand Down
Loading

0 comments on commit c6c9985

Please sign in to comment.