Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Join filter queries using Intersect and Union #2742

Open
wants to merge 9 commits into
base: master
Choose a base branch
from
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2023-2024 Google LLC
* Copyright 2023-2025 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -2530,6 +2530,92 @@ class DatabaseImplTest {
assertThat(result.map { it.resource.logicalId }).containsExactly("100").inOrder()
}

@Test
fun search_patient_with_given_disjoint_and_has_diabetes() {
runBlocking {
val jane =
Patient().apply {
id = "jane-001"
addName(
HumanName().apply {
addGiven("Jane")
family = "Doe"
},
)
}
val john =
Patient().apply {
id = "john-001"
addName(
HumanName().apply {
addGiven("John")
family = "Doe"
},
)
}
val jade =
Patient().apply {
id = "jade-001"
addName(
HumanName().apply {
addGiven("Jade")
family = "Doe"
},
)
}

val diabetes1 =
Condition().apply {
subject = Reference("Patient/${jane.logicalId}")
code = CodeableConcept(Coding("http://snomed.info/sct", "44054006", "Diabetes"))
}
val diabetes2 =
Condition().apply {
subject = Reference("Patient/${john.logicalId}")
code = CodeableConcept(Coding("http://snomed.info/sct", "44054006", "Diabetes"))
}
val diabetes3 =
Condition().apply {
subject = Reference("Patient/${jade.logicalId}")
code = CodeableConcept(Coding("http://snomed.info/sct", "44054006", "Diabetes"))
}
database.insert(jane, jade, john, diabetes1, diabetes2, diabetes3)

val result =
database.search<Patient>(
Search(ResourceType.Patient)
.apply {
has<Condition>(Condition.SUBJECT) {
filter(
Condition.CODE,
{ value = of(Coding("http://snomed.info/sct", "44054006", "Diabetes")) },
)
}

filter(
Patient.GIVEN,
{
value = "John"
modifier = StringFilterModifier.MATCHES_EXACTLY
},
)

filter(
Patient.GIVEN,
{
value = "Jane"
modifier = StringFilterModifier.MATCHES_EXACTLY
},
)
operation = Operation.OR
}
.getQuery(),
)

assertThat(result.map { it.resource.logicalId }).containsExactly("jane-001", "john-001")
}
}

