Skip to content

Commit

Permalink
Catch ArithmeticException at EE boundary, fix xtdb#3569
Browse files Browse the repository at this point in the history
  • Loading branch information
FiV0 committed Oct 18, 2024
1 parent a413b79 commit c0910bb
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 10 deletions.
29 changes: 20 additions & 9 deletions core/src/main/clojure/xtdb/expression.clj
Original file line number Diff line number Diff line change
Expand Up @@ -814,12 +814,7 @@

(defmethod codegen-call [:/ :int :int] [{:keys [arg-types]}]
{:return-type (types/least-upper-bound arg-types)
:->call-code #(do `(try
(quot ~@%)
(catch ArithmeticException e#
(case (.getMessage e#)
"/ by zero" (throw-div-0)
(throw e#)))))})
:->call-code #(do `(quot ~@%))})

(defmethod codegen-call [:/ :num :num] [{:keys [arg-types]}]
{:return-type (types/least-upper-bound arg-types)
Expand Down Expand Up @@ -1611,6 +1606,20 @@
(fn [expr opts]
(f expr (assoc opts :zone-id (.getZone *clock*)))))

(defn arithmetic-ex->runtime-ex [^Throwable cause]
(let [message (.getMessage cause)]
(prn message)
(cond
(and message (str/index-of message "/ by zero"))
(err/runtime-err ::division-by-zero {::err/message "data exception — division by zero"} cause)

(and message (str/index-of message "overflow"))
(err/runtime-err ::overflow-error {::err/message "data exception — overflow error"} cause)

:else
(err/runtime-err ::unknown-arithmetic-error {::err/message "data exception - arithmetic exception"} cause))))


(def ^:private emit-projection
"NOTE: we macroexpand inside the memoize on the assumption that
everything outside yields the same result on the pre-expanded expr - this
Expand All @@ -1630,9 +1639,11 @@
~@writer-bindings
row-count# (.getRowCount ~rel-sym)]
(dotimes [~idx-sym row-count#]
~(continue (fn [t c]
(write-value-out! t c))))))

(try
~(continue (fn [t c]
(write-value-out! t c)))
(catch ArithmeticException e#
(throw (arithmetic-ex->runtime-ex e#)))))))
#_(doto clojure.pprint/pprint) ; <<no-commit>>
#_(->> (binding [*print-meta* true]))
eval))
Expand Down
6 changes: 5 additions & 1 deletion src/test/clojure/xtdb/expression_test.clj
Original file line number Diff line number Diff line change
Expand Up @@ -387,7 +387,11 @@
(t/is (thrown-with-msg? RuntimeException #"division by zero"
(project1 '(/ a 0.0) {:a 5})))
(t/is (thrown-with-msg? RuntimeException #"division by zero"
(project1 '(/ a 0) {:a 5.0}))))
(project1 '(/ a 0) {:a 5.0})))
(t/is (thrown-with-msg? RuntimeException #"overflow"
(project1 '(+ a 9223372036854775807) {:a 9223372036854775807})))
(t/is (thrown-with-msg? RuntimeException #"overflow"
(project1 '(- a -9223372036854775807) {:a 9223372036854775807}))))

(defn- project-mono-value [f-sym val col-type]
(with-open [rel (tu/open-rel [(tu/open-vec (types/col-type->field "s" col-type) [val])])]
Expand Down

0 comments on commit c0910bb

Please sign in to comment.