linkml_store.api.stores.duckdb.duckdb_database module

class DuckDBDatabase(handle=None, recreate_if_exists=False, **kwargs)[source]

Bases: Database

An adapter for DuckDB databases.

Note that this adapter does not make use of a LinkML relational model transformation and SQL Alchemy ORM layer. Instead, it attempts to map each collection (which is of type some LinkML class) to a single DuckDB table. New tables are not created for nested references, and linking tables are not created for many-to-many relationships.

Instead the native DuckDB ARRAY type is used to store multivalued attributes, and DuckDB JSON types are used for nested inlined objects.

collection_class

alias of DuckDBCollection

__init__(handle=None, recreate_if_exists=False, **kwargs)[source]
property engine: Engine
commit(**kwargs)[source]

Commit pending changes to the database.

Parameters:

kwargs

Returns:

close(**kwargs)[source]

Close the database.

Parameters:

kwargs

Returns:

drop(missing_ok=True, **kwargs)[source]

Drop the database and all collections.

>>> from linkml_store.api.client import Client
>>> client = Client()
>>> path = Path("/tmp/test.db")
>>> path.parent.mkdir(exist_ok=True, parents=True)
>>> db = client.attach_database(f"duckdb:///{path}")
>>> db.store({"persons": [{"id": "P1", "name": "John", "age_in_years": 30}]})
>>> coll = db.get_collection("persons")
>>> coll.find({}).num_rows
1
>>> db.drop()
>>> db = client.attach_database("duckdb:///tmp/test.db", alias="test")
>>> coll = db.get_collection("persons")
>>> coll.find({}).num_rows
0
Parameters:

kwargs – additional arguments

query(query, **kwargs)[source]

Run a query against the database.

Examples

>>> from linkml_store.api.client import Client
>>> from linkml_store.api.queries import Query
>>> client = Client()
>>> db = client.attach_database("duckdb", alias="test")
>>> collection = db.create_collection("Person")
>>> collection.insert([{"id": "P1", "name": "John"}, {"id": "P2", "name": "Alice"}])
>>> query = Query(from_table="Person", where_clause={"name": "John"})
>>> result = db.query(query)
>>> len(result.rows)
1
>>> result.rows[0]["id"]
'P1'
type query:

Query

param query:

type kwargs:

param kwargs:

rtype:

QueryResult

return:

init_collections()[source]

Initialize collections.

TODO: Not typically called directly: consider making this private :return:

induce_schema_view()[source]

Induce a schema view from a schema definition.

>>> from linkml_store.api.client import Client
>>> from linkml_store.api.queries import Query
>>> client = Client()
>>> db = client.attach_database("duckdb", alias="test")
>>> collection = db.create_collection("Person")
>>> collection.insert([{"id": "P1", "name": "John", "age_in_years": 25},
...                 {"id": "P2", "name": "Alice", "age_in_years": 25}])
>>> schema_view = db.induce_schema_view()
>>> cd = schema_view.get_class("Person")
>>> cd.attributes["id"].range
'string'
>>> cd.attributes["age_in_years"].range
'integer'
Return type:

SchemaView

Returns:

A schema view

export_database(location, target_format=None, **kwargs)[source]

Export a database to a file or location.

>>> from linkml_store.api.client import Client
>>> client = Client()
>>> db = client.attach_database("duckdb", alias="test")
>>> db.import_database("tests/input/iris.csv", Format.CSV, collection_name="iris")
>>> db.export_database("/tmp/iris.yaml", Format.YAML)
Parameters:
  • location (str) – location of the file

  • target_format (Union[str, Format, None]) – target format

  • kwargs – additional arguments

import_database(location, source_format=None, **kwargs)[source]

Import a database from a file or location.

Parameters:
  • location (str) – location of the file

  • source_format (Optional[str]) – source format

  • kwargs – additional arguments