def METHOD_NAME(query: str, frames: dict) -> str:
    """
    Build query to be executed.
    Table and column names are mapped to the real names
    using the WITH statement.
    Parameters
    ----------
    query : str
        SQL query to be processed.
    frames : dict
        DataFrames referenced by the query.
    Returns
    -------
    str
        SQL query to be executed.
    """
    alias = []
    for name, df in frames.items():
        assert isinstance(df._query_compiler, DFAlgQueryCompiler)
        mf = df._query_compiler._modin_frame
        table = mf.force_import()
        alias.append("WITH " if len(alias) == 0 else "\n),\n")
        alias.extend((name, " AS (\n", "  SELECT\n"))
        for i, col in enumerate(table.column_names):
            alias.append("    " if i == 0 else ",\n    ")
            alias.extend(('"', col, '"', " AS ", '"', ColNameCodec.decode(col), '"'))
        alias.extend(("\n  FROM\n    ", table.name))
    alias.extend(("\n)\n", query))
    return "".join(alias)