Skip to content

Implement window - #478

Merged
aljazerzen merged 10 commits into
mainfrom
window
May 14, 2022
Merged

aljazerzen merged 10 commits into
mainfrom
window

Conversation

@aljazerzen

Copy link
Copy Markdown
Member

I've:

  • added window transform, including docs,
  • added a few window functions to stdlib,
  • refactored literals into their own enum, to facilitate int and float literals,
  • removed raw literal,
  • added AnyOf wrapper for types, so functions such as sum can return column | scalar
  • made functions in select and derive be window functions and not aggregation

Closes #420
Closes #3

@aljazerzen
aljazerzen requested a review from max-sixty May 13, 2022 09:24
Comment thread prql-compiler/src/ast/item.rs Resolved
Comment thread prql-compiler/src/ast/literal.rs Resolved
@max-sixty max-sixty mentioned this pull request May 13, 2022
Comment thread prql-compiler/src/ast/types.rs Resolved
Comment thread prql-compiler/src/ast/types.rs Resolved
Comment thread prql-compiler/src/parser.rs Resolved
Comment thread prql-compiler/src/semantic/resolver.rs Resolved
Comment thread prql-compiler/src/sql/translator.rs Resolved
Comment thread prql-compiler/src/sql/translator.rs Resolved
current row. If a bound is omitted, segment will extend until the end of the table (or group).

For example:
- `rows:0..3` means current row plus two following,

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was thinking how to do current — this approach is perfect, can't believe I didn't think of it

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Though I'm still thinking through the answer to the question "what number is the current row?".

Did you think about making 0 the current row on both sides, and the range being inclusive?

It looks like that's how it's defined here:

`rows:-3..0`  means two preceding rows plus current row,

but possibly not here:

`rows:..1`    means all rows from the start of the table to and including current row,

Or am I missing something?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, that's a mistake. It should be

`rows:-2..1` means two preceding rows plus current row,

Such indexing was supported by Dijkstra it it seem most natural to me. I also wanted this to be similar to python and rust, while distancing from matlab...

@max-sixty max-sixty May 14, 2022 •

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wonderful reference and explanation.

Do you think we should have a slightly different syntax for inclusive vs exclusive ranges, like rust has, then? My concern is that SQL ranges are by default inclusive; e.g. BETWEEN 0 and 3, or in 0..3 includes both 0 & 3, as does range:0..3

There are a few areas like this where it's difficult to completely upgrade SQL, because we can't see through to the data. So we can either separate off the parts we can — e.g. different approach to rows than other ranges — or compromises to their standards — e.g. just use inclusive for everything.

WDYT?

Comment thread reference/src/transforms/window.md Resolved

For ease of use, there are two flags that override `rows` or `range`:

- `expanding:true` is an alias for `range:..1`. Sum using this window is also known as "cumulative sum".

@max-sixty max-sixty May 13, 2022 •

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wouldn't this be (but probably I'm misunderstanding):

Suggested change
- `expanding:true` is an alias for `range:..1`. Sum using this window is also known as "cumulative sum".
- `expanding:true` is an alias for `rows:..1`. Sum using this window is also known as "cumulative sum".

?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@aljazerzen just wanted to check you saw this. IIUC this only matters when there are duplicate values; otherwise they're the same (or possibly my SQL knowledge is rusty)

@max-sixty

Copy link
Copy Markdown
Member

Very impressive! I left a few comments re the rows:..1 vs rows:..0 syntax, and the ORDER BY.

I think this is the last big code feature in the compiler for 0.2! FYI I'll have some time this weekend to clean up things we need for 0.2 — docs / website / follow-ups etc.

@aljazerzen aljazerzen mentioned this pull request May 14, 2022
@aljazerzen
aljazerzen enabled auto-merge (squash) May 14, 2022 08:42
@aljazerzen
aljazerzen merged commit fb8448f into main May 14, 2022
@aljazerzen
aljazerzen deleted the window branch May 14, 2022 08:43
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Window functions Window functions over groups

2 participants