Skip to content

fix: correct precedence parentheses for SQLFragment joins#476

Merged
wschurman merged 1 commit intomainfrom
wschurman/02-26-fix_correct_precedence_parentheses_for_sqlfragment_joins
Feb 27, 2026
Merged

fix: correct precedence parentheses for SQLFragment joins#476
wschurman merged 1 commit intomainfrom
wschurman/02-26-fix_correct_precedence_parentheses_for_sqlfragment_joins

Conversation

@wschurman
Copy link
Member

@wschurman wschurman commented Feb 26, 2026

Why

There's a bug in where clause construction, which used SQLFragmentHelpers.and: https://github.com/expo/entity/blob/main/packages/entity-database-adapter-knex/src/internal/EntityKnexDataManager.ts#L303

The bug is that given something like:

where: sql`account_id = ${accountId}`,
searchWhere: sql`something ILIKE term OR similarity(term, ...) OR ...`

it would produce

WHERE account_id = ${accountId} AND something ILIKE term OR similarity(term, ...) OR ...

which has the incorrect parentheses and therefore incorrect precedence.

This exposed more bugs in the precedence in or, and, join, etc methods.

How

The fix is multifold:

  • Make the public join method only be used for comma-separation joining. Rename it to joinWithCommaSeparator.
  • Wrap each group in or and and in parentheses to ensure correct precedence.
    > SQLFragment.or(sql`account_id = ${accountId}`, sql`app_id = ${appId}`
    sql`(account_id = ${accountId}) OR (app_id = ${appId})`
    

Test Plan

Run existing tests to ensure nothing breaks, update unit tests and inspect output SQL statements to ensure they are correct parentheses. Especially the builds complex queries with multiple helpers test case.

@wschurman wschurman force-pushed the wschurman/02-26-fix_correct_precedence_parentheses_for_sqlfragment_joins branch from fe9288b to fa751d8 Compare February 26, 2026 20:15
@wschurman wschurman force-pushed the wschurman/02-26-feat_add_method_to_get_pagination_cursor_for_single_entity branch from 28e0651 to 1206180 Compare February 26, 2026 21:06
@wschurman wschurman force-pushed the wschurman/02-26-fix_correct_precedence_parentheses_for_sqlfragment_joins branch 2 times, most recently from 698b8b1 to 1d51229 Compare February 26, 2026 21:34
@wschurman wschurman force-pushed the wschurman/02-26-feat_add_method_to_get_pagination_cursor_for_single_entity branch from 1206180 to 0ad1b46 Compare February 26, 2026 21:34
@wschurman wschurman force-pushed the wschurman/02-26-fix_correct_precedence_parentheses_for_sqlfragment_joins branch from 1d51229 to 5cf4f64 Compare February 26, 2026 21:52
@wschurman wschurman force-pushed the wschurman/02-26-feat_add_method_to_get_pagination_cursor_for_single_entity branch from 0ad1b46 to 8d58396 Compare February 26, 2026 22:10
@wschurman wschurman force-pushed the wschurman/02-26-fix_correct_precedence_parentheses_for_sqlfragment_joins branch from 5cf4f64 to 724cbbd Compare February 26, 2026 22:10
@wschurman wschurman requested review from ide and quinlanj February 26, 2026 22:23
@wschurman wschurman marked this pull request as ready for review February 26, 2026 22:23
@codecov
Copy link

codecov bot commented Feb 27, 2026

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 100.00%. Comparing base (898f71c) to head (ffb7a82).
⚠️ Report is 2 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff            @@
##              main      #476   +/-   ##
=========================================
  Coverage   100.00%   100.00%           
=========================================
  Files          110       110           
  Lines        16183     16198   +15     
  Branches       882       871   -11     
=========================================
+ Hits         16183     16198   +15     
Flag Coverage Δ
integration 24.17% <90.24%> (+0.08%) ⬆️
unittest 95.00% <70.73%> (-0.01%) ⬇️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

Copy link
Member Author

wschurman commented Feb 27, 2026

Merge activity

  • Feb 27, 2:15 AM UTC: A user started a stack merge that includes this pull request via Graphite.
  • Feb 27, 2:28 AM UTC: Graphite rebased this pull request as part of a merge.
  • Feb 27, 2:31 AM UTC: @wschurman merged this pull request with Graphite.

@wschurman wschurman changed the base branch from wschurman/02-26-feat_add_method_to_get_pagination_cursor_for_single_entity to graphite-base/476 February 27, 2026 02:24
@wschurman wschurman changed the base branch from graphite-base/476 to main February 27, 2026 02:27
@wschurman wschurman force-pushed the wschurman/02-26-fix_correct_precedence_parentheses_for_sqlfragment_joins branch from 724cbbd to ffb7a82 Compare February 27, 2026 02:28
@wschurman wschurman merged commit 2c4c649 into main Feb 27, 2026
3 checks passed
@wschurman wschurman deleted the wschurman/02-26-fix_correct_precedence_parentheses_for_sqlfragment_joins branch February 27, 2026 02:31
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.

2 participants