Skip to content

Commit 6fd5a10

Browse files
committed
Add get_min_max() to perspective API
1 parent 4f83f6f commit 6fd5a10

File tree

18 files changed

+409
-18
lines changed

18 files changed

+409
-18
lines changed

cpp/perspective/src/cpp/context_one.cpp

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,49 @@ t_ctx1::close(t_index idx) {
9797
return retval;
9898
}
9999

100+
std::pair<t_tscalar, t_tscalar>
101+
t_ctx1::get_min_max(const std::string& colname) const {
102+
auto rval = std::make_pair(mknone(), mknone());
103+
auto aggtable = m_tree->get_aggtable();
104+
t_schema aggschema = aggtable->get_schema();
105+
auto col = aggtable->get_const_column(colname).get();
106+
auto colidx = aggschema.get_colidx(colname);
107+
auto depth = m_config.get_num_rpivots();
108+
const std::vector<t_aggspec>& aggspecs = m_config.get_aggregates();
109+
bool is_finished = false;
110+
while (!is_finished && depth > 0) {
111+
for (std::size_t i = 0; i < m_traversal->size(); i++) {
112+
t_index nidx = m_traversal->get_tree_index(i);
113+
t_index pnidx = m_tree->get_parent_idx(nidx);
114+
if (m_tree->get_depth(nidx) != depth) {
115+
continue;
116+
}
117+
118+
t_uindex agg_ridx = m_tree->get_aggidx(nidx);
119+
t_index agg_pridx = pnidx == INVALID_INDEX ? INVALID_INDEX : m_tree->get_aggidx(pnidx);
120+
t_tscalar val
121+
= extract_aggregate(aggspecs[colidx], col, agg_ridx, agg_pridx);
122+
123+
if (!val.is_valid()) {
124+
continue;
125+
}
126+
127+
is_finished = true;
128+
if (rval.first.is_none() || (!val.is_none() && val < rval.first)) {
129+
rval.first = val;
130+
}
131+
132+
if (val > rval.second) {
133+
rval.second = val;
134+
}
135+
}
136+
137+
depth--;
138+
}
139+
140+
return rval;
141+
}
142+
100143
std::vector<t_tscalar>
101144
t_ctx1::get_data(t_index start_row, t_index end_row, t_index start_col, t_index end_col) const {
102145
PSP_TRACE_SENTINEL();

cpp/perspective/src/cpp/context_two.cpp

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -190,6 +190,87 @@ t_ctx2::get_ctraversal_indices() const {
190190
return std::vector<t_index>();
191191
}
192192

193+
std::pair<t_tscalar, t_tscalar>
194+
t_ctx2::get_min_max(const std::string& colname) const {
195+
t_uindex ctx_nrows = get_row_count();
196+
t_uindex ctx_ncols = get_column_count();
197+
auto rval = std::make_pair(mknone(), mknone());
198+
t_uindex scol = m_trees[0]->get_aggtable()->get_schema().get_colidx(colname);
199+
std::vector<std::pair<t_uindex, t_uindex>> cells;
200+
for (t_index ridx = 0; ridx < ctx_nrows; ++ridx) {
201+
for (t_index cidx = 0; cidx < ctx_ncols; ++cidx) {
202+
cells.push_back(
203+
std::pair<t_index, t_index>(ridx, cidx));
204+
}
205+
}
206+
207+
auto cells_info = resolve_cells(cells);
208+
typedef std::pair<t_uindex, t_uindex> t_aggpair;
209+
std::map<t_aggpair, const t_column*> aggmap;
210+
auto n_aggs = m_config.get_num_aggregates();
211+
for (t_uindex treeidx = 0, tree_loop_end = m_trees.size(); treeidx < tree_loop_end;
212+
++treeidx) {
213+
auto aggtable = m_trees[treeidx]->get_aggtable();
214+
t_schema aggschema = aggtable->get_schema();
215+
for (t_uindex aggidx = 0, agg_loop_end = n_aggs;
216+
aggidx < agg_loop_end; ++aggidx) {
217+
const std::string& aggname = aggschema.m_columns[aggidx];
218+
aggmap[t_aggpair(treeidx, aggidx)] = aggtable->get_const_column(aggname).get();
219+
}
220+
}
221+
222+
const std::vector<t_aggspec>& aggspecs = m_config.get_aggregates();
223+
bool is_finished = false;
224+
t_uindex row_depth = m_row_depth + 1;
225+
while (!is_finished && row_depth > 0) {
226+
for (std::size_t i = 0; i < cells_info.size(); ++i) {
227+
const t_cellinfo& cinfo = cells_info[i];
228+
if (cinfo.m_idx < 0 || cinfo.m_agg_index != scol) {
229+
continue;
230+
} else {
231+
t_index nidx = m_rtraversal->get_tree_index(cinfo.m_ridx);
232+
if (rtree()->get_depth(nidx) != row_depth) {
233+
continue;
234+
}
235+
236+
auto col_depth
237+
= ctree()->get_depth(m_ctraversal->get_tree_index(
238+
calc_translated_colidx(n_aggs, cinfo.m_cidx)));
239+
if (m_config.get_num_cpivots() != col_depth) {
240+
continue;
241+
}
242+
243+
auto aggcol = aggmap[t_aggpair(cinfo.m_treenum, cinfo.m_agg_index)];
244+
t_index p_idx = m_trees[cinfo.m_treenum]->get_parent_idx(cinfo.m_idx);
245+
t_uindex agg_ridx = m_trees[cinfo.m_treenum]->get_aggidx(cinfo.m_idx);
246+
t_uindex agg_pridx = p_idx == INVALID_INDEX
247+
? INVALID_INDEX
248+
: m_trees[cinfo.m_treenum]->get_aggidx(p_idx);
249+
250+
auto val = extract_aggregate(
251+
aggspecs[cinfo.m_agg_index], aggcol, agg_ridx, agg_pridx);
252+
253+
if (!val.is_valid()) {
254+
continue;
255+
}
256+
257+
is_finished = true;
258+
if (rval.first.is_none() || (!val.is_none() && val < rval.first)) {
259+
rval.first = val;
260+
}
261+
262+
if (val > rval.second) {
263+
rval.second = val;
264+
}
265+
}
266+
}
267+
268+
row_depth--;
269+
}
270+
271+
return rval;
272+
}
273+
193274
std::vector<t_tscalar>
194275
t_ctx2::get_data(t_index start_row, t_index end_row, t_index start_col, t_index end_col) const {
195276
t_uindex ctx_nrows = get_row_count();

cpp/perspective/src/cpp/context_unit.cpp

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,28 @@ t_ctxunit::notify(const t_data_table& flattened) {
119119
}
120120
}
121121

122+
std::pair<t_tscalar, t_tscalar>
123+
t_ctxunit::get_min_max(const std::string& colname) const {
124+
auto col = m_gstate->get_table()->get_const_column(colname);
125+
auto rval = std::make_pair(mknone(), mknone());
126+
for (std::size_t i = 0; i < col->size(); i++) {
127+
t_tscalar val = col->get_scalar(i);
128+
if (!val.is_valid()) {
129+
continue;
130+
}
131+
132+
if (rval.first.is_none() || (!val.is_none() && val < rval.first)) {
133+
rval.first = val;
134+
}
135+
136+
if (val > rval.second) {
137+
rval.second = val;
138+
}
139+
}
140+
141+
return rval;
142+
}
143+
122144
/**
123145
* @brief Given a start/end row and column, return the data for the subset.
124146
*

cpp/perspective/src/cpp/context_zero.cpp

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -225,6 +225,39 @@ t_ctx0::notify(const t_data_table& flattened) {
225225
}
226226
}
227227

228+
/**
229+
* @brief Gives the min and max value (by t_tscalar comparison) of the leaf
230+
* nodes of a given column.
231+
*
232+
* @param colname
233+
* @return std::pair<t_tscalar, t_tscalar>
234+
*/
235+
std::pair<t_tscalar, t_tscalar>
236+
t_ctx0::get_min_max(const std::string& colname) const {
237+
std::pair<t_tscalar, t_tscalar> rval(mknone(), mknone());
238+
t_uindex ctx_nrows = get_row_count();
239+
std::vector<t_tscalar> values(ctx_nrows);
240+
std::vector<t_tscalar> pkeys = m_traversal->get_pkeys(0, ctx_nrows);
241+
std::vector<t_tscalar> out_data(pkeys.size());
242+
m_gstate->read_column(colname, pkeys, out_data);
243+
for (t_index ridx = 0; ridx < m_traversal->size(); ++ridx) {
244+
auto val = out_data[ridx];
245+
if (!val.is_valid()) {
246+
continue;
247+
}
248+
249+
if (rval.first.is_none() || (!val.is_none() && val < rval.first)) {
250+
rval.first = val;
251+
}
252+
253+
if (val > rval.second) {
254+
rval.second = val;
255+
}
256+
}
257+
258+
return rval;
259+
}
260+
228261
/**
229262
* @brief Given a start/end row and column index, return the underlying data
230263
* for the requested subset.

cpp/perspective/src/cpp/emscripten.cpp

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1739,6 +1739,16 @@ namespace binding {
17391739
return scalar_to_val(d);
17401740
}
17411741

1742+
template <typename CTX_T>
1743+
t_val
1744+
get_min_max(std::shared_ptr<View<CTX_T>> view, const std::string& colname) {
1745+
t_val arr = t_val::array();
1746+
std::pair<t_tscalar, t_tscalar> min_max = view->get_min_max(colname);
1747+
arr.set(0, scalar_to_val(min_max.first));
1748+
arr.set(1, scalar_to_val(min_max.second));
1749+
return arr;
1750+
}
1751+
17421752
} // end namespace binding
17431753
} // end namespace perspective
17441754

@@ -2214,6 +2224,10 @@ EMSCRIPTEN_BINDINGS(perspective) {
22142224
function("get_from_data_slice_one", &get_from_data_slice<t_ctx1>, allow_raw_pointers());
22152225
function("get_data_slice_two", &get_data_slice<t_ctx2>, allow_raw_pointers());
22162226
function("get_from_data_slice_two", &get_from_data_slice<t_ctx2>, allow_raw_pointers());
2227+
function("get_min_max_unit", &get_min_max<t_ctxunit>);
2228+
function("get_min_max_zero", &get_min_max<t_ctx0>);
2229+
function("get_min_max_one", &get_min_max<t_ctx1>);
2230+
function("get_min_max_two", &get_min_max<t_ctx2>);
22172231
function("to_arrow_unit", &to_arrow<t_ctxunit>);
22182232
function("to_arrow_zero", &to_arrow<t_ctx0>);
22192233
function("to_arrow_one", &to_arrow<t_ctx1>);

cpp/perspective/src/cpp/view.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -390,6 +390,12 @@ View<t_ctx0>::computed_schema() const {
390390
return new_schema;
391391
}
392392

393+
template <typename T>
394+
std::pair<t_tscalar, t_tscalar>
395+
View<T>::get_min_max(const std::string& colname) const {
396+
return m_ctx->get_min_max(colname);
397+
}
398+
393399
template <>
394400
std::shared_ptr<t_data_slice<t_ctxunit>>
395401
View<t_ctxunit>::get_data(

cpp/perspective/src/include/perspective/context_one.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,8 @@ class PERSPECTIVE_EXPORT t_ctx1 : public t_ctxbase<t_ctx1> {
4242

4343
t_depth get_trav_depth(t_index idx) const;
4444

45+
std::pair<t_tscalar, t_tscalar> get_min_max(const std::string& colname) const;
46+
4547
using t_ctxbase<t_ctx1>::get_data;
4648

4749
private:

cpp/perspective/src/include/perspective/context_two.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,8 @@ class PERSPECTIVE_EXPORT t_ctx2 : public t_ctxbase<t_ctx2> {
5050

5151
void set_depth(t_header header, t_depth depth);
5252

53+
std::pair<t_tscalar, t_tscalar> get_min_max(const std::string& colname) const;
54+
5355
using t_ctxbase<t_ctx2>::get_data;
5456

5557
protected:

cpp/perspective/src/include/perspective/context_unit.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,8 @@ class PERSPECTIVE_EXPORT t_ctxunit : public t_ctxbase<t_ctxunit> {
5151

5252
perspective::t_index get_column_count() const;
5353

54+
std::pair<t_tscalar, t_tscalar> get_min_max(const std::string& colname) const;
55+
5456
using t_ctxbase<t_ctxunit>::get_data;
5557

5658
std::vector<t_tscalar> get_data(

cpp/perspective/src/include/perspective/context_zero.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,8 @@ class PERSPECTIVE_EXPORT t_ctx0 : public t_ctxbase<t_ctx0> {
4141
void sort_by();
4242
std::vector<t_sortspec> get_sort_by() const;
4343

44+
std::pair<t_tscalar, t_tscalar> get_min_max(const std::string& colname) const;
45+
4446
using t_ctxbase<t_ctx0>::get_data;
4547

4648
protected:

0 commit comments

Comments
 (0)