@Test
fun search_patient_return_single_patient_who_has_diabetic_careplan() = runBlocking {
val patient =
Expand Down Expand Up @@ -2723,78 +2809,79 @@ class DatabaseImplTest {
}

@Test
fun test_search_multiple_param_disjunction_covid_immunization_records() = runBlocking {
val resources =
listOf(
Immunization().apply {
id = "immunization-1"
vaccineCode =
CodeableConcept(
Coding(
"http://id.who.int/icd11/mms",
"XM1NL1",
"COVID-19 vaccine, inactivated virus",
),
)
status = Immunization.ImmunizationStatus.COMPLETED
},
Immunization().apply {
id = "immunization-2"
vaccineCode =
CodeableConcept(
Coding(
"http://id.who.int/icd11/mms",
"XM5DF6",
"COVID-19 vaccine, live attenuated virus",
),
)
status = Immunization.ImmunizationStatus.COMPLETED
},
Immunization().apply {
id = "immunization-3"
vaccineCode =
CodeableConcept(
Coding("http://id.who.int/icd11/mms", "XM6AT1", "COVID-19 vaccine, DNA based"),
)
status = Immunization.ImmunizationStatus.COMPLETED
},
Immunization().apply {
id = "immunization-4"
vaccineCode =
CodeableConcept(
Coding(
"http://hl7.org/fhir/sid/cvx",
"140",
"Influenza, seasonal, injectable, preservative free",
),
)
status = Immunization.ImmunizationStatus.COMPLETED
},
)
fun test_search_multiple_param_disjunction_covid_immunization_records() {
runBlocking {
val resources =
listOf(
Immunization().apply {
id = "immunization-1"
vaccineCode =
CodeableConcept(
Coding(
"http://id.who.int/icd11/mms",
"XM1NL1",
"COVID-19 vaccine, inactivated virus",
),
)
status = Immunization.ImmunizationStatus.COMPLETED
},
Immunization().apply {
id = "immunization-2"
vaccineCode =
CodeableConcept(
Coding(
"http://id.who.int/icd11/mms",
"XM5DF6",
"COVID-19 vaccine, live attenuated virus",
),
)
status = Immunization.ImmunizationStatus.COMPLETED
},
Immunization().apply {
id = "immunization-3"
vaccineCode =
CodeableConcept(
Coding("http://id.who.int/icd11/mms", "XM6AT1", "COVID-19 vaccine, DNA based"),
)
status = Immunization.ImmunizationStatus.COMPLETED
},
Immunization().apply {
id = "immunization-4"
vaccineCode =
CodeableConcept(
Coding(
"http://hl7.org/fhir/sid/cvx",
"140",
"Influenza, seasonal, injectable, preservative free",
),
)
status = Immunization.ImmunizationStatus.COMPLETED
},
)

database.insert(*resources.toTypedArray())
database.insert(*resources.toTypedArray())

val result =
database.search<Immunization>(
Search(ResourceType.Immunization)
.apply {
filter(
Immunization.VACCINE_CODE,
{ value = of(Coding("http://id.who.int/icd11/mms", "XM1NL1", "")) },
)
val result =
database.search<Immunization>(
Search(ResourceType.Immunization)
.apply {
filter(
Immunization.VACCINE_CODE,
{ value = of(Coding("http://id.who.int/icd11/mms", "XM1NL1", "")) },
)

filter(
Immunization.VACCINE_CODE,
{ value = of(Coding("http://id.who.int/icd11/mms", "XM5DF6", "")) },
)
operation = Operation.OR
}
.getQuery(),
)
filter(
Immunization.VACCINE_CODE,
{ value = of(Coding("http://id.who.int/icd11/mms", "XM5DF6", "")) },
)
operation = Operation.OR
}
.getQuery(),
)

assertThat(result.map { it.resource.vaccineCode.codingFirstRep.code })
.containsExactly("XM1NL1", "XM5DF6")
.inOrder()
assertThat(result.map { it.resource.vaccineCode.codingFirstRep.code })
.containsExactly("XM1NL1", "XM5DF6")
}
}

@Test
Expand Down
28 changes: 15 additions & 13 deletions engine/src/main/java/com/google/android/fhir/search/MoreSearch.kt
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2023-2024 Google LLC
* Copyright 2023-2025 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -364,17 +364,13 @@ internal fun Search.getQuery(
val sortArgs = join.args

val filterQuery = getFilterQueries()
val filterQueryStatement =
filterQuery.joinToString(separator = "${operation.logicalOperator} ") {
// spotless:off
"""
a.resourceUuid IN (
${it.query}
)

""".trimIndent()
// spotless:on
val filterQueryJoinOperator =
when (operation) {
Operation.OR -> "\nUNION\n"
Operation.AND -> "\nINTERSECT\n"
}
val filterQueryStatement =
filterQuery.joinToString(separator = filterQueryJoinOperator) { it.query.trimIndent() }
val filterQueryArgs = filterQuery.flatMap { it.args }

var limitStatement = ""
Expand All @@ -398,8 +394,14 @@ internal fun Search.getQuery(
val filterStatement =
listOf(filterQueryStatement, nestedQueryFilterStatement)
.filter { it.isNotBlank() }
.joinToString(separator = " AND ")
.ifBlank { "a.resourceType = ?" }
.joinToString(separator = "\nINTERSECT\n")
.takeIf { it.isNotBlank() }
?.let { """a.resourceUuid IN (
$it
)
""" }
?: "a.resourceType = ?"

val filterArgs = (filterQueryArgs + nestedQueryFilterArgs).ifEmpty { listOf(type.name) }

val whereArgs = mutableListOf<Any>()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2022-2024 Google LLC
* Copyright 2022-2025 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -214,15 +214,20 @@ internal fun List<NestedSearch>.nestedQuery(
return if (isEmpty()) {
null
} else {
val filterJoinOperator =
when (operation) {
Operation.OR -> "\nUNION\n"
Operation.AND -> "\nINTERSECT\n"
}

map { it.nestedQuery(type) }
.let { searchQueries ->
SearchQuery(
query =
searchQueries.joinToString(
prefix = "a.resourceUuid IN ",
separator = " ${operation.logicalOperator} a.resourceUuid IN",
separator = " $filterJoinOperator",
) { searchQuery ->
"(\n${searchQuery.query}\n) "
"\n${searchQuery.query}\n"
},
args = searchQueries.flatMap { it.args },
)
Expand Down
Loading
Loading