move flatten util into query.go