Skip to content

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

2.9 KB
Raw Blame
(ns metabase-enterprise.search.semantic.index-embedding-test
  "Tests for things at the intersection of the index and embedding namespaces."
  (:require
   [clojure.test :refer :all]
   [metabase-enterprise.search.semantic.index :as semantic.index]
   [metabase.test :as mt]))

(def ^:private test-provider "ai-service")
(def ^:private test-short-model-name "short-model")
(def ^:private test-long-model-name "some-really-really-really-really-really-long-model-name-that-will-exceed-the-limit")
(def ^:private test-vector-dimensions 1024)

(deftest ^:parallel hash-identifier-uses-postgres-byte-limit-test
  (let [identifier (str "index_" (apply str (repeat 30 "é")))]
    (is (<= (count identifier) 63) "the identifier fits when incorrectly counted as characters")
    (is (re-matches #"index_[0-9a-f]{40}"
                    (semantic.index/hash-identifier-if-exceeds-pg-limit identifier))
        "UTF-8 identifiers over PostgreSQL's 63-byte limit are hashed")))

;; Postgres truncates identifier names to 63 bytes (+ 1 byte for a terminating NULL). Ensure that names do not exceed the limit
(deftest ^:parallel index-embedding-name-length-test
  (mt/with-premium-features #{:semantic-search}
    (testing "Truncates if name would be longer than the 63 byte Postgres limit"
      (doseq [name-func [#'semantic.index/hnsw-index-name
                         #'semantic.index/fts-index-name
                         #'semantic.index/fts-native-index-name
                         :table-name]]
        (testing (str name-func)
          (testing "long"
            (is (>= 63 (count (name-func (semantic.index/default-index {:provider          test-provider
                                                                        :model-name        test-long-model-name
                                                                        :vector-dimensions test-vector-dimensions})))))
            (testing "remains unique"
              (is (not= (name-func (semantic.index/default-index {:provider          test-provider
                                                                  :model-name        (str test-long-model-name "a")
                                                                  :vector-dimensions test-vector-dimensions}))
                        (name-func (semantic.index/default-index {:provider          test-provider
                                                                  :model-name        (str test-long-model-name "aa")
                                                                  :vector-dimensions test-vector-dimensions}))))))
          (testing "short"
            (is (>= 63 (count (name-func (semantic.index/default-index {:provider          test-provider
                                                                        :model-name        test-short-model-name
                                                                        :vector-dimensions test-vector-dimensions})))))))))))