code: def filter_intervals(
      ds, intervals, keep=True
  ) -> Union[Table, MatrixTable]:
      """Filter rows with a list of intervals.
  
      Examples
      --------
  
      Filter to loci falling within one interval:
  
      >>> ds_result = hl.filter_intervals(dataset, [hl.parse_locus_interval('17:38449840-38530994')])
  
      Remove all loci within list of intervals:
  
      >>> intervals = [hl.parse_locus_interval(x) for x in ['1:50M-75M', '2:START-400000', '3-22']]
      >>> ds_result = hl.filter_intervals(dataset, intervals, keep=False)
  
      Notes
      -----
      Based on the `keep` argument, this method will either restrict to points
      in the supplied interval ranges, or remove all rows in those ranges.
  
      When ``keep=True``, partitions that don't overlap any supplied interval
      will not be loaded at all.  This enables :func:`.filter_intervals` to be
      used for reasonably low-latency queries of small ranges of the dataset, even
      on large datasets.
  
      Parameters
      ----------
      ds : :class:`.MatrixTable` or :class:`.Table`
          Dataset to filter.
      intervals : :class:`.ArrayExpression` of type :class:`.tinterval`
          Intervals to filter on.  The point type of the interval must
          be a prefix of the key or equal to the first field of the key.
      keep : :obj:`bool`
          If ``True``, keep only rows that fall within any interval in `intervals`.
          If ``False``, keep only rows that fall outside all intervals in
          `intervals`.
  
      Returns
      -------
      :class:`.MatrixTable` or :class:`.Table`
  
      """
  
      if isinstance(ds, MatrixTable):
          k_type = ds.row_key.dtype
      else:
          assert isinstance(ds, Table)
          k_type = ds.key.dtype
  
      point_type = intervals.dtype.element_type.point_type
  
      def is_struct_prefix(partial, full):
          if list(partial) != list(full)[: len(partial)]:
              return False
          for k, v in partial.items():
              if full[k] != v:
                  return False
          return True
  
      if point_type == k_type[0]:
          needs_wrapper = True
          k_name = k_type.fields[0]
          point_type = hl.tstruct(**{k_name: k_type[k_name]})
      elif isinstance(point_type, tstruct) and is_struct_prefix(
          point_type, k_type
      ):
          needs_wrapper = False
      else:
          raise TypeError(
              "The point type is incompatible with key type of the dataset ('{}', '{}')".format(
                  repr(point_type), repr(k_type)
              )
          )
  
      def wrap_input(interval):
          if interval is None:
              raise TypeError(
                  "'filter_intervals' does not allow missing values in 'intervals'."
              )
          elif needs_wrapper:
              return Interval(
                  Struct(**{k_name: interval.start}),
                  Struct(**{k_name: interval.end}),
                  interval.includes_start,
                  interval.includes_end,
              )
          else:
              return interval
  
      intervals = hl.eval(intervals)
      intervals = [wrap_input(i) for i in intervals]
  
      if isinstance(ds, MatrixTable):
          return MatrixTable(
              ir.MatrixFilterIntervals(
                  ds._mir, intervals, point_type, keep
              )
          )
      else:
          return Table(
              ir.TableFilterIntervals(
                  ds._tir, intervals, point_type, keep
              )
          )
  
imports: from typing import Union
  import hail
  from hail.expr import Expression, expr_numeric, expr_array, expr_interval, expr_any, construct_expr, construct_variable
  from hail.expr.types import tlocus, tarray, tstr, tstruct, ttuple
  from hail.matrixtable import MatrixTable
  from hail.table import Table
  from hail.typecheck import typecheck, nullable, func_spec, oneof
  from hail.utils import Interval, Struct, new_temp_file, deduplicate
  from hail.utils.misc import plural
  from hail.utils.java import Env, info
  from hail import ir
label_desc: Use image node_id 11 for calling a global function with example usage: filter_intervals(ds, intervals, keep) and returns: True, MatrixTable, Table, False, False, Interval, interval