Skip to content

Commit ff2cb65

Browse files
committed
Restructured codeset query to fetch all included concepts and left join to all excluded concepts looking for exclusion = null.
1 parent 1c795ee commit ff2cb65

File tree

6 files changed

+51
-37
lines changed

6 files changed

+51
-37
lines changed

src/main/java/org/ohdsi/webapi/vocabulary/ConceptSetExpressionQueryBuilder.java

+45-28
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,11 @@
2424
*/
2525
public class ConceptSetExpressionQueryBuilder {
2626

27-
private final static String CONCEPT_SET_QUERY_TEMPLATE = ResourceHelper.GetResourceAsString("/resources/vocabulary/sql/conceptSetExpression.sql");
28-
private final static String CONCEPT_SET_EXCLUDE_TEMPLATE = ResourceHelper.GetResourceAsString("/resources/vocabulary/sql/conceptSetExclude.sql");
27+
private final static String CONCEPT_SET_QUERY_TEMPLATE = ResourceHelper.GetResourceAsString("/resources/vocabulary/sql/conceptSetQuery.sql");
2928
private final static String CONCEPT_SET_DESCENDANTS_TEMPLATE = ResourceHelper.GetResourceAsString("/resources/vocabulary/sql/conceptSetDescendants.sql");
3029
private final static String CONCEPT_SET_MAPPED_TEMPLATE = ResourceHelper.GetResourceAsString("/resources/vocabulary/sql/conceptSetMapped.sql");
30+
private final static String CONCEPT_SET_INCLUDE_TEMPLATE = ResourceHelper.GetResourceAsString("/resources/vocabulary/sql/conceptSetInclude.sql");
31+
private final static String CONCEPT_SET_EXCLUDE_TEMPLATE = ResourceHelper.GetResourceAsString("/resources/vocabulary/sql/conceptSetExclude.sql");
3132

3233

3334
private ArrayList<Long> getConceptIds(ArrayList<Concept> concepts)
@@ -39,35 +40,51 @@ private ArrayList<Long> getConceptIds(ArrayList<Concept> concepts)
3940
return conceptIdList;
4041
}
4142

43+
44+
45+
private String buildConceptSetSubQuery (
46+
ArrayList<Concept> concepts,
47+
ArrayList<Concept> descendantConcepts
48+
)
49+
{
50+
ArrayList<String> queries = new ArrayList<>();
51+
if (concepts.size() > 0) {
52+
queries.add(StringUtils.replace(CONCEPT_SET_QUERY_TEMPLATE, "@conceptIds", StringUtils.join(getConceptIds(concepts), ",")));
53+
}
54+
if (descendantConcepts.size() > 0) {
55+
queries.add(StringUtils.replace(CONCEPT_SET_DESCENDANTS_TEMPLATE, "@conceptIds", StringUtils.join(getConceptIds(descendantConcepts), ",")));
56+
}
57+
58+
return StringUtils.join(queries, "UNION");
59+
60+
}
61+
62+
private String buildConceptSetMappedQuery (
63+
ArrayList<Concept> mappedConcepts,
64+
ArrayList<Concept> mappedDescendantConcepts
65+
) {
66+
String conceptSetQuery = buildConceptSetSubQuery(mappedConcepts, mappedDescendantConcepts);
67+
return StringUtils.replace(CONCEPT_SET_MAPPED_TEMPLATE, "@conceptsetQuery", conceptSetQuery);
68+
}
69+
4270
private String buildConceptSetQuery(
4371
ArrayList<Concept> concepts,
4472
ArrayList<Concept> descendantConcepts,
45-
ArrayList<Concept> excludeConcepts,
46-
ArrayList<Concept> excludeDescendantConcepts)
73+
ArrayList<Concept> mappedConcepts,
74+
ArrayList<Concept> mappedDesandantConcepts)
4775
{
4876
if (concepts.size() == 0)
4977
{
5078
return "select concept_id from @cdm_database_schema.CONCEPT where 0=1";
5179
}
52-
String conceptSetQuery = StringUtils.replace(CONCEPT_SET_QUERY_TEMPLATE, "@conceptIds",StringUtils.join(getConceptIds(concepts), ","));
53-
if (descendantConcepts.size() > 0) {
54-
String includeDescendantQuery = StringUtils.replace(CONCEPT_SET_DESCENDANTS_TEMPLATE, "@conceptIds", StringUtils.join(getConceptIds(descendantConcepts), ","));
55-
conceptSetQuery = StringUtils.replace(conceptSetQuery,"@descendantQuery", includeDescendantQuery);
56-
} else {
57-
conceptSetQuery = StringUtils.replace(conceptSetQuery, "@descendantQuery", "");
58-
}
59-
if (excludeConcepts.size() > 0)
80+
81+
String conceptSetQuery = buildConceptSetSubQuery(concepts, descendantConcepts);
82+
83+
if (mappedConcepts.size() > 0 || mappedDesandantConcepts.size() > 0)
6084
{
61-
String excludeClause = StringUtils.replace(CONCEPT_SET_EXCLUDE_TEMPLATE,"@conceptIds", StringUtils.join(getConceptIds(excludeConcepts),","));
62-
if (excludeDescendantConcepts.size() > 0){
63-
String excludeClauseDescendantQuery = StringUtils.replace(CONCEPT_SET_DESCENDANTS_TEMPLATE, "@conceptIds", StringUtils.join(getConceptIds(excludeDescendantConcepts), ","));
64-
excludeClause = StringUtils.replace(excludeClause, "@descendantQuery", excludeClauseDescendantQuery);
65-
} else {
66-
excludeClause = StringUtils.replace(excludeClause, "@descendantQuery", "");
67-
}
68-
conceptSetQuery += excludeClause;
85+
buildConceptSetMappedQuery(mappedConcepts,mappedDesandantConcepts);
86+
conceptSetQuery += "UNION\n" + buildConceptSetMappedQuery(mappedConcepts,mappedDesandantConcepts);
6987
}
70-
7188
return conceptSetQuery;
7289
}
7390

@@ -76,11 +93,11 @@ public String buildExpressionQuery(ConceptSetExpression expression)
7693
// handle included concepts.
7794
ArrayList<Concept> includeConcepts = new ArrayList<>();
7895
ArrayList<Concept> includeDescendantConcepts = new ArrayList<>();
79-
ArrayList<Concept> excludeConcepts = new ArrayList<>();
80-
ArrayList<Concept> excludeDescendantConcepts = new ArrayList<>();
81-
8296
ArrayList<Concept> includeMappedConcepts = new ArrayList<>();
8397
ArrayList<Concept> includeMappedDescendantConcepts = new ArrayList<>();
98+
99+
ArrayList<Concept> excludeConcepts = new ArrayList<>();
100+
ArrayList<Concept> excludeDescendantConcepts = new ArrayList<>();
84101
ArrayList<Concept> excludeMappedConcepts = new ArrayList<>();
85102
ArrayList<Concept> excludeMappedDescendantConcepts = new ArrayList<>();
86103

@@ -115,11 +132,11 @@ public String buildExpressionQuery(ConceptSetExpression expression)
115132

116133
// each ArrayList contains the concepts that are used in the sub-query of the codeset expression query
117134

118-
String conceptSetQuery = buildConceptSetQuery(includeConcepts, includeDescendantConcepts, excludeConcepts, excludeDescendantConcepts);
135+
String conceptSetQuery = StringUtils.replace(CONCEPT_SET_INCLUDE_TEMPLATE,"@includeQuery", buildConceptSetQuery(includeConcepts, includeDescendantConcepts, includeMappedConcepts, includeMappedDescendantConcepts));
119136

120-
if (includeMappedConcepts.size() > 0){
121-
String mappedConceptsQuery = buildConceptSetQuery(includeMappedConcepts, includeMappedDescendantConcepts, excludeMappedConcepts, excludeMappedDescendantConcepts);
122-
conceptSetQuery += "\nUNION\n" + StringUtils.replace(CONCEPT_SET_MAPPED_TEMPLATE, "@conceptsetQuery", mappedConceptsQuery);
137+
if (excludeConcepts.size() > 0){
138+
String excludeConceptsQuery = StringUtils.replace(CONCEPT_SET_EXCLUDE_TEMPLATE, "@excludeQuery", buildConceptSetQuery(excludeConcepts, excludeDescendantConcepts, excludeMappedConcepts, excludeMappedDescendantConcepts));
139+
conceptSetQuery += excludeConceptsQuery;
123140
}
124141

125142
return conceptSetQuery;

src/main/resources/resources/vocabulary/sql/conceptSetDescendants.sql

-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
UNION
2-
31
select c.concept_id
42
from @cdm_database_schema.CONCEPT c
53
join @cdm_database_schema.CONCEPT_ANCESTOR ca on c.concept_id = ca.descendant_concept_id
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
LEFT JOIN
22
(
3-
select concept_id from @cdm_database_schema.CONCEPT where concept_id in (@conceptIds)and invalid_reason is null
4-
@descendantQuery
3+
@excludeQuery
54
) E ON I.concept_id = E.concept_id
65
WHERE E.concept_id is null

src/main/resources/resources/vocabulary/sql/conceptSetExpression.sql

-5
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
select distinct I.concept_id FROM
2+
(
3+
@includeQuery
4+
) I
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
select concept_id from @cdm_database_schema.CONCEPT where concept_id in (@conceptIds)and invalid_reason is null

0 commit comments

Comments
 (0)