|
| 1 | +package com.cloudata.structured.sql.provider; |
| 2 | + |
| 3 | +import java.util.List; |
| 4 | +import java.util.Map; |
| 5 | + |
| 6 | +import com.facebook.presto.spi.ColumnHandle; |
| 7 | +import com.facebook.presto.spi.ColumnMetadata; |
| 8 | +import com.facebook.presto.spi.ConnectorMetadata; |
| 9 | +import com.facebook.presto.spi.ConnectorTableMetadata; |
| 10 | +import com.facebook.presto.spi.SchemaTableName; |
| 11 | +import com.facebook.presto.spi.SchemaTablePrefix; |
| 12 | +import com.facebook.presto.spi.TableHandle; |
| 13 | +import com.google.common.collect.Lists; |
| 14 | + |
| 15 | +public class CloudataConnectorMetadata implements ConnectorMetadata { |
| 16 | + |
| 17 | + final String connectorId; |
| 18 | + |
| 19 | + public CloudataConnectorMetadata(String connectorId) { |
| 20 | + this.connectorId = connectorId; |
| 21 | + } |
| 22 | + |
| 23 | + @Override |
| 24 | + public boolean canHandle(TableHandle tableHandle) { |
| 25 | + return tableHandle instanceof CloudataTableHandle |
| 26 | + && ((CloudataTableHandle) tableHandle).getConnectorId().equals(connectorId); |
| 27 | + } |
| 28 | + |
| 29 | + @Override |
| 30 | + public List<String> listSchemaNames() { |
| 31 | + List<String> schemas = Lists.newArrayList(); |
| 32 | + schemas.add("default"); |
| 33 | + return schemas; |
| 34 | + } |
| 35 | + |
| 36 | + @Override |
| 37 | + public CloudataTableHandle getTableHandle(SchemaTableName tableName) { |
| 38 | + if (!listSchemaNames().contains(tableName.getSchemaName())) { |
| 39 | + return null; |
| 40 | + } |
| 41 | + |
| 42 | + // ExampleTable table = exampleClient.getTable(tableName.getSchemaName(), tableName.getTableName()); |
| 43 | + // if (table == null) { |
| 44 | + // return null; |
| 45 | + // } |
| 46 | + |
| 47 | + return new CloudataTableHandle(connectorId, tableName.getSchemaName(), tableName.getTableName()); |
| 48 | + } |
| 49 | + |
| 50 | + @Override |
| 51 | + public ConnectorTableMetadata getTableMetadata(TableHandle table) { |
| 52 | + CloudataTableHandle tableHandle = promote(table); |
| 53 | + return tableHandle.getTableMetadata(); |
| 54 | + } |
| 55 | + |
| 56 | + private CloudataTableHandle promote(TableHandle table) { |
| 57 | + assert (table != null); |
| 58 | + // checkArgument(table instanceof CloudataTableHandle, "tableHandle is not an instance of CloudataTableHandle"); |
| 59 | + assert (table instanceof CloudataTableHandle); |
| 60 | + CloudataTableHandle tableHandle = (CloudataTableHandle) table; |
| 61 | + assert tableHandle.getConnectorId().equals(connectorId); |
| 62 | + // checkArgument(tableHandle.getConnectorId().equals(connectorId), "tableHandle is not for this connector"); |
| 63 | + return tableHandle; |
| 64 | + } |
| 65 | + |
| 66 | + @Override |
| 67 | + public List<SchemaTableName> listTables(String schemaNameOrNull) { |
| 68 | + throw new UnsupportedOperationException(); |
| 69 | + } |
| 70 | + |
| 71 | + @Override |
| 72 | + public ColumnHandle getColumnHandle(TableHandle tableHandle, String columnName) { |
| 73 | + CloudataTableHandle exampleTableHandle = promote(tableHandle); |
| 74 | + |
| 75 | + // ExampleTable table = exampleClient.getTable(exampleTableHandle.getSchemaName(), |
| 76 | + // exampleTableHandle.getTableName()); |
| 77 | + // if (table == null) { |
| 78 | + // throw new TableNotFoundException(exampleTableHandle.toSchemaTableName()); |
| 79 | + // } |
| 80 | + |
| 81 | + // ImmutableMap.Builder<String, ColumnHandle> columnHandles = ImmutableMap.builder(); |
| 82 | + // for (ColumnMetadata columnMetadata : table.getColumnsMetadata()) { |
| 83 | + // columnHandles.put(columnMetadata.getName(), new ExampleColumnHandle(connectorId, columnMetadata)); |
| 84 | + // } |
| 85 | + // return columnHandles.build(); |
| 86 | + |
| 87 | + return exampleTableHandle.getColumnHandle(columnName); |
| 88 | + } |
| 89 | + |
| 90 | + @Override |
| 91 | + public Map<String, ColumnHandle> getColumnHandles(TableHandle tableHandle) { |
| 92 | + CloudataTableHandle exampleTableHandle = promote(tableHandle); |
| 93 | + |
| 94 | + // ExampleTable table = exampleClient.getTable(exampleTableHandle.getSchemaName(), |
| 95 | + // exampleTableHandle.getTableName()); |
| 96 | + // if (table == null) { |
| 97 | + // throw new TableNotFoundException(exampleTableHandle.toSchemaTableName()); |
| 98 | + // } |
| 99 | + |
| 100 | + // ImmutableMap.Builder<String, ColumnHandle> columnHandles = ImmutableMap.builder(); |
| 101 | + // for (ColumnMetadata columnMetadata : table.getColumnsMetadata()) { |
| 102 | + // columnHandles.put(columnMetadata.getName(), new ExampleColumnHandle(connectorId, columnMetadata)); |
| 103 | + // } |
| 104 | + // return columnHandles.build(); |
| 105 | + |
| 106 | + return exampleTableHandle.getColumnHandles(); |
| 107 | + } |
| 108 | + |
| 109 | + @Override |
| 110 | + public ColumnMetadata getColumnMetadata(TableHandle tableHandle, ColumnHandle column) { |
| 111 | + // checkNotNull(tableHandle, "tableHandle is null"); |
| 112 | + // checkArgument(tableHandle instanceof MockTableHandle, "tableHandle is not an instance of MockTableHandle"); |
| 113 | + // checkArgument(((MockTableHandle) tableHandle).getConnectorId().equals(connectorId), |
| 114 | + // "tableHandle is not for this connector"); |
| 115 | + |
| 116 | + CloudataColumnHandle columnHandle = promote(column); |
| 117 | + |
| 118 | + return columnHandle.getColumnMetadata(); |
| 119 | + } |
| 120 | + |
| 121 | + private CloudataColumnHandle promote(ColumnHandle column) { |
| 122 | + // checkNotNull(columnHandle, "columnHandle is null"); |
| 123 | + assert column != null; |
| 124 | + // checkArgument(columnHandle instanceof CloudataColumnHandle, |
| 125 | + // "columnHandle is not an instance of CloudataColumnHandle"); |
| 126 | + assert column instanceof CloudataColumnHandle; |
| 127 | + return (CloudataColumnHandle) column; |
| 128 | + } |
| 129 | + |
| 130 | + @Override |
| 131 | + public Map<SchemaTableName, List<ColumnMetadata>> listTableColumns(SchemaTablePrefix prefix) { |
| 132 | + throw new UnsupportedOperationException(); |
| 133 | + } |
| 134 | + |
| 135 | + @Override |
| 136 | + public TableHandle createTable(ConnectorTableMetadata tableMetadata) { |
| 137 | + throw new UnsupportedOperationException(); |
| 138 | + } |
| 139 | + |
| 140 | + @Override |
| 141 | + public void dropTable(TableHandle tableHandle) { |
| 142 | + throw new UnsupportedOperationException(); |
| 143 | + } |
| 144 | + |
| 145 | +} |
0 commit comments