from typing import Union
from pathlib import Path
import re


def _alter_spark_sql(sql: str, hadoop_local: Union[str, Path]) -> str:
    """Handle special paths in SQL code so that it can be used locally.

    :param sql: A SQL query.
    :param hadoop_local: The local path of Hadoop.
    :return: The altered SQL code which can be used locally.
    """
    sql = re.sub(r"viewfs://[^/]+/", "/", sql)
    prefixes = ["/sys/", "/apps", "/user"]
    for prefix in prefixes:
        sql = sql.replace(prefix, f"{hadoop_local}{prefix}")
    return sql