PythonCallableAsResult.yqls 847 B

1234567891011121314151617181920212223242526272829303132333435363738
  1. (
  2. (let config (DataSource 'config))
  3. # prepare python udf
  4. (let ui64 (DataType 'Uint64))
  5. (let str (DataType 'String))
  6. (let funcType (CallableType '() '(ui64) '(ui64)))
  7. (let udfType (CallableType '() '(funcType) '(ui64)))
  8. (let udfScript (String '@@
  9. def create_counter(start):
  10. def counter(step):
  11. v = counter.x
  12. counter.x += step
  13. return v
  14. counter.x = start
  15. return counter
  16. @@))
  17. (let udf (ScriptUdf 'Python3 'create_counter udfType udfScript))
  18. # call udf
  19. (let counter (Apply udf (Uint64 '1)))
  20. (let result (AsList
  21. (Apply counter (Uint64 '1))
  22. (Apply counter (Uint64 '2))
  23. (Apply counter (Uint64 '3))
  24. (Apply counter (Uint64 '4))
  25. ))
  26. # output result with type
  27. (let res_sink (DataSink 'result))
  28. (let world (Write! world res_sink (Key) result '( '('type) )))
  29. # finish
  30. (return (Commit! world res_sink))
  31. )