xtdb

Stef Coetzee 2024-08-15T01:26:22.276229Z

Encountering an error as of this evening (EDT):

; Execution error (ExceptionInfo) at xtdb.serde/fn (serde.clj:269).
; class xtdb.operator.scan.TrieCursor$fn__26223$fn__26227 cannot be cast to class java.util.function.Function (xtdb.operator.scan.TrieCursor$fn__26223$fn__26227 is in unnamed module of loader clojure.lang.DynamicClassLoader @701c5d4b; java.util.function.Function is in module java.base of loader 'bootstrap')
đź§µ

âś… 1
Stef Coetzee 2024-08-15T01:29:40.172829Z

Reproduced this in isolation, with HTTP client and server. Server config:

;; xtdb.edn
{:log         [:local {:path ".../server/tx-log"}]

 :storage     [:local {:path ".../server/storage"}]

 :http-server {:port 3000}}
;; deps.edn
{:paths     []

 :mvn/repos {"ossrh-snapshots" {:url ""}}

 :deps      {org.clojure/clojure       {:mvn/version "1.11.4"}
             org.slf4j/slf4j-nop       {:mvn/version "2.0.12"}
             com.xtdb/xtdb-core        {:mvn/version "2.0.0-SNAPSHOT"}
             com.xtdb/xtdb-api         {:mvn/version "2.0.0-SNAPSHOT"}
             com.xtdb/xtdb-http-server {:mvn/version "2.0.0-SNAPSHOT"}}

 :aliases   {:xtdb {:jvm-opts ["--add-opens=java.base/java.nio=ALL-UNNAMED"
                               "-Dio.netty.tryReflectionSetAccessible=true"]}}}
Start server with: clojure -M:xtdb -m xtdb.main

Stef Coetzee 2024-08-15T01:34:54.488379Z

Client config:

;; deps.edn
{:paths     ["src"]

 :mvn/repos {"ossrh-snapshots" {:url ""}}

 :deps      {org.clojure/clojure           {:mvn/version "1.11.4"} 
             org.slf4j/slf4j-nop           {:mvn/version "2.0.12"}
             com.xtdb/xtdb-core            {:mvn/version "2.0.0-SNAPSHOT"}
             com.xtdb/xtdb-api             {:mvn/version "2.0.0-SNAPSHOT"}
             com.xtdb/xtdb-http-client-jvm {:mvn/version "2.0.0-SNAPSHOT"}}

 :aliases   {:xtdb {:jvm-opts ["--add-opens=java.base/java.nio=ALL-UNNAMED"
                               "-Dio.netty.tryReflectionSetAccessible=true"]}}}
;; src/mcre/core.clj
(ns mcre.core
  (:require [xtdb.client :as xtc]
            [xtdb.api :as xt]))

(def xt-node-url "")

