Not possible to pass more then one vector variable to or-join

The example schema:

(def schema
[
        {
          :db/ident :type
          :db/valueType :db.type/string
          :db/cardinality :db.cardinality/one
        }
        {
          :db/ident :typeCode
          :db/valueType :db.type/long
          :db/cardinality :db.cardinality/one
        }
        {
          :db/ident :typeId
          :db/valueType :db.type/long
          :db/cardinality :db.cardinality/one
        }
])


(d/transact conn schema)

Example data:

(def data [
  {
    :type "User"
    :typeCode 1
    :typeId 1
  }
  {
    :type "User"
    :typeCode 2
    :typeId 2
  }
])
(d/transact conn data)

The next query fails:

(d/q "[ 
:find ?e 
:in $ ?type [?typeIds ...] [?typeCodes ...] 
:where 
  [?e :type ?type]
  (or-join [?e ?typeCodes ?typeIds]
    [?e :typeCode ?typeCodes]
    [?e :typeId ?typeIds]
  )
]"
(d/db conn), "User" [1, 2], [1, 2])

then the next error happens:

Execution error (ArrayIndexOutOfBoundsException) at datomic.datalog/fn$project (datalog.clj:503).
Index 2 out of bounds for length 2

if to pass non vector variables:

(d/q "[ 
:find ?e 
:in $ ?type ?typeIds ?typeCodes
:where 
  [?e :type ?type]
  (or-join [?e ?typeIds ?typeCodes]
    [?e :typeCode ?typeCodes]
    [?e :typeId ?typeIds] 
  )
]"
(d/db conn), "User" 1, 1)

then all works.

P.S.

  1. Do not try to understand the any meaning in the schema and queries, it is just the quickest example to show the problem
  2. All failed queries are executed successfully when DB is empty
  3. Looks like the order of vector variables from :in section matters for the order of variables for rule-vars of or-join

if to execute

(d/q "[ 
:find ?e 
:in $ ?type [?typeCodes ...] [?typeIds ...] 
:where 
  [?e :type ?type]
  (or-join [?e ?typeCodes ?typeIds]
    [?e :typeCode ?typeCodes]
    [?e :typeId ?typeIds] 
    [(get-else $ ?e :typeId ?typeIds) ?someIds]
    [(get-else $ ?e :typeCode ?typeCodes) ?someCodes]
  )
]"
(d/db conn), "User" [1, 2], [1, 2])

when ?typeIds is the last listed in :in section and rule-vars then the next error happens:

:db.error/insufficient-binding [?typeIds] not bound in expression clause: [(get-else $ ?e :typeId ?typeIds) ?someIds]

datomic-on-prem:1.0.6202

1 Like

@marshall @stu @jaret Guys, can it somehow related to

and

?

1 Like

Hi @nikolayandr,

Regarding the general case of supplying two vectors to an or-join. I’ve reproduced the behavior and we are investigating for a resolution. We will update this thread and your case once we have more information. Thank you for the report!

Or-joins require that all inputs be bound. The get-else in this scenario is not binding one side of the or-join. Thus one side is unbound. I wanted to clarify that because even after we address the multiple vectors this approach would not work.

Thanks,
Jaret

Thank you, @jaret . I believe Not possible to pass attributes as argument for fulltext used inside or-join also relates to this issue. Could you please let know whether it is the same issue, so that we could plan our future work, thanks.