Skip to content

Express lesion visibility as an anti-join - #1667

Merged
danlamanna merged 1 commit into
masterfrom
lesion-visibility-anti-join
Nov 24, 2026
Merged

danlamanna merged 1 commit into
masterfrom
lesion-visibility-anti-join

Conversation

@danlamanna

Copy link
Copy Markdown
Member

Stacked on #1666: it reuses ImagePermissions.visible_image_q.

A lesion is visible when the user can see all of its published images, and there is at least one. That was a bool_and over a GROUP BY of every lesion's accessions, images and share rows, built in full before the first page. It was also wrong: the share rows are per grantee, so a private image shared with two users produced one true and one false row and bool_and hid the lesion from both of them.

The filter is now two correlated subqueries over the image visibility predicate: EXISTS a visible image of the lesion, and NOT EXISTS an invisible one. Both carry the predicate so Postgres filters each side before joining; with a bare "has any image" guard on the first subquery the first page was 3x slower.

Benchmarks, fastest round, master vs this branch:

benchmark 100k images 1.1M images
test_lesion_list[shares] 118 → 68.0 ms (1.73x) 1,564 → 687 ms (2.28x)
test_lesion_list[anonymous] 88.4 → 84.5 ms (1.05x) 1,346 → 878 ms (1.53x)
test_lesion_list[staff] 63.9 → 70.2 ms (0.91x) 659 → 644 ms (1.02x)
test_lesion_detail_api 7.5 → 8.7 ms (0.87x) 8.0 → 8.5 ms (0.94x)
test_lesion_detail_page 10.0 → 11.4 ms (0.88x) 11.4 → 11.4 ms (1.00x)

At 1.1M images the filter's own cost for the shares user went from about 850 ms to nothing measurable: the page is within 2 ms of the staff query, which has no permission filter at all. The remaining 620 ms is the lesion list's own window functions and DISTINCT ON, which run for staff too.

Tests: test_api_lesion_permissions_shares covers a private image shared with the requesting user and with a second user (hidden on master, visible here), and an unpublished accession not changing who can see the lesion.

@coderabbitai

coderabbitai Bot commented Nov 24, 2026 •

Copy link
Copy Markdown

Warning

Review limit reached

You've used all free OSS reviews for now. Wait for the free limit to reset to keep reviewing this public repository.

Next included review available in 48 minutes.

Check out review usage here.

View limit details

Limit details: You’ve used the included review currently available.

Learn how review limits work.

Review configuration:

Run configuration

  • Source mode: Auto
  • Repository: Unavailable
  • Branch: Unavailable
  • File: No configuration file from this repository applied
  • Commit: Unavailable
  • Layers: defaults
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: 0cfecbc0-d72b-4c58-8c2e-33b3027773f1
Commits

Reviewing files that changed from the base of the PR and between 580211b and 0a5056a.

Files selected for processing: 2

  • isic/ingest/models/lesion.py
  • isic/ingest/tests/test_api_lesion.py
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

Share

Base automatically changed from image-perms-without-multiplying-joins to master November 24, 2026 19:12
A lesion is visible when the user can see all of its published images,
and there is at least one: an EXISTS and a NOT EXISTS over the image
visibility predicate instead of a bool_and over one row per image share.
The grouping hid a lesion from everyone a private image was shared with
once it was shared with more than one user.
@danlamanna
danlamanna force-pushed the lesion-visibility-anti-join branch from 70677d2 to 0a5056a Compare November 24, 2026 19:13
@danlamanna
danlamanna enabled auto-merge November 24, 2026 19:13
@danlamanna
danlamanna merged commit e322b80 into master Nov 24, 2026
3 checks passed
@danlamanna
danlamanna deleted the lesion-visibility-anti-join branch November 24, 2026 19:17
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