Skip to content

Commit

Permalink
Fix sync type inference for MongoDB completely null fields
Browse files Browse the repository at this point in the history
  • Loading branch information
camsaul committed Feb 6, 2018
1 parent a393697 commit 443da22
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 2 deletions.
4 changes: 2 additions & 2 deletions src/metabase/driver/mongo.clj
Original file line number Diff line number Diff line change
Expand Up @@ -106,12 +106,12 @@
(find-nested-fields field-value nested-fields)
nested-fields)))))

(s/defn ^:private most-common-object-type :- Class
(s/defn ^:private ^Class most-common-object-type :- (s/maybe Class)
"Given a sequence of tuples like [Class <number-of-occurances>] return the Class with the highest number of
occurances. The basic idea here is to take a sample of values for a Field and then determine the most common type
for its values, and use that as the Metabase base type. For example if we have a Field called `zip_code` and it's a
number 90% of the time and a string the other 10%, we'll just call it a `:type/Number`."
[field-types :- [(s/pair Class "Class", s/Int "Int")]]
[field-types :- [(s/pair (s/maybe Class) "Class", s/Int "Int")]]
(->> field-types
(sort-by second)
last
Expand Down
12 changes: 12 additions & 0 deletions test/metabase/driver/mongo_test.clj
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
[driver :as driver]
[query-processor :as qp]
[query-processor-test :refer [rows]]]
[metabase.driver.mongo :as mongo]
[metabase.driver.mongo.query-processor :as mongo-qp]
[metabase.models
[field :refer [Field]]
Expand Down Expand Up @@ -237,3 +238,14 @@
:collection "venues"}
:type :native
:database (data/id)}))))


;; tests for `most-common-object-type`
(expect
String
(#'mongo/most-common-object-type [[Float 20] [Integer 10] [String 30]]))

;; make sure it handles `nil` types correctly as well (#6880)
(expect
nil
(#'mongo/most-common-object-type [[Float 20] [nil 40] [Integer 10] [String 30]]))

0 comments on commit 443da22

Please sign in to comment.