forked from apache/datafusion-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_udtf.py
More file actions
235 lines (175 loc) · 8.4 KB
/
Copy pathtest_udtf.py
File metadata and controls
235 lines (175 loc) · 8.4 KB
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
# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements. See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership. The ASF licenses this file
# to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance
# with the License. You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
# KIND, either express or implied. See the License for the
# specific language governing permissions and limitations
# under the License.
import pyarrow as pa
import pyarrow.dataset as ds
import pytest
from datafusion import Expr, SessionContext, Table, udtf
from datafusion.context import TableProviderExportable
from datafusion.user_defined import TableFunction
def python_table_function_inner(
num_cols: int, num_rows: int, num_batches: int
) -> Table:
rows = list(range(num_rows))
cols = [pa.array(rows) for _ in range(num_cols)]
names = [f"row_{i}" for i in range(num_cols)]
batch = pa.RecordBatch.from_arrays(cols, names=names)
batches = [batch for _ in range(num_batches)]
dataset = ds.dataset(batches)
return Table(dataset)
def test_python_table_function_with_args_class() -> None:
"""Test Python TableFunction using a class with arguments."""
ctx = SessionContext()
class ParameterizedTableFunction:
"""Table function that takes parameters."""
def __call__(self, num_cols: Expr, num_rows: Expr, num_batches: Expr) -> Table:
return python_table_function_inner(
num_cols.to_variant().value_i64(),
num_rows.to_variant().value_i64(),
num_batches.to_variant().value_i64(),
)
# Register the function
table_func = ParameterizedTableFunction()
table_udtf = udtf(table_func, "param_func")
ctx.register_udtf(table_udtf)
# Call with different parameters
result = ctx.sql("SELECT * FROM param_func(5, 10, 2)").collect()
assert len(result) == 2
assert result[0].num_columns == 5
assert result[0].num_rows == 10
def test_python_table_function_decorator() -> None:
"""Test Python TableFunction using decorator syntax."""
ctx = SessionContext()
@udtf("decorated_func")
def my_decorated_func(num_cols: Expr, num_rows: Expr, num_batches: Expr) -> Table:
return python_table_function_inner(
num_cols.to_variant().value_i64(),
num_rows.to_variant().value_i64(),
num_batches.to_variant().value_i64(),
)
ctx.register_udtf(my_decorated_func)
result = ctx.sql("SELECT * FROM decorated_func(3, 7, 2)").collect()
assert len(result) == 2
assert result[0].num_columns == 3
assert result[0].num_rows == 7
def test_python_table_function_no_args() -> None:
"""Test Python TableFunction with no arguments."""
ctx = SessionContext()
@udtf("static_func")
def static_table_func() -> Table:
return python_table_function_inner(2, 3, 1)
ctx.register_udtf(static_table_func)
result = ctx.sql("SELECT * FROM static_func()").collect()
assert len(result) == 1
assert list(result[0].column(0).to_pylist()) == [0, 1, 2]
assert list(result[0].column(1).to_pylist()) == [0, 1, 2]
def test_python_table_function_single_arg() -> None:
"""Test Python TableFunction with a single argument."""
ctx = SessionContext()
@udtf("single_arg_func")
def single_arg_func(n: Expr) -> TableProviderExportable:
return python_table_function_inner(2, n.to_variant().value_i64(), 1)
ctx.register_udtf(single_arg_func)
result = ctx.sql("SELECT * FROM single_arg_func(15)").collect()
assert len(result) == 1
assert result[0].num_columns == 2
assert result[0].num_rows == 15
def test_python_table_function_with_string_args() -> None:
"""Test Python TableFunction with string arguments."""
ctx = SessionContext()
@udtf("string_arg_func")
def string_arg_func(prefix: Expr) -> TableProviderExportable:
prefix_str = prefix.to_variant().value_string()
# Create a table with the prefix in column names
batch = pa.RecordBatch.from_arrays(
[pa.array([1, 2, 3]), pa.array([4, 5, 6])],
names=[f"{prefix_str}_a", f"{prefix_str}_b"],
)
return Table(ds.dataset([batch]))
ctx.register_udtf(string_arg_func)
result = ctx.sql("SELECT * FROM string_arg_func('test')").collect()
assert len(result) == 1
assert result[0].schema.names == ["test_a", "test_b"]
def test_python_table_function_receives_session() -> None:
"""A UDTF registered ``with_session=True`` gets the calling ctx."""
ctx = SessionContext()
captured: list[SessionContext] = []
@udtf("session_aware_func", with_session=True)
def session_aware_func(*, session: SessionContext) -> TableProviderExportable:
captured.append(session)
batch = pa.RecordBatch.from_pydict({"a": [1, 2, 3]})
return Table(ds.dataset([batch]))
ctx.register_udtf(session_aware_func)
result = ctx.sql("SELECT * FROM session_aware_func()").collect()
assert len(captured) == 1
assert isinstance(captured[0], SessionContext)
# Sharing the same catalog confirms the wrapper points at the caller's state.
assert captured[0].catalog().schema().names() == ctx.catalog().schema().names()
assert result[0].column(0).to_pylist() == [1, 2, 3]
def test_python_table_function_session_used_for_metadata() -> None:
"""The UDTF can inspect session state through the passed-in context."""
ctx = SessionContext()
base_batch = pa.RecordBatch.from_pydict({"x": [10, 20, 30]})
ctx.register_batch("base_tbl", base_batch)
seen_tables: list[set[str]] = []
@udtf("table_inventory", with_session=True)
def table_inventory(*, session: SessionContext) -> TableProviderExportable:
# Stash the visible tables to verify the session wired through.
seen_tables.append(session.catalog().schema().names())
batch = pa.RecordBatch.from_pydict({"name": ["base_tbl"]})
return Table(ds.dataset([batch]))
ctx.register_udtf(table_inventory)
result = ctx.sql("SELECT * FROM table_inventory()").collect()
assert seen_tables == [{"base_tbl"}]
assert result[0].column(0).to_pylist() == ["base_tbl"]
def test_python_table_function_class_callable_with_session() -> None:
"""Class-based UDTFs opt in via ``with_session=True``."""
ctx = SessionContext()
captured: list[SessionContext] = []
class SessionAware:
def __call__(
self, n: Expr, *, session: SessionContext
) -> TableProviderExportable:
captured.append(session)
count = n.to_variant().value_i64()
batch = pa.RecordBatch.from_pydict({"a": list(range(count))})
return Table(ds.dataset([batch]))
ctx.register_udtf(udtf(SessionAware(), "session_class_func", with_session=True))
result = ctx.sql("SELECT * FROM session_class_func(3)").collect()
assert len(captured) == 1
assert isinstance(captured[0], SessionContext)
assert result[0].column(0).to_pylist() == [0, 1, 2]
def test_python_table_function_without_session_flag_no_injection() -> None:
"""Default registration (no ``with_session``) calls func positionally."""
ctx = SessionContext()
@udtf("plain_func")
def plain_func(n: Expr) -> TableProviderExportable:
count = n.to_variant().value_i64()
batch = pa.RecordBatch.from_pydict({"a": list(range(count))})
return Table(ds.dataset([batch]))
ctx.register_udtf(plain_func)
result = ctx.sql("SELECT * FROM plain_func(4)").collect()
assert result[0].column(0).to_pylist() == [0, 1, 2, 3]
def test_with_session_rejected_for_ffi_table_function() -> None:
"""`with_session=True` is incompatible with FFI-exported table functions."""
class FakeFFITableFunction:
# Presence of this attribute is what marks a function as FFI-exported.
__datafusion_table_function__ = "stub"
fake = FakeFFITableFunction()
with pytest.raises(TypeError, match="FFI-exported table functions"):
udtf(fake, "fake_ffi", with_session=True)
with pytest.raises(TypeError, match="FFI-exported table functions"):
TableFunction("fake_ffi", fake, with_session=True)