Skip to content
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

[feat]: Respond with Batch CQL statement executed instead of CQL statement executed when executing Batches #536

Open
millerjp opened this issue Nov 3, 2024 · 0 comments
Assignees
Labels
cql console enhancement New feature or request

Comments

@millerjp
Copy link
Contributor

millerjp commented Nov 3, 2024

Respond with Batch CQL statement executed instead of CQL statement executed.

Screenshot 2024-11-03 at 08 28 29

Batch statments can be of three different types

  • BEGIN BATCH - Logged batches ensure atomicity across multiple operations
  • BEGIN UNLOGGED BATCH - unlogged batches are more performant for single-partition operation but are not atomic so usually avoid them but they need to be tested
  • BEGIN COUNTER BATCH - counter batches are specifically for counter updates

Testing Batch Statments

Create a keyspace and tables:

-- Create keyspace
CREATE KEYSPACE IF NOT EXISTS batch_demo 
WITH replication = {'class': 'SimpleStrategy', 'replication_factor': 1};

USE batch_demo;

-- Product inventory table
CREATE TABLE products (
    product_id uuid,
    name text,
    price decimal,
    stock int,
    last_updated timestamp,
    PRIMARY KEY (product_id)
);

-- Customer orders table
CREATE TABLE orders (
    order_id uuid,
    customer_id uuid,
    product_id uuid,
    quantity int,
    order_status text,
    order_date timestamp,
    PRIMARY KEY (order_id)
);

-- Product views counter table
CREATE TABLE product_views (
    product_id uuid,
    view_count counter,
    PRIMARY KEY (product_id)
);

-- Shopping cart table (for LWT examples)
CREATE TABLE shopping_cart (
    cart_id uuid,
    product_id uuid,
    quantity int,
    version int,
    PRIMARY KEY (cart_id, product_id)
);

Logged Batch Example

BEGIN BATCH
    INSERT INTO products (product_id, name, price, stock, last_updated) 
    VALUES (uuid(), 'Laptop', 999.99, 10, toTimestamp(now()));
    
    INSERT INTO products (product_id, name, price, stock, last_updated) 
    VALUES (uuid(), 'Mouse', 29.99, 50, toTimestamp(now()));
    
    INSERT INTO products (product_id, name, price, stock, last_updated) 
    VALUES (uuid(), 'Keyboard', 59.99, 30, toTimestamp(now()));
APPLY BATCH;

Unlogged Batch Example

BEGIN UNLOGGED BATCH
    UPDATE products SET stock = stock - 1 WHERE product_id = 123e4567-e89b-12d3-a456-426614174000;
    
    INSERT INTO orders (order_id, customer_id, product_id, quantity, order_status, order_date)
    VALUES (uuid(), 123e4567-e89b-12d3-a456-426614174001, 
            123e4567-e89b-12d3-a456-426614174000, 1, 'PENDING', toTimestamp(now()));
APPLY BATCH;

Counter Batch Example

BEGIN COUNTER BATCH
    UPDATE product_views SET view_count = view_count + 1 
    WHERE product_id = 123e4567-e89b-12d3-a456-426614174000;
    
    UPDATE product_views SET view_count = view_count + 1 
    WHERE product_id = 123e4567-e89b-12d3-a456-426614174001;
APPLY BATCH;

LWT (Lightweight Transactions) Batch Examples

First, insert initial data:

INSERT INTO shopping_cart (cart_id, product_id, quantity, version) 
VALUES (123e4567-e89b-12d3-a456-426614174000, 
        123e4567-e89b-12d3-a456-426614174001, 1, 1);

Successful LWT batch:

BEGIN BATCH
    UPDATE shopping_cart 
    SET quantity = 2, version = 2 
    WHERE cart_id = 123e4567-e89b-12d3-a456-426614174000 
    AND product_id = 123e4567-e89b-12d3-a456-426614174001
    IF version = 1;
    
    INSERT INTO orders (order_id, customer_id, product_id, quantity, order_status, order_date)
    VALUES (uuid(), 123e4567-e89b-12d3-a456-426614174002, 
            123e4567-e89b-12d3-a456-426614174001, 2, 'PENDING', toTimestamp(now()));
APPLY BATCH;

Failing LWT batch (will fail because version is now 2):

BEGIN BATCH
    UPDATE shopping_cart 
    SET quantity = 3, version = 3 
    WHERE cart_id = 123e4567-e89b-12d3-a456-426614174000 
    AND product_id = 123e4567-e89b-12d3-a456-426614174001
    IF version = 1;
    
    INSERT INTO orders (order_id, customer_id, product_id, quantity, order_status, order_date)
    VALUES (uuid(), 123e4567-e89b-12d3-a456-426614174002, 
            123e4567-e89b-12d3-a456-426614174001, 3, 'PENDING', toTimestamp(now()));
APPLY BATCH;
@millerjp millerjp added enhancement New feature or request cql console labels Nov 3, 2024
@millerjp millerjp added this to the v1.0.0-release milestone Nov 3, 2024
@millerjp millerjp self-assigned this Nov 3, 2024
@millerjp millerjp changed the title [feat]: Update reponse to handle Batch Statments [feat]: Update response to handle Batch Statments Nov 3, 2024
@millerjp millerjp changed the title [feat]: Update response to handle Batch Statments [feat]: Respond with Batch CQL statement executed instead of CQL statement executed when executing Batches Nov 3, 2024
@millerjp millerjp assigned mhmdkrmabd and unassigned millerjp Nov 3, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
cql console enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants