 
 (let [make-fn (fn [aggs] (fn [query] (if-not aggs query (assoc query :aggs aggs))))]
   (def aggregations
-    {:es-count
+    {:count
      (make-fn nil)
   
      :paid-total-stats-by-time-of-day 

  (defn parse-aggs [agg]
    (loop [agg agg result ()]
      (if-not agg
        (apply merge-with concat result)
        (let [_         (assert (= 1 (count agg)) "Currently only supports one aggregation / hierarchy level")
              [key agg] (-> agg first)
              agg-type  (-> agg keys set (disj :aggs) first)

        mtr-tmps     (get-params :metrics  :tmp)
        mtr-cols     (get-params :metrics  :col)
        
        inner-sql    (str "\n    SELECT\n      "
                            (join ",\n      " (concat
                                                (map col-a-as-b grb-aggs grb-cols)
                                                (-> mtr-funs set sort)))
                           "\n    FROM taxi_trips"
                           "\n   " where-str)
        
        outer-sql  (if (= (first grb-cols) "n_rows")
                     ; A special case when only row count is calculated
                     (-> inner-sql (clojure.string/replace "    " "") (str "\n\n"))
                     
                     ; The generic case with a few levels of aggregations
                     (-> (str "\nSELECT\n  " (join ",\n  " (concat grb-cols mtr-cols ["SUM(1) AS n_rows"]))
                              "\nFROM ("
                              "\n  SELECT\n    " (join ",\n    " (concat grb-cols (-> mtr-tmps set sort)))
                              "\n  FROM ("
                              inner-sql
                              "\n  ) t"
                              "\n) t GROUP BY {GROUP_BY}\n\n")
                         (clojure.string/replace "{GROUP_BY}" (join ", " grb-cols))))]
    (into [outer-sql] where-args)))

; (->> ((:time-pickupday-paidtip filters)) ((:speed-kmph-percentiles-by-day aggregations)) clojure.pprint/pprint)
; (->> ((:time-pickupday-paidtip filters)) ((:paid-total-per-km-stats-by-company-and-day aggregations)) es-to-sql print)
; (->> ((:time-pickupday-paidtip filters)) ((:speed-kmph-percentiles-by-day aggregations)) es-to-sql print)
; (->> ((:time-pickupday-paidtip filters)) ((:paid-total-stats-by-time-of-day  aggregations)) es-to-sql print)
; (->> ((:from-to-time filters)) ((:paid-total-per-km-stats-by-company-and-day aggregations)) es-to-sql print)


(comment
  (->> (for [[filter-name filter-generator] filters
             [aggregation-name aggregation] aggregations]
         (->> (filter-generator) aggregation es-to-sql
              ((fn [[sql & args]] (apply format (clojure.string/replace sql #"\?" "%s")
                                                (map #(if (string? %) (str \' % \') (str %)) args))))))
       (apply str)
       (spit "out.sql")))


(comment
  (->> (for [[filter-name filter-generator] filters
             [aggregation-name aggregation] aggregations]
         (aggregation (filter-generator)))
       (map json/write-str)
       (#(concat % ["" ""]))
       (clojure.string/join "\n")
       (spit "out.json")))
      
