-
Notifications
You must be signed in to change notification settings - Fork 28k
/
LookupCatalogSuite.scala
210 lines (192 loc) · 8.43 KB
/
LookupCatalogSuite.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
/*
* 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.catalyst.catalog.v2
import org.mockito.ArgumentMatchers.any
import org.mockito.Mockito.{mock, when}
import org.mockito.invocation.InvocationOnMock
import org.scalatest.Inside
import org.scalatest.Matchers._
import org.apache.spark.SparkFunSuite
import org.apache.spark.sql.catalog.v2.{CatalogManager, CatalogNotFoundException, CatalogPlugin, Identifier, LookupCatalog}
import org.apache.spark.sql.catalyst.TableIdentifier
import org.apache.spark.sql.catalyst.parser.CatalystSqlParser
import org.apache.spark.sql.util.CaseInsensitiveStringMap
private case class TestCatalogPlugin(override val name: String) extends CatalogPlugin {
override def initialize(name: String, options: CaseInsensitiveStringMap): Unit = Unit
}
class LookupCatalogSuite extends SparkFunSuite with LookupCatalog with Inside {
import CatalystSqlParser._
private val catalogs = Seq("prod", "test").map(x => x -> TestCatalogPlugin(x)).toMap
override val catalogManager: CatalogManager = {
val manager = mock(classOf[CatalogManager])
when(manager.catalog(any())).thenAnswer((invocation: InvocationOnMock) => {
val name = invocation.getArgument[String](0)
catalogs.getOrElse(name, throw new CatalogNotFoundException(s"$name not found"))
})
when(manager.defaultCatalog).thenReturn(None)
manager
}
test("catalog object identifier") {
Seq(
("tbl", None, Seq.empty, "tbl"),
("db.tbl", None, Seq("db"), "tbl"),
("prod.func", catalogs.get("prod"), Seq.empty, "func"),
("ns1.ns2.tbl", None, Seq("ns1", "ns2"), "tbl"),
("prod.db.tbl", catalogs.get("prod"), Seq("db"), "tbl"),
("test.db.tbl", catalogs.get("test"), Seq("db"), "tbl"),
("test.ns1.ns2.ns3.tbl", catalogs.get("test"), Seq("ns1", "ns2", "ns3"), "tbl"),
("`db.tbl`", None, Seq.empty, "db.tbl"),
("parquet.`file:/tmp/db.tbl`", None, Seq("parquet"), "file:/tmp/db.tbl"),
("`org.apache.spark.sql.json`.`s3://buck/tmp/abc.json`", None,
Seq("org.apache.spark.sql.json"), "s3://buck/tmp/abc.json")).foreach {
case (sql, expectedCatalog, namespace, name) =>
inside(parseMultipartIdentifier(sql)) {
case CatalogObjectIdentifier(catalog, ident) =>
catalog shouldEqual expectedCatalog
ident shouldEqual Identifier.of(namespace.toArray, name)
}
}
}
test("table identifier") {
Seq(
("tbl", "tbl", None),
("db.tbl", "tbl", Some("db")),
("`db.tbl`", "db.tbl", None),
("parquet.`file:/tmp/db.tbl`", "file:/tmp/db.tbl", Some("parquet")),
("`org.apache.spark.sql.json`.`s3://buck/tmp/abc.json`", "s3://buck/tmp/abc.json",
Some("org.apache.spark.sql.json"))).foreach {
case (sql, table, db) =>
inside (parseMultipartIdentifier(sql)) {
case AsTableIdentifier(ident) =>
ident shouldEqual TableIdentifier(table, db)
}
}
Seq(
"prod.func",
"prod.db.tbl",
"ns1.ns2.tbl").foreach { sql =>
parseMultipartIdentifier(sql) match {
case AsTableIdentifier(_) =>
fail(s"$sql should not be resolved as TableIdentifier")
case _ =>
}
}
}
test("temporary table identifier") {
Seq(
("tbl", TableIdentifier("tbl")),
("db.tbl", TableIdentifier("tbl", Some("db"))),
("`db.tbl`", TableIdentifier("db.tbl")),
("parquet.`file:/tmp/db.tbl`", TableIdentifier("file:/tmp/db.tbl", Some("parquet"))),
("`org.apache.spark.sql.json`.`s3://buck/tmp/abc.json`",
TableIdentifier("s3://buck/tmp/abc.json", Some("org.apache.spark.sql.json")))).foreach {
case (sqlIdent: String, expectedTableIdent: TableIdentifier) =>
// when there is no catalog and the namespace has one part, the rule should match
inside(parseMultipartIdentifier(sqlIdent)) {
case AsTemporaryViewIdentifier(ident) =>
ident shouldEqual expectedTableIdent
}
}
Seq("prod.func", "prod.db.tbl", "test.db.tbl", "ns1.ns2.tbl", "test.ns1.ns2.ns3.tbl")
.foreach { sqlIdent =>
inside(parseMultipartIdentifier(sqlIdent)) {
case AsTemporaryViewIdentifier(_) =>
fail("AsTemporaryViewIdentifier should not match when " +
"the catalog is set or the namespace has multiple parts")
case _ =>
// expected
}
}
}
}
class LookupCatalogWithDefaultSuite extends SparkFunSuite with LookupCatalog with Inside {
import CatalystSqlParser._
private val catalogs = Seq("prod", "test").map(x => x -> TestCatalogPlugin(x)).toMap
override val catalogManager: CatalogManager = {
val manager = mock(classOf[CatalogManager])
when(manager.catalog(any())).thenAnswer((invocation: InvocationOnMock) => {
val name = invocation.getArgument[String](0)
catalogs.getOrElse(name, throw new CatalogNotFoundException(s"$name not found"))
})
when(manager.defaultCatalog).thenReturn(catalogs.get("prod"))
manager
}
test("catalog object identifier") {
Seq(
("tbl", catalogs.get("prod"), Seq.empty, "tbl"),
("db.tbl", catalogs.get("prod"), Seq("db"), "tbl"),
("prod.func", catalogs.get("prod"), Seq.empty, "func"),
("ns1.ns2.tbl", catalogs.get("prod"), Seq("ns1", "ns2"), "tbl"),
("prod.db.tbl", catalogs.get("prod"), Seq("db"), "tbl"),
("test.db.tbl", catalogs.get("test"), Seq("db"), "tbl"),
("test.ns1.ns2.ns3.tbl", catalogs.get("test"), Seq("ns1", "ns2", "ns3"), "tbl"),
("`db.tbl`", catalogs.get("prod"), Seq.empty, "db.tbl"),
("parquet.`file:/tmp/db.tbl`", catalogs.get("prod"), Seq("parquet"), "file:/tmp/db.tbl"),
("`org.apache.spark.sql.json`.`s3://buck/tmp/abc.json`", catalogs.get("prod"),
Seq("org.apache.spark.sql.json"), "s3://buck/tmp/abc.json")).foreach {
case (sql, expectedCatalog, namespace, name) =>
inside(parseMultipartIdentifier(sql)) {
case CatalogObjectIdentifier(catalog, ident) =>
catalog shouldEqual expectedCatalog
ident shouldEqual Identifier.of(namespace.toArray, name)
}
}
}
test("table identifier") {
Seq(
"tbl",
"db.tbl",
"`db.tbl`",
"parquet.`file:/tmp/db.tbl`",
"`org.apache.spark.sql.json`.`s3://buck/tmp/abc.json`",
"prod.func",
"prod.db.tbl",
"ns1.ns2.tbl").foreach { sql =>
parseMultipartIdentifier(sql) match {
case AsTableIdentifier(_) =>
fail(s"$sql should not be resolved as TableIdentifier")
case _ =>
}
}
}
test("temporary table identifier") {
Seq(
("tbl", TableIdentifier("tbl")),
("db.tbl", TableIdentifier("tbl", Some("db"))),
("`db.tbl`", TableIdentifier("db.tbl")),
("parquet.`file:/tmp/db.tbl`", TableIdentifier("file:/tmp/db.tbl", Some("parquet"))),
("`org.apache.spark.sql.json`.`s3://buck/tmp/abc.json`",
TableIdentifier("s3://buck/tmp/abc.json", Some("org.apache.spark.sql.json")))).foreach {
case (sqlIdent: String, expectedTableIdent: TableIdentifier) =>
// when there is no catalog and the namespace has one part, the rule should match
inside(parseMultipartIdentifier(sqlIdent)) {
case AsTemporaryViewIdentifier(ident) =>
ident shouldEqual expectedTableIdent
}
}
Seq("prod.func", "prod.db.tbl", "test.db.tbl", "ns1.ns2.tbl", "test.ns1.ns2.ns3.tbl")
.foreach { sqlIdent =>
inside(parseMultipartIdentifier(sqlIdent)) {
case AsTemporaryViewIdentifier(_) =>
fail("AsTemporaryViewIdentifier should not match when " +
"the catalog is set or the namespace has multiple parts")
case _ =>
// expected
}
}
}
}