Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

New join api #180

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
new join api
myguidingstar committed Sep 11, 2021
commit 97308a7825333c0dcba104fb1b457d4fad20d7e4
2 changes: 1 addition & 1 deletion dev/resources/common_unpatched.edn
Original file line number Diff line number Diff line change
@@ -67,7 +67,7 @@
"INSERT INTO `article` (`id`, `title`, `current_revision`) VALUES (1, 'introduction', 2), (2, 'hello world', 1)" ]
:down ["DROP TABLE `article`"]}
[:duct.migrator.ragtime/sql :walkable-demo.migration.scenario-5/create-revision-table]
{:up ["CREATE TABLE `revision` (`id` SERIAL, `content` TEXT, `revision` INTEGER)"
{:up ["CREATE TABLE `revision` (`id` INTEGER, `content` TEXT, `revision` INTEGER)"
"INSERT INTO `revision` (`id`, `content`, `revision`) VALUES (1, 'tbd', 1), (1, 'this is sparta', 2), (2, 'welcome to my site', 1)" ]
:down ["DROP TABLE `revision`"]}
}
2 changes: 2 additions & 0 deletions dev/resources/config-mysql.edn
Original file line number Diff line number Diff line change
@@ -13,6 +13,8 @@
#ig/ref :walkable-demo.migration.scenario-3/create-person_pet-table
#ig/ref :walkable-demo.migration.scenario-4/create-human-table
#ig/ref :walkable-demo.migration.scenario-4/create-follow-table
#ig/ref :walkable-demo.migration.scenario-5/create-article-table
#ig/ref :walkable-demo.migration.scenario-5/create-revision-table
;; mysql-specific scenarios (starting from 100)
]}
}
2 changes: 2 additions & 0 deletions dev/resources/config-postgres.edn
Original file line number Diff line number Diff line change
@@ -26,6 +26,8 @@
#ig/ref :walkable-demo.migration.scenario-3/create-person_pet-table
#ig/ref :walkable-demo.migration.scenario-4/create-human-table
#ig/ref :walkable-demo.migration.scenario-4/create-follow-table
#ig/ref :walkable-demo.migration.scenario-5/create-article-table
#ig/ref :walkable-demo.migration.scenario-5/create-revision-table
;; postgres-specific scenarios (starting from 100)

#ig/ref :walkable-demo.migration.scenario-100/create-land-animal-table
2 changes: 2 additions & 0 deletions dev/resources/config-sqlite.edn
Original file line number Diff line number Diff line change
@@ -13,6 +13,8 @@
#ig/ref :walkable-demo.migration.scenario-3/create-person_pet-table
#ig/ref :walkable-demo.migration.scenario-4/create-human-table
#ig/ref :walkable-demo.migration.scenario-4/create-follow-table
#ig/ref :walkable-demo.migration.scenario-5/create-article-table
#ig/ref :walkable-demo.migration.scenario-5/create-revision-table
;; sqlite-specific scenarios (starting from 100)
]}
}
2 changes: 1 addition & 1 deletion dev/resources/core-mysql.edn
Original file line number Diff line number Diff line change
@@ -67,7 +67,7 @@
"INSERT INTO `article` (`id`, `title`, `current_revision`) VALUES (1, 'introduction', 2), (2, 'hello world', 1)" ]
:down ["DROP TABLE `article`"]}
[:duct.migrator.ragtime/sql :walkable-demo.migration.scenario-5/create-revision-table]
{:up ["CREATE TABLE `revision` (`id` SERIAL, `content` TEXT, `revision` INTEGER)"
{:up ["CREATE TABLE `revision` (`id` INTEGER, `content` TEXT, `revision` INTEGER)"
"INSERT INTO `revision` (`id`, `content`, `revision`) VALUES (1, 'tbd', 1), (1, 'this is sparta', 2), (2, 'welcome to my site', 1)" ]
:down ["DROP TABLE `revision`"]}
}
2 changes: 1 addition & 1 deletion dev/resources/core-postgres.edn
Original file line number Diff line number Diff line change
@@ -67,7 +67,7 @@
"INSERT INTO \"article\" (\"id\", \"title\", \"current_revision\") VALUES (1, 'introduction', 2), (2, 'hello world', 1)" ]
:down ["DROP TABLE \"article\""]}
[:duct.migrator.ragtime/sql :walkable-demo.migration.scenario-5/create-revision-table]
{:up ["CREATE TABLE \"revision\" (\"id\" SERIAL, \"content\" TEXT, \"revision\" INTEGER)"
{:up ["CREATE TABLE \"revision\" (\"id\" INTEGER, \"content\" TEXT, \"revision\" INTEGER)"
"INSERT INTO \"revision\" (\"id\", \"content\", \"revision\") VALUES (1, 'tbd', 1), (1, 'this is sparta', 2), (2, 'welcome to my site', 1)" ]
:down ["DROP TABLE \"revision\""]}
}
2 changes: 1 addition & 1 deletion dev/resources/core-sqlite.edn
Original file line number Diff line number Diff line change
@@ -67,7 +67,7 @@
"INSERT INTO `article` (`id`, `title`, `current_revision`) VALUES (1, 'introduction', 2), (2, 'hello world', 1)" ]
:down ["DROP TABLE `article`"]}
[:duct.migrator.ragtime/sql :walkable-demo.migration.scenario-5/create-revision-table]
{:up ["CREATE TABLE `revision` (`id` SERIAL, `content` TEXT, `revision` INTEGER)"
{:up ["CREATE TABLE `revision` (`id` INTEGER, `content` TEXT, `revision` INTEGER)"
"INSERT INTO `revision` (`id`, `content`, `revision`) VALUES (1, 'tbd', 1), (1, 'this is sparta', 2), (2, 'welcome to my site', 1)" ]
:down ["DROP TABLE `revision`"]}
}
55 changes: 43 additions & 12 deletions dev/test/walkable/integration_test/common.cljc
Original file line number Diff line number Diff line change
@@ -14,12 +14,12 @@
:output [:farmer/name :farmer/house-plus :farmer/house-count :farmer/house]}
{:key :farmer/house
:type :join
:join-path [:farmer/house-index :house/index]
:join-path [{:farmer/house-index :house/index}]
:output [:house/color]
:cardinality :one}
{:key :house/owner
:type :join
:join-path [:house/index :farmer/house-index]
:join-path [{:house/index :farmer/house-index}]
:output [:farmer/number]
:cardinality :one}])

