-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
/
iceberg.py
307 lines (244 loc) · 8.8 KB
/
iceberg.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
from __future__ import annotations
import ast
from _ast import GtE, Lt, LtE
from ast import (
Attribute,
BinOp,
BitAnd,
BitOr,
Call,
Compare,
Constant,
Eq,
Gt,
Invert,
List,
Name,
UnaryOp,
)
from functools import partial, singledispatch
from typing import TYPE_CHECKING, Any, Callable
import polars._reexport as pl
from polars._utils.convert import to_py_date, to_py_datetime
from polars.dependencies import pyiceberg
if TYPE_CHECKING:
from datetime import date, datetime
from pyiceberg.table import Table
from polars import DataFrame, LazyFrame, Series
__all__ = ["scan_iceberg"]
_temporal_conversions: dict[str, Callable[..., datetime | date]] = {
"to_py_date": to_py_date,
"to_py_datetime": to_py_datetime,
}
def scan_iceberg(
source: str | Table,
*,
storage_options: dict[str, Any] | None = None,
) -> LazyFrame:
"""
Lazily read from an Apache Iceberg table.
Parameters
----------
source
A PyIceberg table, or a direct path to the metadata.
Note: For Local filesystem, absolute and relative paths are supported but
for the supported object storages - GCS, Azure and S3 full URI must be provided.
storage_options
Extra options for the storage backends supported by `pyiceberg`.
For cloud storages, this may include configurations for authentication etc.
More info is available `here <https://py.iceberg.apache.org/configuration/>`__.
Returns
-------
LazyFrame
Examples
--------
Creates a scan for an Iceberg table from local filesystem, or object store.
>>> table_path = "file:/path/to/iceberg-table/metadata.json"
>>> pl.scan_iceberg(table_path).collect() # doctest: +SKIP
Creates a scan for an Iceberg table from S3.
See a list of supported storage options for S3 `here
<https://py.iceberg.apache.org/configuration/#fileio>`__.
>>> table_path = "s3://bucket/path/to/iceberg-table/metadata.json"
>>> storage_options = {
... "s3.region": "eu-central-1",
... "s3.access-key-id": "THE_AWS_ACCESS_KEY_ID",
... "s3.secret-access-key": "THE_AWS_SECRET_ACCESS_KEY",
... }
>>> pl.scan_iceberg(
... table_path, storage_options=storage_options
... ).collect() # doctest: +SKIP
Creates a scan for an Iceberg table from Azure.
Supported options for Azure are available `here
<https://py.iceberg.apache.org/configuration/#azure-data-lake>`__.
Following type of table paths are supported:
* az://<container>/<path>/metadata.json
* adl://<container>/<path>/metadata.json
* abfs[s]://<container>/<path>/metadata.json
>>> table_path = "az://container/path/to/iceberg-table/metadata.json"
>>> storage_options = {
... "adlfs.account-name": "AZURE_STORAGE_ACCOUNT_NAME",
... "adlfs.account-key": "AZURE_STORAGE_ACCOUNT_KEY",
... }
>>> pl.scan_iceberg(
... table_path, storage_options=storage_options
... ).collect() # doctest: +SKIP
Creates a scan for an Iceberg table from Google Cloud Storage.
Supported options for GCS are available `here
<https://py.iceberg.apache.org/configuration/#google-cloud-storage>`__.
>>> table_path = "s3://bucket/path/to/iceberg-table/metadata.json"
>>> storage_options = {
... "gcs.project-id": "my-gcp-project",
... "gcs.oauth.token": "ya29.dr.AfM...",
... }
>>> pl.scan_iceberg(
... table_path, storage_options=storage_options
... ).collect() # doctest: +SKIP
Creates a scan for an Iceberg table with additional options.
In the below example, `without_files` option is used which loads the table without
file tracking information.
>>> table_path = "/path/to/iceberg-table/metadata.json"
>>> storage_options = {"py-io-impl": "pyiceberg.io.fsspec.FsspecFileIO"}
>>> pl.scan_iceberg(
... table_path, storage_options=storage_options
... ).collect() # doctest: +SKIP
"""
from pyiceberg.io.pyarrow import schema_to_pyarrow
from pyiceberg.table import StaticTable
if isinstance(source, str):
source = StaticTable.from_metadata(
metadata_location=source, properties=storage_options or {}
)
func = partial(_scan_pyarrow_dataset_impl, source)
arrow_schema = schema_to_pyarrow(source.schema())
return pl.LazyFrame._scan_python_function(arrow_schema, func, pyarrow=True)
def _scan_pyarrow_dataset_impl(
tbl: Table,
with_columns: list[str] | None = None,
predicate: str = "",
n_rows: int | None = None,
**kwargs: Any,
) -> DataFrame | Series:
"""
Take the projected columns and materialize an arrow table.
Parameters
----------
tbl
pyarrow dataset
with_columns
Columns that are projected
predicate
pyarrow expression that can be evaluated with eval
n_rows:
Materialize only n rows from the arrow dataset.
batch_size
The maximum row count for scanned pyarrow record batches.
kwargs:
For backward compatibility
Returns
-------
DataFrame
"""
from polars import from_arrow
scan = tbl.scan(limit=n_rows)
if with_columns is not None:
scan = scan.select(*with_columns)
if predicate is not None:
try:
expr_ast = _to_ast(predicate)
pyiceberg_expr = _convert_predicate(expr_ast)
except ValueError as e:
msg = f"Could not convert predicate to PyIceberg: {predicate}"
raise ValueError(msg) from e
scan = scan.filter(pyiceberg_expr)
return from_arrow(scan.to_arrow())
def _to_ast(expr: str) -> ast.expr:
"""
Converts a Python string to an AST.
This will take the Python Arrow expression (as a string), and it will
be converted into a Python AST that can be traversed to convert it to a PyIceberg
expression.
The reason to convert it to an AST is because the PyArrow expression
itself doesn't have any methods/properties to traverse the expression.
We need this to convert it into a PyIceberg expression.
Parameters
----------
expr
The string expression
Returns
-------
The AST representing the Arrow expression
"""
return ast.parse(expr, mode="eval").body
@singledispatch
def _convert_predicate(a: Any) -> Any:
"""Walks the AST to convert the PyArrow expression to a PyIceberg expression."""
msg = f"Unexpected symbol: {a}"
raise ValueError(msg)
@_convert_predicate.register(Constant)
def _(a: Constant) -> Any:
return a.value
@_convert_predicate.register(Name)
def _(a: Name) -> Any:
return a.id
@_convert_predicate.register(UnaryOp)
def _(a: UnaryOp) -> Any:
if isinstance(a.op, Invert):
return pyiceberg.expressions.Not(_convert_predicate(a.operand))
else:
msg = f"Unexpected UnaryOp: {a}"
raise TypeError(msg)
@_convert_predicate.register(Call)
def _(a: Call) -> Any:
args = [_convert_predicate(arg) for arg in a.args]
f = _convert_predicate(a.func)
if f == "field":
return args
elif f in _temporal_conversions:
# convert from polars-native i64 to ISO8601 string
return _temporal_conversions[f](*args).isoformat()
else:
ref = _convert_predicate(a.func.value)[0] # type: ignore[attr-defined]
if f == "isin":
return pyiceberg.expressions.In(ref, args[0])
elif f == "is_null":
return pyiceberg.expressions.IsNull(ref)
elif f == "is_nan":
return pyiceberg.expressions.IsNaN(ref)
msg = f"Unknown call: {f!r}"
raise ValueError(msg)
@_convert_predicate.register(Attribute)
def _(a: Attribute) -> Any:
return a.attr
@_convert_predicate.register(BinOp)
def _(a: BinOp) -> Any:
lhs = _convert_predicate(a.left)
rhs = _convert_predicate(a.right)
op = a.op
if isinstance(op, BitAnd):
return pyiceberg.expressions.And(lhs, rhs)
if isinstance(op, BitOr):
return pyiceberg.expressions.Or(lhs, rhs)
else:
msg = f"Unknown: {lhs} {op} {rhs}"
raise TypeError(msg)
@_convert_predicate.register(Compare)
def _(a: Compare) -> Any:
op = a.ops[0]
lhs = _convert_predicate(a.left)[0]
rhs = _convert_predicate(a.comparators[0])
if isinstance(op, Gt):
return pyiceberg.expressions.GreaterThan(lhs, rhs)
if isinstance(op, GtE):
return pyiceberg.expressions.GreaterThanOrEqual(lhs, rhs)
if isinstance(op, Eq):
return pyiceberg.expressions.EqualTo(lhs, rhs)
if isinstance(op, Lt):
return pyiceberg.expressions.LessThan(lhs, rhs)
if isinstance(op, LtE):
return pyiceberg.expressions.LessThanOrEqual(lhs, rhs)
else:
msg = f"Unknown comparison: {op}"
raise TypeError(msg)
@_convert_predicate.register(List)
def _(a: List) -> Any:
return [_convert_predicate(e) for e in a.elts]