forked from apache/spark
-
Notifications
You must be signed in to change notification settings - Fork 1
/
DataSourceV2SQLSessionCatalogSuite.scala
82 lines (68 loc) · 3.26 KB
/
DataSourceV2SQLSessionCatalogSuite.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
/*
* 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.connector
import org.apache.spark.sql.{DataFrame, Row, SaveMode}
import org.apache.spark.sql.connector.catalog.{Identifier, Table, TableCatalog}
class DataSourceV2SQLSessionCatalogSuite
extends InsertIntoTests(supportsDynamicOverwrite = true, includeSQLOnlyTests = true)
with AlterTableTests
with SessionCatalogTest[InMemoryTable, InMemoryTableSessionCatalog] {
override protected val catalogAndNamespace = ""
override protected def doInsert(tableName: String, insert: DataFrame, mode: SaveMode): Unit = {
val tmpView = "tmp_view"
withTempView(tmpView) {
insert.createOrReplaceTempView(tmpView)
val overwrite = if (mode == SaveMode.Overwrite) "OVERWRITE" else "INTO"
sql(s"INSERT $overwrite TABLE $tableName SELECT * FROM $tmpView")
}
}
override protected def verifyTable(tableName: String, expected: DataFrame): Unit = {
checkAnswer(spark.table(tableName), expected)
checkAnswer(sql(s"SELECT * FROM $tableName"), expected)
checkAnswer(sql(s"SELECT * FROM default.$tableName"), expected)
checkAnswer(sql(s"TABLE $tableName"), expected)
}
override def getTableMetadata(tableName: String): Table = {
val v2Catalog = spark.sessionState.catalogManager.currentCatalog
val nameParts = spark.sessionState.sqlParser.parseMultipartIdentifier(tableName)
v2Catalog.asInstanceOf[TableCatalog]
.loadTable(Identifier.of(nameParts.init.toArray, nameParts.last))
}
test("SPARK-30697: catalog.isView doesn't throw an error for specialized identifiers") {
val t1 = "tbl"
withTable(t1) {
sql(s"CREATE TABLE $t1 (id bigint, data string) USING $v2Format")
def idResolver(id: Identifier): Identifier = Identifier.of(Array("default"), id.name())
InMemoryTableSessionCatalog.withCustomIdentifierResolver(idResolver) {
// The following should not throw AnalysisException.
sql(s"DESCRIBE TABLE ignored.$t1")
}
}
}
test("SPARK-31624: SHOW TBLPROPERTIES working with V2 tables and the session catalog") {
val t1 = "tbl"
withTable(t1) {
sql(s"CREATE TABLE $t1 (id bigint, data string) USING $v2Format TBLPROPERTIES " +
"(key='v', key2='v2')")
checkAnswer(sql(s"SHOW TBLPROPERTIES $t1"), Seq(Row("key", "v"), Row("key2", "v2")))
checkAnswer(sql(s"SHOW TBLPROPERTIES $t1('key')"), Row("key", "v"))
checkAnswer(
sql(s"SHOW TBLPROPERTIES $t1('keyX')"),
Row("keyX", s"Table default.$t1 does not have property: keyX"))
}
}
}