Back to home page

OSCL-LXR

 
 

    


0001 /*
0002  * Licensed to the Apache Software Foundation (ASF) under one or more
0003  * contributor license agreements.  See the NOTICE file distributed with
0004  * this work for additional information regarding copyright ownership.
0005  * The ASF licenses this file to You under the Apache License, Version 2.0
0006  * (the "License"); you may not use this file except in compliance with
0007  * the License.  You may obtain a copy of the License at
0008  *
0009  *    http://www.apache.org/licenses/LICENSE-2.0
0010  *
0011  * Unless required by applicable law or agreed to in writing, software
0012  * distributed under the License is distributed on an "AS IS" BASIS,
0013  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
0014  * See the License for the specific language governing permissions and
0015  * limitations under the License.
0016  */
0017 
0018 package org.apache.spark.sql.connector.catalog;
0019 
0020 import java.util.Map;
0021 
0022 import org.apache.spark.annotation.Evolving;
0023 import org.apache.spark.sql.catalyst.analysis.NamespaceAlreadyExistsException;
0024 import org.apache.spark.sql.catalyst.analysis.NoSuchNamespaceException;
0025 import org.apache.spark.sql.catalyst.analysis.NoSuchTableException;
0026 import org.apache.spark.sql.catalyst.analysis.TableAlreadyExistsException;
0027 import org.apache.spark.sql.connector.expressions.Transform;
0028 import org.apache.spark.sql.types.StructType;
0029 import org.apache.spark.sql.util.CaseInsensitiveStringMap;
0030 
0031 /**
0032  * A simple implementation of {@link CatalogExtension}, which implements all the catalog functions
0033  * by calling the built-in session catalog directly. This is created for convenience, so that users
0034  * only need to override some methods where they want to apply custom logic. For example, they can
0035  * override {@code createTable}, do something else before calling {@code super.createTable}.
0036  *
0037  * @since 3.0.0
0038  */
0039 @Evolving
0040 public abstract class DelegatingCatalogExtension implements CatalogExtension {
0041 
0042   private CatalogPlugin delegate;
0043 
0044   public final void setDelegateCatalog(CatalogPlugin delegate) {
0045     this.delegate = delegate;
0046   }
0047 
0048   @Override
0049   public String name() {
0050     return delegate.name();
0051   }
0052 
0053   @Override
0054   public final void initialize(String name, CaseInsensitiveStringMap options) {}
0055 
0056   @Override
0057   public String[] defaultNamespace() {
0058     return delegate.defaultNamespace();
0059   }
0060 
0061   @Override
0062   public Identifier[] listTables(String[] namespace) throws NoSuchNamespaceException {
0063     return asTableCatalog().listTables(namespace);
0064   }
0065 
0066   @Override
0067   public Table loadTable(Identifier ident) throws NoSuchTableException {
0068     return asTableCatalog().loadTable(ident);
0069   }
0070 
0071   @Override
0072   public void invalidateTable(Identifier ident) {
0073     asTableCatalog().invalidateTable(ident);
0074   }
0075 
0076   @Override
0077   public boolean tableExists(Identifier ident) {
0078     return asTableCatalog().tableExists(ident);
0079   }
0080 
0081   @Override
0082   public Table createTable(
0083       Identifier ident,
0084       StructType schema,
0085       Transform[] partitions,
0086       Map<String, String> properties) throws TableAlreadyExistsException, NoSuchNamespaceException {
0087     return asTableCatalog().createTable(ident, schema, partitions, properties);
0088   }
0089 
0090   @Override
0091   public Table alterTable(
0092       Identifier ident,
0093       TableChange... changes) throws NoSuchTableException {
0094     return asTableCatalog().alterTable(ident, changes);
0095   }
0096 
0097   @Override
0098   public boolean dropTable(Identifier ident) {
0099     return asTableCatalog().dropTable(ident);
0100   }
0101 
0102   @Override
0103   public void renameTable(
0104       Identifier oldIdent,
0105       Identifier newIdent) throws NoSuchTableException, TableAlreadyExistsException {
0106     asTableCatalog().renameTable(oldIdent, newIdent);
0107   }
0108 
0109   @Override
0110   public String[][] listNamespaces() throws NoSuchNamespaceException {
0111     return asNamespaceCatalog().listNamespaces();
0112   }
0113 
0114   @Override
0115   public String[][] listNamespaces(String[] namespace) throws NoSuchNamespaceException {
0116     return asNamespaceCatalog().listNamespaces(namespace);
0117   }
0118 
0119   @Override
0120   public boolean namespaceExists(String[] namespace) {
0121     return asNamespaceCatalog().namespaceExists(namespace);
0122   }
0123 
0124   @Override
0125   public Map<String, String> loadNamespaceMetadata(
0126       String[] namespace) throws NoSuchNamespaceException {
0127     return asNamespaceCatalog().loadNamespaceMetadata(namespace);
0128   }
0129 
0130   @Override
0131   public void createNamespace(
0132       String[] namespace,
0133       Map<String, String> metadata) throws NamespaceAlreadyExistsException {
0134     asNamespaceCatalog().createNamespace(namespace, metadata);
0135   }
0136 
0137   @Override
0138   public void alterNamespace(
0139       String[] namespace,
0140       NamespaceChange... changes) throws NoSuchNamespaceException {
0141     asNamespaceCatalog().alterNamespace(namespace, changes);
0142   }
0143 
0144   @Override
0145   public boolean dropNamespace(String[] namespace) throws NoSuchNamespaceException {
0146     return asNamespaceCatalog().dropNamespace(namespace);
0147   }
0148 
0149   private TableCatalog asTableCatalog() {
0150     return (TableCatalog)delegate;
0151   }
0152 
0153   private SupportsNamespaces asNamespaceCatalog() {
0154     return (SupportsNamespaces)delegate;
0155   }
0156 }