Github user tdas commented on a diff in the pull request: https://github.com/apache/spark/pull/16826#discussion_r103063700 --- Diff: sql/hive/src/main/scala/org/apache/spark/sql/hive/HiveSessionState.scala --- @@ -146,4 +107,153 @@ private[hive] class HiveSessionState(sparkSession: SparkSession) conf.getConf(HiveUtils.HIVE_THRIFT_SERVER_ASYNC) } + /** + * Get an identical copy of the `HiveSessionState`. + * This should ideally reuse the `SessionState.clone` but cannot do so. + * Doing that will throw an exception when trying to clone the catalog. + */ + override def clone(newSparkSession: SparkSession): HiveSessionState = { + val sparkContext = newSparkSession.sparkContext + val confCopy = conf.clone() + val functionRegistryCopy = functionRegistry.clone() + val experimentalMethodsCopy = experimentalMethods.clone() + val sqlParser: ParserInterface = new SparkSqlParser(confCopy) + val catalogCopy = catalog.clone( + newSparkSession, + confCopy, + SessionState.newHadoopConf(sparkContext.hadoopConfiguration, confCopy), + functionRegistryCopy, + sqlParser) + val queryExecutionCreator = (plan: LogicalPlan) => new QueryExecution(newSparkSession, plan) + + val hiveClient = + newSparkSession.sharedState.externalCatalog.asInstanceOf[HiveExternalCatalog].client + .newSession() + + SessionState.mergeSparkConf(confCopy, sparkContext.getConf) + + new HiveSessionState( + sparkContext, + newSparkSession.sharedState, + confCopy, + experimentalMethodsCopy, + functionRegistryCopy, + catalogCopy, + sqlParser, + hiveClient, + HiveSessionState.createAnalyzer(newSparkSession, catalogCopy, confCopy), + new StreamingQueryManager(newSparkSession), + queryExecutionCreator, + HiveSessionState.createPlannerCreator( + newSparkSession, + confCopy, + experimentalMethodsCopy)) + } + +} + +object HiveSessionState { + + def apply(sparkSession: SparkSession): HiveSessionState = { + apply(sparkSession, None) + } + + def apply( + sparkSession: SparkSession, + conf: Option[SQLConf]): HiveSessionState = { + + val initHelper = SessionState(sparkSession, conf) + + val sparkContext = sparkSession.sparkContext + + val catalog = HiveSessionCatalog( + sparkSession, + SessionState.createFunctionResourceLoader(sparkContext, sparkSession.sharedState), + initHelper.functionRegistry, + initHelper.conf, + SessionState.newHadoopConf(sparkContext.hadoopConfiguration, initHelper.conf), + initHelper.sqlParser) + + // A Hive client used for interacting with the metastore. + val metadataHive: HiveClient = + sparkSession.sharedState.externalCatalog.asInstanceOf[HiveExternalCatalog].client + .newSession() + + // An analyzer that uses the Hive metastore. + val analyzer: Analyzer = createAnalyzer(sparkSession, catalog, initHelper.conf) + + val plannerCreator = createPlannerCreator( + sparkSession, + initHelper.conf, + initHelper.experimentalMethods) + + new HiveSessionState( + sparkContext, + sparkSession.sharedState, + initHelper.conf, + initHelper.experimentalMethods, + initHelper.functionRegistry, + catalog, + initHelper.sqlParser, + metadataHive, + analyzer, + initHelper.streamingQueryManager, + initHelper.queryExecutionCreator, + plannerCreator) + } + + def createAnalyzer( + sparkSession: SparkSession, + catalog: HiveSessionCatalog, + sqlConf: SQLConf): Analyzer = { + + new Analyzer(catalog, sqlConf) { + override val extendedResolutionRules = + new ResolveHiveSerdeTable(sparkSession) :: + new FindDataSourceTable(sparkSession) :: + new FindHiveSerdeTable(sparkSession) :: + new ResolveSQLOnFile(sparkSession) :: Nil + + override val postHocResolutionRules = + catalog.ParquetConversions :: + catalog.OrcConversions :: + PreprocessTableCreation(sparkSession) :: + PreprocessTableInsertion(sqlConf) :: + DataSourceAnalysis(sqlConf) :: + HiveAnalysis :: Nil + + override val extendedCheckRules = Seq(PreWriteCheck) + } + } + + def createPlannerCreator( + associatedSparkSession: SparkSession, + sqlConf: SQLConf, + experimentalMethods: ExperimentalMethods): () => SparkPlanner = { + + () => + new SparkPlanner( + associatedSparkSession.sparkContext, + sqlConf, + experimentalMethods.extraStrategies) + with HiveStrategies { + + override val sparkSession: SparkSession = associatedSparkSession + + override def strategies: Seq[Strategy] = { + experimentalMethods.extraStrategies ++ Seq( + FileSourceStrategy, + DataSourceStrategy, + SpecialLimits, + InMemoryScans, + HiveTableScans, + Scripts, + Aggregation, + JoinSelection, + BasicOperators + ) + } + } + } + --- End diff -- nit: extra line.
--- If your project is set up for it, you can reply to this email and have your reply appear on GitHub as well. If your project does not have this feature enabled and wishes so, or if the feature is enabled but not working, please contact infrastructure at infrastruct...@apache.org or file a JIRA ticket with INFRA. --- --------------------------------------------------------------------- To unsubscribe, e-mail: reviews-unsubscr...@spark.apache.org For additional commands, e-mail: reviews-h...@spark.apache.org