tortoise-orm icon indicating copy to clipboard operation
tortoise-orm copied to clipboard

Window Functions

Open mikkothegeeko opened this issue 3 years ago • 0 comments

Is your feature request related to a problem? Please describe. There doesn't exist any documentation on custom to builtin window frames within Tortoise ORM. Is this something that's implemented but not documented? I propose we implement this.

Describe the solution you'd like

from tortoise.functions import WindowFunction

class Lead(Function):
    database_func = "lead"
    func_args = [1]
    over = [some_field, some_field]
    order_by = [some_field, some_field]

sql = Task.all().annotate(my_field=Lead('created_at', '%Y-%m-%d')).sql()

Describe alternatives you've considered We could also try adding a RawSQL class that we can put inside annotate()

Task.all().annotate(end_date=RawSQL("LEAD(start_date, 1) OVER (ORDER BY star_date)"))

mikkothegeeko avatar Sep 14 '22 22:09 mikkothegeeko