Skip to content

Commit

Permalink
new join api
Browse files Browse the repository at this point in the history
  • Loading branch information
myguidingstar committed Sep 11, 2021
1 parent 02d3ea1 commit 6aca382
Show file tree
Hide file tree
Showing 11 changed files with 261 additions and 186 deletions.
2 changes: 1 addition & 1 deletion dev/resources/common_unpatched.edn
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Up @@ -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)
]}
}
Expand Down
2 changes: 2 additions & 0 deletions dev/resources/config-postgres.edn
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions dev/resources/config-sqlite.edn
Original file line number Diff line number Diff line change
Expand Up @@ -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)
]}
}
Expand Down
2 changes: 1 addition & 1 deletion dev/resources/core-mysql.edn
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Up @@ -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
Expand Up @@ -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
Expand Up @@ -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}])

Expand All @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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"}}]}}]}})
56 changes: 26 additions & 30 deletions src/walkable/sql_query_builder/ast.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -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))]
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -184,37 +184,38 @@
(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)

;; TODO: add row constructor for join filters here
conditions (all-filters floor-plan ast)

having (compiled-having floor-plan ast)

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))

Expand All @@ -223,12 +224,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)
Expand Down Expand Up @@ -327,8 +323,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]
Expand Down
Loading

0 comments on commit 6aca382

Please sign in to comment.