Skip to content

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

Update snowflake string filtering (#65853)

Riley Thompsone53d67c763ce
diff --git a/modules/drivers/snowflake/src/metabase/driver/snowflake.clj b/modules/drivers/snowflake/src/metabase/driver/snowflake.clj
index 5c8101f66b9..4c4d0cd196b 100644
--- a/modules/drivers/snowflake/src/metabase/driver/snowflake.clj
+++ b/modules/drivers/snowflake/src/metabase/driver/snowflake.clj
@@ -921,3 +921,33 @@
[_driver]
;; https://docs.snowflake.com/en/sql-reference/identifiers
255)
+
+(defn get-string-filter-arg
+ "Generate the argument to match in the string filters. It's based on sql.qp/generate-pattern."
+ [driver
+ [type val :as arg]
+ {:keys [case-sensitive] :or {case-sensitive true} :as _options}]
+ (if case-sensitive
+ (sql.qp/->honeysql driver arg)
+ (if (= :value type)
+ (sql.qp/->honeysql driver [type (u/lower-case-en val)])
+ [:lower (sql.qp/->honeysql driver arg)])))
+
+(defn- string-filter
+ [driver str-filter field arg {:keys [case-sensitive] :or {case-sensitive true} :as options}]
+ (let [casted-field (sql.qp/->honeysql driver (sql.qp/maybe-cast-uuid-for-text-compare field))]
+ [str-filter
+ (if case-sensitive casted-field [:lower casted-field])
+ (get-string-filter-arg driver arg options)]))
+
+(defmethod sql.qp/->honeysql [:snowflake :contains]
+ [driver [_ field arg options]]
+ (string-filter driver :contains field arg options))
+
+(defmethod sql.qp/->honeysql [:snowflake :starts-with]
+ [driver [_ field arg options]]
+ (string-filter driver :startswith field arg options))
+
+(defmethod sql.qp/->honeysql [:snowflake :ends-with]
+ [driver [_ field arg options]]
+ (string-filter driver :endswith field arg options))
diff --git a/modules/drivers/snowflake/test/metabase/driver/snowflake_test.clj b/modules/drivers/snowflake/test/metabase/driver/snowflake_test.clj
index ba1f79fd42b..4b99ed28d28 100644
--- a/modules/drivers/snowflake/test/metabase/driver/snowflake_test.clj
+++ b/modules/drivers/snowflake/test/metabase/driver/snowflake_test.clj
@@ -23,6 +23,7 @@
[metabase.events.core :as events]
[metabase.lib.core :as lib]
[metabase.lib.metadata :as lib.metadata]
+ [metabase.lib.options :as lib.options]
[metabase.lib.test-metadata :as meta]
[metabase.lib.test-util :as lib.tu]
[metabase.query-processor :as qp]
@@ -1317,3 +1318,86 @@
:type/Integer [:INTEGER]
:type/Text [:TEXT]
:type/Time [:TIME])))
+
+(deftest snowflake-string-filter-tests
+ (mt/test-driver :snowflake
+ (let [metadata-provider (mt/metadata-provider)
+ products (lib.metadata/table metadata-provider (mt/id :products))
+ products-category (lib.metadata/field metadata-provider (mt/id :products :category))
+ products-name (lib.metadata/field metadata-provider (mt/id :products :title))
+ products-id (lib.metadata/field metadata-provider (mt/id :products :id))
+ filter-query (fn [filter]
+ (-> (lib/query metadata-provider products)
+ (lib/filter filter)
+ (lib/order-by products-id :asc)
+ (lib/limit 3)
+ (lib/with-fields [products-id products-name products-category])))]
+ (doseq [[msg filter exp-filter exp-rows] [["case insensitive contains has rows"
+ (-> (lib/contains products-category "GET")
+ (lib.options/update-options assoc :case-sensitive false))
+ "CONTAINS(LOWER(\"PUBLIC\".\"products\".\"category\"), 'get')"
+ [[5 "Enormous Marble Wallet" "Gadget"]
+ [9 "Practical Bronze Computer" "Widget"]
+ [11 "Ergonomic Silk Coat" "Gadget"]]]
+
+ ["case sensitive contains has rows"
+ (-> (lib/contains products-category "Gad")
+ (lib.options/update-options assoc :case-sensitive true))
+ "CONTAINS(\"PUBLIC\".\"products\".\"category\", 'Gad')"
+ [[5 "Enormous Marble Wallet" "Gadget"]
+ [11 "Ergonomic Silk Coat" "Gadget"]
+ [16 "Incredible Bronze Pants" "Gadget"]]]
+
+ ["case sensitive contains with no rows"
+ (-> (lib/contains products-category "gad")
+ (lib.options/update-options assoc :case-sensitive true))
+ "CONTAINS(\"PUBLIC\".\"products\".\"category\", 'gad')"
+ []]
+
+ ["case insensitive starts with has rows"
+ (-> (lib/starts-with products-category "GAD")
+ (lib.options/update-options assoc :case-sensitive false))
+ "STARTSWITH(LOWER(\"PUBLIC\".\"products\".\"category\"), 'gad')"
+ [[5 "Enormous Marble Wallet" "Gadget"]
+ [11 "Ergonomic Silk Coat" "Gadget"]
+ [16 "Incredible Bronze Pants" "Gadget"]]]
+
+ ["case sensitive starts with has rows"
+ (-> (lib/starts-with products-category "Gad")
+ (lib.options/update-options assoc :case-sensitive true))
+ "STARTSWITH(\"PUBLIC\".\"products\".\"category\", 'Gad')"
+ [[5 "Enormous Marble Wallet" "Gadget"]
+ [11 "Ergonomic Silk Coat" "Gadget"]
+ [16 "Incredible Bronze Pants" "Gadget"]]]
+
+ ["case sensitive starts with has no rows"
+ (-> (lib/starts-with products-category "gad")
+ (lib.options/update-options assoc :case-sensitive true))
+ "STARTSWITH(\"PUBLIC\".\"products\".\"category\", 'gad')"
+ []]
+
+ ["case insensitive ends with has rows"
+ (-> (lib/ends-with products-category "GET")
+ (lib.options/update-options assoc :case-sensitive false))
+ "ENDSWITH(LOWER(\"PUBLIC\".\"products\".\"category\"), 'get')"
+ [[5 "Enormous Marble Wallet" "Gadget"]
+ [9 "Practical Bronze Computer" "Widget"]
+ [11 "Ergonomic Silk Coat" "Gadget"]]]
+
+ ["case sensitive ends with has rows"
+ (-> (lib/ends-with products-category "get")
+ (lib.options/update-options assoc :case-sensitive true))
+ "ENDSWITH(\"PUBLIC\".\"products\".\"category\", 'get')"
+ [[5 "Enormous Marble Wallet" "Gadget"]
+ [9 "Practical Bronze Computer" "Widget"]
+ [11 "Ergonomic Silk Coat" "Gadget"]]]
+
+ ["case sensitive ends with has no rows"
+ (-> (lib/ends-with products-category "GET")
+ (lib.options/update-options assoc :case-sensitive true))
+ "ENDSWITH(\"PUBLIC\".\"products\".\"category\", 'GET')"
+ []]]]
+ (testing msg
+ (let [result (qp/process-query (filter-query filter))]
+ (is (= exp-rows (mt/rows result)))
+ (is (str/includes? (-> result :data :native_form :query) exp-filter))))))))
diff --git a/src/metabase/driver/sql/query_processor.clj b/src/metabase/driver/sql/query_processor.clj
index d4aa4c508dc..fd1180efd16 100644
--- a/src/metabase/driver/sql/query_processor.clj
+++ b/src/metabase/driver/sql/query_processor.clj
@@ -1590,7 +1590,7 @@
[::cast-to-text field]
field))
-(mu/defn- maybe-cast-uuid-for-text-compare
+(mu/defn maybe-cast-uuid-for-text-compare
"For :contains, :starts-with, and :ends-with.
Comparing UUID fields against with these operations requires casting as the right side will have `%` for `LIKE` operations."
[field]