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

Support GENERATE * EXCEPT functionality #99

Merged
merged 2 commits into from
Apr 15, 2024
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
4 changes: 4 additions & 0 deletions resources/inferenceql/query/base.bnf
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,10 @@ join-expr-group ::= '(' join-expr ')'
(* generate-expr *)

generate-expr ::= #'(?i)GENERATE' ws generate-list ws #'(?i)UNDER' ws model-expr
<generate-list> ::= generate-star-clause
/ model-var-list
generate-star-clause ::= star (ws? generate-except-clause)?
generate-except-clause ::= #'(?i)EXCEPT' ws? '(' ws? model-var-list ws? ')'

(* generative-join-expr *)

Expand Down
8 changes: 5 additions & 3 deletions resources/inferenceql/query/permissive.bnf
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,6 @@ probability-expr ::= #'(?i)PROBABILITY' ws #'(?i)OF'
ws permissive-event-list
ws #'(?i)UNDER' ws model-expr

(* generate-expr *)

<generate-list> ::= star / identifier-list

(* mutual-information-expr *)

Expand All @@ -57,3 +54,8 @@ mutual-info-expr ::= #'(?i)MUTUAL' ws #'(?i)INFORMATION'
ws #'(?i)OF' ws distribution-event
ws #'(?i)WITH' ws distribution-event
ws #'(?i)UNDER' ws model-expr

(* model var abstraction *)

<model-var> ::= identifier
<model-var-list> ::= identifier-list
9 changes: 5 additions & 4 deletions resources/inferenceql/query/strict.bnf
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,6 @@ density-expr ::= #'(?i)PROBABILITY' ws #'(?i)DENSITY' ws #'(?i)OF'
ws density-event
ws #'(?i)UNDER' ws model-expr

(* generate-expr *)

<generate-list> ::= star / variable-list

(* mutual-information-expr *)

mutual-info-expr ::= #'(?i)MUTUAL' ws #'(?i)INFORMATION'
Expand All @@ -47,3 +43,8 @@ approx-mutual-info-expr ::= #'(?i)APPROXIMATE' ws #'(?i)MUTUAL' ws #'(?i)INFORMA
ws #'(?i)OF' ws variable-list
ws #'(?i)WITH' ws variable-list
ws #'(?i)UNDER' ws model-expr

(* model var list *)

<model-var> ::= variable
<model-var-list> ::= variable-list
17 changes: 9 additions & 8 deletions src/inferenceql/query/literal.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,15 @@
node)]
[[:value child]] (read child)

