Skip to content

Commit 2790e3f

Browse files
author
Alexandra Pavlyshina
committed
measure-evaluate: subject push-down for evidence SQL
1 parent 0ad6339 commit 2790e3f

1 file changed

Lines changed: 16 additions & 0 deletions

File tree

aidbox-custom-operations/measure-evaluate/app/evaluate_measure.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,22 @@ def build_evidence_sql(measure_sql: str, evidence_sql_fragment: str,
114114

115115
ctes = measure_sql[:idx]
116116

117+
# Apply same push-down marker substitution as build_patient_sql so the
118+
# measure CTEs use ix_*_subject indexes instead of scanning the cohort.
119+
# Without this, evidence SQL is bottlenecked by full-cohort CTE work
120+
# (CMS165 evidence query took 170s on 97K cohort before this).
121+
if patient_id:
122+
pid_for_markers = _sanitize_patient_id(patient_id)
123+
ctes = re.sub(
124+
r"--\s*\$SUBJ\$\s+(.+?)\s*$",
125+
lambda m: f"AND {m.group(1)} = '{pid_for_markers}'",
126+
ctes,
127+
flags=re.MULTILINE,
128+
)
129+
ctes = ctes.replace("/*$SUBJ_PARAM$*/", f", '{pid_for_markers}'")
130+
else:
131+
ctes = ctes.replace("/*$SUBJ_PARAM$*/", "")
132+
117133
# Add comma after last CTE closing paren
118134
cte_lines = ctes.rstrip().splitlines()
119135
for i in range(len(cte_lines) - 1, -1, -1):

0 commit comments

Comments
 (0)