-
Notifications
You must be signed in to change notification settings - Fork 28k
/
LookupCatalog.scala
118 lines (107 loc) · 4.15 KB
/
LookupCatalog.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
/*
* 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.catalog.v2
import org.apache.spark.annotation.Experimental
import org.apache.spark.internal.Logging
import org.apache.spark.sql.catalyst.TableIdentifier
/**
* A trait to encapsulate catalog lookup function and helpful extractors.
*/
@Experimental
trait LookupCatalog extends Logging {
protected val catalogManager: CatalogManager
/**
* Returns the default catalog. When set, this catalog is used for all identifiers that do not
* set a specific catalog. When this is None, the session catalog is responsible for the
* identifier.
*
* If this is None and a table's provider (source) is a v2 provider, the v2 session catalog will
* be used.
*/
def defaultCatalog: Option[CatalogPlugin] = catalogManager.defaultCatalog
/**
* This catalog is a v2 catalog that delegates to the v1 session catalog. it is used when the
* session catalog is responsible for an identifier, but the source requires the v2 catalog API.
* This happens when the source implementation extends the v2 TableProvider API and is not listed
* in the fallback configuration, spark.sql.sources.write.useV1SourceList
*/
def sessionCatalog: Option[CatalogPlugin] = catalogManager.v2SessionCatalog
/**
* Extract catalog plugin and remaining identifier names.
*
* This does not substitute the default catalog if no catalog is set in the identifier.
*/
private object CatalogAndIdentifier {
def unapply(parts: Seq[String]): Some[(Option[CatalogPlugin], Seq[String])] = parts match {
case Seq(_) =>
Some((None, parts))
case Seq(catalogName, tail @ _*) =>
try {
Some((Some(catalogManager.catalog(catalogName)), tail))
} catch {
case _: CatalogNotFoundException =>
Some((None, parts))
}
}
}
type CatalogObjectIdentifier = (Option[CatalogPlugin], Identifier)
/**
* Extract catalog and identifier from a multi-part identifier with the default catalog if needed.
*/
object CatalogObjectIdentifier {
def unapply(parts: Seq[String]): Some[CatalogObjectIdentifier] = parts match {
case CatalogAndIdentifier(maybeCatalog, nameParts) =>
Some((
maybeCatalog.orElse(defaultCatalog),
Identifier.of(nameParts.init.toArray, nameParts.last)
))
}
}
/**
* Extract legacy table identifier from a multi-part identifier.
*
* For legacy support only. Please use [[CatalogObjectIdentifier]] instead on DSv2 code paths.
*/
object AsTableIdentifier {
def unapply(parts: Seq[String]): Option[TableIdentifier] = parts match {
case CatalogAndIdentifier(None, names) if defaultCatalog.isEmpty =>
names match {
case Seq(name) =>
Some(TableIdentifier(name))
case Seq(database, name) =>
Some(TableIdentifier(name, Some(database)))
case _ =>
None
}
case _ =>
None
}
}
/**
* For temp views, extract a table identifier from a multi-part identifier if it has no catalog.
*/
object AsTemporaryViewIdentifier {
def unapply(parts: Seq[String]): Option[TableIdentifier] = parts match {
case CatalogAndIdentifier(None, Seq(table)) =>
Some(TableIdentifier(table))
case CatalogAndIdentifier(None, Seq(database, table)) =>
Some(TableIdentifier(table, Some(database)))
case _ =>
None
}
}
}