Repository navigation
Keep cohort sample "not between" age filter inside the cohort - #2591
Merged
Merged
Conversation
The "not between" age mode of a cohort sample added " AND age > @age_max OR age < @age_min" to the WHERE clause of generateSample.sql. AND binds tighter than OR, so the query became (cohort_definition_id = @cohort_definition_id AND age > @age_max) OR (age < @age_min AND <gender filter>) and the sample drew people younger than the minimum age from every cohort in the results schema's cohort table, and people older than the maximum age regardless of the gender filter. Wrap the two age comparisons in parentheses, and render the sample query in its own method so it can be tested. Adds a test that runs the sample query against the embedded Postgres database with people from two cohorts, with and without a gender filter.
chrisknoll
approved these changes
Nov 13, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #2590
The "not between" age mode of a cohort sample appended
AND age > @age_max OR age < @age_minto theWHERE cohort_definition_id = @cohort_definition_idclause ofgenerateSample.sql.ANDbinds tighter thanOR, so the query becameand the sample drew people younger than the minimum age from every cohort in the results schema's
cohorttable, and people older than the maximum age regardless of the gender filter. The other age modes are not affected.Change
CohortSamplingService: wrap the two age comparisons in parentheses,AND (age > @age_max OR age < @age_min).prepareSampleElementsSql, so the filter can be tested without saving a sample;sampleElementsruns it as before.Tests
CohortSamplingServiceTest(embedded Postgres): runs the sample query over people in two cohorts with "not between" 20 and 60, with and without a female gender filter. Onmasterboth tests fail (the first returns a person from the other cohort, the second also returns a male person older than 60).