Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Return booleans from expression comparisons, allow for vectors to be defined in expressions #1548

Merged
merged 7 commits into from
Sep 26, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
remove operator bool() for t_tscalar, implement custom size_t > t_tsc…
…alar comparator, fix vectors for exprtk
  • Loading branch information
sc1f authored and texodus committed Sep 26, 2021
commit 50c28250fb4d29d518c52981c8edae7235abde08
2 changes: 1 addition & 1 deletion cpp/perspective/src/cpp/context_unit.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -381,7 +381,7 @@ t_ctxunit::unity_get_row_data(t_uindex idx) const {

std::vector<t_tscalar>
t_ctxunit::unity_get_row_path(t_uindex idx) const {
return std::vector<t_tscalar>(mktscalar(idx));
return {};
}

std::vector<t_tscalar>
Expand Down
2 changes: 1 addition & 1 deletion cpp/perspective/src/cpp/context_zero.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -798,7 +798,7 @@ t_ctx0::unity_get_column_data(t_uindex idx) const {

std::vector<t_tscalar>
t_ctx0::unity_get_row_path(t_uindex idx) const {
return std::vector<t_tscalar>(mktscalar(idx));
return {};
}

std::vector<t_tscalar>
Expand Down
6 changes: 1 addition & 5 deletions cpp/perspective/src/cpp/emscripten.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -111,11 +111,7 @@ namespace binding {
}
switch (scalar.get_dtype()) {
case DTYPE_BOOL: {
if (scalar) {
return t_val(true);
} else {
return t_val(false);
}
return t_val(scalar.as_bool());
}
case DTYPE_TIME: {
if (cast_double) {
Expand Down
3 changes: 1 addition & 2 deletions cpp/perspective/src/cpp/expression_tables.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -75,8 +75,7 @@ t_expression_tables::calculate_transitions(

bool prev_valid = prev_column.is_valid(ridx);
bool curr_valid = current_column.is_valid(ridx);
bool prev_curr_eq
= prev_valid && curr_value && prev_value == curr_value;
bool prev_curr_eq = prev_valid && curr_valid && (prev_value == curr_value);

t_value_transition transition;

Expand Down
4 changes: 2 additions & 2 deletions cpp/perspective/src/cpp/multi_sort.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -120,8 +120,8 @@ get_minmax_idx(const std::vector<t_tscalar>& vec, t_sorttype stype) {
for (t_index idx = 0, loop_end = vec.size(); idx < loop_end;
++idx) {
double val = std::abs(vec[idx].to_double());
double mindbl = std::abs(double(min_max.first));
double maxdbl = std::abs(double(min_max.second));
double mindbl = std::abs(double(min_max.first.as_bool()));
double maxdbl = std::abs(double(min_max.second.as_bool()));

if (val <= mindbl) {
min_max.first.set(val);
Expand Down
7 changes: 6 additions & 1 deletion cpp/perspective/src/cpp/scalar.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,10 @@ SUPPRESS_WARNINGS_VC(4800)

namespace perspective {

bool operator>(const std::size_t& lhs, const t_tscalar& rhs) {
return rhs.operator<(lhs);
}

#define BINARY_OPERATOR_BODY(OP) \
t_tscalar rval; \
rval.clear(); \
Expand Down Expand Up @@ -867,7 +871,8 @@ t_tscalar::is_signed() const {
|| m_type == DTYPE_INT16 || m_type == DTYPE_INT8);
}

t_tscalar::operator bool() const {
bool
t_tscalar::as_bool() const {
if (m_status != STATUS_VALID)
return false;

Expand Down
28 changes: 16 additions & 12 deletions cpp/perspective/src/cpp/sparse_tree.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1089,7 +1089,7 @@ t_stree::update_agg_table(t_uindex nidx, t_agg_update_info& info,
apply_from_gstate(gstate, expression_master_table,
spec.get_dependencies()[0].name(), pkeys, new_value,
[](const t_tscalar& row_value, t_tscalar& output) {
if (row_value) {
if (row_value.as_bool()) {
output.set(row_value);
return true;
}
Expand Down Expand Up @@ -1231,17 +1231,21 @@ t_stree::update_agg_table(t_uindex nidx, t_agg_update_info& info,
old_value.set(dst->get_scalar(dst_ridx));
auto pkeys = get_pkeys(nidx);

new_value.set(reduce_from_gstate<
std::function<t_tscalar(std::vector<t_tscalar>&)>>(gstate,
expression_master_table, spec.get_dependencies()[0].name(),
pkeys, [](std::vector<t_tscalar>& values) {
t_tscalar rval;
rval.set(true);

for (const auto& v : values) {
if (!v) {
rval.set(false);
break;
new_value.set(
reduce_from_gstate<std::function<t_tscalar(std::vector<t_tscalar>&)>>(
gstate,
expression_master_table,
spec.get_dependencies()[0].name(),
pkeys,
[](std::vector<t_tscalar>& values) {
t_tscalar rval;
rval.set(true);

for (const auto& v : values) {
if (!v.as_bool()) {
rval.set(false);
break;
}
}
}
return rval;
Expand Down
21 changes: 20 additions & 1 deletion cpp/perspective/src/include/perspective/scalar.h
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,15 @@ struct PERSPECTIVE_EXPORT t_tscalar {
bool ends_with(const t_tscalar& other) const;
bool contains(const t_tscalar& other) const;
bool is_valid() const;
operator bool() const;

/**
* @brief Identical to operator bool(), but less ambiguous.
*
* @return true
* @return false
*/
bool as_bool() const;

void clear();
t_dtype get_dtype() const;
const char* get_char_ptr() const;
Expand Down Expand Up @@ -408,6 +416,17 @@ mktscalar(const T& v) {

t_tscalar mktscalar();

/**
* @brief Overload comparisons between size_t and t_tscalar as the comparison
* is used in exprtk for scalar initialization.
*
* @param lhs
* @param rhs
* @return true
* @return false
*/
bool operator>(const std::size_t& lhs, const t_tscalar& rhs);

} // end namespace perspective

namespace std {
Expand Down
53 changes: 53 additions & 0 deletions packages/perspective/test/js/expressions/updates.js
Original file line number Diff line number Diff line change
Expand Up @@ -896,6 +896,59 @@ module.exports = (perspective) => {
table.delete();
});

it("multiple updates change validity", async function () {
const table = await perspective.table(
{
a: "float",
b: "float",
c: "string",
index: "integer",
},
{index: "index"}
);

table.update({
a: [null, null, 1.5, null],
b: [null, 2.5, null, null],
c: ["a", "b", "b", "a"],
index: [1, 2, 3, 4],
});

const view = await table.view({
expressions: ['// computed\n"a" + "b"'],
});

let result = await view.to_columns();
expect(result["computed"]).toEqual([null, null, null, null]);

table.update({
index: [4],
a: [100],
});

result = await view.to_columns();
expect(result["computed"]).toEqual([null, null, null, null]);

table.update({
index: [4],
b: [100],
});

result = await view.to_columns();
expect(result["computed"]).toEqual([null, null, null, 200]);

table.update({
index: [3],
b: [100],
});

result = await view.to_columns();
expect(result["computed"]).toEqual([null, null, 101.5, 200]);

await view.delete();
await table.delete();
});

it("multiple partial update on single computed source column", async function () {
const table = await perspective.table(data);
const view = await table.view({
Expand Down