[[:bool s]] (edn/read-string s)
[[:float s]] (edn/read-string s)
[[:int s]] (edn/read-string s)
[[:nat s]] (edn/read-string s)
[[:identifier child]] (read child)
[[:simple-symbol s]] (edn/read-string (str \" s \"))
[[:delimited-symbol s]] (edn/read-string (str \" s \"))
[[:string s]] (edn/read-string (str \" s \"))
[[:bool s]] (edn/read-string s)
[[:float s]] (edn/read-string s)
[[:int s]] (edn/read-string s)
[[:nat s]] (edn/read-string s)
[[:identifier child]] (read child)
[[:variable _var child]] (read child)

Check warning on line 22 in src/inferenceql/query/literal.cljc

View check run for this annotation

Codecov / codecov/patch

src/inferenceql/query/literal.cljc#L22

Added line #L22 was not covered by tests
[[:simple-symbol s]] (edn/read-string (str \" s \"))
[[:delimited-symbol s]] (edn/read-string (str \" s \"))
[[:string s]] (edn/read-string (str \" s \"))

[[:null _]] nil
[nil] nil
Expand Down
1 change: 1 addition & 0 deletions src/inferenceql/query/parser/tree.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,7 @@
(walk/postwalk remove-ws node)))

(defmacro match
"Like core.match/match, but removes whitespace nodes before matching."
[vars & clauses]
(let [match (macrovich/case :clj 'clojure.core.match/match
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think that the cljs compiler handles this resolution on its own. If you have clojure.blah and that's not found, cljs will try to find cljs.blah. That's why (:require [clojure.string :as ]) works in cljs, for example.

Copy link
Contributor Author

@KingMob KingMob Apr 11, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've never seen macrovich before, so I looked up what it's doing.

Apparently, the intent ofmacrovich/case is for Clojurescript macros. Since Clojurescript macros are technically Clojure code, reader conditionals in them will pick the wrong environment, but not this, somehow.

I don't know what was happening before, but the commit message for the change, bdefd36, says fix(cljs): Get ClojureScript working again, and the diff shows it was relying on cljs's backup loading scheme. Maybe that wasn't working properly, since it was inside macro? I don't know, but Zane might.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Okay! And I know I'm commenting outside of this PR... @zane do you remember what the error was that this addresses?

:cljs 'cljs.core.match/match)]
Expand Down
3 changes: 3 additions & 0 deletions src/inferenceql/query/permissive.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,9 @@
nodes)]
[:generate-expr generate ws variable-list ws under ws model])

[[:generate-except-clause except "(" [:identifier-list & nodes] ")"]]
[:generate-except-clause except ws "(" (identifier-list->variable-list (into [:identifier-list] nodes)) ")"]

[[:density-event-list & _]]
(and-node :density-event-and
(map tree/only-child
Expand Down
52 changes: 34 additions & 18 deletions src/inferenceql/query/plan.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -82,9 +82,13 @@
::plan op}))

(defn generate
[variables sexpr]
"Generate tuples from a model.

`mode` can be :include or :exclude"
[variables sexpr mode]
{::type :inferenceql.query.plan.type/generate
::sexpr sexpr
::mode mode
::variables variables})

(defn limit
Expand Down Expand Up @@ -223,22 +227,26 @@
(let [id (literal/read node)]
(lookup id)))

(defn variable-node->symbol
(defn variable-node->string
"Extracts the identifier name from a variable node."
[node]
(-> node tree/only-child-node literal/read ))
(-> node tree/only-child-node literal/read))

(defmethod plan-impl :generate-expr
[node]
(tree/match [node]
[[:generate-expr _generate generate-list _under model-expr]]
(let [sexpr (scalar/plan model-expr)
variables (case (tree/tag generate-list)
:star
'*
(let [sexpr (scalar/plan model-expr)]
(tree/match [generate-list]
[[:generate-star-clause [:star & _]]]
(generate :* sexpr :include)

[[:generate-star-clause [:star & _]
[:generate-except-clause _except _lparen (:or [:variable-list & variables] [:identifier-list & variables]) _rparen]]]
(generate (map variable-node->string (filter tree/branch? variables)) sexpr :exclude)

:variable-list
(map variable-node->symbol (tree/child-nodes generate-list)))]
(generate variables sexpr))))
[[:variable-list & variables]]
(generate (map variable-node->string (filter tree/branch? variables)) sexpr :include)))))

(defmethod plan-impl :where-clause
[node op]
Expand Down Expand Up @@ -574,15 +582,23 @@

(defmethod eval :inferenceql.query.plan.type/generate
[plan env bindings]
(let [{::keys [sexpr variables]} plan
(let [{::keys [sexpr variables mode]} plan
model (scalar/eval sexpr env bindings)
variables (->> (if (= '* variables)
(gpm/variables model)
variables)
(map str))
samples (map #(update-keys % str)
(repeatedly #(gpm/simulate model variables {})))]
(relation/relation samples :attrs variables)))
model-vars (gpm/variables model)
;; handle models with non-str vars
str-vars->model-vars (zipmap (map name model-vars) model-vars)
->model-var (fn [k]
(or (str-vars->model-vars k)
(throw (ex-info (str "Could not find model var: " (pr-str k))
{:requested-var-name k
:known-model-vars model-vars}))))

Check warning on line 594 in src/inferenceql/query/plan.cljc

View check run for this annotation

Codecov / codecov/patch

src/inferenceql/query/plan.cljc#L592-L594

Added lines #L592 - L594 were not covered by tests
sim-vars (match/match [mode variables]
[:include :*] model-vars
[:include _] (map ->model-var variables)
[:exclude _] (remove (set (map ->model-var variables)) model-vars))
samples (map #(update-keys % name)
(repeatedly #(gpm/simulate model sim-vars {})))]
(relation/relation samples :attrs (map name sim-vars))))

(defmethod eval :inferenceql.query.plan.type/insert
[plan env bindings]
Expand Down
3 changes: 3 additions & 0 deletions test/inferenceql/query/base/parser_test.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -107,3 +107,6 @@

(deftest parse-generative-join-success
(parses "table GENERATIVE JOIN model" :generative-join-expr))

(deftest parse-generate-star
(parses "GENERATE * UNDER model" :generate-expr))
6 changes: 6 additions & 0 deletions test/inferenceql/query/permissive/parser_test.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -35,3 +35,9 @@
"data GENERATIVE JOIN model CONDITIONED BY x = 0"
"data GENERATIVE JOIN model CONSTRAINED BY x > 0"
"data GENERATIVE JOIN model GIVEN x = 0"))

(deftest generate-valid
(are [s] (not (insta/failure? (parser/parse s :start :generate-expr)))
"GENERATE * UNDER model"
"GENERATE foo, bar UNDER model"
"GENERATE * EXCEPT (foo, bar) UNDER model"))
5 changes: 4 additions & 1 deletion test/inferenceql/query/permissive_test.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,10 @@
"GENERATE VAR x, VAR y UNDER model"

"GENERATE x, y, z UNDER model"
"GENERATE VAR x, VAR y, VAR z UNDER model"))
"GENERATE VAR x, VAR y, VAR z UNDER model"

"GENERATE * EXCEPT (x, y) UNDER model"
"GENERATE * EXCEPT (VAR x, VAR y) UNDER model"))

(deftest approximate-mutual-info
(are [permissive strict] (= (strict.parser/parse strict :start :scalar-expr)
Expand Down
3 changes: 2 additions & 1 deletion test/inferenceql/query/plan_test.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -232,7 +232,8 @@
"GENERATE VAR x UNDER model" ["x"]
"GENERATE VAR x, VAR y UNDER model" ["x" "y"]
"GENERATE VAR y, VAR x UNDER model" ["y" "x"]
"GENERATE VAR x, VAR y, VAR z UNDER model" ["x" "y" "z"]))
"GENERATE * EXCEPT (VAR x) UNDER model" ["y"]
"GENERATE * EXCEPT (VAR x, VAR y) UNDER model" nil))
(testing "values"
(let [rel (eval "GENERATE VAR x UNDER model" {"model" model})]
(doseq [tup (relation/tuples (take 1 #_ 5 rel))]
Expand Down
6 changes: 6 additions & 0 deletions test/inferenceql/query/strict/parser_test.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,12 @@
"data GENERATIVE JOIN model CONDITIONED BY VAR x = 0"
"data GENERATIVE JOIN model CONSTRAINED BY VAR x > 0"))

(deftest generate-valid
(are [s] (not (insta/failure? (parser/parse s :start :generate-expr)))
"GENERATE * UNDER model"
"GENERATE VAR foo, VAR bar UNDER model"
"GENERATE * EXCEPT (VAR foo, VAR bar) UNDER model"))

(deftest conditioned-by-valid
(are [s] (not (insta/failure? (parser/parse s)))
"SELECT * FROM (GENERATE * UNDER model CONDITIONED BY VAR x = x)"
Expand Down
7 changes: 5 additions & 2 deletions test/inferenceql/query/strict_test.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -286,14 +286,17 @@

;;; Generate

#_

(deftest generate-generates-correct-columns
(testing "generate"
(testing "GENERATE"
(let [model simple-model
q #(q % [] {"model" model})]
(testing "with star "
(doseq [result (q "SELECT * FROM (GENERATE * UNDER model) LIMIT 10")]
(is (= #{"x" "y"} (set (keys result))))))
(testing "with star except"
(doseq [result (q "SELECT * FROM (GENERATE * EXCEPT (VAR x) UNDER model) LIMIT 10")]
(is (= #{"y"} (set (keys result))))))
(testing "with a single variable"
(doseq [result (q "SELECT * FROM (GENERATE VAR y UNDER model) LIMIT 10")]
(is (= #{"y"} (set (keys result))))))
Expand Down
Loading