def modify_sql(sql, add_comments, add_hints, add_index_hints):
match = query_start_re.match(sql)<EOL>if not match:<EOL><INDENT>return sql<EOL><DEDENT>tokens = [match.group('<STR_LIT>')]<EOL>comments = match.group('<STR_LIT>').strip()<EOL>if comments:<EOL><INDENT>tokens.append(comments)<EOL><DEDENT>for comment in add_comments:<EOL><INDENT>tokens.append('<STR_LIT>'.format(comment))<EOL><DEDENT>if tokens[<NUM_LIT:0>] == "<STR_LIT>":<EOL><INDENT>for group_name, hint_set in SELECT_HINTS.items():<EOL><INDENT>try:<EOL><INDENT>to_add = [hint for hint in add_hints if hint in hint_set][-<NUM_LIT:1>]<EOL>tokens.append(to_add)<EOL><DEDENT>except IndexError:<EOL><INDENT>existing = match.group(group_name)<EOL>if existing is not None:<EOL><INDENT>tokens.append(existing.rstrip())<EOL><DEDENT><DEDENT><DEDENT><DEDENT>remainder = sql[match.end():]<EOL>if tokens[<NUM_LIT:0>] == "<STR_LIT>" and add_index_hints:<EOL><INDENT>for index_hint in add_index_hints:<EOL><INDENT>remainder = modify_sql_index_hints(remainder, *index_hint)<EOL><DEDENT><DEDENT>tokens.append(remainder)<EOL>return '<STR_LIT:U+0020>'.join(tokens)<EOL>
Parse the start of the SQL, injecting each string in add_comments in
individual SQL comments after the first keyword, and adding the named
SELECT hints from add_hints, taking the latest in the list in cases of
multiple mutually exclusive hints being given