Skip to content

Null bindings on prepared statements and undesired tombstone creation

Silviu Caragea edited this page Nov 9, 2022 · 2 revisions

Problem

Inserting a null value into a field from a CQL statement always generates a tombstone. This happens because Cassandra cannot decide whether inserting a null value means that we are trying to void a field that previously had a value or that we do not want to insert a value for that specific field.

Solutions

Using non prepared statement queries an workaround to this problem is straightforward: just do not specify the field with null values when you run the query.

For prepared statements starting erlcass 4.0.8 you have 3 solutions available:

Solution 1: Binding by name (available into all erlcass versions)

The following example will not create a tombstone for name and age parameters because those are not specified into the binding process.

ok = erlcass:add_prepare_statement(identifier, <<"INSERT INTO mytable (key, name, age) VALUES(?,?,?)">>),
ok = erlcass:execute(identifier, ?BIND_BY_NAME, [{<<"key">>, 1}]).

Solution 2: Use the undefined value while binding by index (erlcass >= 4.0.8)

Binding by name is a little too verbose and any peoples prefer to bind the parameters by index. For all null values in order to avoid creating of tombstones send them as undefined atom as in the next example:

ok = erlcass:add_prepare_statement(identifier, <<"INSERT INTO mytable (key, name, age) VALUES(?,?,?)">>),
ok = erlcass:execute(identifier, [1, undefined, undefined]).

Solution 3: Use the null_binding option while prepare your query (erlcass >= 4.0.8)

The null_binding option while preparing your queries allows you to disable the binding of null values (enabled by default). For the next example the fact that name and age are null will not cause tombstones because those fields are ignored while we binding the values.

ok = erlcass:add_prepare_statement(identifier, {<<"INSERT INTO mytable (key, name, age) VALUES(?,?,?)">>, [{null_binding, false}]}),
ok = erlcass:execute(identifier, [1, null, null]).