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

Refactored data_slice to return t_tscalar #541

Merged
merged 2 commits into from
Apr 13, 2019
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
7 changes: 7 additions & 0 deletions cpp/perspective/src/cpp/aggspec.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,13 @@ t_aggspec::name() const {
return m_name;
}

t_tscalar
t_aggspec::name_scalar() const {
t_tscalar s;
s.set(m_name.c_str());
return s;
}

std::string
t_aggspec::disp_name() const {
return m_disp_name;
Expand Down
11 changes: 11 additions & 0 deletions cpp/perspective/src/cpp/context_one.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,17 @@ t_ctx1::get_aggregate(t_uindex idx) const {
return m_config.get_aggregates()[idx];
}

t_tscalar
t_ctx1::get_aggregate_name(t_uindex idx) const {
PSP_TRACE_SENTINEL();
PSP_VERBOSE_ASSERT(m_init, "touching uninited object");
t_tscalar s;
if (idx >= m_config.get_num_aggregates())
return s;
s.set(m_config.get_aggregates()[idx].name_scalar());
return s;
}

std::vector<t_aggspec>
t_ctx1::get_aggregates() const {
PSP_TRACE_SENTINEL();
Expand Down
11 changes: 11 additions & 0 deletions cpp/perspective/src/cpp/context_two.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -541,6 +541,17 @@ t_ctx2::get_aggregates() const {
return m_config.get_aggregates();
}

t_tscalar
t_ctx2::get_aggregate_name(t_uindex idx) const {
PSP_TRACE_SENTINEL();
PSP_VERBOSE_ASSERT(m_init, "touching uninited object");
t_tscalar s;
if (idx >= m_config.get_num_aggregates())
return s;
s.set(m_config.get_aggregates()[idx].name_scalar());
return s;
}

void
t_ctx2::set_depth(t_header header, t_depth depth) {
t_depth new_depth;
Expand Down
6 changes: 3 additions & 3 deletions cpp/perspective/src/cpp/data_slice.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ template <typename CTX_T>
t_data_slice<CTX_T>::t_data_slice(std::shared_ptr<CTX_T> ctx, t_uindex start_row,
t_uindex end_row, t_uindex start_col, t_uindex end_col, t_uindex row_offset,
t_uindex col_offset, const std::shared_ptr<std::vector<t_tscalar>>& slice,
std::vector<std::string> column_names)
std::vector<std::vector<t_tscalar>> column_names)
: m_ctx(ctx)
, m_start_row(start_row)
, m_end_row(end_row)
Expand All @@ -33,7 +33,7 @@ template <typename CTX_T>
t_data_slice<CTX_T>::t_data_slice(std::shared_ptr<CTX_T> ctx, t_uindex start_row,
t_uindex end_row, t_uindex start_col, t_uindex end_col, t_uindex row_offset,
t_uindex col_offset, const std::shared_ptr<std::vector<t_tscalar>>& slice,
std::vector<std::string> column_names, std::vector<t_uindex> column_indices)
std::vector<std::vector<t_tscalar>> column_names, std::vector<t_uindex> column_indices)
: m_ctx(ctx)
, m_start_row(start_row)
, m_end_row(end_row)
Expand Down Expand Up @@ -85,7 +85,7 @@ t_data_slice<CTX_T>::get_slice() const {
}

template <typename CTX_T>
const std::vector<std::string>&
const std::vector<std::vector<t_tscalar>>&
t_data_slice<CTX_T>::get_column_names() const {
return m_column_names;
}
Expand Down
35 changes: 27 additions & 8 deletions cpp/perspective/src/cpp/emscripten.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -322,7 +322,7 @@ namespace binding {
* val
*/
val
scalar_to_val(const t_tscalar& scalar, bool cast_double) {
scalar_to_val(const t_tscalar& scalar, bool cast_double, bool cast_string) {
if (!scalar.is_valid()) {
return val::null();
}
Expand All @@ -334,7 +334,19 @@ namespace binding {
return val(false);
}
}
case DTYPE_TIME:
case DTYPE_TIME: {
if (cast_double) {
auto x = scalar.to_uint64();
double y = *reinterpret_cast<double*>(&x);
return val(y);
} else if (cast_string) {
double ms = scalar.to_double();
emscripten::val date = val::global("Date").new_(ms);
return date.call<val>("toLocaleString");
} else {
return val(scalar.to_double());
}
}
case DTYPE_FLOAT64:
case DTYPE_FLOAT32: {
if (cast_double) {
Expand Down Expand Up @@ -377,6 +389,11 @@ namespace binding {
return scalar_to_val(scalars[idx]);
}

val
scalar_vec_to_string(const std::vector<t_tscalar>& scalars, std::uint32_t idx) {
return scalar_to_val(scalars[idx], false, true);
}

template <typename T, typename U>
std::vector<U>
vecFromArray(T& arr) {
Expand Down Expand Up @@ -1960,7 +1977,7 @@ EMSCRIPTEN_BINDINGS(perspective) {
.function("num_columns", &View<t_ctx0>::num_columns)
.function("get_row_expanded", &View<t_ctx0>::get_row_expanded)
.function("schema", &View<t_ctx0>::schema)
.function("_column_names", &View<t_ctx0>::_column_names)
.function("column_names", &View<t_ctx0>::column_names)
.function("_get_deltas_enabled", &View<t_ctx0>::_get_deltas_enabled)
.function("_set_deltas_enabled", &View<t_ctx0>::_set_deltas_enabled)
.function("get_context", &View<t_ctx0>::get_context, allow_raw_pointers())
Expand All @@ -1985,7 +2002,7 @@ EMSCRIPTEN_BINDINGS(perspective) {
.function("collapse", &View<t_ctx1>::collapse)
.function("set_depth", &View<t_ctx1>::set_depth)
.function("schema", &View<t_ctx1>::schema)
.function("_column_names", &View<t_ctx1>::_column_names)
.function("column_names", &View<t_ctx1>::column_names)
.function("_get_deltas_enabled", &View<t_ctx1>::_get_deltas_enabled)
.function("_set_deltas_enabled", &View<t_ctx1>::_set_deltas_enabled)
.function("get_context", &View<t_ctx1>::get_context, allow_raw_pointers())
Expand All @@ -2010,7 +2027,7 @@ EMSCRIPTEN_BINDINGS(perspective) {
.function("collapse", &View<t_ctx2>::collapse)
.function("set_depth", &View<t_ctx2>::set_depth)
.function("schema", &View<t_ctx2>::schema)
.function("_column_names", &View<t_ctx2>::_column_names)
.function("column_names", &View<t_ctx2>::column_names)
.function("_get_deltas_enabled", &View<t_ctx2>::_get_deltas_enabled)
.function("_set_deltas_enabled", &View<t_ctx2>::_set_deltas_enabled)
.function("get_context", &View<t_ctx2>::get_context, allow_raw_pointers())
Expand Down Expand Up @@ -2060,18 +2077,18 @@ EMSCRIPTEN_BINDINGS(perspective) {
*/
class_<t_data_slice<t_ctx0>>("t_data_slice_ctx0")
.smart_ptr<std::shared_ptr<t_data_slice<t_ctx0>>>("shared_ptr<t_data_slice<t_ctx0>>>")
.function<const std::vector<std::string>&>(
.function<const std::vector<std::vector<t_tscalar>>&>(
"get_column_names", &t_data_slice<t_ctx0>::get_column_names);

class_<t_data_slice<t_ctx1>>("t_data_slice_ctx1")
.smart_ptr<std::shared_ptr<t_data_slice<t_ctx1>>>("shared_ptr<t_data_slice<t_ctx1>>>")
.function<const std::vector<std::string>&>(
.function<const std::vector<std::vector<t_tscalar>>&>(
"get_column_names", &t_data_slice<t_ctx1>::get_column_names)
.function<std::vector<t_tscalar>>("get_row_path", &t_data_slice<t_ctx1>::get_row_path);

class_<t_data_slice<t_ctx2>>("t_data_slice_ctx2")
.smart_ptr<std::shared_ptr<t_data_slice<t_ctx2>>>("shared_ptr<t_data_slice<t_ctx2>>>")
.function<const std::vector<std::string>&>(
.function<const std::vector<std::vector<t_tscalar>>&>(
"get_column_names", &t_data_slice<t_ctx2>::get_column_names)
.function<std::vector<t_tscalar>>("get_row_path", &t_data_slice<t_ctx2>::get_row_path);

Expand Down Expand Up @@ -2152,6 +2169,7 @@ EMSCRIPTEN_BINDINGS(perspective) {
register_vector<t_dtype>("std::vector<t_dtype>");
register_vector<t_cellupd>("std::vector<t_cellupd>");
register_vector<t_tscalar>("std::vector<t_tscalar>");
register_vector<std::vector<t_tscalar>>("std::vector<std::vector<t_tscalar>>");
register_vector<std::string>("std::vector<std::string>");
register_vector<t_updctx>("std::vector<t_updctx>");
register_vector<t_uindex>("std::vector<t_uindex>");
Expand Down Expand Up @@ -2198,6 +2216,7 @@ EMSCRIPTEN_BINDINGS(perspective) {
function("make_table", &make_table<val>, allow_raw_pointers());
function("clone_gnode_table", &clone_gnode_table<val>, allow_raw_pointers());
function("scalar_vec_to_val", &scalar_vec_to_val);
function("scalar_vec_to_string", &scalar_vec_to_string);
function("table_add_computed_column", &table_add_computed_column<val>);
function("col_to_js_typed_array_zero", &col_to_js_typed_array<t_ctx0>);
function("col_to_js_typed_array_one", &col_to_js_typed_array<t_ctx1>);
Expand Down
82 changes: 34 additions & 48 deletions cpp/perspective/src/cpp/view.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -105,9 +105,9 @@ View<t_ctx2>::num_columns() const {

// Metadata construction
template <typename CTX_T>
std::vector<std::string>
View<CTX_T>::_column_names(bool skip, std::int32_t depth) const {
std::vector<std::string> names;
std::vector<std::vector<t_tscalar>>
View<CTX_T>::column_names(bool skip, std::int32_t depth) const {
std::vector<std::vector<t_tscalar>> names;
std::vector<std::string> aggregate_names;

const std::vector<t_aggspec> aggs = m_ctx->get_aggregates();
Expand All @@ -116,7 +116,6 @@ View<CTX_T>::_column_names(bool skip, std::int32_t depth) const {
}

for (t_uindex key = 0, max = m_ctx->unity_get_column_count(); key != max; ++key) {
std::stringstream col_name;
std::string name = aggregate_names[key % aggregate_names.size()];

if (name == "psp_okey") {
Expand All @@ -128,43 +127,30 @@ View<CTX_T>::_column_names(bool skip, std::int32_t depth) const {
continue;
}

std::vector<t_tscalar> new_path;
for (auto path = col_path.rbegin(); path != col_path.rend(); ++path) {
std::string path_name = path->to_string();
// ensure that boolean columns are correctly represented
if (path->get_dtype() == DTYPE_BOOL) {
if (path_name == "0") {
col_name << "false";
} else {
col_name << "true";
}
} else {
col_name << path_name;
}
col_name << m_separator;
new_path.push_back(*path);
}

col_name << name;
names.push_back(col_name.str());
new_path.push_back(m_ctx->get_aggregate_name(key % aggregate_names.size()));
names.push_back(new_path);
}

return names;
}

template <>
std::vector<std::string>
View<t_ctx0>::_column_names(bool skip, std::int32_t depth) const {
std::vector<std::string> names;
std::vector<std::string> aggregate_names = m_ctx->get_column_names();
std::vector<std::vector<t_tscalar>>
View<t_ctx0>::column_names(bool skip, std::int32_t depth) const {
std::vector<std::vector<t_tscalar>> names;

for (t_uindex key = 0, max = m_ctx->unity_get_column_count(); key != max; ++key) {
std::stringstream col_name;

col_name << aggregate_names[key];
if (col_name.str() == "psp_okey") {
t_tscalar name = m_ctx->get_column_name(key);
if (name.to_string() == "psp_okey") {
continue;
};

names.push_back(col_name.str());
std::vector<t_tscalar> col_path;
col_path.push_back(name);
names.push_back(col_path);
}

return names;
Expand All @@ -184,12 +170,10 @@ View<CTX_T>::schema() const {
types[names[i]] = _types[i];
}

auto col_names = _column_names(false);
for (const std::string& name : col_names) {
auto col_names = column_names(false);
for (const std::vector<t_tscalar>& name : col_names) {
// Pull out the main aggregate column
std::size_t last_delimiter = name.find_last_of(m_separator);
std::string agg_name = name.substr(last_delimiter + 1);

std::string agg_name = name.back().to_string();
std::string type_string = dtype_to_str(types[agg_name]);
new_schema[agg_name] = type_string;

Expand All @@ -213,11 +197,11 @@ View<t_ctx0>::schema() const {
types[names[i]] = _types[i];
}

std::vector<std::string> column_names = _column_names(false);
std::vector<std::vector<t_tscalar>> cols = column_names(false);
std::map<std::string, std::string> new_schema;

for (std::size_t i = 0, max = column_names.size(); i != max; ++i) {
std::string name = column_names[i];
for (std::size_t i = 0, max = cols.size(); i != max; ++i) {
std::string name = cols[i].back().to_string();
if (name == "psp_okey") {
continue;
}
Expand All @@ -233,7 +217,7 @@ View<t_ctx0>::get_data(
t_uindex start_row, t_uindex end_row, t_uindex start_col, t_uindex end_col) {
auto slice_ptr = std::make_shared<std::vector<t_tscalar>>(
m_ctx->get_data(start_row, end_row, start_col, end_col));
auto col_names = _column_names();
auto col_names = column_names();
auto data_slice_ptr = std::make_shared<t_data_slice<t_ctx0>>(m_ctx, start_row, end_row,
start_col, end_col, m_row_offset, m_col_offset, slice_ptr, col_names);
return data_slice_ptr;
Expand All @@ -245,8 +229,10 @@ View<t_ctx1>::get_data(
t_uindex start_row, t_uindex end_row, t_uindex start_col, t_uindex end_col) {
auto slice_ptr = std::make_shared<std::vector<t_tscalar>>(
m_ctx->get_data(start_row, end_row, start_col, end_col));
auto col_names = _column_names();
col_names.insert(col_names.begin(), "__ROW_PATH__");
auto col_names = column_names();
t_tscalar row_path;
row_path.set("__ROW_PATH__");
col_names.insert(col_names.begin(), std::vector<t_tscalar>{row_path});
auto data_slice_ptr = std::make_shared<t_data_slice<t_ctx1>>(m_ctx, start_row, end_row,
start_col, end_col, m_row_offset, m_col_offset, slice_ptr, col_names);
return data_slice_ptr;
Expand All @@ -258,7 +244,7 @@ View<t_ctx2>::get_data(
t_uindex start_row, t_uindex end_row, t_uindex start_col, t_uindex end_col) {
std::vector<t_tscalar> slice;
std::vector<t_uindex> column_indices;
std::vector<std::string> column_names;
std::vector<std::vector<t_tscalar>> cols;
bool is_sorted = m_sorts.size() > 0;

if (is_column_only()) {
Expand All @@ -280,7 +266,7 @@ View<t_ctx2>::get_data(
}
}

column_names = _column_names(true, depth);
cols = column_names(true, depth);
column_indices = std::vector<t_uindex>(column_indices.begin() + start_col,
column_indices.begin() + std::min(end_col, (t_uindex)column_indices.size()));

Expand All @@ -300,15 +286,15 @@ View<t_ctx2>::get_data(
iter++;
}
} else {
column_names = _column_names();
cols = column_names();
slice = m_ctx->get_data(start_row, end_row, start_col, end_col);
}

column_names.insert(column_names.begin(), "__ROW_PATH__");
t_tscalar row_path;
row_path.set("__ROW_PATH__");
cols.insert(cols.begin(), std::vector<t_tscalar>{row_path});
auto slice_ptr = std::make_shared<std::vector<t_tscalar>>(slice);
auto data_slice_ptr
= std::make_shared<t_data_slice<t_ctx2>>(m_ctx, start_row, end_row, start_col, end_col,
m_row_offset, m_col_offset, slice_ptr, column_names, column_indices);
auto data_slice_ptr = std::make_shared<t_data_slice<t_ctx2>>(m_ctx, start_row, end_row,
start_col, end_col, m_row_offset, m_col_offset, slice_ptr, cols, column_indices);
return data_slice_ptr;
}

Expand Down
1 change: 1 addition & 0 deletions cpp/perspective/src/include/perspective/aggspec.h
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ class PERSPECTIVE_EXPORT t_aggspec {
t_uindex agg_one_idx, t_uindex agg_two_idx, double agg_one_weight,
double agg_two_weight);
std::string name() const;
t_tscalar name_scalar() const;
std::string disp_name() const;
t_aggtype agg() const;
std::string agg_str() const;
Expand Down
1 change: 1 addition & 0 deletions cpp/perspective/src/include/perspective/context_one.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ class PERSPECTIVE_EXPORT t_ctx1 : public t_ctxbase<t_ctx1> {
t_index close(t_index idx);

t_aggspec get_aggregate(t_uindex idx) const;
t_tscalar get_aggregate_name(t_uindex idx) const;
std::vector<t_aggspec> get_aggregates() const;
std::vector<t_tscalar> get_row_path(t_index idx) const;
void set_depth(t_depth depth);
Expand Down
1 change: 1 addition & 0 deletions cpp/perspective/src/include/perspective/context_two.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ class PERSPECTIVE_EXPORT t_ctx2 : public t_ctxbase<t_ctx2> {
std::vector<t_tscalar> get_column_path_userspace(t_index idx) const;

std::vector<t_aggspec> get_aggregates() const;
t_tscalar get_aggregate_name(t_uindex idx) const;

void column_sort_by(const std::vector<t_sortspec>& sortby);

Expand Down
Loading