Skip to main content

sqlalchemy_window

A SQLAlchemy plugin to add support for PostgreSQL WINDOW clause.

NOTE: only supports SQLAlchemy 2.0 and higher.

Example

Newer versions (>= 0.1.3) allow you to use Window.over_self interface for window functions:

import sqlalchemy as sa
from sqlalchemy_window import over_window, select, window

metadata = sa.MetaData()
table = sa.Table(
  "prices",
  metadata,
  sa.Column("asset", sa.String(16), primary_key=True),
  sa.Column("ts", sa.DateTime(timezone=True), primary_key=True),
  sa.Column("price", sa.Numeric, nullable=False),
)

w = window("w", partition_by=table.c["asset"], order_by=table.c["ts"], range_=(None, None))

query = select(
  w.over_self(sa.func.first_value(table.c["price"])).label("open"),
  w.over_self(sa.func.max(table.c["price"])).label("high"),
  w.over_self(sa.func.min(table.c["price"])).label("low"),
  w.over_self(sa.func.last_value(table.c["price"])).label("close"),
).where(sa.func.cast(table.c["ts"], sa.Date) == '2023-01-01').window(w)

Before that, to build above query you would use over_window factory:

query = select(
  over_window(sa.func.first_value(table.c["price"]), w).label("open"),
  over_window(sa.func.max(table.c["price"]), w).label("high"),
  over_window(sa.func.min(table.c["price"]), w).label("low"),
  over_window(sa.func.last_value(table.c["price"]), w).label("close"),
).where(sa.func.cast(table.c["ts"], sa.Date) == '2023-01-01').window(w)

You can still use over_window in newer versions.

Development

To setup a development environment run:

python3 -m venv venv
source ./venv/bin/activate
pip install --upgrade pip
pip install -r dev-requirements.txt -e .
pre-commit install

Running tests:

make test
make coverage

A waterfountain1996 project.

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

sqlalchemy_window-0.1.4.tar.gz (8.4 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

sqlalchemy_window-0.1.4-py3-none-any.whl (8.0 kB view details)

Uploaded Python 3

File details

Details for the file sqlalchemy_window-0.1.4.tar.gz.

File metadata

  • Download URL: sqlalchemy_window-0.1.4.tar.gz
  • Upload date:
  • Size: 8.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.7.16

File hashes

Hashes for sqlalchemy_window-0.1.4.tar.gz
Algorithm Hash digest
SHA256 e7d5f28d8b8c3091a6ba5519f2dee0803da618bbf47d41bf8be9125ffd1e3b0f
MD5 91fbaf1d3b38ab953f43179578d0c18c
BLAKE2b-256 303127d7a8330f18398f713c4f261a5d27d7047ca77f5e916c72c7d257ad9c6d

See more details on using hashes here.

File details

Details for the file sqlalchemy_window-0.1.4-py3-none-any.whl.

File metadata

File hashes

Hashes for sqlalchemy_window-0.1.4-py3-none-any.whl
Algorithm Hash digest
SHA256 90a14865cc82cf1fb22c103f63852b87f182cfc06e38b0eeffcca406ce5f44a0
MD5 447b18df7a29b894fcf5e81b284bc009
BLAKE2b-256 b320dbc01fc0b66a5ad28e5f0d2d0f2251417c9b59c4cb92dceb2dceab9b4d03

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page