pyspark.sql.Catalog.tableExists

Catalog.tableExists(tableName: str, dbName: Optional[str] = None) → bool

Check if the table or view with the specified name exists. This can either be a temporary view or a table/view.

Parameters
tableNamestr

name of the table to check existence If no database is specified, first try to treat tableName as a multi-layer-namespace identifier, then try to tableName as a normal table name in current database if necessary.

dbNamestr, optional

name of the database to check table existence in.

Returns
bool

Indicating whether the table/view exists

Allowed tableName to be qualified with catalog name when dbName is None.

Examples

This function can check if a table is defined or not:

>>> spark.catalog.tableExists("unexisting_table")
False
>>> df = spark.sql("CREATE TABLE tab1 (name STRING, age INT) USING parquet")
>>> spark.catalog.tableExists("tab1")
True
>>> spark.catalog.tableExists("default.tab1")
True
>>> spark.catalog.tableExists("spark_catalog.default.tab1")
True
>>> spark.catalog.tableExists("tab1", "default")
True
>>> df = spark.sql("DROP TABLE tab1")
>>> spark.catalog.tableExists("unexisting_table")
False

It also works for views:

>>> spark.catalog.tableExists("view1")
False
>>> df = spark.sql("CREATE VIEW view1 AS SELECT 1")
>>> spark.catalog.tableExists("view1")
True
>>> spark.catalog.tableExists("default.view1")
True
>>> spark.catalog.tableExists("spark_catalog.default.view1")
True
>>> spark.catalog.tableExists("view1", "default")
True
>>> df = spark.sql("DROP VIEW view1")
>>> spark.catalog.tableExists("view1")
False

And also for temporary views:

>>> df = spark.sql("CREATE TEMPORARY VIEW view1 AS SELECT 1")
>>> spark.catalog.tableExists("view1")
True
>>> df = spark.sql("DROP VIEW view1")
>>> spark.catalog.tableExists("view1")
False