Skip to content

A reactive notebook for Python — run reproducible experiments, query with SQL, execute as a script, deploy as an app, and version with git. Stored as pure Python. All in a modern, AI-native editor.

fix(table): preserve rows when sorting unequal-length columns (#11197)

Sealdot84e55c42595a
diff --git a/marimo/_plugins/ui/_impl/tables/default_table.py b/marimo/_plugins/ui/_impl/tables/default_table.py
index 6a90b96df3..eee53293a7 100644
--- a/marimo/_plugins/ui/_impl/tables/default_table.py
+++ b/marimo/_plugins/ui/_impl/tables/default_table.py
@@ -398,9 +398,18 @@ class DefaultTableManager(TableManager[JsonTableData]):
if isinstance(self.data, dict) and self.is_column_oriented:
# Column-oriented: sort indices, then reorder all columns
- data_dict = cast(dict[str, list[Any]], self.data)
- first_column = next(iter(data_dict.values()))
- num_rows = len(first_column)
+ num_rows = self.get_num_rows()
+ # Match other table managers, we preserve None values
+ data_dict = {
+ col: (
+ values
+ if len(values) == num_rows
+ else list(values) + [None] * (num_rows - len(values))
+ )
+ for col, values in cast(
+ dict[str, Sequence[Any]], self.data
+ ).items()
+ }
indices = list(range(num_rows))
# Apply sorts in reverse order for stable multi-column sorting
diff --git a/tests/_plugins/ui/_impl/tables/test_default_table.py b/tests/_plugins/ui/_impl/tables/test_default_table.py
index bd17ce76e5..e5dd7628e7 100644
--- a/tests/_plugins/ui/_impl/tables/test_default_table.py
+++ b/tests/_plugins/ui/_impl/tables/test_default_table.py
@@ -1,5 +1,6 @@
from __future__ import annotations
+import copy
import datetime
import json
import string
@@ -1218,6 +1219,81 @@ class TestHeterogeneousRowData(unittest.TestCase):
]
+ ("data", "by", "expected"),
+ [
+ (
+ {"a": [2], "b": [10, 20]},
+ [SortArgs(by="a", descending=False)],
+ {"a": [2, None], "b": [10, 20]},
+ ),
+ (
+ {"a": [2], "b": [10, 20]},
+ [SortArgs(by="b", descending=True)],
+ {"a": [None, 2], "b": [20, 10]},
+ ),
+ (
+ {"b": [10, 20], "a": [2]},
+ [SortArgs(by="a", descending=True)],
+ {"b": [10, 20], "a": [2, None]},
+ ),
+ (
+ {"a": [2, 1], "b": [10]},
+ [SortArgs(by="a", descending=False)],
+ {"a": [1, 2], "b": [None, 10]},
+ ),
+ (
+ {"a": [], "b": [10, 20]},
+ [SortArgs(by="b", descending=True)],
+ {"a": [None, None], "b": [20, 10]},
+ ),
+ (
+ {"a": [], "b": [10, 20]},
+ [SortArgs(by="a", descending=False)],
+ {"a": [None, None], "b": [10, 20]},
+ ),
+ (
+ {"a": [], "b": []},
+ [SortArgs(by="a", descending=False)],
+ {"a": [], "b": []},
+ ),
+ (
+ {"a": (2, 1), "b": (10,)},
+ [SortArgs(by="a", descending=False)],
+ {"a": [1, 2], "b": [None, 10]},
+ ),
+ (
+ {"a": [1, 1, None], "b": [20, 10], "c": [0, 1, 2, 3]},
+ [
+ SortArgs(by="a", descending=True),
+ SortArgs(by="b", descending=False),
+ ],
+ {
+ "a": [1, 1, None, None],
+ "b": [10, 20, None, None],
+ "c": [1, 0, 2, 3],
+ },
+ ),
+ (
+ {"a": ["b", 1], "b": [0, 1, 2]},
+ [SortArgs(by="a", descending=False)],
+ {"a": [1, "b", None], "b": [1, 0, 2]},
+ ),
+ ],
+)
+def test_sort_mismatched_column_lengths(
+ data: Any, by: list[SortArgs], expected: dict[str, list[Any]]
+) -> None:
+ original = copy.deepcopy(data)
+ manager = DefaultTableManager(data)
+
+ sorted_manager = manager.sort_values(by)
+
+ assert sorted_manager.data == expected
+ assert sorted_manager.get_num_rows() == manager.get_num_rows()
+ assert data == original
+
+
class TestMismatchedColumnLengths(unittest.TestCase):
"""Column-oriented data where columns have differing lengths."""
diff --git a/tests/_plugins/ui/_impl/test_table.py b/tests/_plugins/ui/_impl/test_table.py
index a38a32f4f9..99015de3fc 100644
--- a/tests/_plugins/ui/_impl/test_table.py
+++ b/tests/_plugins/ui/_impl/test_table.py
@@ -602,6 +602,33 @@ def test_value_with_cell_selection_then_sorting_dict_of_lists() -> None:
assert type(table.value) is list
[email protected]("descending", [False, True])
+def test_search_sort_mismatched_columns_with_pagination(
+ descending: bool,
+) -> None:
+ data = {"a": [2], "b": [10, 20]}
+ table = ui.table(data)
+ expected = [{"a": 2, "b": 10}, {"a": None, "b": 20}]
+ if descending:
+ expected.reverse()
+
+ for page_number, row in enumerate(expected):
+ response = table._search(
+ SearchTableArgs(
+ sort=[SortArgs(by="b", descending=descending)],
+ page_size=1,
+ page_number=page_number,
+ )
+ )
+ assert response.total_rows == 2
+ assert json.loads(response.data) == [row]
+ assert table._convert_value([str(page_number)]) == {
+ column: [value] for column, value in row.items()
+ }
+
+ assert data == {"a": [2], "b": [10, 20]}
+
+
def test_search_sort_nonexistent_columns() -> None:
data = ["banana", "apple", "cherry", "date", "elderberry"]
table = ui.table(data)