@@ -225,8 +225,10 @@ def get_all_search_attributes(
225
225
workflow_id : str ,
226
226
workflow_run_id : Optional [str ] = None ,
227
227
):
228
+ run_id = workflow_run_id if workflow_run_id is not None else ""
229
+
228
230
return self ._do_get_workflow_search_attributes (
229
- workflow_class , workflow_id , workflow_run_id
231
+ workflow_class , workflow_id , run_id
230
232
)
231
233
232
234
def get_workflow_search_attributes (
@@ -240,24 +242,27 @@ def get_workflow_search_attributes(
240
242
raise ValueError (
241
243
"attribute_keys must contain at least one entry, or use get_all_search_attributes API to get all"
242
244
)
245
+
246
+ run_id = workflow_run_id if workflow_run_id is not None else ""
247
+
243
248
return self ._do_get_workflow_search_attributes (
244
- workflow_class , workflow_id , workflow_run_id , attribute_keys
249
+ workflow_class , workflow_id , run_id , attribute_keys
245
250
)
246
251
247
252
def _do_get_workflow_search_attributes (
248
253
self ,
249
254
workflow_class : type [ObjectWorkflow ],
250
255
workflow_id : str ,
251
- workflow_run_id : Optional [ str ] ,
256
+ workflow_run_id : str ,
252
257
attribute_keys : Optional [list [str ]] = None ,
253
258
):
254
259
wf_type = get_workflow_type_by_class (workflow_class )
255
260
self ._registry .get_workflow_with_check (wf_type )
256
261
257
262
search_attribute_types = self ._registry .get_search_attribute_types (wf_type )
258
263
259
- # if attribute keys is None or empty, iwf server will return all search attributes
260
- if attribute_keys is not None and attribute_keys :
264
+ # if attribute keys is None, will fetch all registered search attributes from the server
265
+ if attribute_keys :
261
266
non_existing_search_attribute_list : list [str ] = []
262
267
for attribute_key in attribute_keys :
263
268
if attribute_key not in search_attribute_types :
@@ -277,10 +282,8 @@ def _do_get_workflow_search_attributes(
277
282
sa_type = search_attribute_types [attribute_key ]
278
283
key_and_types .append (SearchAttributeKeyAndType (attribute_key , sa_type ))
279
284
280
- run_id = workflow_run_id if workflow_run_id is not None else ""
281
-
282
285
response = self ._unregistered_client .get_workflow_search_attributes (
283
- workflow_id , run_id , key_and_types
286
+ workflow_id , workflow_run_id , key_and_types
284
287
)
285
288
286
289
response_sas = response .search_attributes
@@ -296,28 +299,78 @@ def _do_get_workflow_search_attributes(
296
299
if response_sa_key is None :
297
300
raise RuntimeError ("search attribute key is None" )
298
301
response_sa_type = search_attribute_types [response_sa_key ]
299
- value = self . get_search_attribute_value (response_sa_type , response_sa )
302
+ value = get_search_attribute_value (response_sa_type , response_sa )
300
303
result [response_sa_key ] = value
301
304
302
305
return result
303
306
304
- @staticmethod
305
- def get_search_attribute_value (
306
- sa_type : SearchAttributeValueType , attribute : SearchAttribute
307
+ def set_workflow_search_attributes (
308
+ self ,
309
+ workflow_class : type [ObjectWorkflow ],
310
+ workflow_id : str ,
311
+ search_attributes : list [SearchAttribute ],
312
+ workflow_run_id : Optional [str ] = None ,
307
313
):
308
- if (
309
- sa_type == SearchAttributeValueType .KEYWORD
310
- or sa_type == SearchAttributeValueType .DATETIME
311
- or sa_type == SearchAttributeValueType .TEXT
312
- ):
313
- return unset_to_none (attribute .string_value )
314
- elif sa_type == SearchAttributeValueType .INT :
315
- return unset_to_none (attribute .integer_value )
316
- elif sa_type == SearchAttributeValueType .DOUBLE :
317
- return unset_to_none (attribute .double_value )
318
- elif sa_type == SearchAttributeValueType .BOOL :
319
- return unset_to_none (attribute .bool_value )
320
- elif sa_type == SearchAttributeValueType .KEYWORD_ARRAY :
321
- return unset_to_none (attribute .string_array_value )
322
- else :
323
- raise ValueError (f"not supported search attribute value type, { sa_type } " )
314
+ run_id = workflow_run_id if workflow_run_id is not None else ""
315
+
316
+ return self ._do_set_workflow_search_attributes (
317
+ workflow_class , workflow_id , run_id , search_attributes
318
+ )
319
+
320
+ def _do_set_workflow_search_attributes (
321
+ self ,
322
+ workflow_class : type [ObjectWorkflow ],
323
+ workflow_id : str ,
324
+ workflow_run_id : str ,
325
+ search_attributes : list [SearchAttribute ],
326
+ ):
327
+ wf_type = get_workflow_type_by_class (workflow_class )
328
+ self ._registry .get_workflow_with_check (wf_type )
329
+
330
+ search_attribute_types = self ._registry .get_search_attribute_types (wf_type )
331
+
332
+ # Check that the requested sa type is registered to the key
333
+ for search_attribute in search_attributes :
334
+ sa_key = unset_to_none (search_attribute .key )
335
+ if sa_key is None :
336
+ raise RuntimeError ("search attribute key is None" )
337
+ if sa_key not in search_attribute_types :
338
+ raise InvalidArgumentError (f"Search attribute not registered: { sa_key } " )
339
+ registered_value_type = search_attribute_types [sa_key ]
340
+
341
+ sa_value_type = unset_to_none (search_attribute .value_type )
342
+ if sa_value_type is None :
343
+ raise RuntimeError ("search value type is None" )
344
+
345
+ if (
346
+ sa_value_type is not None
347
+ and registered_value_type != sa_value_type .value
348
+ ):
349
+ raise ValueError (
350
+ f"Search attribute key, { sa_key } is registered to type { registered_value_type } , but tried to add search attribute type { sa_value_type .value } "
351
+ )
352
+
353
+ self ._unregistered_client .set_workflow_search_attributes (
354
+ workflow_id , workflow_run_id , search_attributes
355
+ )
356
+
357
+
358
+ def get_search_attribute_value (
359
+ sa_type : SearchAttributeValueType , attribute : SearchAttribute
360
+ ):
361
+ if (
362
+ sa_type == SearchAttributeValueType .KEYWORD
363
+ or sa_type == SearchAttributeValueType .DATETIME
364
+ or sa_type == SearchAttributeValueType .TEXT
365
+ ):
366
+ return unset_to_none (attribute .string_value )
367
+ elif sa_type == SearchAttributeValueType .INT :
368
+ return unset_to_none (attribute .integer_value )
369
+ elif sa_type == SearchAttributeValueType .DOUBLE :
370
+ return unset_to_none (attribute .double_value )
371
+ elif sa_type == SearchAttributeValueType .BOOL :
372
+ return unset_to_none (attribute .bool_value )
373
+ elif sa_type == SearchAttributeValueType .KEYWORD_ARRAY :
374
+ return unset_to_none (attribute .string_array_value )
375
+ else :
376
+ raise ValueError (f"not supported search attribute value type, { sa_type } " )
0 commit comments