/
RuntimeConfig.scala
186 lines (167 loc) · 5.55 KB
/
RuntimeConfig.scala
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
/*
* 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
import org.apache.spark.annotation.Stable
import org.apache.spark.internal.Logging
import org.apache.spark.internal.config.{ConfigEntry, OptionalConfigEntry}
import org.apache.spark.sql.internal.SQLConf
import org.apache.spark.sql.internal.SQLConf.{DeprecatedConfig, RemovedConfig}
/**
* Runtime configuration interface for Spark. To access this, use `SparkSession.conf`.
*
* Options set here are automatically propagated to the Hadoop configuration during I/O.
*
* @since 2.0.0
*/
@Stable
class RuntimeConfig private[sql](sqlConf: SQLConf = new SQLConf) extends Logging {
/**
* Sets the given Spark runtime configuration property.
*
* @since 2.0.0
*/
def set(key: String, value: String): Unit = {
requireNonStaticConf(key)
requireDefaultValueOfRemovedConf(key, value)
logDeprecationWarning(key)
sqlConf.setConfString(key, value)
}
/**
* Sets the given Spark runtime configuration property.
*
* @since 2.0.0
*/
def set(key: String, value: Boolean): Unit = {
set(key, value.toString)
}
/**
* Sets the given Spark runtime configuration property.
*
* @since 2.0.0
*/
def set(key: String, value: Long): Unit = {
set(key, value.toString)
}
/**
* Returns the value of Spark runtime configuration property for the given key.
*
* @throws java.util.NoSuchElementException if the key is not set and does not have a default
* value
* @since 2.0.0
*/
@throws[NoSuchElementException]("if the key is not set")
def get(key: String): String = {
sqlConf.getConfString(key)
}
/**
* Returns the value of Spark runtime configuration property for the given key.
*
* @since 2.0.0
*/
def get(key: String, default: String): String = {
sqlConf.getConfString(key, default)
}
/**
* Returns the value of Spark runtime configuration property for the given key.
*/
@throws[NoSuchElementException]("if the key is not set")
protected[sql] def get[T](entry: ConfigEntry[T]): T = {
sqlConf.getConf(entry)
}
protected[sql] def get[T](entry: OptionalConfigEntry[T]): Option[T] = {
sqlConf.getConf(entry)
}
/**
* Returns the value of Spark runtime configuration property for the given key.
*/
protected[sql] def get[T](entry: ConfigEntry[T], default: T): T = {
sqlConf.getConf(entry, default)
}
/**
* Returns all properties set in this conf.
*
* @since 2.0.0
*/
def getAll: Map[String, String] = {
sqlConf.getAllConfs
}
/**
* Returns the value of Spark runtime configuration property for the given key.
*
* @since 2.0.0
*/
def getOption(key: String): Option[String] = {
try Option(get(key)) catch {
case _: NoSuchElementException => None
}
}
/**
* Resets the configuration property for the given key.
*
* @since 2.0.0
*/
def unset(key: String): Unit = {
requireNonStaticConf(key)
logDeprecationWarning(key)
sqlConf.unsetConf(key)
}
/**
* Indicates whether the configuration property with the given key
* is modifiable in the current session.
*
* @return `true` if the configuration property is modifiable. For static SQL, Spark Core,
* invalid (not existing) and other non-modifiable configuration properties,
* the returned value is `false`.
* @since 2.4.0
*/
def isModifiable(key: String): Boolean = sqlConf.isModifiable(key)
/**
* Returns whether a particular key is set.
*/
protected[sql] def contains(key: String): Boolean = {
sqlConf.contains(key)
}
private def requireNonStaticConf(key: String): Unit = {
if (SQLConf.staticConfKeys.contains(key)) {
throw new AnalysisException(s"Cannot modify the value of a static config: $key")
}
if (sqlConf.setCommandRejectsSparkCoreConfs &&
ConfigEntry.findEntry(key) != null && !SQLConf.sqlConfEntries.containsKey(key)) {
throw new AnalysisException(s"Cannot modify the value of a Spark config: $key")
}
}
private def requireDefaultValueOfRemovedConf(key: String, value: String): Unit = {
SQLConf.removedSQLConfigs.get(key).foreach {
case RemovedConfig(configName, version, defaultValue, comment) =>
if (value != defaultValue) {
throw new AnalysisException(
s"The SQL config '$configName' was removed in the version $version. $comment")
}
}
}
/**
* Logs a warning message if the given config key is deprecated.
*/
private def logDeprecationWarning(key: String): Unit = {
SQLConf.deprecatedSQLConfigs.get(key).foreach {
case DeprecatedConfig(configName, version, comment) =>
logWarning(
s"The SQL config '$configName' has been deprecated in Spark v$version " +
s"and may be removed in the future. $comment")
}
}
}