 ; (->> ((:time-pickupday-paidtip filters)) ((:paid-total-per-km-stats-by-company-and-day aggregations)) clojure.pprint/pprint)
 ; (->> ((:time-pickupday-paidtip filters)) ((:speed-kmph-percentiles-by-day aggregations)) :aggs parse-aggs clojure.pprint/pprint)
 ; (->> ((:time-pickupday-paidtip filters)) ((:paid-total-per-km-stats-by-company-and-day aggregations)) :aggs parse-aggs clojure.pprint/pprint)
+; (->> ((:count aggregations) {}) :aggs parse-aggs clojure.pprint/pprint)
 
 (defn es-to-sql [query]
-  (let [wheres (for [[f-type field f-args] (->> query :query :bool :filter (map parse-filter))]
+  (let [join        #(clojure.string/join % %2)
+        wheres (for [[f-type field f-args] (->> query :query :bool :filter (map parse-filter))]
                   (let [convert (if (clojure.string/ends-with? field "_dt") epoch-to-str identity)]
                     (case f-type
-                      :range {:str (str field " BETWEEN ? AND ?") :args (mapv (comp convert f-args) [:gte :lte])}
-                      :term  {:str (str field " = ?")             :args [f-args]})))
-        where-str    (->> (map :str wheres) (clojure.string/join " AND ") (str " WHERE "))
+                      :range {:str [(str field " BETWEEN ? AND ?")] :args (mapv (comp convert f-args) [:gte :lte])}
+                      :term  {:str [(str field " = ?")]             :args [f-args]}
+                      
+                      :geo_bounding_box
+                      {:str (let [field (subs field 0 (- (count field) 4))]
+                              [(str field "_lat BETWEEN ? AND ?")
+                               (str field "_lon BETWEEN ? AND ?")])
+                       
+                       ; This is truly terrible, but in essense we are picking just min and max of the bounding box's latitude and longitude
+                       ; and I didn't feel like writing lookup keywords to the four corners in the correct order.
+                       :args (mapcat #(apply (juxt min max) (map (comp % f-args) [:top_left :bottom_right])) [:lat :lon])})))
+        where-str    (->> (mapcat :str wheres) (join " AND\n      ") (str " WHERE\n      "))
         where-args   (mapcat :args wheres)
         
         agg-params   (-> query :aggs parse-aggs)


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

                   
                   :histogram
                   (let [interval (-> agg-data :interval float)]
                     [{:agg (format "FLOOR(%s / %.1f)" field interval)
                       :col (gen-col-name field "hist" (:interval agg-data))}])
                   
                   :date_histogram
                   (let [_            (assert (= \d (-> agg-data :interval last)) "Only days are supported for now")
                         interval     (-> agg-data :interval butlast (#(Float. (apply str %))))
                         interval-div (if (= 1.0 interval) "" (format " / %.1f" interval))]
                     [{:agg (format "FLOOR(CAST(CAST(%s AS datetime) AS float)%s)" field interval-div)
                       :col (gen-col-name field "datehist" (:interval agg-data))}]))}
                
                {:metrics
                 (cond
