M
Modular3mo ago
Sören

DuckDB Bindings

Hey Mojicians! To help me getting into Mojo I've started working on a client API/bindings for my favorite database DuckDB: https://github.com/sbrunk/duckdb.mojo
GitHub
GitHub - sbrunk/duckdb.mojo: Mojo Bindings for DuckDB
Mojo Bindings for DuckDB. Contribute to sbrunk/duckdb.mojo development by creating an account on GitHub.
2 Replies
Sören
Sören2mo ago
I've improved the API with a bit of type foo to return native Mojo types, even for nested types like lists (column types in DuckDB can be arbitrarily deeply nested). Before this change results were always in returned in wrapper types like [ListVal[Int32Val]] and we had to peel them out one by one. Now we call i.e. chunk.get(list(string), row=0, col=0) and it will return a List[Optional[String] directly. This should make the API a bit more ergonomic to use:
var con = DuckDB.connect(":memory:")
var chunk = con.execute("SELECT ['a',NULL,'c']").fetch_chunk()
var column_result: List[Optional[String]] = chunk.get(list(string), row=0, col=0).value()
for elem in column_result:
print(elem[].or_else("NULL"), end=" ") # a NULL c

print()
var int_chunk = con.execute("SELECT unnest(range(10))").fetch_chunk()
var int_result: Int64 = int_chunk.get(int64, row=9, col=0).value()
print(int_result) # 9

for elem in int_chunk.get(int64, col=0):
print(elem[].value(), end="") # 0123456789
var con = DuckDB.connect(":memory:")
var chunk = con.execute("SELECT ['a',NULL,'c']").fetch_chunk()
var column_result: List[Optional[String]] = chunk.get(list(string), row=0, col=0).value()
for elem in column_result:
print(elem[].or_else("NULL"), end=" ") # a NULL c

print()
var int_chunk = con.execute("SELECT unnest(range(10))").fetch_chunk()
var int_result: Int64 = int_chunk.get(int64, row=9, col=0).value()
print(int_result) # 9

for elem in int_chunk.get(int64, col=0):
print(elem[].value(), end="") # 0123456789
Sören
Sören4w ago
Slides from the presentation at today's community meeting: https://blog.brunk.io/slides/2024-08-12-mojo-community-meeting-duckdb/
Random Thoughts
DuckDB.mojo – Random Thoughts
A 🔥 client for DuckDB
Want results from more Discord servers?
Add your server