def AllMentionedVariables(x, dive_in_combines=False):
  """Extracting all variables mentioned in an expression."""
  r = []
  if isinstance(x, dict) and 'variable' in x:
    r.append(x['variable']['var_name'])
  if isinstance(x, list):
    for v in x:
      r.extend(AllMentionedVariables(v, dive_in_combines))
  if isinstance(x, dict):
    for k in x:
      # Variables mentioned in 'combine' expression may be resolved via tables
      # of the 'combine' expression. So they are not to be included in the
      # parent query.
      if k != 'combine' or dive_in_combines:
        r.extend(AllMentionedVariables(x[k], dive_in_combines))
  return set(r)