Skip to content

Commit

Permalink
Merge pull request #274 from nmichaud/fix_computed_deltas
Browse files Browse the repository at this point in the history
Computed columns broken with delta updates on non-dependent columns
  • Loading branch information
texodus authored Oct 15, 2018
2 parents f087c2b + b3d7a49 commit 5768fc3
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 6 deletions.
25 changes: 19 additions & 6 deletions packages/perspective/src/cpp/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -780,6 +780,9 @@ sort(t_ctx2_sptr ctx2, val j_sortby) {
*/
val
scalar_to_val(const t_tscalar scalar) {
if (!scalar.is_valid()) {
return val::null();
}
switch (scalar.get_dtype()) {
case DTYPE_BOOL: {
if (scalar) {
Expand Down Expand Up @@ -934,7 +937,7 @@ table_add_computed_column(t_table_sptr table, t_str name, t_dtype dtype, val fun

t_uindex size = table->size();
for (t_uindex ridx = 0; ridx < size; ++ridx) {
val value = val::null();
val value = val::undefined();

switch (arity) {
case 0: {
Expand All @@ -943,28 +946,36 @@ table_add_computed_column(t_table_sptr table, t_str name, t_dtype dtype, val fun
}
case 1: {
i1 = scalar_to_val(icols[0]->get_scalar(ridx));
value = func(i1);
if (!i1.isNull()) {
value = func(i1);
}
break;
}
case 2: {
i1 = scalar_to_val(icols[0]->get_scalar(ridx));
i2 = scalar_to_val(icols[1]->get_scalar(ridx));
value = func(i1, i2);
if (!i1.isNull() && !i2.isNull()) {
value = func(i1, i2);
}
break;
}
case 3: {
i1 = scalar_to_val(icols[0]->get_scalar(ridx));
i2 = scalar_to_val(icols[1]->get_scalar(ridx));
i3 = scalar_to_val(icols[2]->get_scalar(ridx));
value = func(i1, i2, i3);
if (!i1.isNull() && !i2.isNull() && !i3.isNull()) {
value = func(i1, i2, i3);
}
break;
}
case 4: {
i1 = scalar_to_val(icols[0]->get_scalar(ridx));
i2 = scalar_to_val(icols[1]->get_scalar(ridx));
i3 = scalar_to_val(icols[2]->get_scalar(ridx));
i4 = scalar_to_val(icols[3]->get_scalar(ridx));
value = func(i1, i2, i3, i4);
if (!i1.isNull() && !i2.isNull() && !i3.isNull() && !i4.isNull()) {
value = func(i1, i2, i3, i4);
}
break;
}
default: {
Expand All @@ -973,7 +984,9 @@ table_add_computed_column(t_table_sptr table, t_str name, t_dtype dtype, val fun
}
}

set_column_nth(out, ridx, value);
if (!value.isUndefined()) {
set_column_nth(out, ridx, value);
}
}
}

Expand Down
27 changes: 27 additions & 0 deletions packages/perspective/test/js/constructors.js
Original file line number Diff line number Diff line change
Expand Up @@ -362,6 +362,33 @@ module.exports = perspective => {
expect(expected).toEqual(result);
});

it("Computed column of arity 2 with updates on non-dependent columns", async function() {
var meta = {
w: "float",
x: "float",
y: "string",
z: "boolean"
};
var table = perspective.table(meta, {index: "y"});
let table2 = table.add_computed([
{
column: "ratio",
type: "float",
func: (w, x) => w / x,
inputs: ["w", "x"]
}
]);

table2.update(data_3);

let delta_upd = [{y: "a", z: false}, {y: "b", z: true}, {y: "c", z: false}, {y: "d", z: true}];
table2.update(delta_upd);

let result = await table2.view({aggregate: [{op: "count", column: "y"}, {op: "count", column: "ratio"}]}).to_json();
let expected = [{y: "a", ratio: 1.5}, {y: "b", ratio: 1.25}, {y: "c", ratio: 1.1666666666666667}, {y: "d", ratio: 1.125}];
expect(expected).toEqual(result);
});

it("String computed column of arity 1", async function() {
var table = perspective.table(data);

Expand Down

0 comments on commit 5768fc3

Please sign in to comment.