Comments (4)
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.
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.
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.
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)
- DB cannot be started HOT 2
- deal with client calls when server search engine is timed out
- Datalevin doesn't play well with sci (through malli) HOT 6
- value unification in function expression result
- [QUESTION]: separate the client into a small lib? HOT 23
- Allow indexing of arbitrary data
- Bump taoensso/encore is breaking datalevin HOT 4
- Options not recovered after automatic reconnection HOT 2
- Return maps
- Staged entity transactions broken for refs HOT 2
- Contribution Instructions out of date? HOT 3
- TTL for key-value store
- Materialized views
- orm.lmdbjava.Dbi$MapResizedException on query after reimport 0.8.16 → 0.9.5 HOT 1
- Hard fail when upgrading to 0.9.0 from 0.8.29 HOT 6
- Port Jepsen Test for Datomic
- Use CVM algorithm for faster cardinality estimation
- Micro batch transaction mode
- CSV data import HOT 1
- Query functions similar to LIKE and IN operators in SQL
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from datalevin.