From 74c67a38126bac2b991753ab214f06407ef81ffc Mon Sep 17 00:00:00 2001 From: Yuri Astrakhan Date: Sat, 27 Jan 2024 21:48:54 -0500 Subject: [PATCH] Use ORDERBY in calc_agg_tiles_hash - SQLite v3.44 (#1156) Require SQLite v3.44+ ORDER BY clause inside aggregate function instead of the windowing one - might solve out of memory issues reported by users - see #1154 --- Cargo.toml | 2 +- mbtiles/src/validation.rs | 14 +++++--------- 2 files changed, 6 insertions(+), 10 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 37a31fca..deb53b7c 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -72,7 +72,7 @@ serde_with = "3" serde_yaml = "0.9" size_format = "1.0.2" spreet = { version = "0.11", default-features = false } -sqlite-hashes = { version = "0.6", default-features = false, features = ["md5", "window", "hex"] } +sqlite-hashes = { version = "0.6", default-features = false, features = ["md5", "window", "hex"] } # window forces libsqlite3-sys to bundle sqlite3. We require v3.44.0 or newer. sqlx = { version = "0.7", features = ["sqlite", "runtime-tokio"] } subst = { version = "0.3", features = ["yaml"] } thiserror = "1" diff --git a/mbtiles/src/validation.rs b/mbtiles/src/validation.rs index 9680a980..50509a81 100644 --- a/mbtiles/src/validation.rs +++ b/mbtiles/src/validation.rs @@ -470,22 +470,18 @@ where // `md5` functions will fail if the value is not text/blob/null // // Note that ORDER BY controls the output ordering, which is important for the hash value, - // and having it at the top level would not order values properly. + // and we must use ORDER BY as a parameter to the aggregate function itself (available since SQLite 3.44.0) // See https://sqlite.org/forum/forumpost/228bb96e12a746ce " SELECT coalesce( - (SELECT md5_concat_hex( + md5_concat_hex( cast(zoom_level AS text), cast(tile_column AS text), cast(tile_row AS text), tile_data - ) - OVER (ORDER BY zoom_level, tile_column, tile_row ROWS - BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING) - FROM tiles - LIMIT 1), - md5_hex('') -); + ORDER BY zoom_level, tile_column, tile_row), + md5_hex('')) +FROM tiles; ", ); Ok(query.fetch_one(conn).await?.get::(0))