xtdb

seancorfield 2025-05-17T14:11:44.834109Z

Question about the inline XTQL stuff in Beta 8 @jarohen :

FROM (XTDB $$ (-> (from :my-table [x]) (where (<= x 100))) $$) AS my_table
that's based on an example from the docs -- are the parens required here? Is the following valid?
FROM XTDB $$ (-> (from :my-table [x]) (where (<= x 100))) $$ AS my_table

seancorfield 2025-05-17T14:12:37.358549Z

I'm implementing this in HoneySQL, which is why I'm asking...

seancorfield 2025-05-17T14:28:56.024059Z

I've implemented like this for now:

user=> (sql/format [:xtql '(-> (from :my-table [x]) (where (<= x 100)))])
["XTQL $$ (-> (from :my-table [x]) (where (<= x 100))) $$"]
user=> (sql/format {:from [[[:xtql '(-> (from :my-table [x]) (where (<= x 100)))] :my_table]]})
["FROM (XTQL $$ (-> (from :my-table [x]) (where (<= x 100))) $$) AS my_table"]

jarohen 2025-05-17T14:42:55.031089Z

you don't waste your time 😄🙏

jarohen 2025-05-17T14:43:39.254879Z

yes, parens required when it's being used within a wider SQL query, optional at top-level

jarohen 2025-05-17T14:44:42.034139Z

this is mainly just following the syntax for SQL subqueries, I can see whether we can unambiguously remove them for XTQL specifically if that would make things easier

seancorfield 2025-05-17T14:45:29.299149Z

No, it's fine, I just wanted to figure out the implementation. Seems I guessed right 🙂

jarohen 2025-05-17T14:46:17.399849Z

also you may run into https://github.com/xtdb/xtdb/pull/4443 when you start testing with params - it's ugly, I'm ashamed of it, and any better suggestions welcome 😬

seancorfield 2025-05-17T14:51:38.888189Z

Nasty indeed. I'll have to give that some thought.

seancorfield 2025-05-17T14:53:10.969119Z

I created a HoneySQL issue to think about it...

seancorfield 2025-05-17T14:56:38.019569Z

I guess HoneySQL could look at the XTQL form and see (fn [a b c] ..) and add 3 ? parameters...

jarohen 2025-05-17T14:56:45.741229Z

fwiw in the Clojure API we've done a relatively simple parse of the XTQL and ... yep, that

seancorfield 2025-05-17T14:59:01.941469Z

One thing to be aware of here is how '#(...) behaves for that pr-str call:

{:from [[[:xtql '#(-> (from :my-table [x]) (where (<= x %))) :my_table]]]}
;;=>
["FROM (XTQL $$ (fn* [p1__32122#] (-> (from :my-table [x]) (where (<= x p1__32122#)))) $$) AS my_table"]

seancorfield 2025-05-17T14:59:38.678249Z

And I suspect you don't support fn* there? 🙂

seancorfield 2025-05-17T15:02:16.008869Z

How are the actual parameters passed in this case?

seancorfield 2025-05-17T15:02:59.332309Z

As in, how would HoneySQL know to lift values out and provide ? placeholders?

jarohen 2025-05-17T15:03:15.855019Z

yep, we deliberately do support fn* 🙂

jarohen 2025-05-17T15:04:10.605649Z

so we check first is fn or fn*, then count second

seancorfield 2025-05-17T15:04:17.661079Z

OK... so maybe [:xtsql '(fn [v] (-> (from :my_table [x]) (where (<= x v))) 42] as the HoneySQL format?

seancorfield 2025-05-17T15:04:53.849249Z

To produce ["XTQL ($$ .. $$, ?)" 42]

jarohen 2025-05-17T15:05:00.088359Z

that seems good, yeah - :xtql I guess?

seancorfield 2025-05-17T15:05:14.698949Z

Yeah... I have a hard time typing that correctly.

jarohen 2025-05-17T15:05:36.972629Z

ah, there's some good real user feedback 😄

seancorfield 2025-05-17T15:05:51.324599Z

I either type :xtdb or :xtsql 🙂

😄 1
seancorfield 2025-05-17T15:06:15.117859Z

Even in the tests I was just writing for HoneySQL, I repeatedly got it wrong and the tests failed 😞

seancorfield 2025-05-17T16:20:45.926999Z

Per https://github.com/xtdb/xtdb/pull/4443, parameters can be provided to :xtql after the XTQL form, which must either use (fn [..] ..) or the #(..) shorthand syntax to parameterize the query:

clojure
user=> (sql/format {:from [[[:xtql '(fn [v] (-> (from :my-table [x]) (where (<= x v)))) 42] :my_table]]})
["FROM (XTQL ($$ (fn [v] (-> (from :my-table [x]) (where (<= x v)))) $$, ?)) AS my_table" 42]
The shorthand form would be [:xtql '#(-> (from :my-table [x]) (where (<= x %)) 42].

jarohen 2025-05-17T17:43:03.877949Z

> which must either use the fn or #() syntax > this is the only way to do params in XTQL after beta8. tbh I'm hoping that it's only ever you and us that need to worry about the extra ?s 🙂 - anyone going through the xtdb-api should have it handled transparently on their behalf

seancorfield 2025-05-17T22:34:08.656489Z

FYI

jarohen 2025-05-18T06:22:27.455069Z

thanks ever so much Sean 🙏 gratitude

➕ 1