              (distincto q)
              (everyg (partial fieldo query) q)
              (everyg (fn [i]
-                       (fields-sortedo query (cols i) (cols (inc i))))
+                       (fields< query (cols i) (cols (inc i))))
                      (range 0 (dec num-cols)))))))
 
 

;;     C.  Field Name
;;         When two Fields have the same :position and :special_type "group", fall back to sorting Fields alphabetically by name.
;;         This is arbitrary, but it makes the QP deterministic by keeping the results in a consistent order, which makes it testable.
(defn- fields-sortedo [query f1 f2]
  (fresh [g1 g2]
    (field-groupo query f1 g1)
    (field-groupo query f2 g2)

       (membero field fields-fields)))

(defn- aggregate-fieldo [{{ag-type :aggregation-type, ag-field :field} :aggregation} field]
  (all (== field (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
                                              ag-type)))))
       (membero ag-type [:count :avg :sum :stddev :distinct])))

(defn- valid-nameo [{:keys [result-keys]} field]
  (fresh [field-name]