(comment
  (with-open [node (xtc/start-client xt-node-url)]
    (xt/q node
          "SELECT *
           FROM INFORMATION_SCHEMA.TABLES")) ; This SQL query succeeds

  (with-open [node (xtc/start-client xt-node-url)]
    (xt/submit-tx node
                  [[:sql "INSERT INTO foo (_id, a, b)
                          VALUES ('bar', ?, ?)"
                    [0 1]]])) ; Transaction succeeds

  (with-open [node (xtc/start-client xt-node-url)]
    (xt/q node 
          "SELECT * FROM foo")) ; Encounter error with SQL query

  (with-open [node (xtc/start-client xt-node-url)]
    (xt/q node
          '(from :foo {:bind [{:xt/id "foo"} *]}))) ; Encounter error with XTQL query
  :rcf)

Stef Coetzee 2024-08-15T01:36:24.451029Z

After another transaction, query fails with the following error message:

; Execution error (ExceptionInfo) at xtdb.serde/fn (serde.clj:269).
; clojure.lang.ExceptionInfo: Ingestion stopped: class xtdb.indexer.live_index.LiveIndex$reify__24421$fn__24461 cannot be cast to class java.util.function.Function (xtdb.indexer.live_index.LiveIndex$reify__24421$fn__24461 is in unnamed module of loader clojure.lang.DynamicClassLoader @7ed91434; java.util.function.Function is in module java.base of loader 'bootstrap') {}

Stef Coetzee 2024-08-15T01:37:07.506179Z

(Did delete .m2 to start from clean slate.)

Stef Coetzee 2024-08-15T01:40:22.403759Z

Not sure where I might be going astray. 🤔

jarohen 2024-08-15T06:47:56.271199Z

hey Stef - could you try upgrading to Clojure 1.12.0-rc1? we're starting to make use of the new SAM functionality because we've got quite a lot of interop

1
Stef Coetzee 2024-08-15T10:44:54.296549Z

Hadn't thought of that. Thanks @jarohen!

🙏 1
tatut 2024-08-15T12:04:16.177159Z

how do I use system time in a SQL subquery? say I want to NEST_ONE a referenced product with the price it had at the order time

refset 2024-08-16T09:16:11.450359Z

yep I think it's fair to say that the SQL temporal table spec and all the various half-baked implementations just haven't been sufficiently simple or easy for users

tatut 2024-08-15T12:05:08.094309Z

SELECT *,
       NEST_MANY(SELECT *,
                        NEST_ONE(SELECT p.name,p.price
                                   FROM products FOR SYSTEM_TIME AS OF '2024-08-14 20:00' AS p
                                  WHERE p._id=ol.product_id) as product
                   FROM orderline ol
                  WHERE ol.order_id=o._id) AS orderlines
  FROM orders o
that parses, but results in a malformed-queryresponse

tatut 2024-08-15T12:06:07.765569Z

if I use a reference like AS OF o.datetime that fails to parse

tatut 2024-08-15T12:20:15.208899Z

querying just products at a parameterized time works ok, like:

{"sql": "SELECT p.name,p.price FROM products FOR SYSTEM_TIME AS OF $1 p",
 "queryOpts": {"args": [{"@type": "xt:timestamp",
                         "@value": "2024-08-14T20:00:00.000"}]}}

tatut 2024-08-15T12:40:26.504439Z

I was able to work around by

SELECT *,
       NEST_MANY(SELECT *,
                        NEST_ONE(SELECT p.name,p.price
                                   FROM products FOR ALL VALID_TIME p
                                  WHERE p._id=ol.product_id
                                    AND p._valid_from < o._valid_from
                               ORDER BY p._valid_from DESC LIMIT 1) as product
                   FROM orderline ol
                  WHERE ol.order_id=o._id) AS orderlines
  FROM orders o
so fetching for all valid time and selecting one that is at the right time, but should “AS OF” work?

👍 1
tatut 2024-08-15T12:43:32.099539Z

for reference, my example case is here https://github.com/tatut/swixt/blob/main/xtdb-api.restclient#L76

refset 2024-08-15T13:46:28.878269Z

column references aren't supported for AS OF currently, but we have debated it a little again recently, see https://github.com/xtdb/xtdb/issues/3447 and https://github.com/xtdb/xtdb/issues/3552

refset 2024-08-15T13:52:15.129869Z

your original example's malformed-query indicates a poorly handled error but I can see you have an incorrect literal, try: (i.e. add TIMESTAMP and the :00 component)

SELECT *,
       NEST_MANY(SELECT *,
                        NEST_ONE(SELECT p.name,p.price
                                   FROM products FOR SYSTEM_TIME AS OF TIMESTAMP '2024-08-14 20:00:00' AS p
                                  WHERE p._id=ol.product_id) as product
                   FROM orderline ol
                  WHERE ol.order_id=o._id) AS orderlines
  FROM orders o

refset 2024-08-15T13:55:06.833579Z

if you squint, in the middle of the malformed-query error you will see :pred ["clojure.core/some-fn" "xtdb.logical-plan/util-date?" "xtdb.logical-plan/temporal?"], :val "2024-08-14 20:00"

âś… 1
tatut 2024-08-15T13:55:55.296929Z

I would think this use-case would be perfect fit for “time travel”, so you don’t need to keep track of when a product’s price has changed… but can always fetch the price it had at a given order time

📝 1
tatut 2024-08-15T13:56:16.522579Z

so AS OF would be much neater than doing all time and filtering

tatut 2024-08-15T14:01:25.333609Z

or perhaps even better if there was a way to refer to a specific row in time, instead of just referring to id 1 you could refer to 1@12312 (like in git you can permalink to some file at a particular commit hash)

refset 2024-08-15T15:03:23.315309Z

mm, syntax like SELECT * FROM products._id.101@[12312, 234324] could be fun! for now though I would suggest building a compiler on top of HoneySQL or something if you want some short-hand 🙂

tatut 2024-08-15T15:14:29.403569Z

well a syntax isn’t really necessary, just a SQL datatype that references a particular row at a particular time

tatut 2024-08-15T15:15:10.449799Z

idk what it would look like, but it would be very handy for this example

tatut 2024-08-15T15:15:54.282109Z

like “I want this orderline A to refer to a product B as it exists currently” kind of like a permalink

refset 2024-08-15T15:51:28.603979Z

perhaps a computed column like products._row_id that is an alias-like tuple composite key of _id + _system_from + _valid_from

đź’ˇ 1
tatut 2024-08-16T05:19:03.107729Z

I like that the temporality in v2 is more a first class citizen, than in v1… a permalink reference type would make it even better, much more convenient for the programmer than filtering

tatut 2024-08-16T05:20:06.854929Z

I see there’s temporal table support in some relational engines, but for some reason they haven’t really caught on. For example in mariadb, you need to turn off versioning for any schema change in the table, making it mostly useless as you lose history whenever the table changes

đź’Ż 1