Skip to content

The easy-to-use open source Business Intelligence and Embedded Analytics tool that lets everyone work with data :bar_chart:

Fix mongo native preview of queries with variables (#66959)

Riley Thompson7ee2668a24a5
diff --git a/modules/drivers/mongo/src/metabase/driver/mongo.clj b/modules/drivers/mongo/src/metabase/driver/mongo.clj
index 6a1f38a0cd0..8ef5542a91e 100644
--- a/modules/drivers/mongo/src/metabase/driver/mongo.clj
+++ b/modules/drivers/mongo/src/metabase/driver/mongo.clj
@@ -547,7 +547,7 @@
(encode-object-id [oid] (str "ObjectId(\"" (.toString ^ObjectId oid) "\")"))]
(cond
(map? mgo) (encode-map mgo next-indent)
- (vector? mgo) (encode-vector mgo next-indent)
+ (sequential? mgo) (encode-vector mgo next-indent)
(instance? ObjectId mgo) (encode-object-id mgo)
(instance? Binary mgo) (encode-binary mgo)
:else (json/encode mgo))))))
@@ -555,9 +555,12 @@
(defmethod driver/prettify-native-form :mongo
[_driver native-form]
(try
- (encode-mongo native-form)
+ (let [parsed (if (string? native-form)
+ (json/decode native-form)
+ native-form)]
+ (encode-mongo parsed))
(catch Throwable e
- (log/errorf "Unexpected error while encoding Mongo BSON query: %s" (ex-message e))
+ (log/errorf "Unexpected error while prettifying Mongo BSON query: %s" (ex-message e))
(log/debugf e "Query:\n%s" native-form)
native-form)))
diff --git a/modules/drivers/mongo/test/metabase/driver/mongo_test.clj b/modules/drivers/mongo/test/metabase/driver/mongo_test.clj
index a677ca0b1ad..6a33724bc38 100644
--- a/modules/drivers/mongo/test/metabase/driver/mongo_test.clj
+++ b/modules/drivers/mongo/test/metabase/driver/mongo_test.clj
@@ -15,6 +15,7 @@
[metabase.driver.settings :as driver.settings]
[metabase.driver.util :as driver.u]
[metabase.lib.core :as lib]
+ [metabase.lib.metadata :as lib.metadata]
[metabase.lib.util.match :as lib.util.match]
[metabase.query-processor :as qp]
[metabase.query-processor.compile :as qp.compile]
@@ -1174,3 +1175,131 @@
:type/MongoBSONID "objectId"
:type/MongoBinData "binData"
:type/IPAddress "string")))
+
+(deftest ^:parallel prettify-native-form-test
+ (mt/test-driver :mongo
+ (testing "prettifies normal lib query"
+ (let [mp (mt/metadata-provider)
+ products-table (lib.metadata/table mp (mt/id :products))
+ product-category (lib.metadata/field mp (mt/id :products :category))
+ query (-> (lib/query mp products-table)
+ (lib/filter (lib/= product-category
+ "Widget")))]
+ (is (= (str/join "\n"
+ ["["
+ " {"
+ " \"$match\": {"
+ " \"category\": \"Widget\""
+ " }"
+ " },"
+ " {"
+ " \"$project\": {"
+ " \"_id\": \"$_id\","
+ " \"ean\": \"$ean\","
+ " \"title\": \"$title\","
+ " \"category\": \"$category\","
+ " \"vendor\": \"$vendor\","
+ " \"price\": \"$price\","
+ " \"rating\": \"$rating\","
+ " \"created_at\": \"$created_at\""
+ " }"
+ " },"
+ " {"
+ " \"$limit\": 1048575"
+ " }"
+ "]"])
+ (->> (qp.compile/compile-with-inline-parameters query)
+ :query
+ (driver/prettify-native-form :mongo))))))
+ (testing "prettifies native query with variable that is valid json"
+ (let [query {:database (mt/id)
+ :type :native
+ :native {:collection "products"
+ :query (str/join "\n"
+ ["["
+ " {"
+ " \"$match\": {"
+ " \"category\": {{category_var}}"
+ " }},"
+ " {"
+ " \"$project\": {"
+ " \"_id\": \"$_id\","
+ " \"title\": \"$title\","
+ " \"category\": \"$category\""
+ " }},"
+ " {"
+ " \"$limit\": 1048575"
+ " }"
+ "]"])
+ :template-tags {"category_var" {:name "category_var"
+ :display-name "Category Variable"
+ :type :text}}}
+ :parameters [{:type :text
+ :target [:variable [:template-tag "category_var"]]
+ :value "Gadget"}]}]
+ (is (= (str/join "\n"
+ ["["
+ " {"
+ " \"$match\": {"
+ " \"category\": \"Gadget\""
+ " }"
+ " },"
+ " {"
+ " \"$project\": {"
+ " \"_id\": \"$_id\","
+ " \"title\": \"$title\","
+ " \"category\": \"$category\""
+ " }"
+ " },"
+ " {"
+ " \"$limit\": 1048575"
+ " }"
+ "]"])
+ (->> (qp.compile/compile-with-inline-parameters query)
+ :query
+ (driver/prettify-native-form :mongo))))))
+ (testing "prettifies native query with variable that is not valid json"
+ (let [query {:database (mt/id)
+ :type :native
+ :native {:collection "products"
+ :query (str/join "\n"
+ ["["
+ " {"
+ " \"$match\": {"
+ " \"created_at\": {\"$gte\": {{created_at_var}}}"
+ " }},"
+ " {"
+ " \"$project\": {"
+ " \"_id\": \"$_id\","
+ " \"title\": \"$title\","
+ " \"category\": \"$category\""
+ " }},"
+ " {"
+ " \"$limit\": 1048575"
+ " }"
+ "]"])
+ :template-tags {"created_at_var" {:name "created_at_var"
+ :display-name "Created At Variable"
+ :type :date}}}
+ :parameters [{:type :date/single
+ :target [:variable [:template-tag "created_at_var"]]
+ :value "2018-01-01"}]}]
+ (is (= (str/join "\n"
+ ["["
+ " {"
+ " \"$match\": {"
+ " \"created_at\": {\"$gte\": ISODate(\"2018-01-01\")}"
+ " }},"
+ " {"
+ " \"$project\": {"
+ " \"_id\": \"$_id\","
+ " \"title\": \"$title\","
+ " \"category\": \"$category\""
+ " }},"
+ " {"
+ " \"$limit\": 1048575"
+ " }"
+ "]"])
+ (->> (qp.compile/compile-with-inline-parameters query)
+ :query
+ (driver/prettify-native-form :mongo))))))))