Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
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
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
package org.apache.spark.sql.internal

import java.util.TimeZone
import java.util.concurrent.atomic.AtomicReference

import scala.util.Try

Expand Down Expand Up @@ -48,25 +47,14 @@ private[sql] trait SqlApiConf {

private[sql] object SqlApiConf {
// Shared keys.
val ANSI_ENABLED_KEY: String = "spark.sql.ansi.enabled"
val LEGACY_TIME_PARSER_POLICY_KEY: String = "spark.sql.legacy.timeParserPolicy"
val CASE_SENSITIVE_KEY: String = "spark.sql.caseSensitive"
val SESSION_LOCAL_TIMEZONE_KEY: String = "spark.sql.session.timeZone"
val LOCAL_RELATION_CACHE_THRESHOLD_KEY: String = "spark.sql.session.localRelationCacheThreshold"
val ANSI_ENABLED_KEY: String = SqlApiConfHelper.ANSI_ENABLED_KEY
val LEGACY_TIME_PARSER_POLICY_KEY: String = SqlApiConfHelper.LEGACY_TIME_PARSER_POLICY_KEY
val CASE_SENSITIVE_KEY: String = SqlApiConfHelper.CASE_SENSITIVE_KEY
val SESSION_LOCAL_TIMEZONE_KEY: String = SqlApiConfHelper.SESSION_LOCAL_TIMEZONE_KEY
val LOCAL_RELATION_CACHE_THRESHOLD_KEY: String =
SqlApiConfHelper.LOCAL_RELATION_CACHE_THRESHOLD_KEY

/**
* Defines a getter that returns the [[SqlApiConf]] within scope.
*/
private val confGetter = new AtomicReference[() => SqlApiConf](() => DefaultSqlApiConf)

/**
* Sets the active config getter.
*/
private[sql] def setConfGetter(getter: () => SqlApiConf): Unit = {
confGetter.set(getter)
}

def get: SqlApiConf = confGetter.get()()
def get: SqlApiConf = SqlApiConfHelper.getConfGetter.get()()

// Force load SQLConf. This will trigger the installation of a confGetter that points to SQLConf.
Try(SparkClassUtils.classForName("org.apache.spark.sql.internal.SQLConf$"))
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.spark.sql.internal

import java.util.concurrent.atomic.AtomicReference

/**
* SqlApiConfHelper is created to avoid a deadlock during a concurrent access to SQLConf and
* SqlApiConf, which is because SQLConf and SqlApiConf tries to load each other upon
* initializations. SqlApiConfHelper is private to sql package and is not supposed to be
* accessed by end users. Variables and methods within SqlApiConfHelper are defined to
* be used by SQLConf and SqlApiConf only.
*/
private[sql] object SqlApiConfHelper {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you add some documentation explaining why this is needed? And how this code should never be used by an end-user.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

+1 to document the rules to use SqlApiConfHelper.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

// Shared keys.
val ANSI_ENABLED_KEY: String = "spark.sql.ansi.enabled"
val LEGACY_TIME_PARSER_POLICY_KEY: String = "spark.sql.legacy.timeParserPolicy"
val CASE_SENSITIVE_KEY: String = "spark.sql.caseSensitive"
val SESSION_LOCAL_TIMEZONE_KEY: String = "spark.sql.session.timeZone"
val LOCAL_RELATION_CACHE_THRESHOLD_KEY: String = "spark.sql.session.localRelationCacheThreshold"

val confGetter: AtomicReference[() => SqlApiConf] = {
new AtomicReference[() => SqlApiConf](() => DefaultSqlApiConf)
}

def getConfGetter: AtomicReference[() => SqlApiConf] = confGetter
Copy link
Member

@zsxwing zsxwing Jan 5, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If this line is called in a JVM before any code touches object SQLConf, are we going to return DefaultSqlApiConf which is wrong?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@hvanhovell Do you know if the scenario mentioned here could happen?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@zsxwing good point! How about this: We make SqlApiConf the only place to deal with the active conf problem. It should use reflection to get the active SQLConf and set it as the getter. SQLConf should only access this new SqlApiConfHelper for config names.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@zsxwing I don't think it is an issue. An end user should never touch this object unless they know what they are doing. That is a limitation of this approach, we should document this, and we can add a liniting rule if we must.

Just to make sure that we are on the same page. In a normal situation, the initialisation is as follows:

  • User touches the SQLConf object. This loads and initialises the SQLConf object. The loading of the SQLConf object triggers the initialisation of the SqlApiConfHelper because it needs to set the setter, and it needs to load the shared keys.
  • User touces the SqlApiConf object. This tries to load the SQLConf object as part of its initialisation (see previous bullet), if it is on the classpath the getter will be updated to return the SQLConf.


/**
* Sets the active config getter.
*/
def setConfGetter(getter: () => SqlApiConf): Unit = {
confGetter.set(getter)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@ object SQLConf {

// Make sure SqlApiConf is always in sync with SQLConf. SqlApiConf will always try to
// load SqlConf to make sure both classes are in sync from the get go.
SqlApiConf.setConfGetter(() => SQLConf.get)
SqlApiConfHelper.setConfGetter(() => SQLConf.get)

/**
* Returns the active config object within the current scope. If there is an active SparkSession,
Expand Down Expand Up @@ -915,7 +915,7 @@ object SQLConf {
.booleanConf
.createWithDefault(false)

val CASE_SENSITIVE = buildConf(SqlApiConf.CASE_SENSITIVE_KEY)
val CASE_SENSITIVE = buildConf(SqlApiConfHelper.CASE_SENSITIVE_KEY)
.internal()
.doc("Whether the query analyzer should be case sensitive or not. " +
"Default to case insensitive. It is highly discouraged to turn on case sensitive mode.")
Expand Down Expand Up @@ -2757,7 +2757,7 @@ object SQLConf {
Try { DateTimeUtils.getZoneId(zone) }.isSuccess
}

val SESSION_LOCAL_TIMEZONE = buildConf(SqlApiConf.SESSION_LOCAL_TIMEZONE_KEY)
val SESSION_LOCAL_TIMEZONE = buildConf(SqlApiConfHelper.SESSION_LOCAL_TIMEZONE_KEY)
.doc("The ID of session local timezone in the format of either region-based zone IDs or " +
"zone offsets. Region IDs must have the form 'area/city', such as 'America/Los_Angeles'. " +
"Zone offsets must be in the format '(+|-)HH', '(+|-)HH:mm' or '(+|-)HH:mm:ss', e.g '-08', " +
Expand Down Expand Up @@ -3281,7 +3281,7 @@ object SQLConf {
.checkValues(StoreAssignmentPolicy.values.map(_.toString))
.createWithDefault(StoreAssignmentPolicy.ANSI.toString)

val ANSI_ENABLED = buildConf(SqlApiConf.ANSI_ENABLED_KEY)
val ANSI_ENABLED = buildConf(SqlApiConfHelper.ANSI_ENABLED_KEY)
.doc("When true, Spark SQL uses an ANSI compliant dialect instead of being Hive compliant. " +
"For example, Spark will throw an exception at runtime instead of returning null results " +
"when the inputs to a SQL operator/function are invalid." +
Expand Down Expand Up @@ -3914,7 +3914,7 @@ object SQLConf {
.booleanConf
.createWithDefault(false)

val LEGACY_TIME_PARSER_POLICY = buildConf(SqlApiConf.LEGACY_TIME_PARSER_POLICY_KEY)
val LEGACY_TIME_PARSER_POLICY = buildConf(SqlApiConfHelper.LEGACY_TIME_PARSER_POLICY_KEY)
.internal()
.doc("When LEGACY, java.text.SimpleDateFormat is used for formatting and parsing " +
"dates/timestamps in a locale-sensitive manner, which is the approach before Spark 3.0. " +
Expand Down Expand Up @@ -4476,7 +4476,7 @@ object SQLConf {
.createWithDefault(false)

val LOCAL_RELATION_CACHE_THRESHOLD =
buildConf(SqlApiConf.LOCAL_RELATION_CACHE_THRESHOLD_KEY)
buildConf(SqlApiConfHelper.LOCAL_RELATION_CACHE_THRESHOLD_KEY)
.doc("The threshold for the size in bytes of local relations to be cached at " +
"the driver side after serialization.")
.version("3.5.0")
Expand Down