Skip to content

Conversation

findolor
Copy link
Collaborator

@findolor findolor commented Sep 4, 2025

Caution

Chained PR. Do not merge before #6

Motivation

See issue: #10

Solution

Checks

By submitting this for review, I'm confirming I've done the following:

  • made this PR as small as possible
  • unit-tested any new functionality
  • linked any relevant issues or PRs
  • included screenshots (if this involves a front-end change)

fix #10

Summary by CodeRabbit

  • New Features

    • Added SQLite scalar function FLOAT_NEGATE to return the negation of a hex-encoded Rain Float. Supports 0x-prefixed or unprefixed inputs, preserves precision, returns 0x-prefixed output, passes through NULL, and surfaces clear errors for invalid inputs.
  • Tests

    • Introduced a comprehensive test suite for FLOAT_NEGATE (availability, correctness, double-negation, whitespace/mixed-case handling, NULL/error cases).
    • Refactored FLOAT_SUM tests to use shared fixtures for encoding/decoding and input variations.
  • Chores

    • Added reusable float hex utilities for tests.
    • Updated local package dependency to alpha.1.

@findolor findolor self-assigned this Sep 4, 2025
Copy link

coderabbitai bot commented Sep 4, 2025

Walkthrough

Introduces a new SQLite scalar function FLOAT_NEGATE backed by Rust, registers it in the core module, and adds comprehensive tests. Updates float-sum tests to use shared fixtures. Adds shared test utilities for float hex handling. Bumps a local dependency tarball reference.

Changes

Cohort / File(s) Summary of changes
SQLite FLOAT_NEGATE core
packages/sqlite-web-core/src/database_functions/float_negate.rs, packages/sqlite-web-core/src/database_functions/mod.rs
Add FLOAT_NEGATE implementation and helper; register function with SQLite; handle errors; adjust test config in mod.rs.
Float sum test tweaks (Rust)
packages/sqlite-web-core/src/database_functions/float_sum.rs
Update tests to generate hex via Float parsing; maintain existing assertions and behavior.
New FLOAT_NEGATE tests (Svelte)
svelte-test/tests/database-functions/float-negate.test.ts
Add Vitest suite validating availability, correctness, NULL/error handling, and casing/prefix scenarios. Uses fixtures.
Refactor FLOAT_SUM tests to fixtures (Svelte)
svelte-test/tests/database-functions/float-sum.test.ts
Replace local helpers with shared fixtures; update imports and data construction; preserve scenarios and expectations.
Shared float hex fixtures (Svelte)
svelte-test/tests/fixtures/float-utils.ts
Add utilities: PrefixedHex, encode/decode, withoutPrefix, toMixedCase, createFloatHexMap.
Dependency bump (Svelte)
svelte-test/package.json
Update local tarball path for @rainlanguage/sqlite-web from alpha.0 to alpha.1.

Sequence Diagram(s)

sequenceDiagram
  autonumber
  actor Client
  participant SQLite
  participant "float_negate (C ABI)" as ABI
  participant "float_negate_hex_to_hex" as Helper
  participant "Float lib" as Float

  Client->>SQLite: SELECT FLOAT_NEGATE(arg)
  SQLite->>ABI: Invoke xFunc (argc=1, argv[0])
  alt arg is NULL
    ABI-->>SQLite: return NULL
  else arg is text
    ABI->>Helper: negate hex string
    Helper->>Float: from_hex(input)
    Float-->>Helper: Float value
    Helper->>Float: negate (unary -)
    Helper->>Float: as_hex()
    Float-->>Helper: "0x...hex"
    Helper-->>ABI: negated hex
    ABI-->>SQLite: result text
  else invalid input
    ABI-->>SQLite: SQLITE error with message
  end
  SQLite-->>Client: result/error
Loading

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~25 minutes

Suggested reviewers

  • 0xgleb
  • hardyjosh

Pre-merge checks and finishing touches

✅ Passed checks (5 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title Check ✅ Passed The title "Add FLOAT_NEGATE function" is short, a single sentence, and accurately summarizes the primary change in the changeset (addition of a FLOAT_NEGATE scalar function, its C ABI entrypoint, registration, and tests), so it meets the stated title criteria.
Linked Issues Check ✅ Passed The changes satisfy issue #10: a new float_negate.rs implements parsing a Float from hex, negation while preserving precision, and formatting back to hex; a C ABI xFunc (float_negate) and registration in mod.rs were added, and comprehensive Rust and JS tests cover correctness, NULL/error handling, and precision, which meets the linked issue's objective to provide a Float negation utility.
Out of Scope Changes Check ✅ Passed Most changes are directly related to adding FLOAT_NEGATE and its tests; additional modifications are test-related (refactors to float_sum.rs tests, new JS test fixtures in svelte-test, and a svelte-test package.json dependency bump) and the removal of wasm_bindgen_test_configure!(run_in_browser) from the tests module, all of which appear to be testing or packaging adjustments rather than unrelated production logic.
Docstring Coverage ✅ Passed No functions found in the changes. Docstring coverage check skipped.
✨ Finishing touches
🧪 Generate unit tests
  • Create PR with unit tests
  • Post copyable unit tests in a comment
  • Commit unit tests in branch float-negate

📜 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 b5b88f3 and e695e7f.

⛔ Files ignored due to path filters (2)
  • Cargo.lock is excluded by !**/*.lock
  • svelte-test/package-lock.json is excluded by !**/package-lock.json
📒 Files selected for processing (1)
  • svelte-test/package.json (1 hunks)
🧰 Additional context used
📓 Path-based instructions (1)
svelte-test/package.json

📄 CodeRabbit inference engine (CLAUDE.md)

In the Svelte test app, depend on the sqlite-web package via the locally packed tarball produced by npm pack

Files:

  • svelte-test/package.json
🧠 Learnings (3)
📓 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
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: findolor
PR: rainlanguage/sqlite-web#11
File: svelte-test/tests/database-functions/float-negate.test.ts:64-72
Timestamp: 2025-09-17T17:09:58.740Z
Learning: FLOAT_SUM functionality is available and working in the codebase, so tests can rely on it without needing availability checks or gating logic.
📚 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 svelte-test/package.json : In the Svelte test app, depend on the sqlite-web package via the locally packed tarball produced by npm pack

Applied to files:

  • svelte-test/package.json
📚 Learning: 2025-08-27T05:59:04.254Z
Learnt from: findolor
PR: rainlanguage/sqlite-web#5
File: svelte-test/tests/database-functions/bigint-sum.test.ts:2-7
Timestamp: 2025-08-27T05:59:04.254Z
Learning: In the sqlite-web repository, the user prefers to keep .js extensions when importing TypeScript files in test files, even in Vitest environments. This is acceptable and should not be flagged as an issue.

Applied to files:

  • svelte-test/package.json
⏰ 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

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

@findolor findolor linked an issue Sep 15, 2025 that may be closed by this pull request
Base automatically changed from float-sum to main September 17, 2025 16:50
@findolor
Copy link
Collaborator Author

@coderabbitai review

Copy link

coderabbitai bot commented Sep 17, 2025

✅ Actions performed

Review triggered.

Note: CodeRabbit is an incremental review system and does not re-review already reviewed commits. This command is applicable only when automatic reviews are paused.

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: 10

Caution

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

⚠️ Outside diff range comments (3)
svelte-test/tests/database-functions/float-sum.test.ts (2)

48-55: Fix schema: NOT NULL blocks your NULL‑handling tests.

The table defines amount TEXT NOT NULL but later inserts NULL, causing INSERT failures (previously noted in learnings). Make amount nullable for this test table.

Apply this diff:

-        CREATE TABLE float_test (
+        CREATE TABLE float_test (
           id INTEGER PRIMARY KEY AUTOINCREMENT,
-          amount TEXT NOT NULL,
+          amount TEXT,
           category TEXT,
           description TEXT
         )

547-561: Make float_test.amount nullable — NULL-handling tests insert NULL and currently violate NOT NULL

svelte-test/tests/database-functions/float-sum.test.ts: CREATE TABLE float_test defines amount TEXT NOT NULL (around lines 48–50); change to amount TEXT (remove NOT NULL) and re-run the NULL Value Handling block:
pnpm vitest run svelte-test/tests/database-functions/float-sum.test.ts -t "NULL Value Handling"

packages/sqlite-web-core/src/database_functions/float_negate.rs (1)

66-120: Add a test for hex without the 0x prefix (parity with FLOAT_SUM).

FLOAT_SUM accepts no‑prefix hex; FLOAT_NEGATE should, too. Add a positive test to lock this in.

Apply this diff:

@@
     fn test_float_negate_hex_to_hex_high_precision() {
@@
     }
 
+    #[wasm_bindgen_test]
+    fn test_float_negate_hex_to_hex_without_prefix() {
+        let pos_hex = Float::parse("42.125".to_string()).unwrap().as_hex();
+        let no_prefix = pos_hex.trim_start_matches("0x");
+        let expected = Float::parse("-42.125".to_string()).unwrap().as_hex();
+        let out = float_negate_hex_to_hex(no_prefix).unwrap();
+        assert_eq!(out, expected);
+    }
+
     #[wasm_bindgen_test]
     fn test_float_negate_hex_to_hex_whitespace() {
📜 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 190c059.

📒 Files selected for processing (6)
  • packages/sqlite-web-core/src/database_functions/float_negate.rs (1 hunks)
  • packages/sqlite-web-core/src/database_functions/float_sum.rs (2 hunks)
  • packages/sqlite-web-core/src/database_functions/mod.rs (2 hunks)
  • svelte-test/tests/database-functions/float-negate.test.ts (1 hunks)
  • svelte-test/tests/database-functions/float-sum.test.ts (2 hunks)
  • svelte-test/tests/fixtures/float-utils.ts (1 hunks)
🧰 Additional context used
🧠 Learnings (3)
📓 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
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.
📚 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/database-functions/float-negate.test.ts
  • packages/sqlite-web-core/src/database_functions/float_sum.rs
  • svelte-test/tests/database-functions/float-sum.test.ts
📚 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:

  • svelte-test/tests/database-functions/float-negate.test.ts
  • packages/sqlite-web-core/src/database_functions/float_sum.rs
  • packages/sqlite-web-core/src/database_functions/mod.rs
  • packages/sqlite-web-core/src/database_functions/float_negate.rs
  • svelte-test/tests/database-functions/float-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 (6)
packages/sqlite-web-core/src/database_functions/float_sum.rs (2)

189-201: Good move: derive hex from Float in tests (less brittle).

Generating hex via Float::parse(...).as_hex() avoids stale literals and keeps tests aligned with the lib’s encoding. Looks good.


210-221: Uppercase 0X rejection covered well.

Building the inputs dynamically via replacen("0x","0X",1) keeps this negative case future‑proof.

svelte-test/tests/fixtures/float-utils.ts (2)

5-11: Solid error wrapping and typed helpers.

Clear surfacing of parse/fromHex/format failures and strong typing on hex inputs/outputs. LGTM.

Also applies to: 20-31


37-48: Mixed‑case generator behaves as intended.

Alternating case only on a–f preserves the 0x prefix and exercises parser tolerance. Good utility.

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

11-11: Module wiring for FLOAT_NEGATE looks correct.

mod float_negate; and use float_negate::*; are in the right place relative to other registrations.

Also applies to: 16-16

packages/sqlite-web-core/src/database_functions/float_negate.rs (1)

20-64: FFI wrapper is correct and memory‑safe under SQLITE_TRANSIENT.

Argument validation, NULL propagation, CString handling, and result wiring look good.

Please confirm that Float::from_hex intentionally accepts inputs without the 0x prefix (parity with FLOAT_SUM). If so, consider documenting this in the function comment.

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: 6

♻️ Duplicate comments (4)
packages/sqlite-web-core/src/database_functions/float_negate.rs (1)

8-10: Empty-input error handling aligned with FLOAT_SUM (good).

Explicit trim + empty check matches prior guidance and keeps UX consistent.

svelte-test/tests/database-functions/float-negate.test.ts (2)

8-13: Extension-less import is fine per repo conventions.

Acknowledging maintainer’s preference; no action needed.


141-146: Assert success before NULL parsing.

Make the failure mode explicit.

   it("should return NULL when input is NULL", async () => {
-      const res = await db.query("SELECT FLOAT_NEGATE(NULL) as neg");
+      const res = await db.query("SELECT FLOAT_NEGATE(NULL) as neg");
+      expect(res.error).toBeFalsy();
       const data = JSON.parse(res.value || "[]");
       expect(data[0].neg).toBeNull();
   });
packages/sqlite-web-core/src/database_functions/mod.rs (1)

83-101: Good: registered as deterministic and innocuous; improve diagnostics on failure.

Include rc and sqlite3_errmsg(db) to speed up triage when registration fails.

-    if ret != SQLITE_OK {
-        return Err("Failed to register FLOAT_NEGATE function".to_string());
-    }
+    if ret != SQLITE_OK {
+        let msg = unsafe { CStr::from_ptr(sqlite3_errmsg(db)) }
+            .to_string_lossy()
+            .into_owned();
+        return Err(format!("Failed to register FLOAT_NEGATE (rc={ret}): {msg}"));
+    }
📜 Review details

Configuration used: CodeRabbit UI

Review profile: ASSERTIVE

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 190c059 and f5c62e7.

⛔ Files ignored due to path filters (1)
  • svelte-test/tests/database-functions/__screenshots__/float-negate.test.ts/FLOAT-NEGATE-Database-Function-Function-Availability-should-have-FLOAT-NEGATE-function-available-1.png is excluded by !**/*.png
📒 Files selected for processing (3)
  • packages/sqlite-web-core/src/database_functions/float_negate.rs (1 hunks)
  • packages/sqlite-web-core/src/database_functions/mod.rs (2 hunks)
  • svelte-test/tests/database-functions/float-negate.test.ts (1 hunks)
🧰 Additional context used
🧠 Learnings (7)
📓 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
Learnt from: findolor
PR: rainlanguage/sqlite-web#11
File: svelte-test/tests/database-functions/float-negate.test.ts:64-72
Timestamp: 2025-09-17T17:09:58.729Z
Learning: FLOAT_SUM functionality is available and working in the codebase, so tests can rely on it without needing availability checks or gating logic.
📚 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/float_negate.rs
  • svelte-test/tests/database-functions/float-negate.test.ts
  • packages/sqlite-web-core/src/database_functions/mod.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/database-functions/float-negate.test.ts
📚 Learning: 2025-09-17T17:17:41.817Z
Learnt from: findolor
PR: rainlanguage/sqlite-web#11
File: svelte-test/tests/database-functions/float-negate.test.ts:105-119
Timestamp: 2025-09-17T17:17:41.817Z
Learning: The toMixedCase function in svelte-test/tests/fixtures/float-utils.ts only modifies [a-f] characters, preserving the 'x' in '0x' prefixes, so it won't create conflicts with tests that reject '0X' prefixes.

Applied to files:

  • svelte-test/tests/database-functions/float-negate.test.ts
📚 Learning: 2025-09-17T17:17:41.817Z
Learnt from: findolor
PR: rainlanguage/sqlite-web#11
File: svelte-test/tests/database-functions/float-negate.test.ts:105-119
Timestamp: 2025-09-17T17:17:41.817Z
Learning: The toMixedCase function in svelte-test/tests/fixtures/float-utils.ts only modifies characters matching /[a-f]/ (hex digits a-f), preserving the 'x' in '0x' prefixes and all other characters unchanged, so it won't create conflicts with tests that reject '0X' prefixes.

Applied to files:

  • svelte-test/tests/database-functions/float-negate.test.ts
📚 Learning: 2025-09-17T17:09:58.729Z
Learnt from: findolor
PR: rainlanguage/sqlite-web#11
File: svelte-test/tests/database-functions/float-negate.test.ts:64-72
Timestamp: 2025-09-17T17:09:58.729Z
Learning: FLOAT_SUM functionality is available and working in the codebase, so tests can rely on it without needing availability checks or gating logic.

Applied to files:

  • svelte-test/tests/database-functions/float-negate.test.ts
📚 Learning: 2025-08-27T05:59:04.254Z
Learnt from: findolor
PR: rainlanguage/sqlite-web#5
File: svelte-test/tests/database-functions/bigint-sum.test.ts:2-7
Timestamp: 2025-08-27T05:59:04.254Z
Learning: In the sqlite-web repository, the user prefers to keep .js extensions when importing TypeScript files in test files, even in Vitest environments. This is acceptable and should not be flagged as an issue.

Applied to files:

  • svelte-test/tests/database-functions/float-negate.test.ts
🧬 Code graph analysis (3)
packages/sqlite-web-core/src/database_functions/float_negate.rs (1)
packages/sqlite-web-core/src/database_functions/float_sum.rs (4)
  • std (65-65)
  • std (82-82)
  • std (109-112)
  • std (142-145)
svelte-test/tests/database-functions/float-negate.test.ts (2)
svelte-test/tests/fixtures/float-utils.ts (4)
  • createFloatHexMap (50-56)
  • withoutPrefix (33-35)
  • decodeFloatHex (20-31)
  • toMixedCase (37-48)
svelte-test/tests/fixtures/test-helpers.ts (3)
  • PerformanceTracker (138-168)
  • createTestDatabase (6-22)
  • cleanupDatabase (173-201)
packages/sqlite-web-core/src/database_functions/mod.rs (1)
packages/sqlite-web-core/src/database_functions/float_negate.rs (2)
  • float_negate (24-68)
  • std (54-57)
⏰ 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 (6)
packages/sqlite-web-core/src/database_functions/float_negate.rs (4)

12-21: Parse → negate → as_hex flow is correct and lossless.

Straight-through mapping with precise errors; no precision round-trips.


24-36: Argument count guard is correct; message is clear.

Early return avoids undefined argv reads.


118-122: Nice coverage for invalid inputs.

Catches 0X, empty, and non-hex; mirrors runtime behavior well.


8-10: Confirmed: error-string matches FLOAT_SUM

Exact literal found in float_sum.rs:18 and float_negate.rs:9 — "Empty string is not a valid hex number".

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

11-17: Module wiring looks correct.

Re-exports and use statements are coherent; no leakage beyond intended surface.

svelte-test/tests/database-functions/float-negate.test.ts (1)

103-118: Mixed‑case test is valid; prefix remains ‘0x’.

toMixedCase preserves the ‘x’, so this won’t collide with the explicit 0X‑rejection test.

Comment on lines 49 to 58
if let Ok(result_cstr) = CString::new(result_hex) {
sqlite3_result_text(
context,
result_cstr.as_ptr(),
result_cstr.as_bytes().len() as c_int,
Some(std::mem::transmute::<
isize,
unsafe extern "C" fn(*mut std::ffi::c_void),
>(-1isize)), // SQLITE_TRANSIENT
);
Copy link

Choose a reason for hiding this comment

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

🧹 Nitpick

Correct use of SQLITE_TRANSIENT.

Casting -1 to destructor matches pattern used elsewhere (FLOAT_SUM) and avoids lifetime issues.

You could centralize this as a const to reduce repetition:

const SQLITE_TRANSIENT: unsafe extern "C" fn(*mut std::ffi::c_void) =
    unsafe { std::mem::transmute::<isize, _>(-1) };
🤖 Prompt for AI Agents
In packages/sqlite-web-core/src/database_functions/float_negate.rs around lines
49 to 58, the call to sqlite3_result_text currently transmutes -1isize inline to
produce the SQLITE_TRANSIENT destructor; replace the inline transmute with a
shared const binding for SQLITE_TRANSIENT (typed as unsafe extern "C" fn(*mut
std::ffi::c_void)) defined near the top of the module and use that const in
sqlite3_result_text to avoid repeated unsafe transmute usage and clarify intent.

Comment on lines +91 to +97
#[wasm_bindgen_test]
fn test_float_negate_hex_to_hex_zero() {
let zero_hex = Float::parse("0".to_string()).unwrap().as_hex();
let expected_zero_hex = Float::parse("0".to_string()).unwrap().as_hex();
let out = float_negate_hex_to_hex(&zero_hex).unwrap();
assert_eq!(out, expected_zero_hex);
}
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

Add a test for signed zero behavior.

If Float distinguishes -0 and 0, assert expected normalization.

     fn test_float_negate_hex_to_hex_zero() {
         let zero_hex = Float::parse("0".to_string()).unwrap().as_hex();
         let expected_zero_hex = Float::parse("0".to_string()).unwrap().as_hex();
         let out = float_negate_hex_to_hex(&zero_hex).unwrap();
         assert_eq!(out, expected_zero_hex);
     }
+    #[wasm_bindgen_test]
+    fn test_float_negate_hex_to_hex_negative_zero() {
+        let neg_zero_hex = Float::parse("-0".to_string()).unwrap().as_hex();
+        let out = float_negate_hex_to_hex(&neg_zero_hex).unwrap();
+        // Expect normalization to +0; adjust if library preserves -0
+        let zero_hex = Float::parse("0".to_string()).unwrap().as_hex();
+        assert_eq!(out, zero_hex);
+    }
📝 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
#[wasm_bindgen_test]
fn test_float_negate_hex_to_hex_zero() {
let zero_hex = Float::parse("0".to_string()).unwrap().as_hex();
let expected_zero_hex = Float::parse("0".to_string()).unwrap().as_hex();
let out = float_negate_hex_to_hex(&zero_hex).unwrap();
assert_eq!(out, expected_zero_hex);
}
#[wasm_bindgen_test]
fn test_float_negate_hex_to_hex_zero() {
let zero_hex = Float::parse("0".to_string()).unwrap().as_hex();
let expected_zero_hex = Float::parse("0".to_string()).unwrap().as_hex();
let out = float_negate_hex_to_hex(&zero_hex).unwrap();
assert_eq!(out, expected_zero_hex);
}
#[wasm_bindgen_test]
fn test_float_negate_hex_to_hex_negative_zero() {
let neg_zero_hex = Float::parse("-0".to_string()).unwrap().as_hex();
let out = float_negate_hex_to_hex(&neg_zero_hex).unwrap();
// Expect normalization to +0; adjust if library preserves -0
let zero_hex = Float::parse("0".to_string()).unwrap().as_hex();
assert_eq!(out, zero_hex);
}
🤖 Prompt for AI Agents
In packages/sqlite-web-core/src/database_functions/float_negate.rs around lines
91 to 97, add a test covering signed zero: parse "-0" and "0" to hex, call
float_negate_hex_to_hex on each, and assert the output matches the expected
normalized representation (decide whether negating -0 yields "0" or "-0" based
on Float's normalization and assert that explicitly); ensure the test verifies
both parsing and negation round-trips so signed-zero behavior is asserted.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

no need to test this out. out of scope

Copy link

Choose a reason for hiding this comment

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

@findolor Understood! No problem at all - I'll skip this test suggestion since it's out of scope for this PR. The current test coverage looks good for the main functionality.


🧠 Learnings used
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

Learnt from: findolor
PR: rainlanguage/sqlite-web#11
File: svelte-test/tests/database-functions/float-negate.test.ts:105-119
Timestamp: 2025-09-17T17:17:41.817Z
Learning: The toMixedCase function in svelte-test/tests/fixtures/float-utils.ts only modifies characters matching /[a-f]/ (hex digits a-f), preserving the 'x' in '0x' prefixes and all other characters unchanged, so it won't create conflicts with tests that reject '0X' prefixes.

Learnt from: findolor
PR: rainlanguage/sqlite-web#11
File: svelte-test/tests/database-functions/float-negate.test.ts:105-119
Timestamp: 2025-09-17T17:17:41.817Z
Learning: The toMixedCase function in svelte-test/tests/fixtures/float-utils.ts only modifies [a-f] characters, preserving the 'x' in '0x' prefixes, so it won't create conflicts with tests that reject '0X' prefixes.

Copy link

Choose a reason for hiding this comment

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

Float doesn't distinguish between -0 and 0 as far as I remember

Copy link

Choose a reason for hiding this comment

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

Seems like the humans are having a chat. I'll hop back into my burrow for now. If you need me again, just tag @coderabbitai in a new comment, and I'll come hopping out!

Comment on lines +39 to +45
try {
const pragmaResult = await db.query("PRAGMA function_list");
const functions = JSON.parse(pragmaResult.value || "[]");
const entry = functions.find((f: any) => f.name === "FLOAT_NEGATE");
expect(entry).toBeDefined();
} catch (e) {}

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

Don’t swallow PRAGMA errors.

Fail fast or assert success to make availability issues actionable.

-      try {
-        const pragmaResult = await db.query("PRAGMA function_list");
-        const functions = JSON.parse(pragmaResult.value || "[]");
-        const entry = functions.find((f: any) => f.name === "FLOAT_NEGATE");
-        expect(entry).toBeDefined();
-      } catch (e) {}
+      const pragmaResult = await db.query("PRAGMA function_list");
+      expect(pragmaResult.error).toBeFalsy();
+      const functions = JSON.parse(pragmaResult.value || "[]");
+      const entry = functions.find((f: any) => f.name === "FLOAT_NEGATE");
+      expect(entry).toBeDefined();
📝 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
try {
const pragmaResult = await db.query("PRAGMA function_list");
const functions = JSON.parse(pragmaResult.value || "[]");
const entry = functions.find((f: any) => f.name === "FLOAT_NEGATE");
expect(entry).toBeDefined();
} catch (e) {}
const pragmaResult = await db.query("PRAGMA function_list");
expect(pragmaResult.error).toBeFalsy();
const functions = JSON.parse(pragmaResult.value || "[]");
const entry = functions.find((f: any) => f.name === "FLOAT_NEGATE");
expect(entry).toBeDefined();
🤖 Prompt for AI Agents
In svelte-test/tests/database-functions/float-negate.test.ts around lines 39 to
45, the try/catch block currently swallows any error from the PRAGMA query which
hides failures; remove the empty catch and instead let the error propagate or
explicitly fail the test (e.g., await the db.query without a catch or catch and
rethrow/assert with expect.fail or throw new Error) so PRAGMA errors cause the
test to fail and include the error message; also assert that pragmaResult.value
is defined before JSON.parse to avoid masking parse errors.

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 fine

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'll respect your decision to keep the current error handling approach in the test.

  (\   /)
 ( ._.)
o_(")(")

🧠 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.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.

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

Learnt from: findolor
PR: rainlanguage/sqlite-web#11
File: svelte-test/tests/database-functions/float-negate.test.ts:64-72
Timestamp: 2025-09-17T17:09:58.729Z
Learning: FLOAT_SUM functionality is available and working in the codebase, so tests can rely on it without needing availability checks or gating logic.

Learnt from: findolor
PR: rainlanguage/sqlite-web#11
File: svelte-test/tests/database-functions/float-negate.test.ts:105-119
Timestamp: 2025-09-17T17:17:41.817Z
Learning: The toMixedCase function in svelte-test/tests/fixtures/float-utils.ts only modifies [a-f] characters, preserving the 'x' in '0x' prefixes, so it won't create conflicts with tests that reject '0X' prefixes.

Learnt from: findolor
PR: rainlanguage/sqlite-web#11
File: svelte-test/tests/database-functions/float-negate.test.ts:105-119
Timestamp: 2025-09-17T17:17:41.817Z
Learning: The toMixedCase function in svelte-test/tests/fixtures/float-utils.ts only modifies characters matching /[a-f]/ (hex digits a-f), preserving the 'x' in '0x' prefixes and all other characters unchanged, so it won't create conflicts with tests that reject '0X' prefixes.

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

♻️ Duplicate comments (1)
svelte-test/tests/database-functions/float-negate.test.ts (1)

36-41: Swallowed PRAGMA errors (not re‑raising).

This was discussed earlier and accepted; leaving as‑is.

📜 Review details

Configuration used: CodeRabbit UI

Review profile: ASSERTIVE

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between f5c62e7 and fb838e9.

📒 Files selected for processing (2)
  • packages/sqlite-web-core/src/database_functions/float_negate.rs (1 hunks)
  • svelte-test/tests/database-functions/float-negate.test.ts (1 hunks)
🧰 Additional context used
🧠 Learnings (9)
📓 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-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/float-negate.test.ts
  • packages/sqlite-web-core/src/database_functions/float_negate.rs
📚 Learning: 2025-09-17T17:17:41.827Z
Learnt from: findolor
PR: rainlanguage/sqlite-web#11
File: svelte-test/tests/database-functions/float-negate.test.ts:105-119
Timestamp: 2025-09-17T17:17:41.827Z
Learning: The toMixedCase function in svelte-test/tests/fixtures/float-utils.ts only modifies [a-f] characters, preserving the 'x' in '0x' prefixes, so it won't create conflicts with tests that reject '0X' prefixes.

Applied to files:

  • svelte-test/tests/database-functions/float-negate.test.ts
  • packages/sqlite-web-core/src/database_functions/float_negate.rs
📚 Learning: 2025-09-17T17:17:41.827Z
Learnt from: findolor
PR: rainlanguage/sqlite-web#11
File: svelte-test/tests/database-functions/float-negate.test.ts:105-119
Timestamp: 2025-09-17T17:17:41.827Z
Learning: The toMixedCase function in svelte-test/tests/fixtures/float-utils.ts only modifies characters matching /[a-f]/ (hex digits a-f), preserving the 'x' in '0x' prefixes and all other characters unchanged, so it won't create conflicts with tests that reject '0X' prefixes.

Applied to files:

  • svelte-test/tests/database-functions/float-negate.test.ts
  • packages/sqlite-web-core/src/database_functions/float_negate.rs
📚 Learning: 2025-09-17T17:09:58.740Z
Learnt from: findolor
PR: rainlanguage/sqlite-web#11
File: svelte-test/tests/database-functions/float-negate.test.ts:64-72
Timestamp: 2025-09-17T17:09:58.740Z
Learning: FLOAT_SUM functionality is available and working in the codebase, so tests can rely on it without needing availability checks or gating logic.

Applied to files:

  • svelte-test/tests/database-functions/float-negate.test.ts
📚 Learning: 2025-08-27T05:59:04.254Z
Learnt from: findolor
PR: rainlanguage/sqlite-web#5
File: svelte-test/tests/database-functions/bigint-sum.test.ts:2-7
Timestamp: 2025-08-27T05:59:04.254Z
Learning: In the sqlite-web repository, the user prefers to keep .js extensions when importing TypeScript files in test files, even in Vitest environments. This is acceptable and should not be flagged as an issue.

Applied to files:

  • svelte-test/tests/database-functions/float-negate.test.ts
📚 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:

  • svelte-test/tests/database-functions/float-negate.test.ts
  • packages/sqlite-web-core/src/database_functions/float_negate.rs
📚 Learning: 2025-08-27T05:55:40.481Z
Learnt from: findolor
PR: rainlanguage/sqlite-web#5
File: packages/sqlite-web-core/src/database_functions/bigint_sum.rs:76-93
Timestamp: 2025-08-27T05:55:40.481Z
Learning: In the sqlite-web-core codebase, for SQLite aggregate function context initialization, the maintainer prefers using byte-scanning to detect zero-initialized memory rather than using explicit initialization flags or Option wrappers.

Applied to files:

  • packages/sqlite-web-core/src/database_functions/float_negate.rs
📚 Learning: 2025-09-15T06:11:31.781Z
Learnt from: findolor
PR: rainlanguage/sqlite-web#9
File: packages/sqlite-web-core/src/coordination.rs:52-65
Timestamp: 2025-09-15T06:11:31.781Z
Learning: In packages/sqlite-web-core/src/coordination.rs, the pattern of using `db.borrow_mut().take()` followed by async database operations and then putting the database back with `*db.borrow_mut() = Some(database)` is safe in their use case and doesn't cause concurrent access issues or "Database not initialized" errors.

Applied to files:

  • packages/sqlite-web-core/src/database_functions/float_negate.rs
🧬 Code graph analysis (1)
svelte-test/tests/database-functions/float-negate.test.ts (2)
svelte-test/tests/fixtures/float-utils.ts (4)
  • createFloatHexMap (50-56)
  • withoutPrefix (33-35)
  • decodeFloatHex (20-31)
  • toMixedCase (37-48)
svelte-test/tests/fixtures/test-helpers.ts (2)
  • createTestDatabase (6-22)
  • cleanupDatabase (173-201)
🔇 Additional comments (10)
packages/sqlite-web-core/src/database_functions/float_negate.rs (4)

5-21: Helper logic is correct and precise.

Trim + explicit empty-input check + hex parse + negation + as_hex round‑trip looks solid and consistent with other Float functions.


45-52: Good strict UTF‑8 handling.

Switch to CStr::to_str() with a clear error is the right trade‑off for TEXT args.


67-79: Error propagation path is robust.

CString guarding interior NULs and falling back to a fixed message is correct.


56-63: Ensure SQLITE_TRANSIENT() is the special “copy” destructor (-1) and is defined once with the correct destructor function-pointer type.

packages/sqlite-web-core/src/database_functions/float_negate.rs (lines 56–63) calls sqlite3_result_text(..., SQLITE_TRANSIENT()); no SQLITE_TRANSIENT definition was found in the repository — add or reference a single, correctly‑typed SQLITE_TRANSIENT (the -1 cast matching sqlite3_result_text's destructor type) to avoid SQLite reading freed memory.

svelte-test/tests/database-functions/float-negate.test.ts (6)

14-21: Fixture generation looks good.

createFloatHexMap centralizes inputs; keeps tests readable and consistent.


64-80: Zero‑sum property test is spot‑on.

Covers prefixed/unprefixed/hi‑precision/zero and asserts via FLOAT_SUM.


82-99: Whitespace handling test is appropriate.

Validates trim behavior end‑to‑end.


100-115: Mixed‑case acceptance is valid.

toMixedCase only touches [a‑f], preserving the 'x' in '0x' as intended.


117-135: Involution test (double negate) is a good addition.

Covers positive, negative, zero cases by string equality.


145-164: Error‑path assertions look correct.

Covers 0X prefix, non‑hex, and empty string with expected messages.

Comment on lines +139 to +144
it("should return NULL when input is NULL", async () => {
const res = await db.query("SELECT FLOAT_NEGATE(NULL) as neg");
const data = JSON.parse(res.value || "[]");
expect(data[0].neg).toBeNull();
});

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

Assert success before parsing NULL case.

Small clarity win: check res.error is falsy before JSON.parse.

Apply this diff:

   it("should return NULL when input is NULL", async () => {
-      const res = await db.query("SELECT FLOAT_NEGATE(NULL) as neg");
+      const res = await db.query("SELECT FLOAT_NEGATE(NULL) as neg");
+      expect(res.error).toBeFalsy();
       const data = JSON.parse(res.value || "[]");
       expect(data[0].neg).toBeNull();
   });
📝 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
it("should return NULL when input is NULL", async () => {
const res = await db.query("SELECT FLOAT_NEGATE(NULL) as neg");
const data = JSON.parse(res.value || "[]");
expect(data[0].neg).toBeNull();
});
it("should return NULL when input is NULL", async () => {
const res = await db.query("SELECT FLOAT_NEGATE(NULL) as neg");
expect(res.error).toBeFalsy();
const data = JSON.parse(res.value || "[]");
expect(data[0].neg).toBeNull();
});
🤖 Prompt for AI Agents
In svelte-test/tests/database-functions/float-negate.test.ts around lines 139 to
144, the test parses res.value without first asserting the query succeeded; add
an assertion that res.error is falsy (e.g., expect(res.error).toBeFalsy() or
equivalent) immediately after the query and before JSON.parse so the test fails
clearly on query errors instead of throwing during parse.

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 needed

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'll skip that error check suggestion since you've indicated it's not necessary for this test case.


🧠 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: findolor
PR: rainlanguage/sqlite-web#11
File: svelte-test/tests/database-functions/float-negate.test.ts:105-119
Timestamp: 2025-09-17T17:17:41.827Z
Learning: The toMixedCase function in svelte-test/tests/fixtures/float-utils.ts only modifies [a-f] characters, preserving the 'x' in '0x' prefixes, so it won't create conflicts with tests that reject '0X' prefixes.

Learnt from: findolor
PR: rainlanguage/sqlite-web#11
File: svelte-test/tests/database-functions/float-negate.test.ts:105-119
Timestamp: 2025-09-17T17:17:41.827Z
Learning: The toMixedCase function in svelte-test/tests/fixtures/float-utils.ts only modifies characters matching /[a-f]/ (hex digits a-f), preserving the 'x' in '0x' prefixes and all other characters unchanged, so it won't create conflicts with tests that reject '0X' prefixes.

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

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

♻️ Duplicate comments (1)
packages/sqlite-web-core/src/database_functions/float_negate.rs (1)

38-46: Minor: Reuse arg0 to avoid double deref and make intent explicit

Keeps consistency with the comment and avoids repeating *argv.

-    if sqlite3_value_type(*argv) == SQLITE_NULL {
+    let arg0 = *argv;
+    if sqlite3_value_type(arg0) == SQLITE_NULL {
         sqlite3_result_null(context);
         return;
     }
 
-    // Get the text value (now known to be non-NULL).
-    let value_ptr = sqlite3_value_text(*argv);
+    // Get the text value (now known to be non-NULL).
+    let value_ptr = sqlite3_value_text(arg0);
📜 Review details

Configuration used: CodeRabbit UI

Review profile: ASSERTIVE

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between fb838e9 and 3a4eec6.

⛔ Files ignored due to path filters (1)
  • svelte-test/package-lock.json is excluded by !**/package-lock.json
📒 Files selected for processing (1)
  • packages/sqlite-web-core/src/database_functions/float_negate.rs (1 hunks)
🧰 Additional context used
🧠 Learnings (7)
📓 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
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.
📚 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/float_negate.rs
📚 Learning: 2025-08-27T05:55:40.481Z
Learnt from: findolor
PR: rainlanguage/sqlite-web#5
File: packages/sqlite-web-core/src/database_functions/bigint_sum.rs:76-93
Timestamp: 2025-08-27T05:55:40.481Z
Learning: In the sqlite-web-core codebase, for SQLite aggregate function context initialization, the maintainer prefers using byte-scanning to detect zero-initialized memory rather than using explicit initialization flags or Option wrappers.

Applied to files:

  • packages/sqlite-web-core/src/database_functions/float_negate.rs
📚 Learning: 2025-09-15T06:11:31.781Z
Learnt from: findolor
PR: rainlanguage/sqlite-web#9
File: packages/sqlite-web-core/src/coordination.rs:52-65
Timestamp: 2025-09-15T06:11:31.781Z
Learning: In packages/sqlite-web-core/src/coordination.rs, the pattern of using `db.borrow_mut().take()` followed by async database operations and then putting the database back with `*db.borrow_mut() = Some(database)` is safe in their use case and doesn't cause concurrent access issues or "Database not initialized" errors.

Applied to files:

  • packages/sqlite-web-core/src/database_functions/float_negate.rs
📚 Learning: 2025-09-17T17:17:41.827Z
Learnt from: findolor
PR: rainlanguage/sqlite-web#11
File: svelte-test/tests/database-functions/float-negate.test.ts:105-119
Timestamp: 2025-09-17T17:17:41.827Z
Learning: The toMixedCase function in svelte-test/tests/fixtures/float-utils.ts only modifies characters matching /[a-f]/ (hex digits a-f), preserving the 'x' in '0x' prefixes and all other characters unchanged, so it won't create conflicts with tests that reject '0X' prefixes.

Applied to files:

  • packages/sqlite-web-core/src/database_functions/float_negate.rs
📚 Learning: 2025-09-17T17:17:41.827Z
Learnt from: findolor
PR: rainlanguage/sqlite-web#11
File: svelte-test/tests/database-functions/float-negate.test.ts:105-119
Timestamp: 2025-09-17T17:17:41.827Z
Learning: The toMixedCase function in svelte-test/tests/fixtures/float-utils.ts only modifies [a-f] characters, preserving the 'x' in '0x' prefixes, so it won't create conflicts with tests that reject '0X' prefixes.

Applied to files:

  • packages/sqlite-web-core/src/database_functions/float_negate.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:

  • packages/sqlite-web-core/src/database_functions/float_negate.rs
⏰ 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 (5)
packages/sqlite-web-core/src/database_functions/float_negate.rs (5)

8-15: Empty-input handling and parse errors look good

Trim + explicit empty check and clear parse errors match prior functions.


47-54: Strict UTF‑8 validation: LGTM

Correct to fail fast on invalid UTF‑8 rather than lossy conversion.


58-65: Correct use of sqlite3_result_text with SQLITE_TRANSIENT

Passing explicit length and transient destructor is safe; ownership/lifetime is correct.


69-80: Robust error propagation path

Good handling of interior NULs and consistent messaging.


84-137: Unit tests cover key paths

Covers sign flip, zero, precision, whitespace, and invalids. Looks solid.

Comment on lines +91 to +97
#[wasm_bindgen_test]
fn test_float_negate_hex_to_hex_zero() {
let zero_hex = Float::parse("0".to_string()).unwrap().as_hex();
let expected_zero_hex = Float::parse("0".to_string()).unwrap().as_hex();
let out = float_negate_hex_to_hex(&zero_hex).unwrap();
assert_eq!(out, expected_zero_hex);
}
Copy link

Choose a reason for hiding this comment

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

Float doesn't distinguish between -0 and 0 as far as I remember

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: 1

Caution

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

⚠️ Outside diff range comments (1)
svelte-test/tests/database-functions/float-sum.test.ts (1)

47-53: Fix NOT NULL schema: NULL-handling tests currently violate the constraint.

Later tests insert NULL into amount; with TEXT NOT NULL the INSERT fails and can mask assertions.

Apply this diff to allow NULLs:

-        amount TEXT NOT NULL,
+        amount TEXT,

Alternatively, create a separate nullable table just within the “NULL Value Handling” describe.

♻️ Duplicate comments (3)
svelte-test/tests/database-functions/float-sum.test.ts (1)

8-14: Extension-less fixture import is acceptable in this repo.

Prior discussion established this works in your setup; leaving as-is.

svelte-test/tests/database-functions/float-negate.test.ts (2)

7-12: Extension-less fixture import acknowledged.

Consistent with earlier decision; no change needed.


139-144: Optional: assert success before parse.

Tiny clarity win; add an error check prior to JSON.parse.

-      const res = await db.query("SELECT FLOAT_NEGATE(NULL) as neg");
+      const res = await db.query("SELECT FLOAT_NEGATE(NULL) as neg");
+      expect(res.error).toBeFalsy();
       const data = JSON.parse(res.value || "[]");
📜 Review details

Configuration used: CodeRabbit UI

Review profile: ASSERTIVE

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 3a4eec6 and b5b88f3.

⛔ Files ignored due to path filters (2)
  • Cargo.lock is excluded by !**/*.lock
  • svelte-test/package-lock.json is excluded by !**/package-lock.json
📒 Files selected for processing (4)
  • pkg/package.json (1 hunks)
  • svelte-test/package.json (1 hunks)
  • svelte-test/tests/database-functions/float-negate.test.ts (1 hunks)
  • svelte-test/tests/database-functions/float-sum.test.ts (3 hunks)
🧰 Additional context used
📓 Path-based instructions (1)
svelte-test/package.json

📄 CodeRabbit inference engine (CLAUDE.md)

In the Svelte test app, depend on the sqlite-web package via the locally packed tarball produced by npm pack

Files:

  • svelte-test/package.json
🧠 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
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.
📚 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 svelte-test/package.json : In the Svelte test app, depend on the sqlite-web package via the locally packed tarball produced by npm pack

Applied to files:

  • svelte-test/package.json
📚 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/float-negate.test.ts
  • svelte-test/tests/database-functions/float-sum.test.ts
📚 Learning: 2025-09-17T17:17:41.827Z
Learnt from: findolor
PR: rainlanguage/sqlite-web#11
File: svelte-test/tests/database-functions/float-negate.test.ts:105-119
Timestamp: 2025-09-17T17:17:41.827Z
Learning: The toMixedCase function in svelte-test/tests/fixtures/float-utils.ts only modifies [a-f] characters, preserving the 'x' in '0x' prefixes, so it won't create conflicts with tests that reject '0X' prefixes.

Applied to files:

  • svelte-test/tests/database-functions/float-negate.test.ts
  • svelte-test/tests/database-functions/float-sum.test.ts
📚 Learning: 2025-09-17T17:17:41.827Z
Learnt from: findolor
PR: rainlanguage/sqlite-web#11
File: svelte-test/tests/database-functions/float-negate.test.ts:105-119
Timestamp: 2025-09-17T17:17:41.827Z
Learning: The toMixedCase function in svelte-test/tests/fixtures/float-utils.ts only modifies characters matching /[a-f]/ (hex digits a-f), preserving the 'x' in '0x' prefixes and all other characters unchanged, so it won't create conflicts with tests that reject '0X' prefixes.

Applied to files:

  • svelte-test/tests/database-functions/float-negate.test.ts
  • svelte-test/tests/database-functions/float-sum.test.ts
📚 Learning: 2025-09-17T17:09:58.740Z
Learnt from: findolor
PR: rainlanguage/sqlite-web#11
File: svelte-test/tests/database-functions/float-negate.test.ts:64-72
Timestamp: 2025-09-17T17:09:58.740Z
Learning: FLOAT_SUM functionality is available and working in the codebase, so tests can rely on it without needing availability checks or gating logic.

Applied to files:

  • svelte-test/tests/database-functions/float-negate.test.ts
  • svelte-test/tests/database-functions/float-sum.test.ts
📚 Learning: 2025-08-27T05:59:04.254Z
Learnt from: findolor
PR: rainlanguage/sqlite-web#5
File: svelte-test/tests/database-functions/bigint-sum.test.ts:2-7
Timestamp: 2025-08-27T05:59:04.254Z
Learning: In the sqlite-web repository, the user prefers to keep .js extensions when importing TypeScript files in test files, even in Vitest environments. This is acceptable and should not be flagged as an issue.

Applied to files:

  • svelte-test/tests/database-functions/float-negate.test.ts
📚 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:

  • svelte-test/tests/database-functions/float-negate.test.ts
  • svelte-test/tests/database-functions/float-sum.test.ts
🧬 Code graph analysis (2)
svelte-test/tests/database-functions/float-negate.test.ts (2)
svelte-test/tests/fixtures/float-utils.ts (4)
  • createFloatHexMap (50-56)
  • withoutPrefix (33-35)
  • decodeFloatHex (20-31)
  • toMixedCase (37-48)
svelte-test/tests/fixtures/test-helpers.ts (2)
  • createTestDatabase (6-22)
  • cleanupDatabase (173-201)
svelte-test/tests/database-functions/float-sum.test.ts (1)
svelte-test/tests/fixtures/float-utils.ts (1)
  • createFloatHexMap (50-56)
⏰ 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 (5)
pkg/package.json (1)

15-15: LGTM: trailing newline only.

No functional change; package metadata remains consistent.

svelte-test/tests/database-functions/float-sum.test.ts (2)

30-40: Good: centralized hex fixtures.

Using createFloatHexMap with decimal inputs improves readability and consistency across tests.


545-558: Follow-up if keeping NOT NULL (not recommended): gate or restructure NULL tests.

If you don’t relax the schema, these inserts will fail and subsequent SELECT expectations won’t be meaningful. Prefer the schema fix above or build the NULL cases via SELECT literals instead of table rows.

svelte-test/tests/database-functions/float-negate.test.ts (2)

100-115: Mixed‑case hex test is sound.

toMixedCase preserves the 'x' in '0x', so this won’t conflict with the 0X‑rejection test.


117-135: Nice: involution check (double negate).

Covers negative, zero, and positive.

@findolor findolor merged commit a845bdc into main Sep 19, 2025
3 checks passed
@findolor findolor deleted the float-negate branch September 19, 2025 06:47
@coderabbitai coderabbitai bot mentioned this pull request Sep 19, 2025
4 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Add FLOAT_NEGATE function in database functions
3 participants