Skip to content

Commit

Permalink
Merge pull request #208 from jpmorganchase/column-oriented-partial-up…
Browse files Browse the repository at this point in the history
…date

Column oriented partial update
  • Loading branch information
texodus authored Aug 25, 2018
2 parents e9ff128 + 03d848c commit 880b1a1
Show file tree
Hide file tree
Showing 2 changed files with 87 additions and 18 deletions.
42 changes: 24 additions & 18 deletions packages/perspective/src/js/perspective.js
Original file line number Diff line number Diff line change
Expand Up @@ -213,12 +213,15 @@ function parse_data(data, names, types) {
}

} else if (Array.isArray(data[Object.keys(data)[0]])) {
// Column oriented
// Column oriented update. Extending schema not supported here.

names = Object.keys(data);
const names_in_update = Object.keys(data);
row_count = data[names_in_update[0]].length;
names = names || names_in_update;

for (let col_num = 0; col_num < names.length; col_num++) {
const name = names[col_num];

let colNum = 0;
for (let name in data) {
// Infer column type if necessary
if (!preloaded) {
let i = 0;
Expand All @@ -231,10 +234,14 @@ function parse_data(data, names, types) {
types.push(inferredType);
}

let transformed = transform_data(types[colNum], data[name]);
colNum++;
// Extract the data or fill with undefined if column doesn't exist (nothing in column changed)
let transformed;
if (data.hasOwnProperty(name)) {
transformed = transform_data(types[col_num], data[name]);
} else {
transformed = new Array(row_count);
}
cdata.push(transformed);
row_count = transformed.length
}

} else if (typeof data[Object.keys(data)[0]] === "string" || typeof data[Object.keys(data)[0]] === "function") {
Expand Down Expand Up @@ -496,6 +503,8 @@ view.prototype.schema = async function() {
let schema = this.gnode.get_tblschema();
let _types = schema.types();
let names = schema.columns();
schema.delete();

let types = {};
for (let i = 0; i < names.size(); i ++) {
types[names.get(i)] = _types.get(i).value
Expand All @@ -520,6 +529,10 @@ view.prototype.schema = async function() {
new_schema[col_name] = map_aggregate_types(col_name, new_schema[col_name], this.config.aggregate);
}
}

_types.delete();
names.delete();

return new_schema;
}

Expand Down Expand Up @@ -958,17 +971,6 @@ table.prototype._schema = function () {
continue;
}
new_schema[columns.get(key)] = get_column_type(types.get(key).value);
/*if (types.get(key).value === 1 || types.get(key).value === 2) {
new_schema[columns.get(key)] = "integer";
} else if (types.get(key).value === 19) {
new_schema[columns.get(key)] = "string";
} else if (types.get(key).value === 10 || types.get(key).value === 9) {
new_schema[columns.get(key)] = "float";
} else if (types.get(key).value === 11) {
new_schema[columns.get(key)] = "boolean";
} else if (types.get(key).value === 12) {
new_schema[columns.get(key)] = "date";
}*/
}
schema.delete();
columns.delete();
Expand Down Expand Up @@ -1455,6 +1457,10 @@ table.prototype._column_metadata = function () {
metadata.push(meta);
}

types.delete()
cols.delete();
schema.delete();

return metadata;
}

Expand Down
63 changes: 63 additions & 0 deletions packages/perspective/test/js/updates.js
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,21 @@ module.exports = (perspective) => {
expect(result).toEqual(data);
});

it("Column oriented `update()` with columns in different order to schema", async function () {
var table = perspective.table(meta);

var reordered_col_data = {
'y': ['a', 'b', 'c', 'd'],
'z': [true, false, true, false],
'x': [1, 2, 3, 4]
};

table.update(reordered_col_data);
var view = table.view();
let result = await view.to_json();
expect(result).toEqual(data);
});

it("Column data constructor then column oriented `update()`", async function () {
var colUpdate = {
'x': [3, 4, 5],
Expand Down Expand Up @@ -327,6 +342,54 @@ module.exports = (perspective) => {
table.update(partial);
});

it("partial column oriented update", function (done) {
var partial = {
x: [5, undefined],
y: ['a', 'b'],
z: [undefined, true]
};

var expected = [
{'x': 5, 'y':'a', 'z': true},
{'x': 2, 'y':'b', 'z': true},
{'x': 3, 'y':'c', 'z': true},
{'x': 4, 'y':'d', 'z': false}
];
var table = perspective.table(meta, {index: 'y'});
var view = table.view();
table.update(col_data);
view.on_update(async function (new_data) {
expect(new_data).toEqual(expected.slice(0, 2));
let json = await view.to_json();
expect(json).toEqual(expected);
done();
});
table.update(partial);
});

it("partial column oriented update with entire columns missing", function (done) {
var partial = {
y: ['a', 'b'],
z: [false, true]
};

var expected = [
{'x': 1, 'y':'a', 'z': false},
{'x': 2, 'y':'b', 'z': true},
{'x': 3, 'y':'c', 'z': true},
{'x': 4, 'y':'d', 'z': false}
];
var table = perspective.table(meta, {index: 'y'});
var view = table.view();
table.update(col_data);
view.on_update(async function (new_data) {
expect(new_data).toEqual(expected.slice(0, 2));
let json = await view.to_json();
expect(json).toEqual(expected);
done();
});
table.update(partial);
});

});

Expand Down

0 comments on commit 880b1a1

Please sign in to comment.