Skip to content

Commit dfa6fb4

Browse files
luluortacloud-fan
authored andcommitted
[SPARK-33389][SQL] Make internal classes of SparkSession always using active SQLConf
### What changes were proposed in this pull request? This PR makes internal classes of SparkSession always using active SQLConf. We should remove all `conf: SQLConf`s from ctor-parameters of this classes (`Analyzer`, `SparkPlanner`, `SessionCatalog`, `CatalogManager` `SparkSqlParser` and etc.) and use `SQLConf.get` instead. ### Why are the changes needed? Code refine. ### Does this PR introduce _any_ user-facing change? No. ### How was this patch tested? Existing test Closes #30299 from luluorta/SPARK-33389. Authored-by: luluorta <[email protected]> Signed-off-by: Wenchen Fan <[email protected]>
1 parent aa508fc commit dfa6fb4

40 files changed

+226
-257
lines changed
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one or more
3+
* contributor license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright ownership.
5+
* The ASF licenses this file to You under the Apache License, Version 2.0
6+
* (the "License"); you may not use this file except in compliance with
7+
* the License. You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
package org.apache.spark.sql.catalyst
19+
20+
import org.apache.spark.sql.internal.SQLConf
21+
22+
/**
23+
* Trait for getting the active SQLConf.
24+
*/
25+
trait SQLConfHelper {
26+
27+
/**
28+
* The active config object within the current scope.
29+
* See [[SQLConf.get]] for more information.
30+
*/
31+
def conf: SQLConf = SQLConf.get
32+
}

sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/analysis/Analyzer.scala

Lines changed: 8 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -58,15 +58,14 @@ import org.apache.spark.util.Utils
5858
*/
5959
object SimpleAnalyzer extends Analyzer(
6060
new CatalogManager(
61-
new SQLConf().copy(SQLConf.CASE_SENSITIVE -> true),
6261
FakeV2SessionCatalog,
6362
new SessionCatalog(
6463
new InMemoryCatalog,
65-
EmptyFunctionRegistry,
66-
new SQLConf().copy(SQLConf.CASE_SENSITIVE -> true)) {
64+
EmptyFunctionRegistry) {
6765
override def createDatabase(dbDefinition: CatalogDatabase, ignoreIfExists: Boolean): Unit = {}
68-
}),
69-
new SQLConf().copy(SQLConf.CASE_SENSITIVE -> true))
66+
})) {
67+
override def resolver: Resolver = caseSensitiveResolution
68+
}
7069

