Skip to content
Snippets Groups Projects
Unverified Commit c0f9d8de authored by Paul Rosenzweig's avatar Paul Rosenzweig Committed by GitHub
Browse files

Drop `special_type` when aggregating PKs and FKs (#11221)

parent 515e7e9e
No related merge requests found
......@@ -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,
......
......@@ -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
}
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");
});
});
});
});
});
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment