From 0ce8f5a4fd47f5ab23cc241f07e31d33eeeafa4f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jens=20Pryce-=C3=85klundh?= <112686610+JPryce-Aklundh@users.noreply.github.com> Date: Mon, 10 Mar 2025 09:00:36 +0100 Subject: [PATCH 1/5] initial --- modules/ROOT/content-nav.adoc | 1 + modules/ROOT/pages/expressions/index.adoc | 1 + .../ROOT/pages/expressions/mathematical-expressions.adoc | 9 +++++++++ 3 files changed, 11 insertions(+) create mode 100644 modules/ROOT/pages/expressions/mathematical-expressions.adoc diff --git a/modules/ROOT/content-nav.adoc b/modules/ROOT/content-nav.adoc index 1a27ec07d..3594d56d3 100644 --- a/modules/ROOT/content-nav.adoc +++ b/modules/ROOT/content-nav.adoc @@ -72,6 +72,7 @@ *** xref:expressions/predicates/string-operators.adoc[] *** xref:expressions/predicates/path-pattern-expressions.adoc[] *** xref:expressions/predicates/type-predicate-expressions.adoc[] +** xref:expressions/mathematical-expressions.adoc[] ** xref:expressions/conditional-expressions.adoc[] * xref:functions/index.adoc[] diff --git a/modules/ROOT/pages/expressions/index.adoc b/modules/ROOT/pages/expressions/index.adoc index 1b1812a3a..b66d28fb7 100644 --- a/modules/ROOT/pages/expressions/index.adoc +++ b/modules/ROOT/pages/expressions/index.adoc @@ -11,4 +11,5 @@ For details and examples of specific expressions, see the following sections: ** xref:expressions/predicates/string-operators.adoc[]: `STARTS WITH`, `ENDS WITH`, `CONTAINS`, `IS NORMALIZED`, `IS NOT NORMALIZED`, `=~` ** xref:expressions/predicates/path-pattern-expressions.adoc[]: information about filtering queries with path pattern expressions. ** xref:expressions/predicates/type-predicate-expressions.adoc[]: information about how to verify the value type of a Cypher expression. +* xref:expressions/mathematical-expressions.adoc[]: `+`, `-`, `*`, `/`, `%`, `^`. * xref:expressions/conditional-expressions.adoc[] diff --git a/modules/ROOT/pages/expressions/mathematical-expressions.adoc b/modules/ROOT/pages/expressions/mathematical-expressions.adoc new file mode 100644 index 000000000..4e60336a4 --- /dev/null +++ b/modules/ROOT/pages/expressions/mathematical-expressions.adoc @@ -0,0 +1,9 @@ += Mathematical expressions + +Cypher contains a number of mathematical operators used for arithmetical calculations: + +* + + + + From 1db81f37a114ee2935a4b8ddb17d4246494d25c5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jens=20Pryce-=C3=85klundh?= <112686610+JPryce-Aklundh@users.noreply.github.com> Date: Mon, 10 Mar 2025 14:16:45 +0100 Subject: [PATCH 2/5] second --- modules/ROOT/content-nav.adoc | 2 +- modules/ROOT/pages/expressions/index.adoc | 2 +- .../expressions/mathematical-expressions.adoc | 9 - .../expressions/mathematical-operators.adoc | 224 ++++++++++++++++++ 4 files changed, 226 insertions(+), 11 deletions(-) delete mode 100644 modules/ROOT/pages/expressions/mathematical-expressions.adoc create mode 100644 modules/ROOT/pages/expressions/mathematical-operators.adoc diff --git a/modules/ROOT/content-nav.adoc b/modules/ROOT/content-nav.adoc index 3594d56d3..66de0ff85 100644 --- a/modules/ROOT/content-nav.adoc +++ b/modules/ROOT/content-nav.adoc @@ -72,7 +72,7 @@ *** xref:expressions/predicates/string-operators.adoc[] *** xref:expressions/predicates/path-pattern-expressions.adoc[] *** xref:expressions/predicates/type-predicate-expressions.adoc[] -** xref:expressions/mathematical-expressions.adoc[] +** xref:expressions/mathematical-operators.adoc[] ** xref:expressions/conditional-expressions.adoc[] * xref:functions/index.adoc[] diff --git a/modules/ROOT/pages/expressions/index.adoc b/modules/ROOT/pages/expressions/index.adoc index b66d28fb7..49bb55a52 100644 --- a/modules/ROOT/pages/expressions/index.adoc +++ b/modules/ROOT/pages/expressions/index.adoc @@ -11,5 +11,5 @@ For details and examples of specific expressions, see the following sections: ** xref:expressions/predicates/string-operators.adoc[]: `STARTS WITH`, `ENDS WITH`, `CONTAINS`, `IS NORMALIZED`, `IS NOT NORMALIZED`, `=~` ** xref:expressions/predicates/path-pattern-expressions.adoc[]: information about filtering queries with path pattern expressions. ** xref:expressions/predicates/type-predicate-expressions.adoc[]: information about how to verify the value type of a Cypher expression. -* xref:expressions/mathematical-expressions.adoc[]: `+`, `-`, `*`, `/`, `%`, `^`. +* xref:expressions/mathematical-operators.adoc[]: `+`, `-`, `*`, `/`, `%`, `^`. * xref:expressions/conditional-expressions.adoc[] diff --git a/modules/ROOT/pages/expressions/mathematical-expressions.adoc b/modules/ROOT/pages/expressions/mathematical-expressions.adoc deleted file mode 100644 index 4e60336a4..000000000 --- a/modules/ROOT/pages/expressions/mathematical-expressions.adoc +++ /dev/null @@ -1,9 +0,0 @@ -= Mathematical expressions - -Cypher contains a number of mathematical operators used for arithmetical calculations: - -* - - - - diff --git a/modules/ROOT/pages/expressions/mathematical-operators.adoc b/modules/ROOT/pages/expressions/mathematical-operators.adoc new file mode 100644 index 000000000..5e188dfba --- /dev/null +++ b/modules/ROOT/pages/expressions/mathematical-operators.adoc @@ -0,0 +1,224 @@ += Mathematical operators +:description: Information about Cypher's mathematical operators. +:table-caption!: + +Cypher contains the following mathematical operators: + +* Addition or unary addition: `+` +* Subtraction or unary minus: `-` +* Multiplication: `*` +* Division: `/` +* Modulo division: `%` +* Exponentiation: `^` + +For additional mathematical expressions, see: + +* xref:functions/mathematical-logarithmic.adoc[Logarithmic functions] +* xref:functions/mathematical-numeric.adoc[Numeric functions] +* xref:functions/mathematical-trigonometric.adoc[Trigonometric functions] + +[[examples]] +== Examples + +.Addition operator (`+`) +[source, cypher] +---- +RETURN 10 + 5 AS result +---- + +.Result +[role="queryresult",options="header,footer",cols="1* Date: Tue, 11 Mar 2025 14:28:31 +0100 Subject: [PATCH 3/5] third --- .../expressions/mathematical-operators.adoc | 70 +++++++++++++------ 1 file changed, 50 insertions(+), 20 deletions(-) diff --git a/modules/ROOT/pages/expressions/mathematical-operators.adoc b/modules/ROOT/pages/expressions/mathematical-operators.adoc index 5e188dfba..a9472f945 100644 --- a/modules/ROOT/pages/expressions/mathematical-operators.adoc +++ b/modules/ROOT/pages/expressions/mathematical-operators.adoc @@ -168,33 +168,33 @@ RETURN p.discountPrice AS discountPrice 1+d|Rows: 1 |=== + -[[precedence]] +[[order-of-precedence]] == Order of precedence -The following list details the order of precedence of the mathematical operators in ascending order: +The following table details the order of precedence of the mathematical operators in ascending order. +Note, the lower the precedence level, the higher the binding power of the operands. +Also note, if parentheses `()` are used, any operation within them takes precedence, overriding the default order of precedence. -.Mathematical operators order of precedence -[options="header"] +.Mathematical operators: order of precedence +[options="header"cols="a,2a,2a"] |=== - -| Precedence | Operators | Associativity - | Precedence | Operators | Associativity -| 1 | Unary negation (`-`), Unary positive (`+`) | Right to left +| 1 | Unary negation (`-`), unary positive (`+`) | Right to left | 2 | Exponentiation (`^`) | Right to left -| 3 | Multiplication (`*`), Division (`/`), Modulo (`%`) | Left to right -| 4 | Addition (`+`), Subtraction (`-`) | Left to right +| 3 | Multiplication (`*`), division (`/`), modulo division (`%`) | Left to right +| 4 | Addition (`+`), subtraction (`-`) | Left to right |=== Operators within the same precedence group are evaluated based on associativity. -.Combining mathematical operators +.Expression with several different mathematical operations [source, cypher] ---- -RETURN -50 + 6 * 3 - 100 / 5 ^ 2 % +12 AS result +RETURN (((-50) + (6 * 3)) - ((100 / (5 ^ 2)) % 12)) AS result ---- .Result @@ -202,23 +202,53 @@ RETURN -50 + 6 * 3 - 100 / 5 ^ 2 % +12 AS result |=== | result -| -35 +| -36 1+d|Rows: 1 |=== -.Order of Evaluation -[options="header"] +.Order of evaluation +[options="header", cols="a,2a,a"] |=== | Precedence | Operation | Result | 1 | Unary negation (`-50`) | `-50` -| 1 | Unary positive (`+12`) | `12` | 2 | Exponentiation (`5 ^ 2`) | `25` -| 3 | Division (`100 / 25`) | `4` | 3 | Multiplication (`6 * 3`) | `18` -| 3 | Multiplication (`4 * 12`) | `48` -| 3 | Modulo (`48 % 5`) | `3` +| 3 | Division (`100 / 25`) | `4` +| 3 | Modulo division (`4 % 12`) | `4` | 4 | Addition (`-50 + 18`) | `-32` -| 4 | Subtraction (`-32 - 3`) | `-35` +| 4 | Subtraction (`-32 - 4`) | `-36` + +|=== + +Only bracketing some of the operations within parentheses will change the order of precedence and may, therefore, change the result of an expression. + +.Parenthesizing single operation +[source, cypher] +---- +RETURN (-50 + 6) * 3 - 100 / 5 ^ 2 % 12 AS result +---- + +.Result +[role="queryresult",options="header,footer",cols="1* Date: Tue, 11 Mar 2025 14:41:57 +0100 Subject: [PATCH 4/5] remove mathematical operators from old operators page and fix links --- .../gql-conformance/supported-mandatory.adoc | 2 +- .../functions/mathematical-logarithmic.adoc | 2 +- .../pages/functions/mathematical-numeric.adoc | 2 +- .../functions/mathematical-trigonometric.adoc | 2 +- modules/ROOT/pages/syntax/operators.adoc | 54 ------------------- 5 files changed, 4 insertions(+), 58 deletions(-) diff --git a/modules/ROOT/pages/appendix/gql-conformance/supported-mandatory.adoc b/modules/ROOT/pages/appendix/gql-conformance/supported-mandatory.adoc index 7ecb42f80..fc2c528fd 100644 --- a/modules/ROOT/pages/appendix/gql-conformance/supported-mandatory.adoc +++ b/modules/ROOT/pages/appendix/gql-conformance/supported-mandatory.adoc @@ -185,7 +185,7 @@ For example, `RETURN sum()` on an empty table returns `NULL` in GQL, but i | 20.21 | -| xref:syntax/operators.adoc#query-operators-mathematical[Mathematical operators] +| xref:expressions/mathematical-operators.adoc[Mathematical operators] | | 20.22 diff --git a/modules/ROOT/pages/functions/mathematical-logarithmic.adoc b/modules/ROOT/pages/functions/mathematical-logarithmic.adoc index 278a8a2f3..412c69268 100644 --- a/modules/ROOT/pages/functions/mathematical-logarithmic.adoc +++ b/modules/ROOT/pages/functions/mathematical-logarithmic.adoc @@ -4,7 +4,7 @@ [[query-functions-logarithmic]] = Mathematical functions - logarithmic -Logarithmic mathematical functions operate on numeric expressions only, and will return an error if used on any other values. See also xref::syntax/operators.adoc#query-operators-mathematical[Mathematical operators]. +Logarithmic mathematical functions operate on numeric expressions only, and will return an error if used on any other values. See also xref:expressions/mathematical-operators.adoc[Mathematical operators]. [[functions-e]] diff --git a/modules/ROOT/pages/functions/mathematical-numeric.adoc b/modules/ROOT/pages/functions/mathematical-numeric.adoc index fb0ecabe7..3401c5c24 100644 --- a/modules/ROOT/pages/functions/mathematical-numeric.adoc +++ b/modules/ROOT/pages/functions/mathematical-numeric.adoc @@ -5,7 +5,7 @@ = Mathematical functions - numeric Numeric mathematical functions operate on numeric expressions only, and will return an error if used on any other values. -See also xref::syntax/operators.adoc#query-operators-mathematical[Mathematical operators]. +See also xref:expressions/mathematical-operators.adoc[Mathematical operators]. [[example-graph]] == Example graph diff --git a/modules/ROOT/pages/functions/mathematical-trigonometric.adoc b/modules/ROOT/pages/functions/mathematical-trigonometric.adoc index cd68ba4be..48582e7dd 100644 --- a/modules/ROOT/pages/functions/mathematical-trigonometric.adoc +++ b/modules/ROOT/pages/functions/mathematical-trigonometric.adoc @@ -4,7 +4,7 @@ [[query-functions-trigonometric]] = Mathematical functions - trigonometric -Trigonometric mathematical functions operate on numeric expressions only, and will return an error if used on any other values. See also xref::syntax/operators.adoc#query-operators-mathematical[Mathematical operators]. +Trigonometric mathematical functions operate on numeric expressions only, and will return an error if used on any other values. See also xref:expressions/mathematical-operators.adoc[Mathematical operators]. [[functions-acos]] == acos() diff --git a/modules/ROOT/pages/syntax/operators.adoc b/modules/ROOT/pages/syntax/operators.adoc index f350e7589..c5f6e9c77 100644 --- a/modules/ROOT/pages/syntax/operators.adoc +++ b/modules/ROOT/pages/syntax/operators.adoc @@ -12,7 +12,6 @@ This page contains an overview of the available Cypher operators. |=== | xref::syntax/operators.adoc#query-operators-aggregation[Aggregation operators] | `DISTINCT` | xref::syntax/operators.adoc#query-operators-property[Property operators] | `.` for static property access, `[]` for dynamic property access, `=` for replacing all properties, `+=` for mutating specific properties -| xref::syntax/operators.adoc#query-operators-mathematical[Mathematical operators] | `+`, `-`, `*`, `/`, `%`, `^` | xref::syntax/operators.adoc#query-operators-string[String operators] | `+` and `\|\|` (string concatenation), `IS NORMALIZED` | xref::syntax/operators.adoc#query-operators-temporal[Temporal operators] | `+` and `-` for operations between durations and temporal instants/durations, `*` and `/` for operations between durations and numbers | xref::syntax/operators.adoc#query-operators-map[Map operators] | `.` for static value access by key, `[]` for dynamic value access by key @@ -189,59 +188,6 @@ The properties on the node are updated as follows by those provided in the map: See xref::clauses/set.adoc#set-setting-properties-using-map[Mutate specific properties using a map and `+=`] for more details on using the property mutation operator `+=`. - -[[query-operators-mathematical]] -== Mathematical operators - -The mathematical operators comprise: - -* addition: `+` -* subtraction or unary minus: `-` -* multiplication: `*` -* division: `/` -* modulo division: `%` -* exponentiation: `^` - - -[[syntax-using-the-exponentiation-operator]] -=== Using the exponentiation operator `^` - -.Query -[source, cypher] ----- -WITH 2 AS number, 3 AS exponent -RETURN number ^ exponent AS result ----- - -.Result -[role="queryresult",options="header,footer",cols="1* Date: Tue, 11 Mar 2025 15:19:48 +0100 Subject: [PATCH 5/5] clarify --- .../pages/expressions/mathematical-operators.adoc | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/modules/ROOT/pages/expressions/mathematical-operators.adoc b/modules/ROOT/pages/expressions/mathematical-operators.adoc index a9472f945..d40f68a92 100644 --- a/modules/ROOT/pages/expressions/mathematical-operators.adoc +++ b/modules/ROOT/pages/expressions/mathematical-operators.adoc @@ -191,18 +191,21 @@ Also note, if parentheses `()` are used, any operation within them takes precede Operators within the same precedence group are evaluated based on associativity. +The order of precedence ensures that the following two expressions return the same result. + .Expression with several different mathematical operations [source, cypher] ---- -RETURN (((-50) + (6 * 3)) - ((100 / (5 ^ 2)) % 12)) AS result +RETURN -50 + 6 * 3 - 100 / 5 ^ 2 % 12 AS result1, + (((-50) + (6 * 3)) - ((100 / (5 ^ 2)) % 12)) AS result2 ---- .Result -[role="queryresult",options="header,footer",cols="1*