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

Monadic Left Join Syntax does not work with clauses not immediately before #1063

Closed
deusaquilus opened this issue Apr 5, 2018 · 2 comments
Closed

Comments

@deusaquilus
Copy link
Collaborator

deusaquilus commented Apr 5, 2018

Version: (e.g. 2.3.3)
Module: (e.g. quill-sql)
Database: (e.g. any)

Let's say you have a query that looks like this:

case class Person(name: String, residesIn: String)
case class Country(id:Int, name: String)

quote {
      for {
        p <- query[Person]
        c <- query[Country].leftJoin(_.name == ct.residesIn)
      } yield (p, c)
}

This will work. However, as soon as you introduce something in front of the c <- query[Country]... after the p <- query[Person], for example:

quote {
      for {
        p <- query[Person]
        p1 <- query[Person] if (p.name == p1.name) // introduce something in between p and c
        c <- query[Country].leftJoin(_.name == p.residesIn)
      } yield (p.name, c.map(_.name))
}

Will yield the following error:

Error:(64, 16) exception during macro expansion: 
java.lang.IllegalArgumentException: requirement failed: Found an `ON` table reference of a table that is not available: Set(p). The `ON` condition can only use tables defined through explicit joins.
	at scala.Predef$.require(Predef.scala:224)
	at io.getquill.context.sql.idiom.VerifySqlQuery$$anonfun$io$getquill$context$sql$idiom$VerifySqlQuery$$loop$1$1.apply(VerifySqlQuery.scala:49)
	at io.getquill.context.sql.idiom.VerifySqlQuery$$anonfun$io$getquill$context$sql$idiom$VerifySqlQuery$$loop$1$1.apply(VerifySqlQuery.scala:39)
	at scala.collection.LinearSeqOptimized$class.foldLeft(LinearSeqOptimized.scala:124)
	at scala.collection.immutable.List.foldLeft(List.scala:84)
	at io.getquill.context.sql.idiom.VerifySqlQuery$.io$getquill$context$sql$idiom$VerifySqlQuery$$loop$1(VerifySqlQuery.scala:39)
	at io.getquill.context.sql.idiom.VerifySqlQuery$.verifyFlatJoins(VerifySqlQuery.scala:56)
	at io.getquill.context.sql.idiom.VerifySqlQuery$.verify(VerifySqlQuery.scala:61)
	at io.getquill.context.sql.idiom.VerifySqlQuery$.io$getquill$context$sql$idiom$VerifySqlQuery$$verify(VerifySqlQuery.scala:31)
	at io.getquill.context.sql.idiom.VerifySqlQuery$.apply(VerifySqlQuery.scala:27)
	at io.getquill.context.sql.idiom.SqlIdiom$class.translate(SqlIdiom.scala:30)
	at io.getquill.SQLServerDialect$.translate(SQLServerDialect.scala:37)
	at io.getquill.context.ContextMacro$class.translateStatic(ContextMacro.scala:51)
	at io.getquill.context.ContextMacro$class.translate(ContextMacro.scala:37)
	at io.getquill.context.ContextMacro$class.expand(ContextMacro.scala:24)
	at io.getquill.context.QueryMacro.expand(QueryMacro.scala:8)
	at io.getquill.context.QueryMacro.expandQueryWithMeta(QueryMacro.scala:45)
	at io.getquill.context.QueryMacro.expandQuery(QueryMacro.scala:20)
	at io.getquill.context.QueryMacro.runQuery(QueryMacro.scala:12)
    println(run(q))

Scastie can be found here:
https://scastie.scala-lang.org/deusaquilus/dXxO5hyRRbuAf8Hgz5aghQ

Workaround

Re-arrange the query so that leftJoins only use variables immediately before.

@getquill/maintainers

Edit: More readable.

@deusaquilus
Copy link
Collaborator Author

I see this as addressed in #636. In loop inside verifyFlatJoins inside VerifySqlQuery.scala, isn't it supposed to be:

        case (av, TableContext(_, alias)) => av ++ Set(alias) // i.e. the ++ is added
        case (av, InfixContext(_, alias)) => av ++ Set(alias)
        case (av, QueryContext(_, alias)) => av ++ Set(alias)

As far as I understand, the aliases are supposed to be accumulated...

@deusaquilus
Copy link
Collaborator Author

Looks like I didn't have a proper understanding of the issue. Closing.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant