Skip to content
Snippets Groups Projects
Unverified Commit b3c8ea8f authored by Cam Saul's avatar Cam Saul Committed by GitHub
Browse files

Make sure auto binning always has at least one bin (#20978)

parent 569cdc4d
Branches
Tags
No related merge requests found
......@@ -63,9 +63,12 @@
(s/defn ^:private calculate-num-bins :- su/IntGreaterThanZero
"Calculate number of bins of width `bin-width` required to cover interval [`min-value`, `max-value`]."
[min-value :- s/Num, max-value :- s/Num, bin-width :- (s/constrained s/Num (complement neg?) "number >= 0")]
(long (Math/ceil (/ (- max-value min-value)
bin-width))))
[min-value :- s/Num
max-value :- s/Num
bin-width :- (s/constrained s/Num (complement neg?) "number >= 0")]
(max (long (Math/ceil (/ (- max-value min-value)
bin-width)))
1))
(s/defn ^:private resolve-default-strategy :- [(s/one (s/enum :bin-width :num-bins) "strategy")
(s/one {:bin-width s/Num, :num-bins su/IntGreaterThanZero} "opts")]
......
......@@ -2,7 +2,9 @@
(:require [clojure.test :refer :all]
[metabase.models.card :refer [Card]]
[metabase.models.field :as field :refer [Field]]
[metabase.query-processor :as qp]
[metabase.query-processor.middleware.binning :as binning]
[metabase.sync :as sync]
[metabase.test :as mt]
[metabase.util :as u]))
......@@ -150,3 +152,25 @@
:breakout [[:field "PRICE" {:base-type :type/Float, :binning {:strategy :default}}]]
:aggregation [[:count]]})
(mt/formatted-rows [int int]))))))
(mt/defdataset single-row
[["t" [{:field-name "lat"
:base-type :type/Decimal
:semantic-type :type/Latitude}
{:field-name "lon"
:base-type :type/Decimal
:semantic-type :type/Longitude}]
[[-27.137453079223633 -52.5982666015625]]]])
(deftest auto-bin-single-row-test
(testing "Make sure we can auto-bin a Table that only has a single row (#13914)"
(mt/dataset single-row
;; sync the Database so we have valid fingerprints for our columns.
(sync/sync-database! (mt/db))
(let [query (mt/mbql-query t
{:breakout [[:field %lat {:binning {:strategy :default}}]
[:field %lon {:binning {:strategy :default}}]]
:aggregation [[:count]]})]
(mt/with-native-query-testing-context query
(is (= [[-30.00M -60.00M 1]]
(mt/rows (qp/process-query query)))))))))
......@@ -408,7 +408,7 @@
;; TODO - not sure everything below belongs in this namespace
(s/defn ^:private dataset-field-definition :- ValidFieldDefinition
[{:keys [coercion-strategy base-type] :as field-definition-map} :- DatasetFieldDefinition]
[field-definition-map :- DatasetFieldDefinition]
"Parse a Field definition (from a `defdatset` form or EDN file) and return a FieldDefinition instance for
comsumption by various test-data-loading methods."
;; if definition uses a coercion strategy they need to provide the effective-type
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment