Skip to content

Commit

Permalink
[SPARK-26946][SQL][FOLLOWUP] Handle lookupCatalog function not defined
Browse files Browse the repository at this point in the history
Treat it as catalog not found.
  • Loading branch information
jzhuge committed May 26, 2019
1 parent 447bfde commit a79cb84
Showing 1 changed file with 20 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@

package org.apache.spark.sql.catalog.v2

import scala.util.{Failure, Success, Try}

import org.apache.spark.annotation.Experimental
import org.apache.spark.sql.catalyst.TableIdentifier

Expand All @@ -26,27 +28,31 @@ import org.apache.spark.sql.catalyst.TableIdentifier
@Experimental
trait LookupCatalog {

def lookupCatalog: Option[(String) => CatalogPlugin] = None
def lookupCatalog: Option[String => CatalogPlugin] = None

type CatalogObjectIdentifier = (Option[CatalogPlugin], Identifier)

/**
* Extract catalog plugin and identifier from a multi-part identifier.
*/
object CatalogObjectIdentifier {
def unapply(parts: Seq[String]): Option[CatalogObjectIdentifier] = lookupCatalog.map { lookup =>
parts match {
case Seq(name) =>
(None, Identifier.of(Array.empty, name))
case Seq(catalogName, tail @ _*) =>
try {
val catalog = lookup(catalogName)
(Some(catalog), Identifier.of(tail.init.toArray, tail.last))
} catch {
case _: CatalogNotFoundException =>
(None, Identifier.of(parts.init.toArray, parts.last))
}
}
def unapply(parts: Seq[String]): Option[CatalogObjectIdentifier] = parts match {
case Seq(name) =>
Some((None, Identifier.of(Array.empty, name)))
case Seq(catalogName, tail @ _*) =>
lookupCatalog match {
case Some(lookup) =>
Try(lookup(catalogName)) match {
case Success(catalog) =>
Some((Some(catalog), Identifier.of(tail.init.toArray, tail.last)))
case Failure(_: CatalogNotFoundException) =>
Some((None, Identifier.of(parts.init.toArray, parts.last)))
case Failure(ex) =>
throw ex
}
case None =>
Some((None, Identifier.of(parts.init.toArray, parts.last)))
}
}
}

Expand Down

0 comments on commit a79cb84

Please sign in to comment.