Skip to content

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

Wrap sqlserver boolean expressions in a case statement (#67136)

Riley Thompson502b66528cbe
diff --git a/.clj-kondo/config/modules/config.edn b/.clj-kondo/config/modules/config.edn
index ccd149919d7..3ef710719b6 100644
--- a/.clj-kondo/config/modules/config.edn
+++ b/.clj-kondo/config/modules/config.edn
@@ -749,6 +749,7 @@
metabase.lib.schema.common
metabase.lib.schema.expression
metabase.lib.schema.expression.temporal
+ metabase.lib.schema.filter
metabase.lib.schema.id
metabase.lib.schema.info
metabase.lib.schema.join
diff --git a/modules/drivers/sqlserver/src/metabase/driver/sqlserver.clj b/modules/drivers/sqlserver/src/metabase/driver/sqlserver.clj
index 3be20d85f78..e5587f124db 100644
--- a/modules/drivers/sqlserver/src/metabase/driver/sqlserver.clj
+++ b/modules/drivers/sqlserver/src/metabase/driver/sqlserver.clj
@@ -603,9 +603,19 @@
;; Tell [[sql.qp/as]] to insert a cast to :bit for boolean expressions. This ensures the :type/Boolean is
;; preserved in results metadata, so downstream questions and query stages can use the column in contexts
;; where a boolean is required; otherwise, SQL Server returns a value of type int for `SELECT 1 AS MyBool`.
- maybe-add-cast #(cond-> %
+ ;; For comparison expressions (e.g. [:> field1 field2]), tell [[sql.qp/as]] to wrap it in a case statement
+ ;; and then cast it to a :bit. See #53805 for more details.
+ maybe-add-cast #(cond
+ (sql.qp.boolean-to-comparison/predicate-expression-clause? %)
+ (-> %
+ (driver-api/assoc-field-options ::sql.qp/wrap-in-case true)
+ (driver-api/assoc-field-options ::sql.qp/add-cast :bit))
+
(sql.qp.boolean-to-comparison/boolean-expression-clause? %)
- (driver-api/assoc-field-options ::sql.qp/add-cast :bit))]
+ (driver-api/assoc-field-options % ::sql.qp/add-cast :bit)
+
+ :else
+ %)]
(->> (update query :fields #(mapv maybe-add-cast %))
(parent-method driver :fields honeysql-form))))
@@ -809,6 +819,12 @@
[driver [_ expr]]
(sql.qp/->honeysql driver [::sql.qp/cast expr "varchar(256)"]))
+;; This is used to wrap comparison expressions (e.g. [:> field1 field2]) in a case statement as
+;; SQL server does not have a boolean data type. See #53805 for more details.
+(defmethod sql.qp/->honeysql [:sqlserver ::sql.qp/wrap-in-case]
+ [driver [_tag expr]]
+ [:case (sql.qp/->honeysql driver expr) [:inline 1] :else [:inline 0]])
+
(defmethod driver/db-default-timezone :sqlserver
[driver database]
(sql-jdbc.execute/do-with-connection-with-options
diff --git a/modules/drivers/sqlserver/test/metabase/driver/sqlserver_test.clj b/modules/drivers/sqlserver/test/metabase/driver/sqlserver_test.clj
index a389876a537..4f575562ad0 100644
--- a/modules/drivers/sqlserver/test/metabase/driver/sqlserver_test.clj
+++ b/modules/drivers/sqlserver/test/metabase/driver/sqlserver_test.clj
@@ -15,6 +15,7 @@
[metabase.driver.sql.query-processor :as sql.qp]
[metabase.driver.sqlserver :as sqlserver]
[metabase.lib.core :as lib]
+ [metabase.lib.metadata :as lib.metadata]
[metabase.query-processor :as qp]
[metabase.query-processor.compile :as qp.compile]
[metabase.query-processor.middleware.limit :as limit]
@@ -645,6 +646,85 @@
(is (= expected-types
(map :base_type results-metadata-cols))))))))))))
+(deftest ^:parallel predicate-expression-in-custom-column-test
+ (mt/test-driver :sqlserver
+ (let [mp (mt/metadata-provider)
+ products (lib.metadata/table mp (mt/id :products))
+ id (lib.metadata/field mp (mt/id :products :id))
+ price (lib.metadata/field mp (mt/id :products :price))
+ category (lib.metadata/field mp (mt/id :products :category))]
+ (testing "predicate expression with >"
+ (let [query (-> (lib/query mp products)
+ (lib/expression "price_gt60" (lib/> price 60))
+ (lib/limit 5))
+ query (lib/with-fields query [id category price (lib/expression-ref query "price_gt60")])]
+ (is (= [[1 "Gizmo" 29.46 false]
+ [2 "Doohickey" 70.08 true]
+ [3 "Doohickey" 35.39 false]
+ [4 "Doohickey" 73.99 true]
+ [5 "Gadget" 82.75 true]]
+ (mt/rows (qp/process-query query))))))
+ (testing "predicate expression with contains"
+ (let [query (-> (lib/query mp products)
+ (lib/expression "contains_get" (lib/contains category "get"))
+ (lib/limit 5))
+ query (lib/with-fields query [id category price (lib/expression-ref query "contains_get")])]
+ (is (= [[1 "Gizmo" 29.46 false]
+ [2 "Doohickey" 70.08 false]
+ [3 "Doohickey" 35.39 false]
+ [4 "Doohickey" 73.99 false]
+ [5 "Gadget" 82.75 true]]
+ (mt/rows (qp/process-query query))))))
+ (testing "predicate expression with not"
+ (let [query (-> (lib/query mp products)
+ (lib/expression "not_predicate" (lib/not (lib/> price 60)))
+ (lib/limit 5))
+ query (lib/with-fields query [id category price (lib/expression-ref query "not_predicate")])]
+ (is (= [[1 "Gizmo" 29.46 true]
+ [2 "Doohickey" 70.08 false]
+ [3 "Doohickey" 35.39 true]
+ [4 "Doohickey" 73.99 false]
+ [5 "Gadget" 82.75 false]]
+ (mt/rows (qp/process-query query))))))
+ (testing "predicate expression with or, and a filter"
+ (let [query (-> (lib/query mp products)
+ (lib/expression "or_predicate" (lib/or (lib/> price 60) (lib/contains category "get")))
+ (lib/filter (lib/contains category "g"))
+ (lib/limit 5))
+ query (lib/with-fields query [id category price (lib/expression-ref query "or_predicate")])]
+ (is (= [[1 "Gizmo" 29.46 false]
+ [5 "Gadget" 82.75 true]
+ [9 "Widget" 58.31 true]
+ [10 "Gizmo" 31.79 false]
+ [11 "Gadget" 88.3 true]]
+ (mt/rows (qp/process-query query))))))
+ (testing "predicate expression with and, and a filter"
+ (let [query (-> (lib/query mp products)
+ (lib/expression "and_predicate" (lib/and (lib/> price 60) (lib/contains category "get")))
+ (lib/filter (lib/contains category "g"))
+ (lib/limit 5))
+ query (lib/with-fields query [id category price (lib/expression-ref query "and_predicate")])]
+ (is (= [[1 "Gizmo" 29.46 false]
+ [5 "Gadget" 82.75 true]
+ [9 "Widget" 58.31 false]
+ [10 "Gizmo" 31.79 false]
+ [11 "Gadget" 88.3 true]]
+ (mt/rows (qp/process-query query))))))
+ (testing "nested predicate expression, and a filter"
+ (let [query (-> (lib/query mp products)
+ (lib/expression "nested_predicate" (lib/and (lib/and (lib/> price 30) (lib/< price 60))
+ (lib/or (lib/contains category "wid")
+ (lib/contains category "gad"))))
+ (lib/filter (lib/contains category "g"))
+ (lib/limit 5))
+ query (lib/with-fields query [id category price (lib/expression-ref query "nested_predicate")])]
+ (is (= [[1 "Gizmo" 29.46 false]
+ [5 "Gadget" 82.75 false]
+ [9 "Widget" 58.31 true]
+ [10 "Gizmo" 31.79 false]
+ [11 "Gadget" 88.3 false]]
+ (mt/rows (qp/process-query query)))))))))
+
(deftest filter-by-datetime-fields-test
(mt/test-driver :sqlserver
(testing "Should match datetime fields even in non-default timezone (#30454)"
diff --git a/src/metabase/driver/sql/query_processor.clj b/src/metabase/driver/sql/query_processor.clj
index fc7540ad1b1..d5d618081f4 100644
--- a/src/metabase/driver/sql/query_processor.clj
+++ b/src/metabase/driver/sql/query_processor.clj
@@ -1483,16 +1483,25 @@
;; -> [[::h2x/identifier ...] [[::h2x/identifier ...]]]
;; -> SELECT date_extract(\"x\", 'month') AS \"x\"
- `clause` will be wrapped in a ::cast if ::add-cast is found in the `clause` options
+ `clause` will be wrapped in a ::cast if ::add-cast is found in the `clause` options
;; Honey SQL 2
(as [:expression \"x\" {:base-type :type/Boolean, ::add-cast :bit}])
;; -> [[::h2x/typed [:cast ... [:raw \"bit\"]] {:database-type \"bit\"}] [[::h2x/identifier ...]]]
- ;; -> SELECT CAST(1 AS bit) AS \"x\""
+ ;; -> SELECT CAST(1 AS bit) AS \"x\"
+
+ `clause` will be wrapped in a ::case if ::wrap-in-case is found in the `clause` options
+
+ ;; Honey SQL 2
+ (as [:expression \"x\" {:base-type :type/Boolean, ::wrap-in-case true}])
+ ;; -> [:case [:> ... ...] [:inline 1] :else [:inline 0]]
+ ;; -> SELECT CASE WHEN ... > ... THEN 1 ELSE 0 END AS \"x\""
[driver clause & _unique-name-fn]
- (let [cast-type (-> clause driver-api/field-options ::add-cast)
- wrap-cast #(vector ::cast % cast-type)
- maybe-cast #(cond-> % cast-type wrap-cast)
+ (let [{cast-type ::add-cast
+ wrap-in-case? ::wrap-in-case} (driver-api/field-options clause)
+ maybe-cast #(cond-> %
+ wrap-in-case? (as-> <> (vector ::wrap-in-case <>))
+ cast-type (as-> <> (vector ::cast <> cast-type)))
honeysql-form (->honeysql driver (maybe-cast clause))
field-alias (field-clause->alias driver clause)]
(if field-alias
diff --git a/src/metabase/driver/sql/query_processor/boolean_to_comparison.clj b/src/metabase/driver/sql/query_processor/boolean_to_comparison.clj
index e8e4b4d64c8..eb4d397a72e 100644
--- a/src/metabase/driver/sql/query_processor/boolean_to_comparison.clj
+++ b/src/metabase/driver/sql/query_processor/boolean_to_comparison.clj
@@ -9,6 +9,7 @@
(:require
[metabase.driver-api.core :as driver-api]
[metabase.driver.sql.query-processor :as sql.qp]
+ [metabase.lib.schema.filter :as lib.schema.filter]
[metabase.util.performance :refer [some mapv update-keys]]))
;; Oracle and SQLServer (and maybe others) use 0 and 1 for boolean constants, but, for example, none of the following
@@ -79,6 +80,17 @@
(and (driver-api/is-clause? :expression clause)
(boolean-value-clause? (driver-api/expression-with-name sql.qp/*inner-query* (second clause)))))
+(defn predicate-expression-clause?
+ "Is `clause` an :expression clause containing a predicate operator (e.g. :and, :=, :contains, etc.)?
+
+ This function expects to be called in a context where sql.qp/*inner-query* is bound, so that it can lookup
+ expression refs by name, if necessary, to determine whether the expression is a predicate operator."
+ [clause]
+ (and (driver-api/is-clause? :expression clause)
+ (->> (second clause)
+ (driver-api/expression-with-name sql.qp/*inner-query*)
+ (driver-api/is-clause? lib.schema.filter/predicate-operators))))
+
(defn boolean->comparison
"Convert boolean field refs or expression literals to equivalent boolean comparison expressions.
diff --git a/src/metabase/lib/schema/filter.cljc b/src/metabase/lib/schema/filter.cljc
index 0959ff13783..a34618c5d41 100644
--- a/src/metabase/lib/schema/filter.cljc
+++ b/src/metabase/lib/schema/filter.cljc
@@ -29,6 +29,10 @@
(expression/comparable-expressions? (get argv i) (get argv j)))
compared-position-pairs)))))]]))
+(def predicate-operators
+ "Set of predicate operators that can be user in filter clauses."
+ #{:and :or :not := :!= :> :>= :< :<= :is-null :not-null :is-empty :not-empty :starts-with :ends-with :contains :does-not-contain :between :inside})
+
(mr/def ::default-filter-operator
"Filter operators that should be supported by any column type. Note that the FE allows only `:is-empty` and
`:not-empty` for string columns."