If the author was willing to learn Haskell there’s a library, esqueleto, which does exactly this. It’s better than the m4 solution in that the type system will ensure the query fragments are composed correctly with respect to the typing rules of Haskell.
The drawback is that query fragments written in esqueleto’s experimental DSL are a bit funny looking compared to the plain SQL you’re trying to write. And you’d have to learn a non-trivial amount of Haskell which is a tall order.
Using m4 to this purpose is an interesting, out of the box idea. I’m curious what challenges it has in practice.