Skip to content

Commit

Permalink
[DT-774][risk=no] some code cleanup before starting (#8419)
Browse files Browse the repository at this point in the history
* DT-774 some code cleanup before starting

* DT-774 spotless

* DT-774 fixing test case

* DT-774 spotless
  • Loading branch information
freemabd authored Mar 6, 2024
1 parent e0ced94 commit f8e8edd
Show file tree
Hide file tree
Showing 4 changed files with 152 additions and 120 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -177,15 +177,15 @@ public List<Criteria> findCriteriaByDomainIdAndConceptIds(
.findCriteriaByDomainIdAndStandardAndConceptIds(domainId, false, sourceIds)
.stream()
.map(cohortBuilderMapper::dbModelToClient)
.collect(Collectors.toList()));
.toList());
}
if (!standardConceptIds.isEmpty()) {
criteriaList.addAll(
cbCriteriaDao
.findCriteriaByDomainIdAndStandardAndConceptIds(domainId, true, standardIds)
.stream()
.map(cohortBuilderMapper::dbModelToClient)
.collect(Collectors.toList()));
.toList());
}
return criteriaList;
}
Expand Down Expand Up @@ -419,8 +419,7 @@ private List<CardCount> findDomainCountsV2(
.collect(Collectors.toList());

// filter strDomains to remove domains that have a cardCount by domain
domainNames.removeAll(
cardCounts.stream().map(DbCardCount::getDomainId).collect(Collectors.toList()));
domainNames.removeAll(cardCounts.stream().map(DbCardCount::getDomainId).toList());

cardCounts.addAll(cbCriteriaDao.findDomainCounts(searchTerm, standard, domainNames));

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Set;
import java.util.stream.Collectors;
import org.apache.commons.collections4.CollectionUtils;
Expand Down Expand Up @@ -90,47 +91,50 @@ public final class SearchGroupItemQueryBuilder {
private static final String UNION_DISTINCT_TEMPLATE = "UNION DISTINCT\n";
private static final String DESC = " DESC";
private static final String BASE_SQL =
"SELECT DISTINCT person_id, entry_date, concept_id\n"
+ "FROM `${projectId}.${dataSetId}.cb_search_all_events`\n"
+ "WHERE ";
"""
SELECT DISTINCT person_id, entry_date, concept_id
FROM `${projectId}.${dataSetId}.cb_search_all_events`
WHERE\s""";
private static final String STANDARD_SQL = "is_standard = %s";
private static final String CONCEPT_ID_UNNEST_SQL = "concept_id IN unnest(%s)";
private static final String CONCEPT_ID_IN_SQL = "concept_id IN";
private static final String STANDARD_OR_SOURCE_SQL =
CONCEPT_ID_UNNEST_SQL + AND + STANDARD_SQL + "\n";
public static final String CHILD_LOOKUP_SQL =
" (SELECT DISTINCT c.concept_id\n"
+ "FROM `${projectId}.${dataSetId}.cb_criteria` c\n"
+ "JOIN (select cast(cr.id as string) as id\n"
+ "FROM `${projectId}.${dataSetId}.cb_criteria` cr\n"
+ "WHERE concept_id IN unnest(%s)\n"
+ "AND full_text LIKE '%%_rank1]%%') a\n"
+ "ON (c.path LIKE CONCAT('%%.', a.id, '.%%') OR c.path LIKE CONCAT('%%.', a.id) OR c.path LIKE CONCAT(a.id, '.%%') OR c.path = a.id)\n"
+ "WHERE is_standard = %s\n"
+ "AND is_selectable = 1)";
"""
(SELECT DISTINCT c.concept_id
FROM `${projectId}.${dataSetId}.cb_criteria` c
JOIN (SELECT CAST(cr.id as string) AS id
FROM `${projectId}.${dataSetId}.cb_criteria` cr
WHERE concept_id IN unnest(%s)
AND full_text LIKE '%%_rank1]%%'
) a ON (c.path LIKE CONCAT('%%.', a.id, '.%%') OR c.path LIKE CONCAT('%%.', a.id) OR c.path LIKE CONCAT(a.id, '.%%') OR c.path = a.id)
WHERE is_standard = %s
AND is_selectable = 1)""";
public static final String DRUG_CHILD_LOOKUP_SQL =
" (SELECT DISTINCT ca.descendant_id\n"
+ "FROM `${projectId}.${dataSetId}.cb_criteria_ancestor` ca\n"
+ "JOIN (select distinct c.concept_id\n"
+ "FROM `${projectId}.${dataSetId}.cb_criteria` c\n"
+ "JOIN (select cast(cr.id as string) as id\n"
+ "FROM `${projectId}.${dataSetId}.cb_criteria` cr\n"
+ "WHERE concept_id IN unnest(%s)\n"
+ "AND full_text LIKE '%%_rank1]%%') a\n"
+ "ON (c.path LIKE CONCAT('%%.', a.id, '.%%') OR c.path LIKE CONCAT('%%.', a.id) OR c.path LIKE CONCAT(a.id, '.%%') OR c.path = a.id)\n"
+ "WHERE is_standard = %s\n"
+ "AND is_selectable = 1) b ON (ca.ancestor_id = b.concept_id))";
"""
(SELECT DISTINCT ca.descendant_id
FROM `${projectId}.${dataSetId}.cb_criteria_ancestor` ca
JOIN (SELECT DISTINCT c.concept_id
FROM `${projectId}.${dataSetId}.cb_criteria` c
JOIN (SELECT CAST(cr.id as string) AS id
FROM `${projectId}.${dataSetId}.cb_criteria` cr
WHERE concept_id IN unnest(%s)
AND full_text LIKE '%%_rank1]%%'
) a ON (c.path LIKE CONCAT('%%.', a.id, '.%%') OR c.path LIKE CONCAT('%%.', a.id) OR c.path LIKE CONCAT(a.id, '.%%') OR c.path = a.id)
WHERE is_standard = %s
AND is_selectable = 1) b ON (ca.ancestor_id = b.concept_id))""";
public static final String QUESTION_LOOKUP_SQL =
"question_concept_id IN (SELECT DISTINCT concept_id\n"
+ "FROM `${projectId}.${dataSetId}.cb_criteria` c\n"
+ "JOIN (select cast(cr.id as string) as id\n"
+ "FROM `${projectId}.${dataSetId}.cb_criteria` cr\n"
+ "WHERE concept_id IN (@surveyConceptIds)\n"
+ "AND domain_id = 'SURVEY') a\n"
+ "ON (c.path like CONCAT('%', a.id, '.%'))\n"
+ "WHERE domain_id = 'SURVEY'\n"
+ "AND type = 'PPI'\n"
+ "AND subtype = 'QUESTION')";
"""
question_concept_id IN (SELECT DISTINCT concept_id
FROM `${projectId}.${dataSetId}.cb_criteria` c
JOIN (SELECT CAST(cr.id as string) AS id
FROM `${projectId}.${dataSetId}.cb_criteria` cr
WHERE concept_id IN (@surveyConceptIds)
AND domain_id = 'SURVEY') a ON (c.path like CONCAT('%', a.id, '.%'))
WHERE domain_id = 'SURVEY'
AND type = 'PPI'
AND subtype = 'QUESTION')""";
private static final String PARENT_STANDARD_OR_SOURCE_SQL =
CONCEPT_ID_IN_SQL + CHILD_LOOKUP_SQL + AND + STANDARD_SQL + "\n";
private static final String DRUG_SQL =
Expand All @@ -156,25 +160,34 @@ public final class SearchGroupItemQueryBuilder {
"temp1.person_id = temp2.person_id AND temp1.entry_date between "
+ "DATE_SUB(temp2.entry_date, INTERVAL %s DAY) and DATE_ADD(temp2.entry_date, INTERVAL %s DAY)\n";
private static final String TEMPORAL_EXIST =
"SELECT temp1.person_id\n"
+ "FROM (%s) temp1\n"
+ "WHERE EXISTS (SELECT 1\n"
+ "FROM (%s) temp2\n"
+ "WHERE (%s))\n";
"""
SELECT temp1.person_id
FROM (%s) temp1
WHERE EXISTS (SELECT 1
FROM (%s) temp2
WHERE (%s))
""";
private static final String TEMPORAL_JOIN =
"SELECT temp1.person_id\n"
+ "FROM (%s) temp1\n"
+ "JOIN (SELECT person_id, visit_occurrence_id, entry_date\n"
+ "FROM (%s)\n"
+ ") temp2 on (%s)\n";
"""
SELECT temp1.person_id
FROM (%s) temp1
JOIN (SELECT person_id, visit_occurrence_id, entry_date
FROM (%s)
) temp2 on (%s)
""";
private static final String TEMPORAL_SQL =
"SELECT person_id, visit_occurrence_id, entry_date%s\n"
+ "FROM `${projectId}.${dataSetId}.cb_search_all_events`\n"
+ "WHERE %s";
"""
SELECT person_id, visit_occurrence_id, entry_date%s
FROM `${projectId}.${dataSetId}.cb_search_all_events`
WHERE %s""";
private static final String RANK_1_SQL =
", RANK() OVER (PARTITION BY person_id ORDER BY entry_date%s) rn";
private static final String TEMPORAL_RANK_1_SQL =
"SELECT person_id, visit_occurrence_id, entry_date\n" + "FROM (%s) a\n" + "WHERE rn = 1\n";
"""
SELECT person_id, visit_occurrence_id, entry_date
FROM (%s) a
WHERE rn = 1
""";

// sql parts to help construct Modifiers BigQuery sql
private static final String MODIFIER_SQL_TEMPLATE =
Expand All @@ -189,26 +202,42 @@ public final class SearchGroupItemQueryBuilder {

// sql parts to help construct demographic BigQuery sql
private static final String DEC_SQL =
"EXISTS (\n"
+ "SELECT 'x' FROM `${projectId}.${dataSetId}.death` d\n"
+ "WHERE d.person_id = p.person_id)\n";
"""
EXISTS (
SELECT 'x'
FROM `${projectId}.${dataSetId}.death` d
WHERE d.person_id = p.person_id
)
""";
private static final String DEMO_BASE =
"SELECT person_id\n" + "FROM `${projectId}.${dataSetId}.person` p\nWHERE\n";
"""
SELECT person_id
FROM `${projectId}.${dataSetId}.person` p
WHERE
""";
private static final String AGE_SQL =
"SELECT person_id\n"
+ "FROM `${projectId}.${dataSetId}.cb_search_person` p\nWHERE %s %s %s\n";
"""
SELECT person_id
FROM `${projectId}.${dataSetId}.cb_search_person` p
WHERE %s %s %s
""";
private static final String AGE_DEC_SQL = "AND NOT " + DEC_SQL;
private static final String DEMO_IN_SQL = "%s IN unnest(%s)\n";
private static final String HAS_DATA_SQL =
"SELECT person_id\n" + "FROM `${projectId}.${dataSetId}.cb_search_person` p\nWHERE %s = 1\n";
"""
SELECT person_id
FROM `${projectId}.${dataSetId}.cb_search_person` p
WHERE %s = 1
""";
private static final String CB_SEARCH_ALL_EVENTS_WHERE =
"SELECT person_id FROM `${projectId}.${dataSetId}.cb_search_all_events`\nWHERE ";
private static final String PERSON_ID_IN = "person_id IN (";
private static final String VARIANT_SQL =
"SELECT person_id\n"
+ "FROM `${projectId}.${dataSetId}.cb_variant_to_person`\n"
+ "CROSS JOIN UNNEST(person_ids) AS person_id\n"
+ "WHERE vid IN unnest(%s)";
"""
SELECT person_id
FROM `${projectId}.${dataSetId}.cb_variant_to_person`
CROSS JOIN UNNEST(person_ids) AS person_id
WHERE vid IN unnest(%s)""";

/** Build the innermost sql using search parameters, modifiers and attributes. */
public static void buildQuery(
Expand Down Expand Up @@ -514,25 +543,25 @@ private static String processAttributeSql(
String versionParam;
List<Long> conceptIds =
parameter.getAttributes().stream()
.filter(attr -> attr.getConceptId() != null)
.map(Attribute::getConceptId)
.collect(Collectors.toList());
.filter(Objects::nonNull)
.toList();
List<Attribute> cats =
parameter.getAttributes().stream()
.filter(attr -> attr.getName().equals(AttrName.CAT))
.collect(Collectors.toList());
.toList();
List<Attribute> nums =
parameter.getAttributes().stream()
.filter(attr -> attr.getName().equals(AttrName.NUM))
.collect(Collectors.toList());
.toList();
List<Attribute> any =
parameter.getAttributes().stream()
.filter(attr -> attr.getName().equals(AttrName.ANY))
.collect(Collectors.toList());
.toList();
List<Attribute> versions =
parameter.getAttributes().stream()
.filter(attr -> attr.getName().equals(AttrName.SURVEY_VERSION_CONCEPT_ID))
.collect(Collectors.toList());
.toList();
String standardParam =
QueryParameterUtil.addQueryParameterValue(
queryParams, QueryParameterValue.int64(parameter.isStandard() ? 1 : 0));
Expand Down Expand Up @@ -722,15 +751,12 @@ private static void addParamValueAndFormat(
String standardOrSourceParam =
QueryParameterUtil.addQueryParameterValue(
queryParams, QueryParameterValue.int64(standardOrSource));
List<Long> conceptIds =
searchParameters.stream().map(SearchParameter::getConceptId).collect(Collectors.toList());
List<Long> conceptIds = searchParameters.stream().map(SearchParameter::getConceptId).toList();

Map<Boolean, List<SearchParameter>> parentsAndChildren =
searchParameters.stream().collect(Collectors.partitioningBy(SearchParameter::isGroup));
List<Long> parents =
parentsAndChildren.get(true).stream()
.map(SearchParameter::getConceptId)
.collect(Collectors.toList());
parentsAndChildren.get(true).stream().map(SearchParameter::getConceptId).toList();

String conceptIdsParam =
QueryParameterUtil.addQueryParameterValue(
Expand All @@ -754,9 +780,7 @@ private static void addParamValueAndFormat(
/** Helper method to return a modifier. */
private static Modifier getModifier(List<Modifier> modifiers, ModifierType modifierType) {
List<Modifier> modifierList =
modifiers.stream()
.filter(modifier -> modifier.getName().equals(modifierType))
.collect(Collectors.toList());
modifiers.stream().filter(modifier -> modifier.getName().equals(modifierType)).toList();
if (modifierList.isEmpty()) {
return null;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -121,26 +121,32 @@ public static List<String> getDisplayNameList() {
private static final String NA = "n/a";

private static final String CONTIG_POSITION_SQL =
"WHERE vid IN (\n"
+ "SELECT vid\n"
+ "FROM `${projectId}.${dataSetId}.cb_variant_attribute_contig_position`\n"
+ "WHERE contig = @contig\n"
+ "AND position BETWEEN @start AND @end\n"
+ ")\n";
"""
WHERE vid IN (
SELECT vid
FROM `${projectId}.${dataSetId}.cb_variant_attribute_contig_position`
WHERE contig = @contig
AND position BETWEEN @start AND @end
)
""";

private static final String GENE_SQL =
"WHERE vid IN (\n"
+ "SELECT vid\n"
+ "FROM `${projectId}.${dataSetId}.cb_variant_attribute_genes`\n"
+ "WHERE gene_symbol = @gene\n"
+ ")\n";
"""
WHERE vid IN (
SELECT vid
FROM `${projectId}.${dataSetId}.cb_variant_attribute_genes`
WHERE gene_symbol = @gene
)
""";

private static final String RS_NUMBER_SQL =
"WHERE vid IN (\n"
+ "SELECT vid\n"
+ "FROM `${projectId}.${dataSetId}.cb_variant_attribute_rs_number`\n"
+ "WHERE rs_number = @rs_number\n"
+ ")\n";
"""
WHERE vid IN (
SELECT vid
FROM `${projectId}.${dataSetId}.cb_variant_attribute_rs_number`
WHERE rs_number = @rs_number
)
""";

private static final String FILTERS_SQL =
"WITH genes AS (\n"
Expand Down Expand Up @@ -345,11 +351,12 @@ private static Map<String, QueryParameterValue> generateParams(
String searchTerm = filters.getSearchTerm();
Map<String, QueryParameterValue> params = new HashMap<>();
switch (SearchTermType.fromValue(searchTerm)) {
case VID:
case VID -> {
// vid SQL only returns 1 variant so no need to add filter params
params.put("vid", QueryParameterValue.string(searchTerm.toUpperCase()));
return params;
case CONTIG:
}
case CONTIG -> {
// example search term for contig -> chr13:32355000-32375000
String[] chr = searchTerm.split(":");
String[] position = chr[1].split("-");
Expand All @@ -364,7 +371,8 @@ private static Map<String, QueryParameterValue> generateParams(
}
generateFilterParams(filters, params);
return params;
case GENE:
}
case GENE -> {
params.put("gene", QueryParameterValue.string(searchTerm.toUpperCase()));
if (limit != null) {
params.put("limit", QueryParameterValue.int64(limit));
Expand All @@ -374,7 +382,8 @@ private static Map<String, QueryParameterValue> generateParams(
}
generateFilterParams(filters, params);
return params;
case RS_NUMBER:
}
case RS_NUMBER -> {
params.put("rs_number", QueryParameterValue.string(searchTerm.toLowerCase()));
if (limit != null) {
params.put("limit", QueryParameterValue.int64(limit));
Expand All @@ -384,8 +393,8 @@ private static Map<String, QueryParameterValue> generateParams(
}
generateFilterParams(filters, params);
return params;
default:
throw new BadRequestException("Search term not supported");
}
default -> throw new BadRequestException("Search term not supported");
}
}

Expand All @@ -396,17 +405,15 @@ private static void generateFilterParams(
if (isNotEmpty(consequences)) {
// Don't change the source consequence list as it's used for multiple queries.
// Generate a new list without n/a to exclude from the IN clause.
List<String> cons =
consequences.stream().filter(s -> !s.equals(NA)).collect(Collectors.toList());
List<String> cons = consequences.stream().filter(s -> !s.equals(NA)).toList();
for (int i = 0; i < cons.size(); i++) {
params.put("consStr" + i, QueryParameterValue.string("%" + cons.get(i) + "%"));
}
}
if (isNotEmpty(clinicalSigns)) {
// Don't change the source consequence list as it's used for multiple queries.
// Generate a new list without n/a to exclude from the IN clause.
List<String> clinSigns =
clinicalSigns.stream().filter(s -> !s.equals(NA)).collect(Collectors.toList());
List<String> clinSigns = clinicalSigns.stream().filter(s -> !s.equals(NA)).toList();
for (int i = 0; i < clinSigns.size(); i++) {
params.put("clinStr" + i, QueryParameterValue.string("%" + clinSigns.get(i) + "%"));
}
Expand Down
Loading

0 comments on commit f8e8edd

Please sign in to comment.