zeruibao opened a new pull request, #53314:
URL: https://github.com/apache/spark/pull/53314

   ### What changes were proposed in this pull request?
   Fix type handling of namedTuple for transfromWithStateInPandas.
   
   ### Why are the changes needed?
   The customers hit the issue when using namedTuple as value of structType like
   
   ```
   class BillingAddress(NamedTuple):
       address_id: int
       transaction_id: int
       
    def handleInputRows(
           self, 
           key: Any, 
           rows: Iterator[Row], 
           timerValues: TimerValues
       ) -> Iterator[Row]:             
           address: BillingAddress = BillingAddress(address_id = 1, 
transaction_id= 1)
           address_list = []
           address_list.append(address)
           self.addresses.update((address_list,))    
   ```
   
   The `_serialize_to_bytes` cannot construct the namedTuple correctly and hit
   ```
   File 
"/databricks/spark/python/pyspark/sql/streaming/stateful_processor_api_client.py",
 line 575, in normalize_value
       return type(v)(normalize_value(e) for e in v)
              ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
   TypeError: BillingAddress.__new__() missing 3 required positional arguments: 
'event_timestamp', 'processing_timestamp', and 'transaction_id'
   ```
   It's because NamedTuple cannot accept generator as parameter.
   
   ### Does this PR introduce _any_ user-facing change?
   No
   
   
   ### How was this patch tested?
   UT
   
   
   ### Was this patch authored or co-authored using generative AI tooling?
   No
   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to