Skip to content

Commit fda23d8

Browse files
authored
Merge pull request #2545 from finos/fix-split-by-date
Fix `split_by` with `date`/`datetime` columns
2 parents c4b5c01 + 7c70021 commit fda23d8

7 files changed

Lines changed: 98 additions & 5 deletions

File tree

cpp/perspective/src/cpp/emscripten.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2141,7 +2141,7 @@ EMSCRIPTEN_BINDINGS(perspective) {
21412141
.function("schema", &View<t_ctxunit>::schema)
21422142
.function("expression_schema", &View<t_ctxunit>::expression_schema)
21432143
.function("column_names", &View<t_ctxunit>::column_names)
2144-
.function("column_paths", &View<t_ctxunit>::column_paths)
2144+
.function("column_paths", &View<t_ctxunit>::column_paths_string)
21452145
.function("_get_deltas_enabled", &View<t_ctxunit>::_get_deltas_enabled)
21462146
.function("_set_deltas_enabled", &View<t_ctxunit>::_set_deltas_enabled)
21472147
.function(
@@ -2172,7 +2172,7 @@ EMSCRIPTEN_BINDINGS(perspective) {
21722172
.function("schema", &View<t_ctx0>::schema)
21732173
.function("expression_schema", &View<t_ctx0>::expression_schema)
21742174
.function("column_names", &View<t_ctx0>::column_names)
2175-
.function("column_paths", &View<t_ctx0>::column_paths)
2175+
.function("column_paths", &View<t_ctx0>::column_paths_string)
21762176
.function("_get_deltas_enabled", &View<t_ctx0>::_get_deltas_enabled)
21772177
.function("_set_deltas_enabled", &View<t_ctx0>::_set_deltas_enabled)
21782178
.function(
@@ -2207,7 +2207,7 @@ EMSCRIPTEN_BINDINGS(perspective) {
22072207
.function("schema", &View<t_ctx1>::schema)
22082208
.function("expression_schema", &View<t_ctx1>::expression_schema)
22092209
.function("column_names", &View<t_ctx1>::column_names)
2210-
.function("column_paths", &View<t_ctx1>::column_paths)
2210+
.function("column_paths", &View<t_ctx1>::column_paths_string)
22112211
.function("_get_deltas_enabled", &View<t_ctx1>::_get_deltas_enabled)
22122212
.function("_set_deltas_enabled", &View<t_ctx1>::_set_deltas_enabled)
22132213
.function(
@@ -2241,7 +2241,7 @@ EMSCRIPTEN_BINDINGS(perspective) {
22412241
.function("schema", &View<t_ctx2>::schema)
22422242
.function("expression_schema", &View<t_ctx2>::expression_schema)
22432243
.function("column_names", &View<t_ctx2>::column_names)
2244-
.function("column_paths", &View<t_ctx2>::column_paths)
2244+
.function("column_paths", &View<t_ctx2>::column_paths_string)
22452245
.function("_get_deltas_enabled", &View<t_ctx2>::_get_deltas_enabled)
22462246
.function("_set_deltas_enabled", &View<t_ctx2>::_set_deltas_enabled)
22472247
.function(

cpp/perspective/src/cpp/view.cpp

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -270,6 +270,25 @@ View<CTX_T>::column_paths() const {
270270
return names;
271271
}
272272

273+
template <typename CTX_T>
274+
std::vector<std::vector<std::string>>
275+
View<CTX_T>::column_paths_string() const {
276+
auto paths = column_paths();
277+
std::vector<std::vector<std::string>> out;
278+
out.reserve(paths.size());
279+
for (const auto& path : paths) {
280+
std::vector<std::string> row;
281+
row.reserve(path.size());
282+
for (const auto& c : path) {
283+
row.push_back(c.to_string());
284+
}
285+
286+
out.push_back(row);
287+
}
288+
289+
return out;
290+
}
291+
273292
template <typename CTX_T>
274293
std::map<std::string, std::string>
275294
View<CTX_T>::schema() const {

cpp/perspective/src/include/perspective/view.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,8 @@ class PERSPECTIVE_EXPORT View {
125125
*/
126126
std::vector<std::vector<t_tscalar>> column_paths() const;
127127

128+
std::vector<std::vector<std::string>> column_paths_string() const;
129+
128130
/**
129131
* @brief
130132
*

packages/perspective-viewer-datagrid/test/js/superstore.spec.js

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,4 +62,30 @@ test.describe("Datagrid with superstore data set", () => {
6262
"row-headers-are-printed-correctly"
6363
);
6464
});
65+
66+
test("Column headers are printed correctly, split_by a date column", async ({
67+
page,
68+
}) => {
69+
await page.goto("/tools/perspective-test/src/html/basic-test.html");
70+
await page.evaluate(async () => {
71+
while (!window["__TEST_PERSPECTIVE_READY__"]) {
72+
await new Promise((x) => setTimeout(x, 10));
73+
}
74+
});
75+
76+
await page.evaluate(async () => {
77+
await document.querySelector("perspective-viewer").restore({
78+
plugin: "Datagrid",
79+
columns: ["Sales", "Profit"],
80+
group_by: ["State"],
81+
split_by: ["New Col"],
82+
expressions: { "New Col": "bucket(\"Order Date\",'Y')" },
83+
});
84+
});
85+
86+
compareContentsToSnapshot(
87+
await getDatagridContents(page),
88+
"column-headers-are-printed-correctly-split-by-a-date-column"
89+
);
90+
});
6591
});

packages/perspective/src/js/perspective.js

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -324,6 +324,25 @@ export default function (Module) {
324324
return extracted;
325325
};
326326

327+
const extract_vector_string = function (vector) {
328+
// handles deletion already - do not call delete() on the input vector
329+
// again
330+
let extracted = [];
331+
for (let i = 0; i < vector.size(); i++) {
332+
let item = vector.get(i);
333+
let row = [];
334+
for (let i = 0; i < item.size(); i++) {
335+
let s = item.get(i);
336+
row.push(s);
337+
}
338+
339+
item.delete();
340+
extracted.push(row);
341+
}
342+
vector.delete();
343+
return extracted;
344+
};
345+
327346
/**
328347
* The schema of this {@link module:perspective~view}.
329348
*
@@ -420,7 +439,7 @@ export default function (Module) {
420439
* @returns {Array<String>} an Array of Strings containing the column paths.
421440
*/
422441
view.prototype.column_paths = function () {
423-
return extract_vector_scalar(this._View.column_paths()).map((x) =>
442+
return extract_vector_string(this._View.column_paths()).map((x) =>
424443
x.join(defaults.COLUMN_SEPARATOR_STRING)
425444
);
426445
};

packages/perspective/test/js/pivots.spec.js

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3082,5 +3082,32 @@ const std = (nums) => {
30823082
view.delete();
30833083
table.delete();
30843084
});
3085+
3086+
test("Should format date columns in split_by", async function () {
3087+
const table = await perspective.table({
3088+
w: "float",
3089+
x: "integer",
3090+
y: "string",
3091+
z: "date",
3092+
});
3093+
3094+
await table.update(data_8);
3095+
const view = await table.view({ group_by: ["y"], split_by: ["z"] });
3096+
const paths = await view.column_paths();
3097+
expect(paths).toEqual([
3098+
"__ROW_PATH__",
3099+
"2019-04-11|w",
3100+
"2019-04-11|x",
3101+
"2019-04-11|y",
3102+
"2019-04-11|z",
3103+
"2019-04-13|w",
3104+
"2019-04-13|x",
3105+
"2019-04-13|y",
3106+
"2019-04-13|z",
3107+
]);
3108+
3109+
view.delete();
3110+
table.delete();
3111+
});
30853112
});
30863113
})(perspective);
1.26 KB
Binary file not shown.

0 commit comments

Comments
 (0)