Skip to content

Commit 303ee3f

Browse files
rdbluecloud-fan
authored andcommitted
[SPARK-24252][SQL] Add TableCatalog API
## What changes were proposed in this pull request? This adds the TableCatalog API proposed in the [Table Metadata API SPIP](https://docs.google.com/document/d/1zLFiA1VuaWeVxeTDXNg8bL6GP3BVoOZBkewFtEnjEoo/edit#heading=h.m45webtwxf2d). For `TableCatalog` to use `Table`, it needed to be moved into the catalyst module where the v2 catalog API is located. This also required moving `TableCapability`. Most of the files touched by this PR are import changes needed by this move. ## How was this patch tested? This adds a test implementation and contract tests. Closes #24246 from rdblue/SPARK-24252-add-table-catalog-api. Authored-by: Ryan Blue <blue@apache.org> Signed-off-by: Wenchen Fan <wenchen@databricks.com>
1 parent 5e79ae3 commit 303ee3f

File tree

15 files changed

+1594
-64
lines changed

15 files changed

+1594
-64
lines changed

sql/catalyst/src/main/java/org/apache/spark/sql/catalog/v2/IdentifierImpl.java

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,12 @@
1717

1818
package org.apache.spark.sql.catalog.v2;
1919

20+
import com.google.common.base.Preconditions;
2021
import org.apache.spark.annotation.Experimental;
2122

23+
import java.util.Arrays;
24+
import java.util.Objects;
25+
2226
/**
2327
* An {@link Identifier} implementation.
2428
*/
@@ -29,6 +33,8 @@ class IdentifierImpl implements Identifier {
2933
private String name;
3034

3135
IdentifierImpl(String[] namespace, String name) {
36+
Preconditions.checkNotNull(namespace, "Identifier namespace cannot be null");
37+
Preconditions.checkNotNull(name, "Identifier name cannot be null");
3238
this.namespace = namespace;
3339
this.name = name;
3440
}
@@ -42,4 +48,23 @@ public String[] namespace() {
4248
public String name() {
4349
return name;
4450
}
51+
52+
@Override
53+
public boolean equals(Object o) {
54+
if (this == o) {
55+
return true;
56+
}
57+
58+
if (o == null || getClass() != o.getClass()) {
59+
return false;
60+
}
61+
62+
IdentifierImpl that = (IdentifierImpl) o;
63+
return Arrays.equals(namespace, that.namespace) && name.equals(that.name);
64+
}
65+
66+
@Override
67+
public int hashCode() {
68+
return Objects.hash(Arrays.hashCode(namespace), name);
69+
}
4570
}
Lines changed: 137 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,137 @@
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.catalog.v2;
19+
20+
import org.apache.spark.sql.catalog.v2.expressions.Transform;
21+
import org.apache.spark.sql.catalyst.analysis.NoSuchNamespaceException;
22+
import org.apache.spark.sql.catalyst.analysis.NoSuchTableException;
23+
import org.apache.spark.sql.catalyst.analysis.TableAlreadyExistsException;
24+
import org.apache.spark.sql.sources.v2.Table;
25+
import org.apache.spark.sql.types.StructType;
26+
27+
import java.util.Map;
28+
29+
/**
30+
* Catalog methods for working with Tables.
31+
* <p>
32+
* TableCatalog implementations may be case sensitive or case insensitive. Spark will pass
33+
* {@link Identifier table identifiers} without modification. Field names passed to
34+
* {@link #alterTable(Identifier, TableChange...)} will be normalized to match the case used in the
35+
* table schema when updating, renaming, or dropping existing columns when catalyst analysis is case
36+
* insensitive.
37+
*/
38+
public interface TableCatalog extends CatalogPlugin {
39+
/**
40+
* List the tables in a namespace from the catalog.
41+
* <p>
42+
* If the catalog supports views, this must return identifiers for only tables and not views.
43+
*
44+
* @param namespace a multi-part namespace
45+
* @return an array of Identifiers for tables
46+
* @throws NoSuchNamespaceException If the namespace does not exist (optional).
47+
*/
48+
Identifier[] listTables(String[] namespace) throws NoSuchNamespaceException;
49+
50+
/**
51+
* Load table metadata by {@link Identifier identifier} from the catalog.
52+
* <p>
53+
* If the catalog supports views and contains a view for the identifier and not a table, this
54+
* must throw {@link NoSuchTableException}.
55+
*
56+
* @param ident a table identifier
57+
* @return the table's metadata
58+
* @throws NoSuchTableException If the table doesn't exist or is a view
59+
*/
60+
Table loadTable(Identifier ident) throws NoSuchTableException;
61+
62+
/**
63+
* Invalidate cached table metadata for an {@link Identifier identifier}.
64+
* <p>
65+
* If the table is already loaded or cached, drop cached data. If the table does not exist or is
66+
* not cached, do nothing. Calling this method should not query remote services.
67+
*
68+
* @param ident a table identifier
69+
*/
70+
default void invalidateTable(Identifier ident) {
71+
}
72+
73+
/**
74+
* Test whether a table exists using an {@link Identifier identifier} from the catalog.
75+
* <p>
76+
* If the catalog supports views and contains a view for the identifier and not a table, this
77+
* must return false.
78+
*
79+
* @param ident a table identifier
80+
* @return true if the table exists, false otherwise
81+
*/
82+
default boolean tableExists(Identifier ident) {
83+
try {
84+
return loadTable(ident) != null;
85+
} catch (NoSuchTableException e) {
86+
return false;
87+
}
88+
}
89+
90+
/**
91+
* Create a table in the catalog.
92+
*
93+
* @param ident a table identifier
94+
* @param schema the schema of the new table, as a struct type
95+
* @param partitions transforms to use for partitioning data in the table
96+
* @param properties a string map of table properties
97+
* @return metadata for the new table
98+
* @throws TableAlreadyExistsException If a table or view already exists for the identifier
99+
* @throws UnsupportedOperationException If a requested partition transform is not supported
100+
* @throws NoSuchNamespaceException If the identifier namespace does not exist (optional)
101+
*/
102+
Table createTable(
103+
Identifier ident,
104+
StructType schema,
105+
Transform[] partitions,
106+
Map<String, String> properties) throws TableAlreadyExistsException, NoSuchNamespaceException;
107+
108+
/**
109+
* Apply a set of {@link TableChange changes} to a table in the catalog.
110+
* <p>
111+
* Implementations may reject the requested changes. If any change is rejected, none of the
112+
* changes should be applied to the table.
113+
* <p>
114+
* If the catalog supports views and contains a view for the identifier and not a table, this
115+
* must throw {@link NoSuchTableException}.
116+
*
117+
* @param ident a table identifier
118+
* @param changes changes to apply to the table
119+
* @return updated metadata for the table
120+
* @throws NoSuchTableException If the table doesn't exist or is a view
121+
* @throws IllegalArgumentException If any change is rejected by the implementation.
122+
*/
123+
Table alterTable(
124+
Identifier ident,
125+
TableChange... changes) throws NoSuchTableException;
126+
127+
/**
128+
* Drop a table in the catalog.
129+
* <p>
130+
* If the catalog supports views and contains a view for the identifier and not a table, this
131+
* must not drop the view and must return false.
132+
*
133+
* @param ident a table identifier
134+
* @return true if a table was deleted, false if no table exists for the identifier
135+
*/
136+
boolean dropTable(Identifier ident);
137+
}

0 commit comments

Comments
 (0)