Can you replace the FlatMap function by a Filter function?
Filter is semantically richer (the optimizer knows what is happening).