Skip to content

Fix: Render boolean functions bare in MySQL and MariaDB filters - #625

Merged
adrien-berchet merged 1 commit into
geoalchemy:mainfrom
adrien-berchet:fix_issue621
Nov 3, 2026
Merged

adrien-berchet merged 1 commit into
geoalchemy:mainfrom
adrien-berchet:fix_issue621

Conversation

@adrien-berchet

Copy link
Copy Markdown
Member

Fixes #621

Since 0.18 (geoalchemy#540), spatial predicates like ST_Contains have a Boolean
return type. MySQL and MariaDB do not support native booleans, so
SQLAlchemy rendered them as `ST_Contains(...) = 1` when used as filters,
which prevents these servers from using spatial indexes (MySQL bug
#76384) and leads to full table scans.

Compile GeoAlchemy2 boolean functions used as filters bare on MySQL and
MariaDB (`ST_Contains(...)` and `NOT ST_Contains(...)`). Other dialects
keep the comparison, which is required by MSSQL and by SpatiaLite, whose
predicates return -1 for NULL arguments. Other boolean expressions are
not affected.
@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review Summary

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ⚠️ Failed 2026-11-03T11:50:23.044881Z
We couldn’t complete this review. Comment @codex review to try again.
ccb402a PR opened
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@chatgpt-codex-connector

Copy link
Copy Markdown

You have reached your Codex usage limits for code reviews. You can see your limits in the Codex usage dashboard.

@adrien-berchet

Copy link
Copy Markdown
Member Author

Hi @gaellegaret-joy
This PR should solve your issue based on your suggested fix. Can you test the branch please? Also, multiple changes were introduced since 0.20.0 so don't hesitate to report any other issue you may encounter.

@gaellegaret-joy

Copy link
Copy Markdown

Thanks a lot for the quick fix! I tested the branch (ccb402a) on our application (MySQL 8.0.42, SQLAlchemy 2.0.51, Python 3.12), after removing our type_=NullType() workaround:

  • spatial predicates are now rendered bare in our filters, including JOIN ... ON clauses and predicates on a bound WKBElement, and MySQL uses the spatial indexes again: our nightly query went from ~900 ms to ~8 ms per call, with the same results;
  • our test suite (~1,500 tests) passes, and I found no other difference with 0.20.0: binding WKBElements and reading geometries back give the same results.

@adrien-berchet
adrien-berchet merged commit 8a645d5 into geoalchemy:main Nov 3, 2026
8 checks passed
@adrien-berchet

Copy link
Copy Markdown
Member Author

Great! Thank you very much for taking time to test!
I will try to push a new release soon, I'll let you know. I just need to update the docs to ensure it is consistent with this release.

@adrien-berchet
adrien-berchet deleted the fix_issue621 branch November 3, 2026 16:19
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.

[Bug report] Since 0.18, spatial predicates compile to ... = 1 on MySQL and no longer use spatial indexes

2 participants