Skip to content

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

Add snowflake collate expression (#65944)

Riley Thompson7d7595996cfb
diff --git a/docs/developers-guide/driver-changelog.md b/docs/developers-guide/driver-changelog.md
index 2778ad2c23e..f80d8ba945e 100644
--- a/docs/developers-guide/driver-changelog.md
+++ b/docs/developers-guide/driver-changelog.md
@@ -4,6 +4,10 @@ title: Driver interface changelog
# Driver Interface Changelog
+## Metabase 0.58.0
+
+- Added a `:collate` feature for drivers that support collation settings on text fields
+
## Metabase 0.57.0
- `driver/field-reference-mlv2` is now deprecated, and is no longer used. Please remove your implementations.
diff --git a/frontend/src/metabase-types/api/database.ts b/frontend/src/metabase-types/api/database.ts
index 1a86304ca30..f20af1f873f 100644
--- a/frontend/src/metabase-types/api/database.ts
+++ b/frontend/src/metabase-types/api/database.ts
@@ -54,6 +54,7 @@ export type DatabaseFeature =
| "distinct-where"
| "saved-question-sandboxing"
| "split-part"
+ | "collate"
| "transforms/python"
| "transforms/table";
diff --git a/frontend/src/metabase/querying/expressions/config.ts b/frontend/src/metabase/querying/expressions/config.ts
index fdc6ad73d85..6c9e6788fd8 100644
--- a/frontend/src/metabase/querying/expressions/config.ts
+++ b/frontend/src/metabase/querying/expressions/config.ts
@@ -460,6 +460,27 @@ const STRING = defineClauses(
},
],
},
+ collate: {
+ displayName: "collate",
+ type: "string",
+ requiresFeature: "collate",
+ description: () =>
+ t`Applies a collation to a text value for sorting and comparison purposes.`,
+ args: () => [
+ {
+ name: t`value`,
+ type: "string",
+ description: t`The column or text to apply the collation to.`,
+ example: dimension(t`Name`),
+ },
+ {
+ name: t`collation`,
+ type: "string",
+ description: t`The collation specification.`,
+ example: t`en-ci-ai`,
+ },
+ ],
+ },
"regex-match-first": {
displayName: `regexExtract`,
type: "string",
diff --git a/modules/drivers/snowflake/src/metabase/driver/snowflake.clj b/modules/drivers/snowflake/src/metabase/driver/snowflake.clj
index 4c4d0cd196b..e73367a10a6 100644
--- a/modules/drivers/snowflake/src/metabase/driver/snowflake.clj
+++ b/modules/drivers/snowflake/src/metabase/driver/snowflake.clj
@@ -72,6 +72,7 @@
:expressions/date true
:identifiers-with-spaces true
:split-part true
+ :collate true
:now true
:database-routing true
:metadata/table-existence-check true
@@ -516,6 +517,10 @@
[driver [_ value]]
[:to_char (sql.qp/->honeysql driver value)])
+(defmethod sql.qp/->honeysql [:snowflake :collate]
+ [driver [_ arg collation]]
+ [:collate (sql.qp/->honeysql driver arg) (sql.qp/->honeysql driver collation)])
+
(defn- db-name
"As mentioned above, old versions of the Snowflake driver used `details.dbname` to specify the physical database, but
tests (and Snowflake itself) expected `details.db`. This has since been fixed, but for legacy support we'll still
diff --git a/modules/drivers/snowflake/test/metabase/driver/snowflake_test.clj b/modules/drivers/snowflake/test/metabase/driver/snowflake_test.clj
index 497d031c936..a8bd06132b7 100644
--- a/modules/drivers/snowflake/test/metabase/driver/snowflake_test.clj
+++ b/modules/drivers/snowflake/test/metabase/driver/snowflake_test.clj
@@ -1321,13 +1321,13 @@
(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))
+ (let [mp (mt/metadata-provider)
+ products (lib.metadata/table mp (mt/id :products))
+ products-category (lib.metadata/field mp (mt/id :products :category))
+ products-name (lib.metadata/field mp (mt/id :products :title))
+ products-id (lib.metadata/field mp (mt/id :products :id))
filter-query (fn [filter]
- (-> (lib/query metadata-provider products)
+ (-> (lib/query mp products)
(lib/filter filter)
(lib/order-by products-id :asc)
(lib/limit 3)
@@ -1399,5 +1399,25 @@
[]]]]
(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))))))))
+ (is (str/includes? (-> result :data :native_form :query) exp-filter))
+ (is (= exp-rows (mt/rows result)))))))))
+
+(deftest snowflake-collate-comparison-test
+ (mt/test-driver :snowflake
+ (let [mp (mt/metadata-provider)
+ products (lib.metadata/table mp (mt/id :products))
+ collation-query (fn [collation]
+ (let [query (-> (lib/query mp products)
+ (lib/expression "contains_result" (lib/contains (lib/collate "café" collation) "CAFE"))
+ (lib/limit 1))
+ contains-expr (lib/expression-ref query "contains_result")]
+ (lib/with-fields query [contains-expr])))]
+ (doseq [[collation exp-filter exp-rows] [["en-ci-ai"
+ "CONTAINS(COLLATE('café', 'en-ci-ai'), 'CAFE')"
+ [[true]]]
+ ["en-ci"
+ "CONTAINS(COLLATE('café', 'en-ci'), 'CAFE')"
+ [[false]]]]]
+ (let [result (qp/process-query (collation-query collation))]
+ (is (str/includes? (-> result :data :native_form :query) exp-filter))
+ (is (= exp-rows (mt/rows result))))))))
diff --git a/src/metabase/driver.clj b/src/metabase/driver.clj
index 9d1be1fb443..249a4a63863 100644
--- a/src/metabase/driver.clj
+++ b/src/metabase/driver.clj
@@ -698,6 +698,9 @@
;; Does this driver support splitting strings and extracting a part?
:split-part
+ ;; Does this driver support collation settings on text fields?
+ :collate
+
;; True if this driver requires `:temporal-unit :default` on all temporal field refs, even if no temporal
;; bucketing was specified in the query.
;; Generally false, but a few time-series based analytics databases (eg. Druid) require it.
diff --git a/src/metabase/legacy_mbql/schema.cljc b/src/metabase/legacy_mbql/schema.cljc
index 18fe96fffda..88fea84f8e2 100644
--- a/src/metabase/legacy_mbql/schema.cljc
+++ b/src/metabase/legacy_mbql/schema.cljc
@@ -469,7 +469,7 @@
(def string-functions
"Functions that return string values. Should match [[StringExpression]]."
#{:substring :trim :rtrim :ltrim :upper :lower :replace :concat :regex-match-first :coalesce :case :if
- :host :domain :subdomain :path :month-name :quarter-name :day-name :text :split-part})
+ :host :domain :subdomain :path :month-name :quarter-name :day-name :text :split-part :collate})
(mr/def ::StringExpressionArg
[:multi
@@ -601,6 +601,10 @@
delimiter [:string {:min 1}]
position [:ref ::IntGreaterThanZeroOrNumericExpression])
+(defclause collate
+ s [:ref ::StringExpressionArg]
+ collation :string)
+
(defclause length
s [:ref ::StringExpressionArg])
@@ -1202,7 +1206,7 @@
(mr/def ::StringExpression
(one-of substring trim ltrim rtrim replace lower upper concat regex-match-first coalesce case if host domain
- subdomain path month-name quarter-name day-name text split-part))
+ subdomain path month-name quarter-name day-name text split-part collate))
(mr/def ::FieldOrExpressionDef
"Schema for anything that is accepted as a top-level expression definition, either an arithmetic expression such as a
diff --git a/src/metabase/lib/convert.cljc b/src/metabase/lib/convert.cljc
index b900b1145ed..6ddf6ea69bd 100644
--- a/src/metabase/lib/convert.cljc
+++ b/src/metabase/lib/convert.cljc
@@ -571,7 +571,7 @@
:get-week :get-year :get-month :get-day :get-hour
:get-minute :get-second :get-quarter
:datetime-add :datetime-subtract :date
- :concat :substring :replace :regex-match-first :split-part
+ :concat :substring :replace :regex-match-first :split-part :collate
:length :trim :ltrim :rtrim :upper :lower :text :integer :today]]
(lib.hierarchy/derive tag ::expression))
diff --git a/src/metabase/lib/core.cljc b/src/metabase/lib/core.cljc
index 6ed73bf54bf..39ccb2bad7a 100644
--- a/src/metabase/lib/core.cljc
+++ b/src/metabase/lib/core.cljc
@@ -241,6 +241,7 @@
text
today
split-part
+ collate
integer
float]
[lib.extraction
diff --git a/src/metabase/lib/expression.cljc b/src/metabase/lib/expression.cljc
index 434abb6a29a..c4c1bfa7c01 100644
--- a/src/metabase/lib/expression.cljc
+++ b/src/metabase/lib/expression.cljc
@@ -361,6 +361,7 @@
(lib.common/defop concat [s1 s2 & more])
(lib.common/defop substring [s start end])
(lib.common/defop split-part [s delimiter index])
+(lib.common/defop collate [s collation])
(lib.common/defop replace [s search replacement])
(lib.common/defop regex-match-first [s regex])
(lib.common/defop length [s])
diff --git a/src/metabase/lib/schema/expression/string.cljc b/src/metabase/lib/schema/expression/string.cljc
index e1403ed9ee0..2d3c316a659 100644
--- a/src/metabase/lib/schema/expression/string.cljc
+++ b/src/metabase/lib/schema/expression/string.cljc
@@ -42,6 +42,10 @@
#_delimiter [:schema [:string {:min 1}]] ;; literal string
#_position [:schema [:ref ::expression/positive-integer-or-numeric-expression]])
+(mbql-clause/define-tuple-mbql-clause :collate :- :type/Text
+ #_str [:schema [:ref ::expression/string]]
+ #_collation [:schema :string])
+
(mbql-clause/define-catn-mbql-clause :concat :- :type/Text
[:args [:repeat {:min 2} [:schema [:ref ::expression/expression]]]])