Commit 7df58e5
feat: pass calling SessionContext to Python UDTF callbacks (#1555)
* feat: pass calling SessionContext to Python UDTF callbacks
DataFusion 53 added `TableFunctionImpl::call_with_args(TableFunctionArgs)`
where `TableFunctionArgs` carries both the positional expression
arguments and the calling `&dyn Session`. The pure-Python UDTF path
previously discarded everything but the exprs.
Thread the session through when the user callback's signature opts in
by declaring a `session` keyword parameter (or `**kwargs`). At call
time we downcast the `&dyn Session` to its canonical `SessionState`
impl and build a fresh `SessionContext` over the same Arc-shared state,
exposed to Python as a `datafusion.SessionContext` wrapper. Existing
callbacks whose signatures do not declare `session` continue to be
called with the positional expression arguments only — no behavior
change for current users.
Note: a UDTF body cannot drive a fresh `ctx.sql(...).collect()` on the
passed-in session because the outer SQL execution already holds the
tokio runtime. Use the session for metadata access (catalogs, UDF
lookups, config) rather than nested DataFrame collection.
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
* docs: clarify py_session_from_session downcast is defensive
The doc comment implied a foreign FFI session was a real input. No
current path reaches a pure-Python UDTF with a non-SessionState
session: the SQL planner and __call__ both hand a SessionState, and a
ForeignSession would only arrive via FFI-export of the UDTF, which
datafusion-python does not do. Reword to state the guard is defensive
and rewrap the error string.
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
* refactor: opt-in UDTF session injection via with_session flag
Replaces signature sniffing with an explicit ``with_session=True`` kwarg
on ``TableFunction`` / ``udtf``. Avoids name-based detection footguns
(positional-only ``session`` params, accidental ``**kwargs`` opt-in,
shadowing by unrelated params) and makes author intent visible at
registration. Also documents the feature in the UDTF user guide.
Rust field renamed ``accepts_session`` -> ``inject_session_on_call`` to
match the Python-side opt-in semantics.
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
* fix: reject with_session=True for FFI UDTFs and qualify mutation docs
Raise TypeError when with_session=True is combined with an FFI-exported
table function (one exposing __datafusion_table_function__). The Rust
FFI branch does not consult the flag, so it would silently be dropped;
guard both TableFunction.__init__ and the udtf() convenience entry.
Qualify the doc claim that mutations through the injected session
propagate to the caller: registry mutations do (shared Arc registries),
but config changes do not (SessionConfig is cloned). Mirror the caveat
in TableFunction.__init__ per the user-guide caveats convention.
Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
---------
Co-authored-by: Claude Opus 4.7 (1M context) <noreply@anthropic.com>1 parent 9872283 commit 7df58e5
4 files changed
Lines changed: 303 additions & 29 deletions
File tree
- crates/core/src
- docs/source/user-guide/common-operations
- python
- datafusion
- tests
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
18 | 18 | | |
19 | 19 | | |
20 | 20 | | |
21 | | - | |
22 | | - | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
23 | 25 | | |
24 | 26 | | |
25 | 27 | | |
26 | 28 | | |
27 | 29 | | |
28 | | - | |
| 30 | + | |
29 | 31 | | |
30 | 32 | | |
31 | 33 | | |
32 | 34 | | |
33 | 35 | | |
34 | 36 | | |
| 37 | + | |
| 38 | + | |
| 39 | + | |
| 40 | + | |
| 41 | + | |
| 42 | + | |
| 43 | + | |
| 44 | + | |
| 45 | + | |
| 46 | + | |
| 47 | + | |
| 48 | + | |
35 | 49 | | |
36 | 50 | | |
37 | 51 | | |
| |||
40 | 54 | | |
41 | 55 | | |
42 | 56 | | |
43 | | - | |
44 | 57 | | |
45 | 58 | | |
46 | | - | |
| 59 | + | |
47 | 60 | | |
48 | 61 | | |
49 | 62 | | |
50 | 63 | | |
51 | 64 | | |
52 | 65 | | |
53 | | - | |
| 66 | + | |
54 | 67 | | |
55 | 68 | | |
56 | 69 | | |
57 | 70 | | |
| 71 | + | |
58 | 72 | | |
59 | 73 | | |
60 | 74 | | |
| |||
80 | 94 | | |
81 | 95 | | |
82 | 96 | | |
83 | | - | |
84 | | - | |
| 97 | + | |
| 98 | + | |
| 99 | + | |
| 100 | + | |
85 | 101 | | |
86 | 102 | | |
87 | 103 | | |
| |||
107 | 123 | | |
108 | 124 | | |
109 | 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 | + | |
110 | 161 | | |
111 | 162 | | |
112 | | - | |
113 | | - | |
| 163 | + | |
| 164 | + | |
114 | 165 | | |
115 | | - | |
| 166 | + | |
| 167 | + | |
| 168 | + | |
| 169 | + | |
| 170 | + | |
| 171 | + | |
| 172 | + | |
116 | 173 | | |
117 | 174 | | |
118 | 175 | | |
119 | 176 | | |
120 | | - | |
121 | 177 | | |
122 | | - | |
123 | | - | |
| 178 | + | |
| 179 | + | |
| 180 | + | |
| 181 | + | |
| 182 | + | |
| 183 | + | |
| 184 | + | |
| 185 | + | |
124 | 186 | | |
125 | 187 | | |
126 | 188 | | |
| |||
132 | 194 | | |
133 | 195 | | |
134 | 196 | | |
135 | | - | |
136 | | - | |
| 197 | + | |
| 198 | + | |
137 | 199 | | |
138 | 200 | | |
139 | 201 | | |
| |||
Lines changed: 39 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
431 | 431 | | |
432 | 432 | | |
433 | 433 | | |
| 434 | + | |
| 435 | + | |
| 436 | + | |
| 437 | + | |
| 438 | + | |
| 439 | + | |
| 440 | + | |
| 441 | + | |
| 442 | + | |
| 443 | + | |
| 444 | + | |
| 445 | + | |
| 446 | + | |
| 447 | + | |
| 448 | + | |
| 449 | + | |
| 450 | + | |
| 451 | + | |
| 452 | + | |
| 453 | + | |
| 454 | + | |
| 455 | + | |
| 456 | + | |
| 457 | + | |
| 458 | + | |
| 459 | + | |
| 460 | + | |
| 461 | + | |
| 462 | + | |
| 463 | + | |
| 464 | + | |
| 465 | + | |
| 466 | + | |
| 467 | + | |
| 468 | + | |
| 469 | + | |
| 470 | + | |
| 471 | + | |
| 472 | + | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1102 | 1102 | | |
1103 | 1103 | | |
1104 | 1104 | | |
| 1105 | + | |
| 1106 | + | |
| 1107 | + | |
| 1108 | + | |
| 1109 | + | |
| 1110 | + | |
| 1111 | + | |
| 1112 | + | |
| 1113 | + | |
| 1114 | + | |
| 1115 | + | |
| 1116 | + | |
| 1117 | + | |
| 1118 | + | |
| 1119 | + | |
| 1120 | + | |
| 1121 | + | |
| 1122 | + | |
1105 | 1123 | | |
1106 | 1124 | | |
1107 | 1125 | | |
| |||
1110 | 1128 | | |
1111 | 1129 | | |
1112 | 1130 | | |
1113 | | - | |
| 1131 | + | |
| 1132 | + | |
| 1133 | + | |
| 1134 | + | |
| 1135 | + | |
| 1136 | + | |
1114 | 1137 | | |
1115 | 1138 | | |
1116 | 1139 | | |
| 1140 | + | |
| 1141 | + | |
| 1142 | + | |
| 1143 | + | |
| 1144 | + | |
| 1145 | + | |
| 1146 | + | |
| 1147 | + | |
| 1148 | + | |
| 1149 | + | |
| 1150 | + | |
| 1151 | + | |
| 1152 | + | |
| 1153 | + | |
| 1154 | + | |
| 1155 | + | |
| 1156 | + | |
| 1157 | + | |
1117 | 1158 | | |
1118 | 1159 | | |
1119 | 1160 | | |
1120 | | - | |
| 1161 | + | |
| 1162 | + | |
| 1163 | + | |
| 1164 | + | |
| 1165 | + | |
| 1166 | + | |
| 1167 | + | |
| 1168 | + | |
1121 | 1169 | | |
1122 | 1170 | | |
1123 | 1171 | | |
| |||
1128 | 1176 | | |
1129 | 1177 | | |
1130 | 1178 | | |
| 1179 | + | |
| 1180 | + | |
1131 | 1181 | | |
1132 | 1182 | | |
1133 | 1183 | | |
1134 | 1184 | | |
1135 | 1185 | | |
1136 | | - | |
| 1186 | + | |
1137 | 1187 | | |
| 1188 | + | |
| 1189 | + | |
1138 | 1190 | | |
1139 | 1191 | | |
1140 | 1192 | | |
1141 | | - | |
1142 | | - | |
| 1193 | + | |
| 1194 | + | |
| 1195 | + | |
| 1196 | + | |
| 1197 | + | |
| 1198 | + | |
| 1199 | + | |
1143 | 1200 | | |
1144 | 1201 | | |
1145 | | - | |
| 1202 | + | |
| 1203 | + | |
| 1204 | + | |
1146 | 1205 | | |
1147 | 1206 | | |
| 1207 | + | |
| 1208 | + | |
| 1209 | + | |
| 1210 | + | |
| 1211 | + | |
| 1212 | + | |
| 1213 | + | |
1148 | 1214 | | |
1149 | 1215 | | |
1150 | | - | |
| 1216 | + | |
| 1217 | + | |
| 1218 | + | |
1151 | 1219 | | |
1152 | 1220 | | |
1153 | 1221 | | |
1154 | 1222 | | |
1155 | 1223 | | |
| 1224 | + | |
| 1225 | + | |
1156 | 1226 | | |
1157 | 1227 | | |
1158 | 1228 | | |
1159 | 1229 | | |
1160 | 1230 | | |
1161 | 1231 | | |
1162 | | - | |
| 1232 | + | |
1163 | 1233 | | |
1164 | 1234 | | |
1165 | 1235 | | |
1166 | 1236 | | |
1167 | | - | |
1168 | | - | |
1169 | | - | |
1170 | | - | |
1171 | | - | |
| 1237 | + | |
| 1238 | + | |
| 1239 | + | |
| 1240 | + | |
| 1241 | + | |
| 1242 | + | |
| 1243 | + | |
| 1244 | + | |
| 1245 | + | |
1172 | 1246 | | |
1173 | 1247 | | |
1174 | 1248 | | |
| |||
0 commit comments