From 748e8d128d9f8a1e590b26fd96d215a0a8666c29 Mon Sep 17 00:00:00 2001 From: Cam Saul <cammsaul@gmail.com> Date: Tue, 20 Nov 2018 15:21:28 -0800 Subject: [PATCH] Add expression_name to QP cols results --- .../lib/queries/StructuredQuery.js | 4 +-- frontend/src/metabase/lib/dataset.js | 4 +-- frontend/src/metabase/meta/types/Dataset.js | 2 +- .../query_processor/middleware/annotate.clj | 10 ++++--- .../middleware/annotate_test.clj | 28 ++++++++++++++++--- 5 files changed, 35 insertions(+), 13 deletions(-) diff --git a/frontend/src/metabase-lib/lib/queries/StructuredQuery.js b/frontend/src/metabase-lib/lib/queries/StructuredQuery.js index bc342fe5b0e..4bad6be280f 100644 --- a/frontend/src/metabase-lib/lib/queries/StructuredQuery.js +++ b/frontend/src/metabase-lib/lib/queries/StructuredQuery.js @@ -738,8 +738,8 @@ export default class StructuredQuery extends AtomicQuery { return ["fk->", column.fk_field_id, column.id]; } else if (column.id != null) { return ["field-id", column.id]; - } else if (column["expression-name"] != null) { - return ["expression", column["expression-name"]]; + } else if (column.expression_name != null) { + return ["expression", column.expression_name]; } else if (column.source === "aggregation") { // FIXME: aggregations > 0? return ["aggregation", 0]; diff --git a/frontend/src/metabase/lib/dataset.js b/frontend/src/metabase/lib/dataset.js index 59203fd4658..9f4352a8d97 100644 --- a/frontend/src/metabase/lib/dataset.js +++ b/frontend/src/metabase/lib/dataset.js @@ -53,8 +53,8 @@ export function fieldRefForColumn(column: Column): ?ConcreteField { } else { return ["field-id", column.id]; } - } else if (column["expression-name"] != null) { - return ["expression", column["expression-name"]]; + } else if (column.expression_name != null) { + return ["expression", column.expression_name]; } else { return null; } diff --git a/frontend/src/metabase/meta/types/Dataset.js b/frontend/src/metabase/meta/types/Dataset.js index dc8bc93f12a..fa28fdee6ab 100644 --- a/frontend/src/metabase/meta/types/Dataset.js +++ b/frontend/src/metabase/meta/types/Dataset.js @@ -24,7 +24,7 @@ export type Column = { unit?: DatetimeUnit, binning_info?: BinningInfo, fk_field_id?: FieldId, - "expression-name"?: any, + expression_name?: any, settings?: ColumnSettings, }; diff --git a/src/metabase/query_processor/middleware/annotate.clj b/src/metabase/query_processor/middleware/annotate.clj index 724dd03d901..a41216eba55 100644 --- a/src/metabase/query_processor/middleware/annotate.clj +++ b/src/metabase/query_processor/middleware/annotate.clj @@ -90,10 +90,12 @@ :display_name (humanization/name->human-readable-name field-name)} [:expression expression-name] - {:name expression-name - :display_name expression-name - :base_type :type/Float - :special_type :type/Number} + {:name expression-name + :display_name expression-name + :base_type :type/Float + :special_type :type/Number + ;; provided so the FE can add easily add sorts and the like when someone clicks a column header + :expression_name expression-name} [:field-id id] (dissoc (qp.store/field id) :database_type) diff --git a/test/metabase/query_processor/middleware/annotate_test.clj b/test/metabase/query_processor/middleware/annotate_test.clj index c8828a5fe31..2d9115cb9e0 100644 --- a/test/metabase/query_processor/middleware/annotate_test.clj +++ b/test/metabase/query_processor/middleware/annotate_test.clj @@ -2,11 +2,11 @@ (:require [expectations :refer [expect]] [metabase.models.field :refer [Field]] [metabase.query-processor - [interface :as i] - [store :as qp.store]] + [interface :as qp.i] + [store :as qp.store] + [test-util :as qp.test-util]] [metabase.query-processor.middleware.annotate :as annotate] - [metabase.test.data :as data] - [metabase.query-processor.interface :as qp.i]) + [metabase.test.data :as data]) (:import metabase.driver.h2.H2Driver)) ;;; +----------------------------------------------------------------------------------------------------------------+ @@ -239,3 +239,23 @@ :type :query :query {:source-table (data/id :venues) :aggregation [[:count] [:sum] [:count] [:named [:count] "count_2"]]}}))) + +;; make sure expressions come back with the right set of keys, including `:expression_name` (#8854) +(expect + {:name "discount_price" + :display_name "discount_price" + :base_type :type/Float + :special_type :type/Number + :expression_name "discount_price" + :source :fields} + (-> (qp.test-util/with-everything-store + ((annotate/add-column-info (constantly {})) + {:database (data/id) + :type :query + :query (data/$ids [venues {:wrap-field-ids? true}] + {:source-table $$table + :expressions {"discount_price" [:* 0.9 [:field-id $price]]} + :fields [$name [:expression "discount_price"]] + :limit 10})})) + :cols + second)) -- GitLab