Skip to content

Enable custom aggregate functions (take 2) #529

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

Merged
merged 36 commits into from
Sep 8, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
36 commits
Select commit Hold shift + click to select a range
24a6a12
initial commit.
Jul 3, 2020
fad9ba6
documentation
llimllib Sep 5, 2022
d191caa
remove no-longer-valid type
llimllib Sep 5, 2022
0d937a7
close over state initialization for performance
llimllib Sep 5, 2022
8fd3f8a
link documentation in comment
llimllib Sep 5, 2022
ba733ba
more testing
llimllib Sep 5, 2022
9e6b462
run tests if they're main
llimllib Sep 6, 2022
573afa7
accept a single arg
llimllib Sep 6, 2022
a3abdcb
this kind of works but I'm abandoning this branch
llimllib Sep 6, 2022
9daf01f
a middle road sqlite3_agg_context solution
llimllib Sep 6, 2022
ec5c72b
try out auto-updating state
llimllib Sep 6, 2022
a927950
improve quantile test, add multiple agg test
llimllib Sep 6, 2022
e643bd9
add a null to the test
llimllib Sep 6, 2022
2cbdb0e
acorn fails to parse ||=, whatever
llimllib Sep 6, 2022
b9ccd48
make eslint happy
llimllib Sep 6, 2022
ac548d4
make initial_value an argument
llimllib Sep 7, 2022
bf22aa1
test step and finalize exceptions
llimllib Sep 7, 2022
55858e9
add memory leak test
llimllib Sep 7, 2022
9a0c185
update docs to current interface
llimllib Sep 7, 2022
2445107
delete state in exception handlers
llimllib Sep 7, 2022
5b62cf6
remove null state
llimllib Sep 7, 2022
062f147
return init function and document object
llimllib Sep 7, 2022
7aff1ae
more tests and update back to init function
llimllib Sep 7, 2022
67f85e5
update redefinition test for new interface
llimllib Sep 7, 2022
b8692d4
update README to match fixed signature
llimllib Sep 7, 2022
b41e5cf
more consistent test formatting
llimllib Sep 7, 2022
d257bba
Update README.md
llimllib Sep 7, 2022
e82c286
clarify what exactly the result will contain
llimllib Sep 7, 2022
b65457c
Update README.md
lovasoa Sep 7, 2022
8d2c2e0
Update README.md
lovasoa Sep 7, 2022
f8f4a7c
Update README.md
lovasoa Sep 7, 2022
bdaa1b6
Update README.md
lovasoa Sep 7, 2022
e86d7ff
Update README.md
lovasoa Sep 7, 2022
423fc36
Improve documentation and type annotations
lovasoa Sep 8, 2022
f8e7bd3
ignore documentation in eslintrc
lovasoa Sep 8, 2022
799ebcd
reduce code size
lovasoa Sep 8, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
a middle road sqlite3_agg_context solution
  • Loading branch information
llimllib committed Sep 6, 2022
commit 9daf01f942ba656c0bfbbc3eafbe254f6aa9da67
51 changes: 39 additions & 12 deletions src/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,6 @@ Module["onRuntimeInitialized"] = function onRuntimeInitialized() {
var SQLITE_FLOAT = 2;
var SQLITE_TEXT = 3;
var SQLITE_BLOB = 4;
var SQLITE_NULL = 5;
// var - Encodings, used for registering functions.
var SQLITE_UTF8 = 1;
// var - cwrap function
Expand Down Expand Up @@ -1267,36 +1266,64 @@ Module["onRuntimeInitialized"] = function onRuntimeInitialized() {
name,
aggregateFunctions
) {
if (!aggregateFunctions.hasOwnProperty("init") ||
!aggregateFunctions.hasOwnProperty("step") ||
!aggregateFunctions.hasOwnProperty("finalize"))
throw "An aggregate function must have init, step and finalize properties";
if (!Object.hasOwnProperty.call(aggregateFunctions, "step")
|| !Object.hasOwnProperty.call(
aggregateFunctions, "finalize"
)
) {
throw "An aggregate function must have step and finalize "
+ "properties";
}

// state is a state array; we'll use the pointer p to serve as the
// key for where we hold our state so that multiple invocations of
// this function never step on each other
var state = {};

var state;
function wrapped_step(cx, argc, argv) {
var p = sqlite3_aggregate_context(cx, 999);
if (!state) {
state = aggregateFunctions["init"].apply(null);
// The first time the sqlite3_aggregate_context(C,N) routine is
// called for a particular aggregate function, SQLite allocates N
// bytes of memory, zeroes out that memory, and returns a pointer
// to the new memory.
//
// We're going to use that pointer as a key to our state array,
// since using sqlite3_aggregate_context as it's meant to be used
// through webassembly seems to be very difficult. Just allocate
// one byte.
var p = sqlite3_aggregate_context(cx, 1);

// If this is the first invocation of wrapped_step, state[p]
if (!state[p]) {
if (Object.hasOwnProperty.call(aggregateFunctions, "init")) {
state[p] = aggregateFunctions["init"].apply(null);
} else {
state[p] = [];
}
}

var args = parseFunctionArguments(argc, argv);
var mergedArgs = [state].concat(args);
var mergedArgs = [state[p]].concat(args);
try {
aggregateFunctions["step"].apply(null, mergedArgs);
} catch (error) {
sqlite3_result_error(cx, error, -1);
}
}

function wrapped_finalize(cx) {
var result;
var p = sqlite3_aggregate_context(cx, 1);
try {
result = aggregateFunctions["finalize"].apply(null, [state]);
result = aggregateFunctions["finalize"].apply(null, [state[p]]);
} catch (error) {
sqlite3_result_error(cx, error, -1);
state = null;
return;
}
setFunctionResult(cx, result);
state = null;

// clear the state for this invocation
delete state[p];
}

if (Object.prototype.hasOwnProperty.call(this.functions, name)) {
Expand Down
6 changes: 3 additions & 3 deletions test/test_aggregate_functions.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,15 +30,15 @@ exports.test = function (SQL, assert) {

db.create_aggregate(
"json_agg", {
init: function() { return { vals: [] }; },
step: function(state, val) { state.vals.push(val); },
finalize: function(state) { return JSON.stringify(state.vals); }
step: function(state, val) { state.push(val); },
finalize: function(state) { return JSON.stringify(state); }
}
);

db.exec("CREATE TABLE test2 (col, col2);");
db.exec("INSERT INTO test2 values ('four score', 12), ('and seven', 7), ('years ago', 1);");
var result = db.exec("SELECT json_agg(col) FROM test2;");
console.log("output: ", result[0].values);
assert.deepEqual(JSON.parse(result[0].values[0]), ["four score", "and seven", "years ago"], "Aggregate function that returns JSON");
}

Expand Down
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy