I found the issue. I was using a custom SQL query in two different join-paths to get two subsets of the original query and then unioning them back together for the final output. It was fine until the output stage.
I fixed it by duplicating the custom SQL and using it as the source for the second path. It now outputs successfully.