 
 (defn- maybe-create-ag-field [{{ag-type :aggregation-type, ag-field :field} :aggregation}]
   (when (contains? #{:avg :count :distinct :stddev :sum} ag-type)
-    (-> (if (contains? #{:count :distinct} ag-type)
-          {:base-type    :IntegerField
-           :field-name   "count"
-           :special-type :number}
-          (-> ag-field
-              (select-keys [:base-type :special-type])
-              (assoc :field-name (if (= ag-type :distinct) "count"
-                                     (name ag-type)))))
-        (assoc :group (field-groups :aggregation)))))
+    [(if (contains? #{:count :distinct} ag-type)
+        {:base-type    :IntegerField
+         :field-name   "count"
+         :special-type :number}
+        (-> ag-field
+            (select-keys [:base-type :special-type])
+            (assoc :field-name (if (= ag-type :distinct) "count"
+                                   (name ag-type)))))]))
 
 (defn- query-add-info [query results]
   (let [result-keys (vec (keys (first results)))
-        fields      (for [field (concat (for [[i f] (map-indexed vector (flatten-collect-fields (:breakout query)))]
-                                          (assoc f :group          (field-groups :breakout)
-                                                   :group-position i))
-                                        (for [[i f] (map-indexed vector [(maybe-create-ag-field query)])]
-                                          (assoc f :group          (field-groups :aggregation)
-                                                   :group-position i))
-                                        (for [[i f] (map-indexed vector (when-not (:fields-is-implicit query)
-                                                                          (flatten-collect-fields (:fields query))))]
-                                          (assoc f :group          (field-groups :explicit-fields)
-                                                   :group-position i))
-                                        (for [[i {:keys [position special-type], :as f}] (map-indexed vector (sort-by :field-name (flatten-collect-fields query)))]
-                                          (assoc f :group          (field-groups :other)
-                                                   :group-position (+ (* 1000 position)
-                                                                      (* 100  (or (special-type-groups special-type)
-                                                                                  (special-type-groups :other)))
-                                                                      i))))]
-                      (-> field
-                          (assoc :field-name (keyword (:field-name field)))
-                          (dissoc :parent :parent-id :table-name)))]
+        fields      (apply concat (for [[group-name fields] [[:breakout        (flatten-collect-fields (:breakout query))]
+                                                             [:aggregation     (maybe-create-ag-field query)]
+                                                             [:explicit-fields (when-not (:fields-is-implicit query)
+                                                                                 (flatten-collect-fields (:fields query)))]
+                                                             [:other           (for [field (sort-by :field-name (flatten-collect-fields query))]
+                                                                                 (assoc field :special-type-group (or (special-type-groups (:special-type field))
+                                                                                                                      (special-type-groups :other))))]]]
+                                    (for [[i field] (map-indexed vector fields)]
+                                      (-> field
+                                          (assoc :group          (field-groups group-name)
+                                                 :group-position i
+                                                 :field-name     (keyword (:field-name field)))
+                                          (dissoc :parent :parent-id :table-name)))))]
     (assoc query
            :result-keys  result-keys
            :query-fields (sort-by :group (for [k result-keys]

                                :field-name   :name
                                :special-type :special_type
                                :table-id     :table_id})
             (dissoc :position :group :group-position))))

(defn- add-fields-extra-info
  "Add `:extra_info` about `ForeignKeys` to `Fields` whose `special_type` is `:fk`."

       :columns (mapv name columns)
       :rows    (for [row results]
                  (mapv row columns))})))
