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
18package org.apache.spark.sql.internal
19
20import org.apache.spark.SparkFunSuite
21import org.apache.spark.sql.internal.SQLConf._
22
23class SQLConfEntrySuite extends SparkFunSuite {
24
25  val conf = new SQLConf
26
27  test("intConf") {
28    val key = "spark.sql.SQLConfEntrySuite.int"
29    val confEntry = SQLConfigBuilder(key).intConf.createWithDefault(1)
30    assert(conf.getConf(confEntry, 5) === 5)
31
32    conf.setConf(confEntry, 10)
33    assert(conf.getConf(confEntry, 5) === 10)
34
35    conf.setConfString(key, "20")
36    assert(conf.getConfString(key, "5") === "20")
37    assert(conf.getConfString(key) === "20")
38    assert(conf.getConf(confEntry, 5) === 20)
39
40    val e = intercept[IllegalArgumentException] {
41      conf.setConfString(key, "abc")
42    }
43    assert(e.getMessage === s"$key should be int, but was abc")
44  }
45
46  test("longConf") {
47    val key = "spark.sql.SQLConfEntrySuite.long"
48    val confEntry = SQLConfigBuilder(key).longConf.createWithDefault(1L)
49    assert(conf.getConf(confEntry, 5L) === 5L)
50
51    conf.setConf(confEntry, 10L)
52    assert(conf.getConf(confEntry, 5L) === 10L)
53
54    conf.setConfString(key, "20")
55    assert(conf.getConfString(key, "5") === "20")
56    assert(conf.getConfString(key) === "20")
57    assert(conf.getConf(confEntry, 5L) === 20L)
58
59    val e = intercept[IllegalArgumentException] {
60      conf.setConfString(key, "abc")
61    }
62    assert(e.getMessage === s"$key should be long, but was abc")
63  }
64
65  test("booleanConf") {
66    val key = "spark.sql.SQLConfEntrySuite.boolean"
67    val confEntry = SQLConfigBuilder(key).booleanConf.createWithDefault(true)
68    assert(conf.getConf(confEntry, false) === false)
69
70    conf.setConf(confEntry, true)
71    assert(conf.getConf(confEntry, false) === true)
72
73    conf.setConfString(key, "true")
74    assert(conf.getConfString(key, "false") === "true")
75    assert(conf.getConfString(key) === "true")
76    assert(conf.getConf(confEntry, false) === true)
77
78    val e = intercept[IllegalArgumentException] {
79      conf.setConfString(key, "abc")
80    }
81    assert(e.getMessage === s"$key should be boolean, but was abc")
82  }
83
84  test("doubleConf") {
85    val key = "spark.sql.SQLConfEntrySuite.double"
86    val confEntry = SQLConfigBuilder(key).doubleConf.createWithDefault(1d)
87    assert(conf.getConf(confEntry, 5.0) === 5.0)
88
89    conf.setConf(confEntry, 10.0)
90    assert(conf.getConf(confEntry, 5.0) === 10.0)
91
92    conf.setConfString(key, "20.0")
93    assert(conf.getConfString(key, "5.0") === "20.0")
94    assert(conf.getConfString(key) === "20.0")
95    assert(conf.getConf(confEntry, 5.0) === 20.0)
96
97    val e = intercept[IllegalArgumentException] {
98      conf.setConfString(key, "abc")
99    }
100    assert(e.getMessage === s"$key should be double, but was abc")
101  }
102
103  test("stringConf") {
104    val key = "spark.sql.SQLConfEntrySuite.string"
105    val confEntry = SQLConfigBuilder(key).stringConf.createWithDefault(null)
106    assert(conf.getConf(confEntry, "abc") === "abc")
107
108    conf.setConf(confEntry, "abcd")
109    assert(conf.getConf(confEntry, "abc") === "abcd")
110
111    conf.setConfString(key, "abcde")
112    assert(conf.getConfString(key, "abc") === "abcde")
113    assert(conf.getConfString(key) === "abcde")
114    assert(conf.getConf(confEntry, "abc") === "abcde")
115  }
116
117  test("enumConf") {
118    val key = "spark.sql.SQLConfEntrySuite.enum"
119    val confEntry = SQLConfigBuilder(key)
120      .stringConf
121      .checkValues(Set("a", "b", "c"))
122      .createWithDefault("a")
123    assert(conf.getConf(confEntry) === "a")
124
125    conf.setConf(confEntry, "b")
126    assert(conf.getConf(confEntry) === "b")
127
128    conf.setConfString(key, "c")
129    assert(conf.getConfString(key, "a") === "c")
130    assert(conf.getConfString(key) === "c")
131    assert(conf.getConf(confEntry) === "c")
132
133    val e = intercept[IllegalArgumentException] {
134      conf.setConfString(key, "d")
135    }
136    assert(e.getMessage === s"The value of $key should be one of a, b, c, but was d")
137  }
138
139  test("stringSeqConf") {
140    val key = "spark.sql.SQLConfEntrySuite.stringSeq"
141    val confEntry = SQLConfigBuilder(key)
142      .stringConf
143      .toSequence
144      .createWithDefault(Nil)
145    assert(conf.getConf(confEntry, Seq("a", "b", "c")) === Seq("a", "b", "c"))
146
147    conf.setConf(confEntry, Seq("a", "b", "c", "d"))
148    assert(conf.getConf(confEntry, Seq("a", "b", "c")) === Seq("a", "b", "c", "d"))
149
150    conf.setConfString(key, "a,b,c,d,e")
151    assert(conf.getConfString(key, "a,b,c") === "a,b,c,d,e")
152    assert(conf.getConfString(key) === "a,b,c,d,e")
153    assert(conf.getConf(confEntry, Seq("a", "b", "c")) === Seq("a", "b", "c", "d", "e"))
154  }
155
156  test("optionalConf") {
157    val key = "spark.sql.SQLConfEntrySuite.optional"
158    val confEntry = SQLConfigBuilder(key)
159      .stringConf
160      .createOptional
161
162    assert(conf.getConf(confEntry) === None)
163    conf.setConfString(key, "a")
164    assert(conf.getConf(confEntry) === Some("a"))
165  }
166
167  test("duplicate entry") {
168    val key = "spark.sql.SQLConfEntrySuite.duplicate"
169    SQLConfigBuilder(key).stringConf.createOptional
170    intercept[IllegalArgumentException] {
171      SQLConfigBuilder(key).stringConf.createOptional
172    }
173  }
174}
175