Skip to content
Snippets Groups Projects
Unverified Commit a336f639 authored by Ariya Hidayat's avatar Ariya Hidayat Committed by GitHub
Browse files

Use type inference in ExpressionDimensions (#15965)

parent 3f2f92c3
No related merge requests found
......@@ -25,6 +25,8 @@ import { DATETIME_UNITS, formatBucketing } from "metabase/lib/query_time";
import type Aggregation from "./queries/structured/Aggregation";
import StructuredQuery from "./queries/StructuredQuery";
import { infer, MONOTYPE } from "metabase/lib/expressions/typeinferencer";
/**
* A dimension option returned by the query_metadata API
*/
......@@ -1001,20 +1003,50 @@ export class ExpressionDimension extends Dimension {
}
field() {
const query = this._query;
const table = query ? query.table() : null;
let type = MONOTYPE.Number; // fallback
if (query) {
const datasetQuery = query.query();
const expressions = datasetQuery ? datasetQuery.expressions : {};
type = infer(expressions[this.name()]);
} else {
type = infer(this._args[0]);
}
let base_type = "type/Float"; // fallback
switch (type) {
case MONOTYPE.String:
base_type = "type/Text";
break;
case MONOTYPE.Boolean:
base_type = "type/Boolean";
break;
default:
break;
}
return new Field({
id: this.mbql(),
name: this.name(),
display_name: this.displayName(),
semantic_type: null,
base_type: "type/Float",
// HACK: need to thread the query through to this fake Field
query: this._query,
table: this._query ? this._query.table() : null,
base_type,
query,
table,
});
}
icon(): IconName {
// TODO: eventually will need to get the type from the return type of the expression
const { base_type } = this.field();
switch (base_type) {
case "type/Text":
return "string";
default:
break;
}
return "int";
}
}
......
......@@ -620,7 +620,7 @@ describe("Dimension", () => {
id: ["expression", "Hello World"],
name: "Hello World",
display_name: "Hello World",
base_type: "type/Float",
base_type: "type/Text",
semantic_type: null,
field_ref: ["expression", "Hello World"],
});
......
......@@ -4,6 +4,7 @@ import {
_typeUsingGet,
_typeUsingPlaceholder,
openOrdersTable,
openProductsTable,
visitQuestionAdhoc,
} from "__support__/e2e/cypress";
......@@ -414,6 +415,28 @@ describe("scenarios > question > custom columns", () => {
cy.contains("37.65");
});
describe("data type", () => {
it("should understand string functions", () => {
openProductsTable({ mode: "notebook" });
cy.findByText("Custom column").click();
popover().within(() => {
cy.get("[contenteditable='true']")
.type("concat([Category], [Title])")
.blur();
cy.findByPlaceholderText("Something nice and descriptive").type(
"CategoryTitle",
);
cy.findByRole("button", { name: "Done" }).click();
});
cy.findByText("Filter").click();
popover()
.findByText("CategoryTitle")
.click();
cy.findByPlaceholderText("Enter a number").should("not.exist");
cy.findByPlaceholderText("Enter some text");
});
});
it("should handle using `case()` when referencing the same column names (metabase#14854)", () => {
const CC_NAME = "CE with case";
......
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