Skip to content

Instantly share code, notes, and snippets.

@tkroman
Last active November 4, 2018 20:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tkroman/5787261ab751e2eb2ff4d4e223773824 to your computer and use it in GitHub Desktop.
Save tkroman/5787261ab751e2eb2ff4d4e223773824 to your computer and use it in GitHub Desktop.
ch join builder from homegrown ast
def join(queries: NonEmptyList[TopLevelQuery]): TopLevelQuery = {
val size: Int = queries.size
if (size == 1) {
queries.head
} else {
queries
.reduceRight { case (q, acc) =>
acc.map { acc =>
val fiels: List[String] =
acc.select.fields
TopLevelQuery(
Select(fiels),
Some(From.Subquery(
q.copy(join = Some(Join(acc, fiels)))
))
)
}
}
.value
// Can be a minor optimization
// but i don't feel like it now:
//
// tlq.from match {
// case Some(x) => x match {
// case From.Table(_) =>
// throw new IllegalStateException(s"From=Table, this can't happen [$tlq]. @rt")
// case From.Subquery(tree) =>
// tree
// }
// case None =>
// throw new IllegalStateException(s"From=None, this can't happen [$tlq]. @rt")
// }
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment