Skip to content

Keep cohort sample "not between" age filter inside the cohort - #2591

Merged
chrisknoll merged 1 commit into
OHDSI:masterfrom
DG47:fix/cohort-sample-age-not-between
Nov 13, 2026
Merged

chrisknoll merged 1 commit into
OHDSI:masterfrom
DG47:fix/cohort-sample-age-not-between

Conversation

@DG47

@DG47 DG47 commented Nov 10, 2026

Copy link
Copy Markdown
Contributor

Fixes #2590

The "not between" age mode of a cohort sample appended AND age > @age_max OR age < @age_min to the WHERE cohort_definition_id = @cohort_definition_id clause of generateSample.sql. AND binds tighter than OR, so the query became

WHERE (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. The other age modes are not affected.

Change

  • CohortSamplingService: wrap the two age comparisons in parentheses, AND (age > @age_max OR age < @age_min).
  • The sample query is now rendered in its own package-private method, prepareSampleElementsSql, so the filter can be tested without saving a sample; sampleElements runs 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. On master both tests fail (the first returns a person from the other cohort, the second also returns a male person older than 60).
mvn -B -DskipUnitTests=false -DskipITtests=true test -Dtest=CohortSamplingServiceTest,PersonServiceSqlTest,CleanupCohortTaskletTest
Tests run: 5, Failures: 0, Errors: 0, Skipped: 0

mvn -B -DskipUnitTests=false -DskipITtests=true test
Tests run: 171, Failures: 0, Errors: 0, Skipped: 5

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
chrisknoll merged commit cb9e135 into OHDSI:master Nov 13, 2026
4 checks passed
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.

Cohort sample with age "not between" includes people from other cohorts and ignores the gender filter

2 participants