Skip to content

Commit

Permalink
Use ORDERBY in calc_agg_tiles_hash - SQLite v3.44 (#1156)
Browse files Browse the repository at this point in the history
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
  • Loading branch information
nyurik authored Jan 28, 2024
1 parent c4af48b commit 74c67a3
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 10 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
14 changes: 5 additions & 9 deletions mbtiles/src/validation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::<String, _>(0))
Expand Down

0 comments on commit 74c67a3

Please sign in to comment.