(
    sql: str,
    add_comments: List[str],
    add_hints: List[str],
    add_index_hints: List[Tuple[str, str, str, str]],
)
return " ".join(tokens)
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
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
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
def modify_sql(
    sql: str,
    add_comments: List[str],
    add_hints: List[str],
    add_index_hints: List[Tuple[str, str, str, str]],
) -> str:
    """
    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
    """
    match = query_start_re.match(sql)
    if not match:
        # We don't understand what kind of query this is, don't rewrite it
        return sql

    tokens = [match.group("keyword")]
    comments = match.group("comments").strip()
    if comments:
        tokens.append(comments)

    # Inject comments after all existing comments
    for comment in add_comments:
        tokens.append(f"/*{comment}*/")

    # Don't bother with SELECT rewrite rules on non-SELECT queries
    if tokens[0] == "SELECT":

        for group_name, hint_set in SELECT_HINTS.items():

            try:
                # Take the last hint we were told to add from this hint_set
                to_add = [hint for hint in add_hints if hint in hint_set][-1]
                tokens.append(to_add)
            except IndexError:
                # We weren't told to add any, so just add any hint from this
                # set that was already there
                existing = match.group(group_name)
                if existing is not None:
                    tokens.append(existing.rstrip())

    # Maybe rewrite the remainder of the statement for index hints
    remainder = sql[match.end() :]

    if tokens[0] == "SELECT" and add_index_hints:
        for index_hint in add_index_hints:
            remainder = modify_sql_index_hints(remainder, *index_hint)

    # Join everything
    tokens.append(remainder)
    return " ".join(tokens)
def modify_sql ( sql : str , add_comments : List [ str ] , add_hints : List [ str ] , add_index_hints : List [ Tuple [ str , str , str , str ] ] , ) -> str : match = query_start_re . match ( sql ) if not match : # We don't understand what kind of query this is, don't rewrite it return sql tokens = [ match . group ( "keyword" ) ] comments = match . group ( "comments" ) . strip ( ) if comments : tokens . append ( comments ) # Inject comments after all existing comments for comment in add_comments : tokens . append ( f"/*{comment}*/" ) # Don't bother with SELECT rewrite rules on non-SELECT queries if tokens [ 0 ] == "SELECT" : for group_name , hint_set in SELECT_HINTS . items ( ) : try : # Take the last hint we were told to add from this hint_set to_add = [ hint for hint in add_hints if hint in hint_set ] [ - 1 ] tokens . append ( to_add ) except IndexError : # We weren't told to add any, so just add any hint from this # set that was already there existing = match . group ( group_name ) if existing is not None : tokens . append ( existing . rstrip ( ) ) # Maybe rewrite the remainder of the statement for index hints remainder = sql [ match . end ( ) : ] if tokens [ 0 ] == "SELECT" and add_index_hints : for index_hint in add_index_hints : remainder = modify_sql_index_hints ( remainder , * index_hint ) # Join everything tokens . append ( remainder ) return " " . join ( tokens )