|
| 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