Skip to content

Commit

Permalink
feat: Add CONDITIONED BY * EXCEPT capability
Browse files Browse the repository at this point in the history
  • Loading branch information
KingMob committed Apr 15, 2024
1 parent b5e9461 commit f4abb7c
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 16 deletions.
7 changes: 5 additions & 2 deletions resources/inferenceql/query/base.bnf
Original file line number Diff line number Diff line change
Expand Up @@ -80,8 +80,8 @@ select-list ::= select-star-clause
/ selection (ws? ',' ws? selection)*
/ aggregation (ws? ',' ws? aggregation)*

select-star-clause ::= star (ws? select-except-clause)?
star ::= '*'
select-star-clause ::= star (ws? select-except-clause)?
select-except-clause ::= #'(?i)EXCEPT' ws? '(' ws? identifier-list ws? ')'

selection ::= (scalar-expr | aggregation) (ws alias-clause)?
Expand Down Expand Up @@ -225,7 +225,10 @@ density-event-and ::= density-event-1 (ws #'(?i)AND' ws density-event-1)+

density-event-group ::= '(' ws? density-event ws? ')'

conditioned-by-expr ::= model-expr ws #'(?i)CONDITIONED' ws #'(?i)BY' ws ('*' | density-event)
conditioned-by-expr ::= model-expr ws #'(?i)CONDITIONED' ws #'(?i)BY' ws (conditioned-by-star-clause | density-event)
<conditioned-by-star-clause> ::= star (ws? conditioned-by-except-clause)?
conditioned-by-except-clause ::= #'(?i)EXCEPT' ws? (<'('> ws? model-var-list ws? <')'> | model-var-list)


incorporate-expr ::= #'(?i)INCORPORATE' ws relation-expr ws #'(?i)INTO' ws model-expr

Expand Down
46 changes: 32 additions & 14 deletions src/inferenceql/query/scalar.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -56,15 +56,15 @@
[:distribution-event-and left _and right] [:and (plan left) (plan right)]

[:distribution-event-binop (variable :guard (tree/tag-pred :variable)) [:binop s] (scalar :guard (tree/tag-pred :scalar-expr))] [(keyword s) (plan variable) (plan scalar)]
[:distribution-event-binop (scalar :guard (tree/tag-pred :scalar-expr)) [:binop s] (variable :guard (tree/tag-pred :variable))] [(keyword s) (plan variable) (plan scalar)]
[:distribution-event-binop (scalar :guard (tree/tag-pred :scalar-expr)) [:binop s] (variable :guard (tree/tag-pred :variable))] [(keyword s) (plan variable) (plan scalar)]

Check warning on line 59 in src/inferenceql/query/scalar.cljc

View check run for this annotation

Codecov / codecov/patch

src/inferenceql/query/scalar.cljc#L59

Added line #L59 was not covered by tests

[:distribution-event-group "(" child ")"] (plan child)

[:density-event child] (plan child)
[:density-event-and & children] (into {} (comp (filter tree/branch?) (map plan)) children)

[:density-event-eq (variable :guard (tree/tag-pred :variable)) _= (scalar :guard (tree/tag-pred :scalar-expr))] {(plan variable) (plan scalar)}
[:density-event-eq (scalar :guard (tree/tag-pred :scalar-expr)) _= (variable :guard (tree/tag-pred :variable))] {(plan variable) (plan scalar)}
[:density-event-eq (scalar :guard (tree/tag-pred :scalar-expr)) _= (variable :guard (tree/tag-pred :variable))] {(plan variable) (plan scalar)}

Check warning on line 67 in src/inferenceql/query/scalar.cljc

View check run for this annotation

Codecov / codecov/patch

src/inferenceql/query/scalar.cljc#L67

Added line #L67 was not covered by tests

[:density-event-group "(" child ")"] (plan child)

Expand All @@ -81,14 +81,19 @@
(let [query-plan (requiring-resolve 'inferenceql.query.plan/plan)]
`(~'iql/eval-relation-plan (~'quote ~(query-plan relation))))])

[:conditioned-by-expr model _conditioned _by "*"] `(~'iql/condition-all ~(plan model))
[:conditioned-by-expr model _conditioned _by event] `(~'iql/condition ~(plan model) ~(plan event))
[:conditioned-by-expr model _conditioned _by [:star _]] `(~'iql/condition-all ~(plan model))
[:conditioned-by-expr model _conditioned _by [:star _] [:conditioned-by-except-clause & except-children]]
`(~'iql/condition-all-except ~(plan model) ~(plan (into [:conditioned-by-except-clause] except-children)))
[:conditioned-by-expr model _conditioned _by child] `(~'iql/condition ~(plan model) ~(plan child))
[:conditioned-by-except-clause _except model-var-list] (plan model-var-list)

[:constrained-by-expr model _constrained _by event] `(~'iql/constrain ~(plan model) ~(plan event))


[:value child] (literal/read child)

[:variable _var child] (id-node->str child)
[:variable-list & variables] (map plan variables)
[:variable-list & variables] (into [] (comp (filter tree/branch?) (map plan)) variables) ; remove commas

[:identifier child] (plan child)
[:delimited-symbol s] (list 'iql/safe-get 'iql-bindings s)
Expand Down Expand Up @@ -138,14 +143,26 @@
(gpm/condition conditions))))))

(defn condition-all
[model bindings]
(let [conditions (reduce (fn [conditions variable]
(cond-> conditions
(contains? bindings variable)
(assoc variable (get bindings variable))))
{}
(map str (gpm/variables model)))]
(condition model conditions)))
"Retrieves all variables from the model and conditions them based on the
value found in the bindings, which includes the current tuple/row.
The 3-arity version takes an additional coll of vars to exclude."
([model bindings]
(condition-all model #{} bindings))
([model exclusions bindings]
(let [exclusions (set exclusions)
condition-vars (into []
(comp
(map name)
(filter (complement exclusions)))
(gpm/variables model))
conditions (reduce (fn [conditions variable]
(cond-> conditions
(contains? bindings variable)
(assoc variable (get bindings variable))))
{}
condition-vars)]
(condition model conditions))))

(defn operation?
"Given an event form, returns `true` if the form is an operation."
Expand Down Expand Up @@ -273,7 +290,8 @@
#?@(:clj ['eval-relation-plan
(let [eval (requiring-resolve 'inferenceql.query.plan/eval)]
#(generative-table/generative-table (eval % env bindings)))])
#?@(:clj ['condition-all #(condition-all % bindings)])
#?@(:clj ['condition-all #(condition-all % bindings)
'condition-all-except #(condition-all %1 %2 bindings)])
'condition condition
'constrain constrain
'mutual-info mutual-info
Expand Down

0 comments on commit f4abb7c

Please sign in to comment.