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
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
more testing
  • Loading branch information
llimllib committed Sep 5, 2022
commit ba733ba041c04c38ba4b1cb08098172e74c964ce
46 changes: 42 additions & 4 deletions test/test_aggregate_functions.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,54 @@ exports.test = function (SQL, assert) {
var db = new SQL.Database();

db.create_aggregate(
"js_sum",
"sum",
function () { return { sum: 0 }; },
function (state, value) { state.sum += value; },
function (state) { return state.sum; }
);

db.exec("CREATE TABLE test (col);");
db.exec("INSERT INTO test VALUES (1), (2), (3);");
var result = db.exec("SELECT js_sum(col) FROM test;");
var result = db.exec("SELECT sum(col) FROM test;");
assert.equal(result[0].values[0][0], 6, "Simple aggregate function.");

// TODO: Add test cases..
}
db.create_aggregate(
"percentile",
function () { return { vals: [], pctile: null }; }, // init
function (state, value, pctile) {
state.vals.push(value);
},
function (state) {
return percentile(state.vals, state.pctile);
}
);
var result = db.exec("SELECT percentile(col, 20) FROM test;");
assert.equal(result[0].values[0][0], 1, "Aggregate function with two args");

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

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;");
assert.deepEqual(JSON.parse(result[0].values[0]), ["four score", "and seven", "years ago"], "Aggregate function that returns JSON");
}

// helper function to calculate a percentile from an array. Will modify the
// array in-place.
function percentile(arr, p) {
arr.sort();
const pos = (arr.length - 1) * (p / 100);
const base = Math.floor(pos);
const rest = pos - base;
if (arr[base + 1] !== undefined) {
return arr[base] + rest * (arr[base + 1] - arr[base]);
} else {
return arr[base];
}
};

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