@@ -243,25 +243,29 @@ def __init__(self, fields, original_fields, input_schema):
243243 self .passthrough_fields = []
244244
245245 script = []
246- for name , expr in fields .items ():
246+ for i , ( name , expr ) in enumerate ( fields .items () ):
247247 if isinstance (expr , str ) and expr in input_schema :
248248 self .passthrough_fields .append ((name , expr ))
249249 continue
250250
251251 if isinstance (expr , str ):
252252 expr = {'expression' : expr }
253253
254+ # We use numeric indexing (func_{i}) instead of reusing the output field
255+ # name to prevent syntax errors if output names contain spaces or hyphens.
256+ # We also use bracket notation for robustness against input field names
257+ # that aren't compliant dot-access identifiers.
254258 if 'expression' in expr :
255259 e = expr ['expression' ]
256- code = f"var func_{ name } = (__row__) => {{ " + " " .join (
257- [f"const { n } = __row__. { n } ;"
260+ code = f"var func_{ i } = (__row__) => {{ " + " " .join (
261+ [f"const { n } = __row__[' { n } '] ;"
258262 for n in original_fields if n in e ]) + f" return ({ e } ); }}"
259263 script .append (code )
260- self .field_funcs [name ] = f"func_{ name } "
264+ self .field_funcs [name ] = f"func_{ i } "
261265 elif 'callable' in expr :
262- code = f"var func_{ name } = { expr ['callable' ]} "
266+ code = f"var func_{ i } = { expr ['callable' ]} "
263267 script .append (code )
264- self .field_funcs [name ] = f"func_{ name } "
268+ self .field_funcs [name ] = f"func_{ i } "
265269 elif 'path' in expr and 'name' in expr :
266270 path = expr ['path' ]
267271 func_name = expr ['name' ]
@@ -320,8 +324,11 @@ def _get_javascript_udf_code(
320324 udf_code = FileSystems .open (path ).read ().decode ()
321325 return udf_code , name
322326 elif expression :
327+ # We use bracket notation for robustness against field names that
328+ # aren't compliant dot-access identifiers.
323329 udf_code = f"var { function_name } = (__row__) => {{ " + " " .join ([
324- f"const { n } = __row__.{ n } ;" for n in original_fields if n in expression
330+ f"const { n } = __row__['{ n } '];"
331+ for n in original_fields if n in expression
325332 ]) + f" return ({ expression } ); }}"
326333 return udf_code , function_name
327334 elif callable :
0 commit comments