Giter Club home page Giter Club logo

Comments (4)

garrett-hopper avatar garrett-hopper commented on June 9, 2024

This will need to be implemented in Datalevin directly (rather than in Datascript and duplicated here), as Datascript does not support :db/tupleValue (or :db/tupleValues) at all.

From @huahaiy regarding Datascript not supporting homogenous or heterogeneous tuples

Datascript only supports composite Tuples, because it does not differentiate data types, they are all clojure data, no strings, int, etc

Relevant Slack thread for anyone in the future: https://clojurians.slack.com/archives/C01RD3AF336/p1691260941968389

from datalevin.

garrett-hopper avatar garrett-hopper commented on June 9, 2024

This appears to have been my own misunderstanding of Datomic's query syntax when it comes to tuples.

(There is still an issue with negative number tempids not being recognized in tuple types.)

It appears to work as expected when I use untuple correctly

(let [schema  {:parent/children {:db/valueType   :db.type/tuple
                                 :db/tupleTypes  [:db.type/ref :db.type/long]
                                 :db/cardinality :db.cardinality/many}
               :parent/name     {:db/valueType :db.type/string
                                 :db/unique    :db.unique/identity}
               :child/name      {:db/valueType :db.type/string
                                 :db/unique    :db.unique/identity}}
      conn    (d/create-conn nil schema)
      tempids (:tempids
               (d/transact! conn [{:db/id      -1
                                   :child/name "Child 1"}
                                  {:db/id      -2
                                   :child/name "Child 2"}
                                  {:db/id       -3
                                   :parent/name "Parent 1"}
                                  {:db/id       -4
                                   :parent/name "Parent 2"}]))]
  ;; Work around tempids not working in tuple types
  (d/transact! conn [{:db/id           (tempids -3)
                      :parent/children [[(tempids -1) 0]
                                        [(tempids -2) 1]]}
                     {:db/id           (tempids -4)
                      :parent/children [[(tempids -1) 1]
                                        [(tempids -2) 0]]}])
  (d/q (template
        [:find ?parent ?parent-name ?child-name ?position
         :where
         [?parent :parent/name ?parent-name]
         [?parent :parent/children ?children]
         [(untuple ?children) [?child ?position]]
         [?child :child/name ?child-name]])
       @conn))

from datalevin.

garrett-hopper avatar garrett-hopper commented on June 9, 2024

The desired code without the workaround would be this:

(let [schema  {:parent/children {:db/valueType   :db.type/tuple
                                 :db/tupleTypes  [:db.type/ref :db.type/long]
                                 :db/cardinality :db.cardinality/many}
               :parent/name     {:db/valueType :db.type/string
                                 :db/unique    :db.unique/identity}
               :child/name      {:db/valueType :db.type/string
                                 :db/unique    :db.unique/identity}}
      conn    (d/create-conn nil schema)]
  (d/transact! conn [{:db/id      -1
                      :child/name "Child 1"}
                     {:db/id      -2
                      :child/name "Child 2"}
                     {:db/id       -3
                      :parent/name "Parent 1"
                      :parent/children [[-1 0]
                                        [-2 1]]}
                     {:db/id       -4
                      :parent/name "Parent 2"
                      :parent/children [[-1 1]
                                        [-2 0]]}])
  (d/q (template
        [:find ?parent ?parent-name ?child-name ?position
         :where
         [?parent :parent/name ?parent-name]
         [?parent :parent/children ?children]
         [(untuple ?children) [?child ?position]]
         [?child :child/name ?child-name]])
       @conn))

from datalevin.

garrett-hopper avatar garrett-hopper commented on June 9, 2024

untuple is just an alias for identity (built_ins.cljc#L132), therefore identity could be used in its place for the same purpose. (Though untuple does signal intent better)
Using one or the other is required because destructuring is only supported in clauses that are function calls. (You can't destructure a tuple in a pattern matching clause.)

Also, homogenous tuples can be destructured using ...
E.g.

[?e :tuple ?tuple]
[(untuple ?tuple) [?x ...]]

In which case ?x will be bound to each item in ?tuple as expected.

from datalevin.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.