Skip to content

Commit 8856712

Browse files
geeksilva97aduh95
authored andcommitted
sqlite,doc,test: add aggregate function
PR-URL: #56600 Reviewed-By: Colin Ihrig <[email protected]> Reviewed-By: James M Snell <[email protected]>
1 parent 5e0503a commit 8856712

File tree

5 files changed

+843
-27
lines changed

5 files changed

+843
-27
lines changed

doc/api/sqlite.md

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,85 @@ added: v22.5.0
119119

120120
Constructs a new `DatabaseSync` instance.
121121

122+
### `database.aggregate(name, options)`
123+
124+
<!-- YAML
125+
added: REPLACEME
126+
-->
127+
128+
Registers a new aggregate function with the SQLite database. This method is a wrapper around
129+
[`sqlite3_create_window_function()`][].
130+
131+
* `name` {string} The name of the SQLite function to create.
132+
* `options` {Object} Function configuration settings.
133+
* `deterministic` {boolean} If `true`, the [`SQLITE_DETERMINISTIC`][] flag is
134+
set on the created function. **Default:** `false`.
135+
* `directOnly` {boolean} If `true`, the [`SQLITE_DIRECTONLY`][] flag is set on
136+
the created function. **Default:** `false`.
137+
* `useBigIntArguments` {boolean} If `true`, integer arguments to `options.step` and `options.inverse`
138+
are converted to `BigInt`s. If `false`, integer arguments are passed as
139+
JavaScript numbers. **Default:** `false`.
140+
* `varargs` {boolean} If `true`, `options.step` and `options.inverse` may be invoked with any number of
141+
arguments (between zero and [`SQLITE_MAX_FUNCTION_ARG`][]). If `false`,
142+
`inverse` and `step` must be invoked with exactly `length` arguments.
143+
**Default:** `false`.
144+
* `start` {number | string | null | Array | Object | Function} The identity
145+
value for the aggregation function. This value is used when the aggregation
146+
function is initialized. When a {Function} is passed the identity will be its return value.
147+
* `step` {Function} The function to call for each row in the aggregation. The
148+
function receives the current state and the row value. The return value of
149+
this function should be the new state.
150+
* `result` {Function} The function to call to get the result of the
151+
aggregation. The function receives the final state and should return the
152+
result of the aggregation.
153+
* `inverse` {Function} When this function is provided, the `aggregate` method will work as a window function.
154+
The function receives the current state and the dropped row value. The return value of this function should be the
155+
new state.
156+
157+
When used as a window function, the `result` function will be called multiple times.
158+
159+
```cjs
160+
const { DatabaseSync } = require('node:sqlite');
161+
162+
const db = new DatabaseSync(':memory:');
163+
db.exec(`
164+
CREATE TABLE t3(x, y);
165+
INSERT INTO t3 VALUES ('a', 4),
166+
('b', 5),
167+
('c', 3),
168+
('d', 8),
169+
('e', 1);
170+
`);
171+
172+
db.aggregate('sumint', {
173+
start: 0,
174+
step: (acc, value) => acc + value,
175+
});
176+
177+
db.prepare('SELECT sumint(y) as total FROM t3').get(); // { total: 21 }
178+
```
179+
180+
```mjs
181+
import { DatabaseSync } from 'node:sqlite';
182+
183+
const db = new DatabaseSync(':memory:');
184+
db.exec(`
185+
CREATE TABLE t3(x, y);
186+
INSERT INTO t3 VALUES ('a', 4),
187+
('b', 5),
188+
('c', 3),
189+
('d', 8),
190+
('e', 1);
191+
`);
192+
193+
db.aggregate('sumint', {
194+
start: 0,
195+
step: (acc, value) => acc + value,
196+
});
197+
198+
db.prepare('SELECT sumint(y) as total FROM t3').get(); // { total: 21 }
199+
```
200+
122201
### `database.close()`
123202

124203
<!-- YAML
@@ -728,6 +807,7 @@ resolution handler passed to [`database.applyChangeset()`][]. See also
728807
[`sqlite3_column_origin_name()`]: https://www.sqlite.org/c3ref/column_database_name.html
729808
[`sqlite3_column_table_name()`]: https://www.sqlite.org/c3ref/column_database_name.html
730809
[`sqlite3_create_function_v2()`]: https://www.sqlite.org/c3ref/create_function.html
810+
[`sqlite3_create_window_function()`]: https://www.sqlite.org/c3ref/create_function.html
731811
[`sqlite3_exec()`]: https://www.sqlite.org/c3ref/exec.html
732812
[`sqlite3_expanded_sql()`]: https://www.sqlite.org/c3ref/expanded_sql.html
733813
[`sqlite3_get_autocommit()`]: https://sqlite.org/c3ref/get_autocommit.html

src/env_properties.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -201,6 +201,7 @@
201201
V(input_string, "input") \
202202
V(internal_binding_string, "internalBinding") \
203203
V(internal_string, "internal") \
204+
V(inverse_string, "inverse") \
204205
V(ipv4_string, "IPv4") \
205206
V(ipv6_string, "IPv6") \
206207
V(isclosing_string, "isClosing") \
@@ -327,6 +328,7 @@
327328
"const __esModule = true;") \
328329
V(require_string, "require") \
329330
V(resource_string, "resource") \
331+
V(result_string, "result") \
330332
V(retry_string, "retry") \
331333
V(return_arrays_string, "returnArrays") \
332334
V(return_string, "return") \
@@ -353,12 +355,14 @@
353355
V(specifier_string, "specifier") \
354356
V(stack_string, "stack") \
355357
V(standard_name_string, "standardName") \
358+
V(start_string, "start") \
356359
V(start_time_string, "startTime") \
357360
V(state_string, "state") \
358361
V(statement_string, "statement") \
359362
V(stats_string, "stats") \
360363
V(status_string, "status") \
361364
V(stdio_string, "stdio") \
365+
V(step_string, "step") \
362366
V(stream_average_duration_string, "streamAverageDuration") \
363367
V(stream_count_string, "streamCount") \
364368
V(subject_string, "subject") \

0 commit comments

Comments
 (0)