Skip to content
Snippets Groups Projects
Commit f276a93c authored by Cam Saul's avatar Cam Saul
Browse files

Mongo AVG Aggregation

parent 41ef7dad
No related branches found
No related tags found
No related merge requests found
......@@ -107,7 +107,8 @@
(defaggregation ["avg" field-id]
(aggregate {$group {"_id" nil
"avg" {$avg (field-id->$string field-id)}}}))
"avg" {$avg (field-id->$string field-id)}}}
{$project {"_id" false, "avg" true}}))
(defaggregation ["count" field-id]
(aggregate {$match {(field-id->kw field-id) {$exists true}}}
......@@ -170,6 +171,16 @@
:order_by [[user-id "ascending"]]
:limit 10}})))
(defn z []
(let [venues-id (sel :one :id Table :name "venues", :db_id (:id @db))]
(driver/process-query {:type :query
:database (:id @db)
:query {:source_table venues-id
:filter [nil nil]
:aggregation ["avg" (sel :one :id Field :name "latitude", :table_id venues-id)]
:breakout [nil]
:limit nil}})))
;; ## BREAKOUT
;; This is similar to the aggregation stuff but has to be implemented separately
......
......@@ -4,37 +4,22 @@
[metabase.driver :as driver]
[metabase.test-data :refer [db-id table->id field->id]]))
(def ^:const venues-columns
["ID" "CATEGORY_ID" "PRICE" "LONGITUDE" "LATITUDE" "NAME"])
(def venues-cols
(delay [{:extra_info {} :special_type :id, :base_type :BigIntegerField, :description nil, :name "ID", :table_id (table->id :venues), :id (field->id :venues :id)}
{:extra_info {:target_table_id (table->id :categories)} :special_type :fk, :base_type :IntegerField, :description nil, :name "CATEGORY_ID", :table_id (table->id :venues), :id (field->id :venues :category_id)}
{:extra_info {} :special_type :category, :base_type :IntegerField, :description nil, :name "PRICE", :table_id (table->id :venues), :id (field->id :venues :price)}
{:extra_info {} :special_type :longitude, :base_type :FloatField, :description nil, :name "LONGITUDE", :table_id (table->id :venues), :id (field->id :venues :longitude)}
{:extra_info {} :special_type :latitude, :base_type :FloatField, :description nil, :name "LATITUDE", :table_id (table->id :venues), :id (field->id :venues :latitude)}
{:extra_info {} :special_type nil, :base_type :TextField, :description nil, :name "NAME", :table_id (table->id :venues), :id (field->id :venues :name)}]))
;; # ERROR RESPONSES
;; ## "AVG" AGGREGATION
(expect {:status :completed,
:row_count 1,
:data {:rows [[35.50589199999998]]
:columns ["avg"]
:cols [{:base_type :FloatField
:special_type :latitude
:name "avg"
:id nil
:table_id nil
:description nil}]}}
(driver/process-query {:type :query
:database @db-id
:query {:source_table (table->id :venues)
:filter [nil nil]
:aggregation ["avg" (field->id :venues :latitude)]
;; Check that we get an error response formatted the way we'd expect
(expect
{:status :failed
:error "Column \"CHECKINS.NAME\" not found; SQL statement:\nSELECT \"CHECKINS\".* FROM \"CHECKINS\" WHERE (\"CHECKINS\".\"NAME\" = ?)"}
(driver/process-query {:database @db-id
:type :query
:query {:source_table (table->id :checkins)
:filter ["=" (field->id :venues :name) 1] ; wrong Field
:aggregation ["rows"]
:breakout [nil]
:limit nil}}))
;; # ---------------------------------------- TODO - ONES THAT STILL NEED TO BE MOVED OVER ----------------------------------------
;; ## "STDDEV" AGGREGATION
(expect {:status :completed
:row_count 1
......@@ -82,18 +67,3 @@
:filter [nil nil]
:breakout [(field->id :users :last_login)]
:aggregation ["cum_sum" (field->id :users :id)]}}))
;; # ERROR RESPONSES
;; Check that we get an error response formatted the way we'd expect
(expect
{:status :failed
:error "Column \"CHECKINS.NAME\" not found; SQL statement:\nSELECT \"CHECKINS\".* FROM \"CHECKINS\" WHERE (\"CHECKINS\".\"NAME\" = ?)"}
(driver/process-query {:database @db-id
:type :query
:query {:source_table (table->id :checkins)
:filter ["=" (field->id :venues :name) 1] ; wrong Field
:aggregation ["rows"]
:breakout [nil]
:limit nil}}))
......@@ -289,6 +289,24 @@
:breakout [nil]
:limit nil})
;; ## "AVG" AGGREGATION
(qp-expect-with-all-drivers
{:rows [[35.50589199999998]]
:columns ["avg"]
:cols [{:base_type :FloatField
:special_type :latitude
:name "avg"
:id nil
:table_id nil
:description nil}]}
{:source_table (id :venues)
:filter [nil nil]
:aggregation ["avg" (id :venues :latitude)]
:breakout [nil]
:limit nil})
;; ### "DISTINCT COUNT" AGGREGATION
(qp-expect-with-all-drivers
{:rows [[15]]
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment