Skip to content

Commit

Permalink
[SPARK-36896][PYTHON] Return boolean for dropTempView and `dropGlob…
Browse files Browse the repository at this point in the history
…alTempView`

### What changes were proposed in this pull request?
Currently `dropTempView` and `dropGlobalTempView` don't have return value, which conflicts with their docstring:
`Returns true if this view is dropped successfully, false otherwise.`. And that's not consistent with the same API in other languages.

The PR proposes a fix for that.

### Why are the changes needed?
Be consistent with API in other languages.

### Does this PR introduce _any_ user-facing change?
Yes.
#### From
```py
# dropTempView
>>> spark.createDataFrame([(1, 1)]).createTempView("my_table")
>>> spark.table("my_table").collect()
[Row(_1=1, _2=1)]
>>> spark.catalog.dropTempView("my_table")
>>> spark.catalog.dropTempView("my_table")

# dropGlobalTempView
>>> spark.createDataFrame([(1, 1)]).createGlobalTempView("my_table")
>>> spark.table("global_temp.my_table").collect()
[Row(_1=1, _2=1)]
>>> spark.catalog.dropGlobalTempView("my_table")
>>> spark.catalog.dropGlobalTempView("my_table")
```

#### To
```py
# dropTempView
>>> spark.createDataFrame([(1, 1)]).createTempView("my_table")
>>> spark.table("my_table").collect()
[Row(_1=1, _2=1)]
>>> spark.catalog.dropTempView("my_table")
True
>>> spark.catalog.dropTempView("my_table")
False

# dropGlobalTempView
>>> spark.createDataFrame([(1, 1)]).createGlobalTempView("my_table")
>>> spark.table("global_temp.my_table").collect()
[Row(_1=1, _2=1)]
>>> spark.catalog.dropGlobalTempView("my_table")
True
>>> spark.catalog.dropGlobalTempView("my_table")
False
```

### How was this patch tested?
Existing tests.

Closes #34147 from xinrong-databricks/fix_return.

Authored-by: Xinrong Meng <xinrong.meng@databricks.com>
Signed-off-by: Hyukjin Kwon <gurwls223@apache.org>
  • Loading branch information
xinrong-meng authored and HyukjinKwon committed Sep 30, 2021
1 parent b60e576 commit ad5a535
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 3 deletions.
8 changes: 5 additions & 3 deletions python/pyspark/sql/catalog.py
Expand Up @@ -50,7 +50,7 @@ def currentDatabase(self):
@since(2.0)
def setCurrentDatabase(self, dbName):
"""Sets the current default database in this session."""
return self._jcatalog.setCurrentDatabase(dbName)
self._jcatalog.setCurrentDatabase(dbName)

@since(2.0)
def listDatabases(self):
Expand Down Expand Up @@ -323,12 +323,13 @@ def dropTempView(self, viewName):
>>> spark.table("my_table").collect()
[Row(_1=1, _2=1)]
>>> spark.catalog.dropTempView("my_table")
True
>>> spark.table("my_table") # doctest: +IGNORE_EXCEPTION_DETAIL
Traceback (most recent call last):
...
AnalysisException: ...
"""
self._jcatalog.dropTempView(viewName)
return self._jcatalog.dropTempView(viewName)

def dropGlobalTempView(self, viewName):
"""Drops the global temporary view with the given view name in the catalog.
Expand All @@ -343,12 +344,13 @@ def dropGlobalTempView(self, viewName):
>>> spark.table("global_temp.my_table").collect()
[Row(_1=1, _2=1)]
>>> spark.catalog.dropGlobalTempView("my_table")
True
>>> spark.table("global_temp.my_table") # doctest: +IGNORE_EXCEPTION_DETAIL
Traceback (most recent call last):
...
AnalysisException: ...
"""
self._jcatalog.dropGlobalTempView(viewName)
return self._jcatalog.dropGlobalTempView(viewName)

def registerFunction(self, name, f, returnType=None):
"""An alias for :func:`spark.udf.register`.
Expand Down
6 changes: 6 additions & 0 deletions python/pyspark/sql/dataframe.py
Expand Up @@ -136,6 +136,8 @@ def registerTempTable(self, name):
>>> sorted(df.collect()) == sorted(df2.collect())
True
>>> spark.catalog.dropTempView("people")
True
"""
warnings.warn(
"Deprecated in 2.0, use createOrReplaceTempView instead.",
Expand Down Expand Up @@ -164,6 +166,7 @@ def createTempView(self, name):
...
AnalysisException: u"Temporary table 'people' already exists;"
>>> spark.catalog.dropTempView("people")
True
"""
self._jdf.createTempView(name)
Expand All @@ -185,6 +188,7 @@ def createOrReplaceTempView(self, name):
>>> sorted(df3.collect()) == sorted(df2.collect())
True
>>> spark.catalog.dropTempView("people")
True
"""
self._jdf.createOrReplaceTempView(name)
Expand All @@ -209,6 +213,7 @@ def createGlobalTempView(self, name):
...
AnalysisException: u"Temporary table 'people' already exists;"
>>> spark.catalog.dropGlobalTempView("people")
True
"""
self._jdf.createGlobalTempView(name)
Expand All @@ -229,6 +234,7 @@ def createOrReplaceGlobalTempView(self, name):
>>> sorted(df3.collect()) == sorted(df2.collect())
True
>>> spark.catalog.dropGlobalTempView("people")
True
"""
self._jdf.createOrReplaceGlobalTempView(name)
Expand Down

0 comments on commit ad5a535

Please sign in to comment.