Skip to content

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

Fix: No drill-thru to filter a table by clicking a NULL FK value (#62569)

github-actions[bot]71f7fe60f16b
diff --git a/e2e/test/scenarios/question-reproductions/reproductions.cy.spec.ts b/e2e/test/scenarios/question-reproductions/reproductions.cy.spec.ts
index d3e4393a77d..b30b3d7fd1a 100644
--- a/e2e/test/scenarios/question-reproductions/reproductions.cy.spec.ts
+++ b/e2e/test/scenarios/question-reproductions/reproductions.cy.spec.ts
@@ -1202,3 +1202,91 @@ describe("issue 52872", () => {
});
});
});
+
+describe("issue 35561", () => {
+ beforeEach(() => {
+ H.restore();
+ cy.signInAsAdmin();
+ });
+
+ it("should display a functional fk drill for both null and non-null values", () => {
+ H.createNativeQuestion(
+ {
+ name: "Model",
+ native: {
+ query: `SELECT 1 AS ID, CAST(NULL AS INT) AS USER_ID
+UNION ALL
+SELECT 2 AS ID, 3 AS USER_ID
+`,
+ },
+ type: "model",
+ },
+ {},
+ ).then(async ({ body: model }) => {
+ await cy.request("POST", `/api/card/${model.id}/query`);
+ await H.setModelMetadata(model.id, (field) => {
+ if (field.name === "USER_ID") {
+ return {
+ ...field,
+ display_name: "User ID",
+ semantic_type: "type/FK",
+ fk_target_field_id: ORDERS.ID,
+ };
+ }
+ return field;
+ });
+ H.createQuestion(
+ {
+ name: "Question",
+ query: {
+ "source-table": `card__${model.id}`,
+ },
+ display: "table",
+ },
+ { wrapId: true, visitQuestion: true },
+ );
+ });
+
+ cy.findByTestId("table-root")
+ .findAllByRole("row")
+ .first()
+ .within(() => {
+ cy.get("[data-column-id='USER_ID']").click();
+ });
+ H.popover().within(() => {
+ cy.findByText("View Models with no User").should("be.visible").click();
+ });
+ cy.findByTestId("table-body").within(() => {
+ cy.findAllByRole("row").should("have.length", 1);
+ cy.findAllByRole("row")
+ .first()
+ .within(() => {
+ cy.get("[data-column-id='USER_ID']").within(() => {
+ cy.get("[data-testid=cell-data]").should("not.exist");
+ });
+ });
+ });
+
+ H.visitQuestion("@questionId");
+
+ cy.findByTestId("table-root")
+ .findAllByRole("row")
+ .eq(1)
+ .within(() => {
+ cy.get("[data-column-id='USER_ID']").click();
+ });
+ H.popover().within(() => {
+ cy.findByText("View this User's Models").should("be.visible").click();
+ });
+ cy.findByTestId("table-body").within(() => {
+ cy.findAllByRole("row").should("have.length", 1);
+ cy.findAllByRole("row")
+ .first()
+ .within(() => {
+ cy.get("[data-column-id='USER_ID']").within(() => {
+ cy.get("[data-testid=cell-data]").should("have.text", "3");
+ });
+ });
+ });
+ });
+});
diff --git a/frontend/src/metabase/querying/drills/utils/fk-filter-drill.ts b/frontend/src/metabase/querying/drills/utils/fk-filter-drill.ts
index 9607000321a..77daf020227 100644
--- a/frontend/src/metabase/querying/drills/utils/fk-filter-drill.ts
+++ b/frontend/src/metabase/querying/drills/utils/fk-filter-drill.ts
@@ -13,6 +13,7 @@ export const fkFilterDrill: Drill<Lib.FKFilterDrillThruInfo> = ({
drill,
drillInfo,
applyDrill,
+ clicked,
}) => {
const { tableName, columnName } = drillInfo;
const locale = dayjs.locale();
@@ -20,10 +21,15 @@ export const fkFilterDrill: Drill<Lib.FKFilterDrillThruInfo> = ({
const columnTitle =
locale === "en" ? singularize(stripId(columnName)) : stripId(columnName);
+ const isNullValue = clicked?.value === null;
+ const title = isNullValue
+ ? t`View ${tableTitle} with no ${columnTitle}`
+ : t`View this ${columnTitle}'s ${tableTitle}`;
+
return [
{
name: "fk-filter",
- title: t`View this ${columnTitle}'s ${tableTitle}`,
+ title,
section: "standalone_filter",
icon: "filter",
buttonType: "horizontal",
diff --git a/src/metabase/lib/drill_thru/fk_filter.cljc b/src/metabase/lib/drill_thru/fk_filter.cljc
index 5c487256cc3..5a3e007aa33 100644
--- a/src/metabase/lib/drill_thru/fk_filter.cljc
+++ b/src/metabase/lib/drill_thru/fk_filter.cljc
@@ -46,15 +46,17 @@
{:keys [column column-ref value], :as _context} :- ::lib.schema.drill-thru/context]
(when (and column
(some? value)
- (not= value :null) ; If the FK is null, don't show this option.
(lib.drill-thru.common/mbql-stage? query stage-number)
(not (lib.types.isa/primary-key? column))
(lib.types.isa/foreign-key? column))
(let [source (or (some->> query lib.util/source-table-id (lib.metadata/table query))
- (some->> query lib.util/source-card-id (lib.metadata/card query)))]
+ (some->> query lib.util/source-card-id (lib.metadata/card query)))
+ filter-expr (if (= value :null)
+ (lib.options/ensure-uuid (lib.filter/is-null column-ref))
+ (lib.options/ensure-uuid (lib.filter/= column-ref value)))]
{:lib/type :metabase.lib.drill-thru/drill-thru
:type :drill-thru/fk-filter
- :filter (lib.options/ensure-uuid [:= {} column-ref value])
+ :filter filter-expr
:column-name (lib.metadata.calculation/display-name query stage-number column :long)
:table-name (lib.metadata.calculation/display-name query 0 source)})))
diff --git a/test/metabase/lib/drill_thru/fk_filter_test.cljc b/test/metabase/lib/drill_thru/fk_filter_test.cljc
index a0f11f3cff9..a4b280eb7dd 100644
--- a/test/metabase/lib/drill_thru/fk_filter_test.cljc
+++ b/test/metabase/lib/drill_thru/fk_filter_test.cljc
@@ -11,11 +11,10 @@
#?(:cljs (comment metabase.test-runner.assert-exprs.approximately-equal/keep-me))
(deftest ^:parallel fk-filter-availability-test
- (testing "fk-filter is available for cell clicks on FKs with non-NULL values"
+ (testing "fk-filter is available for cell clicks on FKs with any value including NULL"
(doseq [[test-case context {:keys [click column-type]}] (canned/canned-clicks)]
(if (and (= click :cell)
- (= column-type :fk)
- (not= (:value context) :null))
+ (= column-type :fk))
(is (canned/returned test-case context :drill-thru/fk-filter))
(is (not (canned/returned test-case context :drill-thru/fk-filter)))))))
@@ -52,23 +51,19 @@
:query-type :aggregated
:column-name "max"})))
-(deftest ^:parallel do-not-return-fk-filter-for-null-fk-test
- (testing "#13957 if this is an FK column but the value clicked is NULL, don't show the FK filter drill"
+(deftest ^:parallel return-fk-filter-for-null-fk-test
+ (testing "#35561 FK filter drill should be available for NULL FK values to filter by IS NULL"
(let [test-case {:drill-type :drill-thru/fk-filter
:click-type :cell
:query-type :unaggregated
:column-name "PRODUCT_ID"
:expected {:type :drill-thru/fk-filter}}
- ;{:keys [query row]} (lib.drill-thru.tu/query-and-row-for-test-case test-case)
- ;context (lib.drill-thru.tu/test-case-context query row test-case)
- ;drill-types #(->> % (lib/available-drill-thrus query) (map :type) set)
row (get-in lib.drill-thru.tu/test-queries ["ORDERS" :unaggregated :row])]
(testing "returned with non-NULL value"
(lib.drill-thru.tu/test-returns-drill test-case))
- (testing "not returned with NULL value"
- (-> test-case
- (assoc :custom-row (assoc row "PRODUCT_ID" nil))
- (dissoc :expected))))))
+ (testing "returned with NULL value"
+ (lib.drill-thru.tu/test-returns-drill
+ (assoc test-case :custom-row (assoc row "PRODUCT_ID" nil)))))))
(deftest ^:parallel fk-filter-on-model-test
(testing "FK filter drill should not appear on native query models (#35689, #36633)"
@@ -175,4 +170,20 @@
["ORDERS" :aggregated :row "PRODUCT_ID"])]]}]}
:expected-native {:stages [{:filters [[:= {} [:field {} "PRODUCT_ID"]
(get-in lib.drill-thru.tu/test-queries
- ["ORDERS" :aggregated :row "PRODUCT_ID"])]]}]}}))))
+ ["ORDERS" :aggregated :row "PRODUCT_ID"])]]}]}})))
+
+ (testing "adds an is-null filter for NULL FK values"
+ (let [row (get-in lib.drill-thru.tu/test-queries ["ORDERS" :unaggregated :row])]
+ (lib.drill-thru.tu/test-drill-application
+ {:click-type :cell
+ :query-type :unaggregated
+ :column-name "PRODUCT_ID"
+ :custom-row (assoc row "PRODUCT_ID" nil)
+ :drill-type :drill-thru/fk-filter
+ :expected {:lib/type :metabase.lib.drill-thru/drill-thru
+ :type :drill-thru/fk-filter
+ :column-name "Product ID"
+ :table-name string?}
+ :expected-query {:stages [{:filters [[:is-null {}
+ [:field {} (lib.drill-thru.tu/field-key=
+ "PRODUCT_ID" (meta/id :orders :product-id))]]]}]}}))))