datalevin

2025-03-23T19:33:28.043969Z

So I've been playing around with using datalevin to query arbitrary data sources.

(def db
  [[0 :name "Bob"]
   [0 :species "Parakeet"]
   [0 :age 3]
   [0 :owners 0 :age 2]
   [0 :owners 0 :name "vigo"]
   [0 :owners 0 :foods 0 :type "avocado"]
   [0 :owners 1 :age 2]
   [0 :owners 1 :name "Bob"]
   [0 :owners 1 :foods 0 :type "apple"]
   [0 :owners 1 :foods 1 :type "avocado"]
   [1 :name "George"]
   [1 :species "Goldfish"]
   [1 :age 1]
   [1 :owners 0 :age 2]
   [1 :owners 0 :name "Bob"]])

(d/q
  '[:find [?name ...]
    :where
    [?pet :owners _  :foods _ :type "avocado"]
    [?pet :name ?name]]
  db)
;; ["Bob"]
Is this behaviour intentional? Cause it's really cool/useful.

Huahai 2025-03-23T20:12:08.137819Z

Yes, these are by design, inherited from Datascript

πŸš€ 1
2025-03-23T20:22:41.605799Z

So my next question is there any reason why this doesn't work with pull? As far as I can tell it's because pull checks for a db (same in Datascript).

2025-03-23T20:23:06.279719Z

I'm going to explore changing that check to see if it would just work with pull

Huahai 2025-03-23T20:48:23.514679Z

Pull expects a triple representation, while queries work with relations, I.e. a collection of tuples. Triple is a special case of tuples

Huahai 2025-03-23T20:49:37.273869Z

Pull is really about scanning triple indices. What you are seeing with datalog working on data collections is about hash join on relations

Huahai 2025-03-23T20:58:58.888739Z

Bindings work not just with vectors, you can destruct map and nested data structures as well. See https://github.com/juji-io/datalevin/blob/a1bbfaa167755056b51dc39deb6405df7490f052/test/datalevin/test/query.clj#L248 for examples

πŸ‘ 2
Ahmed Hassan 2025-03-23T21:03:55.355619Z

test-nested-bindings have single level nested map, is it ergonomic to query deeply nested map?

Huahai 2025-03-23T21:08:34.072279Z

Is it ergonomic? Depending on your personal taste? I guess. I don’t know.

2025-03-23T21:17:41.950499Z

Yeah, after I asked I realised even if pull did work. I probably wouldn't want it, as I'm mostly using this to transform external data into something that I can query in Datalog to extract the bits I care about and then convert to triples my database schema understands. This is all super useful!

2025-03-23T19:34:57.308929Z

Because if I can transform arbitrary edn data into a list of paths they can then be queried.

2025-03-23T19:35:05.936379Z

using datalog

Huahai 2025-03-23T20:14:08.444319Z

Yup