diff --git a/src/metabase/driver.clj b/src/metabase/driver.clj index 9738f2450c6f988b174b541cbb1b24fd2b0006d0..cf6e2f1115cd369ea58fe7d280cc5da83f49e5f2 100644 --- a/src/metabase/driver.clj +++ b/src/metabase/driver.clj @@ -958,13 +958,13 @@ nil) (defmulti table-name-length-limit - "Return the maximum number of characters allowed in a table name, or `nil` if there is no limit." + "Return the maximum number of bytes allowed in a table name, or `nil` if there is no limit." {:changelog-test/ignore true, :added "0.47.0", :arglists '([driver])} dispatch-on-initialized-driver :hierarchy #'hierarchy) (defmulti column-name-length-limit - "Return the maximum number of characters allowed in a column name, or `nil` if there is no limit." + "Return the maximum number of bytes allowed in a column name, or `nil` if there is no limit." {:changelog-test/ignore true, :added "0.49.19", :arglists '([driver])} dispatch-on-initialized-driver :hierarchy #'hierarchy) diff --git a/src/metabase/upload.clj b/src/metabase/upload.clj index e1f13959ca1df54e626cd3b26a6d641cbc3e08e0..b9dcede34337395e6bac2dd144fef54d40be31fa 100644 --- a/src/metabase/upload.clj +++ b/src/metabase/upload.clj @@ -41,22 +41,22 @@ (set! *warn-on-reflection* true) -(def ^:private max-field-name-length - "This tracks the size of the metabase_field.name field" +(def ^:private max-field-name-bytes + "This tracks the size of the metabase_field.name field, in bytes." 254) (def ^:private min-safe (fnil min Long/MAX_VALUE Long/MAX_VALUE)) (defn- max-column-bytes [driver] (let [column-limit (some-> driver driver/column-name-length-limit)] - (min-safe column-limit max-field-name-length))) + (min-safe column-limit max-field-name-bytes))) (defn- normalize-column-name [driver raw-name] (if (str/blank? raw-name) "unnamed_column" (u/slugify (str/trim raw-name) - ;; since normalized names contain only ASCII characters, we can conflate bytes and length here. + ;; since slugified names contain only ASCII characters, we can conflate bytes and length here. {:max-length (max-column-bytes driver)}))) (def auto-pk-column-name @@ -108,6 +108,8 @@ [driver table-name] (let [time-format "_yyyyMMddHHmmss" slugified-name (or (u/slugify table-name) "") + ;; since both the time-format and the slugified-name contain only ASCII characters, we can behave as if + ;; [[driver/table-name-length-limit]] were defining a length in characters. max-length (- (driver/table-name-length-limit driver) (count time-format)) acceptable-length (min (count slugified-name) max-length) truncated-name-without-time (subs slugified-name 0 acceptable-length)]