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

Create a fake field for an aggregation dimension's field() method (#11800)

parent 37ebb715
No related branches found
No related tags found
No related merge requests found
......@@ -847,22 +847,30 @@ export class AggregationDimension extends Dimension {
}
column(extra = {}) {
const aggregation = this.aggregation();
const { special_type, ...column } = super.column();
return {
...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,
...super.column(),
source: "aggregation",
...extra,
};
}
field() {
// FIXME: it isn't really correct to return the unaggregated field. return a fake Field object?
const dimension = this.aggregation().dimension();
return dimension ? dimension.field() : super.field();
const aggregation = this.aggregation();
if (!aggregation) {
return super.field();
}
const dimension = aggregation.dimension();
const field = dimension && dimension.field();
const { special_type } = field || {};
return new Field({
name: aggregation.columnName(),
display_name: aggregation.displayName(),
base_type: aggregation.baseType(),
// don't pass through `special_type` when aggregating these types
...(!UNAGGREGATED_SPECIAL_TYPES.has(special_type) && { special_type }),
query: this._query,
metadata: this._metadata,
});
}
/**
......
......@@ -509,17 +509,21 @@ describe("Dimension", () => {
});
});
function aggregation(agg) {
const query = new StructuredQuery(ORDERS.question(), {
type: "query",
database: SAMPLE_DATASET.id,
query: {
"source-table": ORDERS.id,
aggregation: [agg],
},
});
return Dimension.parseMBQL(["aggregation", 0], metadata, query);
}
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);
return aggregation(["sum", ["field-id", column.id]]);
}
it("should clear unaggregated special types", () => {
......@@ -534,6 +538,29 @@ describe("Dimension", () => {
expect(special_type).toBe("type/Currency");
});
});
describe("field()", () => {
it("should return a float field for sum of order total", () => {
const { base_type } = aggregation([
"sum",
["field-id", ORDERS.TOTAL.id],
]).field();
expect(base_type).toBe("type/Float");
});
it("should return an int field for count distinct of product category", () => {
const { base_type } = aggregation([
"distinct",
["field-id", PRODUCTS.CATEGORY.id],
]).field();
expect(base_type).toBe("type/Integer");
});
it("should return an int field for count", () => {
const { base_type } = aggregation(["count"]).field();
expect(base_type).toBe("type/Integer");
});
});
});
});
});
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