Skip to content

Commit 0bf2c28

Browse files
geeksilva97RafaelGSS
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 ce2274d commit 0bf2c28

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
@@ -751,6 +830,7 @@ resolution handler passed to [`database.applyChangeset()`][]. See also
751830
[`sqlite3_column_origin_name()`]: https://www.sqlite.org/c3ref/column_database_name.html
752831
[`sqlite3_column_table_name()`]: https://www.sqlite.org/c3ref/column_database_name.html
753832
[`sqlite3_create_function_v2()`]: https://www.sqlite.org/c3ref/create_function.html
833+
[`sqlite3_create_window_function()`]: https://www.sqlite.org/c3ref/create_function.html
754834
[`sqlite3_exec()`]: https://www.sqlite.org/c3ref/exec.html
755835
[`sqlite3_expanded_sql()`]: https://www.sqlite.org/c3ref/expanded_sql.html
756836
[`sqlite3_last_insert_rowid()`]: https://www.sqlite.org/c3ref/last_insert_rowid.html

src/env_properties.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -207,6 +207,7 @@
207207
V(inputs_string, "inputs") \
208208
V(internal_binding_string, "internalBinding") \
209209
V(internal_string, "internal") \
210+
V(inverse_string, "inverse") \
210211
V(ipv4_string, "IPv4") \
211212
V(ipv6_string, "IPv6") \
212213
V(isclosing_string, "isClosing") \
@@ -334,6 +335,7 @@
334335
"const __esModule = true;") \
335336
V(require_string, "require") \
336337
V(resource_string, "resource") \
338+
V(result_string, "result") \
337339
V(retry_string, "retry") \
338340
V(return_arrays_string, "returnArrays") \
339341
V(return_string, "return") \
@@ -361,12 +363,14 @@
361363
V(specifier_string, "specifier") \
362364
V(stack_string, "stack") \
363365
V(standard_name_string, "standardName") \
366+
V(start_string, "start") \
364367
V(start_time_string, "startTime") \
365368
V(state_string, "state") \
366369
V(statement_string, "statement") \
367370
V(stats_string, "stats") \
368371
V(status_string, "status") \
369372
V(stdio_string, "stdio") \
373+
V(step_string, "step") \
370374
V(stream_average_duration_string, "streamAverageDuration") \
371375
V(stream_count_string, "streamCount") \
372376
V(subject_string, "subject") \

0 commit comments

Comments
 (0)