Flexible and Scalable Query Planning in Distributed and Heterogeneous Environments