Fork me on GitHub
#xtdb
<
2023-08-10
>
mesota18:08:48

Hello there, XTDB noob here. I’m having a problem with the following attempt at date comparison: Is it the nested function call or something else? The XTDB docs don’t say that about predicates but transformation functions. Also, what would be the best way to compare dates? Is there a date data type that works better in this situation? Thanks PS: The date values are just for testing the query. In real usage, it will be other values.

'{:find [(pull e [*])]
         :where [
                 [(= (clojure.core/compare  (java.time.LocalDateTime/now) (java.time.LocalDateTime/of 2018 1 1)) -1 )]
                                   ]}

refset10:08:49

Hey @U9FEN7GF6 🙂 so firstly you can't use arbitrary Clojure expressions in the middle of Datalog, instead you are limited to only the top-level of parens being interpreted as a function call. In this case that function is =, and the (clojure.core/compare ... is being interpreted as a list of symbols rather than a nested expression. To work around this you can use intermediate clauses that introduce new logic variables, e.g. [(java.time.LocalDateTime/now) now] [(= now -1)] As for date types, XT uses Date internally quite heavily but other types are natively supported within the sorted indexes, including LocalDateTime - specifically: https://github.com/xtdb/xtdb/blob/29fa08b921e0809f37469cbb40972daa4b88851c/core/src/xtdb/codec.clj#L96-L100 I couldn't personally say that any date type is 'better', although I would tend to stick with Date until I had reasons to use anything else, purely because I understand it the best 😅 Also note that clojure.core/compare should be available as compare (i.e. without the ns component, along with everything else under clojure.core/*) when used in the function position at the top-level of a clause, but if there's ever a chance of clashing symbols then it doesn't hurt to be verbose ☺️

👍 2
mesota10:08:04

Thanks! In the XTDB datalog tutorial, it says “You can use any Clojure function as a predicate function” and it doesn’t discuss anything about other limitations including nested expressions and so on. Anyway, I guess I’ll have to study XTDB more thoroughly.

refset10:08:54

Ahh, thanks for pointing that out. I agree that would be an excellent place to mention these things!

👍 4