From 7c0ed986c1fdb06397f84bd38bc723c2a5deb852 Mon Sep 17 00:00:00 2001 From: vrustx <279631638@qq.com> Date: Sat, 30 May 2026 23:22:44 +0800 Subject: [PATCH] fix: enable SQLite math functions (#553) --- .cargo/config.toml | 4 ++++ crates/dbx-core/src/db/sqlite.rs | 26 ++++++++++++++++++++++++++ 2 files changed, 30 insertions(+) diff --git a/.cargo/config.toml b/.cargo/config.toml index 6c00888368..92fc9a760d 100644 --- a/.cargo/config.toml +++ b/.cargo/config.toml @@ -1,2 +1,6 @@ +[env] +# Enable SQLite's built-in math functions for rusqlite's bundled libsqlite3. +LIBSQLITE3_FLAGS = "SQLITE_ENABLE_MATH_FUNCTIONS" + [target.x86_64-pc-windows-msvc] linker = "lld-link" diff --git a/crates/dbx-core/src/db/sqlite.rs b/crates/dbx-core/src/db/sqlite.rs index 5d28e94194..57b97c5ab3 100644 --- a/crates/dbx-core/src/db/sqlite.rs +++ b/crates/dbx-core/src/db/sqlite.rs @@ -178,6 +178,32 @@ mod tests { assert_eq!(result.rows[0][0], serde_json::json!("yes")); } + #[tokio::test] + async fn bundled_sqlite_math_functions_are_available() { + let pool = connect_path(":memory:").await.expect("connect in-memory SQLite"); + + let floor_result = + execute_query(&pool, "WITH test(x) AS (VALUES (1.1), (1.2), (1.3)) SELECT FLOOR(x) FROM test") + .await + .expect("FLOOR() should be available"); + + assert_eq!(floor_result.rows.len(), 3); + for row in floor_result.rows { + assert_eq!(row[0].as_f64(), Some(1.0)); + } + + let result = execute_query(&pool, "SELECT ACOS(1.0), ACOSH(1.0), ASIN(0.0), CEIL(1.2), PI()") + .await + .expect("SQLite math functions should be available"); + + assert_eq!(result.rows[0][0].as_f64(), Some(0.0)); + assert_eq!(result.rows[0][1].as_f64(), Some(0.0)); + assert_eq!(result.rows[0][2].as_f64(), Some(0.0)); + assert_eq!(result.rows[0][3].as_f64(), Some(2.0)); + let pi = result.rows[0][4].as_f64().expect("PI() returns a real value"); + assert!((3.14..3.15).contains(&pi)); + } + #[tokio::test] async fn substring_rewrite_works_in_direct_query() { let pool = connect_path(":memory:").await.expect("connect in-memory SQLite");