From 7b4de21ade0f8ed16c9f0150a09f4499498ffc46 Mon Sep 17 00:00:00 2001 From: Ariya Hidayat <ariya@metabase.com> Date: Fri, 25 Feb 2022 18:39:43 -0800 Subject: [PATCH] Teach custom column's type inference about semantic_type (#20748) --- frontend/src/metabase-lib/lib/Dimension.ts | 17 +++++++++++------ .../metabase-lib/lib/Dimension.unit.spec.js | 2 +- 2 files changed, 12 insertions(+), 7 deletions(-) diff --git a/frontend/src/metabase-lib/lib/Dimension.ts b/frontend/src/metabase-lib/lib/Dimension.ts index 826e3ca03ae..588f468f93c 100644 --- a/frontend/src/metabase-lib/lib/Dimension.ts +++ b/frontend/src/metabase-lib/lib/Dimension.ts @@ -1185,28 +1185,32 @@ export class ExpressionDimension extends Dimension { field() { const query = this._query; const table = query ? query.table() : null; - let type = MONOTYPE.Number; // fallback + + // fallback + let type = MONOTYPE.Number; + let semantic_type = null; if (query) { const datasetQuery = query.query(); const expressions = datasetQuery?.expressions ?? {}; + const expr = expressions[this.name()]; - const env = mbql => { + const field = mbql => { const dimension = Dimension.parseMBQL( mbql, this._metadata, this._query, ); - return dimension.field().base_type; + return dimension?.field(); }; - type = infer(expressions[this.name()], env); + type = infer(expr, mbql => field(mbql)?.base_type); + semantic_type = infer(expr, mbql => field(mbql)?.semantic_type); } else { type = infer(this._expressionName); } let base_type = type; - if (!type.startsWith("type/")) { base_type = "type/Float"; // fallback @@ -1222,14 +1226,15 @@ export class ExpressionDimension extends Dimension { default: break; } + semantic_type = base_type; } return new Field({ id: this.mbql(), name: this.name(), display_name: this.displayName(), - semantic_type: null, base_type, + semantic_type, query, table, }); diff --git a/frontend/test/metabase-lib/lib/Dimension.unit.spec.js b/frontend/test/metabase-lib/lib/Dimension.unit.spec.js index 3c901cbe163..c5e336d29d2 100644 --- a/frontend/test/metabase-lib/lib/Dimension.unit.spec.js +++ b/frontend/test/metabase-lib/lib/Dimension.unit.spec.js @@ -679,7 +679,7 @@ describe("Dimension", () => { name: "Hello World", display_name: "Hello World", base_type: "type/Text", - semantic_type: null, + semantic_type: "type/Text", field_ref: ["expression", "Hello World", null], }); }); -- GitLab