diff --git a/frontend/src/metabase-lib/lib/Dimension.js b/frontend/src/metabase-lib/lib/Dimension.js index add910c0baccb83d02ac8214e305f61fdf5bfab5..96ba79779f9e28a5f7fb957441d7af38cb51e672 100644 --- a/frontend/src/metabase-lib/lib/Dimension.js +++ b/frontend/src/metabase-lib/lib/Dimension.js @@ -821,6 +821,11 @@ export class ExpressionDimension extends Dimension { } } +// These types aren't aggregated. e.g. if you take the distinct count of a FK +// column, you now have a normal integer and should see relevant filters for +// that type. +const UNAGGREGATED_SPECIAL_TYPES = new Set([TYPE.FK, TYPE.PK]); + /** * Aggregation reference, `["aggregation", aggregation-index]` */ @@ -841,8 +846,11 @@ export class AggregationDimension extends Dimension { column(extra = {}) { const aggregation = this.aggregation(); + const { special_type, ...column } = super.column(); return { - ...super.column(), + ...column, + // don't pass through `special_type` when aggregating these types + ...(!UNAGGREGATED_SPECIAL_TYPES.has(special_type) && { special_type }), base_type: aggregation ? aggregation.baseType() : TYPE.Float, source: "aggregation", ...extra, diff --git a/frontend/test/__support__/sample_dataset_fixture.json b/frontend/test/__support__/sample_dataset_fixture.json index 92a998a6b8eeb6e30da39e67ae466e55d32f9045..a00bd06cd9dbe6bc4870384d2e5240f048387c5e 100644 --- a/frontend/test/__support__/sample_dataset_fixture.json +++ b/frontend/test/__support__/sample_dataset_fixture.json @@ -521,7 +521,7 @@ "6": { "description": "The total billed amount.", "table_id": 1, - "special_type": null, + "special_type": "type/Currency", "name": "TOTAL", "caveats": null, "fk_target_field_id": null, @@ -1387,4 +1387,4 @@ 1 ] } -} \ No newline at end of file +} diff --git a/frontend/test/metabase-lib/lib/Dimension.unit.spec.js b/frontend/test/metabase-lib/lib/Dimension.unit.spec.js index d5fd21737711ca1906d972abea0c05e243cf2793..ab733bd5fd686bcd9f0037fe4eb65b33a80430b2 100644 --- a/frontend/test/metabase-lib/lib/Dimension.unit.spec.js +++ b/frontend/test/metabase-lib/lib/Dimension.unit.spec.js @@ -1,5 +1,11 @@ import Dimension, { FKDimension } from "metabase-lib/lib/Dimension"; -import { metadata, ORDERS, PRODUCTS } from "__support__/sample_dataset_fixture"; +import StructuredQuery from "metabase-lib/lib/queries/StructuredQuery"; +import { + metadata, + ORDERS, + PRODUCTS, + SAMPLE_DATASET, +} from "__support__/sample_dataset_fixture"; describe("Dimension", () => { describe("STATIC METHODS", () => { @@ -195,7 +201,7 @@ describe("Dimension", () => { name: "TOTAL", display_name: "Total", base_type: "type/Float", - special_type: null, + special_type: "type/Currency", field_ref: ["field-id", ORDERS.TOTAL.id], }); }); @@ -392,7 +398,7 @@ describe("Dimension", () => { name: "TOTAL", display_name: "Total", base_type: "type/Float", - special_type: null, + special_type: "type/Currency", field_ref: [ "binning-strategy", ["field-id", ORDERS.TOTAL.id], @@ -486,7 +492,7 @@ describe("Dimension", () => { name: "TOTAL", display_name: "Total", base_type: "type/Float", - special_type: null, + special_type: "type/Currency", field_ref: ["joined-field", "join1", ["field-id", ORDERS.TOTAL.id]], }); }); @@ -502,6 +508,32 @@ describe("Dimension", () => { expect(dimension.mbql()).toEqual(["aggregation", 1]); }); }); + + describe("column()", () => { + function sumOf(column) { + const query = new StructuredQuery(ORDERS.question(), { + type: "query", + database: SAMPLE_DATASET.id, + query: { + "source-table": ORDERS.id, + aggregation: [["sum", ["field-id", column.id]]], + }, + }); + return Dimension.parseMBQL(["aggregation", 0], metadata, query); + } + + it("should clear unaggregated special types", () => { + const { special_type } = sumOf(ORDERS.PRODUCT_ID).column(); + + expect(special_type).toBe(undefined); + }); + + it("should retain aggregated special types", () => { + const { special_type } = sumOf(ORDERS.TOTAL).column(); + + expect(special_type).toBe("type/Currency"); + }); + }); }); }); });