Skip to content

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

Add tests for already fixed bugs (#73463)

Riley Thompsonbc15bb95d1a6
diff --git a/enterprise/backend/test/metabase_enterprise/cache/strategies_test.clj b/enterprise/backend/test/metabase_enterprise/cache/strategies_test.clj
index 2fc8230c02b..a8b739fa2eb 100644
--- a/enterprise/backend/test/metabase_enterprise/cache/strategies_test.clj
+++ b/enterprise/backend/test/metabase_enterprise/cache/strategies_test.clj
@@ -4,6 +4,7 @@
[java-time.api :as t]
[metabase-enterprise.cache.strategies :as strategies]
[metabase-enterprise.cache.task.refresh-cache-configs :as task.cache]
+ [metabase.lib.core :as lib]
[metabase.queries.models.query :as query]
[metabase.query-processor.card :as qp.card]
[metabase.query-processor.test :as qp]
@@ -181,3 +182,50 @@
(let [q (#'qp.card/query-for-card card4 [] {} {} {})]
(is (=? {:type :ttl :multiplier 200}
(:cache-strategy q))))))))))))
+
+(deftest dashboard-caching-with-parameterized-native-query-test
+ ;; Regression test for https://github.com/metabase/metabase/issues/45412
+ ;; Dashboard-level caching should work with parameterized native queries:
+ ;; same params → cache hit, different params → cache miss.
+ (mt/with-empty-h2-app-db!
+ (mt/with-premium-features #{:cache-granular-controls}
+ (let [native-query (-> (lib/native-query (mt/metadata-provider) "SELECT {{num}} AS result")
+ (lib/with-template-tags {"num" {:id "abc123"
+ :name "num"
+ :display-name "Number"
+ :type :number}}))]
+ (mt/with-temp [:model/Card card {:dataset_query native-query}
+ :model/Dashboard dashboard {}
+ :model/DashboardCard dashcard {:dashboard_id (:id dashboard)
+ :card_id (:id card)
+ :parameter_mappings
+ [{:parameter_id "num_param"
+ :card_id (:id card)
+ :target [:variable [:template-tag "num"]]}]}
+ :model/CacheConfig _ {:model "dashboard"
+ :model_id (:id dashboard)
+ :strategy :duration
+ :config {:duration 1
+ :unit "hours"}}]
+ (mt/with-model-cleanup [[:model/QueryCache :updated_at]]
+ (mt/with-clock #t "2024-02-13T10:00:00Z"
+ (are [param-val cache-details] (let [params [{:id "num_param"
+ :type :number
+ :target [:variable [:template-tag "num"]]
+ :value param-val}]
+ query-result (-> (#'qp.card/query-for-card card params {} {}
+ {:dashboard-id (:id dashboard)
+ :dashcard-id (:id dashcard)})
+ (qp/process-query))]
+ (= [[param-val]] (mt/rows query-result))
+ (is (=? cache-details (:cache/details query-result))))
+ ;; first run is not cached
+ 42 {:stored true, :hash some?}
+ ;; second run with same param is cached
+ 42 {:cached true, :updated_at some?, :hash some?}
+ ;; different param is not cached
+ 99 {:stored true, :hash some?}
+ ;; second run with param is cached
+ 99 {:cached true, :updated_at some?, :hash some?}
+ ;; original param is still cached
+ 42 {:cached true, :updated_at some?, :hash some?}))))))))
diff --git a/test/metabase/query_processor/drill_thru_e2e_test.clj b/test/metabase/query_processor/drill_thru_e2e_test.clj
index 1c034c5ce0b..c626ba92ec6 100644
--- a/test/metabase/query_processor/drill_thru_e2e_test.clj
+++ b/test/metabase/query_processor/drill_thru_e2e_test.clj
@@ -6,6 +6,7 @@
[metabase.lib.metadata :as lib.metadata]
[metabase.lib.ref :as lib.ref]
[metabase.lib.test-util :as lib.tu]
+ [metabase.lib.test-util.notebook-helpers :as lib.tu.notebook]
^{:clj-kondo/ignore [:deprecated-namespace]} [metabase.query-processor.store :as qp.store]
[metabase.query-processor.test :as qp]
[metabase.test :as mt]))
@@ -142,3 +143,56 @@
["Organic" 738]
["Twitter" 795]]
(mt/formatted-rows [str int] (qp/process-query query'))))))))))
+
+(deftest ^:parallel nested-custom-column-pivot-drill-thru-test
+ (testing "Drill-through on pivot table with nested custom column should not error (#64985)"
+ ;; Reproduces a bug where drilling through a pivot table breaks with "Column not found"
+ ;; when the breakout is a custom column (expression) defined after an aggregation stage.
+ ;; The drill produced a WHERE clause referencing "source.test" but "test" is a computed
+ ;; expression (CONCAT), not a real column in the source subquery.
+ (mt/dataset test-data
+ (qp.store/with-metadata-provider (mt/id)
+ (let [mp (qp.store/metadata-provider)
+ products (lib.metadata/table mp (mt/id :products))
+ price (lib.metadata/field mp (mt/id :products :price))
+ category (lib.metadata/field mp (mt/id :products :category))
+ base-query (-> (lib/query mp products)
+ (lib/aggregate (lib/count))
+ (lib/aggregate (lib/sum price))
+ (lib/breakout category)
+ (lib/append-stage))
+ base-cols (lib/returned-columns base-query)
+ cat-col (lib.tu.notebook/find-col-with-spec base-query base-cols {} {:display-name "Category"})
+ sum-col (lib.tu.notebook/find-col-with-spec base-query base-cols {} {:display-name "Sum of Price"})
+ with-expr (lib/expression base-query "test" (lib/concat (lib/ref cat-col) ""))
+ breakout-cols (lib/breakoutable-columns with-expr)
+ test-col-b (lib.tu.notebook/find-col-with-spec with-expr breakout-cols {} {:display-name "test"})
+ query (-> with-expr
+ (lib/aggregate (lib/count))
+ (lib/aggregate (lib/sum sum-col))
+ (lib/breakout test-col-b))
+ ;; Simulate pivot cell click on "Gizmo" row
+ ret-cols (lib/returned-columns query)
+ test-col (lib.tu.notebook/find-col-with-spec query ret-cols {} {:display-name "test"})
+ count-col (lib.tu.notebook/find-col-with-spec query ret-cols {} {:display-name "Count"})
+ sum-sum-col (lib.tu.notebook/find-col-with-spec query ret-cols {} {:display-name "Sum of Sum of Price"})
+ context {:column count-col
+ :column-ref (lib.ref/ref count-col)
+ :value 1
+ :row [{:column test-col
+ :column-ref (lib.ref/ref test-col)
+ :value "Gizmo"}
+ {:column count-col
+ :column-ref (lib.ref/ref count-col)
+ :value 1}
+ {:column sum-sum-col
+ :column-ref (lib.ref/ref sum-sum-col)
+ :value 1}]
+ :dimensions [{:column test-col
+ :column-ref (lib.ref/ref test-col)
+ :value "Gizmo"}]}
+ ur-drill (m/find-first #(= (:type %) :drill-thru/underlying-records) (lib/available-drill-thrus query context))
+ drill-query (lib/drill-thru query ur-drill)]
+ (mt/with-native-query-testing-context drill-query
+ (is (= [["Gizmo" 51 2834.88 "Gizmo"]]
+ (mt/formatted-rows [str int 2.0 str] (qp/process-query drill-query))))))))))
diff --git a/test/metabase/query_processor/offset_test.clj b/test/metabase/query_processor/offset_test.clj
index b1f1e631a68..c1ae209430c 100644
--- a/test/metabase/query_processor/offset_test.clj
+++ b/test/metabase/query_processor/offset_test.clj
@@ -238,3 +238,25 @@
(testing "Offset column is correctly shifted according to aggregation column"
(is (= (butlast ag-col-vals)
(drop 1 ofs-ag-col-vals))))))))))
+
+(deftest ^:parallel offset-with-date-field-in-breakout-and-custom-expression
+ (testing "A query with an offset and a date field in the breakout and a custom expression works (#65503)"
+ (mt/test-drivers (mt/normal-drivers-with-feature :window-functions/offset)
+ (let [mp (mt/metadata-provider)
+ orders (lib.metadata/table mp (mt/id :orders))
+ created-at (lib.metadata/field mp (mt/id :orders :created_at))
+ total (lib.metadata/field mp (mt/id :orders :total))
+ query (-> (lib/query mp orders)
+ (lib/expression "test" created-at)
+ (lib/breakout (lib/with-temporal-bucket created-at :year))
+ (lib/aggregate (lib/sum total))
+ (lib/aggregate (lib/offset (lib/sum total) -1))
+ (assoc-in [:middleware :format-rows?] false))]
+ (is (= [[#t "2016-01-01" 42156.94 nil]
+ [#t "2017-01-01" 205256.4 42156.94]
+ [#t "2018-01-01" 510043.47 205256.4]
+ [#t "2019-01-01" 577064.96 510043.47]
+ [#t "2020-01-01" 176095.93 577064.96]]
+ (->> query
+ (qp/process-query)
+ (mt/formatted-rows [->local-date 2.0 2.0]))))))))