Skip to content

Commit

Permalink
[SPARK-39547][SQL] V2SessionCatalog should not throw NoSuchDatabaseEx…
Browse files Browse the repository at this point in the history
…ception in loadNamspaceMetadata

### What changes were proposed in this pull request?

This change attempts to make V2SessionCatalog return NoSuchNameSpaceException rather than NoSuchDataseException

### Why are the changes needed?

if a catalog doesn't overrides `namespaceExists` it by default uses `loadNamespaceMetadata` and in case a `db` not exists loadNamespaceMetadata throws a `NoSuchDatabaseException` which is not catched and we see failures even with `if exists` clause. One such use case we observed was in iceberg table a post test clean up was failing with `NoSuchDatabaseException` now. Also queries such as `DROP TABLE IF EXISTS {}` fails with no such db exception.

### Does this PR introduce _any_ user-facing change?

No

### How was this patch tested?

Modified the UT to match the proposed behviour

Closes apache#36948 from singhpk234/fix/loadNamespaceMetadata.

Authored-by: Prashant Singh <psinghvk@amazon.com>
Signed-off-by: Wenchen Fan <wenchen@databricks.com>
  • Loading branch information
Prashant Singh authored and cloud-fan committed Jun 23, 2022
1 parent e5b7fb8 commit 9513393
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import scala.collection.JavaConverters._
import scala.collection.mutable

import org.apache.spark.sql.catalyst.{FunctionIdentifier, SQLConfHelper, TableIdentifier}
import org.apache.spark.sql.catalyst.analysis.{NoSuchTableException, TableAlreadyExistsException}
import org.apache.spark.sql.catalyst.analysis.{NoSuchDatabaseException, NoSuchTableException, TableAlreadyExistsException}
import org.apache.spark.sql.catalyst.catalog.{CatalogDatabase, CatalogTable, CatalogTableType, CatalogUtils, SessionCatalog}
import org.apache.spark.sql.connector.catalog.{CatalogManager, CatalogV2Util, FunctionCatalog, Identifier, NamespaceChange, SupportsNamespaces, Table, TableCatalog, TableChange, V1Table}
import org.apache.spark.sql.connector.catalog.NamespaceChange.RemoveProperty
Expand Down Expand Up @@ -245,7 +245,12 @@ class V2SessionCatalog(catalog: SessionCatalog)
override def loadNamespaceMetadata(namespace: Array[String]): util.Map[String, String] = {
namespace match {
case Array(db) =>
catalog.getDatabaseMetadata(db).toMetadata
try {
catalog.getDatabaseMetadata(db).toMetadata
} catch {
case _: NoSuchDatabaseException =>
throw QueryCompilationErrors.noSuchNamespaceError(namespace)
}

case _ =>
throw QueryCompilationErrors.noSuchNamespaceError(namespace)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,5 +59,6 @@ trait DescribeNamespaceSuiteBase extends command.DescribeNamespaceSuiteBase
* table catalog.
*/
class DescribeNamespaceSuite extends DescribeNamespaceSuiteBase with CommandSuiteBase {
override def notFoundMsgPrefix: String = if (conf.useV1Command) "Database" else "Namespace"
override def commandVersion: String = super[DescribeNamespaceSuiteBase].commandVersion
}
Original file line number Diff line number Diff line change
Expand Up @@ -850,7 +850,7 @@ class V2SessionCatalogNamespaceSuite extends V2SessionCatalogBaseSuite {
test("loadNamespaceMetadata: fail missing namespace") {
val catalog = newCatalog()

val exc = intercept[NoSuchDatabaseException] {
val exc = intercept[NoSuchNamespaceException] {
catalog.loadNamespaceMetadata(testNs)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,5 +24,6 @@ import org.apache.spark.sql.execution.command.v1
* table catalog.
*/
class DescribeNamespaceSuite extends v1.DescribeNamespaceSuiteBase with CommandSuiteBase {
override def notFoundMsgPrefix: String = if (conf.useV1Command) "Database" else "Namespace"
override def commandVersion: String = super[DescribeNamespaceSuiteBase].commandVersion
}

0 comments on commit 9513393

Please sign in to comment.