logicRelation
//ddl.scala
//viewDefinition 为logicRelation
case class CreateTempViewUsing(
tableIdent: TableIdentifier,
userSpecifiedSchema: Option[StructType],
replace: Boolean,
global: Boolean,
provider: String,
options: Map[String, String]) extends RunnableCommand {
if (tableIdent.database.isDefined) {
throw new AnalysisException(
s"Temporary view '$tableIdent' should not have specified a database")
}
override def argString: String = {
s"[tableIdent:$tableIdent " +
userSpecifiedSchema.map(_ + " ").getOrElse("") +
s"replace:$replace " +
s"provider:$provider " +
CatalogUtils.maskCredentials(options)
}
override def run(sparkSession: SparkSession): Seq[Row] = {
if (provider.toLowerCase(Locale.ROOT) == DDLUtils.HIVE_PROVIDER) {
throw new AnalysisException("Hive data source can only be used with tables, " +
"you can't use it with CREATE TEMP VIEW USING")
}
val dataSource = DataSource(
sparkSession,
userSpecifiedSchema = userSpecifiedSchema,
className = provider,
options = options)
val catalog = sparkSession.sessionState.catalog
val viewDefinition = Dataset.ofRows(
sparkSession, LogicalRelation(dataSource.resolveRelation())).logicalPlan
if (global) {
catalog.createGlobalTempView(tableIdent.table, viewDefinition, replace)
} else {
catalog.createTempView(tableIdent.table, viewDefinition, replace)
}
Seq.empty[Row]
}
}
//SessionCatalog.scala
//tempViews 为SessionCatalog 全局变量
protected val tempViews = new mutable.HashMap[String, LogicalPlan]
def createTempView(
name: String,
tableDefinition: LogicalPlan,
overrideIfExists: Boolean): Unit = synchronized {
val table = formatTableName(name)
if (tempViews.contains(table) && !overrideIfExists) {
throw new TempTableAlreadyExistsException(name)
}
tempViews.put(table, tableDefinition)
}