Git Product home page Git Product logo

sqlalchemy-window's Introduction

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 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.

sqlalchemy-window's People

Contributors

waterfountain1996 avatar

Watchers

Kostas Georgiou avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.