diff --git a/frontend/src/metabase-lib/lib/queries/StructuredQuery.js b/frontend/src/metabase-lib/lib/queries/StructuredQuery.js index bc342fe5b0eddf0db4d52d69e2a3606c8450c79e..4bad6be280fb305b0dec2e52bd1e7577dfa9e551 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 59203fd46585d233ee57e0153f69e2305cf3fe7a..9f4352a8d97d870b93a133912af77ab3a3769757 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 dc8bc93f12a996e4ba452a6234c8137910de0254..fa28fdee6ab03308201f14f93add8d21a1b4cd68 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 724dd03d9012aaca107b017ddd482cea0d5de1a2..a41216eba55007d4e4dfc15e634acb5626d94c4b 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 c8828a5fe315c19fe6ad6012bb81d51bf1111d9d..2d9115cb9e00106160e602a2b56e5e74d51d7148 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))