Skip to content

Commit 2da406c

Browse files
gatorsmiledongjoon-hyun
authored andcommitted
[SPARK-27618][SQL][FOLLOW-UP] Unnecessary access to externalCatalog
## What changes were proposed in this pull request? This PR is to add test cases for ensuring that we do not have unnecessary access to externalCatalog. In the future, we can follow these examples to improve our test coverage in this area. ## How was this patch tested? N/A Closes apache#24511 from gatorsmile/addTestcaseSpark-27618. Authored-by: gatorsmile <[email protected]> Signed-off-by: Dongjoon Hyun <[email protected]>
1 parent 3670826 commit 2da406c

File tree

2 files changed

+79
-0
lines changed

2 files changed

+79
-0
lines changed

sql/catalyst/pom.xml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,11 @@
7171
<type>test-jar</type>
7272
<scope>test</scope>
7373
</dependency>
74+
<dependency>
75+
<groupId>org.mockito</groupId>
76+
<artifactId>mockito-core</artifactId>
77+
<scope>test</scope>
78+
</dependency>
7479

7580
<dependency>
7681
<groupId>org.apache.spark</groupId>
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
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+
18+
package org.apache.spark.sql.catalyst.analysis
19+
20+
import java.net.URI
21+
22+
import org.mockito.Mockito._
23+
import org.scalatest.Matchers
24+
25+
import org.apache.spark.sql.catalyst.{FunctionIdentifier, TableIdentifier}
26+
import org.apache.spark.sql.catalyst.catalog.{CatalogDatabase, CatalogStorageFormat, CatalogTable, CatalogTableType, ExternalCatalog, InMemoryCatalog, SessionCatalog}
27+
import org.apache.spark.sql.catalyst.expressions.{Alias, AttributeReference}
28+
import org.apache.spark.sql.catalyst.plans.logical.{LocalRelation, Project}
29+
import org.apache.spark.sql.internal.SQLConf
30+
import org.apache.spark.sql.types._
31+
32+
class AnalysisExternalCatalogSuite extends AnalysisTest with Matchers {
33+
private def getAnalyzer(externCatalog: ExternalCatalog): Analyzer = {
34+
val conf = new SQLConf()
35+
val catalog = new SessionCatalog(externCatalog, FunctionRegistry.builtin, conf)
36+
catalog.createDatabase(
37+
CatalogDatabase("default", "", new URI("loc"), Map.empty),
38+
ignoreIfExists = false)
39+
catalog.createTable(
40+
CatalogTable(
41+
TableIdentifier("t1", Some("default")),
42+
CatalogTableType.MANAGED,
43+
CatalogStorageFormat.empty,
44+
StructType(Seq(StructField("a", IntegerType, nullable = true)))),
45+
ignoreIfExists = false)
46+
new Analyzer(catalog, conf)
47+
}
48+
49+
test("query builtin functions don't call the external catalog") {
50+
val inMemoryCatalog = new InMemoryCatalog
51+
val catalog = spy(inMemoryCatalog)
52+
val analyzer = getAnalyzer(catalog)
53+
reset(catalog)
54+
val testRelation = LocalRelation(AttributeReference("a", IntegerType, nullable = true)())
55+
val func =
56+
Alias(UnresolvedFunction("sum", Seq(UnresolvedAttribute("a")), isDistinct = false), "s")()
57+
val plan = Project(Seq(func), testRelation)
58+
analyzer.execute(plan)
59+
verifyZeroInteractions(catalog)
60+
}
61+
62+
test("check the existence of builtin functions don't call the external catalog") {
63+
val inMemoryCatalog = new InMemoryCatalog
64+
val externCatalog = spy(inMemoryCatalog)
65+
val catalog = new SessionCatalog(externCatalog, FunctionRegistry.builtin, conf)
66+
catalog.createDatabase(
67+
CatalogDatabase("default", "", new URI("loc"), Map.empty),
68+
ignoreIfExists = false)
69+
reset(externCatalog)
70+
catalog.functionExists(FunctionIdentifier("sum"))
71+
verifyZeroInteractions(externCatalog)
72+
}
73+
74+
}

0 commit comments

Comments
 (0)