def _nested_statement_end_idx(sql_query, action_idx):
  """If acion_idx is start of nested statement, return end_idx, else None."""
  if action_idx + 2 >= len(sql_query.actions):
    return None
  current_action = sql_query.actions[action_idx]
  next_action = sql_query.actions[action_idx + 1]
  if current_action.symbol == '(' and next_action.symbol.lower() == 'select':
    open_parens = 0
    for end_idx in range(action_idx + 1, len(sql_query.actions)):
      nested_action = sql_query.actions[end_idx]
      if nested_action.symbol == '(':
        open_parens += 1
      if nested_action.symbol == ')':
        if open_parens > 0:
          open_parens -= 1
        else:
          return end_idx
    raise ParseError('Could not find end index for nested statement: %s' %
                     sql_query)
  return None
If acion_idx is start of nested statement, return end_idx, else None.
language/xsp/data_preprocessing/abstract_sql_converters.py