Skip to content

Commit 89f5484

Browse files
committed
Revert "feat: update SessionContext to support dynamic auto-registration of Python objects based on session config"
This reverts commit 207cf95.
1 parent 207cf95 commit 89f5484

File tree

3 files changed

+21
-32
lines changed

3 files changed

+21
-32
lines changed

docs/source/user-guide/dataframe/index.rst

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -228,10 +228,10 @@ Core Classes
228228
* :py:meth:`~datafusion.SessionContext.from_pandas` - Create from Pandas DataFrame
229229
* :py:meth:`~datafusion.SessionContext.from_arrow` - Create from Arrow data
230230

231-
``SessionContext`` can automatically resolve SQL table names that match
232-
in-scope Python data objects. When automatic lookup is enabled, a query
233-
such as ``ctx.sql("SELECT * FROM pdf")`` will register a pandas or
234-
PyArrow object named ``pdf`` without calling
231+
``SessionContext`` automatically resolves SQL table names that match
232+
in-scope Python data objects. When ``auto_register_python_objects`` is
233+
enabled (the default), a query such as ``ctx.sql("SELECT * FROM pdf")``
234+
will register a pandas or PyArrow object named ``pdf`` without calling
235235
:py:meth:`~datafusion.SessionContext.from_pandas` or
236236
:py:meth:`~datafusion.SessionContext.from_arrow` explicitly. This requires
237237
the corresponding library (``pandas`` for pandas objects, ``pyarrow`` for
@@ -242,18 +242,16 @@ Core Classes
242242
import pandas as pd
243243
from datafusion import SessionContext
244244
245-
ctx = SessionContext(auto_register_python_objects=True)
245+
ctx = SessionContext()
246246
pdf = pd.DataFrame({"value": [1, 2, 3]})
247247
248248
df = ctx.sql("SELECT SUM(value) AS total FROM pdf")
249249
print(df.to_pandas()) # automatically registers `pdf`
250250
251-
Automatic lookup is disabled by default. Enable it by passing
252-
``auto_register_python_objects=True`` when constructing the session or by
253-
configuring :py:class:`~datafusion.SessionConfig` with
254-
:py:meth:`~datafusion.SessionConfig.with_python_table_lookup`. Use
255-
:py:meth:`~datafusion.SessionContext.set_python_table_lookup` to toggle the
256-
behaviour at runtime.
251+
To opt out, either pass ``auto_register_python_objects=False`` when
252+
constructing the session, or call
253+
:py:meth:`~datafusion.SessionContext.set_python_table_lookup` with
254+
``False`` to require explicit registration.
257255

258256
See: :py:class:`datafusion.SessionContext`
259257

python/datafusion/context.py

Lines changed: 8 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -503,7 +503,7 @@ def __init__(
503503
config: SessionConfig | None = None,
504504
runtime: RuntimeEnvBuilder | None = None,
505505
*,
506-
auto_register_python_objects: bool | None = None,
506+
auto_register_python_objects: bool = True,
507507
) -> None:
508508
"""Main interface for executing queries with DataFusion.
509509
@@ -516,10 +516,7 @@ def __init__(
516516
runtime: Runtime configuration options.
517517
auto_register_python_objects: Automatically register referenced
518518
Python objects (such as pandas or PyArrow data) when ``sql``
519-
queries reference them by name. When omitted, this defaults to
520-
the value configured via
521-
:py:meth:`~datafusion.SessionConfig.with_python_table_lookup`
522-
(``False`` unless explicitly enabled).
519+
queries reference them by name.
523520
524521
Example usage:
525522
@@ -535,12 +532,6 @@ def __init__(
535532
config.config_internal if config is not None else None,
536533
runtime.config_internal if runtime is not None else None,
537534
)
538-
539-
if auto_register_python_objects is None:
540-
auto_register_python_objects = getattr(
541-
config, "_python_table_lookup", False
542-
)
543-
544535
self._auto_python_table_lookup = auto_register_python_objects
545536

546537
def __repr__(self) -> str:
@@ -569,18 +560,18 @@ def enable_url_table(self) -> SessionContext:
569560
obj = klass.__new__(klass)
570561
obj.ctx = self.ctx.enable_url_table()
571562
obj._auto_python_table_lookup = getattr(
572-
self, "_auto_python_table_lookup", False
563+
self, "_auto_python_table_lookup", True
573564
)
574565
return obj
575566

576567
def set_python_table_lookup(self, enabled: bool = True) -> SessionContext:
577568
"""Enable or disable automatic registration of Python objects in SQL.
578569
579570
Args:
580-
enabled: When ``True``, SQL queries automatically attempt to
581-
resolve missing table names by looking up Python objects in the
582-
caller's scope. Use ``False`` to require explicit registration
583-
of any referenced tables.
571+
enabled: When ``True`` (default), SQL queries automatically attempt
572+
to resolve missing table names by looking up Python objects in
573+
the caller's scope. When ``False``, missing tables will raise an
574+
error unless they have been explicitly registered.
584575
585576
Returns:
586577
The current :py:class:`SessionContext` instance for chaining.
@@ -662,7 +653,7 @@ def _execute_sql() -> DataFrame:
662653
try:
663654
return _execute_sql()
664655
except Exception as err:
665-
if not getattr(self, "_auto_python_table_lookup", False):
656+
if not getattr(self, "_auto_python_table_lookup", True):
666657
raise
667658

668659
missing_tables = self._extract_missing_table_names(err)

python/tests/test_context.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -738,10 +738,10 @@ def test_sql_with_options_no_statements(ctx):
738738
ctx.sql_with_options(sql, options=options)
739739

740740

741-
def test_session_config_python_table_lookup_enables_auto_registration():
741+
def test_sql_auto_register_pandas():
742742
pd = pytest.importorskip("pandas")
743743

744-
ctx = SessionContext(config=SessionConfig().with_python_table_lookup(True))
744+
ctx = SessionContext()
745745
pdf = pd.DataFrame({"value": [1, 2, 3]})
746746
assert len(pdf) == 3
747747

@@ -750,7 +750,7 @@ def test_session_config_python_table_lookup_enables_auto_registration():
750750

751751

752752
def test_sql_auto_register_arrow():
753-
ctx = SessionContext(auto_register_python_objects=True)
753+
ctx = SessionContext()
754754
arrow_table = pa.table({"value": [1, 2, 3, 4]})
755755
assert arrow_table.num_rows == 4
756756

@@ -761,7 +761,7 @@ def test_sql_auto_register_arrow():
761761
def test_sql_auto_register_disabled():
762762
pd = pytest.importorskip("pandas")
763763

764-
ctx = SessionContext()
764+
ctx = SessionContext(auto_register_python_objects=False)
765765
pdf = pd.DataFrame({"value": [1, 2, 3]})
766766
assert len(pdf) == 3
767767

0 commit comments

Comments
 (0)