A Whirlwind Tour of Datomic Query

16 April 2013

Introduction

Reading the Code

=>

A Simple Schema

Get Connected

Listing 1: Getting Connected

( require

'[datomic.api :as d]

'[datomic.samples.news :as news] )



( def uri "datomic:mem://news" )

( def conn ( news/setup-sample-db-1 uri ))



Get a Database Value

Listing 2: Getting a Database Value

( def db ( d/db conn ))



A first query

Listing 3: Finding All Users

( d/q '[ :find ?e

:where [?e :user/email ]]

db )



=> #{[17592186045424] [17592186045425]}



As query is the most commonly used function in Datomic, it has the terse name d/q .

. The first argument is a query expression. The :where clause indicates what you want to find, "those entities ?e that have a :user/email attribute." The :find clause tells which variables to return.

clause indicates what you want to find, "those entities that have a attribute." The clause tells which variables to return. The second argument is the database value we obtained in the previous step.

A simple join

Listing 4: Finding a Specific User

( d/q '[ :find ?e

:in $ ?email

:where [?e :user/email ?email]]

db

"editor@example.com" )



=> #{[17592186045425]}

A database join

Listing 5: Finding a User's Comments

( d/q '[ :find ?comment

:in $ ?email

:where [?user :user/email ?email]

[?comment :comment/author ?user]]

db

"editor@example.com" )



=> #{[17592186045451]

[17592186045450]

... }



Aggregates

Listing 6: Returning an Aggregate

( d/q '[ :find ( count ?comment )

:in $ ?email

:where [?user :user/email ?email]

[?comment :comment/author ?user]]

db

"editor@example.com" )



=> [[10]]



More joins

Listing 7: Multiple Joins

( d/q '[ :find ( count ?comment )

:where [?comment :comment/author ]

[?commentable :comments ?comment]

[?commentable :user/email ]]

db )



=> []



Schema-aware joins

Listing 8: A Schema Query

( d/q '[ :find ?attr-name

:where [?ref :comments ]

[?ref ?attr]

[?attr :db/ident ?attr-name]]

db )



=> #{[ :story/title ]

[ :comment/body ]

[ :story/url ]

[ :comment/author ]

[ :comments ]}



Entities

Listing 9: Finding an Entity ID

( def editor-id ( ->> ( d/q '[ :find ?e

:in $ ?email

:where [?e :user/email ?email]]

db

"editor@example.com" )

ffirst ))







Listing 10: Getting an Entity

( def editor ( d/entity db editor-id ))



Listing 11: A Lazy Entity

editor



=> { :db/id 17592186045425}



Listing 12: Requesting an Attribute

( :user/firstName editor )



=> "Edward"



Listing 13: Touching an Entity

( d/touch editor )

=> { :user/firstName "Edward" ,

:user/lastName "Itor" ,

:user/email "editor@example.com" ,

:db/id 17592186045425}



Are Entities ORM?

Listing 14: Navigating Backwards

( -> editor :comment/_author )



=> [{ :db/id 17592186045441}

{ :db/id 17592186045443}

... ]



Listing 15: Navigating Deeper

( ->> editor :comment/_author ( mapcat :comments ))



=> ( { :db/id 17592186045448}

{ :db/id 17592186045450}

... )



Time travel

the transaction entity tx that created the datom

that created the datom the relative time, t of the transaction

of the transaction the clock time :db/txInstant of the transaction

Listing 16: Querying for a Transaction

( def txid ( ->> ( d/q '[ :find ?tx

:in $ ?e

:where [?e :user/firstName _ ?tx]]

db

editor-id )

ffirst ))



Listing 17: Converting Transaction to T

( d/tx->t txid )

=> 1023





Listing 18: Getting a Tx Instant

( -> ( d/entity ( d/db conn ) txid ) :db/txInstant )



=> #inst "2013-02-20T16:27:11.788-00:00"



Listing 19: Going Back in Time

( def older-db ( d/as-of db ( dec txid )))



( :user/firstName ( d/entity older-db editor-id ))



=> "Ed"



Auditing

Listing 20: Querying Across All Time

( def hist ( d/history db ))



( ->> ( d/q '[ :find ?tx ?v ?op

:in $ ?e ?attr

:where [?e ?attr ?v ?tx ?op]]

hist

editor-id

:user/firstName )

( sort-by first ))



=> ( [13194139534319 "Ed" true]

[13194139534335 "Ed" false]

[13194139534335 "Edward" true] )



Everything is Data

Listing 21: Querying Plain Java Data

( d/q '[ :find ?e

:where [?e :user/email ]]

[[1 :user/email "jdoe@example.com" ]

[1 :user/firstName "John" ]

[2 :user/email "jane@example.com" ]] )



=> #{[1] [2]}



This tour is to help those new to Datomic understand Datomic's built-in datalog by providing a simple domain and schema, and by walking through some use cases. For a more complete treatment of Datomic's query capabilities, please take a look at the documentation To follow along in code, you can download Datomic Free Edition at http://downloads.datomic.com/free.html , and the sample project at https://github.com/datomic/day-of-datomic . The code examples should be executed interactively, in the order presented in the article, from a Clojure REPL. The complete code is in the query_tour.clj file . Theprefix indicates responses that is printed by the REPL. The use of ellipsis () in output indicates that a larger result has been truncated for brevity.The example queries that follow work against a simplified schema that you might use for a social news database, containing users, stories, and comments:In order to get a database connection, you will need tonamespaces for the Datomic API and for the news application, then setup a sample database.The connection from the previous step is already populated with a schema and sample data, but before you query against it, you must get the currentof the database. Syntactically this is trival:While the syntax of this step is trivial, the semantic implications are deep. Datomic queries do not happen "over there" in some database process, they happen here, in your process's memory space.Users in the system are identified by aattribute. Lets find all of them:The large integers are entity ids. Entity ids are auto-assigned, so you may not see the same values on your system. There are several things to note here:Rather than finding all users, let's query for a particular user. This requires joining two query inputs: the database value, and the email you are seeking:Notice that there are now three arguments to the query: the query expression plus the two inputs. Also, since there are two inputs, there is now anclause to name the inputs, in the order they appear, e.g.binds the database, andbinds "editor@example.com". Names starting withname databases, and names starting withname variables.The previous join of a database to a single prebound variable may not even look like a join to you – in many databases this operation would be called a parameterized query. So let's look at a more traditional join, that leverages indexes in the database to associate more than one "type" of entity. The following query will find all of the editor's comments:Notice that theclause now has two data patterns: one associating users to their emails, and another associating comments to the user that authored them. Because the variableappears in both clauses, the joins on, finding only comments made by the editor.You can obtain aggregates by changing theclause to include an aggregating function. Instead of finding the editor's comments, let's justthem:Queries can make many joins. The following query joins comments to their referents, and joins the referents toto find any comments that are about people:No results is good news, because you don't want to let things get personal by allowing people to comment on other people.You cannot comment on people, but what kinds of thingsyou comment on?The three slots you have seen in data patterns so far are, and. Up this point, theandpositions have typically been variables, but thehas always been constant.That need not be the case. Here is a query that finds all the attributes of entities that have been commented on:Schema entities are ordinary entities, like any other data in the system. Rather then return their entity ids (in the query above), you can join throughto find the programmatic identifiers that name each attribute. Judging from these attribute names, there are comments about stories and comments about other comments, exactly what you would expect given the schema.In client/server databases where query happens in another process, it is often critical to get the answer and all of its details in a single query. (If you spread the work across multiple steps, you run the risk of the data changing between steps, leading to inconsistent results.)Since Datomic performs queries in-process, against an immutable database value, it is feasible to decompose the work of query into steps. One very decomposition is to use a query toentities, and then to use Datomic's entity API to navigate to the relevant details about those entities.Let's try it with the editor. First, use a query to find the editor's entity id:Notice that the query returns only the, not any particular attribute values.Now, you can call, passing the database value and the entity id to get the editor entity.When you first look at an entity, it appears to be a mostly empty map:That is because entities are. Their attribute values will appear once you ask for them:If you are feeling more eager, you canan entity to immediately realize all of its attributes:No. Entities are used for some of the same purposes that you might use an ORM, but their capabilities are quite different. Entities differ from ORM objects in that the conversion between raw datoms and entities is entirely a mechanical process. There is never any configuration, and relationships are always available and (lazily!) navigable.Entities also differ from most ORM objects in that relationships can be navigated in either direction. In the previous code example, you saw how themethod would automatically navigate all outbound relationships from an entity. However, you can also navigaterelationships, by following the convention of prefixing attribute names with an underscore. For example, a user's comments happen to be modeled as a relationship from the comment to the editor. To reach these comments from the editor entity, you can navigate theattribute backwards:This process can, of course, be extended as far as you like, e.g. the following example navigates to all the comments people have made on the editor's comments:Update-in-place databases can tell you about the present, but most businesses need also to know about the past. Datomic provides this, by allowing you to take a value of a database as of a certain point in time.Given any datom, there are three time-related pieces of data you can request:The transaction entity is available as a fourth optional component of any data pattern. The following query finds the transaction that set the current value for the editor's first name:Given a transaction id, thefunction returns the system-relative time that the transaction happened.Relative time is useful for "happened-before" type questions, but sometimes you want to know the actual wall clock time. This is stored once per transaction, as theproperty of theentity:Given a, orvalue, you can travel to that point in time with. The example below goes back in time to before the point that the first name Edward was introduced, to see its past value:The example above shows anvalue of the database being used as an argument to, butvalues can be used anyhere a current database value can be used, including as an argument to query.Whileis useful for looking at a moment in the past, you may also want to perform time-spanning queries. This is particularly true in audit scenarios, where you might want a complete history report of some value.To perform such queries, you can use theview of a database, which spans all of time. The following query shows the entire history of the editor'sattribute:The optional fifth field in adata pattern, namedin the example above, matches true if a datom is asserted, or false if it is being retracted.Transaction 13194139534319 set the editor's first name to "Ed", and transaction 13194139534335 set it to "Edward". The cardinality ofis one, which means that the system will only permit one value of that attribute per entity at any given time. To enforce this constaint, Datomic will automatically retract past values where necessary. Thus the transaction that asserts "Edward" also includes a retraction for the previous value "Ed".One of Datomic's design goals is "put declarative power in the hands of developers." Having queries that run in your process goes a long way to meet this objective, but what if you have data that is not in a Datomic database?Datomic's query engine can run without a database, against arbitrary data structures in your application.The following example shows the query you began with, this time running against a plain Java list in memory:The idea behind POJOs (plain old Java objects) is exactly right, and Datomic encourages you to take it one step further, to plain old lists and maps.