Skip to content

Commit

Permalink
spanner: Properly handle multi-row mutations with heterogeneous columns
Browse files Browse the repository at this point in the history
Prior to this change, attempting to insert multiple rows at once with Table#insert or
Transaction#insert where some rows lack a value for nullable columns would result in data attempting
to be inserted into the incorrect column, due to a flawed assumption in transaction-request.js that
every single row contains exact same set of columns, and that the order of keys returned by
Object.keys would be identical for each and every row.

This fixes #2411.
  • Loading branch information
jscinoz committed Jun 28, 2017
1 parent ca0d96c commit a2a8ce5
Showing 1 changed file with 20 additions and 3 deletions.
23 changes: 20 additions & 3 deletions packages/spanner/src/transaction-request.js
Original file line number Diff line number Diff line change
Expand Up @@ -684,12 +684,29 @@ TransactionRequest.prototype.mutate_ = function(method, table, keyVals, cb) {

var mutation = {};

var columns = keyVals.reduce(function(allKeys, keyVal) {
var keys = Object.keys(keyVal);
var key;

for (var i = 0, ii = keys.length; i < ii; ++i) {
key = keys[i];

if (allKeys.indexOf(key) === -1) {
allKeys.push(key);
}
}

return allKeys;
}, []).sort();

mutation[method] = {
table: table,
columns: Object.keys(keyVals[0]),
columns: columns,
values: keyVals.map(function(keyVal) {
return Object.keys(keyVal).map(function(key) {
return codec.encode(keyVal[key]);
return columns.map(function(key) {
var value = keyVal[key];

return codec.encode(value === undefined ? null : value);
});
})
};
Expand Down

0 comments on commit a2a8ce5

Please sign in to comment.