Using set-returning functions for some parts of queries