duckdb-engine 0.1.7
pip install duckdb-engine==0.1.7
Released:
No project description provided
Navigation
Unverified details
These details have not been verified by PyPIProject links
Meta
- License: MIT License (MIT)
- Author: Elliana
- Requires: Python >=3.6.1
Classifiers
- License
- Programming Language
Project description
duckdb_engine
Very very very basic sqlalchemy driver for duckdb
Once you install this package, you should be able to just use it, as sqlalchemy does a python path search
from sqlalchemy import Column, Integer, Sequence, String, create_engine
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm.session import Session
Base = declarative_base()
class FakeModel(Base): # type: ignore
__tablename__ = "fake"
id = Column(Integer, Sequence("fakemodel_id_sequence"), primary_key=True)
name = Column(String)
eng = create_engine("duckdb:///:memory:")
Base.metadata.create_all(eng)
session = Session(bind=eng)
session.add(FakeModel(name="Frank"))
session.commit()
frank = session.query(FakeModel).one()
assert frank.name == "Frank"
Things to keep in mind
Duckdb's SQL parser is based on the PostgreSQL parser, but not all features in PostgreSQL are supported in duckdb. Because the duckdb_engine
dialect is derived from the postgresql
dialect, sqlalchemy
may try to use PostgreSQL-only features. Below are some caveats to look out for.
Auto-incrementing ID columns
When defining an Integer column as a primary key, sqlalchemy
uses the SERIAL
datatype for PostgreSQL. Duckdb does not yet support this datatype because it's a non-standard PostgreSQL legacy type, so a workaround is to use the sqlalchemy.Sequence()
object to auto-increment the key. For more information on sequences, you can find the sqlalchemy Sequence
documentation here.
The following example demonstrates how to create an auto-incrementing ID column for a simple table:
>>> import sqlalchemy
>>> engine = sqlalchemy.create_engine('duckdb:////path/to/duck.db')
>>> metadata = sqlalchemy.MetaData(engine)
>>> user_id_seq = sqlalchemy.Sequence('user_id_seq')
>>> users_table = sqlalchemy.Table(
... 'users',
... metadata,
... sqlalchemy.Column(
... 'id',
... sqlalchemy.Integer,
... user_id_seq,
... server_default=user_id_seq.next_value(),
... primary_key=True,
... ),
... )
>>> metadata.create_all(bind=engine)
Pandas read_sql()
chunksize
The pandas.read_sql()
method can read tables from duckdb_engine
into DataFrames, but the sqlalchemy.engine.result.ResultProxy
trips up when fetchmany()
is called. Therefore, for now chunksize=None
(default) is necessary when reading duckdb tables into DataFrames. For example:
>>> import pandas as pd
>>> import sqlalchemy
>>> engine = sqlalchemy.create_engine('duckdb:////path/to/duck.db')
>>> df = pd.read_sql('users', engine) ### Works as expected
>>> df = pd.read_sql('users', engine, chunksize=25) ### Throws an exception
Project details
Unverified details
These details have not been verified by PyPIProject links
Meta
- License: MIT License (MIT)
- Author: Elliana
- Requires: Python >=3.6.1
Classifiers
- License
- Programming Language
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file duckdb_engine-0.1.7.tar.gz
.
File metadata
- Download URL: duckdb_engine-0.1.7.tar.gz
- Upload date:
- Size: 6.5 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.1.6 CPython/3.9.6 Windows/10
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 6a4a77b703fba58509edbfbea427858f267453a219e6119e14492da1d80f7b9d |
|
MD5 | 818064272ded406788ddd27ff68a0fc4 |
|
BLAKE2b-256 | 0217d23f8d002b09602b4a6d50a5f3596e24980e806db3d2455a7235eec00f34 |
File details
Details for the file duckdb_engine-0.1.7-py3-none-any.whl
.
File metadata
- Download URL: duckdb_engine-0.1.7-py3-none-any.whl
- Upload date:
- Size: 7.9 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.1.6 CPython/3.9.6 Windows/10
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 5f0c186876f24c588fdbf60037c81d8bc6402c5428ecd1f6b1009826f76be067 |
|
MD5 | de639c0ac8fdf4a9c123629683f06434 |
|
BLAKE2b-256 | 8aae88dc74b074dca153eca499f93e0ff90333bc56a38f873d17d0029cc1ecd2 |