Reland "[recipe-py] rdb query: support tr_fields"

This reverts commit c3be45efdc6968f92f0e7376c3740b8413fca76a.

Reason for revert: reland the original CL with a fix

Original change's description:
> Revert "[recipe-py] rdb query: support tr_fields"
>
> This reverts commit 7c7d1692a27912d9750f8ddcdb06300b66f3939a.
>
> Reason for revert: broke https://ci.chromium.org/ui/p/infra/builders/ci/recipe_engine-recipes-tests/1445/overview
> Original change's description:
> > [recipe-py] rdb query: support tr_fields
> >
> > Bug: 1166491
> > Change-Id: Ia6d3621a6177071e8c879ed14355ef865a0682d2
> > Reviewed-on: https://chromium-review.googlesource.com/c/infra/luci/recipes-py/+/2976542
> > Commit-Queue: Chan Li <chanli@chromium.org>
> > Reviewed-by: Scott Lee <ddoman@chromium.org>
>
> Bug: 1166491
> Change-Id: I025a28d9a4eec2e8b4536afc13a0ee7b12608182
> No-Presubmit: true
> No-Tree-Checks: true
> No-Try: true
> Reviewed-on: https://chromium-review.googlesource.com/c/infra/luci/recipes-py/+/2977057
> Bot-Commit: Rubber Stamper <rubber-stamper@appspot.gserviceaccount.com>
> Commit-Queue: Chan Li <chanli@chromium.org>

Bug: 1166491
Change-Id: I1a523e1622b0f75ef44bd2e6145706ed9d8f567e
Reviewed-on: https://chromium-review.googlesource.com/c/infra/luci/recipes-py/+/2977058
Reviewed-by: Scott Lee <ddoman@chromium.org>
Commit-Queue: Chan Li <chanli@chromium.org>
diff --git a/README.recipes.md b/README.recipes.md
index 1e802ef..683651e 100644
--- a/README.recipes.md
+++ b/README.recipes.md
@@ -2760,7 +2760,7 @@
 
 &mdash; **def [assert\_enabled](/recipe_modules/resultdb/api.py#45)(self):**
 
-&mdash; **def [config\_test\_presentation](/recipe_modules/resultdb/api.py#443)(self, column_keys=(), grouping_keys=('status',)):**
+&mdash; **def [config\_test\_presentation](/recipe_modules/resultdb/api.py#450)(self, column_keys=(), grouping_keys=('status',)):**
 
 Specifies how the test results should be rendered.
 
@@ -2798,7 +2798,7 @@
   test_exonerations (list): A list of test_result_pb2.TestExoneration.
   step_name (str): name of the step.
 
-&mdash; **def [get\_test\_result\_history](/recipe_modules/resultdb/api.py#215)(self, realm, test_id_regexp, variant_predicate=None, time_range=None, page_size=10, page_token=None, step_name=None):**
+&mdash; **def [get\_test\_result\_history](/recipe_modules/resultdb/api.py#222)(self, realm, test_id_regexp, variant_predicate=None, time_range=None, page_size=10, page_token=None, step_name=None):**
 
 Receive test results for a given configuration.
 
@@ -2846,7 +2846,7 @@
 Returns:
   A list of invocation_ids.
 
-&mdash; **def [query](/recipe_modules/resultdb/api.py#154)(self, inv_ids, variants_with_unexpected_results=False, merge=False, limit=None, step_name=None):**
+&mdash; **def [query](/recipe_modules/resultdb/api.py#154)(self, inv_ids, variants_with_unexpected_results=False, merge=False, limit=None, step_name=None, tr_fields=None):**
 
 Returns test results in the invocations.
 
@@ -2875,7 +2875,8 @@
   limit (int): maximum number of test results to return.
     Defaults to 1000.
   step_name (str): name of the step.
-
+  tr_fields (list of str): test result fields in the response.
+    Test result name will always be included regardless of this param value.
 Returns:
   A dict {invocation_id: api.Invocation}.
 
@@ -2892,7 +2893,7 @@
 This updates the inclusions of the current invocation specified in the
 LUCI_CONTEXT.
 
-&mdash; **def [wrap](/recipe_modules/resultdb/api.py#344)(self, cmd, test_id_prefix='', base_variant=None, test_location_base='', base_tags=None, coerce_negative_duration=False, include=False, realm='', location_tags_file='', require_build_inv=True, exonerate_unexpected_pass=False):**
+&mdash; **def [wrap](/recipe_modules/resultdb/api.py#351)(self, cmd, test_id_prefix='', base_variant=None, test_location_base='', base_tags=None, coerce_negative_duration=False, include=False, realm='', location_tags_file='', require_build_inv=True, exonerate_unexpected_pass=False):**
 
 Wraps the command with ResultSink.
 
diff --git a/recipe_modules/resultdb/api.py b/recipe_modules/resultdb/api.py
index 4e8d0d1..ded83af 100644
--- a/recipe_modules/resultdb/api.py
+++ b/recipe_modules/resultdb/api.py
@@ -156,7 +156,8 @@
             variants_with_unexpected_results=False,
             merge=False,
             limit=None,
-            step_name=None):
+            step_name=None,
+            tr_fields=None):
     """Returns test results in the invocations.
 
     Most users will be interested only in results of test variants that had
@@ -184,13 +185,16 @@
       limit (int): maximum number of test results to return.
         Defaults to 1000.
       step_name (str): name of the step.
-
+      tr_fields (list of str): test result fields in the response.
+        Test result name will always be included regardless of this param value.
     Returns:
       A dict {invocation_id: api.Invocation}.
     """
     assert len(inv_ids) > 0
     assert all(isinstance(id, str) for id in inv_ids), inv_ids
     assert limit is None or limit >= 0
+    assert tr_fields is None or all(
+        isinstance(field, str) for field in tr_fields), tr_fields
     limit = limit or 1000
 
     args = [
@@ -201,6 +205,9 @@
       args += ['-u']
     if merge:
       args += ['-merge']
+    if tr_fields:
+      args += ['-tr-fields', ','.join(tr_fields)]
+
     args += list(inv_ids)
 
     step_res = self._run_rdb(
diff --git a/recipe_modules/resultdb/examples/query.py b/recipe_modules/resultdb/examples/query.py
index 2e06622..8b5574d 100644
--- a/recipe_modules/resultdb/examples/query.py
+++ b/recipe_modules/resultdb/examples/query.py
@@ -26,6 +26,7 @@
       step_name='rdb query',
       variants_with_unexpected_results=True,
       merge=True,
+      tr_fields=['tags'],
   )
   if inv_bundle:
     pres = api.step.active_result.presentation