summaryrefslogtreecommitdiffstats
path: root/Doc/includes
diff options
context:
space:
mode:
authorErlend Egeberg Aasland <erlend.aasland@innova.no>2022-04-12 00:55:59 (GMT)
committerGitHub <noreply@github.com>2022-04-12 00:55:59 (GMT)
commit9ebcece82fe11b87cc3d6e6b4c439aab9e3ab1e6 (patch)
treeef6b3c2d043f9b85ed4b15aa684eab941e25347f /Doc/includes
parentf45aa8f304a12990c2ca687f2088f04b07906033 (diff)
downloadcpython-9ebcece82fe11b87cc3d6e6b4c439aab9e3ab1e6.zip
cpython-9ebcece82fe11b87cc3d6e6b4c439aab9e3ab1e6.tar.gz
cpython-9ebcece82fe11b87cc3d6e6b4c439aab9e3ab1e6.tar.bz2
gh-79097: Add support for aggregate window functions in sqlite3 (GH-20903)
Diffstat (limited to 'Doc/includes')
-rw-r--r--Doc/includes/sqlite3/sumintwindow.py46
1 files changed, 46 insertions, 0 deletions
diff --git a/Doc/includes/sqlite3/sumintwindow.py b/Doc/includes/sqlite3/sumintwindow.py
new file mode 100644
index 0000000..0e915d6
--- /dev/null
+++ b/Doc/includes/sqlite3/sumintwindow.py
@@ -0,0 +1,46 @@
+# Example taken from https://www.sqlite.org/windowfunctions.html#udfwinfunc
+import sqlite3
+
+
+class WindowSumInt:
+ def __init__(self):
+ self.count = 0
+
+ def step(self, value):
+ """Adds a row to the current window."""
+ self.count += value
+
+ def value(self):
+ """Returns the current value of the aggregate."""
+ return self.count
+
+ def inverse(self, value):
+ """Removes a row from the current window."""
+ self.count -= value
+
+ def finalize(self):
+ """Returns the final value of the aggregate.
+
+ Any clean-up actions should be placed here.
+ """
+ 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)
+con.create_window_function("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
+""")
+print(cur.fetchall())