Skip to content

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

use json unquote (#66512)

Riley Thompson8ce3b6b4068d
diff --git a/src/metabase/driver/mysql.clj b/src/metabase/driver/mysql.clj
index 7eacf0d78b9..aea99da33ee 100644
--- a/src/metabase/driver/mysql.clj
+++ b/src/metabase/driver/mysql.clj
@@ -432,7 +432,7 @@
nfc-path (:nfc-path stored-field)
parent-identifier (sql.qp.u/nfc-field->parent-identifier unwrapped-identifier stored-field)
jsonpath-query (format "$.%s" (str/join "." (map handle-name (rest nfc-path))))
- json-extract+jsonpath [:json_extract parent-identifier jsonpath-query]]
+ json-extract+jsonpath [:json_unquote [:json_extract parent-identifier jsonpath-query]]]
(case (u/lower-case-en field-type)
;; If we see JSON datetimes we expect them to be in ISO8601. However, MySQL expects them as something different.
;; We explicitly tell MySQL to go and accept ISO8601, because that is JSON datetimes, although there is no real standard for JSON, ISO8601 is the de facto standard.
diff --git a/test/metabase/driver/mysql_test.clj b/test/metabase/driver/mysql_test.clj
index 66e8d2150ee..d6fa0a094fe 100644
--- a/test/metabase/driver/mysql_test.clj
+++ b/test/metabase/driver/mysql_test.clj
@@ -473,17 +473,39 @@
(let [boop-identifier (h2x/identifier :field "boop" "bleh -> meh")]
(testing "Transforming MBQL query with JSON in it to mysql query works"
(let [boop-field {:nfc-path [:bleh :meh] :database-type "bigint"}]
- (is (= ["CONVERT(JSON_EXTRACT(`boop`.`bleh`, ?), UNSIGNED)" "$.\"meh\""]
+ (is (= ["CONVERT(JSON_UNQUOTE(JSON_EXTRACT(`boop`.`bleh`, ?)), UNSIGNED)" "$.\"meh\""]
(sql.qp/format-honeysql :mysql (sql.qp/json-query :mysql boop-identifier boop-field))))))
(testing "What if types are weird and we have lists"
(let [weird-field {:nfc-path [:bleh "meh" :foobar 1234] :database-type "bigint"}]
- (is (= ["CONVERT(JSON_EXTRACT(`boop`.`bleh`, ?), UNSIGNED)" "$.\"meh\".\"foobar\".\"1234\""]
+ (is (= ["CONVERT(JSON_UNQUOTE(JSON_EXTRACT(`boop`.`bleh`, ?)), UNSIGNED)" "$.\"meh\".\"foobar\".\"1234\""]
(sql.qp/format-honeysql :mysql (sql.qp/json-query :mysql boop-identifier weird-field))))))
(testing "Doesn't complain when field is boolean"
(let [boolean-boop-field {:database-type "boolean" :nfc-path [:bleh "boop" :foobar 1234]}]
- (is (= ["JSON_EXTRACT(`boop`.`bleh`, ?)" "$.\"boop\".\"foobar\".\"1234\""]
+ (is (= ["JSON_UNQUOTE(JSON_EXTRACT(`boop`.`bleh`, ?))" "$.\"boop\".\"foobar\".\"1234\""]
(sql.qp/format-honeysql :mysql (sql.qp/json-query :mysql boop-identifier boolean-boop-field))))))))
+(tx/defdataset json-unquote-test
+ [["json_test"
+ [{:field-name "data", :base-type :type/JSON}]
+ [["{\"int_val\": 42, \"str_val\": \"hello\"}"]
+ ["{\"int_val\": 123, \"str_val\": \"world\"}"]]]])
+
+(deftest json-unfolding-returns-unquoted-values-test
+ (testing "JSON unfolding should return values without JSON quotes (#61408)"
+ (mt/test-driver :mysql
+ (when-not (mysql/mariadb? (mt/db))
+ (mt/dataset json-unquote-test
+ (let [mp (mt/metadata-provider)
+ json-table (lib.metadata/table mp (mt/id :json_test))
+ int-field (lib.metadata/field mp (mt/id :json_test "data → int_val"))
+ str-field (lib.metadata/field mp (mt/id :json_test "data → str_val"))]
+ (is (= [[42.0 "hello"]
+ [123.0 "world"]]
+ (-> (lib/query mp json-table)
+ (lib/with-fields [int-field str-field])
+ (qp/process-query)
+ (mt/rows))))))))))
+
;; MariaDB doesn't have support for explicit JSON columns, it does it in a more SQL Server-ish way
;; where LONGTEXT columns are the actual JSON columns and there's JSON functions that just work on them,
;; construed as text.
@@ -537,14 +559,20 @@
:aggregation [[:count]]
:breakout [[:field (u/the-id field) nil]]}))]
(is (= ["SELECT"
- " (JSON_EXTRACT(`json`.`json_bit`, ?) + 0.0) AS `json_bit → 1234`,"
+ " ("
+ " JSON_UNQUOTE(JSON_EXTRACT(`json`.`json_bit`, ?)) + 0.0"
+ " ) AS `json_bit → 1234`,"
" COUNT(*) AS `count`"
"FROM"
" `json`"
"GROUP BY"
- " (JSON_EXTRACT(`json`.`json_bit`, ?) + 0.0)"
+ " ("
+ " JSON_UNQUOTE(JSON_EXTRACT(`json`.`json_bit`, ?)) + 0.0"
+ " )"
"ORDER BY"
- " (JSON_EXTRACT(`json`.`json_bit`, ?) + 0.0) ASC"]
+ " ("
+ " JSON_UNQUOTE(JSON_EXTRACT(`json`.`json_bit`, ?)) + 0.0"
+ " ) ASC"]
(str/split-lines (driver/prettify-native-form :mysql (:query compile-res)))))
(is (= '("$.\"1234\"" "$.\"1234\"" "$.\"1234\"") (:params compile-res))))))))))
@@ -564,7 +592,7 @@
:min-value 0.75,
:max-value 54.0,
:bin-width 0.75}}]]
- (is (= ["((FLOOR((((JSON_EXTRACT(`json`.`json_bit`, ?) + 0.0) - 0.75) / 0.75)) * 0.75) + 0.75)"
+ (is (= ["((FLOOR((((JSON_UNQUOTE(JSON_EXTRACT(`json`.`json_bit`, ?)) + 0.0) - 0.75) / 0.75)) * 0.75) + 0.75)"
"$.\"1234\""]
(sql.qp/format-honeysql :mysql (sql.qp/->honeysql :mysql field-clause)))))))))))))