Last active
November 4, 2018 20:39
-
-
Save tkroman/5787261ab751e2eb2ff4d4e223773824 to your computer and use it in GitHub Desktop.
ch join builder from homegrown ast
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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