Skip to content

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

Implement the changes requested in #71771's review comments (#72024)

metambend45d28ecbbd5
diff --git a/src/metabase/driver/sql/query_processor.clj b/src/metabase/driver/sql/query_processor.clj
index 37fc74a2012..46b18290345 100644
--- a/src/metabase/driver/sql/query_processor.clj
+++ b/src/metabase/driver/sql/query_processor.clj
@@ -914,50 +914,60 @@
;; which means nested native queries will cause bugs like #42817
;; but this should all be fixed with field refs overhaul!
;; https://linear.app/metabase-inc/issue/ENG-8766/[epic]-field-refs-overhaul
- field-metadata (when (integer? id-or-name)
- (driver-api/field (driver-api/metadata-provider) id-or-name))
+ field-metadata (when (integer? id-or-name)
+ (driver-api/field (driver-api/metadata-provider) id-or-name))
;; For fields with parent-id (struct-type nested fields), include parent field names
;; in the identifier so e.g. `result.tag_name` is rendered instead of just `tag_name`.
- parent-names (when-let [parent-id (:parent-id field-metadata)]
- (parent-id->ancestor-names parent-id))
- allow-casting? (and (or (:qp/native-sandbox-column.force-coercion-strategy options)
- (and field-metadata
- (or (pos-int? (driver-api/qp.add.source-table options))
- (:qp/allow-coercion-for-columns-without-integer-qp.add.source-table options))))
- (not (:qp/ignore-coercion options)))
+ parent-names (when-let [parent-id (:parent-id field-metadata)]
+ (parent-id->ancestor-names parent-id))
+ allow-casting? (and (or (:qp/native-sandbox-column.force-coercion-strategy options)
+ (and field-metadata
+ (or (pos-int? (driver-api/qp.add.source-table options))
+ (:qp/allow-coercion-for-columns-without-integer-qp.add.source-table
+ options))))
+ (not (:qp/ignore-coercion options)))
;; preserve metadata attached to the original field clause, for example BigQuery temporal type information.
- identifier (-> (apply h2x/identifier :field
- (concat source-table-aliases
- (->honeysql driver [::nfc-path source-nfc-path])
- parent-names
- [source-alias]))
- (with-meta (meta field-clause)))
- identifier (->honeysql driver identifier)
+ identifier (-> (apply h2x/identifier :field
+ (concat source-table-aliases
+ (->honeysql driver [::nfc-path source-nfc-path])
+ parent-names
+ [source-alias]))
+ (with-meta (meta field-clause)))
+ identifier (->honeysql driver identifier)
;; If no field-metadata is available, but a coercion strategy must be applied, synthesize enough
;; `field-metadata` to be able to cast it correctly.
- field-metadata (or field-metadata
- (when allow-casting?
- (-> options
- (select-keys [:base-type :effetive-type])
- (assoc :coercion-strategy
- (:qp/native-sandbox-column.force-coercion-strategy options)))))
- casted-field (cast-field-if-needed driver field-metadata identifier)
- database-type (or (h2x/database-type casted-field)
- (:database-type field-metadata))
- effective-type (when-not database-type
- (let [et (or (:effective-type options) (:base-type options))]
- (when (isa? et :type/Temporal)
- et)))
- maybe-add-type-info (fn [expr]
- (if (h2x/type-info->db-type (h2x/type-info expr))
- expr
- (if database-type
- (h2x/with-database-type-info expr database-type)
- (h2x/with-type-info expr {:effective-type effective-type}))))]
+ field-metadata (or field-metadata
+ (when allow-casting?
+ (-> options
+ (select-keys [:base-type :effective-type])
+ (assoc :coercion-strategy
+ (:qp/native-sandbox-column.force-coercion-strategy options)))))
+ casted-field (cast-field-if-needed driver field-metadata identifier)
+ database-type (or (h2x/database-type casted-field)
+ (:database-type field-metadata))
+ ;; In principle, setting the type based on the effective (or base) type is OK
+ ;; in all cases, but currently it's needed for correctness for temporal types
+ ;; and always setting them would result in failed tests that assert that the type
+ ;; info is missing.
+ ;; Another way of fixing #71551 could be tracking the database type through cards
+ ;; where the fields are converted to columns and lose the database type info.
+ ;; This way should be much easier once the column lineage can be tracked using
+ ;; column keys.
+ effective-temporal-type (when-not database-type
+ (let [et (or (:effective-type options) (:base-type options))]
+ (when (isa? et :type/Temporal)
+ et)))
+ ;; only called if database-type or effective-temporal-type is set
+ maybe-add-type-info (fn [expr]
+ (if (h2x/type-info->db-type (h2x/type-info expr))
+ expr
+ (if database-type
+ (h2x/with-database-type-info expr database-type)
+ (h2x/with-type-info expr {:effective-type effective-temporal-type}))))]
(u/prog1
(cond->> (if allow-casting? casted-field identifier)
;; only add type info if it wasn't added by [[cast-field-if-needed]]
- (or database-type effective-type) maybe-add-type-info
+ (or database-type effective-temporal-type) maybe-add-type-info
(:temporal-unit options) (apply-temporal-bucketing driver options)
(:binning options) (apply-binning options))
(log/trace (binding [*print-meta* true]