The easy-to-use open source Business Intelligence and Embedded Analytics tool that lets everyone work with data :bar_chart:
TypeScript analyticsbibusiness-intelligencebusinessintelligenceclojuredashboarddatadata-analysisdata-visualizationdatabasemetabasemysqlpostgrespostgresqlreportingslacksql-editorvisualization
Fix duplicated rows on the Erroring questions page (#79520)
Stas Gavrylov6b311219dc4a
diff --git a/enterprise/backend/src/metabase_enterprise/audit_app/pages/common/cards.clj b/enterprise/backend/src/metabase_enterprise/audit_app/pages/common/cards.cljindex cb88672d4a6..ccca3f62afd 100644--- a/enterprise/backend/src/metabase_enterprise/audit_app/pages/common/cards.clj+++ b/enterprise/backend/src/metabase_enterprise/audit_app/pages/common/cards.clj@@ -1,16 +1,26 @@(ns metabase-enterprise.audit-app.pages.common.cards)(def latest-qe- "HoneySQL for a CTE to get latest QueryExecution for a Card."- [:latest_qe {:select [:query_execution.card_id :error :query_execution.started_at]- :from [:query_execution]- ;; Join on BOTH card_id and started_at, because some cards share the same timestamp.- :join [[{:select [:card_id [:%max.started_at :started_at]]- :from [:query_execution]- :group-by [:card_id]} :inner_qe]- [:and- [:= :query_execution.card_id :inner_qe.card_id]- [:= :query_execution.started_at :inner_qe.started_at]]]}])+ "HoneySQL for a CTE to get latest QueryExecution for a Card. Exactly one row per Card."+ ;; A Card can have several executions sharing its max started_at, so we need to rank them first.+ [:latest_qe {:select [:card_id :error :started_at]+ :from [[{:select [:query_execution.card_id+ :query_execution.error+ :query_execution.started_at+ [[:over [[:row_number]+ {:partition-by [:query_execution.card_id]+ :order-by [[:query_execution.id :desc]]}+ :rn]]]]+ :from [:query_execution]+ ;; Join on BOTH card_id and started_at, because some cards share the same timestamp.+ :join [[{:select [:card_id [:%max.started_at :started_at]]+ :from [:query_execution]+ :group-by [:card_id]} :inner_qe]+ [:and+ [:= :query_execution.card_id :inner_qe.card_id]+ [:= :query_execution.started_at :inner_qe.started_at]]]}+ :ranked_qe]]+ :where [:= :rn [:inline 1]]}])(def query-runs"HoneySQL for a CTE to include the total number of queries for each Card forever."diff --git a/enterprise/backend/src/metabase_enterprise/audit_app/pages/queries.clj b/enterprise/backend/src/metabase_enterprise/audit_app/pages/queries.cljindex 8db4b6e800c..f8d79cab651 100644--- a/enterprise/backend/src/metabase_enterprise/audit_app/pages/queries.clj+++ b/enterprise/backend/src/metabase_enterprise/audit_app/pages/queries.clj@@ -1,5 +1,6 @@(ns metabase-enterprise.audit-app.pages.queries(:require+ [honey.sql.helpers :as sql.helpers][metabase-enterprise.audit-app.interface :as audit.i][metabase-enterprise.audit-app.pages.common :as common][metabase-enterprise.audit-app.pages.common.cards :as cards]@@ -86,4 +87,5 @@(common/add-search-clause search-term :card.name :latest_qe.error :db.name coll-name)(common/add-sort-clause(or sort-column "card.name")- (or sort-direction "asc")))))})))+ (or sort-direction "asc"))+ (sql.helpers/order-by [:card.id :asc]))))})))diff --git a/enterprise/backend/test/metabase_enterprise/audit_app/pages/queries_test.clj b/enterprise/backend/test/metabase_enterprise/audit_app/pages/queries_test.cljindex 27c666b1709..6cd95dba66c 100644--- a/enterprise/backend/test/metabase_enterprise/audit_app/pages/queries_test.clj+++ b/enterprise/backend/test/metabase_enterprise/audit_app/pages/queries_test.clj@@ -116,3 +116,59 @@(is (= [erroring-id] (map first rows))))(testing "and the total_count agrees"(is (= 1 (bad-table-total "collision-error-marker-abc" nil nil))))))))))++(deftest bad-table-dedupes-tied-executions-for-one-card-test+ (testing "a card whose latest executions tie on started_at appears once, not once per tied execution"+ (mt/test-helpers-set-global-values!+ (let [shared-ts #t "2026-04-04T09:00:00Z"]+ (mt/with-temp [:model/Card {card-id :id} {:name "Kestrel tied-run card"}+ ;; an older run, to prove ranking picks from the latest timestamp only+ :model/QueryExecution _ (merge query-execution-defaults+ {:card_id card-id+ :executor_id (mt/user->id :crowberto)+ :started_at #t "2026-04-04T08:00:00Z"+ :error "tied-run-marker-de stale error"})+ ;; two runs of the SAME card landing in the same clock tick+ :model/QueryExecution _ (merge query-execution-defaults+ {:card_id card-id+ :executor_id (mt/user->id :crowberto)+ :started_at shared-ts+ :error "tied-run-marker-de first error"})+ :model/QueryExecution _ (merge query-execution-defaults+ {:card_id card-id+ :executor_id (mt/user->id :crowberto)+ :started_at shared-ts+ :error "tied-run-marker-de second error"})]+ (let [rows (mt/rows (run-query ::queries/bad-table :args ["tied-run-marker-de" nil nil]))]+ (testing "one row for the card (the un-deduped join returns one per tied execution)"+ (is (= 1 (count rows)))+ (is (= [card-id] (map first rows))))+ (testing "the surviving row is the most recently inserted of the tied executions"+ (is (= "tied-run-marker-de second error..." (nth (first rows) 2))))+ (testing "and the total_count agrees"+ (is (= 1 (bad-table-total "tied-run-marker-de" nil nil))))))))))++(deftest bad-table-paging-is-stable-across-tied-sort-values-test+ (testing "cards tied on the sort column don't repeat across pages: each page yields a distinct card"+ (mt/test-helpers-set-global-values!+ (let [shared-ts #t "2026-05-05T15:00:00Z"+ page (fn [offset]+ (mt/rows (run-query ::queries/bad-table+ :args ["paging-tie-marker-fg" "last_run_at" "desc"]+ :limit 1+ :offset offset)))]+ (mt/with-temp [:model/Card {card-a :id} {:name "Avocet paging card"}+ :model/Card {card-b :id} {:name "Bittern paging card"}+ :model/QueryExecution _ (merge query-execution-defaults+ {:card_id card-a+ :executor_id (mt/user->id :crowberto)+ :started_at shared-ts+ :error "paging-tie-marker-fg"})+ :model/QueryExecution _ (merge query-execution-defaults+ {:card_id card-b+ :executor_id (mt/user->id :crowberto)+ :started_at shared-ts+ :error "paging-tie-marker-fg"})]+ (is (= 2 (bad-table-total "paging-tie-marker-fg" "last_run_at" "desc")))+ (is (= #{card-a card-b}+ (set (concat (map first (page 0)) (map first (page 1)))))))))))