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

Fix null bind values with string vars #477

Merged
merged 4 commits into from
May 11, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
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
20 changes: 12 additions & 8 deletions src/fluree/db/query/exec/where.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -375,14 +375,18 @@
[_db solution pattern _ error-ch]
(let [bind (val pattern)]
(go
(reduce (fn [solution* b]
(let [f (::fn b)
var-name (::var b)]
(try*
(->> (f solution)
(add-fn-result-to-solution solution* var-name))
(catch* e (>! error-ch e)))))
solution (vals bind)))))
(let [result
(reduce (fn [solution* b]
(let [f (::fn b)
var-name (::var b)]
(try*
(->> (f solution)
(add-fn-result-to-solution solution* var-name))
(catch* e (update solution* ::errors conj e)))))
solution (vals bind))]
(when-let [errors (::errors result)]
(async/onto-chan! error-ch errors))
result))))

(def blank-solution {})

Expand Down
17 changes: 4 additions & 13 deletions src/fluree/db/query/fql/parse.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,6 @@

(defn parse-code
[x]
(log/debug "parse-code:" x)
(if (list? x)
x
(safe-read x)))
Expand All @@ -151,9 +150,7 @@
"Evals and returns bind function."
[var-name fn-code]
(let [code (parse-code fn-code)
_ (log/debug "parse-bind-function code:" code)
f (eval/compile code false)]
(log/debug "parse-bind-function f:" f)
(where/->function var-name f)))

(def ^:const default-recursion-depth 100)
Expand Down Expand Up @@ -304,7 +301,6 @@

(defmulti parse-pattern
(fn [pattern _vars _db _context]
(log/debug "parse-pattern pattern:" pattern)
(cond
(map? pattern) (->> pattern keys first)
(map-entry? pattern) :binding
Expand Down Expand Up @@ -336,20 +332,20 @@

(defn parse-bind-map
[bind]
(reduce (fn [m k] (update m k #(parse-bind-function k %)))
bind (keys bind)))
(reduce-kv (fn [m k v]
(let [parsed-k (parse-var-name k)]
(assoc m parsed-k (parse-bind-function parsed-k v))))
{} bind))

(defn parse-where-clause
[clause vars db context]
(let [patterns (->> clause
(remove filter-pattern?)
(log/debug->>val "patterns to parse:")
(mapv (fn [pattern]
(parse-pattern pattern vars db context))))
filters (->> clause
(filter filter-pattern?)
(parse-filter-maps vars))]
(log/debug "parse-where-clause patterns:" patterns)
(where/->where-clause patterns filters)))

(defn parse-triple
Expand All @@ -368,7 +364,6 @@

(defmethod parse-pattern :triple
[triple _ db context]
(log/debug "parse-triple:" triple)
(parse-triple triple db context))

(defmethod parse-pattern :union
Expand All @@ -389,14 +384,11 @@
(defmethod parse-pattern :bind
[{:keys [bind]} _vars _db _context]
(let [parsed (parse-bind-map bind)
_ (log/debug "parsed bind map:" parsed)
pattern (where/->pattern :bind parsed)]
(log/debug "parse-pattern :bind pattern:" pattern)
pattern))

(defmethod parse-pattern :binding
[[v f] _vars _db _context]
(log/debug "parse-pattern binding v:" v "- f:" f)
(where/->pattern :binding [v f]))

(defn parse-where
Expand Down Expand Up @@ -480,7 +472,6 @@
[q db]
(let [context (parse-context q db)
[vars values] (parse-values q)
_ (log/debug "parse-analytical-query*:" q)
where (parse-where q vars db context)
grouping (parse-grouping q)
ordering (parse-ordering q)]
Expand Down
59 changes: 29 additions & 30 deletions test/fluree/db/transact/update_test.clj
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
(ns fluree.db.transact.update-test
(:require [clojure.test :refer :all]
[fluree.db.test-utils :as test-utils]
[fluree.db.json-ld.api :as fluree]
[fluree.db.util.log :as log]))
[fluree.db.json-ld.api :as fluree]))

(deftest ^:integration deleting-data
(testing "Deletions of entire subjects."
Expand Down Expand Up @@ -91,33 +90,33 @@
["Bob" "Alice"])
"Only Bob and Alice should be left in the db.")

(testing "Updating property value only if it's current value is a match."
(is (= [{:id :ex/bob,
:rdf/type [:ex/User],
:schema/name "Bob"
:schema/age 23}]
@(fluree/query db-update-bob
'{:select {?s [:*]}
:where [[?s :id :ex/bob]]}))
"Bob's age should now be updated to 23 (from 22)."))
(testing "Updating property value only if it's current value is a match."
(is (= [{:id :ex/bob,
:rdf/type [:ex/User],
:schema/name "Bob"
:schema/age 23}]
@(fluree/query db-update-bob
'{:select {?s [:*]}
:where [[?s :id :ex/bob]]}))
"Bob's age should now be updated to 23 (from 22)."))

(testing "No update should happen if there is no match."
(is (= [{:id :ex/bob,
:rdf/type [:ex/User],
:schema/name "Bob"
:schema/age 22}]
@(fluree/query db-update-bob2
'{:select {?s [:*]}
:where [[?s :id :ex/bob]]}))
"Bob's age should have not been changed and still be 22."))
(testing "No update should happen if there is no match."
(is (= [{:id :ex/bob,
:rdf/type [:ex/User],
:schema/name "Bob"
:schema/age 22}]
@(fluree/query db-update-bob2
'{:select {?s [:*]}
:where [[?s :id :ex/bob]]}))
"Bob's age should have not been changed and still be 22."))

(testing "Replacing existing property value with new property value."
(is (= [{:id :ex/jane,
:rdf/type [:ex/User],
:schema/name "Jane"
:schema/email "[email protected]"
:schema/age 31}]
@(fluree/query db-update-jane
'{:select {?s [:*]}
:where [[?s :id :ex/jane]]}))
"Jane's age should now be updated to 31 (from 30).")))))
(testing "Replacing existing property value with new property value."
(is (= [{:id :ex/jane,
:rdf/type [:ex/User],
:schema/name "Jane"
:schema/email "[email protected]"
:schema/age 31}]
@(fluree/query db-update-jane
'{:select {?s [:*]}
:where [[?s :id :ex/jane]]}))
"Jane's age should now be updated to 31 (from 30).")))))