Skip to content

Commit fdb397a

Browse files
committed
optimize clinical event derived table related queries
1 parent c4752a7 commit fdb397a

File tree

3 files changed

+86
-12
lines changed

3 files changed

+86
-12
lines changed

src/main/resources/db-scripts/clickhouse/clickhouse.sql

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -364,7 +364,7 @@ CREATE TABLE clinical_event_derived
364364
cancer_study_identifier LowCardinality(String)
365365
)
366366
ENGINE = MergeTree
367-
ORDER BY (event_type, patient_unique_id, cancer_study_identifier);
367+
ORDER BY (cancer_study_identifier, event_type, patient_unique_id);
368368

369369
INSERT INTO clinical_event_derived
370370
SELECT

src/main/resources/mappers/clickhouse/studyview/ClickhouseStudyViewFilterMapper.xml

Lines changed: 40 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -190,8 +190,15 @@
190190
ced.cancer_study_identifier AS cancer_study_identifier
191191
FROM clinical_event_derived ced
192192
<where>
193-
key = 'SAMPLE_ID'
194-
AND (event_type ILIKE 'Sample Acquisition' OR event_type ILIKE 'SPECIMEN')
193+
<if test="studyViewFilterContext.customDataFilterCancerStudies != null and !studyViewFilterContext.customDataFilterCancerStudies.isEmpty()">
194+
cancer_study_identifier IN
195+
<foreach item="studyId" collection="studyViewFilterContext.customDataFilterCancerStudies" open="(" separator="," close=")">
196+
#{studyId}
197+
</foreach>
198+
AND
199+
</if>
200+
(event_type ILIKE 'Sample Acquisition' OR event_type ILIKE 'SPECIMEN')
201+
AND key = 'SAMPLE_ID'
195202
</where>
196203
GROUP BY patient_unique_id, ced.value, cancer_study_identifier
197204
) ced
@@ -202,8 +209,16 @@
202209
value AS treatment,
203210
argMin(start_date, start_date) AS treatment_time_taken
204211
FROM clinical_event_derived
205-
WHERE lower(event_type) = 'treatment'
206-
AND key = 'AGENT'
212+
WHERE
213+
<if test="studyViewFilterContext.customDataFilterCancerStudies != null and !studyViewFilterContext.customDataFilterCancerStudies.isEmpty()">
214+
cancer_study_identifier IN
215+
<foreach item="studyId" collection="studyViewFilterContext.customDataFilterCancerStudies" open="(" separator="," close=")">
216+
#{studyId}
217+
</foreach>
218+
AND
219+
</if>
220+
lower(event_type) = 'treatment'
221+
AND key = 'AGENT'
207222
GROUP BY patient_unique_id, value
208223
) ced_inner ON ced_inner.patient_unique_id = ced.patient_unique_id
209224
<where>
@@ -232,7 +247,14 @@
232247
SELECT patient_unique_id
233248
FROM clinical_event_derived
234249
<where>
235-
<foreach item="dataFilterValue" collection="clinicalEventFilter.values" open="(" separator=") OR (" close=")">
250+
<if test="studyViewFilterContext.customDataFilterCancerStudies != null and !studyViewFilterContext.customDataFilterCancerStudies.isEmpty()">
251+
cancer_study_identifier IN
252+
<foreach item="studyId" collection="studyViewFilterContext.customDataFilterCancerStudies" open="(" separator="," close=")">
253+
#{studyId}
254+
</foreach>
255+
AND
256+
</if>
257+
<foreach item="dataFilterValue" collection="clinicalEventFilter.values" open="((" separator=") OR (" close="))">
236258
event_type = #{dataFilterValue.value}
237259
</foreach>
238260
</where>
@@ -250,9 +272,19 @@
250272
SELECT patient_unique_id
251273
FROM clinical_event_derived
252274
<where>
253-
<foreach item="patientTreatmentFilter" collection="andedPatientTreatmentFilters.getFilters()" open="("
254-
separator=") OR ("
255-
close=")"> lower(event_type) = 'treatment'
275+
<if test="studyViewFilterContext.customDataFilterCancerStudies != null and !studyViewFilterContext.customDataFilterCancerStudies.isEmpty()">
276+
cancer_study_identifier IN
277+
<foreach item="studyId" collection="studyViewFilterContext.customDataFilterCancerStudies" open="(" separator="," close=")">
278+
#{studyId}
279+
</foreach>
280+
AND
281+
</if>
282+
<foreach
283+
item="patientTreatmentFilter"
284+
collection="andedPatientTreatmentFilters.getFilters()"
285+
open="((" separator=") OR (" close="))"
286+
>
287+
lower(event_type) = 'treatment'
256288
AND key = 'AGENT'
257289
AND value = #{patientTreatmentFilter.treatment}
258290
</foreach>

src/main/resources/mappers/clickhouse/treatment/ClickhouseTreatmentMapper.xml

Lines changed: 45 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,13 @@
1616
count(DISTINCT patient_unique_id) AS count
1717
FROM clinical_event_derived
1818
<where>
19+
<if test="studyViewFilterContext.customDataFilterCancerStudies != null and !studyViewFilterContext.customDataFilterCancerStudies.isEmpty()">
20+
cancer_study_identifier IN
21+
<foreach item="studyId" collection="studyViewFilterContext.customDataFilterCancerStudies" open="(" separator="," close=")">
22+
#{studyId}
23+
</foreach>
24+
AND
25+
</if>
1926
lower(event_type) = 'treatment'
2027
AND key = 'AGENT'
2128
AND
@@ -34,6 +41,13 @@
3441
patient_unique_id
3542
FROM clinical_event_derived
3643
<where>
44+
<if test="studyViewFilterContext.customDataFilterCancerStudies != null and !studyViewFilterContext.customDataFilterCancerStudies.isEmpty()">
45+
cancer_study_identifier IN
46+
<foreach item="studyId" collection="studyViewFilterContext.customDataFilterCancerStudies" open="(" separator="," close=")">
47+
#{studyId}
48+
</foreach>
49+
AND
50+
</if>
3751
lower(event_type) = 'treatment'
3852
AND key = 'AGENT'
3953
AND
@@ -48,8 +62,15 @@
4862
count(distinct ced.value) AS totalSamples
4963
FROM clinical_event_derived ced
5064
<where>
65+
<if test="studyViewFilterContext.customDataFilterCancerStudies != null and !studyViewFilterContext.customDataFilterCancerStudies.isEmpty()">
66+
cancer_study_identifier IN
67+
<foreach item="studyId" collection="studyViewFilterContext.customDataFilterCancerStudies" open="(" separator="," close=")">
68+
#{studyId}
69+
</foreach>
70+
AND
71+
</if>
72+
(event_type ILIKE 'Sample Acquisition' OR event_type ILIKE 'SPECIMEN')
5173
AND key = 'SAMPLE_ID'
52-
AND (event_type ILIKE 'Sample Acquisition' OR event_type ILIKE 'SPECIMEN')
5374
AND
5475
concat(ced.cancer_study_identifier, '_', ced.value) IN ( <include refid="org.cbioportal.infrastructure.repository.clickhouse.studyview.ClickhouseStudyViewFilterMapper.sampleUniqueIdsFromStudyViewFilter"/>)
5576
AND
@@ -58,6 +79,13 @@
5879
patient_unique_id
5980
FROM clinical_event_derived
6081
<where>
82+
<if test="studyViewFilterContext.customDataFilterCancerStudies != null and !studyViewFilterContext.customDataFilterCancerStudies.isEmpty()">
83+
cancer_study_identifier IN
84+
<foreach item="studyId" collection="studyViewFilterContext.customDataFilterCancerStudies" open="(" separator="," close=")">
85+
#{studyId}
86+
</foreach>
87+
AND
88+
</if>
6189
lower(event_type) = 'treatment'
6290
AND key = 'AGENT'
6391
AND
@@ -85,8 +113,15 @@
85113
ced.cancer_study_identifier AS cancer_study_identifier
86114
FROM clinical_event_derived ced
87115
<where>
88-
key = 'SAMPLE_ID'
89-
AND (event_type ILIKE 'Sample Acquisition' OR event_type ILIKE 'SPECIMEN')
116+
<if test="studyViewFilterContext.customDataFilterCancerStudies != null and !studyViewFilterContext.customDataFilterCancerStudies.isEmpty()">
117+
cancer_study_identifier IN
118+
<foreach item="studyId" collection="studyViewFilterContext.customDataFilterCancerStudies" open="(" separator="," close=")">
119+
#{studyId}
120+
</foreach>
121+
AND
122+
</if>
123+
(event_type ILIKE 'Sample Acquisition' OR event_type ILIKE 'SPECIMEN')
124+
AND key = 'SAMPLE_ID'
90125
</where>
91126
GROUP BY patient_unique_id, ced.value, cancer_study_identifier
92127
</sql>
@@ -98,6 +133,13 @@
98133
argMin(start_date, start_date) AS treatment_time_taken
99134
FROM clinical_event_derived
100135
<where>
136+
<if test="studyViewFilterContext.customDataFilterCancerStudies != null and !studyViewFilterContext.customDataFilterCancerStudies.isEmpty()">
137+
cancer_study_identifier IN
138+
<foreach item="studyId" collection="studyViewFilterContext.customDataFilterCancerStudies" open="(" separator="," close=")">
139+
#{studyId}
140+
</foreach>
141+
AND
142+
</if>
101143
lower(event_type) = 'treatment'
102144
AND key = 'AGENT'
103145
</where>

0 commit comments

Comments
 (0)