Fix buildQueryDefinition for legacy queries.