Skip to content

Commit

Permalink
Add stats extension (#2)
Browse files Browse the repository at this point in the history
  • Loading branch information
jvail committed Mar 21, 2022
1 parent ba161b8 commit dbb2581
Show file tree
Hide file tree
Showing 17 changed files with 99 additions and 56 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
src/tmp
test/files/tmp
build
node_modules
.vscode
Expand Down
3 changes: 3 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
[submodule "src/spatialite"]
path = src/spatialite
url = https://salsa.debian.org/debian-gis-team/spatialite.git
[submodule "src/sqlean"]
path = src/sqlean
url = https://github.com/nalgeon/sqlean.git
3 changes: 3 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# 0.1.0-beta.2

- add stats extension from https://github.com/nalgeon/sqlean
13 changes: 9 additions & 4 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ ZLIB_SRC = $(BUILD_DIR)/zlib-$(ZLIB_VERSION)
RTTOPO_SRC = $(BUILD_DIR)/librttopo
XML2_SRC = $(BUILD_DIR)/libxml2-$(XML2_VERSION)
ICONV_SRC = $(BUILD_DIR)/libiconv-$(ICONV_VERSION)
SQLEAN_EXT_SRC = src/sqlean/src

DIST_FLAGS :=
DIST_FLAGS += -s EXPORT_ES6=1
Expand Down Expand Up @@ -53,7 +54,7 @@ else
EMX_FLAGS += -Os
endif

em: dir zlib iconv sqlite proj geos rttopo xml2 spatialite
em: dir zlib iconv sqlite proj geos rttopo xml2 spatialite extensions

dir:
mkdir -p $(BUILD_DIR);
Expand Down Expand Up @@ -102,12 +103,12 @@ sqlite-src:
sqlite-conf: sqlite-src
cd $(SQLITE_SRC); \
emconfigure ./configure $(PREFIX) \
--disable-static-shell --disable-shared --disable-editline --disable-readline;
--disable-tcl --disable-shared --disable-editline --disable-readline --disable-load-extension;

sqlite: sqlite-conf
cd $(SQLITE_SRC); \
emmake make -j4 \
CFLAGS="$(EMX_FLAGS) -DSQLITE_ENABLE_FTS5 -DSQLITE_USE_URI=1 -DSQLITE_DQS=0 -DSQLITE_THREADSAFE=0 -DSQLITE_ENABLE_JSON1 -DSQLITE_DEFAULT_MEMSTATUS=0 -DSQLITE_OMIT_DEPRECATED -DSQLITE_OMIT_TCL_VARIABLE -DSQLITE_OMIT_SHARED_CACHE -DSQLITE_DEFAULT_FOREIGN_KEYS=1" \
CFLAGS="$(EMX_FLAGS) -DSQLITE_CORE -DSQLITE_ENABLE_FTS5 -DSQLITE_USE_URI=1 -DSQLITE_DQS=0 -DSQLITE_THREADSAFE=0 -DSQLITE_ENABLE_JSON1 -DSQLITE_DEFAULT_MEMSTATUS=0 -DSQLITE_OMIT_DEPRECATED -DSQLITE_OMIT_TCL_VARIABLE -DSQLITE_OMIT_SHARED_CACHE -DSQLITE_DEFAULT_FOREIGN_KEYS=1" \
LDFLAGS="-s ERROR_ON_UNDEFINED_SYMBOLS=0"; \
emmake make install;

Expand Down Expand Up @@ -250,6 +251,10 @@ spatialite-clean:
cd $(SPATIALITE_SRC) && make clean;


extensions:
emcc -v -I$(SQLITE_SRC) -DSQLITE_CORE $(EMX_FLAGS) $(ELD_FLAGS) -c $(SQLEAN_EXT_SRC)/sqlite3-stats.c -o $(BC_DIR)/ex.o;


tests:
cd $(SPATIALITE_SRC)/test; \
echo " \
Expand All @@ -272,7 +277,7 @@ tests:
spl: src/pre.js
emcc -v $(EMX_FLAGS) $(ELD_FLAGS) $(DIST_FLAGS) $(EXPORTED_FUNCTIONS) $(EXPORTED_RUNTIME_METHODS) \
-lz -lminizip -liconv -lsqlite3 -lgeos_c -lgeos -lrttopo -lproj -L$(BC_DIR)/lib \
-s ERROR_ON_UNDEFINED_SYMBOLS=0 $(BC_DIR)/lib/libspatialite.a \
-s ERROR_ON_UNDEFINED_SYMBOLS=0 $(BC_DIR)/lib/libspatialite.a $(BC_DIR)/ex.o \
-I$(SQLITE_SRC) \
-I$(SPATIALITE_SRC)/src/headers \
-I$(SPATIALITE_SRC)/src/headers/spatialite \
Expand Down
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@

SpatiaLite and friends - sqlite, geos, proj, rttopo - for node (sync API) and browser (async API).

[Here is a list](doc/spatialite_functions.md) of supported SpatiaLite SQL functions.
[Here is a list](doc/spatialite_functions.md) of supported SpatiaLite SQL functions and [a list](doc/extensions_functions.md) of available SQLite extension functions/modules.

Comments, bug reports and suggestions are welcome! spl.js will remain beta at least until the release of SpatiaLite 5.1.0.

## Install

```bash
npm install [email protected].1
npm install [email protected].2
```

The library for browsers bundles both the WebWorker script and the wasm file (~ 4MB). PROJ files (like proj.db) are not bundled but available from the `dist/proj` folder.
Expand Down
2 changes: 1 addition & 1 deletion dist/index.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/spl.js

Large diffs are not rendered by default.

Binary file modified dist/spl.wasm
Binary file not shown.
5 changes: 5 additions & 0 deletions doc/extensions_functions.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# spl.js supported SQLite extensions functions

## Statistics

https://github.com/nalgeon/sqlean/blob/main/docs/stats.md
75 changes: 30 additions & 45 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "spl.js",
"version": "0.1.0-beta.1",
"version": "0.1.0-beta.2",
"description": "SpatiaLite and friends - sqlite, geos, proj, rttopo - for node and browser.",
"main": "dist/spl.js",
"browser": "dist/index.js",
Expand Down
3 changes: 2 additions & 1 deletion src/exported_functions.json
Original file line number Diff line number Diff line change
Expand Up @@ -45,5 +45,6 @@
"_sqlite3_backup_finish",
"_sqlite3_errcode",
"_gaiaOutGeoJSON",
"_gaiaOutBufferReset"
"_gaiaOutBufferReset",
"_sqlite3_stats_init"
]
4 changes: 3 additions & 1 deletion src/spl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,8 @@ const spl = function (wasmBinary=null, options: ISplOptions | {} ={}): ISPLSync
spatialite_cleanup_ex = _emspl.cwrap('spatialite_cleanup_ex', 'number', ['number']),
spatialite_shutdown = _emspl.cwrap('spatialite_shutdown', 'void', ['void']);

const sqlite3_stats_init = _emspl.cwrap('sqlite3_stats_init', 'number', ['number', 'number', 'number']);

const sqlite3_open = _emspl.cwrap('sqlite3_open', 'number', ['string', 'number']),
sqlite3_close_v2 = _emspl.cwrap('sqlite3_close_v2', 'number', ['number']),
sqlite3_prepare_v2 = _emspl.cwrap('sqlite3_prepare_v2', 'number', ['number', 'string', 'number', 'number', 'number']),
Expand Down Expand Up @@ -443,12 +445,12 @@ const spl = function (wasmBinary=null, options: ISplOptions | {} ={}): ISPLSync

tmpPtr = stackAlloc(4);
const ret = sqlite3_open(filename || ':memory:', tmpPtr);
// const ret = sqlite3_open_v2(filename || ':memory:', tmpPtr, 0x00000002 + 0x00000004 + 0x00000040, 0);
dbHandle = getValue(tmpPtr, 'i32');

if (ret === SQLITE.OK) {
cache = spatialite_alloc_connection();
spatialite_init_ex(dbHandle, cache, false);
sqlite3_stats_init(dbHandle, 0, 0);
} else {
throw new Error(sqlite3_errmsg(dbHandle));
}
Expand Down
1 change: 1 addition & 0 deletions src/sqlean
Submodule sqlean added at a52051
19 changes: 19 additions & 0 deletions test/browser.js
Original file line number Diff line number Diff line change
Expand Up @@ -184,3 +184,22 @@ tape('mounting', async t => {
);

});


// copied from src/sqlean/test/stats.sql
tape('extensions - stats', async t => {

t.plan(7);

const spl = await SPL();
const db = await spl.db();

t.true(await db.exec('select percentile(value, 25) = 25.5 from generate_series(1, 99)').get.first);
t.true(await db.exec('select round(stddev(value), 1) = 28.7 from generate_series(1, 99)').get.first);
t.true(await db.exec('select round(stddev_samp(value), 1) = 28.7 from generate_series(1, 99)').get.first);
t.true(await db.exec('select round(stddev_pop(value), 1) = 28.6 from generate_series(1, 99)').get.first);
t.true(await db.exec('select variance(value) = 825 from generate_series(1, 99)').get.first);
t.true(await db.exec('select var_samp(value) = 825 from generate_series(1, 99)').get.first);
t.true(await db.exec('select round(var_pop(value), 0) = 817 from generate_series(1, 99)').get.first);

});
Empty file added test/files/tmp/.gitkeep
Empty file.
18 changes: 18 additions & 0 deletions test/node.js
Original file line number Diff line number Diff line change
Expand Up @@ -460,3 +460,21 @@ tape('json', t => {
)

});


// copied from src/sqlean/test/stats.sql
tape('extensions - stats', t => {

t.plan(7);

const db = spatial().db()

t.true(db.exec('select percentile(value, 25) = 25.5 from generate_series(1, 99)').get.first);
t.true(db.exec('select round(stddev(value), 1) = 28.7 from generate_series(1, 99)').get.first);
t.true(db.exec('select round(stddev_samp(value), 1) = 28.7 from generate_series(1, 99)').get.first);
t.true(db.exec('select round(stddev_pop(value), 1) = 28.6 from generate_series(1, 99)').get.first);
t.true(db.exec('select variance(value) = 825 from generate_series(1, 99)').get.first);
t.true(db.exec('select var_samp(value) = 825 from generate_series(1, 99)').get.first);
t.true(db.exec('select round(var_pop(value), 0) = 817 from generate_series(1, 99)').get.first);

});

0 comments on commit dbb2581

Please sign in to comment.