Skip to content

Commit

Permalink
[SPARK-30697][SQL] Handle database and namespace exceptions in catalo…
Browse files Browse the repository at this point in the history
…g.isView

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

Adds NoSuchDatabaseException and NoSuchNamespaceException to the `isView` method for SessionCatalog.

### Why are the changes needed?

This method prevents specialized resolutions from kicking in within Analysis when using V2 Catalogs if the identifier is a specialized identifier.

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

No

### How was this patch tested?

Added test to DataSourceV2SessionCatalogSuite

Closes #27423 from brkyvz/isViewF.

Authored-by: Burak Yavuz <brkyvz@gmail.com>
Signed-off-by: Wenchen Fan <wenchen@databricks.com>
  • Loading branch information
brkyvz authored and cloud-fan committed Feb 3, 2020
1 parent fb321b6 commit 2eccfd8
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -826,6 +826,8 @@ class SessionCatalog(
getTempViewOrPermanentTableMetadata(ident).tableType == CatalogTableType.VIEW
} catch {
case _: NoSuchTableException => false
case _: NoSuchDatabaseException => false
case _: NoSuchNamespaceException => false
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,13 @@ class InMemoryTableSessionCatalog extends TestV2SessionCatalogBase[InMemoryTable
new InMemoryTable(name, schema, partitions, properties)
}

override def loadTable(ident: Identifier): Table = {
val identToUse = Option(InMemoryTableSessionCatalog.customIdentifierResolution)
.map(_(ident))
.getOrElse(ident)
super.loadTable(identToUse)
}

override def alterTable(ident: Identifier, changes: TableChange*): Table = {
val fullIdent = fullIdentifier(ident)
Option(tables.get(fullIdent)) match {
Expand All @@ -125,6 +132,21 @@ class InMemoryTableSessionCatalog extends TestV2SessionCatalogBase[InMemoryTable
}
}

object InMemoryTableSessionCatalog {
private var customIdentifierResolution: Identifier => Identifier = _

def withCustomIdentifierResolver(
resolver: Identifier => Identifier)(
f: => Unit): Unit = {
try {
customIdentifierResolution = resolver
f
} finally {
customIdentifierResolution = null
}
}
}

private [connector] trait SessionCatalogTest[T <: Table, Catalog <: TestV2SessionCatalogBase[T]]
extends QueryTest
with SharedSparkSession
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,4 +49,18 @@ class DataSourceV2SQLSessionCatalogSuite
v2Catalog.asInstanceOf[TableCatalog]
.loadTable(Identifier.of(Array.empty, nameParts.last))
}

test("SPARK-30697: catalog.isView doesn't throw an error for specialized identifiers") {
val t1 = "tbl"
withTable(t1) {
sql(s"CREATE TABLE $t1 (id bigint, data string) USING $v2Format")

def idResolver(id: Identifier): Identifier = Identifier.of(Array.empty, id.name())

InMemoryTableSessionCatalog.withCustomIdentifierResolver(idResolver) {
// The following should not throw AnalysisException.
sql(s"DESCRIBE TABLE ignored.$t1")
}
}
}
}

0 comments on commit 2eccfd8

Please sign in to comment.