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

Fix segfault and errors on 2-sided sorted views when data window is invalid #1153

Merged
merged 3 commits into from
Aug 26, 2020
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
43 changes: 32 additions & 11 deletions cpp/perspective/src/cpp/view.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -379,21 +379,42 @@ View<t_ctx2>::get_data(
* Perspective generates headers for sorted columns, so we have to
* skip them in the underlying slice.
*/
auto depth = m_column_pivots.size();
auto col_length = m_ctx->unity_get_column_count();
column_indices.push_back(0);
for (t_uindex i = 0; i < col_length; ++i) {
if (m_ctx->unity_get_column_path(i + 1).size() == depth) {
column_indices.push_back(i + 1);
t_uindex start_col_index = start_col;
t_uindex end_col_index = end_col;

// Only construct column_indices if start_col > end_col - get_data will
// handle the incorrect data window properly, which is consistent
// with the implementation for when the context is not sorted.
if (start_col < end_col) {
auto depth = m_column_pivots.size();
auto col_length = m_ctx->unity_get_column_count();
column_indices.push_back(0);
for (t_uindex i = 0; i < col_length; ++i) {
if (m_ctx->unity_get_column_path(i + 1).size() == depth) {
column_indices.push_back(i + 1);
}
}
}

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()));
cols = column_names(true, depth);

// Filter down column indices by user-provided start/end columns
column_indices = std::vector<t_uindex>(
column_indices.begin() + start_col,
column_indices.begin() + std::min(end_col, (t_uindex)column_indices.size())
);

// If start_col == end_col, then column_indices will be an empty
// vector. Only try to access the first and last elements if the
// vector is not empty. `get_data` correctly handles cases where
// start == end and start > end.
if (column_indices.size() > 0) {
start_col_index = column_indices.front();
end_col_index = column_indices.back() + 1;
}
}

std::vector<t_tscalar> slice_with_headers = m_ctx->get_data(
start_row, end_row, column_indices.front(), column_indices.back() + 1);
start_row, end_row, start_col_index, end_col_index);

auto iter = slice_with_headers.begin();
while (iter != slice_with_headers.end()) {
Expand Down
175 changes: 175 additions & 0 deletions python/perspective/perspective/tests/table/test_to_format.py
Original file line number Diff line number Diff line change
Expand Up @@ -528,6 +528,26 @@ def test_to_records_zero_over_max_col(self):
)
assert records == data

def test_to_records_zero_start_gt_end_col(self):
data = [{"a": 1.5, "b": 2.5}, {"a": 3.5, "b": 4.5}]
tbl = Table(data)
view = tbl.view()
records = view.to_records(
start_col=2,
end_col=1
)
assert records == [{}, {}]

def test_to_records_zero_start_eq_end_col(self):
data = [{"a": 1.5, "b": 2.5}, {"a": 3.5, "b": 4.5}]
tbl = Table(data)
view = tbl.view()
records = view.to_records(
start_col=1,
end_col=1
)
assert records == [{}, {}]

def test_to_records_one_over_max_col(self):
data = [{"a": 1.5, "b": 2.5}, {"a": 3.5, "b": 4.5}]
tbl = Table(data)
Expand All @@ -543,6 +563,42 @@ def test_to_records_one_over_max_col(self):
{'__ROW_PATH__': [3.5], 'a': 3.5, 'b': 4.5}
]

def test_to_records_one_start_gt_end_col(self):
data = [{"a": 1.5, "b": 2.5}, {"a": 3.5, "b": 4.5}]
tbl = Table(data)
view = tbl.view(
row_pivots=["a"]
)
records = view.to_records(
start_col=2,
end_col=1
)
assert records == [{}, {}, {}]

def test_to_records_one_start_gt_end_col_large(self):
data = [{"a": 1.5, "b": 2.5}, {"a": 3.5, "b": 4.5}]
tbl = Table(data)
view = tbl.view(
row_pivots=["a"]
)
records = view.to_records(
start_col=20,
end_col=19
)
assert records == [{}, {}, {}]

