diff --git a/backend/mbql/src/metabase/mbql/util.clj b/backend/mbql/src/metabase/mbql/util.clj index 472697db82dbf0c6260384d327357b4a67ab176b..3fa6c4848cdd2a46a216d6cd5386ff4e733067c5 100644 --- a/backend/mbql/src/metabase/mbql/util.clj +++ b/backend/mbql/src/metabase/mbql/util.clj @@ -442,7 +442,7 @@ "Unwrap a Field `clause`, if it's something that can be unwrapped (i.e. something that is, or wraps, a `:field-id` or `:field-literal`). Otherwise return `clause` as-is." [clause] - (if (is-clause? #{:field-id :fk-> :field-literal :datetime-field :binning-strategy} clause) + (if (is-clause? #{:field-id :fk-> :field-literal :datetime-field :binning-strategy :joined-field} clause) (unwrap-field-clause clause) clause)) @@ -453,10 +453,9 @@ (field-clause->id-or-literal [:datetime-field [:field-id 100] ...]) ; -> 100 (field-clause->id-or-literal [:field-id 100]) ; -> 100 - For expressions (or any other clauses) this returns the clause as-is, so as to facilitate the primary use case of - comparing Field clauses." + For expressions returns the expression name." [clause :- mbql.s/Field] - (second (unwrap-field-clause clause))) + (second (maybe-unwrap-field-clause clause))) (s/defn add-order-by-clause :- mbql.s/MBQLQuery "Add a new `:order-by` clause to an MBQL `inner-query`. If the new order-by clause references a Field that is diff --git a/backend/mbql/test/metabase/mbql/util_test.clj b/backend/mbql/test/metabase/mbql/util_test.clj index 44737e9e3c7ce9dec2ce7e462709e41027c78387..d6d4d5ae0ebbaaf51abe4d7bfa353795fbe2df62 100644 --- a/backend/mbql/test/metabase/mbql/util_test.clj +++ b/backend/mbql/test/metabase/mbql/util_test.clj @@ -1059,3 +1059,15 @@ :query {:source-query {:expressions {:two [:+ 1 1]} :source-table 1}}} "two")) + +(expect + 1 + (mbql.u/field-clause->id-or-literal [:field-id 1])) + +(expect + "foo" + (mbql.u/field-clause->id-or-literal [:field-literal "foo" :type/Integer])) + +(expect + "foo" + (mbql.u/field-clause->id-or-literal [:expression "foo"]))