|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one |
| 3 | + * or more contributor license agreements. See the NOTICE file |
| 4 | + * distributed with this work for additional information |
| 5 | + * regarding copyright ownership. The ASF licenses this file |
| 6 | + * to you under the Apache License, Version 2.0 (the |
| 7 | + * "License"); you may not use this file except in compliance |
| 8 | + * with the License. You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, |
| 13 | + * software distributed under the License is distributed on an |
| 14 | + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | + * KIND, either express or implied. See the License for the |
| 16 | + * specific language governing permissions and limitations |
| 17 | + * under the License. |
| 18 | + */ |
| 19 | + |
| 20 | +package org.apache.tsfile.spark; |
| 21 | + |
| 22 | +import org.apache.tsfile.i18n.Messages; |
| 23 | + |
| 24 | +import org.apache.spark.sql.catalyst.analysis.NoSuchNamespaceException; |
| 25 | +import org.apache.spark.sql.catalyst.analysis.NoSuchTableException; |
| 26 | +import org.apache.spark.sql.catalyst.analysis.TableAlreadyExistsException; |
| 27 | +import org.apache.spark.sql.connector.catalog.Identifier; |
| 28 | +import org.apache.spark.sql.connector.catalog.Table; |
| 29 | +import org.apache.spark.sql.connector.catalog.TableCatalog; |
| 30 | +import org.apache.spark.sql.connector.catalog.TableChange; |
| 31 | +import org.apache.spark.sql.connector.expressions.Transform; |
| 32 | +import org.apache.spark.sql.types.StructType; |
| 33 | +import org.apache.spark.sql.util.CaseInsensitiveStringMap; |
| 34 | + |
| 35 | +import java.util.ArrayList; |
| 36 | +import java.util.HashMap; |
| 37 | +import java.util.List; |
| 38 | +import java.util.Map; |
| 39 | + |
| 40 | +public class TsFileTableCatalog implements TableCatalog { |
| 41 | + |
| 42 | + private final Map<String, StoredTable> tables = new HashMap<>(); |
| 43 | + private String name; |
| 44 | + |
| 45 | + @Override |
| 46 | + public void initialize(String name, CaseInsensitiveStringMap options) { |
| 47 | + this.name = name; |
| 48 | + } |
| 49 | + |
| 50 | + @Override |
| 51 | + public String name() { |
| 52 | + return name; |
| 53 | + } |
| 54 | + |
| 55 | + @Override |
| 56 | + public Identifier[] listTables(String[] namespace) throws NoSuchNamespaceException { |
| 57 | + List<Identifier> identifiers = new ArrayList<>(); |
| 58 | + for (StoredTable table : tables.values()) { |
| 59 | + if (sameNamespace(namespace, table.identifier.namespace())) { |
| 60 | + identifiers.add(table.identifier); |
| 61 | + } |
| 62 | + } |
| 63 | + return identifiers.toArray(new Identifier[0]); |
| 64 | + } |
| 65 | + |
| 66 | + @Override |
| 67 | + public Table loadTable(Identifier ident) throws NoSuchTableException { |
| 68 | + StoredTable storedTable = tables.get(key(ident)); |
| 69 | + if (storedTable == null) { |
| 70 | + throw new NoSuchTableException(ident); |
| 71 | + } |
| 72 | + return storedTable.table(); |
| 73 | + } |
| 74 | + |
| 75 | + @Override |
| 76 | + public Table createTable( |
| 77 | + Identifier ident, StructType schema, Transform[] partitions, Map<String, String> properties) |
| 78 | + throws TableAlreadyExistsException, NoSuchNamespaceException { |
| 79 | + String key = key(ident); |
| 80 | + if (tables.containsKey(key)) { |
| 81 | + throw new TableAlreadyExistsException(ident); |
| 82 | + } |
| 83 | + Map<String, String> connectorOptions = connectorOptions(properties); |
| 84 | + StoredTable storedTable = new StoredTable(ident, schema, connectorOptions); |
| 85 | + tables.put(key, storedTable); |
| 86 | + return storedTable.table(); |
| 87 | + } |
| 88 | + |
| 89 | + @Override |
| 90 | + public Table alterTable(Identifier ident, TableChange... changes) throws NoSuchTableException { |
| 91 | + throw new UnsupportedOperationException(Messages.get("error.spark.catalog_alter_unsupported")); |
| 92 | + } |
| 93 | + |
| 94 | + @Override |
| 95 | + public boolean dropTable(Identifier ident) { |
| 96 | + return tables.remove(key(ident)) != null; |
| 97 | + } |
| 98 | + |
| 99 | + @Override |
| 100 | + public void renameTable(Identifier oldIdent, Identifier newIdent) |
| 101 | + throws NoSuchTableException, TableAlreadyExistsException { |
| 102 | + StoredTable storedTable = tables.remove(key(oldIdent)); |
| 103 | + if (storedTable == null) { |
| 104 | + throw new NoSuchTableException(oldIdent); |
| 105 | + } |
| 106 | + String newKey = key(newIdent); |
| 107 | + if (tables.containsKey(newKey)) { |
| 108 | + tables.put(key(oldIdent), storedTable); |
| 109 | + throw new TableAlreadyExistsException(newIdent); |
| 110 | + } |
| 111 | + tables.put(newKey, storedTable.withIdentifier(newIdent)); |
| 112 | + } |
| 113 | + |
| 114 | + private static Map<String, String> connectorOptions(Map<String, String> properties) { |
| 115 | + Map<String, String> options = new HashMap<>(); |
| 116 | + for (Map.Entry<String, String> entry : properties.entrySet()) { |
| 117 | + String key = entry.getKey(); |
| 118 | + if (key.startsWith(TableCatalog.OPTION_PREFIX)) { |
| 119 | + key = key.substring(TableCatalog.OPTION_PREFIX.length()); |
| 120 | + } |
| 121 | + if (TsFileTableOptions.isKnownOption(key)) { |
| 122 | + options.put(key, entry.getValue()); |
| 123 | + } |
| 124 | + } |
| 125 | + return options; |
| 126 | + } |
| 127 | + |
| 128 | + private static String key(Identifier ident) { |
| 129 | + return String.join("\u0001", ident.namespace()) + "\u0002" + ident.name(); |
| 130 | + } |
| 131 | + |
| 132 | + private static boolean sameNamespace(String[] left, String[] right) { |
| 133 | + if (left.length != right.length) { |
| 134 | + return false; |
| 135 | + } |
| 136 | + for (int i = 0; i < left.length; i++) { |
| 137 | + if (!left[i].equals(right[i])) { |
| 138 | + return false; |
| 139 | + } |
| 140 | + } |
| 141 | + return true; |
| 142 | + } |
| 143 | + |
| 144 | + private static class StoredTable { |
| 145 | + private final Identifier identifier; |
| 146 | + private final StructType schema; |
| 147 | + private final Map<String, String> properties; |
| 148 | + |
| 149 | + private StoredTable(Identifier identifier, StructType schema, Map<String, String> properties) { |
| 150 | + this.identifier = identifier; |
| 151 | + this.schema = schema; |
| 152 | + this.properties = properties; |
| 153 | + } |
| 154 | + |
| 155 | + private StoredTable withIdentifier(Identifier identifier) { |
| 156 | + return new StoredTable(identifier, schema, properties); |
| 157 | + } |
| 158 | + |
| 159 | + private Table table() { |
| 160 | + return new TsFileTable(schema, properties); |
| 161 | + } |
| 162 | + } |
| 163 | +} |
0 commit comments