(
        self,
        model: NonSourceCompiledNode,
        manifest: Manifest,
        extra_context: Optional[Dict[str, Any]],
    )
return model, prepended_ctes
This method is called by the 'compile_node' method. Starting
        from the node that it is passed in, it will recursively call
        itself using the 'extra_ctes'.  The 'ephemeral' models do
        not produce SQL that is executed directly, instead they
        are rolled up into the models that refer to them by
        inserting CTEs into the SQL.
This method is called by the 'compile_node' method. Starting
        from the node that it is passed in, it will recursively call
        itself using the 'extra_ctes'.  The 'ephemeral' models do
        not produce SQL that is executed directly, instead they
        are rolled up into the models that refer to them by
        inserting CTEs into the SQL.
This method is called by the compile_node method . Starting from the node that it is passed in it will recursively call itself using the extra_ctes . The ephemeral models do not produce SQL that is executed directly instead they are rolled up into the models that refer to them by inserting CTEs into the SQL .
def _recursively_prepend_ctes(
        self,
        model: NonSourceCompiledNode,
        manifest: Manifest,
        extra_context: Optional[Dict[str, Any]],
    ) -> Tuple[NonSourceCompiledNode, List[InjectedCTE]]:
        """This method is called by the 'compile_node' method. Starting
        from the node that it is passed in, it will recursively call
        itself using the 'extra_ctes'.  The 'ephemeral' models do
        not produce SQL that is executed directly, instead they
        are rolled up into the models that refer to them by
        inserting CTEs into the SQL.
        """
        if model.compiled_sql is None:
            raise RuntimeException(
                'Cannot inject ctes into an unparsed node', model
            )
        if model.extra_ctes_injected:
            return (model, model.extra_ctes)

        # Just to make it plain that nothing is actually injected for this case
        if not model.extra_ctes:
            model.extra_ctes_injected = True
            manifest.update_node(model)
            return (model, model.extra_ctes)

        # This stores the ctes which will all be recursively
        # gathered and then "injected" into the model.
        prepended_ctes: List[InjectedCTE] = []

        # extra_ctes are added to the model by
        # RuntimeRefResolver.create_relation, which adds an
        # extra_cte for every model relation which is an
        # ephemeral model.
        for cte in model.extra_ctes:
            if cte.id not in manifest.nodes:
                raise InternalException(
                    f'During compilation, found a cte reference that '
                    f'could not be resolved: {cte.id}'
                )
            cte_model = manifest.nodes[cte.id]

            if not cte_model.is_ephemeral_model:
                raise InternalException(f'{cte.id} is not ephemeral')

            # This model has already been compiled, so it's been
            # through here before
            if getattr(cte_model, 'compiled', False):
                assert isinstance(cte_model, tuple(COMPILED_TYPES.values()))
                cte_model = cast(NonSourceCompiledNode, cte_model)
                new_prepended_ctes = cte_model.extra_ctes

            # if the cte_model isn't compiled, i.e. first time here
            else:
                # This is an ephemeral parsed model that we can compile.
                # Compile and update the node
                cte_model = self._compile_node(
                    cte_model, manifest, extra_context)
                # recursively call this method
                cte_model, new_prepended_ctes = \
                    self._recursively_prepend_ctes(
                        cte_model, manifest, extra_context
                    )
                # Save compiled SQL file and sync manifest
                self._write_node(cte_model)
                manifest.sync_update_node(cte_model)

            _extend_prepended_ctes(prepended_ctes, new_prepended_ctes)

            new_cte_name = self.add_ephemeral_prefix(cte_model.name)
            rendered_sql = (
                cte_model._pre_injected_sql or cte_model.compiled_sql
            )
            sql = f' {new_cte_name} as (\n{rendered_sql}\n)'

            _add_prepended_cte(prepended_ctes, InjectedCTE(id=cte.id, sql=sql))

        injected_sql = self._inject_ctes_into_sql(
            model.compiled_sql,
            prepended_ctes,
        )
        model._pre_injected_sql = model.compiled_sql
        model.compiled_sql = injected_sql
        model.extra_ctes_injected = True
        model.extra_ctes = prepended_ctes
        model.validate(model.to_dict(omit_none=True))

        manifest.update_node(model)

        return model, prepended_ctes
def _recursively_prepend_ctes ( self , model : NonSourceCompiledNode , manifest : Manifest , extra_context : Optional [ Dict [ str , Any ] ] , ) -> Tuple [ NonSourceCompiledNode , List [ InjectedCTE ] ] : if model . compiled_sql is None : raise RuntimeException ( 'Cannot inject ctes into an unparsed node' , model ) if model . extra_ctes_injected : return ( model , model . extra_ctes ) # Just to make it plain that nothing is actually injected for this case if not model . extra_ctes : model . extra_ctes_injected = True manifest . update_node ( model ) return ( model , model . extra_ctes ) # This stores the ctes which will all be recursively # gathered and then "injected" into the model. prepended_ctes : List [ InjectedCTE ] = [ ] # extra_ctes are added to the model by # RuntimeRefResolver.create_relation, which adds an # extra_cte for every model relation which is an # ephemeral model. for cte in model . extra_ctes : if cte . id not in manifest . nodes : raise InternalException ( f'During compilation, found a cte reference that ' f'could not be resolved: {cte.id}' ) cte_model = manifest . nodes [ cte . id ] if not cte_model . is_ephemeral_model : raise InternalException ( f'{cte.id} is not ephemeral' ) # This model has already been compiled, so it's been # through here before if getattr ( cte_model , 'compiled' , False ) : assert isinstance ( cte_model , tuple ( COMPILED_TYPES . values ( ) ) ) cte_model = cast ( NonSourceCompiledNode , cte_model ) new_prepended_ctes = cte_model . extra_ctes # if the cte_model isn't compiled, i.e. first time here else : # This is an ephemeral parsed model that we can compile. # Compile and update the node cte_model = self . _compile_node ( cte_model , manifest , extra_context ) # recursively call this method cte_model , new_prepended_ctes = self . _recursively_prepend_ctes ( cte_model , manifest , extra_context ) # Save compiled SQL file and sync manifest self . _write_node ( cte_model ) manifest . sync_update_node ( cte_model ) _extend_prepended_ctes ( prepended_ctes , new_prepended_ctes ) new_cte_name = self . add_ephemeral_prefix ( cte_model . name ) rendered_sql = ( cte_model . _pre_injected_sql or cte_model . compiled_sql ) sql = f' {new_cte_name} as (\n{rendered_sql}\n)' _add_prepended_cte ( prepended_ctes , InjectedCTE ( id = cte . id , sql = sql ) ) injected_sql = self . _inject_ctes_into_sql ( model . compiled_sql , prepended_ctes , ) model . _pre_injected_sql = model . compiled_sql model . compiled_sql = injected_sql model . extra_ctes_injected = True model . extra_ctes = prepended_ctes model . validate ( model . to_dict ( omit_none = True ) ) manifest . update_node ( model ) return model , prepended_ctes