Skip to content

Register custom functions via the SQLite C API directly#8

Merged
colemancda merged 3 commits into
masterfrom
feature/function
Jul 16, 2026
Merged

Register custom functions via the SQLite C API directly#8
colemancda merged 3 commits into
masterfrom
feature/function

Conversation

@colemancda

Copy link
Copy Markdown
Member

No description provided.

Call sqlite3_create_function_v2 with @convention(c) function pointers and a
retained context pointer, instead of SQLite.swift's createFunction. SQLite.swift
registers the callback as a @convention(block) closure cast to a raw pointer,
which is unreliable off Apple platforms and segfaults on Linux (upstream
stephencelis/SQLite.swift#1071). A plain C function pointer works everywhere.

- New CustomFunction.swift: C-API registration, argument/result marshalling
  between sqlite3_value/context and AttributeValue, with the SQLite C module
  imported from the system SQLite3 on Apple and the embedded copy elsewhere.
- Package.swift depends on stephencelis/CSQLite so the C API is available on
  Linux/Android where the system SQLite3 module isn't.
- Removed the Darwin-only gate on the custom-function tests; they now run on
  every platform.
R*Tree is an optional SQLite compile-time feature (SQLITE_ENABLE_RTREE),
present in the system SQLite on Apple platforms but not in the embedded SQLite
used on Linux. Gate the app-managed R*Tree test on a runtime capability probe
so it runs where rtree exists and is skipped otherwise. The custom-function
tests themselves now pass on every platform via the C-API registration.
Compare distance filtering/sorting via the registered SQL `distance` function
(executed in SQLite) against fetching every row and filtering/sorting in Swift.
Two scenarios: a selective radius filter+sort, and a full sort with no filter.

Opt-in via the BENCHMARK environment variable so the 100k-row dataset doesn't
slow the normal test matrix:

    BENCHMARK=1 swift test -c release --filter Benchmark
@colemancda
colemancda merged commit 27f305f into master Jul 16, 2026
10 of 12 checks passed
@colemancda
colemancda deleted the feature/function branch July 16, 2026 22:05
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.

1 participant