From 487c06b53034e4dd1c41b29c030ee2861fc401f4 Mon Sep 17 00:00:00 2001 From: Siew Kam Onn Date: Fri, 22 Aug 2025 17:34:08 +0800 Subject: [PATCH 1/5] Add test for showing empty DataFrame and improve print output for empty DataFrames --- python/tests/test_dataframe.py | 7 +++++++ src/dataframe.rs | 11 +++++++---- 2 files changed, 14 insertions(+), 4 deletions(-) diff --git a/python/tests/test_dataframe.py b/python/tests/test_dataframe.py index 1fd99b339..50e9481d5 100644 --- a/python/tests/test_dataframe.py +++ b/python/tests/test_dataframe.py @@ -252,6 +252,13 @@ def test_filter(df): assert result.column(2) == pa.array([5]) +def test_show_empty(df, capsys): + df_empty = df.filter(column("a") > literal(3)) + df_empty.show() + captured = capsys.readouterr() + assert "DataFrame has no rows" in captured.out + + def test_sort(df): df = df.sort(column("b").sort(ascending=False)) diff --git a/src/dataframe.rs b/src/dataframe.rs index 05f665cda..1437f5f82 100644 --- a/src/dataframe.rs +++ b/src/dataframe.rs @@ -998,10 +998,13 @@ impl PyDataFrame { fn print_dataframe(py: Python, df: DataFrame) -> PyDataFusionResult<()> { // Get string representation of record batches let batches = wait_for_future(py, df.collect())??; - let batches_as_string = pretty::pretty_format_batches(&batches); - let result = match batches_as_string { - Ok(batch) => format!("DataFrame()\n{batch}"), - Err(err) => format!("Error: {:?}", err.to_string()), + let result = if batches.is_empty() { + "DataFrame has no rows".to_string() + } else { + match pretty::pretty_format_batches(&batches) { + Ok(batch) => format!("DataFrame()\n{batch}"), + Err(err) => format!("Error: {:?}", err.to_string()), + } }; // Import the Python 'builtins' module to access the print function From 42c5cb821ae80b11b399f745218c653c7eb68381 Mon Sep 17 00:00:00 2001 From: Siew Kam Onn Date: Fri, 22 Aug 2025 18:06:23 +0800 Subject: [PATCH 2/5] Add tests for handling empty DataFrames and zero-row queries --- python/tests/test_dataframe.py | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/python/tests/test_dataframe.py b/python/tests/test_dataframe.py index 50e9481d5..f1b30a849 100644 --- a/python/tests/test_dataframe.py +++ b/python/tests/test_dataframe.py @@ -2664,3 +2664,32 @@ def trigger_interrupt(): # Make sure the interrupt thread has finished interrupt_thread.join(timeout=1.0) + + +def test_show_from_empty_rows(capsys): + """Create a DataFrame with a valid schema but zero rows and call show(). + + This verifies that showing an empty-but-schema'd DataFrame does not panic + and prints a helpful message instead. + """ + # duplicate of test_show_empty; covered elsewhere + pass + + +def test_select_where_no_rows(capsys): + """Create a DataFrame a:[1,2,3], filter with a>4 to produce zero rows and call show(). + + This verifies that a query returning zero rows does not trigger a panic and + instead prints a helpful message. + """ + # duplicate of test_show_empty; covered elsewhere + pass + + +def test_sql_select_where_no_rows(capsys): + """Register a table 't' with a:[1,2,3], run SQL that returns no rows, and call show(). + + Ensures SQL path that returns zero rows doesn't panic when showing results. + """ + # duplicate of test_show_empty; covered elsewhere + pass From d698a98e3de00568ec9e084eeb0d0dc979e8019a Mon Sep 17 00:00:00 2001 From: Siew Kam Onn Date: Fri, 22 Aug 2025 19:15:55 +0800 Subject: [PATCH 3/5] Add tests for showing DataFrames with no rows and improve output messages --- python/tests/test_dataframe.py | 38 +++++++++++----------------------- 1 file changed, 12 insertions(+), 26 deletions(-) diff --git a/python/tests/test_dataframe.py b/python/tests/test_dataframe.py index f1b30a849..e1ce509b6 100644 --- a/python/tests/test_dataframe.py +++ b/python/tests/test_dataframe.py @@ -2665,31 +2665,17 @@ def trigger_interrupt(): # Make sure the interrupt thread has finished interrupt_thread.join(timeout=1.0) +def test_show_select_where_no_rows(capsys) -> None: + ctx = SessionContext() + df = ctx.sql("SELECT 1 WHERE 1=0") + df.show() + out = capsys.readouterr().out + assert "DataFrame has no rows" in out -def test_show_from_empty_rows(capsys): - """Create a DataFrame with a valid schema but zero rows and call show(). - - This verifies that showing an empty-but-schema'd DataFrame does not panic - and prints a helpful message instead. - """ - # duplicate of test_show_empty; covered elsewhere - pass - - -def test_select_where_no_rows(capsys): - """Create a DataFrame a:[1,2,3], filter with a>4 to produce zero rows and call show(). - - This verifies that a query returning zero rows does not trigger a panic and - instead prints a helpful message. - """ - # duplicate of test_show_empty; covered elsewhere - pass - - -def test_sql_select_where_no_rows(capsys): - """Register a table 't' with a:[1,2,3], run SQL that returns no rows, and call show(). - Ensures SQL path that returns zero rows doesn't panic when showing results. - """ - # duplicate of test_show_empty; covered elsewhere - pass +def test_show_from_empty_batch(capsys) -> None: + ctx = SessionContext() + batch = pa.record_batch([pa.array([], type=pa.int32())], names=["a"]) + ctx.create_dataframe([[batch]]).show() + out = capsys.readouterr().out + assert "| a |" in out \ No newline at end of file From 3a8264452095b6d18f9a8e8183e306471e3c4e2e Mon Sep 17 00:00:00 2001 From: Siew Kam Onn Date: Fri, 22 Aug 2025 21:29:59 +0800 Subject: [PATCH 4/5] Fix assertion in test_show_from_empty_batch to ensure proper output for empty DataFrames --- python/tests/test_dataframe.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/python/tests/test_dataframe.py b/python/tests/test_dataframe.py index e1ce509b6..e8624a86c 100644 --- a/python/tests/test_dataframe.py +++ b/python/tests/test_dataframe.py @@ -2678,4 +2678,4 @@ def test_show_from_empty_batch(capsys) -> None: batch = pa.record_batch([pa.array([], type=pa.int32())], names=["a"]) ctx.create_dataframe([[batch]]).show() out = capsys.readouterr().out - assert "| a |" in out \ No newline at end of file + assert "| a |" in out From db4b9ee788d349ae16f520ed7498ed559a78e835 Mon Sep 17 00:00:00 2001 From: Siew Kam Onn Date: Fri, 22 Aug 2025 21:31:13 +0800 Subject: [PATCH 5/5] feat(tests): add a blank line before test_show_select_where_no_rows function for improved readability --- python/tests/test_dataframe.py | 1 + 1 file changed, 1 insertion(+) diff --git a/python/tests/test_dataframe.py b/python/tests/test_dataframe.py index e8624a86c..0cd56219a 100644 --- a/python/tests/test_dataframe.py +++ b/python/tests/test_dataframe.py @@ -2665,6 +2665,7 @@ def trigger_interrupt(): # Make sure the interrupt thread has finished interrupt_thread.join(timeout=1.0) + def test_show_select_where_no_rows(capsys) -> None: ctx = SessionContext() df = ctx.sql("SELECT 1 WHERE 1=0")