@@ -2664,3 +2664,70 @@ def trigger_interrupt():
26642664
26652665 # Make sure the interrupt thread has finished
26662666 interrupt_thread .join (timeout = 1.0 )
2667+
2668+
2669+ def test_show_from_empty_rows (capsys ):
2670+ """Create a DataFrame with a valid schema but zero rows and call show().
2671+
2672+ This verifies that showing an empty-but-schema'd DataFrame does not panic
2673+ and prints a helpful message instead.
2674+ """
2675+ ctx = SessionContext ()
2676+
2677+ # Create a RecordBatch with the correct schema but zero rows
2678+ batch = pa .RecordBatch .from_arrays (
2679+ [pa .array ([], type = pa .int64 ()), pa .array ([], type = pa .string ())],
2680+ names = ["a" , "b" ],
2681+ )
2682+
2683+ df = ctx .create_dataframe ([[batch ]])
2684+
2685+ # Call show() and capture stdout
2686+ df .show ()
2687+ captured = capsys .readouterr ()
2688+
2689+ # Expect either an explicit no-rows message or an empty DataFrame print
2690+ out = captured .out .lower ()
2691+ assert "dataframe has no rows" in out or "dataframe()" in out
2692+
2693+
2694+ def test_select_where_no_rows (capsys ):
2695+ """Create a DataFrame a:[1,2,3], filter with a>4 to produce zero rows and call show().
2696+
2697+ This verifies that a query returning zero rows does not trigger a panic and
2698+ instead prints a helpful message.
2699+ """
2700+ ctx = SessionContext ()
2701+
2702+ batch = pa .RecordBatch .from_arrays (
2703+ [pa .array ([1 , 2 , 3 ], type = pa .int64 ())], names = ["a" ]
2704+ )
2705+
2706+ df = ctx .create_dataframe ([[batch ]])
2707+
2708+ df_filtered = df .filter (column ("a" ) > literal (4 ))
2709+
2710+ df_filtered .show ()
2711+ captured = capsys .readouterr ()
2712+ out = captured .out .lower ()
2713+
2714+ assert "dataframe has no rows" in out or "dataframe()" in out
2715+
2716+
2717+ def test_sql_select_where_no_rows (capsys ):
2718+ """Register a table 't' with a:[1,2,3], run SQL that returns no rows, and call show().
2719+
2720+ Ensures SQL path that returns zero rows doesn't panic when showing results.
2721+ """
2722+ ctx = SessionContext ()
2723+
2724+ batch = pa .RecordBatch .from_arrays ([pa .array ([1 , 2 , 3 ], type = pa .int64 ())], names = ["a" ])
2725+ ctx .register_record_batches ("t" , [[batch ]])
2726+
2727+ df = ctx .sql ("SELECT * FROM t WHERE a > 4" )
2728+
2729+ df .show ()
2730+ captured = capsys .readouterr ()
2731+ out = captured .out .lower ()
2732+
2733+ assert "dataframe has no rows" in out or "dataframe()" in out
0 commit comments