Skip to content

Commit

Permalink
Refactor joining nested queries to search filter queries
Browse files Browse the repository at this point in the history
  • Loading branch information
LZRS committed Dec 16, 2024
1 parent a0f5efb commit 5d34d06
Show file tree
Hide file tree
Showing 3 changed files with 169 additions and 8 deletions.
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
Expand Up @@ -366,14 +366,8 @@ internal fun Search.getQuery(
var filterStatement = ""
val filterArgs = mutableListOf<Any>()
val nestedSearchQuery = nestedSearches.nestedQuery(type, operation)
val filterQuery =
getFilterQueries().let {
if (nestedSearchQuery != null) {
it + nestedSearchQuery
} else {
it
}
}
val filterQuery = getFilterQueries() + (nestedSearchQuery?.let { listOf(it) } ?: emptyList())

val filterJoinOperator =
when (operation) {
Operation.OR -> "\nUNION\n"
Expand Down
81 changes: 81 additions & 0 deletions engine/src/test/java/com/google/android/fhir/search/SearchTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -1961,6 +1961,87 @@ class SearchTest {
)
}

@Test
fun search_patient_multiple_given_disjoint_has_condition_diabetes() {
val query =
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(query.query)
.isEqualTo(
"""
SELECT a.resourceUuid, a.serializedResource
FROM ResourceEntity a
WHERE a.resourceType = ?
AND a.resourceUuid IN (
SELECT resourceUuid FROM StringIndexEntity
WHERE resourceType = ? AND index_name = ? AND index_value = ?
UNION
SELECT resourceUuid FROM StringIndexEntity
WHERE resourceType = ? AND index_name = ? AND index_value = ?
INTERSECT
SELECT resourceUuid
FROM ResourceEntity a
WHERE a.resourceType = ? AND a.resourceId IN (
SELECT substr(a.index_value, 9)
FROM ReferenceIndexEntity a
WHERE a.resourceType = ? AND a.index_name = ?
AND a.resourceUuid IN (
SELECT resourceUuid FROM TokenIndexEntity
WHERE resourceType = ? AND index_name = ? AND (index_value = ? AND IFNULL(index_system,'') = ?)
)
)
)
"""
.trimIndent(),
)

assertThat(query.args)
.isEqualTo(
listOf(
"Patient",
"Patient",
"given",
"John",
"Patient",
"given",
"Jane",
"Patient",
"Condition",
"subject",
"Condition",
"code",
"44054006",
"http://snomed.info/sct",
),
)
}

@Test
fun practitioner_has_patient_has_condition_diabetes_and_hypertension() {
val query =
Expand Down

0 comments on commit 5d34d06

Please sign in to comment.