Skip to content

Commit

Permalink
add support for complex multiplicative expressions
Browse files Browse the repository at this point in the history
  • Loading branch information
dpetran committed May 31, 2024
1 parent e4f7c9c commit 7977d64
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 7 deletions.
30 changes: 26 additions & 4 deletions src/clj/fluree/db/query/sparql/translator.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -120,8 +120,22 @@
{:status 400 :error :db/invalid-query}))))

(defmethod parse-term :NumericLiteral
[[_ num-str]]
(read-string num-str))
;; NumericLiteral ::= NumericLiteralUnsigned WS | NumericLiteralPositive WS | NumericLiteralNegative WS
;; <NumericLiteralUnsigned> ::= INTEGER | DECIMAL | DOUBLE
;; <NumericLiteralPositive> ::= INTEGER_POSITIVE | DECIMAL_POSITIVE | DOUBLE_POSITIVE
;; <NumericLiteralNegative> ::= INTEGER_NEGATIVE | DECIMAL_NEGATIVE | DOUBLE_NEGATIVE
;; <INTEGER_POSITIVE> ::= '+' INTEGER
;; <DECIMAL_POSITIVE> ::= '+' DECIMAL
;; <DOUBLE_POSITIVE> ::= '+' DOUBLE
;; <INTEGER_NEGATIVE> ::= '-' INTEGER
;; <DECIMAL_NEGATIVE> ::= '-' DECIMAL
;; <DOUBLE_NEGATIVE> ::= '-' DOUBLE
;; <INTEGER> ::= #"[0-9]+"
;; <DECIMAL> ::= #"[0-9]*\.[0-9]*"
;; <DOUBLE> ::= #"[0-9]+\.[0-9]*|(\.[0-9]+)|([0-9]+)" EXPONENT
;; EXPONENT ::= #"[eE][+-]?[0-9]+"
[[_ sign num-str]]
(read-string (str sign num-str)))

(defmethod parse-term :MultiplicativeExpression
;; MultiplicativeExpression ::= UnaryExpression ( '*' UnaryExpression | '/' UnaryExpression )*
Expand All @@ -130,8 +144,16 @@
;; | '-' PrimaryExpression
;; | PrimaryExpression
;; <PrimaryExpression> ::= BrackettedExpression | BuiltInCall | iriOrFunction | RDFLiteral | NumericLiteral | BooleanLiteral | Var
[[_ & expression]]
(str/join " " (mapv parse-term expression)))
[[_ & [expr0 & [op1 expr1 & exprs]]]]
(if op1
;; we need to recursively compose expressions
(loop [[op expr & r] exprs
result (str "(" op1 " " (parse-term expr0) " " (parse-term expr1) ")")]
(if (and op expr)
(recur r (str "(" op " " result " " (parse-term expr) ")"))
result))
;; A single UnaryExpression doesn't need to be composed with anything else
(parse-term expr0)))

(defmethod parse-term :NumericExpression
;; NumericExpression ::= WS AdditiveExpression WS
Expand Down
10 changes: 7 additions & 3 deletions test/fluree/db/query/sparql_test.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -242,14 +242,18 @@
(is (= [[:bind "?handle" "dsanchez"]
{"@id" "?person", "person:handle" "?handle"}]
where)))
(let [query "SELECT ?person ?prefix ?foofix
(let [query "SELECT ?person ?prefix ?foofix ?num1
WHERE {BIND (SUBSTR(?handle, 4) AS ?prefix)
BIND (REPLACE(?prefix, \"abc\", \"FOO\") AS ?foofix)
?person person:handle ?handle.}"
BIND (?age*4*3/-2*(-4/2) AS ?num1)
?person person:handle ?handle.
?person person:age ?age}"
{:keys [where]} (sparql/->fql query)]
(is (= [[:bind "?prefix" "(subStr ?handle 4)"]
[:bind "?foofix" "(replace ?prefix \"abc\" \"FOO\")"]
{"@id" "?person", "person:handle" "?handle"}]
[:bind "?num1" "(* (/ (* (* ?age 4) 3) -2) (/ -4 2))"]
{"@id" "?person", "person:handle" "?handle"}
{"@id" "?person", "person:age" "?age"}]
where)))))

;;TODO: not yet supported
Expand Down

0 comments on commit 7977d64

Please sign in to comment.