7170
object FakeV2SessionCatalog extends TableCatalog {
7271
private def fail() = throw new UnsupportedOperationException
@@ -130,10 +129,8 @@ object AnalysisContext {
130129
* Provides a logical query plan analyzer, which translates [[UnresolvedAttribute]]s and
131130
* [[UnresolvedRelation]]s into fully typed objects using information in a [[SessionCatalog]].
132131
*/
133-
class Analyzer(
134-
override val catalogManager: CatalogManager,
135-
conf: SQLConf)
136-
extends RuleExecutor[LogicalPlan] with CheckAnalysis with LookupCatalog {
132+
class Analyzer(override val catalogManager: CatalogManager)
133+
extends RuleExecutor[LogicalPlan] with CheckAnalysis with LookupCatalog with SQLConfHelper {
137134

138135
private val v1SessionCatalog: SessionCatalog = catalogManager.v1SessionCatalog
139136

@@ -144,10 +141,8 @@ class Analyzer(
144141
override def isView(nameParts: Seq[String]): Boolean = v1SessionCatalog.isView(nameParts)
145142

146143
// Only for tests.
147-
def this(catalog: SessionCatalog, conf: SQLConf) = {
148-
this(
149-
new CatalogManager(conf, FakeV2SessionCatalog, catalog),
150-
conf)
144+
def this(catalog: SessionCatalog) = {
145+
this(new CatalogManager(FakeV2SessionCatalog, catalog))
151146
}
152147

153148
def executeAndCheck(plan: LogicalPlan, tracker: QueryPlanningTracker): LogicalPlan = {

sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/analysis/timeZoneAnalysis.scala

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,10 @@
1616
*/
1717
package org.apache.spark.sql.catalyst.analysis
1818

19+
import org.apache.spark.sql.catalyst.SQLConfHelper
1920
import org.apache.spark.sql.catalyst.expressions.{Cast, Expression, ListQuery, TimeZoneAwareExpression}
2021
import org.apache.spark.sql.catalyst.plans.logical.LogicalPlan
2122
import org.apache.spark.sql.catalyst.rules.Rule
22-
import org.apache.spark.sql.internal.SQLConf
2323
import org.apache.spark.sql.types.DataType
2424

2525
/**
@@ -47,10 +47,7 @@ object ResolveTimeZone extends Rule[LogicalPlan] {
4747
* Mix-in trait for constructing valid [[Cast]] expressions.
4848
*/
4949
trait CastSupport {
50-
/**
51-
* Configuration used to create a valid cast expression.
52-
*/
53-
def conf: SQLConf
50+
self: SQLConfHelper =>
5451

5552
/**
5653
* Create a Cast expression with the session local time zone.

sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/analysis/view.scala

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ package org.apache.spark.sql.catalyst.analysis
2020
import org.apache.spark.sql.catalyst.expressions.Alias
2121
import org.apache.spark.sql.catalyst.plans.logical.{LogicalPlan, Project, View}
2222
import org.apache.spark.sql.catalyst.rules.Rule
23-
import org.apache.spark.sql.internal.SQLConf
2423

2524
/**
2625
* This file defines view types and analysis rules related to views.
@@ -54,8 +53,6 @@ import org.apache.spark.sql.internal.SQLConf
5453
* completely resolved during the batch of Resolution.
5554
*/
5655
object EliminateView extends Rule[LogicalPlan] with CastSupport {
57-
override def conf: SQLConf = SQLConf.get
58-
5956
override def apply(plan: LogicalPlan): LogicalPlan = plan transformUp {
6057
// The child has the different output attributes with the View operator. Adds a Project over
6158
// the child of the view.

sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/catalog/SessionCatalog.scala

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -61,10 +61,11 @@ class SessionCatalog(
6161
externalCatalogBuilder: () => ExternalCatalog,
6262
globalTempViewManagerBuilder: () => GlobalTempViewManager,
6363
functionRegistry: FunctionRegistry,
64-
conf: SQLConf,
6564
hadoopConf: Configuration,
6665
parser: ParserInterface,
67-
functionResourceLoader: FunctionResourceLoader) extends Logging {
66+
functionResourceLoader: FunctionResourceLoader,
67+
cacheSize: Int = SQLConf.get.tableRelationCacheSize,
68+
cacheTTL: Long = SQLConf.get.metadataCacheTTL) extends SQLConfHelper with Logging {
6869
import SessionCatalog._
6970
import CatalogTypes.TablePartitionSpec
7071

@@ -77,18 +78,21 @@ class SessionCatalog(
7778
() => externalCatalog,
7879
() => new GlobalTempViewManager(conf.getConf(GLOBAL_TEMP_DATABASE)),
7980
functionRegistry,
80-
conf,
8181
new Configuration(),
8282
new CatalystSqlParser(),
83-
DummyFunctionResourceLoader)
83+
DummyFunctionResourceLoader,
84+
conf.tableRelationCacheSize,
85+
conf.metadataCacheTTL)
86+
}
87+
88+
// For testing only.
89+
def this(externalCatalog: ExternalCatalog, functionRegistry: FunctionRegistry) = {
90+
this(externalCatalog, functionRegistry, SQLConf.get)
8491
}
8592

8693
// For testing only.
8794
def this(externalCatalog: ExternalCatalog) = {
88-
this(
89-
externalCatalog,
90-
new SimpleFunctionRegistry,
91-
new SQLConf().copy(SQLConf.CASE_SENSITIVE -> true))
95+
this(externalCatalog, new SimpleFunctionRegistry)
9296
}
9397

9498
lazy val externalCatalog = externalCatalogBuilder()
@@ -136,9 +140,6 @@ class SessionCatalog(
136140
}
137141

138142
private val tableRelationCache: Cache[QualifiedTableName, LogicalPlan] = {
139-
val cacheSize = conf.tableRelationCacheSize
140-
val cacheTTL = conf.metadataCacheTTL
141-
142143
var builder = CacheBuilder.newBuilder()
143144
.maximumSize(cacheSize)
144145

sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/catalog/interface.scala

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ import org.apache.commons.lang3.StringUtils
2828

2929
import org.apache.spark.internal.Logging
3030
import org.apache.spark.sql.AnalysisException
31-
import org.apache.spark.sql.catalyst.{FunctionIdentifier, InternalRow, TableIdentifier}
31+
import org.apache.spark.sql.catalyst.{FunctionIdentifier, InternalRow, SQLConfHelper, TableIdentifier}
3232
import org.apache.spark.sql.catalyst.analysis.MultiInstanceRelation
3333
import org.apache.spark.sql.catalyst.expressions.{Attribute, AttributeMap, AttributeReference, Cast, ExprId, Literal}
3434
import org.apache.spark.sql.catalyst.plans.logical._
@@ -177,8 +177,7 @@ case class CatalogTablePartition(
177177
case class BucketSpec(
178178
numBuckets: Int,
179179
bucketColumnNames: Seq[String],
180-
sortColumnNames: Seq[String]) {
181-
def conf: SQLConf = SQLConf.get
180+
sortColumnNames: Seq[String]) extends SQLConfHelper {
182181

183182
if (numBuckets <= 0 || numBuckets > conf.bucketingMaxBuckets) {
184183
throw new AnalysisException(

sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/optimizer/Optimizer.scala

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -377,9 +377,8 @@ object SimpleTestOptimizer extends SimpleTestOptimizer
377377

378378
class SimpleTestOptimizer extends Optimizer(
379379
new CatalogManager(
380-
new SQLConf().copy(SQLConf.CASE_SENSITIVE -> true),
381380
FakeV2SessionCatalog,
382-
new SessionCatalog(new InMemoryCatalog, EmptyFunctionRegistry, new SQLConf())))
381+
new SessionCatalog(new InMemoryCatalog, EmptyFunctionRegistry)))
383382

384383
/**
385384
* Remove redundant aliases from a query plan. A redundant alias is an alias that does not change

sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/optimizer/PropagateEmptyRelation.scala

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@ import org.apache.spark.sql.catalyst.expressions._
2222
import org.apache.spark.sql.catalyst.plans._
2323
import org.apache.spark.sql.catalyst.plans.logical._
2424
import org.apache.spark.sql.catalyst.rules._
25-
import org.apache.spark.sql.internal.SQLConf
2625

2726
/**
2827
* Collapse plans consisting empty local relations generated by [[PruneFilters]].
@@ -47,8 +46,6 @@ object PropagateEmptyRelation extends Rule[LogicalPlan] with PredicateHelper wit
4746
private def nullValueProjectList(plan: LogicalPlan): Seq[NamedExpression] =
4847
plan.output.map{ a => Alias(cast(Literal(null), a.dataType), a.name)(a.exprId) }
4948

50-
override def conf: SQLConf = SQLConf.get
51-
5249
def apply(plan: LogicalPlan): LogicalPlan = plan transformUp {
5350
case p: Union if p.children.exists(isEmptyLocalRelation) =>
5451
val newChildren = p.children.filterNot(isEmptyLocalRelation)

sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/optimizer/StarSchemaDetection.scala

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,18 +19,16 @@ package org.apache.spark.sql.catalyst.optimizer
1919

2020
import scala.annotation.tailrec
2121

22+
import org.apache.spark.sql.catalyst.SQLConfHelper
2223
import org.apache.spark.sql.catalyst.expressions._
2324
import org.apache.spark.sql.catalyst.planning.PhysicalOperation
2425
import org.apache.spark.sql.catalyst.plans._
2526
import org.apache.spark.sql.catalyst.plans.logical._
26-
import org.apache.spark.sql.internal.SQLConf
2727

2828
/**
2929
* Encapsulates star-schema detection logic.
3030
*/
31-
object StarSchemaDetection extends PredicateHelper {
32-
33-
private def conf = SQLConf.get
31+
object StarSchemaDetection extends PredicateHelper with SQLConfHelper {
3432

3533
/**
3634
* Star schema consists of one or more fact tables referencing a number of dimension

sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/parser/AstBuilder.scala

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ import org.antlr.v4.runtime.tree.{ParseTree, RuleNode, TerminalNode}
2828

2929
import org.apache.spark.internal.Logging
3030
import org.apache.spark.sql.AnalysisException
31-
import org.apache.spark.sql.catalyst.{FunctionIdentifier, TableIdentifier}
31+
import org.apache.spark.sql.catalyst.{FunctionIdentifier, SQLConfHelper, TableIdentifier}
3232
import org.apache.spark.sql.catalyst.analysis._
3333
import org.apache.spark.sql.catalyst.catalog.{BucketSpec, CatalogStorageFormat, FunctionResource, FunctionResourceType}
3434
import org.apache.spark.sql.catalyst.expressions._
@@ -51,11 +51,9 @@ import org.apache.spark.util.random.RandomSampler
5151
* The AstBuilder converts an ANTLR4 ParseTree into a catalyst Expression, LogicalPlan or
5252
* TableIdentifier.
5353
*/
54-
class AstBuilder extends SqlBaseBaseVisitor[AnyRef] with Logging {
54+
class AstBuilder extends SqlBaseBaseVisitor[AnyRef] with SQLConfHelper with Logging {
5555
import ParserUtils._
5656

57-
protected def conf: SQLConf = SQLConf.get
58-
5957
protected def typedVisit[T](ctx: ParseTree): T = {
6058
ctx.accept(this).asInstanceOf[T]
6159
}

0 commit comments

Comments
 (0)