From 6150ad3d4afd27caf76f6c92550b105688a02492 Mon Sep 17 00:00:00 2001
From: Cam Saul <cam@geotip.com>
Date: Tue, 30 Jun 2015 17:18:25 -0700
Subject: [PATCH] handle nested fields in MQL

---
 src/metabase/driver/mongo.clj      | 11 +++++++++--
 src/metabase/driver/mongo/util.clj |  6 +-----
 src/metabase/util.clj              |  7 +++++++
 test/metabase/test/util/mql.clj    | 18 +++++++++---------
 4 files changed, 26 insertions(+), 16 deletions(-)

diff --git a/src/metabase/driver/mongo.clj b/src/metabase/driver/mongo.clj
index 030c8298279..b49921d8509 100644
--- a/src/metabase/driver/mongo.clj
+++ b/src/metabase/driver/mongo.clj
@@ -140,12 +140,19 @@
      with dataset mongo
      ag rows
      tbl tips
-     filter = ["." (id :tips :venue) "name"] "Kyle's Low-Carb Grill"
+     filter = venue...name "Kyle's Low-Carb Grill"
      lim 10))
 
+(defn x2 []
+  (Q run against geographical-tips
+     using mongo
+     aggregate rows
+     of tips
+     filter = venue...name "Kyle's Low-Carb Grill"
+     limit 10))
+
 (defn y []
   (datasets/with-dataset :mongo
     (data/with-temp-db [db (data/dataset-loader) defs/geographical-tips]
       (with-mongo-connection [_ db]
-        ;; 61 ms ???
         (active-subfield-names->type driver &tips.venue)))))
diff --git a/src/metabase/driver/mongo/util.clj b/src/metabase/driver/mongo/util.clj
index c256523aeae..142a950efab 100644
--- a/src/metabase/driver/mongo/util.clj
+++ b/src/metabase/driver/mongo/util.clj
@@ -43,7 +43,6 @@
   "Run F with a new connection (bound to `*mongo-connection*`) to DATABASE.
    Don't use this directly; use `with-mongo-connection`."
   [f database]
-  (println (metabase.util/format-color 'red "<<OPENING A NEW MONGO CONNECTION>>"))
   (let [connection-string (cond
                             (string? database)              database
                             (:dbname (:details database))   (details-map->connection-string (:details database)) ; new-style -- entire Database obj
@@ -55,10 +54,7 @@
       (binding [*mongo-connection* mongo-connection]
         (f *mongo-connection*))
       (finally
-        (println (metabase.util/format-color 'red "DISCONNECTING!"))
-        (assert conn)
-        (mg/disconnect conn)
-        (println (metabase.util/format-color 'green "OK."))))))
+        (mg/disconnect conn)))))
 
 (defmacro with-mongo-connection
   "Open a new MongoDB connection to DATABASE-OR-CONNECTION-STRING, bind connection to BINDING, execute BODY, and close the connection.
diff --git a/src/metabase/util.clj b/src/metabase/util.clj
index 15210e9d713..4b5d93bb0fa 100644
--- a/src/metabase/util.clj
+++ b/src/metabase/util.clj
@@ -268,4 +268,11 @@
   ([color-symb x]
    ((ns-resolve 'colorize.core color-symb) (pprint-to-str x))))
 
+(defmacro cond-let
+  "Like `if-let` or `when-let`, but for `cond`."
+  [binding-form then-form & more]
+  `(if-let ~binding-form ~then-form
+           ~(when (seq more)
+              `(cond-let ~@more))))
+
 (require-dox-in-this-namespace)
diff --git a/test/metabase/test/util/mql.clj b/test/metabase/test/util/mql.clj
index 6ff85924f34..0210af0e459 100644
--- a/test/metabase/test/util/mql.clj
+++ b/test/metabase/test/util/mql.clj
@@ -5,7 +5,8 @@
             [clojure.walk :refer [macroexpand-all]]
             [metabase.driver :as driver]
             [metabase.test.data :as data]
-            [metabase.test.data.datasets :as datasets]))
+            [metabase.test.data.datasets :as datasets]
+            [metabase.util :as u]))
 
 (defn- partition-tokens [keywords tokens]
   (->> (loop [all [], current-split nil, [token & more] tokens]
@@ -74,14 +75,13 @@
 (defmacro Q:field [f]
   (or (when (symbol? f)
         (let [f (name f)]
-          (if-let [[_ from to] (re-matches #"^(.*)->(.*)$" f)]
-            ["fk->" `(Q:field ~(symbol from)) `(Q:field ~(symbol to))]
-            (if-let [[_ ag-field-index] (re-matches #"^ag\.(\d+)$" f)]
-              ["aggregation" (Integer/parseInt ag-field-index)]
-              (when-let [[_ table field] (re-matches #"^(?:([^\.]+)\.)?([^\.]+)$" f)]
-                `(~'id ~(if table (keyword table)
-                            'table)
-                       ~(keyword field)))))))
+          (u/cond-let
+           [[_ from to] (re-matches #"^(.+)->(.+)$" f)]                  ["fk->" `(Q:field ~(symbol from)) `(Q:field ~(symbol to))]
+           [[_ f sub] (re-matches #"^(.+)\.\.\.(.+)$" f)]                ["." `(Q:field ~(symbol f)) sub #_`(Q:field ~(symbol sub))]
+           [[_ ag-field-index] (re-matches #"^ag\.(\d+)$" f)]            ["aggregation" (Integer/parseInt ag-field-index)]
+           [[_ table field] (re-matches #"^(?:([^\.]+)\.)?([^\.]+)$" f)] `(~'id ~(if table (keyword table)
+                                                                                     'table)
+                                                                                ~(keyword field)))))
       f))
 
 (defmacro Q [& tokens]
-- 
GitLab