Skip to content

Conversation

findolor
Copy link
Collaborator

@findolor findolor commented Sep 17, 2025

Summary

  • Remove the unused RAIN_MATH database function from sqlite-web-core package

https://chatgpt.com/codex/tasks/task_e_68ca6d3e774083339bf7bfe57af190bc

Summary by CodeRabbit

  • Refactor
    • Removed Rain Math integration and the RAIN_MATH_PROCESS function; DB now exposes only BIGINT_SUM and FLOAT_SUM.
  • Tests
    • Replaced RAIN_MATH_PROCESS tests with BIGINT_SUM- and FLOAT_SUM-focused checks covering availability, error handling, and success paths; integration tests updated.
  • Chores
    • Cleaned up related imports and public re-exports.
  • Note
    • If you relied on RAIN_MATH_PROCESS, migrate to supported functions (e.g., BIGINT_SUM).

Copy link

coderabbitai bot commented Sep 17, 2025

Walkthrough

Removed the rain_math SQLite function and its registration/tests; retained BIGINT_SUM and FLOAT_SUM aggregates. Added imports: I256 and FromStr to bigint_sum, and rain_math_float::Float to float_sum. Updated tests to stop referencing RAIN_MATH_PROCESS and to validate BIGINT_SUM error handling and FLOAT_SUM presence.

Changes

Cohort / File(s) Summary
Module registration
packages/sqlite-web-core/src/database_functions/mod.rs
Removed rain_math module export and deleted RAIN_MATH_PROCESS registration; kept BIGINT_SUM and FLOAT_SUM registrations and related imports.
BIGINT_SUM implementation
packages/sqlite-web-core/src/database_functions/bigint_sum.rs
Added use alloy::primitives::I256; and use std::str::FromStr; imports. No signature or behavior changes.
FLOAT_SUM implementation
packages/sqlite-web-core/src/database_functions/float_sum.rs
Added use rain_math_float::Float; import. No signature or behavior changes.
Removed Rain Math function
packages/sqlite-web-core/src/database_functions/rain_math.rs
Deleted entire file including rain_math_process FFI entrypoint and its wasm tests.
Tests — bigint/float presence
svelte-test/tests/database-functions/bigint-sum.test.ts, svelte-test/tests/database-functions/float-sum.test.ts
Removed runtime checks/tests that invoked RAIN_MATH_PROCESS; bigint test simplified to SELECT BIGINT_SUM("123"); float test now only verifies FLOAT_SUM.
Tests — error handling / integration
svelte-test/tests/integration/error-handling.test.ts
Replaced RAIN_MATH_PROCESS-based error tests with tests asserting BIGINT_SUM reports errors for invalid numeric strings and succeeds for valid numeric strings; database sanity checks preserved.

Sequence Diagram(s)

sequenceDiagram
  autonumber
  actor Client
  participant SQLite
  participant Extension as sqlite-web-core
  participant BIGINT_SUM
  participant FLOAT_SUM

  Client->>SQLite: Load extension
  SQLite->>Extension: register_custom_functions()
  Note right of Extension #efefef: RAIN_MATH_PROCESS removed
  Extension->>SQLite: register BIGINT_SUM (aggregate)
  Extension->>SQLite: register FLOAT_SUM (aggregate)

  Client->>SQLite: SELECT BIGINT_SUM(amount)
  SQLite->>BIGINT_SUM: xStep called per row
  BIGINT_SUM-->>SQLite: xFinal -> result or error
  SQLite-->>Client: Return result or error
Loading

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~25 minutes

Possibly related PRs

Suggested reviewers

  • hardyjosh
  • 0xgleb

Pre-merge checks and finishing touches

✅ Passed checks (3 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title Check ✅ Passed The title "Remove unused RAIN_MATH database function" is a short, single sentence that clearly and accurately summarizes the primary change in the patch (removal of the rain_math function, its registration, and related tests), so it conveys the main intent to a reviewer scanning PR history.
Docstring Coverage ✅ Passed No functions found in the changes. Docstring coverage check skipped.
✨ Finishing touches
  • 📝 Generate Docstrings
🧪 Generate unit tests
  • Create PR with unit tests
  • Post copyable unit tests in a comment
  • Commit unit tests in branch codex/remove-rain_math-database-function

📜 Recent review details

Configuration used: CodeRabbit UI

Review profile: ASSERTIVE

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 7c322a5 and 02c74a1.

📒 Files selected for processing (6)
  • packages/sqlite-web-core/src/database_functions/bigint_sum.rs (1 hunks)
  • packages/sqlite-web-core/src/database_functions/float_sum.rs (1 hunks)
  • packages/sqlite-web-core/src/database_functions/mod.rs (0 hunks)
  • svelte-test/tests/database-functions/bigint-sum.test.ts (1 hunks)
  • svelte-test/tests/database-functions/float-sum.test.ts (0 hunks)
  • svelte-test/tests/integration/error-handling.test.ts (1 hunks)
💤 Files with no reviewable changes (2)
  • svelte-test/tests/database-functions/float-sum.test.ts
  • packages/sqlite-web-core/src/database_functions/mod.rs
🧰 Additional context used
🧠 Learnings (8)
📓 Common learnings
Learnt from: CR
PR: rainlanguage/sqlite-web#0
File: CLAUDE.md:0-0
Timestamp: 2025-09-17T06:55:20.178Z
Learning: Applies to packages/sqlite-web-core/src/database_functions.rs : Integrate and expose rain.math.float-backed custom database functions for SQLite queries
📚 Learning: 2025-09-17T06:55:20.178Z
Learnt from: CR
PR: rainlanguage/sqlite-web#0
File: CLAUDE.md:0-0
Timestamp: 2025-09-17T06:55:20.178Z
Learning: Applies to packages/sqlite-web-core/src/database_functions.rs : Integrate and expose rain.math.float-backed custom database functions for SQLite queries

Applied to files:

  • packages/sqlite-web-core/src/database_functions/bigint_sum.rs
  • svelte-test/tests/database-functions/bigint-sum.test.ts
  • packages/sqlite-web-core/src/database_functions/float_sum.rs
📚 Learning: 2025-08-27T05:52:51.073Z
Learnt from: findolor
PR: rainlanguage/sqlite-web#5
File: packages/sqlite-web-core/src/database_functions/bigint_sum.rs:13-30
Timestamp: 2025-08-27T05:52:51.073Z
Learning: The ethnum::I256 type used in this codebase does not have a from_str_prefixed method available for parsing hex strings with 0x prefixes. Manual prefix detection and from_str_radix should be used instead.

Applied to files:

  • packages/sqlite-web-core/src/database_functions/bigint_sum.rs
📚 Learning: 2025-09-17T08:04:44.062Z
Learnt from: findolor
PR: rainlanguage/sqlite-web#6
File: svelte-test/tests/database-functions/float-sum.test.ts:78-86
Timestamp: 2025-09-17T08:04:44.062Z
Learning: In svelte-test/tests/database-functions/float-sum.test.ts, the schema defines `amount TEXT NOT NULL` but the NULL value handling tests attempt to insert NULL values, creating a constraint violation that prevents the tests from passing.

Applied to files:

  • svelte-test/tests/database-functions/bigint-sum.test.ts
  • svelte-test/tests/integration/error-handling.test.ts
📚 Learning: 2025-09-19T07:06:20.173Z
Learnt from: findolor
PR: rainlanguage/sqlite-web#14
File: svelte-test/tests/database-functions/float-is-zero.test.ts:39-57
Timestamp: 2025-09-19T07:06:20.173Z
Learning: PRAGMA function_list is not available in the WASM SQLite test environment used by svelte-test, so availability testing should rely on direct function calls rather than PRAGMA queries.

Applied to files:

  • svelte-test/tests/database-functions/bigint-sum.test.ts
📚 Learning: 2025-09-19T07:06:37.309Z
Learnt from: findolor
PR: rainlanguage/sqlite-web#14
File: svelte-test/tests/database-functions/float-is-zero.test.ts:60-90
Timestamp: 2025-09-19T07:06:37.309Z
Learning: For floating-point database functions in the rainlanguage/sqlite-web codebase, negative zero handling is not required and doesn't need to be tested.

Applied to files:

  • svelte-test/tests/database-functions/bigint-sum.test.ts
📚 Learning: 2025-09-17T17:44:54.855Z
Learnt from: findolor
PR: rainlanguage/sqlite-web#15
File: svelte-test/tests/integration/error-handling.test.ts:418-434
Timestamp: 2025-09-17T17:44:54.855Z
Learning: In the sqlite-web test harness, parameter binding is not available for the db.query() method, so string interpolation with manual SQL escaping using value.replace(/'/g, "''") is the necessary approach for handling special characters in test data.

Applied to files:

  • svelte-test/tests/integration/error-handling.test.ts
📚 Learning: 2025-09-17T18:08:06.145Z
Learnt from: findolor
PR: rainlanguage/sqlite-web#11
File: packages/sqlite-web-core/src/database_functions/float_negate.rs:16-17
Timestamp: 2025-09-17T18:08:06.145Z
Learning: The rain-math-float Float type implements arithmetic operations (including unary negation with the - operator) to return Result types rather than direct values, requiring .map_err(...)? for error handling of potential mathematical errors like overflow or underflow.

Applied to files:

  • packages/sqlite-web-core/src/database_functions/float_sum.rs
🧬 Code graph analysis (1)
packages/sqlite-web-core/src/database_functions/bigint_sum.rs (1)
packages/sqlite-web-core/src/database_functions/float_sum.rs (4)
  • std (69-69)
  • std (86-86)
  • std (113-116)
  • std (146-149)
⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (2)
  • GitHub Check: test
  • GitHub Check: test
🔇 Additional comments (3)
packages/sqlite-web-core/src/database_functions/float_sum.rs (1)

2-2: Import is correct and needed for this module.

Brings Float into scope explicitly; aligns with usage below. No further issues.

svelte-test/tests/integration/error-handling.test.ts (1)

407-474: LGTM: robust pivot to BIGINT_SUM with proper test-data sanitization.

  • Good coverage of invalid inputs and recovery to valid state.
  • Manual quote escaping is appropriate given the harness constraints.
packages/sqlite-web-core/src/database_functions/bigint_sum.rs (1)

2-3: I256 API present; WASM build unverified

I confirmed I256::from_hex_str and I256::checked_add exist for the alloy versions referenced; they're used in packages/sqlite-web-core/src/database_functions/bigint_sum.rs (from_hex_str @ line 34, checked_add @ line 41). Run a local WASM build for the package with alloy features enabled to confirm there are no wasm-only compilation issues.


Comment @coderabbitai help to get the list of available commands and usage tips.

@findolor findolor changed the title Restore rain-math-float lock dependency Remove unused RAIN_MATH database function Sep 17, 2025
Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 2

Caution

Some comments are outside the diff and can’t be posted inline due to platform limitations.

⚠️ Outside diff range comments (2)
packages/sqlite-web-core/src/database_functions/bigint_sum.rs (1)

25-35: Optional: support negative hex (e.g., "-0xFF")

Currently hex handling excludes signed hex. If desired, extend parsing to handle a leading '-' before 0x/0X.

Apply this diff inside add_value where the hex/decimal branch is chosen:

-        let num = if trimmed.starts_with("0x") || trimmed.starts_with("0X") {
+        let num = if let Some(rest) = trimmed.strip_prefix("-0x").or_else(|| trimmed.strip_prefix("-0X")) {
+            if rest.is_empty() {
+                return Err("Incomplete hex number: missing digits after -0x".to_string());
+            }
+            let v = I256::from_hex_str(rest)
+                .map_err(|e| format!("Failed to parse hex number '{}': {}", trimmed, e))?;
+            v.checked_neg()
+                .ok_or_else(|| format!("Integer overflow when negating {}", v))?
+        } else if trimmed.starts_with("0x") || trimmed.starts_with("0X") {
             let hex_part = &trimmed[2..];
             if hex_part.is_empty() {
                 return Err("Incomplete hex number: missing digits after 0x".to_string());
             }
             I256::from_hex_str(hex_part)
                 .map_err(|e| format!("Failed to parse hex number '{}': {}", trimmed, e))?
         } else {
             I256::from_str(trimmed)
                 .map_err(|e| format!("Failed to parse number '{}': {}", trimmed, e))?
         };

Would you like this behavior? If not in scope for this PR, we can backlog it.

svelte-test/tests/database-functions/bigint-sum.test.ts (1)

197-205: Numeric HAVING with text results — minor note

BIGINT_SUM returns text; SQLite coerces to numeric for > '0'. This is fine for sign checks; if you later depend on ordering magnitude beyond 1e15, consider explicit casts.

📜 Review details

Configuration used: CodeRabbit UI

Review profile: ASSERTIVE

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between cec3e27 and 2b50f08.

📒 Files selected for processing (5)
  • packages/sqlite-web-core/src/database_functions/bigint_sum.rs (1 hunks)
  • packages/sqlite-web-core/src/database_functions/mod.rs (1 hunks)
  • packages/sqlite-web-core/src/database_functions/rain_math.rs (0 hunks)
  • svelte-test/tests/database-functions/bigint-sum.test.ts (1 hunks)
  • svelte-test/tests/integration/error-handling.test.ts (1 hunks)
💤 Files with no reviewable changes (1)
  • packages/sqlite-web-core/src/database_functions/rain_math.rs
🧰 Additional context used
🧠 Learnings (4)
📓 Common learnings
Learnt from: CR
PR: rainlanguage/sqlite-web#0
File: CLAUDE.md:0-0
Timestamp: 2025-09-17T06:55:20.165Z
Learning: Applies to packages/sqlite-web-core/src/database_functions.rs : Integrate and expose rain.math.float-backed custom database functions for SQLite queries
📚 Learning: 2025-09-17T06:55:20.165Z
Learnt from: CR
PR: rainlanguage/sqlite-web#0
File: CLAUDE.md:0-0
Timestamp: 2025-09-17T06:55:20.165Z
Learning: Applies to packages/sqlite-web-core/src/database_functions.rs : Integrate and expose rain.math.float-backed custom database functions for SQLite queries

Applied to files:

  • packages/sqlite-web-core/src/database_functions/bigint_sum.rs
  • packages/sqlite-web-core/src/database_functions/mod.rs
  • svelte-test/tests/database-functions/bigint-sum.test.ts
📚 Learning: 2025-08-27T05:52:51.073Z
Learnt from: findolor
PR: rainlanguage/sqlite-web#5
File: packages/sqlite-web-core/src/database_functions/bigint_sum.rs:13-30
Timestamp: 2025-08-27T05:52:51.073Z
Learning: The ethnum::I256 type used in this codebase does not have a from_str_prefixed method available for parsing hex strings with 0x prefixes. Manual prefix detection and from_str_radix should be used instead.

Applied to files:

  • packages/sqlite-web-core/src/database_functions/bigint_sum.rs
📚 Learning: 2025-09-17T08:04:44.039Z
Learnt from: findolor
PR: rainlanguage/sqlite-web#6
File: svelte-test/tests/database-functions/float-sum.test.ts:78-86
Timestamp: 2025-09-17T08:04:44.039Z
Learning: In svelte-test/tests/database-functions/float-sum.test.ts, the schema defines `amount TEXT NOT NULL` but the NULL value handling tests attempt to insert NULL values, creating a constraint violation that prevents the tests from passing.

Applied to files:

  • svelte-test/tests/integration/error-handling.test.ts
  • svelte-test/tests/database-functions/bigint-sum.test.ts
⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (2)
  • GitHub Check: test
  • GitHub Check: test
🔇 Additional comments (7)
packages/sqlite-web-core/src/database_functions/bigint_sum.rs (3)

2-2: Bring FromStr into scope — LGTM

This unblocks I256::from_str usage in parsing paths.


158-392: Test coverage breadth — LGTM

Hex/decimal, overflow, edge cases, and a real-world scenario are well covered.


107-112: Confirmed — sqlite3_result_error() from xStep aborts the statement per SQLite docs

SQLite's C API documents sqlite3_result_error() as signaling/“throwing” an error and the aggregate/window-function example shows it being called from xStep, so calling it here will abort evaluation. Verify this on our WASM build (run a small repro where xStep calls sqlite3_result_error and confirm the statement is aborted).
Location: packages/sqlite-web-core/src/database_functions/bigint_sum.rs (lines 107–112).

packages/sqlite-web-core/src/database_functions/mod.rs (2)

13-56: RAIN_MATH removal path — LGTM

Registration now cleanly exposes only BIGINT_SUM and FLOAT_SUM.


22-22: Mark functions deterministic for better planning

Add SQLITE_DETERMINISTIC to the function flags to improve SQLite's query planning. rg returned no matches for SQLITE_DETERMINISTIC in the repo — verify the binding exists or import/use the numeric flag.

Applies to: packages/sqlite-web-core/src/database_functions/mod.rs (lines 22, 42)

-            SQLITE_UTF8,
+            SQLITE_UTF8 | SQLITE_DETERMINISTIC,
svelte-test/tests/database-functions/bigint-sum.test.ts (1)

56-66: Function-availability probe — LGTM

Using SELECT BIGINT_SUM("123") as a smoke test is simple and reliable across SQLite builds that may not expose PRAGMA function_list.

svelte-test/tests/integration/error-handling.test.ts (1)

407-474: Robust invalid-input coverage — LGTM

Good spread of invalid strings; post-error recovery assertion is valuable.

@@ -1,43 +1,17 @@
use alloy::primitives::{I256, U256};
use rain_math_float::Float;
use alloy::primitives::I256;
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🧹 Nitpick

Avoid cross-module aliasing; import I256 where it’s used

I256 isn’t used in this module; it’s only relied upon by bigint_sum.rs via super::*. Import it directly in the child module and drop this import to reduce coupling and silence the unused-import lint.

Apply this diff here:

-use alloy::primitives::I256;

And add in packages/sqlite-web-core/src/database_functions/bigint_sum.rs near the top:

 use super::*;
+use alloy::primitives::I256;
 use std::str::FromStr;

If you keep the parent import on purpose for visibility, confirm CI isn’t treating the unused import as a warning/error.

📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
use alloy::primitives::I256;
🤖 Prompt for AI Agents
In packages/sqlite-web-core/src/database_functions/mod.rs around line 1, remove
the unused cross-module import use alloy::primitives::I256; because I256 is not
used in this parent module; instead add a direct import in
packages/sqlite-web-core/src/database_functions/bigint_sum.rs near the top
(e.g., use alloy::primitives::I256;) so the child module declares its own
dependency and eliminates coupling; if you intentionally left the parent import
for visibility, confirm CI does not treat unused imports as warnings/errors and
keep it only after verifying CI settings.

Comment on lines +418 to +434
for (const value of invalidValues) {
const sanitizedValue = value.replace(/'/g, "''");

await db.query('DELETE FROM custom_function_test');
await db.query(`
INSERT INTO custom_function_test (amount) VALUES ('${sanitizedValue}')
`);

const result = await db.query('SELECT BIGINT_SUM(amount) as total FROM custom_function_test');

expect(result.error).toBeDefined();

const errorMessage =
result.error?.msg || result.error?.readableMsg || String(result.error);

expect(errorMessage.toLowerCase()).toMatch(/failed|invalid|empty|incomplete/);
}
Copy link

@coderabbitai coderabbitai bot Sep 17, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🧹 Nitpick

Prefer parameter binding over string interpolation (tests)

Even in tests, replacing manual quote-doubling with bound parameters avoids edge cases and improves readability.

Example:

await db.query(
  'INSERT INTO custom_function_test (amount) VALUES (?1)',
  ['not_a_number']
);

Does db.query support bound parameters in this test harness? If yes, I can refactor the tests.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is not possible

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@findolor, understood! I acknowledge that parameter binding isn't available in your test harness. The manual quote escaping with value.replace(/'/g, "''") is the appropriate solution given that constraint.

Thanks for clarifying this limitation - the current string interpolation approach is perfectly valid when parameter binding isn't supported by the test infrastructure.


✏️ Learnings added
Learnt from: findolor
PR: rainlanguage/sqlite-web#15
File: svelte-test/tests/integration/error-handling.test.ts:418-434
Timestamp: 2025-09-17T17:44:54.832Z
Learning: In the sqlite-web test harness, parameter binding is not available for the db.query() method, so string interpolation with manual SQL escaping using value.replace(/'/g, "''") is the necessary approach for handling special characters in test data.

Note: Learnings are effective only in the context of similar code segments. To apply general coding guidelines, please configure review instructions. You can manage existing learnings in the app.


🧠 Learnings used
Learnt from: findolor
PR: rainlanguage/sqlite-web#6
File: svelte-test/tests/database-functions/float-sum.test.ts:78-86
Timestamp: 2025-09-17T08:04:44.062Z
Learning: In svelte-test/tests/database-functions/float-sum.test.ts, the schema defines `amount TEXT NOT NULL` but the NULL value handling tests attempt to insert NULL values, creating a constraint violation that prevents the tests from passing.

Learnt from: CR
PR: rainlanguage/sqlite-web#0
File: CLAUDE.md:0-0
Timestamp: 2025-09-17T06:55:20.178Z
Learning: Applies to packages/sqlite-web-core/src/database_functions.rs : Integrate and expose rain.math.float-backed custom database functions for SQLite queries

@findolor findolor merged commit d8c4699 into main Sep 23, 2025
3 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants