-
-
Notifications
You must be signed in to change notification settings - Fork 1.9k
Improve sqlite Aggregration Protocols
#15188
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Draft
max-muoto
wants to merge
4
commits into
python:main
Choose a base branch
from
max-muoto:fix-aggregration-protocols
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+172
−22
Draft
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,142 @@ | ||
| import sqlite3 | ||
| import sys | ||
|
|
||
|
|
||
| class WindowSumInt: | ||
| def __init__(self) -> None: | ||
| self.count = 0 | ||
|
|
||
| def step(self, param: int) -> None: | ||
| self.count += param | ||
|
|
||
| def value(self) -> int: | ||
| return self.count | ||
|
|
||
| def inverse(self, param: int) -> None: | ||
| self.count -= param | ||
|
|
||
| def finalize(self) -> int: | ||
| return self.count | ||
|
|
||
|
|
||
| con = sqlite3.connect(":memory:") | ||
| cur = con.execute("CREATE TABLE test(x, y)") | ||
| values = [("a", 4), ("b", 5), ("c", 3), ("d", 8), ("e", 1)] | ||
| cur.executemany("INSERT INTO test VALUES(?, ?)", values) | ||
|
|
||
| if sys.version_info >= (3, 11): | ||
| con.create_window_function("sumint", 1, WindowSumInt) | ||
|
|
||
| con.create_aggregate("sumint", 1, WindowSumInt) | ||
| cur.execute( | ||
| """ | ||
| SELECT x, sumint(y) OVER ( | ||
| ORDER BY x ROWS BETWEEN 1 PRECEDING AND 1 FOLLOWING | ||
| ) AS sum_y | ||
| FROM test ORDER BY x | ||
| """ | ||
| ) | ||
| con.close() | ||
|
|
||
|
|
||
| def _create_window_function() -> WindowSumInt: | ||
| return WindowSumInt() | ||
|
|
||
|
|
||
| # A callable should work as well. | ||
| if sys.version_info >= (3, 11): | ||
| con.create_window_function("sumint", 1, _create_window_function) | ||
| con.create_aggregate("sumint", 1, _create_window_function) | ||
|
|
||
| # With num_args set to 1, the callable should not be called with more than one. | ||
|
|
||
|
|
||
| class WindowSumIntMultiArgs: | ||
| def __init__(self) -> None: | ||
| self.count = 0 | ||
|
|
||
| def step(self, *args: int) -> None: | ||
| self.count += sum(args) | ||
|
|
||
| def value(self) -> int: | ||
| return self.count | ||
|
|
||
| def inverse(self, *args: int) -> None: | ||
| self.count -= sum(args) | ||
|
|
||
| def finalize(self) -> int: | ||
| return self.count | ||
|
|
||
|
|
||
| if sys.version_info >= (3, 11): | ||
| con.create_window_function("sumint", 1, WindowSumIntMultiArgs) | ||
| con.create_window_function("sumint", 2, WindowSumIntMultiArgs) | ||
|
|
||
| con.create_aggregate("sumint", 1, WindowSumIntMultiArgs) | ||
| con.create_aggregate("sumint", 2, WindowSumIntMultiArgs) | ||
|
|
||
|
|
||
| # Test case: Fixed parameter aggregates (the common case in practice) | ||
| class FixedTwoParamAggregate: | ||
| def __init__(self) -> None: | ||
| self.total = 0 | ||
|
|
||
| def step(self, a: int, b: int) -> None: | ||
| self.total += a + b | ||
|
|
||
| def finalize(self) -> int: | ||
| return self.total | ||
|
|
||
|
|
||
| con.create_aggregate("sum2", 2, FixedTwoParamAggregate) | ||
|
|
||
|
|
||
| class FixedThreeParamWindowAggregate: | ||
| def __init__(self) -> None: | ||
| self.total = 0 | ||
|
|
||
| def step(self, a: int, b: int, c: int) -> None: | ||
| self.total += a + b + c | ||
|
|
||
| def inverse(self, a: int, b: int, c: int) -> None: | ||
| self.total -= a + b + c | ||
|
|
||
| def value(self) -> int: | ||
| return self.total | ||
|
|
||
| def finalize(self) -> int: | ||
| return self.total | ||
|
|
||
|
|
||
| if sys.version_info >= (3, 11): | ||
| con.create_window_function("sum3", 3, FixedThreeParamWindowAggregate) | ||
|
|
||
|
|
||
| # What do protocols still catch? | ||
|
|
||
|
|
||
| # Missing required method | ||
| class MissingStep: | ||
| def __init__(self) -> None: | ||
| self.total = 0 | ||
|
|
||
| def finalize(self) -> int: | ||
| return self.total | ||
|
|
||
|
|
||
| con.create_aggregate("bad", 2, MissingStep) # type: ignore[arg-type] # missing step method | ||
|
|
||
|
|
||
| # Invalid return type from finalize (not a valid SQLite type) | ||
| class BadFinalizeReturn: | ||
| def __init__(self) -> None: | ||
| self.items: list[int] = [] | ||
|
|
||
| def step(self, x: int) -> None: | ||
| self.items.append(x) | ||
|
|
||
| def finalize(self) -> list[int]: # list is not a valid SQLite type | ||
| return self.items | ||
|
|
||
|
|
||
| con.create_aggregate("bad2", 1, BadFinalizeReturn) # type: ignore[arg-type] # bad return type |
File renamed without changes.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We shouldn't necessarily enforce usage of
*argsin the any params case.