Fixed infinite loop when skipping bad aggregate query. Tolerate flattening an unused relation.