Skip to content

Commit

Permalink
Correctly emit and compare uuids in EE
Browse files Browse the repository at this point in the history
  • Loading branch information
FiV0 committed Aug 10, 2023
1 parent a1e0ebe commit f6b622c
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 2 deletions.
5 changes: 3 additions & 2 deletions core/src/main/clojure/xtdb/expression.clj
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
(java.nio ByteBuffer)
(java.nio.charset StandardCharsets)
(java.time Clock Duration Instant LocalDate LocalDateTime LocalTime OffsetDateTime ZoneOffset ZonedDateTime)
(java.util Arrays Date List)
(java.util Arrays Date List UUID)
(java.util.regex Pattern)
(java.util.stream IntStream)
(org.apache.arrow.vector PeriodDuration ValueVector)
Expand Down Expand Up @@ -327,6 +327,7 @@
(defmethod emit-value LocalDateTime [_ code] `(util/instant->micros (.toInstant ~code ZoneOffset/UTC)))
(defmethod emit-value LocalTime [_ code] `(.toNanoOfDay ~code))
(defmethod emit-value Duration [_ code] `(quot (.toNanos ~code) 1000))
(defmethod emit-value UUID [_ code] `(util/uuid->byte-buffer ~code))

;; consider whether a bound hash map for literal parameters would be better
;; so this could be a runtime 'wrap the byte array' instead of this round trip through the clj compiler.
Expand Down Expand Up @@ -675,7 +676,7 @@
{:return-type :bool
:->call-code #(do `(= ~@%))})

(doseq [col-type #{:varbinary :utf8}]
(doseq [col-type #{:varbinary :utf8 :uuid}]
(defmethod codegen-call [:= col-type col-type] [_]
{:return-type :bool,
:->call-code #(do `(.equals ~@%))}))
Expand Down
7 changes: 7 additions & 0 deletions core/src/main/clojure/xtdb/util.clj
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,13 @@
(.putLong (.getLeastSignificantBits uuid)))]
(.array bb)))

(defn uuid->byte-buffer ^ByteBuffer [^UUID uuid]
(let [bb (doto (ByteBuffer/allocate 16)
(.putLong (.getMostSignificantBits uuid))
(.putLong (.getLeastSignificantBits uuid)))]
(.position bb 0)
bb))

(defn ->lex-hex-string
"Turn a long into a lexicographically-sortable hex string by prepending the length"
[^long l]
Expand Down
5 changes: 5 additions & 0 deletions src/test/clojure/xtdb/expression_test.clj
Original file line number Diff line number Diff line change
Expand Up @@ -1744,3 +1744,8 @@
(t/is (= exp (test-null-cast :i64)))
(t/is (= exp (test-null-cast :utf8)))
(t/is (= exp (test-null-cast [:timestamp-local :milli]))))))

(t/deftest test-uuids
(t/is (= true
(project1 '(= #uuid "00000000-0000-0000-0000-000000000000" a)
{:a #uuid "00000000-0000-0000-0000-000000000000"}))))

0 comments on commit f6b622c

Please sign in to comment.