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

Catch sqlite exceptions when filter entities #6601

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package org.odk.collect.entities.javarosa.filter

import android.database.sqlite.SQLiteException
import org.javarosa.core.model.CompareToNodeExpression
import org.javarosa.core.model.condition.EvaluationContext
import org.javarosa.core.model.condition.FilterStrategy
Expand Down Expand Up @@ -41,7 +42,11 @@ class LocalEntitiesFilterStrategy(entitiesRepository: EntitiesRepository) :
val query = xPathExpressionToQuery(predicate, sourceInstance, evaluationContext)

return if (query != null) {
queryToTreeReferences(query, sourceInstance)
try {
queryToTreeReferences(query, sourceInstance)
} catch (e: SQLiteException) {
next.get()
}
} else {
next.get()
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package org.odk.collect.entities.storage

import android.database.sqlite.SQLiteException
import org.odk.collect.entities.javarosa.parse.EntitySchema
import org.odk.collect.shared.Query

Expand Down Expand Up @@ -56,7 +57,7 @@ class InMemEntitiesRepository : EntitiesRepository {
EntitySchema.VERSION -> it.version.toString()
else -> it.properties.find { propertyName ->
propertyName.first == query.column
}?.second
}?.second ?: throw SQLiteException("No such column: ${query.column}")
}
fieldName == query.value
}
Expand All @@ -69,7 +70,7 @@ class InMemEntitiesRepository : EntitiesRepository {
EntitySchema.VERSION -> it.version.toString()
else -> it.properties.find { propertyName ->
propertyName.first == query.column
}?.second
}?.second ?: throw SQLiteException("No such column: ${query.column}")
}
fieldName != query.value
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -541,6 +541,121 @@ class LocalEntitiesFilterStrategyTest {
assertThat(fallthroughFilterStrategy.fellThrough, equalTo(false))
assertThat(instanceProvider.fullParsePerformed, equalTo(false))
}

@Test
fun `works correctly but not in the optimized way with question = expressions`() {
entitiesRepository.save("things", Entity.New("thing1", "Thing1"))

val scenario = Scenario.init(
"Secondary instance form",
html(
head(
title("Secondary instance form"),
model(
mainInstance(
t(
"data id=\"create-entity-form\"",
t("ref_question"),
t("question"),
)
),
t("instance id=\"things\" src=\"jr://file-csv/things.csv\""),
bind("/data/ref_question").type("string"),
bind("/data/question").type("string")
)
),
body(
select1Dynamic(
"/data/question",
"instance('things')/root/item[/data/ref_question='']",
"name",
"label"
)
)
),
controllerSupplier
)

val choices = scenario.choicesOf("/data/question").map { it.value }
assertThat(choices, containsInAnyOrder("thing1"))
assertThat(fallthroughFilterStrategy.fellThrough, equalTo(true))
}

@Test
fun `works correctly but not in the optimized way with question = undefined`() {
entitiesRepository.save("things", Entity.New("thing1", "Thing1"))

val scenario = Scenario.init(
"Secondary instance form",
html(
head(
title("Secondary instance form"),
model(
mainInstance(
t(
"data id=\"create-entity-form\"",
t("ref_question"),
t("question"),
)
),
t("instance id=\"things\" src=\"jr://file-csv/things.csv\""),
bind("/data/ref_question").type("string"),
bind("/data/question").type("string")
)
),
body(
select1Dynamic(
"/data/question",
"instance('things')/root/item[/data/ref_question=undefined]",
"name",
"label"
)
)
),
controllerSupplier
)

val choices = scenario.choicesOf("/data/question").map { it.value }
assertThat(choices, containsInAnyOrder("thing1"))
assertThat(fallthroughFilterStrategy.fellThrough, equalTo(true))
}

@Test
fun `works correctly but not in the optimized way with non existing property = expressions`() {
entitiesRepository.save("things", Entity.New("thing1", "Thing1"))

val scenario = Scenario.init(
"Secondary instance form",
html(
head(
title("Secondary instance form"),
model(
mainInstance(
t(
"data id=\"create-entity-form\"",
t("question"),
)
),
t("instance id=\"things\" src=\"jr://file-csv/things.csv\""),
bind("/data/question").type("string")
)
),
body(
select1Dynamic(
"/data/question",
"instance('things')/root/item[not_existing_property='value']",
"name",
"label"
)
)
),
controllerSupplier
)

val choices = scenario.choicesOf("/data/question").map { it.value }
assertThat(choices.isEmpty(), equalTo(true))
assertThat(fallthroughFilterStrategy.fellThrough, equalTo(true))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

works correctly in the optimized way -- what makes it optimized? This looks like it fell through which I thought would be non-optimized.

This test looks identical to https://github.com/getodk/collect/pull/6601/files#diff-4a25b4c0ba0780186c2edabe26d8518d4e3e6a022a482669373e2a71ceac6dfbR585 to me

In this test not_existing_property is the reference in the secondary instance and 'value' is a static value.

In the test above, undefined is the reference in the secondary instance and /data/ref_question is a static value that came from the form.

It's confusing because the order is different and the intended meaning is different! But I believe that from the system's standpoint they should be in the same category. If there's code somewhere that treats them as different we should dig into it, I think.

Copy link
Member Author

@grzesiek2010 grzesiek2010 Feb 14, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

works correctly in the optimized way -- what makes it optimized? This looks like it fell through which I thought would be non-optimized.

I made a mistake in the title it's not optimized of course. I will fix that.

If you set not_existing_property='value', it will always return an empty list of entities (because not_existing_property does not exist).
However, if you use undefined=/data/ref_question or 'value'=/data/ref_question (because we focus too much on the undefined case), the list won't be empty - as long as the expected value matches the one entered in the question.
Wouldn't it be enough to treat these two cases as distinct and have separate tests for each?

Copy link
Member

@lognaturel lognaturel Feb 14, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you set not_existing_property='value', it will always return an empty list of entities (because not_existing_property does not exist).

And with not_existing_property='', you should always get all Entities. That's the same as the undefined=/data/ref_question case when /data/ref_question has a blank value.

I sort of see the distinction that you're making, thanks for explaining it again, that was helpful. It doesn't hurt to have a few extra tests and I don't think there's a fundamental misunderstanding here. 👍

'value'=/data/ref_question

This one does definitely feel different from the other two to me. This should be identified as not an expression that can use the optimized filter strategy and bypass before it even gets there (maybe good to double check this?). The other two should be identified as candidates for the filter strategy and then be kicked back to the next one once the column is determined to be unknown.

}
}

private class FallthroughFilterStrategy : FilterStrategy {
Expand Down