Skip to content

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

fix: handle pivot-grouping as a BigDecimal (#62189)

Edward Pageta27f64224012
diff --git a/src/metabase/pivot/core.cljc b/src/metabase/pivot/core.cljc
index 0ced086ab09..bb0b4be5b02 100644
--- a/src/metabase/pivot/core.cljc
+++ b/src/metabase/pivot/core.cljc
@@ -27,6 +27,23 @@
(decimal? x) (double x) ;; Handles BigDecimal
:else x)))
+(defn- ensure-is-int
+ "Convert a clojure value that may not be a type bitwise operations can use into one it can. Or throw an
+ error if it is not losslessly convertible into an integer. "
+ [x]
+ #?(:cljs x
+ :clj (cond
+ (int? x) x
+ (integer? x) (long x)
+ (decimal? x) (try (.longValueExact ^BigDecimal x)
+ ;; catch this error since java.lang.ArithmeticException doesn't make sense
+ (catch java.lang.ArithmeticException e
+ (throw (ex-info "Non-Integer cannot be used as pivot-grouping column"
+ {:data x}
+ e))))
+ :else (throw (ex-info "Non-Integer cannot be used as pivot-grouping column"
+ {:data x})))))
+
(defn- pivot-group-column?
"Is the given column the pivot-grouping column?"
[col]
@@ -44,7 +61,7 @@
(memoize
(fn [pivot-group num-breakouts]
(let [breakout-indexes (range num-breakouts)]
- (into [] (filter #(zero? (bit-and (bit-shift-left 1 %) pivot-group)) breakout-indexes))))))
+ (into [] (filter #(zero? (bit-and (bit-shift-left 1 %) (ensure-is-int pivot-group))) breakout-indexes))))))
(defn- remove-item-by-index
"Remove an item with the given `index` from collection `v`."
diff --git a/test/metabase/pivot/core_test.cljc b/test/metabase/pivot/core_test.cljc
index fffbdc7a70c..708bcd919f5 100644
--- a/test/metabase/pivot/core_test.cljc
+++ b/test/metabase/pivot/core_test.cljc
@@ -526,3 +526,91 @@
:span 1
:maxDepthBelow 0}]
result)))))
+
+#?(:clj
+ (deftest ^:parallel ensure-is-int-test
+ (testing "ensure-is-int properly converts different numeric types for bitwise operations"
+ (testing "Should handle regular integers"
+ (is (= 5 (#'pivot/ensure-is-int 5)))
+ (is (= 0 (#'pivot/ensure-is-int 0)))
+ (is (= 7 (#'pivot/ensure-is-int 7))))
+
+ (testing "Should handle long values"
+ (is (= 5 (#'pivot/ensure-is-int 5N)))
+ (is (= 0 (#'pivot/ensure-is-int 0N)))
+ (is (= 7 (#'pivot/ensure-is-int 7N))))
+
+ (testing "Should convert BigDecimal values that are losslessly convertible to int"
+ (is (= 5 (#'pivot/ensure-is-int (BigDecimal. "5"))))
+ (is (= 0 (#'pivot/ensure-is-int (BigDecimal. "0"))))
+ (is (= 7 (#'pivot/ensure-is-int (BigDecimal. "7"))))
+ (is (= 123 (#'pivot/ensure-is-int (BigDecimal. "123"))))
+
+ ;; Test with explicitly integer-valued BigDecimals
+ (is (= 5 (#'pivot/ensure-is-int (BigDecimal. "5.0"))))
+ (is (= 42 (#'pivot/ensure-is-int (BigDecimal. "42.00")))))
+
+ (testing "Should handle edge cases"
+ ;; Test with maximum int value as BigDecimal
+ (is (= Integer/MAX_VALUE
+ (#'pivot/ensure-is-int (BigDecimal. (str Integer/MAX_VALUE)))))
+
+ ;; Test with zero as BigDecimal
+ (is (= 0 (#'pivot/ensure-is-int BigDecimal/ZERO))))
+ (testing "Should throw if it has decimal places"
+ (is (thrown-with-msg? clojure.lang.ExceptionInfo #"Non-Integer cannot be used as pivot-grouping column"
+ (#'pivot/ensure-is-int (BigDecimal. "42.11")))))
+ (testing "Should throw if not convertible"
+ (is (thrown-with-msg? clojure.lang.ExceptionInfo #"Non-Integer cannot be used as pivot-grouping column"
+ (#'pivot/ensure-is-int "1232")))))))
+
+#?(:clj
+ (deftest ^:parallel bigdecimal-bitwise-operations-integration-test
+ (testing "Full integration test showing BigDecimal pivot-grouping values work through the bitwise pipeline"
+ (testing "Bitwise operations work correctly after ensure-is-int conversion"
+ (let [pivot-group-bigdecimal (BigDecimal. "5") ; binary: 101
+ pivot-group-int 5 ; same value as int
+ num-breakouts 3]
+
+ (doseq [bit-index (range num-breakouts)]
+ (let [bit-mask (bit-shift-left 1 bit-index)
+ bigdecimal-result (bit-and bit-mask (#'pivot/ensure-is-int pivot-group-bigdecimal))
+ int-result (bit-and bit-mask pivot-group-int)]
+ (is (= int-result bigdecimal-result)
+ (str "Bitwise AND with bit-mask " bit-mask " should be same for BigDecimal and int"))))
+
+ ;; Test the full active breakout logic
+ (is (= (#'pivot/get-active-breakout-indexes pivot-group-int num-breakouts)
+ (#'pivot/get-active-breakout-indexes pivot-group-bigdecimal num-breakouts))
+ "get-active-breakout-indexes should return same result for BigDecimal and int")))
+
+ (testing "Works with decimal representations that are integers"
+ (let [pivot-group-decimal (BigDecimal. "3.0") ; Should convert to 3
+ pivot-group-int 3
+ num-breakouts 3]
+
+ (is (= (#'pivot/get-active-breakout-indexes pivot-group-int num-breakouts)
+ (#'pivot/get-active-breakout-indexes pivot-group-decimal num-breakouts))
+ "BigDecimal with .0 should work same as integer")))
+
+ (testing "Handles edge case BigDecimal values"
+ (is (= [0 1 2]
+ (#'pivot/get-active-breakout-indexes BigDecimal/ZERO 3))
+ "BigDecimal/ZERO should work like integer 0")
+
+ (is (= [1 2]
+ (#'pivot/get-active-breakout-indexes BigDecimal/ONE 3))
+ "BigDecimal/ONE should work like integer 1")))
+
+ (testing "Memoization works correctly with BigDecimal values"
+ (let [pivot-group (BigDecimal. "6")
+ num-breakouts 3
+ first-call (#'pivot/get-active-breakout-indexes pivot-group num-breakouts)
+ second-call (#'pivot/get-active-breakout-indexes pivot-group num-breakouts)]
+
+ (is (= first-call second-call))
+ (is (= [0] first-call) "pivot-group 6 with 3 breakouts should return [0]")
+
+ (let [equivalent-pivot-group (BigDecimal. "6.0")
+ third-call (#'pivot/get-active-breakout-indexes equivalent-pivot-group num-breakouts)]
+ (is (= first-call third-call) "Equivalent BigDecimal values should return same result"))))))