Spark: Check Table Exists Quickly

This code snippet checks for table existence really fast:

def table_exists(spark: SparkSession, table_name: str) -> bool:
    # a more logical way would be to use spark catalog, however it's really slow
    from pyspark.sql.utils import AnalysisException
    try:
        spark.table(table_name)
    except AnalysisException:
        return False

    return True

Thanks! You can always email me or use contact form for more questions/comments etc.