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
[QP] Only label remaps for columns that actually need it (#65860)
Braden Shepherdson51067eb58634
diff --git a/enterprise/backend/test/metabase_enterprise/sandbox/query_processor/middleware/sandboxing_test.clj b/enterprise/backend/test/metabase_enterprise/sandbox/query_processor/middleware/sandboxing_test.cljindex 77824739ae2..fc281e153de 100644--- a/enterprise/backend/test/metabase_enterprise/sandbox/query_processor/middleware/sandboxing_test.clj+++ b/enterprise/backend/test/metabase_enterprise/sandbox/query_processor/middleware/sandboxing_test.clj@@ -1627,6 +1627,39 @@(mt/rows(qp/process-query (query))))))))+(deftest fk-remaps-are-marked-correctly-test+ (testing "when a selected column is the target of an external (FK) remapping, it is not marked as remapped (#65726)"+ ;; Eg. COUNT of orders by Products->Title with Orders.PRODUCT_ID mapped to Products->Title.+ ;; There's no remapping need there: the breakout is actually on the title field already.+ ;; TODO: (Braden 2025-11-13) I think remappings and breakouts are pretty janky - the Right Thing is to walk+ ;; backward from all "selected" fields in the last stage to originals, and include any remaps they have.+ (met/with-gtaps! {:gtaps {:orders {:remappings {"user_id" ["variable" [:field (mt/id :orders :user_id) nil]]}}},+ :attributes {"user_id" 1}}+ (let [mp (lib.tu/remap-metadata-provider+ (mt/metadata-provider)+ (mt/id :orders :product_id)+ (mt/id :products :title))]+ (qp.store/with-metadata-provider mp+ (let [query (-> (lib/query mp (lib.metadata/table mp (mt/id :orders)))+ (lib/aggregate (lib/count))+ (lib/breakout (lib.metadata/field mp (mt/id :products :title)))+ (lib/limit 5))]+ (testing "Query should complete successfully with normal processing"+ (let [result (qp/process-query query)]+ (is (= :completed (:status result)))+ (is (=? [{:name "TITLE"+ :id (mt/id :products :title)+ :lib/original-fk-field-id (mt/id :orders :product_id)+ :remapped_from (symbol "nil #_\"key is not present.\"")}+ {:name "count"}]+ (mt/cols result)))+ (is (= [["Awesome Bronze Plate" 1]+ ["Awesome Concrete Shoes" 1]+ ["Ergonomic Silk Table" 1]+ ["Fantastic Wool Shirt" 1]+ ["Mediocre Rubber Shoes" 1]]+ (mt/rows result)))))))))))+(deftest datetime-extraction-to-int-test(testing "Downloading CSV/XLSX for query with COUNT grouped by day of month should work for sandboxed users (#UXW-660)"(met/with-gtaps! {:gtaps {:orders {:remappings {"user_id" ["variable" [:field (mt/id :orders :user_id) nil]]}}},diff --git a/src/metabase/query_processor/middleware/add_remaps.clj b/src/metabase/query_processor/middleware/add_remaps.cljindex 5cf9e742ef9..35c959f2962 100644--- a/src/metabase/query_processor/middleware/add_remaps.clj+++ b/src/metabase/query_processor/middleware/add_remaps.clj@@ -349,10 +349,19 @@;; last stage (only stage) is native(= (:lib/type (lib/query-stage query -1)) :mbql.stage/native))query- (let [{:keys [remaps query]} (add-fk-remaps query)]+ (let [{:keys [remaps query]} (add-fk-remaps query)+ returned-ids (into #{} (keep :id) (lib/returned-columns query))+ ;; Only retain those remappings which actually apply to returned columns.+ ;; Otherwise, if an FK has an external remapping (say, Orders.PRODUCT_ID -> Product.TITLE) and we implicitly+ ;; join `Product.TITLE` directly, we actually don't want to mark that column as :remapped_from anything -+ ;; it wasn't remapped, it was explicitly referenced. So if the FK doesn't appear as a returned column, any+ ;; remaps we might have inherited from earlier stages etc. don't apply. See #65726+ remaps (into [] (comp (filter (comp returned-ids :field-id))+ ;; Convert the remappings to plain maps rather than record types.+ (map #(into {} %)))+ remaps)](cond-> query- ;; convert the remappings to plain maps so we don't have to look at record type nonsense everywhere- (seq remaps) (assoc ::external-remaps (mapv (partial into {}) remaps))))))+ (seq remaps) (assoc ::external-remaps remaps)))));;;; Post-processing