-
Notifications
You must be signed in to change notification settings - Fork 31
Null bindings on prepared statements and undesired tombstone creation
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.
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:
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}]).
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]).
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]).