@@ -30,12 +30,12 @@
:output [:kid/name]}
{:key :toy/owner
:type :join
:join-path [:toy/owner-number :kid/number]
:join-path [{:toy/owner-number :kid/number}]
:output [:kid/name :kid/number]
:cardinality :one}
{:key :kid/toy
:type :join
:join-path [:kid/number :toy/owner-number]
:join-path [{:kid/number :toy/owner-number}]
:output [:toy/color :toy/index]
:cardinality :one}
{:key :kid/number
@@ -50,7 +50,8 @@
:output [:human/number :human/name :human/yob :human/age]}
{:key :human/follow
:type :join
:join-path [:human/number :follow/human-1 :follow/human-2 :human/number]
:join-path [{:human/number :follow/human-1}
{:follow/human-2 :human/number}]
:output [:human/number :human/name :human/yob :human/age]}])

(def person-pet-registry
@@ -82,8 +83,8 @@
{:key :person/pet
:type :join
:join-path
[:person/number :person-pet/person-number
:person-pet/pet-index :pet/index]
[{:person/number :person-pet/person-number}
{:person-pet/pet-index :pet/index}]
:output [:pet/index
:person-pet/adoption-year
:pet/name
@@ -92,13 +93,14 @@
{:key :pet/owner
:type :join
:join-path
[:pet/index :person-pet/pet-index :person-pet/person-number :person/number]
[{:pet/index :person-pet/pet-index}
{:person-pet/person-number :person/number}]
:output [:person/number]}
{:key :person/pet-count
:type :join
:join-path
[:person/number :person-pet/person-number
:person-pet/pet-index :pet/index]
[{:person/number :person-pet/person-number}
{:person-pet/pet-index :pet/index}]
:aggregate true
:formula [:count-*]}
{:key :pets/by-color
@@ -111,6 +113,18 @@
:type :pseudo-column
:formula [:count :pet/index]}])

(def article-revision-registry
[{:key :articles/list
:type :root
:table "article"
:output [:article/id :article/title :article/current-revision]}
{:key :article/revision
:type :join
:cardinality :one
:output [:revision/content]
:join-path [{:article/id :revision/id
:article/current-revision :revision/revision}]}])

(def common-scenarios
{:farmer-house
{:registry farmer-house-registry
@@ -161,7 +175,7 @@
{:farmer/house [:house/index :house/color]}]}]
:expected
#:farmers{:farmers [#:farmer{:number 2, :house #:house {:index "20", :color "brown"}}]}}]}

:kid-toy
{:registry kid-toy-registry
:test-suite
@@ -418,4 +432,21 @@
:person/age]}]
:expected
#:people {:people [#:person{:number 1, :name "jon", :yob 1980, :age 38}
#:person{:number 2, :name "mary", :yob 1992, :age 26}]}}]}})
#:person{:number 2, :name "mary", :yob 1992, :age 26}]}}]}

