code: def parse_list_batches_query_v1(
      user: str, q: str, last_batch_id: Optional[int]
  ) -> Tuple[str, List[Any]]:
      where_conditions = [
          "(billing_project_users.`user` = %s AND billing_project_users.billing_project = batches.billing_project)",
          "NOT deleted",
      ]
      where_args: List[Any] = [user]
  
      if last_batch_id is not None:
          where_conditions.append("(batches.id < %s)")
          where_args.append(last_batch_id)
  
      terms = q.split()
      for _t in terms:
          if _t[0] == "!":
              negate = True
              t = _t[1:]
          else:
              negate = False
              t = _t
  
          if "=" in t:
              k, v = t.split("=", 1)
              condition = """
  ((batches.id) IN
   (SELECT batch_id FROM job_group_attributes
    WHERE `key` = %s AND `value` = %s))
  """
              args = [k, v]
          elif t.startswith("has:"):
              k = t[4:]
              condition = """
  ((batches.id) IN
   (SELECT batch_id FROM job_group_attributes
    WHERE `key` = %s))
  """
              args = [k]
          elif t.startswith("user:"):
              k = t[5:]
              condition = """
  (batches.`user` = %s)
  """
              args = [k]
          elif t.startswith("billing_project:"):
              k = t[16:]
              condition = """
  (billing_projects.name_cs = %s)
  """
              args = [k]
          elif t == "open":
              condition = "(`state` = 'open')"
              args = []
          elif t == "closed":
              condition = "(`state` != 'open')"
              args = []
          elif t == "complete":
              condition = "(`state` = 'complete')"
              args = []
          elif t == "running":
              condition = "(`state` = 'running')"
              args = []
          elif t == "cancelled":
              condition = "(job_groups_cancelled.id IS NOT NULL)"
              args = []
          elif t == "failure":
              condition = "(n_failed > 0)"
              args = []
          elif t == "success":
              # need complete because there might be no jobs
              condition = (
                  "(`state` = 'complete' AND n_succeeded = n_jobs)"
              )
              args = []
          else:
              raise QueryError(f"Invalid search term: {t}.")
  
          if negate:
              condition = f"(NOT {condition})"
  
          where_conditions.append(condition)
          where_args.extend(args)
  
      sql = f"""
  WITH base_t AS (
    SELECT batches.*,
      job_groups_cancelled.id IS NOT NULL AS cancelled,
      job_groups_n_jobs_in_complete_states.n_completed,
      job_groups_n_jobs_in_complete_states.n_succeeded,
      job_groups_n_jobs_in_complete_states.n_failed,
      job_groups_n_jobs_in_complete_states.n_cancelled
    FROM batches
    LEFT JOIN billing_projects ON batches.billing_project = billing_projects.name
    LEFT JOIN job_groups_n_jobs_in_complete_states
      ON batches.id = job_groups_n_jobs_in_complete_states.id
    LEFT JOIN job_groups_cancelled
      ON batches.id = job_groups_cancelled.id
    STRAIGHT_JOIN billing_project_users ON batches.billing_project = billing_project_users.billing_project
    WHERE {' AND '.join(where_conditions)}
    ORDER BY id DESC
    LIMIT 51
  )
  SELECT base_t.*, cost_t.cost, cost_t.cost_breakdown
  FROM base_t
  LEFT JOIN LATERAL (
    SELECT COALESCE(SUM(`usage` * rate), 0) AS cost, JSON_OBJECTAGG(resources.resource, COALESCE(`usage` * rate, 0)) AS cost_breakdown
    FROM (
      SELECT batch_id, resource_id, CAST(COALESCE(SUM(`usage`), 0) AS SIGNED) AS `usage`
      FROM aggregated_job_group_resources_v3
      WHERE base_t.id = aggregated_job_group_resources_v3.batch_id
      GROUP BY batch_id, resource_id
    ) AS usage_t
    LEFT JOIN resources ON usage_t.resource_id = resources.resource_id
    GROUP BY batch_id
  ) AS cost_t ON TRUE
  ORDER BY id DESC;
  """
  
      return (sql, where_args)
  
imports: from typing import Any, List, Optional, Tuple
  from ...exceptions import QueryError
  from .query import job_state_search_term_to_states
label_desc: Use image node_id 1 for calling a global function with example usage: parse_list_batches_query_v1(user, q, last_batch_id) and returns: sql,  where_args