     query
     (add-error query :group "You must have a select clause to use grouping.")))
 
+(defn- convert-group-to-mongo-form
+  [group]
+  (if (coll? group)
+    (str "__" (str/join "." (map name group)))
+    group))
+
 (defn- validate-group-fields
-  [query group metadata slice]
-  (let [group (map (fn [g] (if (coll? g)
-                             (str "__" (str/join "." (map name g)))
-                             g)) group)]
-    (reduce #(validate-field %1 :group metadata slice %2) query group)))
+  [query group]
+  (let [group (map convert-group-to-mongo-form group)]
+    (reduce #(validate-field %1 :group %2) query group)))
 
 (defn- validate-group-only-group-dimensions
-  [query group dimensions]
-  (let [dimensions (set dimensions)]
+  [{:keys [slicedef] :as query} group]
+  (let [dimensions (set (:dimensions slicedef))]
     (reduce
      (fn [query field]
        (let [field (name field)]

             (fnil #(conj % message) (vector))))

(defn validate-field
  [query clause metadata slice field]
  (let [field (name field)]
    (if (valid-field? metadata slice field)
      query
      (add-error query clause (str "\"" field "\" is not a valid field.")))))


(defn- validate-select-fields
  [query metadata slice select]
  (let [fields (map (comp name #(cond
                                 (:aggregation %) (second (:aggregation %))
                                 :default (:select %))) select)]
    (reduce #(validate-field %1 :select metadata slice %2) query fields)))

(defn- validate-select-no-aggregations-without-group
  [query select]

     query (map first-or-identity group))))

(defn- validate-group
  [query]
  (if-let [group (:group query)]
    (try
      (let [group (parse parser/group-expr group)]
        (-> query
            validate-group-requires-select
            (validate-group-fields group)
            (validate-group-only-group-dimensions group)))
      (catch Exception e
        (log/error "Exception:" (class e) e)
        (add-error query :group "Could not parse this clause.")))
