Skip to content
Snippets Groups Projects
Commit 547457b8 authored by Cam Saül's avatar Cam Saül
Browse files

Add new field->identifier protocol method

parent 4b2dbd6d
No related branches found
No related tags found
No related merge requests found
......@@ -329,6 +329,12 @@
ag-type)))
:else (str schema-name \. table-name \. field-name)))
;; TODO - Making 2 DB calls for each field to fetch its dataset is inefficient and makes me cry, but this method is currently only used for SQL params so it's not a huge deal at this point
(defn- field->identifier [{table-id :table_id, :as field}]
(let [db-id (db/select-one-field :db_id 'Table :id table-id)
dataset (:dataset-id (db/select-one-field :details Database, :id db-id))]
(hsql/raw (apply format "[%s.%s.%s]" dataset (field/qualified-name-components field)))))
;; We have to override the default SQL implementations of breakout and order-by because BigQuery propogates casting functions in SELECT
;; BAD:
;; SELECT msec_to_timestamp([sad_toucan_incidents.incidents.timestamp]) AS [sad_toucan_incidents.incidents.timestamp], count(*) AS [count]
......@@ -386,8 +392,9 @@
:date (u/drop-first-arg date)
:date-string->literal (u/drop-first-arg date-string->literal)
:field->alias (u/drop-first-arg field->alias)
:field->identifier (u/drop-first-arg field->identifier)
:prepare-value (u/drop-first-arg prepare-value)
:quote-style (constantly :sqlserver) ; we want identifiers quoted [like].[this]
:quote-style (constantly :sqlserver) ; we want identifiers quoted [like].[this] initially (we have to convert them to [like.this] before executing)
:string-length-fn (u/drop-first-arg string-length-fn)
:unix-timestamp->timestamp (u/drop-first-arg unix-timestamp->timestamp)})
......
......@@ -19,6 +19,7 @@
java.util.Map
(clojure.lang Keyword PersistentVector)
com.mchange.v2.c3p0.ComboPooledDataSource
metabase.models.field.FieldInstance
(metabase.query_processor.interface Field Value)))
(defprotocol ISQLDriver
......@@ -66,6 +67,12 @@
(excluded-schemas ^java.util.Set [this]
"*OPTIONAL*. Set of string names of schemas to skip syncing tables from.")
(field->identifier [this, ^FieldInstance field]
"*OPTIONAL*. Return a HoneySQL form that should be used as the identifier for FIELD.
The default implementation returns a keyword generated by from the components returned by `field/qualified-name-components`.
Other drivers like BigQuery need to do additional qualification, e.g. the dataset name as well.
(At the time of this writing, this is only used by the SQL parameters implementation; in the future it will probably be used in more places as well.)")
(field-percent-urls [this field]
"*OPTIONAL*. Implementation of the `:field-percent-urls-fn` to be passed to `make-analyze-table`.
The default implementation is `fast-field-percent-urls`, which avoids a full table scan. Substitue this with `slow-field-percent-urls` for databases
......@@ -420,6 +427,7 @@
:current-datetime-fn (constantly :%now)
:date-string->literal (u/drop-first-arg hx/literal)
:excluded-schemas (constantly nil)
:field->identifier (u/drop-first-arg (comp (partial apply hsql/qualify) field/qualified-name-components))
:field->alias (u/drop-first-arg name)
:field-percent-urls fast-field-percent-urls
:prepare-value (u/drop-first-arg :value)
......
......@@ -62,7 +62,7 @@
FieldInstance
(->sql [this]
(->sql (let [identifier (apply hsql/qualify (field/qualified-name-components this))]
(->sql (let [identifier ((resolve 'metabase.driver.generic-sql/field->identifier) *driver* this)]
(if (re-find #"^date/" (:type this))
((resolve 'metabase.driver.generic-sql/date) *driver* :day identifier)
identifier))))
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment