1. A computer implemented method for processing data in a database management system implemented on a network of two or more nodes, each of the two or more nodes comprising a processor, a storage device, and a memory, the method comprising: receiving a query referencing a first user-defined table function, a second user-defined table function, and a table distributed across the two or more nodes; executing the first user-defined table function on each of the two or more nodes by processing records of the table residing on the respective nodes; generating a data sample per node as a result of executing the first user-defined table function on each of the two or more nodes; storing the generated data samples in a selected set of nodes; and executing the second user-defined table function on each of the selected set of nodes by processing records of the data samples residing on the respective nodes of the selected set of nodes to compute a user-defined aggregate.