File tree Expand file tree Collapse file tree 2 files changed +45
-12
lines changed
Expand file tree Collapse file tree 2 files changed +45
-12
lines changed Original file line number Diff line number Diff line change @@ -710,21 +710,25 @@ def _execute_sql() -> DataFrame:
710710 self .ctx .sql_with_options (query , options .options_internal )
711711 )
712712
713- try :
714- return _execute_sql ()
715- except Exception as err :
716- if not getattr (self , "_auto_python_table_lookup" , False ):
717- raise
713+ auto_lookup_enabled = getattr (self , "_auto_python_table_lookup" , False )
714+
715+ while True :
716+ try :
717+ return _execute_sql ()
718+ except Exception as err :
719+ if not auto_lookup_enabled :
720+ raise
718721
719- missing_tables = self ._extract_missing_table_names (err )
720- if not missing_tables :
721- raise
722+ missing_tables = self ._extract_missing_table_names (err )
723+ if not missing_tables :
724+ raise
722725
723- registered = self ._register_python_tables (missing_tables )
724- if not registered :
725- raise
726+ registered = self ._register_python_tables (missing_tables )
727+ if not registered :
728+ raise
726729
727- return _execute_sql ()
730+ # Retry to allow registering additional tables referenced in the query.
731+ continue
728732
729733 def sql_with_options (self , query : str , options : SQLOptions ) -> DataFrame :
730734 """Create a :py:class:`~datafusion.dataframe.DataFrame` from SQL query text.
Original file line number Diff line number Diff line change @@ -294,6 +294,35 @@ def test_sql_auto_register_arrow_table():
294294 assert result [0 ].column (0 ).to_pylist ()[0 ] == 6
295295
296296
297+ def test_sql_auto_register_multiple_tables_single_query ():
298+ ctx = SessionContext (auto_register_python_objects = True )
299+
300+ customers = pa .Table .from_pydict ( # noqa: F841
301+ {"customer_id" : [1 , 2 ], "name" : ["Alice" , "Bob" ]}
302+ )
303+ orders = pa .Table .from_pydict ( # noqa: F841
304+ {"order_id" : [100 , 200 ], "customer_id" : [1 , 2 ]}
305+ )
306+
307+ result = ctx .sql (
308+ """
309+ SELECT c.customer_id, o.order_id
310+ FROM customers c
311+ JOIN orders o ON c.customer_id = o.customer_id
312+ ORDER BY o.order_id
313+ """
314+ ).collect ()
315+
316+ actual = pa .Table .from_batches (result )
317+ expected = pa .Table .from_pydict (
318+ {"customer_id" : [1 , 2 ], "order_id" : [100 , 200 ]}
319+ )
320+
321+ assert actual .equals (expected )
322+ assert ctx .table_exist ("customers" )
323+ assert ctx .table_exist ("orders" )
324+
325+
297326def test_sql_auto_register_arrow_outer_scope ():
298327 ctx = SessionContext ()
299328 ctx .auto_register_python_variables = True
You can’t perform that action at this time.
0 commit comments