-
Notifications
You must be signed in to change notification settings - Fork 29.3k
[SPARK-33075][SQL] Enable auto bucketed scan by default (disable only for cached query) #30138
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 4 commits
7315be8
610b598
da54eaa
09c6ca9
8c1d11e
9e6aaf4
070da00
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -29,7 +29,7 @@ import org.apache.spark.{SPARK_VERSION, SparkConf, SparkContext, TaskContext} | |
| import org.apache.spark.annotation.{DeveloperApi, Experimental, Stable, Unstable} | ||
| import org.apache.spark.api.java.JavaRDD | ||
| import org.apache.spark.internal.Logging | ||
| import org.apache.spark.internal.config.EXECUTOR_ALLOW_SPARK_CONTEXT | ||
| import org.apache.spark.internal.config.{ConfigEntry, EXECUTOR_ALLOW_SPARK_CONTEXT} | ||
| import org.apache.spark.rdd.RDD | ||
| import org.apache.spark.scheduler.{SparkListener, SparkListenerApplicationEnd} | ||
| import org.apache.spark.sql.catalog.Catalog | ||
|
|
@@ -1077,6 +1077,27 @@ object SparkSession extends Logging { | |
| throw new IllegalStateException("No active or default Spark session found"))) | ||
| } | ||
|
|
||
| /** | ||
| * Returns a cloned SparkSession with all specified configurations disabled, or | ||
| * the original SparkSession if all configurations are already disabled. | ||
| * | ||
| * @since 3.1.0 | ||
| */ | ||
| def getOrCloneSessionWithConfigsOff( | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. curious why we need to add this? what's the issue we are preventing? also why
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Oh,
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @viirya - sure, updated. |
||
| session: SparkSession, | ||
| configurations: Seq[ConfigEntry[Boolean]]): SparkSession = { | ||
| val configsEnabled = configurations.filter(session.sessionState.conf.getConf(_)) | ||
| if (configsEnabled.isEmpty) { | ||
| session | ||
| } else { | ||
| val newSession = session.cloneSession() | ||
| configsEnabled.foreach(conf => { | ||
| newSession.sessionState.conf.setConf(conf, false) | ||
| }) | ||
| newSession | ||
| } | ||
| } | ||
|
|
||
| //////////////////////////////////////////////////////////////////////////////////////// | ||
| // Private methods from now on | ||
| //////////////////////////////////////////////////////////////////////////////////////// | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -22,6 +22,7 @@ import scala.collection.immutable.IndexedSeq | |
| import org.apache.hadoop.fs.{FileSystem, Path} | ||
|
|
||
| import org.apache.spark.internal.Logging | ||
| import org.apache.spark.internal.config.ConfigEntry | ||
| import org.apache.spark.sql.{Dataset, SparkSession} | ||
| import org.apache.spark.sql.catalyst.expressions.{Attribute, SubqueryExpression} | ||
| import org.apache.spark.sql.catalyst.optimizer.EliminateResolvedHint | ||
|
|
@@ -31,6 +32,7 @@ import org.apache.spark.sql.execution.columnar.{DefaultCachedBatchSerializer, In | |
| import org.apache.spark.sql.execution.command.CommandUtils | ||
| import org.apache.spark.sql.execution.datasources.{FileIndex, HadoopFsRelation, LogicalRelation} | ||
| import org.apache.spark.sql.execution.datasources.v2.{DataSourceV2Relation, FileTable} | ||
| import org.apache.spark.sql.internal.SQLConf | ||
| import org.apache.spark.storage.StorageLevel | ||
| import org.apache.spark.storage.StorageLevel.MEMORY_AND_DISK | ||
|
|
||
|
|
@@ -55,6 +57,17 @@ class CacheManager extends Logging with AdaptiveSparkPlanHelper { | |
| @transient @volatile | ||
| private var cachedData = IndexedSeq[CachedData]() | ||
|
|
||
| /** | ||
| * Configurations needs to be turned off, to avoid regression for cached query, so that the | ||
| * outputPartitioning of the underlying cached query plan can be leveraged later. | ||
| * Configurations include: | ||
| * 1. AQE | ||
| * 2. Automatic bucketed table scan | ||
| */ | ||
| private val forceDisableConfigs: Seq[ConfigEntry[Boolean]] = Seq( | ||
| SQLConf.ADAPTIVE_EXECUTION_ENABLED, | ||
| SQLConf.AUTO_BUCKETED_SCAN_ENABLED) | ||
|
|
||
| /** Clears all cached tables. */ | ||
| def clearCache(): Unit = this.synchronized { | ||
| cachedData.foreach(_.cachedRepresentation.cacheBuilder.clearCache()) | ||
|
|
@@ -79,10 +92,11 @@ class CacheManager extends Logging with AdaptiveSparkPlanHelper { | |
| if (lookupCachedData(planToCache).nonEmpty) { | ||
| logWarning("Asked to cache already cached data.") | ||
| } else { | ||
| // Turn off AQE so that the outputPartitioning of the underlying plan can be leveraged. | ||
| val sessionWithAqeOff = getOrCloneSessionWithAqeOff(query.sparkSession) | ||
| val inMemoryRelation = sessionWithAqeOff.withActive { | ||
| val qe = sessionWithAqeOff.sessionState.executePlan(planToCache) | ||
| // Turn off configs so that the outputPartitioning of the underlying plan can be leveraged. | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit: this comment seems duplicated with above.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @viirya - removed. |
||
| val sessionWithConfigsOff = SparkSession.getOrCloneSessionWithConfigsOff( | ||
| query.sparkSession, forceDisableConfigs) | ||
| val inMemoryRelation = sessionWithConfigsOff.withActive { | ||
| val qe = sessionWithConfigsOff.sessionState.executePlan(planToCache) | ||
| InMemoryRelation( | ||
| storageLevel, | ||
| qe, | ||
|
|
@@ -188,10 +202,11 @@ class CacheManager extends Logging with AdaptiveSparkPlanHelper { | |
| } | ||
| needToRecache.map { cd => | ||
| cd.cachedRepresentation.cacheBuilder.clearCache() | ||
| // Turn off AQE so that the outputPartitioning of the underlying plan can be leveraged. | ||
| val sessionWithAqeOff = getOrCloneSessionWithAqeOff(spark) | ||
| val newCache = sessionWithAqeOff.withActive { | ||
| val qe = sessionWithAqeOff.sessionState.executePlan(cd.plan) | ||
| // Turn off configs so that the outputPartitioning of the underlying plan can be leveraged. | ||
| val sessionWithConfigsOff = SparkSession.getOrCloneSessionWithConfigsOff( | ||
| spark, forceDisableConfigs) | ||
| val newCache = sessionWithConfigsOff.withActive { | ||
| val qe = sessionWithConfigsOff.sessionState.executePlan(cd.plan) | ||
| InMemoryRelation(cd.cachedRepresentation.cacheBuilder, qe) | ||
| } | ||
| val recomputedPlan = cd.copy(cachedRepresentation = newCache) | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this is not needed for internal APIs.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@cloud-fan - removed.