From 7a24fd036751064342414008b86c04f64b1b083d Mon Sep 17 00:00:00 2001 From: Sean Corfield Date: Thu, 12 Dec 2024 11:07:30 -0800 Subject: [PATCH] incorporate feedback from @jarohen Signed-off-by: Sean Corfield --- doc/xtdb.md | 23 ++++++++++++++++++++--- test/honey/sql_test.cljc | 8 ++++---- 2 files changed, 24 insertions(+), 7 deletions(-) diff --git a/doc/xtdb.md b/doc/xtdb.md index 384eac2..3f11274 100644 --- a/doc/xtdb.md +++ b/doc/xtdb.md @@ -46,7 +46,7 @@ user=> (sql/format '{from foo where (= status "active")}) You can also `SELECT *` and then exclude columns and/or rename columns. ```clojure -user=> (sql/format '{select ((* {exclude _id rename ((title, name))}))}) +user=> (sql/format {:select [[:* {:exclude :_id :rename [[:title, :name]]}]]}) ["SELECT * EXCLUDE _id RENAME title AS name"] user=> (sql/format '{select ((a.* {exclude _id}) (b.* {rename ((title, name))})) @@ -55,6 +55,16 @@ user=> (sql/format '{select ((a.* {exclude _id}) ["SELECT a.* EXCLUDE _id, b.* RENAME title AS name FROM foo AS a INNER JOIN bar AS b ON a._id = b.foo_id"] ``` +`:exclude` can accept a single column, or a sequence of columns. +`:rename` accepts a sequence of pairs (column name, new name). + +```clojure +user=> (sql/format {:select [[:* {:exclude [:_id :upc] + :rename [[:title, :name] + [:price, :cost]]}]]}) +["SELECT * EXCLUDE (_id, upc) RENAME (title AS name, price AS cost)"] +``` + ## Nested Sub-Queries XTDB can produce structured results from `SELECT` queries containing @@ -103,6 +113,13 @@ user=> (sql/format {:select [[[:record {:_id 1 :status "active"}]]]}) ["SELECT RECORD (_id: 1, status: 'active')"] ``` +A third option is to use `:inline` with a hash map: + +```clojure +user=> (sql/format {:select [[[:inline {:_id 1 :status "active"}]]]}) +["SELECT {_id: 1, status: 'active'}"] +``` + ## Object Navigation Expressions In order to deal with nested documents, XTDB provides syntax to navigate @@ -144,9 +161,9 @@ See [XTDB's Top-level queries documentation](https://docs.xtdb.com/reference/mai Here's one fairly complex example: ```clojure -user=> (sql/format {:setting [[:basis-to [:inline :DATE "2024-11-24"]] +user=> (sql/format {:setting [[:snapshot-time :to [:inline :DATE "2024-11-24"]] [:default :valid-time :to :between [:inline :DATE "2022"] :and [:inline :DATE "2023"]]]}) -["SETTING BASIS TO DATE '2024-11-24', DEFAULT VALID_TIME TO BETWEEN DATE '2022' AND DATE '2023'"] +["SETTING SNAPSHOT_TIME TO DATE '2024-11-24', DEFAULT VALID_TIME TO BETWEEN DATE '2022' AND DATE '2023'"] ``` Table references (e.g., in a `FROM` clause) can also have temporal qualifiers. diff --git a/test/honey/sql_test.cljc b/test/honey/sql_test.cljc index 575e7a2..40a2fc0 100644 --- a/test/honey/sql_test.cljc +++ b/test/honey/sql_test.cljc @@ -1403,15 +1403,15 @@ ORDER BY id = ? DESC (testing "setting default time" (is (= ["SETTING DEFAULT SYSTEM_TIME AS OF DATE '2024-11-24'"] (sut/format {:setting [:default :system-time :as-of [:inline :DATE "2024-11-24"]]}))) - (is (= ["SETTING BASIS TO DATE '2024-11-24', DEFAULT VALID_TIME TO BETWEEN DATE '2022' AND DATE '2023'"] - (sut/format {:setting [[:basis-to [:inline :DATE "2024-11-24"]] + (is (= ["SETTING SNAPSHOT_TIME TO DATE '2024-11-24', DEFAULT VALID_TIME TO BETWEEN DATE '2022' AND DATE '2023'"] + (sut/format {:setting [[:snapshot-time :to [:inline :DATE "2024-11-24"]] [:default :valid-time :to :between [:inline :DATE "2022"] :and [:inline :DATE "2023"]]]}))) (is (= ["SETTING DEFAULT SYSTEM_TIME AS OF DATE '2024-11-24' SELECT * FROM table"] (sut/format (-> (h/setting :default :system-time :as-of [:inline :DATE "2024-11-24"]) (h/select :*) (h/from :table))))) - (is (= ["SETTING BASIS TO DATE '2024-11-24', DEFAULT VALID_TIME TO BETWEEN DATE '2022' AND DATE '2023' SELECT * FROM table"] - (sut/format (-> (h/setting [:basis-to [:inline :DATE "2024-11-24"]] + (is (= ["SETTING SNAPSHOT_TIME TO DATE '2024-11-24', DEFAULT VALID_TIME TO BETWEEN DATE '2022' AND DATE '2023' SELECT * FROM table"] + (sut/format (-> (h/setting [:snapshot-time :to [:inline :DATE "2024-11-24"]] [:default :valid-time :to :between [:inline :DATE "2022"] :and [:inline :DATE "2023"]]) (h/select :*) (h/from :table)))))))