Skip to content

Commit d120b2e

Browse files
committed
Merge branch 'main' into oom-1206
2 parents 914f17e + bf22c1d commit d120b2e

File tree

19 files changed

+1294
-770
lines changed

19 files changed

+1294
-770
lines changed

Cargo.lock

Lines changed: 400 additions & 417 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -42,24 +42,24 @@ protoc = ["datafusion-substrait/protoc"]
4242
substrait = ["dep:datafusion-substrait"]
4343

4444
[dependencies]
45-
tokio = { version = "1.45", features = [
45+
tokio = { version = "1.47", features = [
4646
"macros",
4747
"rt",
4848
"rt-multi-thread",
4949
"sync",
5050
] }
51-
pyo3 = { version = "0.24", features = [
51+
pyo3 = { version = "0.25", features = [
5252
"extension-module",
5353
"abi3",
5454
"abi3-py39",
5555
] }
56-
pyo3-async-runtimes = { version = "0.24", features = ["tokio-runtime"] }
56+
pyo3-async-runtimes = { version = "0.25", features = ["tokio-runtime"] }
5757
pyo3-log = "0.12.4"
58-
arrow = { version = "55.1.0", features = ["pyarrow"] }
59-
datafusion = { version = "49.0.2", features = ["avro", "unicode_expressions"] }
60-
datafusion-substrait = { version = "49.0.2", optional = true }
61-
datafusion-proto = { version = "49.0.2" }
62-
datafusion-ffi = { version = "49.0.2" }
58+
arrow = { version = "56", features = ["pyarrow"] }
59+
datafusion = { version = "50", features = ["avro", "unicode_expressions"] }
60+
datafusion-substrait = { version = "50", optional = true }
61+
datafusion-proto = { version = "50" }
62+
datafusion-ffi = { version = "50" }
6363
prost = "0.13.1" # keep in line with `datafusion-substrait`
6464
uuid = { version = "1.18", features = ["v4"] }
6565
mimalloc = { version = "0.1", optional = true, default-features = false, features = [
@@ -79,7 +79,7 @@ log = "0.4.27"
7979

8080
[build-dependencies]
8181
prost-types = "0.13.1" # keep in line with `datafusion-substrait`
82-
pyo3-build-config = "0.24"
82+
pyo3-build-config = "0.25"
8383

8484
[lib]
8585
name = "datafusion_python"

benchmarks/max_cpu_usage.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@
5353

5454
def main(num_rows: int, partitions: int) -> None:
5555
"""Run a simple aggregation after repartitioning.
56-
56+
5757
This function demonstrates basic partitioning concepts using synthetic data.
5858
Real-world performance will depend on your specific data sources, query types,
5959
and system configuration.

docs/source/user-guide/common-operations/windows.rst

Lines changed: 24 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ We'll use the pokemon dataset (from Ritchie Vink) in the following examples.
3131
.. ipython:: python
3232
3333
from datafusion import SessionContext
34-
from datafusion import col
34+
from datafusion import col, lit
3535
from datafusion import functions as f
3636
3737
ctx = SessionContext()
@@ -120,16 +120,14 @@ two preceding rows.
120120

121121
.. ipython:: python
122122
123-
from datafusion.expr import WindowFrame
123+
from datafusion.expr import Window, WindowFrame
124124
125125
df.select(
126126
col('"Name"'),
127127
col('"Speed"'),
128-
f.window("avg",
129-
[col('"Speed"')],
130-
order_by=[col('"Speed"')],
131-
window_frame=WindowFrame("rows", 2, 0)
132-
).alias("Previous Speed")
128+
f.avg(col('"Speed"'))
129+
.over(Window(window_frame=WindowFrame("rows", 2, 0), order_by=[col('"Speed"')]))
130+
.alias("Previous Speed"),
133131
)
134132
135133
Null Treatment
@@ -151,21 +149,27 @@ it's ``Type 2`` column that are null.
151149
152150
from datafusion.common import NullTreatment
153151
154-
df.filter(col('"Type 1"') == lit("Bug")).select(
152+
df.filter(col('"Type 1"') == lit("Bug")).select(
155153
'"Name"',
156154
'"Type 2"',
157-
f.window("last_value", [col('"Type 2"')])
158-
.window_frame(WindowFrame("rows", None, 0))
159-
.order_by(col('"Speed"'))
160-
.null_treatment(NullTreatment.IGNORE_NULLS)
161-
.build()
162-
.alias("last_wo_null"),
163-
f.window("last_value", [col('"Type 2"')])
164-
.window_frame(WindowFrame("rows", None, 0))
165-
.order_by(col('"Speed"'))
166-
.null_treatment(NullTreatment.RESPECT_NULLS)
167-
.build()
168-
.alias("last_with_null")
155+
f.last_value(col('"Type 2"'))
156+
.over(
157+
Window(
158+
window_frame=WindowFrame("rows", None, 0),
159+
order_by=[col('"Speed"')],
160+
null_treatment=NullTreatment.IGNORE_NULLS,
161+
)
162+
)
163+
.alias("last_wo_null"),
164+
f.last_value(col('"Type 2"'))
165+
.over(
166+
Window(
167+
window_frame=WindowFrame("rows", None, 0),
168+
order_by=[col('"Speed"')],
169+
null_treatment=NullTreatment.RESPECT_NULLS,
170+
)
171+
)
172+
.alias("last_with_null"),
169173
)
170174
171175
Aggregate Functions

docs/source/user-guide/data-sources.rst

Lines changed: 34 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -172,10 +172,41 @@ which can lead to a significant performance difference.
172172
df = ctx.table("my_delta_table")
173173
df.show()
174174
175-
Iceberg
176-
-------
175+
Apache Iceberg
176+
--------------
177177

178-
Coming soon!
178+
DataFusion 45.0.0 and later support the ability to register Apache Iceberg tables as table providers through the Custom Table Provider interface.
179+
180+
This requires either the `pyiceberg <https://pypi.org/project/pyiceberg/>`__ library (>=0.10.0) or the `pyiceberg-core <https://pypi.org/project/pyiceberg-core/>`__ library (>=0.5.0).
181+
182+
* The ``pyiceberg-core`` library exposes Iceberg Rust's implementation of the Custom Table Provider interface as python bindings.
183+
* The ``pyiceberg`` library utilizes the ``pyiceberg-core`` python bindings under the hood and provides a native way for Python users to interact with the DataFusion.
184+
185+
.. code-block:: python
186+
187+
from datafusion import SessionContext
188+
from pyiceberg.catalog import load_catalog
189+
import pyarrow as pa
190+
191+
# Load catalog and create/load a table
192+
catalog = load_catalog("catalog", type="in-memory")
193+
catalog.create_namespace_if_not_exists("default")
194+
195+
# Create some sample data
196+
data = pa.table({"x": [1, 2, 3], "y": [4, 5, 6]})
197+
iceberg_table = catalog.create_table("default.test", schema=data.schema)
198+
iceberg_table.append(data)
199+
200+
# Register the table with DataFusion
201+
ctx = SessionContext()
202+
ctx.register_table_provider("test", iceberg_table)
203+
204+
# Query the table using DataFusion
205+
ctx.table("test").show()
206+
207+
208+
Note that the Datafusion integration rely on features from the `Iceberg Rust <https://github.com/apache/iceberg-rust/>`_ implementation instead of the `PyIceberg <https://github.com/apache/iceberg-python/>`_ implementation.
209+
Features that are available in PyIceberg but not yet in Iceberg Rust will not be available when using DataFusion.
179210

180211
Custom Table Provider
181212
---------------------

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

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,56 @@ DataFusion's DataFrame API offers a wide range of operations:
126126
# Drop columns
127127
df = df.drop("temporary_column")
128128
129+
Column Names as Function Arguments
130+
----------------------------------
131+
132+
Some ``DataFrame`` methods accept column names when an argument refers to an
133+
existing column. These include:
134+
135+
* :py:meth:`~datafusion.DataFrame.select`
136+
* :py:meth:`~datafusion.DataFrame.sort`
137+
* :py:meth:`~datafusion.DataFrame.drop`
138+
* :py:meth:`~datafusion.DataFrame.join` (``on`` argument)
139+
* :py:meth:`~datafusion.DataFrame.aggregate` (grouping columns)
140+
141+
See the full function documentation for details on any specific function.
142+
143+
Note that :py:meth:`~datafusion.DataFrame.join_on` expects ``col()``/``column()`` expressions rather than plain strings.
144+
145+
For such methods, you can pass column names directly:
146+
147+
.. code-block:: python
148+
149+
from datafusion import col, functions as f
150+
151+
df.sort('id')
152+
df.aggregate('id', [f.count(col('value'))])
153+
154+
The same operation can also be written with explicit column expressions, using either ``col()`` or ``column()``:
155+
156+
.. code-block:: python
157+
158+
from datafusion import col, column, functions as f
159+
160+
df.sort(col('id'))
161+
df.aggregate(column('id'), [f.count(col('value'))])
162+
163+
Note that ``column()`` is an alias of ``col()``, so you can use either name; the example above shows both in action.
164+
165+
Whenever an argument represents an expression—such as in
166+
:py:meth:`~datafusion.DataFrame.filter` or
167+
:py:meth:`~datafusion.DataFrame.with_column`—use ``col()`` to reference
168+
columns. The comparison and arithmetic operators on ``Expr`` will automatically
169+
convert any non-``Expr`` value into a literal expression, so writing
170+
171+
.. code-block:: python
172+
173+
from datafusion import col
174+
df.filter(col("age") > 21)
175+
176+
is equivalent to using ``lit(21)`` explicitly. Use ``lit()`` (also available
177+
as ``literal()``) when you need to construct a literal expression directly.
178+
129179
Terminal Operations
130180
-------------------
131181

python/datafusion/context.py

Lines changed: 39 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -22,29 +22,31 @@
2222
import warnings
2323
from typing import TYPE_CHECKING, Any, Protocol
2424

25-
import pyarrow as pa
26-
2725
try:
2826
from warnings import deprecated # Python 3.13+
2927
except ImportError:
3028
from typing_extensions import deprecated # Python 3.12
3129

30+
import pyarrow as pa
31+
3232
from datafusion.catalog import Catalog, CatalogProvider, Table
3333
from datafusion.dataframe import DataFrame
34-
from datafusion.expr import Expr, SortExpr, sort_list_to_raw_sort_list
34+
from datafusion.expr import SortKey, sort_list_to_raw_sort_list
3535
from datafusion.record_batch import RecordBatchStream
3636
from datafusion.user_defined import AggregateUDF, ScalarUDF, TableFunction, WindowUDF
3737

3838
from ._internal import RuntimeEnvBuilder as RuntimeEnvBuilderInternal
3939
from ._internal import SessionConfig as SessionConfigInternal
4040
from ._internal import SessionContext as SessionContextInternal
4141
from ._internal import SQLOptions as SQLOptionsInternal
42+
from ._internal import expr as expr_internal
4243

4344
if TYPE_CHECKING:
4445
import pathlib
46+
from collections.abc import Sequence
4547

4648
import pandas as pd
47-
import polars as pl
49+
import polars as pl # type: ignore[import]
4850

4951
from datafusion.plan import ExecutionPlan, LogicalPlan
5052

@@ -553,7 +555,7 @@ def register_listing_table(
553555
table_partition_cols: list[tuple[str, str | pa.DataType]] | None = None,
554556
file_extension: str = ".parquet",
555557
schema: pa.Schema | None = None,
556-
file_sort_order: list[list[Expr | SortExpr]] | None = None,
558+
file_sort_order: Sequence[Sequence[SortKey]] | None = None,
557559
) -> None:
558560
"""Register multiple files as a single table.
559561
@@ -567,23 +569,20 @@ def register_listing_table(
567569
table_partition_cols: Partition columns.
568570
file_extension: File extension of the provided table.
569571
schema: The data source schema.
570-
file_sort_order: Sort order for the file.
572+
file_sort_order: Sort order for the file. Each sort key can be
573+
specified as a column name (``str``), an expression
574+
(``Expr``), or a ``SortExpr``.
571575
"""
572576
if table_partition_cols is None:
573577
table_partition_cols = []
574578
table_partition_cols = self._convert_table_partition_cols(table_partition_cols)
575-
file_sort_order_raw = (
576-
[sort_list_to_raw_sort_list(f) for f in file_sort_order]
577-
if file_sort_order is not None
578-
else None
579-
)
580579
self.ctx.register_listing_table(
581580
name,
582581
str(path),
583582
table_partition_cols,
584583
file_extension,
585584
schema,
586-
file_sort_order_raw,
585+
self._convert_file_sort_order(file_sort_order),
587586
)
588587

589588
def sql(self, query: str, options: SQLOptions | None = None) -> DataFrame:
@@ -808,7 +807,7 @@ def register_parquet(
808807
file_extension: str = ".parquet",
809808
skip_metadata: bool = True,
810809
schema: pa.Schema | None = None,
811-
file_sort_order: list[list[SortExpr]] | None = None,
810+
file_sort_order: Sequence[Sequence[SortKey]] | None = None,
812811
) -> None:
813812
"""Register a Parquet file as a table.
814813
@@ -827,7 +826,9 @@ def register_parquet(
827826
that may be in the file schema. This can help avoid schema
828827
conflicts due to metadata.
829828
schema: The data source schema.
830-
file_sort_order: Sort order for the file.
829+
file_sort_order: Sort order for the file. Each sort key can be
830+
specified as a column name (``str``), an expression
831+
(``Expr``), or a ``SortExpr``.
831832
"""
832833
if table_partition_cols is None:
833834
table_partition_cols = []
@@ -840,9 +841,7 @@ def register_parquet(
840841
file_extension,
841842
skip_metadata,
842843
schema,
843-
[sort_list_to_raw_sort_list(exprs) for exprs in file_sort_order]
844-
if file_sort_order is not None
845-
else None,
844+
self._convert_file_sort_order(file_sort_order),
846845
)
847846

848847
def register_csv(
@@ -1099,7 +1098,7 @@ def read_parquet(
10991098
file_extension: str = ".parquet",
11001099
skip_metadata: bool = True,
11011100
schema: pa.Schema | None = None,
1102-
file_sort_order: list[list[Expr | SortExpr]] | None = None,
1101+
file_sort_order: Sequence[Sequence[SortKey]] | None = None,
11031102
) -> DataFrame:
11041103
"""Read a Parquet source into a :py:class:`~datafusion.dataframe.Dataframe`.
11051104
@@ -1116,19 +1115,17 @@ def read_parquet(
11161115
schema: An optional schema representing the parquet files. If None,
11171116
the parquet reader will try to infer it based on data in the
11181117
file.
1119-
file_sort_order: Sort order for the file.
1118+
file_sort_order: Sort order for the file. Each sort key can be
1119+
specified as a column name (``str``), an expression
1120+
(``Expr``), or a ``SortExpr``.
11201121
11211122
Returns:
11221123
DataFrame representation of the read Parquet files
11231124
"""
11241125
if table_partition_cols is None:
11251126
table_partition_cols = []
11261127
table_partition_cols = self._convert_table_partition_cols(table_partition_cols)
1127-
file_sort_order = (
1128-
[sort_list_to_raw_sort_list(f) for f in file_sort_order]
1129-
if file_sort_order is not None
1130-
else None
1131-
)
1128+
file_sort_order = self._convert_file_sort_order(file_sort_order)
11321129
return DataFrame(
11331130
self.ctx.read_parquet(
11341131
str(path),
@@ -1179,6 +1176,24 @@ def execute(self, plan: ExecutionPlan, partitions: int) -> RecordBatchStream:
11791176
"""Execute the ``plan`` and return the results."""
11801177
return RecordBatchStream(self.ctx.execute(plan._raw_plan, partitions))
11811178

1179+
@staticmethod
1180+
def _convert_file_sort_order(
1181+
file_sort_order: Sequence[Sequence[SortKey]] | None,
1182+
) -> list[list[expr_internal.SortExpr]] | None:
1183+
"""Convert nested ``SortKey`` sequences into raw sort expressions.
1184+
1185+
Each ``SortKey`` can be a column name string, an ``Expr``, or a
1186+
``SortExpr`` and will be converted using
1187+
:func:`datafusion.expr.sort_list_to_raw_sort_list`.
1188+
"""
1189+
# Convert each ``SortKey`` in the provided sort order to the low-level
1190+
# representation expected by the Rust bindings.
1191+
return (
1192+
[sort_list_to_raw_sort_list(f) for f in file_sort_order]
1193+
if file_sort_order is not None
1194+
else None
1195+
)
1196+
11821197
@staticmethod
11831198
def _convert_table_partition_cols(
11841199
table_partition_cols: list[tuple[str, str | pa.DataType]],

0 commit comments

Comments
 (0)