docs/kb/lwt-differences.rst
ScyllaDB is making an effort to be compatible with Cassandra, down to the level of limitations of the implementation. How is it different?
IF EXISTS, IF NOT EXISTS, and other conditions for the same row.More on :doc:Lightweight Transactions (LWT) </features/lwt>
Conditional batches are BATCH statements that contain one or more conditional statements. A batch is executed only if all conditions in all statements are true, and all conditions are evaluated against the initial database state. ScyllaDB allows using different conditions such as IF EXISTS, IF NOT EXISTS, and other IF expressions within the same batch, even if the statements affect the same row.
For example, the following batch statement is valid in ScyllaDB and will be applied successfully:
.. code-block:: cql
BEGIN BATCH UPDATE movies.nowshowing SET main_actor = NULL WHERE movie = 'Invisible Man' IF director = 'Leigh Whannell' UPDATE movies.nowshowing SET released = NULL WHERE movie = 'Invisible Man' IF EXISTS APPLY BATCH;
[applied] | movie | location | run_day | run_time | director | main_actor | released | theater
-----------+---------------+----------+---------+----------+----------------+----------------+------------+--------- True | Invisible Man | null | null | null | Leigh Whannell | Elisabeth Moss | 2022-04-06 | null True | Invisible Man | null | null | null | Leigh Whannell | Elisabeth Moss | 2022-04-06 | null
By contrast, Cassandra does not allow mixing IF EXISTS, IF NOT EXISTS, and other conditions for the same row:
.. code-block:: cql
BEGIN BATCH UPDATE movies.nowshowing SET main_actor = NULL WHERE movie = 'Invisible Man' IF director = 'Leigh Whannell' UPDATE movies.nowshowing SET released = NULL WHERE movie = 'Invisible Man' IF EXISTS APPLY BATCH;
InvalidRequest: Error from server: code=2200 [Invalid query] message="Cannot mix IF conditions and IF EXISTS for the same row"
Moreover, ScyllaDB does not return an error even if the conditions are impossible to satisfly, i.e. a batch contains IF EXISTS and IF NOT EXISTS clauses for the same row. The following query will be executed, though not applied, as the conditions are impossible to satisfy:
.. code-block:: cql
BEGIN BATCH
INSERT INTO movies.nowshowing (movie, location, theater, run_day, run_time) VALUES ('Invisible Man', 'Times Square', 'AMC Empire 25', 'Saturday', '23:00:00') IF NOT EXISTS
UPDATE movies.nowshowing SET theater = NULL WHERE movie = 'Invisible Man' AND location = 'Times Square' AND run_day = 'Saturday' AND run_time = '23:00:00' IF EXISTS
APPLY BATCH;
[applied] | movie | location | run_day | run_time | director | main_actor | released | theater
-----------+-------+----------+---------+----------+----------+------------+----------+---------
False | null | null | null | null | null | null | null | null
False | null | null | null | null | null | null | null | null
In comparison, Cassandra returns an error in this case:
.. code-block:: cql
BEGIN BATCH
INSERT INTO movies.nowshowing (movie, location, theater, run_day, run_time) VALUES ('Invisible Man', 'Times Square', 'AMC Empire 25', 'Saturday', '23:00:00') IF NOT EXISTS
UPDATE movies.nowshowing SET theater = NULL WHERE movie = 'Invisible Man' AND location = 'Times Square' AND run_day = 'Saturday' AND run_time = '23:00:00' IF EXISTS
APPLY BATCH;
InvalidRequest: Error from server: code=2200 [Invalid query] message="Cannot mix IF EXISTS and IF NOT EXISTS conditions for the same row"