private static boolean isManagedTable(Table table) { return table.getTableType().equals(MANAGED_TABLE.name()); }
@Override public String mapToClientType(String hiveTypeName) { try { TableType hiveType = TableType.valueOf(hiveTypeName.toUpperCase()); return hiveType.name(); } catch (IllegalArgumentException e) { LOG.warn("Invalid hive table type " + hiveTypeName); return hiveTypeName; } }
@Override public Set<String> getTableTypeNames() { Set<String> typeNameSet = new HashSet<String>(); for (TableType typeNames : TableType.values()) { typeNameSet.add(typeNames.name()); } return typeNameSet; } }
public TableBuilder() { // Set some reasonable defaults dbName = Warehouse.DEFAULT_DATABASE_NAME; tableParams = new HashMap<>(); createTime = lastAccessTime = (int)(System.currentTimeMillis() / 1000); retention = 0; partCols = new ArrayList<>(); type = TableType.MANAGED_TABLE.name(); mvReferencedTables = new HashSet<>(); temporary = false; super.setChild(this); }
@Override public synchronized Optional<List<String>> getAllViews(String databaseName) { Optional<List<String>> tables = getAllTables(databaseName); if (!tables.isPresent()) { return Optional.empty(); } List<String> views = tables.get().stream() .map(tableName -> getTable(databaseName, tableName)) .filter(Optional::isPresent) .map(Optional::get) .filter(table -> table.getTableType().equals(VIRTUAL_VIEW.name())) .map(Table::getTableName) .collect(toList()); return Optional.of(ImmutableList.copyOf(views)); }
public ClassicTableTypeMapping () { hiveToClientMap.put(TableType.MANAGED_TABLE.name(), ClassicTableTypes.TABLE.name()); hiveToClientMap.put(TableType.EXTERNAL_TABLE.name(), ClassicTableTypes.TABLE.name()); hiveToClientMap.put(TableType.VIRTUAL_VIEW.name(), ClassicTableTypes.VIEW.name()); hiveToClientMap.put(TableType.MATERIALIZED_VIEW.toString(), ClassicTableTypes.MATERIALIZED_VIEW.toString()); clientToHiveMap.putAll(ClassicTableTypes.TABLE.name(), Arrays.asList( TableType.MANAGED_TABLE.name(), TableType.EXTERNAL_TABLE.name())); clientToHiveMap.put(ClassicTableTypes.VIEW.name(), TableType.VIRTUAL_VIEW.name()); clientToHiveMap.put(ClassicTableTypes.MATERIALIZED_VIEW.toString(), TableType.MATERIALIZED_VIEW.toString()); }
@Override public synchronized void dropTable(String databaseName, String tableName, boolean deleteData) { requireNonNull(databaseName, "databaseName is null"); requireNonNull(tableName, "tableName is null"); Table table = getRequiredTable(databaseName, tableName); Path tableMetadataDirectory = getTableMetadataDirectory(databaseName, tableName); // It is safe to delete the whole meta directory for external tables and views if (!table.getTableType().equals(MANAGED_TABLE.name()) || deleteData) { deleteMetadataDirectory(tableMetadataDirectory); } else { // in this case we only wan to delete the metadata of a managed table deleteSchemaFile("table", tableMetadataDirectory); deleteTablePrivileges(table); } }
@Override public synchronized void replaceTable(String databaseName, String tableName, Table newTable, PrincipalPrivileges principalPrivileges) { Table table = getRequiredTable(databaseName, tableName); if (!table.getTableType().equals(VIRTUAL_VIEW.name()) || !newTable.getTableType().equals(VIRTUAL_VIEW.name())) { throw new PrestoException(HIVE_METASTORE_ERROR, "Only views can be updated with replaceTable"); } if (!table.getDatabaseName().equals(databaseName) || !table.getTableName().equals(tableName)) { throw new PrestoException(HIVE_METASTORE_ERROR, "Replacement table must have same name"); } Path tableMetadataDirectory = getTableMetadataDirectory(table); writeSchemaFile("table", tableMetadataDirectory, tableCodec, new TableMetadata(newTable), true); // replace existing permissions deleteTablePrivileges(table); for (Entry<String, Collection<HivePrivilegeInfo>> entry : principalPrivileges.getUserPrivileges().asMap().entrySet()) { setTablePrivileges(entry.getKey(), USER, table.getDatabaseName(), table.getTableName(), entry.getValue()); } for (Entry<String, Collection<HivePrivilegeInfo>> entry : principalPrivileges.getRolePrivileges().asMap().entrySet()) { setTablePrivileges(entry.getKey(), ROLE, table.getDatabaseName(), table.getTableName(), entry.getValue()); } }
public TableMetadata(Table table, Map<String, HiveColumnStatistics> columnStatistics) { owner = table.getOwner(); tableType = table.getTableType(); dataColumns = table.getDataColumns(); partitionColumns = table.getPartitionColumns(); parameters = table.getParameters(); StorageFormat tableFormat = table.getStorage().getStorageFormat(); storageFormat = Arrays.stream(HiveStorageFormat.values()) .filter(format -> tableFormat.equals(StorageFormat.fromHiveStorageFormat(format))) .findFirst(); bucketProperty = table.getStorage().getBucketProperty(); serdeParameters = table.getStorage().getSerdeParameters(); if (tableType.equals(TableType.EXTERNAL_TABLE.name())) { externalLocation = Optional.of(table.getStorage().getLocation()); } else { externalLocation = Optional.empty(); } viewOriginalText = table.getViewOriginalText(); viewExpandedText = table.getViewExpandedText(); this.columnStatistics = ImmutableMap.copyOf(requireNonNull(columnStatistics, "columnStatistics is null")); }
public PartitionMetadata(Table table, PartitionWithStatistics partitionWithStatistics) { Partition partition = partitionWithStatistics.getPartition(); PartitionStatistics statistics = partitionWithStatistics.getStatistics(); this.columns = partition.getColumns(); this.parameters = updateStatisticsParameters(partition.getParameters(), statistics.getBasicStatistics()); StorageFormat tableFormat = partition.getStorage().getStorageFormat(); storageFormat = Arrays.stream(HiveStorageFormat.values()) .filter(format -> tableFormat.equals(StorageFormat.fromHiveStorageFormat(format))) .findFirst(); if (table.getTableType().equals(TableType.EXTERNAL_TABLE.name())) { externalLocation = Optional.of(partition.getStorage().getLocation()); } else { externalLocation = Optional.empty(); } bucketProperty = partition.getStorage().getBucketProperty(); serdeParameters = partition.getStorage().getSerdeParameters(); columnStatistics = ImmutableMap.copyOf(statistics.getColumnStatistics()); }
private Table createView() throws Exception { Table table = createTable("test_append_part_table_view", getYearAndMonthPartCols(), null, TableType.VIRTUAL_VIEW.name(), null); return table; }
private Table createExternalTable() throws Exception { Map<String, String> tableParams = new HashMap<>(); tableParams.put("EXTERNAL", "TRUE"); Table table = createTable("test_append_part_external_table", getYearAndMonthPartCols(), tableParams, TableType.EXTERNAL_TABLE.name(), metaStore.getWarehouseRoot() + "/test_append_part_external_table"); return table; }
@Override public synchronized void dropTable(String databaseName, String tableName, boolean deleteData) { List<String> locations = listAllDataPaths(this, databaseName, tableName); SchemaTableName schemaTableName = new SchemaTableName(databaseName, tableName); Table table = relations.remove(schemaTableName); if (table == null) { throw new TableNotFoundException(schemaTableName); } views.remove(schemaTableName); partitions.keySet().removeIf(partitionName -> partitionName.matches(databaseName, tableName)); // remove data if (deleteData && table.getTableType().equals(MANAGED_TABLE.name())) { for (String location : locations) { if (location != null) { File directory = new File(new Path(location).toUri()); checkArgument(isParentDir(directory, baseDirectory), "Table directory must be inside of the metastore base directory"); deleteDirectory(directory); } } } }
private Table createTableWithPartitions() throws Exception { Table table = createTable("test_append_part_table_with_parts", getYearAndMonthPartCols(), null, TableType.MANAGED_TABLE.name(), metaStore.getWarehouseRoot() + "/test_append_part_table_with_parts"); createPartition(table, Lists.newArrayList("2017", "march")); createPartition(table, Lists.newArrayList("2017", "april")); createPartition(table, Lists.newArrayList("2018", "march")); return table; }
private TableMeta createTestTable(String dbName, String tableName, TableType type) throws Exception { Table table = createTable(dbName, tableName, type); client.createTable(table); TableMeta tableMeta = new TableMeta(dbName, tableName, type.name()); tableMeta.setCatName("hive"); return tableMeta; }
public static com.facebook.presto.hive.metastore.Table getPrestoTestTable(String dbName) { return com.facebook.presto.hive.metastore.Table.builder() .setDatabaseName(dbName) .setTableName("test-tbl" + generateRandom()) .setOwner("owner") .setParameters(ImmutableMap.of()) .setTableType(TableType.EXTERNAL_TABLE.name()) .setDataColumns(ImmutableList.of(getPrestoTestColumn())) .setPartitionColumns(ImmutableList.of(getPrestoTestColumn())) .setViewOriginalText(Optional.of("originalText")) .setViewExpandedText(Optional.of("expandedText")) .withStorage(STORAGE_CONSUMER).build(); }
private TableMeta createTestTable(String dbName, String tableName, TableType type, String comment) throws Exception { Table table = createTable(dbName, tableName, type); table.getParameters().put("comment", comment); client.createTable(table); TableMeta tableMeta = new TableMeta(dbName, tableName, type.name()); tableMeta.setComments(comment); tableMeta.setCatName("hive"); return tableMeta; }
public static Table getGlueTestTable(String dbName) { return new Table() .withDatabaseName(dbName) .withName("test-tbl" + generateRandom()) .withOwner("owner") .withParameters(ImmutableMap.of()) .withPartitionKeys(ImmutableList.of(getGlueTestColumn())) .withStorageDescriptor(getGlueTestStorageDescriptor()) .withTableType(TableType.EXTERNAL_TABLE.name()) .withViewOriginalText("originalText") .withViewExpandedText("expandedText"); }
private static Table createSimpleTable(SchemaTableName schemaTableName, List<Column> columns, ConnectorSession session, Path targetPath, String queryId) { String tableOwner = session.getUser(); String schemaName = schemaTableName.getSchemaName(); String tableName = schemaTableName.getTableName(); return Table.builder() .setDatabaseName(schemaName) .setTableName(tableName) .setOwner(tableOwner) .setTableType(TableType.MANAGED_TABLE.name()) .setParameters(ImmutableMap.of( PRESTO_VERSION_NAME, TEST_SERVER_VERSION, PRESTO_QUERY_ID_NAME, queryId)) .setDataColumns(columns) .withStorage(storage -> storage .setLocation(targetPath.toString()) .setStorageFormat(fromHiveStorageFormat(ORC)) .setSerdeParameters(ImmutableMap.of())) .build(); }
private void createMaterializedView(String dbName, String tableName, Set<String> tablesUsed) throws TException { Table t = new TableBuilder() .setDbName(dbName) .setTableName(tableName) .setType(TableType.MATERIALIZED_VIEW.name()) .addMaterializedViewReferencedTables(tablesUsed) .addCol("foo", "string") .addCol("bar", "string") .create(client, conf); }