-
Notifications
You must be signed in to change notification settings - Fork 885
JAVA-2555: Generate append/prepend constructs compatible with legacy C* versions #1379
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
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
a9f5f9a
JAVA-2555: Generate append/prepend constructs compatible with legacy …
1d4fa5f
JAVA-2596: Consider collection removals as idempotent in query builder
88a9031
[feedback JAVA-2596] Fix wrong example
5290e35
[feedback JAVA-2596] Clarify when elements are idempotent
c4bcd03
Fix examples to use SimpleStatement instead of Statement
40bcff3
Rephrase method javadocs
6c94a8d
Merge branch '4.x' into java2555
adutra File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -39,15 +39,15 @@ If you use the result of a user-defined function in an INSERT or UPDATE statemen | |
| of knowing if that function is idempotent: | ||
|
|
||
| ```java | ||
| Statement statement = insertInto("foo").value("k", function("generate_id")).build(); | ||
| SimpleStatement statement = insertInto("foo").value("k", function("generate_id")).build(); | ||
| // INSERT INTO foo (k) VALUES (generate_id()) | ||
| assert !statement.isIdempotent(); | ||
| ``` | ||
|
|
||
| This extends to arithmetic operations using such terms: | ||
|
|
||
| ```java | ||
| Statement statement = | ||
| SimpleStatement statement = | ||
| insertInto("foo").value("k", add(function("generate_id"), literal(1))).build(); | ||
| // INSERT INTO foo (k) VALUES (generate_id()+1) | ||
| assert !statement.isIdempotent(); | ||
|
|
@@ -56,7 +56,7 @@ assert !statement.isIdempotent(); | |
| Raw terms could be anything, so they are also considered unsafe by default: | ||
|
|
||
| ```java | ||
| Statement statement = | ||
| SimpleStatement statement = | ||
| insertInto("foo").value("k", raw("generate_id()+1")).build(); | ||
| // INSERT INTO foo (k) VALUES (generate_id()+1) | ||
| assert !statement.isIdempotent(); | ||
|
|
@@ -68,7 +68,7 @@ If a WHERE clause in an UPDATE or DELETE statement uses a comparison with an uns | |
| potentially apply to different rows for each execution: | ||
|
|
||
| ```java | ||
| Statement statement = | ||
| SimpleStatement statement = | ||
| update("foo") | ||
| .setColumn("v", bindMarker()) | ||
| .whereColumn("k").isEqualTo(function("non_idempotent_func")) | ||
|
|
@@ -82,7 +82,7 @@ assert !statement.isIdempotent(); | |
| Counter updates are never idempotent: | ||
|
|
||
| ```java | ||
| Statement statement = | ||
| SimpleStatement statement = | ||
| update("foo") | ||
| .increment("c") | ||
| .whereColumn("k").isEqualTo(bindMarker()) | ||
|
|
@@ -94,20 +94,20 @@ assert !statement.isIdempotent(); | |
| Nor is appending or prepending an element to a list: | ||
|
|
||
| ```java | ||
| Statement statement = | ||
| SimpleStatement statement = | ||
| update("foo") | ||
| .appendListElement("l", literal(1)) | ||
| .whereColumn("k").isEqualTo(bindMarker()) | ||
| .build(); | ||
| // UPDATE foo SET l+=[1] WHERE k=? | ||
| // UPDATE foo SET l=l+[1] WHERE k=? | ||
| assert !statement.isIdempotent(); | ||
| ``` | ||
|
|
||
| The generic `append` and `prepend` methods apply to any kind of collection, so we have to consider | ||
| them unsafe by default too: | ||
|
|
||
| ```java | ||
| Statement statement = | ||
| SimpleStatement statement = | ||
| update("foo") | ||
| .prepend("l", literal(Arrays.asList(1, 2, 3))) | ||
| .whereColumn("k").isEqualTo(bindMarker()) | ||
|
|
@@ -116,12 +116,66 @@ Statement statement = | |
| assert !statement.isIdempotent(); | ||
| ``` | ||
|
|
||
| The generic `remove` method is however safe since collection removals are idempotent: | ||
|
|
||
| ```java | ||
| SimpleStatement statement = | ||
| update("foo") | ||
| .remove("l", literal(Arrays.asList(1, 2, 3))) | ||
| .whereColumn("k").isEqualTo(bindMarker()) | ||
| .build(); | ||
| // UPDATE foo SET l=l-[1,2,3] WHERE k=? | ||
| assert statement.isIdempotent(); | ||
| ``` | ||
|
|
||
| When appending, prepending or removing a single element to/from a collection, it is possible to use | ||
| the dedicated methods listed below; their idempotence depends on the collection type (list, set or | ||
| map), the operation (append, prepend or removal) and the idempotence of the element being | ||
| added/removed: | ||
|
adutra marked this conversation as resolved.
|
||
|
|
||
| 1. `appendListElement` : not idempotent | ||
| 2. `prependListElement` : not idempotent | ||
| 3. `removeListElement` : idempotent if element is idempotent | ||
| 4. `appendSetElement` : idempotent if element is idempotent | ||
| 5. `prependSetElement` : idempotent if element is idempotent | ||
| 6. `removeSetElement` : idempotent if element is idempotent | ||
| 7. `appendMapElement` : idempotent if both key and value are idempotent | ||
| 8. `prependMapElement` : idempotent if both key and value are idempotent | ||
| 9. `removeMapElement` : idempotent if both key and value are idempotent | ||
|
|
||
| In practice, most invocations of the above methods will be idempotent because most collection | ||
| elements are. For example, the following statement is idempotent since `literal(1)` is also | ||
| idempotent: | ||
|
|
||
| ```java | ||
| SimpleStatement statement = | ||
| update("foo") | ||
| .removeListElement("l", literal(1)) | ||
| .whereColumn("k").isEqualTo(bindMarker()) | ||
| .build(); | ||
| // UPDATE foo SET l=l-[1] WHERE k=? | ||
| assert statement.isIdempotent(); | ||
| ``` | ||
|
|
||
| However, in rare cases the resulting statement won't be marked idempotent, e.g. if you use a | ||
| function to select a collection element: | ||
|
|
||
| ```java | ||
| SimpleStatement statement = | ||
| update("foo") | ||
| .removeListElement("l", function("myfunc")) | ||
| .whereColumn("k").isEqualTo(bindMarker()) | ||
| .build(); | ||
| // UPDATE foo SET l=l-[myfunc()] WHERE k=? | ||
| assert !statement.isIdempotent(); | ||
| ``` | ||
|
|
||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 👍 I had to refer to the javadocs to remember when terms weren't idempotent, so good thing to mention it in the manual too. |
||
| ### Unsafe deletions | ||
|
|
||
| Deleting from a list is not idempotent: | ||
|
|
||
| ```java | ||
| Statement statement = | ||
| SimpleStatement statement = | ||
| deleteFrom("foo") | ||
| .element("l", literal(0)) | ||
| .whereColumn("k").isEqualTo(bindMarker()) | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this change confuses me, I think that the goal of the ticket is to support
+=syntax, but here instead we are changing the syntax tovalue= value+...There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No the goal of the ticket is not to support
x += y. This is the currently supported syntax, but it has the drawback of not being available for C* < 3.10. The goal is to use the old, alternative syntaxx = x + y, which is more widely supported.