forked from apache/spark
-
Notifications
You must be signed in to change notification settings - Fork 4
/
CatalogSuite.scala
296 lines (274 loc) · 11.5 KB
/
CatalogSuite.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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
/*
* 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 java.io.{File, FilenameFilter}
import io.grpc.StatusRuntimeException
import org.apache.commons.io.FileUtils
import org.apache.spark.sql.connect.client.util.RemoteSparkSession
import org.apache.spark.sql.types.{DoubleType, LongType, StructType}
import org.apache.spark.storage.StorageLevel
class CatalogSuite extends RemoteSparkSession with SQLHelper {
test("Database APIs") {
val currentDb = spark.catalog.currentDatabase
assert(currentDb == "default")
withTempDatabase { db =>
try {
spark.catalog.setCurrentDatabase(db)
assert(spark.catalog.currentDatabase == db)
val dbs = spark.catalog.listDatabases().collect().sortBy(_.name)
assert(dbs.length == 2)
assert(dbs.map(_.name) sameElements Array(db, currentDb))
assert(dbs.map(_.catalog).distinct sameElements Array("spark_catalog"))
var databasesWithPattern = spark.catalog.listDatabases("def*").collect().sortBy(_.name)
assert(databasesWithPattern.length == 1)
assert(databasesWithPattern.map(_.name) sameElements Array(currentDb))
databasesWithPattern = spark.catalog.listDatabases("def2*").collect().sortBy(_.name)
assert(databasesWithPattern.length == 0)
val database = spark.catalog.getDatabase(db)
assert(database.name == db)
val message = intercept[StatusRuntimeException] {
spark.catalog.getDatabase("notExists")
}.getMessage
assert(message.contains("SCHEMA_NOT_FOUND"))
assert(spark.catalog.databaseExists(db))
assert(!spark.catalog.databaseExists("notExists"))
} finally {
spark.catalog.setCurrentDatabase(currentDb)
assert(spark.catalog.currentDatabase == currentDb)
}
}
}
test("CatalogMetadata APIs") {
val currentCatalog = spark.catalog.currentCatalog()
assert(currentCatalog == "spark_catalog")
try {
val catalogs = spark.catalog.listCatalogs().collect()
assert(catalogs.length == 1)
assert(catalogs.map(_.name) sameElements Array("spark_catalog"))
val message = intercept[StatusRuntimeException] {
spark.catalog.setCurrentCatalog("notExists")
}.getMessage
assert(message.contains("plugin class not found"))
spark.catalog.setCurrentCatalog("testcat")
assert(spark.catalog.currentCatalog().equals("testcat"))
val catalogsAfterChange = spark.catalog.listCatalogs().collect()
assert(catalogsAfterChange.length == 2)
assert(catalogsAfterChange.map(_.name).toSet == Set("testcat", "spark_catalog"))
var catalogsWithPattern = spark.catalog.listCatalogs("spark*").collect()
assert(catalogsWithPattern.length == 1)
assert(catalogsWithPattern.map(_.name) sameElements Array("spark_catalog"))
catalogsWithPattern = spark.catalog.listCatalogs("hive*").collect()
assert(catalogsWithPattern.length == 0)
} finally {
spark.catalog.setCurrentCatalog(currentCatalog)
assert(spark.catalog.currentCatalog() == "spark_catalog")
}
}
test("Table APIs") {
assert(spark.catalog.listTables().collect().isEmpty)
val parquetTableName = "parquet_table"
val orcTableName = "orc_table"
val jsonTableName = "json_table"
withTable(parquetTableName) {
withTempPath { table1Dir =>
val session = spark
import session.implicits._
val df1 = Seq("Bob", "Alice", "Nico", "Bob", "Alice").toDF("name")
df1.write.parquet(table1Dir.getPath)
spark.catalog.createTable(parquetTableName, table1Dir.getPath).collect()
withTable(orcTableName, jsonTableName) {
withTempPath { table2Dir =>
val df2 = Seq("Bob", "Alice", "Nico", "Bob", "Alice").zipWithIndex.toDF("name", "id")
df2.write.orc(table2Dir.getPath)
spark.catalog.createTable(orcTableName, table2Dir.getPath, "orc").collect()
val orcTable = spark.catalog.getTable(orcTableName)
assert(!orcTable.isTemporary)
assert(orcTable.name == orcTableName)
assert(orcTable.tableType == "EXTERNAL")
assert(
spark.catalog.listColumns(orcTableName).collect().map(_.name).toSet == Set(
"name",
"id"))
}
val schema = new StructType().add("id", LongType).add("a", DoubleType)
spark.catalog
.createTable(jsonTableName, "json", schema, Map.empty[String, String])
.collect()
val jsonTable = spark.catalog.getTable("default", jsonTableName)
assert(!jsonTable.isTemporary)
assert(jsonTable.name == jsonTableName)
assert(jsonTable.tableType == "MANAGED")
assert(spark.catalog.tableExists(jsonTableName))
assert(
spark.catalog.listTables().collect().map(_.name).toSet == Set(
parquetTableName,
orcTableName,
jsonTableName))
assert(
spark.catalog
.listTables(spark.catalog.currentDatabase, "par*")
.collect()
.map(_.name)
.toSet == Set(parquetTableName))
assert(
spark.catalog.listTables(spark.catalog.currentDatabase, "txt*").collect().isEmpty)
}
assert(spark.catalog.tableExists(parquetTableName))
assert(!spark.catalog.tableExists(orcTableName))
assert(!spark.catalog.tableExists(jsonTableName))
assert(spark.catalog.listTables().collect().map(_.name).toSet == Set(parquetTableName))
}
}
val message = intercept[StatusRuntimeException] {
spark.catalog.getTable(parquetTableName)
}.getMessage
assert(message.contains("TABLE_OR_VIEW_NOT_FOUND"))
assert(spark.catalog.listTables().collect().isEmpty)
}
test("Cache Table APIs") {
val parquetTableName = "parquet_table"
withTable(parquetTableName) {
withTempPath { table1Dir =>
val session = spark
import session.implicits._
val df1 = Seq("Bob", "Alice", "Nico", "Bob", "Alice").toDF("name")
df1.write.parquet(table1Dir.getPath)
spark.catalog.createTable(parquetTableName, table1Dir.getPath).collect()
// Test cache and uncacheTable
spark.catalog.cacheTable(parquetTableName)
assert(spark.catalog.isCached(parquetTableName))
spark.catalog.uncacheTable(parquetTableName)
assert(!spark.catalog.isCached(parquetTableName))
// Test cache with `StorageLevel` and clearCache
spark.catalog.cacheTable(parquetTableName, StorageLevel.MEMORY_ONLY)
assert(spark.catalog.isCached(parquetTableName))
spark.catalog.clearCache()
assert(!spark.catalog.isCached(parquetTableName))
}
}
}
test("TempView APIs") {
val viewName = "view1"
val globalViewName = "g_view1"
try {
spark.range(100).createTempView(viewName)
val view = spark.catalog.getTable(viewName)
assert(view.name == viewName)
assert(view.isTemporary)
assert(view.tableType == "TEMPORARY")
spark.range(100).createGlobalTempView(globalViewName)
val globalView = spark.catalog.getTable(s"global_temp.$globalViewName")
assert(globalView.name == globalViewName)
assert(globalView.isTemporary)
assert(globalView.tableType == "TEMPORARY")
} finally {
spark.catalog.dropTempView(viewName)
spark.catalog.dropGlobalTempView(globalViewName)
assert(!spark.catalog.tableExists(viewName))
assert(!spark.catalog.tableExists(globalViewName))
}
}
test("Function API") {
val dbName = spark.catalog.currentDatabase
val functions1 = spark.catalog.listFunctions().collect()
assert(functions1.nonEmpty)
val functions2 = spark.catalog.listFunctions(dbName).collect()
assert(functions1.map(_.name) sameElements functions2.map(_.name))
val absFunctionName = "abs"
assert(spark.catalog.functionExists(absFunctionName))
assert(spark.catalog.getFunction(absFunctionName).name == absFunctionName)
val notExistsFunction = "notExists"
assert(!spark.catalog.functionExists(notExistsFunction))
val message = intercept[StatusRuntimeException] {
spark.catalog.getFunction(notExistsFunction)
}.getMessage
assert(message.contains("UNRESOLVED_ROUTINE"))
}
test("recoverPartitions") {
val tableName = "test"
withTable(tableName) {
withTempPath { dir =>
spark
.range(5)
.selectExpr("id as fieldOne", "id as partCol")
.write
.partitionBy("partCol")
.mode("overwrite")
.save(dir.getAbsolutePath)
spark.sql(s"""
|create table $tableName (fieldOne long, partCol int)
|using parquet
|options (path "${dir.toURI}")
|partitioned by (partCol)""".stripMargin)
spark.sql(s"show partitions $tableName").count()
assert(spark.sql(s"select * from $tableName").count() == 0)
spark.catalog.recoverPartitions(tableName)
assert(spark.sql(s"select * from $tableName").count() == 5)
}
}
}
test("refreshTable") {
withTempPath { dir =>
val tableName = "spark_catalog.default.my_table"
withTable(tableName) {
try {
spark.sql(s"""
| CREATE TABLE $tableName(col STRING) USING TEXT
| LOCATION '${dir.getAbsolutePath}'
|""".stripMargin)
spark.sql(s"""INSERT INTO $tableName SELECT 'abc'""".stripMargin)
spark.catalog.cacheTable(tableName)
assert(spark.table(tableName).collect().length == 1)
FileUtils.deleteDirectory(dir)
assert(spark.table(tableName).collect().length == 1)
spark.catalog.refreshTable(tableName)
assert(spark.table(tableName).collect().length == 0)
} finally {
spark.catalog.clearCache()
}
}
}
}
test("refreshByPath") {
withTempPath { dir =>
val tableName = "spark_catalog.default.my_table"
withTable(tableName) {
try {
spark.sql(s"""
| CREATE TABLE $tableName(col STRING) USING TEXT
| LOCATION '${dir.getAbsolutePath}'
|""".stripMargin)
spark.sql(s"""INSERT INTO $tableName SELECT 'abc'""".stripMargin)
spark.catalog.cacheTable(tableName)
assert(spark.table(tableName).collect().length == 1)
// delete one
new File(dir.getAbsolutePath)
.listFiles(new FilenameFilter() {
override def accept(dir: File, name: String): Boolean = name.endsWith(".txt")
})
.head
.delete()
assert(spark.table(tableName).collect().length == 1)
spark.catalog.refreshByPath(dir.getAbsolutePath)
assert(spark.table(tableName).collect().length == 0)
} finally {
spark.catalog.clearCache()
}
}
}
}
}