Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Fix temp view handling: views have precedence over catalog lookups.
  • Loading branch information
rdblue committed Jul 9, 2019
commit 4169a8760d6e358914071460c91f381d4ae89b0b
Original file line number Diff line number Diff line change
Expand Up @@ -669,6 +669,10 @@ class Analyzer(
import org.apache.spark.sql.catalog.v2.utils.CatalogV2Util._

def apply(plan: LogicalPlan): LogicalPlan = plan.resolveOperatorsUp {
case u @ UnresolvedRelation(AsTemporaryViewIdentifier(ident))
if catalog.isTemporaryTable(ident) =>
u // temporary views take precedence over catalog table names

case u @ UnresolvedRelation(CatalogObjectIdentifier(Some(catalogPlugin), ident)) =>
loadTable(catalogPlugin, ident).map(DataSourceV2Relation.create).getOrElse(u)
}
Expand Down Expand Up @@ -706,6 +710,10 @@ class Analyzer(
// Note this is compatible with the views defined by older versions of Spark(before 2.2), which
// have empty defaultDatabase and all the relations in viewText have database part defined.
def resolveRelation(plan: LogicalPlan): LogicalPlan = plan match {
case u @ UnresolvedRelation(AsTemporaryViewIdentifier(ident))
if catalog.isTemporaryTable(ident) =>
resolveRelation(lookupTableFromCatalog(ident, u, AnalysisContext.get.defaultDatabase))

case u @ UnresolvedRelation(AsTableIdentifier(ident)) if !isRunningDirectlyOnFiles(ident) =>
val defaultDatabase = AnalysisContext.get.defaultDatabase
val foundRelation = lookupTableFromCatalog(ident, u, defaultDatabase)
Expand All @@ -715,10 +723,6 @@ class Analyzer(
u
}

case u @ UnresolvedRelation(AsTemporaryViewIdentifier(ident))
if catalog.isTemporaryTable(ident) =>
resolveRelation(lookupTableFromCatalog(ident, u, AnalysisContext.get.defaultDatabase))

// The view's child should be a logical plan parsed from the `desc.viewText`, the variable
// `viewText` should be defined, or else we throw an error on the generation of the View
// operator.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ class LookupCatalogSuite extends SparkFunSuite with LookupCatalog with Inside {
.foreach { sqlIdent =>
inside(parseMultipartIdentifier(sqlIdent)) {
case AsTemporaryViewIdentifier(_) =>
fail("AsTemporaryTableIdentifier should not match when " +
fail("AsTemporaryViewIdentifier should not match when " +
"the catalog is set or the namespace has multiple parts")
case _ =>
// expected
Expand Down Expand Up @@ -185,7 +185,7 @@ class LookupCatalogWithDefaultSuite extends SparkFunSuite with LookupCatalog wit
.foreach { sqlIdent =>
inside(parseMultipartIdentifier(sqlIdent)) {
case AsTemporaryViewIdentifier(_) =>
fail("AsTemporaryTableIdentifier should not match when " +
fail("AsTemporaryViewIdentifier should not match when " +
"the catalog is set or the namespace has multiple parts")
case _ =>
// expected
Expand Down