def test_to_records_one_start_eq_end_col(self):
data = [{"a": 1.5, "b": 2.5}, {"a": 3.5, "b": 4.5}]
tbl = Table(data)
view = tbl.view(
row_pivots=["a"]
)
records = view.to_records(
start_col=0,
end_col=0
)
assert records == [{}, {}, {}]

def test_to_records_two_over_max_col(self):
data = [{"a": 1, "b": 2}, {"a": 3, "b": 4}]
tbl = Table(data)
Expand Down Expand Up @@ -594,6 +650,125 @@ def test_to_records_two_end_col(self):
{'2|a': None, '2|b': None, '4|a': 3, '4|b': 4, '__ROW_PATH__': [3]}
]

def test_to_records_two_start_gt_end_col(self):
data = [{"a": 1, "b": 2}, {"a": 3, "b": 4}]
tbl = Table(data)
view = tbl.view(
row_pivots=["a"],
column_pivots=["b"]
)
records = view.to_records(
end_row=12,
start_col=5,
end_col=4
)
assert records == [{}, {}, {}]

def test_to_records_two_start_gt_end_col_large_overage(self):
data = [{"a": 1, "b": 2}, {"a": 3, "b": 4}]
tbl = Table(data)
view = tbl.view(
row_pivots=["a"],
column_pivots=["b"]
)
records = view.to_records(
end_row=12,
start_col=50,
end_col=49
)
assert records == [{}, {}, {}]

def test_to_records_two_start_end_col_equiv(self):
data = [{"a": 1, "b": 2}, {"a": 3, "b": 4}]
tbl = Table(data)
view = tbl.view(
row_pivots=["a"],
column_pivots=["b"]
)
records = view.to_records(
end_row=12,
start_col=5,
end_col=5
)
assert records == [{}, {}, {}]


def test_to_records_two_sorted_start_gt_end_col(self):
data = [{"a": 1, "b": 2}, {"a": 3, "b": 4}]
tbl = Table(data)
view = tbl.view(
row_pivots=["a"],
column_pivots=["b"],
sort=[["a", "desc"]]
)
records = view.to_records(
end_row=12,
start_col=5,
end_col=4
)
assert records == [{}, {}, {}]

def test_to_records_two_sorted_start_gt_end_col_large_overage(self):
data = [{"a": 1, "b": 2}, {"a": 3, "b": 4}]
tbl = Table(data)
view = tbl.view(
row_pivots=["a"],
column_pivots=["b"],
sort=[["a", "desc"]]
)
records = view.to_records(
end_row=12,
start_col=20,
end_col=30
)
assert records == [{}, {}, {}]

def test_to_records_two_sorted_start_gt_end_col_overage(self):
data = [{"a": 1, "b": 2}, {"a": 3, "b": 4}]
tbl = Table(data)
view = tbl.view(
columns=[],
row_pivots=["a"],
column_pivots=["b"],
sort=[["a", "desc"]]
)
records = view.to_records(
end_row=12,
start_col=1,
end_col=3
)
assert records == [{}, {}, {}]

def test_to_records_two_sorted_start_end_col(self):
data = [{"a": 1, "b": 2}, {"a": 3, "b": 4}]
tbl = Table(data)
view = tbl.view(
row_pivots=["a"],
column_pivots=["b"],
sort=[["a", "desc"]]
)
records = view.to_records(
start_col=1,
end_col=2
)
assert records == [{"__ROW_PATH__": []}, {"__ROW_PATH__": [3]}, {"__ROW_PATH__": [1]}]


def test_to_records_two_sorted_start_end_col_equiv(self):
data = [{"a": 1, "b": 2}, {"a": 3, "b": 4}]
tbl = Table(data)
view = tbl.view(
row_pivots=["a"],
column_pivots=["b"],
sort=[["a", "desc"]]
)
records = view.to_records(
end_row=12,
start_col=5,
end_col=5
)
assert records == [{}, {}, {}]

def test_to_records_start_col_end_col(self):
data = [{"a": 1, "b": 2, "c": 3}, {"a": 3, "b": 4, "c": 5}]
tbl = Table(data)
Expand Down