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 to_columns_string() C++ JSON API #2315

Merged
merged 3 commits into from
Jul 29, 2023
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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -217,3 +217,4 @@ playwright-report/
playwright/.cache/

.pyodide-xbuildenv
benchmark_venv
13 changes: 9 additions & 4 deletions cpp/perspective/src/cpp/emscripten.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1899,7 +1899,8 @@ EMSCRIPTEN_BINDINGS(perspective) {
.function("get_sort", &View<t_ctxunit>::get_sort)
.function("get_step_delta", &View<t_ctxunit>::get_step_delta)
.function("get_column_dtype", &View<t_ctxunit>::get_column_dtype)
.function("is_column_only", &View<t_ctxunit>::is_column_only);
.function("is_column_only", &View<t_ctxunit>::is_column_only)
.function("to_columns", &View<t_ctxunit>::to_columns);

class_<View<t_ctx0>>("View_ctx0")
.constructor<std::shared_ptr<Table>, std::shared_ptr<t_ctx0>,
Expand All @@ -1925,7 +1926,9 @@ EMSCRIPTEN_BINDINGS(perspective) {
.function("get_sort", &View<t_ctx0>::get_sort)
.function("get_step_delta", &View<t_ctx0>::get_step_delta)
.function("get_column_dtype", &View<t_ctx0>::get_column_dtype)
.function("is_column_only", &View<t_ctx0>::is_column_only);
.function("is_column_only", &View<t_ctx0>::is_column_only)
.function("to_columns", &View<t_ctx0>::to_columns);
;

class_<View<t_ctx1>>("View_ctx1")
.constructor<std::shared_ptr<Table>, std::shared_ptr<t_ctx1>,
Expand Down Expand Up @@ -1954,7 +1957,8 @@ EMSCRIPTEN_BINDINGS(perspective) {
.function("get_sort", &View<t_ctx1>::get_sort)
.function("get_step_delta", &View<t_ctx1>::get_step_delta)
.function("get_column_dtype", &View<t_ctx1>::get_column_dtype)
.function("is_column_only", &View<t_ctx1>::is_column_only);
.function("is_column_only", &View<t_ctx1>::is_column_only)
.function("to_columns", &View<t_ctx1>::to_columns);

class_<View<t_ctx2>>("View_ctx2")
.constructor<std::shared_ptr<Table>, std::shared_ptr<t_ctx2>,
Expand Down Expand Up @@ -1984,7 +1988,8 @@ EMSCRIPTEN_BINDINGS(perspective) {
.function("get_row_path", &View<t_ctx2>::get_row_path)
.function("get_step_delta", &View<t_ctx2>::get_step_delta)
.function("get_column_dtype", &View<t_ctx2>::get_column_dtype)
.function("is_column_only", &View<t_ctx2>::is_column_only);
.function("is_column_only", &View<t_ctx2>::is_column_only)
.function("to_columns", &View<t_ctx2>::to_columns);

/******************************************************************************
*
Expand Down
38 changes: 11 additions & 27 deletions cpp/perspective/src/cpp/scalar.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1039,11 +1039,16 @@ t_tscalar::to_string(bool for_expr) const {
auto d = get<t_date>();
ss << "date(" << d.year() << ", " << d.month() << ", "
<< d.day() << ")";

return ss.str();
} else {
ss << get<t_date>();
t_date date_val = get<t_date>();
tm t = date_val.get_tm();
time_t epoch_delta = mktime(&t);
std::chrono::milliseconds timestamp(epoch_delta * 1000);
date::sys_time<std::chrono::milliseconds> ts(timestamp);
return date::format("%F", ts);
}

return ss.str();
} break;
case DTYPE_BOOL: {
ss << std::boolalpha << get<bool>();
Expand All @@ -1063,28 +1068,7 @@ t_tscalar::to_string(bool for_expr) const {
// local time and not UTC.
std::chrono::milliseconds timestamp(to_int64());
date::sys_time<std::chrono::milliseconds> ts(timestamp);
std::time_t temp = std::chrono::system_clock::to_time_t(ts);
std::tm* t = std::localtime(&temp);

// use a mix of strftime and date::format
std::string buffer;
buffer.resize(64);

// write y-m-d h:m in local time into buffer, and if successful
// write the rest of the date, otherwise print the date in UTC.
std::size_t len
= strftime(&buffer[0], buffer.size(), "%Y-%m-%d %H:%M:", t);
if (len > 0) {
buffer.resize(len);
ss << buffer;
ss << date::format(
"%S", ts); // represent second and millisecond
} else {
std::cerr << to_int64() << " failed strftime" << std::endl;
ss << date::format("%Y-%m-%d %H:%M:%S UTC", ts);
}

return ss.str();
return date::format("%F %T", ts);
} break;
case DTYPE_STR: {
if (for_expr) {
Expand Down Expand Up @@ -1595,9 +1579,9 @@ t_tscalar::can_store_inplace(const char* s) {
bool
t_tscalar::is_nan() const {
if (m_type == DTYPE_FLOAT64)
return std::isnan(get<double>());
return std::isnan(get<double>()) || std::isinf(get<double>());
if (m_type == DTYPE_FLOAT32)
return std::isnan(get<float>());
return std::isnan(get<float>()) || std::isinf(get<float>());
return false;
}

Expand Down
Loading
Loading