-
Notifications
You must be signed in to change notification settings - Fork 29
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
Values clause does not support empty sequence #63
Comments
one possible solution I see is method |
I don't have a lot of insight on this but I'll share my thoughts:
|
So if |
I couldn't find a case in our codebase where "erasing" the condition would be the desired semantic. If anything, I think it would be both more surprising than the current behavior and lead to subtle silent bugs. Instead, if we want to avoid the surprise of a runtime exception, I would propose replacing It is the equivalent of |
@aboisvert having a case class that represents some filters: case class Filters(a: A, b: Seq[B])
Table.select
.filter(_.a === filters.a)
.filter(t => db.values(filters.b).contains(t.b)) instead i need to do this: val query1 = Table.select.filter(_.a === filters.a)
val query2 = if(filters.b.nonEmpty) {
query1.filter(t => db.values(filters.b).contains(t.b))
else {
query1
} simple example, but if I will need to manually handle more dead branches, for example if |
We also do this (conditional filtering) a lot in our codebase, and we have an extension method called extension [Q, R](select: Select[Q, R])
/**
* Filters this [[Select]] with the given query predicate, if the condition is true
*/
def filterIf(condition: Boolean)(queryPredicate: Q => Expr[Boolean]): Select[Q, R] =
if condition then select.filter(queryPredicate) else select Taking your example as an illustration, val query = Table.select
.filter(_.a === filters.a)
.filterIf(filters.b.nonEmpty)(t => Values(filters.b).contains(t.b)) If this pattern is common enough for you, you can even shorten it to something like, val query = Table.select
.filter(_.a === filters.a)
.filterIfContains(_.b, filters.b) which would be another extension method or addition to the /**
* Filters this [[Select]] based on the given expression matching any of the given values.
* Does not filter if values is empty.
*/
def filterIfContains[T](expr: Q => Expr[T], values: Seq[T])(using
Queryable.Row[T, T]
): Select[Q, R] =
if values.isEmpty then select
else select.filter(q => Values[T, T](values).contains(expr(q))) (just a sketch, this may not compile or run correctly) |
@aboisvert I am 100% happy with having those as a helper methods on select in std. I guess PR will follow |
I got an error:
assertion failed: `Values` clause does not support empty sequence
when trying to do a
Table.select.filter(t => db.values(ids).contains(t.id))
It's rather combersome, because i will need to eliminate all the branches with empty sequence myself.
Is it not supported by design or is it not supported YET ?
The text was updated successfully, but these errors were encountered: