def METHOD_NAME(
    self,
    source: exp.Select,
    name: str,
    joins: t.Dict[str, t.Optional[exp.Join]],
    group_by: t.List[exp.Expression],
    mapping: t.Dict[str, str],
) -> exp.Select:
    grain = [e.copy() for e in group_by]
    table_name = remove_namespace(name)
    mapping = {v: k for k, v in mapping.items()}
    for expr in grain:
        for node, *_ in expr.walk():
            if isinstance(node, exp.Column):
                models = self.graph.models_for_column(name, node.name)
                if name in models:
                    node.args["table"] = exp.to_identifier(table_name)
                elif models:
                    t = mapping.get(node.table)
                    model = next(
                        (model for model in models if remove_namespace(model) == t), models[0]
                    )
                    node.args["table"] = exp.to_identifier(t or remove_namespace(model))
                    if model not in joins:
                        joins[model] = None
    for target, join in joins.items():
        path = self.graph.find_path(name, target)
        for i in range(len(path) - 1):
            a_ref = path[i]
            b_ref = path[i + 1]
            a_model_alias = remove_namespace(a_ref.model_name)
            b_model_alias = remove_namespace(b_ref.model_name)
            a = a_ref.expression.copy()
            a.set("table", exp.to_identifier(a_model_alias))
            b = b_ref.expression.copy()
            b.set("table", exp.to_identifier(b_model_alias))
            on = a.eq(b)
            if join:
                join.set("on", on)
                source.append("joins", join)
            else:
                source.join(
                    b_ref.model_name,
                    on=on,
                    join_type="LEFT",
                    join_alias=b_model_alias,
                    dialect=self.dialect,
                    copy=False,
                )
    return source.select(*grain, copy=False).group_by(*grain, copy=False)