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

Add 'not in' filter operation #255

Merged
merged 2 commits into from
Oct 2, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
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
3 changes: 3 additions & 0 deletions packages/perspective/src/cpp/base.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -252,6 +252,9 @@ filter_op_to_str(t_filter_op op) {
case FILTER_OP_IN: {
return "in";
} break;
case FILTER_OP_NOT_IN: {
return "not in";
} break;
case FILTER_OP_AND: {
return "and";
} break;
Expand Down
3 changes: 2 additions & 1 deletion packages/perspective/src/cpp/filter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -82,8 +82,9 @@ t_fterm::get_expr() const {
ss << filter_op_to_str(m_op) << " ";
ss << m_threshold.to_string(true);
} break;
case FILTER_OP_NOT_IN:
case FILTER_OP_IN: {
ss << " in (";
ss << " " << filter_op_to_str(m_op) << " (";
for (auto v : m_bag) {
ss << v.to_string(true) << ", ";
}
Expand Down
2 changes: 2 additions & 0 deletions packages/perspective/src/cpp/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ _get_fterms(t_schema schema, val j_filters) {
t_filter_op comp = filter[1].as<t_filter_op>();

switch (comp) {
case FILTER_OP_NOT_IN:
case FILTER_OP_IN: {
t_tscalvec terms{};
std::vector<std::string> j_terms = vecFromJSArray<std::string>(filter[2]);
Expand Down Expand Up @@ -1139,6 +1140,7 @@ EMSCRIPTEN_BINDINGS(perspective) {
.value("FILTER_OP_CONTAINS", FILTER_OP_CONTAINS)
.value("FILTER_OP_OR", FILTER_OP_OR)
.value("FILTER_OP_IN", FILTER_OP_IN)
.value("FILTER_OP_NOT_IN", FILTER_OP_NOT_IN)
.value("FILTER_OP_AND", FILTER_OP_AND)
.value("FILTER_OP_IS_NAN", FILTER_OP_IS_NAN)
.value("FILTER_OP_IS_NOT_NAN", FILTER_OP_IS_NOT_NAN)
Expand Down
1 change: 1 addition & 0 deletions packages/perspective/src/include/perspective/base.h
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,7 @@ enum t_filter_op {
FILTER_OP_CONTAINS,
FILTER_OP_OR,
FILTER_OP_IN,
FILTER_OP_NOT_IN,
FILTER_OP_AND,
FILTER_OP_IS_NAN,
FILTER_OP_IS_NOT_NAN,
Expand Down
14 changes: 14 additions & 0 deletions packages/perspective/src/include/perspective/build_filter.h
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,20 @@ apply_filters_helper(const t_table& tbl, const t_str& column, t_mask& mask, t_ts

tbl, column, mask, values);
} break;
case FILTER_OP_NOT_IN: {
std::set<CTYPE_T, t_filter_comparator<CTYPE_T>> values;
for (t_uindex fidx = 0, loop_end = filter.m_bag.size();

fidx < loop_end; ++fidx) {
values.insert(filter
.m_bag[fidx]

.get<CTYPE_T>());
}
fop_apply<CTYPE_T, DTYPE_T, t_operator_not_in<CTYPE_T, DTYPE_T>>(

tbl, column, mask, values);
} break;
default: { PSP_COMPLAIN_AND_ABORT("Unknown filter_op detected"); }
};
}
Expand Down
12 changes: 12 additions & 0 deletions packages/perspective/src/include/perspective/filter.h
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,15 @@ struct t_operator_in {
}
};

template <typename DATA_T, int DTYPE_T>
struct t_operator_not_in {
inline bool
operator()(const DATA_T& value, const std::set<DATA_T>& threshold_values) {
return threshold_values.find(value) == threshold_values.end();
}
};


template <typename DATA_T, int DTYPE_T>
struct t_operator_begins_with {
inline bool
Expand Down Expand Up @@ -142,6 +151,9 @@ struct PERSPECTIVE_EXPORT t_fterm {
operator()(t_tscalar s) const {
t_bool rv;
switch (m_op) {
case FILTER_OP_NOT_IN: {
rv = std::find(m_bag.begin(), m_bag.end(), s) == m_bag.end();
} break;
case FILTER_OP_IN: {
rv = std::find(m_bag.begin(), m_bag.end(), s) != m_bag.end();
} break;
Expand Down
1 change: 1 addition & 0 deletions packages/perspective/src/js/perspective.js
Original file line number Diff line number Diff line change
Expand Up @@ -1109,6 +1109,7 @@ module.exports = function(Module) {
"ends with": __MODULE__.t_filter_op.FILTER_OP_ENDS_WITH,
or: __MODULE__.t_filter_op.FILTER_OP_OR,
in: __MODULE__.t_filter_op.FILTER_OP_IN,
"not in": __MODULE__.t_filter_op.FILTER_OP_NOT_IN,
and: __MODULE__.t_filter_op.FILTER_OP_AND,
"is nan": __MODULE__.t_filter_op.FILTER_OP_IS_NAN,
"is not nan": __MODULE__.t_filter_op.FILTER_OP_IS_NOT_NAN
Expand Down
11 changes: 11 additions & 0 deletions packages/perspective/test/js/filters.js
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,17 @@ module.exports = perspective => {
});
});

describe("not in", function() {
it("y not in ['d']", async function() {
var table = perspective.table(data);
var view = table.view({
filter: [["y", "not in", ["d"]]]
});
let json = await view.to_json();
expect(rdata.slice(0, 3)).toEqual(json);
});
});

describe("contains", function() {
it("y contains 'a'", async function() {
var table = perspective.table(data);
Expand Down