Skip to content
Snippets Groups Projects
Unverified Commit 00e4226b authored by Dalton's avatar Dalton Committed by GitHub
Browse files

fix expression dimension failing to find underlying field (#18748)

* fix expression dimension failing to find underlying field

* add a repro
parent f121d4d8
Branches
Tags
No related merge requests found
......@@ -545,14 +545,33 @@ export class FieldDimension extends Dimension {
field(): {} {
if (this.isIntegerFieldId()) {
return (
(this._metadata && this._metadata.field(this.fieldIdOrName())) ||
new Field({
id: this._fieldIdOrName,
metadata: this._metadata,
query: this._query,
})
);
const field = this._metadata?.field(this.fieldIdOrName());
if (field) {
return field;
}
// if the field isn't in metadata, there _might_ be a card tied to this Dimension
// if so, check the card's result_metadata
const question = this.query()?.question();
if (question != null) {
const field = _.findWhere(question.getResultMetadata(), {
id: this.fieldIdOrName(),
});
if (field) {
return new Field({
...field,
metadata: this._metadata,
query: this._query,
});
}
}
return new Field({
id: this._fieldIdOrName,
metadata: this._metadata,
query: this._query,
});
}
// look for a "virtual" field on the query's table or question
......
......@@ -250,6 +250,34 @@ describe("Dimension", () => {
expect(dimension.field().metadata).toEqual(metadata);
expect(dimension.field().displayName()).toEqual("Total");
});
it("should return the correct Field from a query's result_metadata when the metadata object is missing the Field", () => {
const emptyMetadata = {
field: () => {},
table: () => {},
};
const question = ORDERS.question().setResultsMetadata({
columns: [ORDERS.TOTAL],
});
const query = new StructuredQuery(question, {
type: "query",
database: SAMPLE_DATASET.id,
query: {
"source-table": ORDERS.id,
},
});
const dimension = Dimension.parseMBQL(
["field", ORDERS.TOTAL.id, null],
emptyMetadata,
query,
);
const field = dimension.field();
expect(field.id).toEqual(ORDERS.TOTAL.id);
expect(field.base_type).toEqual("type/Float");
});
});
});
});
......
import { restore, popover } from "__support__/e2e/cypress";
import { SAMPLE_DATASET } from "__support__/e2e/cypress_sample_dataset";
const { ORDERS, ORDERS_ID } = SAMPLE_DATASET;
const questionDetails = {
name: "18747",
query: {
"source-table": ORDERS_ID,
expressions: {
["Quantity_2"]: ["field", ORDERS.QUANTITY, null],
},
},
};
describe("issue 18747", () => {
beforeEach(() => {
restore();
cy.signInAsAdmin();
cy.createQuestionAndDashboard({ questionDetails }).then(
({ body: { id, card_id, dashboard_id } }) => {
cy.request("PUT", `/api/dashboard/${dashboard_id}`, {
cards: [
{
id,
card_id,
row: 0,
col: 0,
sizeX: 12,
sizeY: 8,
},
],
}).then(() => {
cy.visit(`/dashboard/${dashboard_id}`);
});
},
);
});
it("should correctly filter the table with a number parameter mapped to the custom column Quantity_2", () => {
addNumberParameterToDashboard();
mapParameterToCustomColumn();
cy.contains("Save").click();
// wait for saving to finish
cy.contains("You're editing this dashboard.").should("not.exist");
addValueToParameterFilter();
cy.get(".CardVisualization tbody > tr").should("have.length", 1);
// check that the parameter value is parsed correctly on page load
cy.reload();
cy.get(".LoadingSpinner").should("not.exist");
cy.get(".CardVisualization tbody > tr").should("have.length", 1);
});
});
function addNumberParameterToDashboard() {
cy.icon("pencil").click();
cy.icon("filter").click();
cy.contains("Number").click();
cy.findByText("Equal to").click();
}
function mapParameterToCustomColumn() {
cy.get(".DashCard")
.contains("Select…")
.click();
popover()
.contains("Quantity_2")
.click({ force: true });
}
function addValueToParameterFilter() {
cy.contains("Equal to").click();
popover()
.find("input")
.type("14");
popover()
.contains("Add filter")
.click();
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment