Repository navigation
Expand file tree
/
Copy pathudf_rehydrate.py
More file actions
242 lines (202 loc) · 7.36 KB
/
Copy pathudf_rehydrate.py
File metadata and controls
242 lines (202 loc) · 7.36 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
236
237
238
239
240
241
242
"""Rehydrate trusted UDF callables from registry source text.
Feast persists both dill bytes (``body``) and source (``body_text`` / ``udf_string``).
Preferring source avoids:
* Spark driver segfaults (exit 139) when dill-restored callables touch DataFrames
* Cross-Python-version serve failures when apply and feature-server differ
"""
from __future__ import annotations
import builtins
import dis
import logging
from types import CodeType
from typing import Callable, Optional, Set
import dill
logger = logging.getLogger(__name__)
def _strip_leading_decorators(udf_string: str) -> str:
"""Remove leading ``@decorator`` lines before ``def`` / ``async def``.
Uses a linear paren-balanced scan (O(n)). Avoids nested regex quantifiers that
CodeQL flags as ReDoS-prone on adversarial ``@`` / newline spam in body_text.
"""
text = udf_string.lstrip()
if not text.startswith("@"):
return text
i = 0
n = len(text)
while i < n:
while i < n and text[i] in " \t\r\n":
i += 1
if i >= n:
break
if text[i] == "#":
while i < n and text[i] != "\n":
i += 1
continue
rest = text[i:]
if rest.startswith("async def ") or rest.startswith("def "):
break
if text[i] != "@":
break
i += 1 # past '@'
while i < n and (text[i].isalnum() or text[i] in "._"):
i += 1
while i < n and text[i] in " \t":
i += 1
if i < n and text[i] == "(":
depth = 0
in_str: Optional[str] = None
while i < n:
ch = text[i]
if in_str is not None:
if ch == "\\" and i + 1 < n:
i += 2
continue
if text.startswith(in_str, i):
i += len(in_str)
in_str = None
continue
i += 1
continue
if text.startswith('"""', i) or text.startswith("'''", i):
in_str = text[i : i + 3]
i += 3
continue
if ch in ("'", '"'):
in_str = ch
i += 1
continue
if ch == "(":
depth += 1
elif ch == ")":
depth -= 1
i += 1
if depth == 0:
break
continue
i += 1
while i < n and text[i] in " \t\r":
i += 1
if i < n and text[i] == "\n":
i += 1
else:
while i < n and text[i] != "\n":
i += 1
if i < n and text[i] == "\n":
i += 1
stripped = text[i:]
return stripped if stripped.strip() else udf_string
def _exec_namespace() -> dict:
"""Minimal globals so typical UDF source can exec without full feast apply ctx."""
ns: dict = {"__name__": "feast_udf_rehydrate"}
try:
import pandas as pd
ns["pd"] = pd
ns["pandas"] = pd
except ImportError:
pass
try:
import numpy as np
ns["np"] = np
ns["numpy"] = np
except ImportError:
pass
return ns
def _unresolved_global_names(func: Callable, namespace: dict) -> Set[str]:
"""Global names *func* loads that neither *namespace* nor builtins provide.
``exec``-ing a function definition only binds the function; the globals its
body reads are looked up when it is *called*. Source text alone therefore
cannot tell us whether the callable actually works — a UDF referencing a
helper or constant from its defining module execs cleanly and then raises
``NameError`` mid-retrieval.
Only ``LOAD_GLOBAL`` operands count. ``co_names`` also holds attribute names
(``df.columns``), which would flag working UDFs and push them onto the dill
path for no reason. Nested code objects (inner functions, comprehensions)
are walked too.
"""
missing: Set[str] = set()
code = getattr(func, "__code__", None)
if code is None:
return missing
seen: Set[int] = set()
pending = [code]
while pending:
current = pending.pop()
if id(current) in seen:
continue
seen.add(id(current))
for instruction in dis.get_instructions(current):
if instruction.opname != "LOAD_GLOBAL":
continue
name = instruction.argval
if not isinstance(name, str):
continue
if name not in namespace and not hasattr(builtins, name):
missing.add(name)
for const in current.co_consts:
if isinstance(const, CodeType):
pending.append(const)
return missing
def rehydrate_udf_from_source(
udf_string: str,
*,
preferred_name: Optional[str] = None,
) -> Optional[Callable]:
"""Exec ``udf_string`` and return the resulting callable, or None.
``udf_string`` is treated as trusted registry content written by ``feast apply``.
Returns None on failure so callers can fall back to dill.
"""
if not (udf_string or "").strip():
return None
source = _strip_leading_decorators(udf_string)
ns = _exec_namespace()
try:
exec(source, ns) # noqa: S102 — trusted registry source
except Exception as e:
logger.debug("udf source rehydrate failed: %s", e)
return None
if preferred_name and preferred_name in ns and callable(ns[preferred_name]):
return _accept_if_self_contained(ns[preferred_name], ns)
for value in ns.values():
if not callable(value):
continue
name = getattr(value, "__name__", None)
if name in (None, "<lambda>", "__build_class__"):
continue
# Skip imported modules / classes we seeded
if name in ("DataFrame",):
continue
return _accept_if_self_contained(value, ns)
return None
def _accept_if_self_contained(func: Callable, ns: dict) -> Optional[Callable]:
"""Return *func* only if every global it reads is available, else ``None``.
Returning ``None`` lets :func:`resolve_udf` fall back to the dill body, which
carries the UDF's captured globals and can still run.
"""
missing = _unresolved_global_names(func, ns)
if missing:
logger.debug(
"udf source rehydrate skipped for %s: unresolved global names %s; "
"falling back to the serialized body",
getattr(func, "__name__", func),
sorted(missing),
)
return None
return func
def resolve_udf(
*,
udf_string: str = "",
body: Optional[bytes] = None,
fallback_udf: Optional[Callable] = None,
preferred_name: Optional[str] = None,
) -> Callable:
"""Resolve a UDF: source first, then ``fallback_udf``, then dill ``body``."""
rehydrated = rehydrate_udf_from_source(udf_string, preferred_name=preferred_name)
if rehydrated is not None:
return rehydrated
if fallback_udf is not None:
return fallback_udf
if body:
return dill.loads(body)
raise ValueError(
"Cannot resolve UDF: empty udf_string/body_text and no dill body or "
"fallback callable. Re-run feast apply so body_text is persisted."
)