Skip to content

The easy-to-use open source Business Intelligence and Embedded Analytics tool that lets everyone work with data :bar_chart:

Fix field resolution when using a field name ref in the first stage (#62232)

Cam Saul0e75814ad48e
diff --git a/src/metabase/lib/expression.cljc b/src/metabase/lib/expression.cljc
index 81d63f70f19..47f09ae84e8 100644
--- a/src/metabase/lib/expression.cljc
+++ b/src/metabase/lib/expression.cljc
@@ -542,7 +542,7 @@
(mr/validator ::lib.schema.aggregation/aggregation))
(def ^:private filter-validator
- (mr/validator ::lib.schema/filterable))
+ (mr/validator ::lib.schema.expression/boolean))
(defn- expression->name
[expr]
diff --git a/src/metabase/lib/field/resolution.cljc b/src/metabase/lib/field/resolution.cljc
index a2d574be924..a4c7760ce3c 100644
--- a/src/metabase/lib/field/resolution.cljc
+++ b/src/metabase/lib/field/resolution.cljc
@@ -425,7 +425,7 @@
(defn- fallback-metadata [id-or-name]
(log/warn (u/format-color :red
- (str "We tried every trick we could think of and still failed to resolve field a"
+ (str "We tried every trick we could think of and still failed to resolve a field"
" ref. If the query doesn't work, this is why. Returning fallback metadata for %s")
(pr-str id-or-name)))
(merge
@@ -446,14 +446,23 @@
[query stage-number id-or-name]
(b/cond
:let [stage (lib.util/query-stage query stage-number)]
- (and (pos-int? id-or-name)
- (:source-table stage))
+ (and (:source-table stage)
+ (pos-int? id-or-name))
(when-some [col (field-metadata query id-or-name)]
;; don't return this field if it's not actually from the source table. We want some of the fallback
;; resolution pathways to figure out what join this came from.
(when (= (:table-id col) (:source-table stage))
col))
+ ;; we maybe have incorrectly used a field name ref when we should have used a field ID ref.
+ ;;
+ ;; TODO (Cam 8/15/25) -- what happpens if this field is marked inactive? It won't come back from
+ ;; `returned-columns`... we'd get fallback metadata, right?
+ (and (:source-table stage)
+ (string? id-or-name))
+ (m/find-first #(= (:name %) id-or-name)
+ (lib.metadata.calculation/returned-columns query (lib.metadata/table query (:source-table stage))))
+
(= (:lib/type stage) :mbql.stage/native)
(when-some [col (resolve-in-current-stage-metadata query stage-number id-or-name)]
(-> col
diff --git a/src/metabase/lib/query.cljc b/src/metabase/lib/query.cljc
index bed72302b71..147bc0f9c4a 100644
--- a/src/metabase/lib/query.cljc
+++ b/src/metabase/lib/query.cljc
@@ -211,6 +211,7 @@
query (-> query
(assoc :lib/metadata metadata-provider)
(dissoc :lib.convert/converted?)
+
lib.normalize/normalize)
stages (:stages query)]
(cond-> query
diff --git a/src/metabase/lib/schema.cljc b/src/metabase/lib/schema.cljc
index 3a47e20c725..49126439d6f 100644
--- a/src/metabase/lib/schema.cljc
+++ b/src/metabase/lib/schema.cljc
@@ -102,11 +102,8 @@
[:sequential {:min 1} [:ref ::ref/ref]]
[:ref ::lib.schema.util/distinct-mbql-clauses]])
-(mr/def ::filterable
- [:ref ::expression/boolean])
-
(mr/def ::filters
- [:sequential {:min 1} ::filterable])
+ [:sequential {:min 1} [:ref ::expression/boolean]])
(defn- bad-ref-clause? [ref-type valid-ids x]
(and (vector? x)
diff --git a/src/metabase/lib/schema/common.cljc b/src/metabase/lib/schema/common.cljc
index bbea3ec17ca..26d48fde8ca 100644
--- a/src/metabase/lib/schema/common.cljc
+++ b/src/metabase/lib/schema/common.cljc
@@ -162,7 +162,9 @@
(str "Not a valid base type: " (pr-str value)))}
base-type?]])
-(defn- normalize-options-map [m]
+(defn normalize-options-map
+ "Basic normalization behavior for an MBQL clause options map."
+ [m]
(let [m (normalize-map m)]
(-> m
;; add `:lib/uuid` if it's missing
diff --git a/src/metabase/lib/schema/literal.cljc b/src/metabase/lib/schema/literal.cljc
index 3884b55e907..a4245a66efd 100644
--- a/src/metabase/lib/schema/literal.cljc
+++ b/src/metabase/lib/schema/literal.cljc
@@ -142,6 +142,13 @@
[:merge
[:ref ::common/options]
[:map
+ {:decode/normalize (fn [m]
+ (when (map? m)
+ (-> m
+ common/normalize-options-map
+ (cond-> (and (:base-type m)
+ (not (:effective-type m)))
+ (assoc :effective-type (:base-type m))))))}
[:effective-type ::common/base-type]
[:unit {:optional true} [:maybe ::temporal-bucketing/unit]]]])
diff --git a/test/metabase/driver/sql/query_processor_test.clj b/test/metabase/driver/sql/query_processor_test.clj
index 180761c2e4d..c3aad340bd7 100644
--- a/test/metabase/driver/sql/query_processor_test.clj
+++ b/test/metabase/driver/sql/query_processor_test.clj
@@ -1441,3 +1441,32 @@
" 20"]}
(-> (qp.compile/compile query)
(update :query #(str/split-lines (driver/prettify-native-form :h2 %))))))))))
+
+(deftest ^:parallel field-name-ref-in-parameters-test
+ (testing "Should generate correct SQL if we use a field name ref in parameters"
+ (let [query {:type :query
+ :database (meta/id)
+ :query {:source-table (meta/id :products)}
+ :parameters [{:type :id
+ :value [144]
+ :id "92eb69ea"
+ :target [:dimension [:field "ID" {:base-type :type/BigInteger}]]}]}]
+ (qp.store/with-metadata-provider meta/metadata-provider
+ (is (= {:query ["SELECT"
+ " \"PUBLIC\".\"PRODUCTS\".\"ID\" AS \"ID\","
+ " \"PUBLIC\".\"PRODUCTS\".\"EAN\" AS \"EAN\","
+ " \"PUBLIC\".\"PRODUCTS\".\"TITLE\" AS \"TITLE\","
+ " \"PUBLIC\".\"PRODUCTS\".\"CATEGORY\" AS \"CATEGORY\","
+ " \"PUBLIC\".\"PRODUCTS\".\"VENDOR\" AS \"VENDOR\","
+ " \"PUBLIC\".\"PRODUCTS\".\"PRICE\" AS \"PRICE\","
+ " \"PUBLIC\".\"PRODUCTS\".\"RATING\" AS \"RATING\","
+ " \"PUBLIC\".\"PRODUCTS\".\"CREATED_AT\" AS \"CREATED_AT\""
+ "FROM"
+ " \"PUBLIC\".\"PRODUCTS\""
+ "WHERE"
+ " \"PUBLIC\".\"PRODUCTS\".\"ID\" = 144"
+ "LIMIT"
+ " 1048575"]
+ :params nil}
+ (-> (qp.compile/compile query)
+ (update :query #(str/split-lines (driver/prettify-native-form :h2 %))))))))))
diff --git a/test/metabase/lib/field/resolution_test.cljc b/test/metabase/lib/field/resolution_test.cljc
index 4e7c03f4dce..7d8c50bb8ec 100644
--- a/test/metabase/lib/field/resolution_test.cljc
+++ b/test/metabase/lib/field/resolution_test.cljc
@@ -1285,3 +1285,27 @@
(lib.field.resolution/resolve-field-ref
query -1
[:field {:base-type :type/Integer, :lib/uuid "00000000-0000-0000-0000-000000000000"} "my_numberLiteral"]))))))
+
+(deftest ^:parallel field-name-ref-in-first-stage-test
+ (testing "Should be able to resolve a field name ref in the first stage of a query"
+ (let [query (lib/query
+ meta/metadata-provider
+ {:lib/type :mbql/query
+ :database (meta/id)
+ :stages [{:lib/type :mbql.stage/mbql
+ :source-table (meta/id :products)
+ :fields [[:field {} (meta/id :products :id)]
+ [:field {} (meta/id :products :title)]]
+ :filters [[:=
+ {}
+ [:field {:base-type :type/BigInteger} "ID"]
+ [:value {:base-type :type/BigInteger} 144]]]}]})]
+ (is (=? (-> (lib.field.resolution/resolve-field-ref
+ query
+ -1
+ [:field {:base-type :type/BigInteger, :lib/uuid "00000000-0000-0000-0000-000000000000"} (meta/id :products :id)])
+ (dissoc :lib/original-ref :lib/original-display-name))
+ (lib.field.resolution/resolve-field-ref
+ query
+ -1
+ [:field {:base-type :type/BigInteger, :lib/uuid "00000000-0000-0000-0000-000000000000"} "ID"]))))))
diff --git a/test/metabase/lib/schema/filter_test.cljc b/test/metabase/lib/schema/filter_test.cljc
index b7959063f84..9fd4512d66b 100644
--- a/test/metabase/lib/schema/filter_test.cljc
+++ b/test/metabase/lib/schema/filter_test.cljc
@@ -125,3 +125,15 @@
[:=
[:field {} 2]
[:field {:join-alias "Parent"} 1]]))))
+
+(deftest ^:parallel normalize-clause-add-uuids-test
+ (is (=? [:=
+ {:lib/uuid string?}
+ [:field {:lib/uuid string?, :base-type :type/BigInteger} "ID"]
+ [:value {:lib/uuid string?, :base-type :type/BigInteger, :effective-type :type/BigInteger} 144]]
+ (lib/normalize
+ :mbql.clause/=
+ [:=
+ {}
+ [:field {:base-type :type/BigInteger} "ID"]
+ [:value {:base-type :type/BigInteger} 144]]))))
diff --git a/test/metabase/query_processor/util/add_alias_info_test.clj b/test/metabase/query_processor/util/add_alias_info_test.clj
index a2ef6161291..6360b0c10c1 100644
--- a/test/metabase/query_processor/util/add_alias_info_test.clj
+++ b/test/metabase/query_processor/util/add_alias_info_test.clj
@@ -1294,3 +1294,27 @@
first
:filters
first))))))
+
+(deftest ^:parallel field-name-ref-in-first-stage-test
+ (testing "Should add correct alias info if we use a field name ref in the first stage of a query"
+ (let [query (lib/query
+ meta/metadata-provider
+ {:type :query
+ :database (meta/id)
+ :query {:source-table (meta/id :products)}
+ :parameters [{:type :id
+ :value [144]
+ :id "92eb69ea"
+ :target [:dimension [:field "ID" {:base-type :type/BigInteger}]]}]})]
+ (is (=? [:=
+ {}
+ [:field
+ {::add/source-alias "ID"
+ ::add/source-table (meta/id :products)}
+ "ID"]
+ [:value {} 144]]
+ (-> (add-alias-info query)
+ :stages
+ first
+ :filters
+ first))))))