:article-revision
{:registry article-revision-registry
:test-suite
[{:message "join with multiple column pairs should work"
:query `[{:articles/list [:article/id :article/title
{:article/revision [:revision/content]}]}]
:expected
#:articles{:list
[#:article{:id 1,
:title "introduction",
:revision
#:revision{:content "this is sparta"}}
#:article{:id 2,
:title "hello world",
:revision
#:revision{:content "welcome to my site"}}]}}]}})
55 changes: 25 additions & 30 deletions src/walkable/sql_query_builder/ast.cljc
Original file line number Diff line number Diff line change
@@ -10,13 +10,13 @@
[floor-plan {:keys [dispatch-key]}]
(get-in floor-plan [:target-table dispatch-key]))

(defn target-column
(defn target-columns
[floor-plan {:keys [dispatch-key]}]
(get-in floor-plan [:target-column dispatch-key]))
(get-in floor-plan [:target-columns dispatch-key]))

(defn source-column
(defn source-columns
[floor-plan {:keys [dispatch-key]}]
(get-in floor-plan [:source-column dispatch-key]))
(get-in floor-plan [:source-columns dispatch-key]))

(defn result-key [ast]
(let [k (:pathom/as (:params ast))]
@@ -138,17 +138,17 @@

(defn process-children*
"Infers which columns to include in SQL query from child keys in ast"
[floor-plan ast]
[floor-plan ast]
(let [all-children (:children ast)

{:keys [columns joins]}
(group-by #(keyword-type floor-plan %) all-children)

child-column-keys
(into #{} (map :dispatch-key) columns)

child-source-columns
(into #{} (map #(source-column floor-plan %)) joins)]
(into #{} (mapcat #(source-columns floor-plan %)) joins)]
{:columns-to-query (set/union child-column-keys child-source-columns)}))

(defn process-children
@@ -184,16 +184,16 @@
(defn individual-query-template
[{:keys [floor-plan ast pagination]}]
(let [ident? (vector? (:key ast))

{:keys [:columns-to-query]} (process-children floor-plan ast)
target-column (target-column floor-plan ast)

target-columns (target-columns floor-plan ast)

{:keys [:offset :limit :order-by :order-by-columns]}
(when-not ident? pagination)

columns-to-query
(-> (clojure.set/union columns-to-query order-by-columns)
(conj-some target-column))
(clojure.set/union columns-to-query order-by-columns (set target-columns))

selection
(process-selection floor-plan columns-to-query)
@@ -204,17 +204,17 @@

sql-query {:raw-string
(emitter/->query-string
{:target-table (target-table floor-plan ast)
:join-statement (join-statement floor-plan ast)
:selection (:raw-string selection)
:conditions (:raw-string conditions)
;; TODO:
;; use :raw-string/:params here in case there are variables in group-by columns
:group-by (compiled-group-by floor-plan ast)
:having (:raw-string having)
:offset offset
:limit limit
:order-by order-by})
{:target-table (target-table floor-plan ast)
:join-statement (join-statement floor-plan ast)
:selection (:raw-string selection)
:conditions (:raw-string conditions)
;; TODO:
;; use :raw-string/:params here in case there are variables in group-by columns
:group-by (compiled-group-by floor-plan ast)
:having (:raw-string having)
:offset offset
:limit limit
:order-by order-by})
:params (expressions/combine-params selection conditions having)}]
sql-query))

@@ -223,12 +223,7 @@
[shared-query batched-individuals]))

#_(defn combine-without-cte [{:keys [batched-individuals]}]
batched-individuals)

(defn source-column-variable-values
[v]
{:variable-values {`floor-plan/source-column-value
(expressions/compile-to-string {} v)}})
batched-individuals)

(defn compute-graphs [floor-plan env variables]
(let [variable->graph-index (variable->graph-index floor-plan)
@@ -327,8 +322,8 @@
(let [q (batched-individuals env entities)]
(when (not-empty (:raw-string q))
(->> q
(process-query floor-plan env)
eliminate-unknown-variables))))))
(process-query floor-plan env)
eliminate-unknown-variables))))))

(defn prepare-query
[floor-plan ast]
Loading