(self, sql: str, ctes: List[InjectedCTE])
return str(parsed)
`ctes` is a list of InjectedCTEs like:

            [
                InjectedCTE(
                    id="cte_id_1",
                    sql="__dbt__cte__ephemeral as (select * from table)",
                ),
                InjectedCTE(
                    id="cte_id_2",
                    sql="__dbt__cte__events as (select id, type from events)",
                ),
            ]

        Given `sql` like:

          "with internal_cte as (select * from sessions)
           select * from internal_cte"

        This will spit out:

          "with __dbt__cte__ephemeral as (select * from table),
                __dbt__cte__events as (select id, type from events),
                with internal_cte as (select * from sessions)
           select * from internal_cte"

        (Whitespace enhanced for readability.)
`ctes` is a list of InjectedCTEs like:
ctes is a list of InjectedCTEs like :
def _inject_ctes_into_sql(self, sql: str, ctes: List[InjectedCTE]) -> str:
        """
        `ctes` is a list of InjectedCTEs like:

            [
                InjectedCTE(
                    id="cte_id_1",
                    sql="__dbt__cte__ephemeral as (select * from table)",
                ),
                InjectedCTE(
                    id="cte_id_2",
                    sql="__dbt__cte__events as (select id, type from events)",
                ),
            ]

        Given `sql` like:

          "with internal_cte as (select * from sessions)
           select * from internal_cte"

        This will spit out:

          "with __dbt__cte__ephemeral as (select * from table),
                __dbt__cte__events as (select id, type from events),
                with internal_cte as (select * from sessions)
           select * from internal_cte"

        (Whitespace enhanced for readability.)
        """
        if len(ctes) == 0:
            return sql

        parsed_stmts = sqlparse.parse(sql)
        parsed = parsed_stmts[0]

        with_stmt = None
        for token in parsed.tokens:
            if token.is_keyword and token.normalized == 'WITH':
                with_stmt = token
                break

        if with_stmt is None:
            # no with stmt, add one, and inject CTEs right at the beginning
            first_token = parsed.token_first()
            with_stmt = sqlparse.sql.Token(sqlparse.tokens.Keyword, 'with')
            parsed.insert_before(first_token, with_stmt)
        else:
            # stmt exists, add a comma (which will come after injected CTEs)
            trailing_comma = sqlparse.sql.Token(
                sqlparse.tokens.Punctuation, ','
            )
            parsed.insert_after(with_stmt, trailing_comma)

        token = sqlparse.sql.Token(
            sqlparse.tokens.Keyword,
            ", ".join(c.sql for c in ctes)
        )
        parsed.insert_after(with_stmt, token)

        return str(parsed)
def _inject_ctes_into_sql ( self , sql : str , ctes : List [ InjectedCTE ] ) -> str : if len ( ctes ) == 0 : return sql parsed_stmts = sqlparse . parse ( sql ) parsed = parsed_stmts [ 0 ] with_stmt = None for token in parsed . tokens : if token . is_keyword and token . normalized == 'WITH' : with_stmt = token break if with_stmt is None : # no with stmt, add one, and inject CTEs right at the beginning first_token = parsed . token_first ( ) with_stmt = sqlparse . sql . Token ( sqlparse . tokens . Keyword , 'with' ) parsed . insert_before ( first_token , with_stmt ) else : # stmt exists, add a comma (which will come after injected CTEs) trailing_comma = sqlparse . sql . Token ( sqlparse . tokens . Punctuation , ',' ) parsed . insert_after ( with_stmt , trailing_comma ) token = sqlparse . sql . Token ( sqlparse . tokens . Keyword , ", " . join ( c . sql for c in ctes ) ) parsed . insert_after ( with_stmt , token ) return str ( parsed )