code: def insert_rls_in_predicate(
      token_list: TokenList,
      database_id: int,
      default_schema: Optional[str],
  ) -> TokenList:
      """
      Update a statement inplace applying any associated RLS predicates.
  
      The RLS predicate is ``AND``ed to any existing predicates:
  
          before: SELECT * FROM some_table WHERE 1=1
          after:  SELECT * FROM some_table WHERE ( 1=1) AND some_table.id=42
  
      """
      rls: Optional[TokenList] = None
      state = InsertRLSState.SCANNING
      for token in token_list.tokens:
          # Recurse into child token list
          if isinstance(token, TokenList):
              i = token_list.tokens.index(token)
              token_list.tokens[i] = insert_rls_in_predicate(
                  token,
                  database_id,
                  default_schema,
              )
  
          # Found a source keyword (FROM/JOIN)
          if imt(token, m=[(Keyword, "FROM"), (Keyword, "JOIN")]):
              state = InsertRLSState.SEEN_SOURCE
  
          # Found identifier/keyword after FROM/JOIN, test for table
          elif state == InsertRLSState.SEEN_SOURCE and (
              isinstance(token, Identifier) or token.ttype == Keyword
          ):
              rls = get_rls_for_table(
                  token, database_id, default_schema
              )
              if rls:
                  state = InsertRLSState.FOUND_TABLE
  
          # Found WHERE clause, insert RLS. Note that we insert it even it already exists,
          # to be on the safe side: it could be present in a clause like `1=1 OR RLS`.
          elif state == InsertRLSState.FOUND_TABLE and isinstance(
              token, Where
          ):
              rls = cast(TokenList, rls)
              token.tokens[1:1] = [
                  Token(Whitespace, " "),
                  Token(Punctuation, "("),
              ]
              token.tokens.extend(
                  [
                      Token(Punctuation, ")"),
                      Token(Whitespace, " "),
                      Token(Keyword, "AND"),
                      Token(Whitespace, " "),
                  ]
                  + rls.tokens
              )
              state = InsertRLSState.SCANNING
  
          # Found ON clause, insert RLS. The logic for ON is more complicated than the logic
          # for WHERE because in the former the comparisons are siblings, while on the
          # latter they are children.
          elif (
              state == InsertRLSState.FOUND_TABLE
              and token.ttype == Keyword
              and token.value.upper() == "ON"
          ):
              tokens = [
                  Token(Whitespace, " "),
                  rls,
                  Token(Whitespace, " "),
                  Token(Keyword, "AND"),
                  Token(Whitespace, " "),
                  Token(Punctuation, "("),
              ]
              i = token_list.tokens.index(token)
              token.parent.tokens[i + 1 : i + 1] = tokens
              i += len(tokens) + 2
  
              # close parenthesis after last existing comparison
              j = 0
              for j, sibling in enumerate(token_list.tokens[i:]):
                  # scan until we hit a non-comparison keyword (like ORDER BY) or a WHERE
                  if (
                      sibling.ttype == Keyword
                      and not imt(
                          sibling,
                          m=[
                              (Keyword, "AND"),
                              (Keyword, "OR"),
                              (Keyword, "NOT"),
                          ],
                      )
                      or isinstance(sibling, Where)
                  ):
                      j -= 1
                      break
              token.parent.tokens[i + j + 1 : i + j + 1] = [
                  Token(Whitespace, " "),
                  Token(Punctuation, ")"),
                  Token(Whitespace, " "),
              ]
  
              state = InsertRLSState.SCANNING
  
          # Found table but no WHERE clause found, insert one
          elif (
              state == InsertRLSState.FOUND_TABLE
              and token.ttype != Whitespace
          ):
              i = token_list.tokens.index(token)
              token_list.tokens[i:i] = [
                  Token(Whitespace, " "),
                  Where(
                      [
                          Token(Keyword, "WHERE"),
                          Token(Whitespace, " "),
                          rls,
                      ]
                  ),
                  Token(Whitespace, " "),
              ]
  
              state = InsertRLSState.SCANNING
  
          # Found nothing, leaving source
          elif (
              state == InsertRLSState.SEEN_SOURCE
              and token.ttype != Whitespace
          ):
              state = InsertRLSState.SCANNING
  
      # found table at the end of the statement; append a WHERE clause
      if state == InsertRLSState.FOUND_TABLE:
          token_list.tokens.extend(
              [
                  Token(Whitespace, " "),
                  Where(
                      [
                          Token(Keyword, "WHERE"),
                          Token(Whitespace, " "),
                          rls,
                      ]
                  ),
              ]
          )
  
      return token_list
  
imports: import logging
  import re
  from collections.abc import Iterator
  from dataclasses import dataclass
  from typing import Any, cast, Optional
  from urllib import parse
  import sqlparse
  from sqlalchemy import and_
  from sqlparse import keywords
  from sqlparse.lexer import Lexer
  from sqlparse.sql import Identifier, IdentifierList, Parenthesis, remove_quotes, Token, TokenList, Where
  from sqlparse.tokens import Comment, CTE, DDL, DML, Keyword, Name, Punctuation, String, Whitespace, Wildcard
  from sqlparse.utils import imt
  from superset.exceptions import QueryClauseValidationException
  from superset.utils.backports import StrEnum
label_desc: Use image node_id 10 for calling a global function with example usage: insert_rls_in_predicate(token_list, database_id, default_schema) and returns: token_list