default Optional<List<FieldSchema>> getFields(String databaseName, String tableName) { Optional<Table> table = getTable(databaseName, tableName); if (!table.isPresent()) { throw new TableNotFoundException(new SchemaTableName(databaseName, tableName)); } if (table.get().getSd() == null) { throw new PrestoException(HIVE_INVALID_METADATA, "Table is missing storage descriptor"); } return Optional.of(table.get().getSd().getCols()); } }
public void undo(ExtendedHiveMetastore metastore) { if (!done) { return; } if (partitionName.isPresent()) { metastore.updatePartitionStatistics(tableName.getSchemaName(), tableName.getTableName(), partitionName.get(), this::resetStatistics); } else { metastore.updateTableStatistics(tableName.getSchemaName(), tableName.getTableName(), this::resetStatistics); } }
@Override public void checkCanCreateView(ConnectorTransactionHandle transaction, Identity identity, SchemaTableName viewName) { if (!isDatabaseOwner(identity, viewName.getSchemaName())) { denyCreateView(viewName.toString()); } }
@Override public void checkCanCreateTable(ConnectorTransactionHandle transaction, Identity identity, SchemaTableName tableName) { if (!isDatabaseOwner(transaction, identity, tableName.getSchemaName())) { denyCreateTable(tableName.toString()); } }
@Override public Optional<ConnectorNewTableLayout> getInsertLayout(ConnectorSession session, ConnectorTableHandle tableHandle) { HiveTableHandle hiveTableHandle = (HiveTableHandle) tableHandle; SchemaTableName tableName = hiveTableHandle.getSchemaTableName(); Table table = metastore.getTable(tableName.getSchemaName(), tableName.getTableName()) .orElseThrow(() -> new TableNotFoundException(tableName)); Optional<HiveBucketHandle> hiveBucketHandle = getHiveBucketHandle(table); if (!hiveBucketHandle.isPresent()) { return Optional.empty(); } HiveBucketProperty bucketProperty = table.getStorage().getBucketProperty() .orElseThrow(() -> new NoSuchElementException("Bucket property should be set")); if (!bucketProperty.getSortedBy().isEmpty() && !isSortedWritingEnabled(session)) { throw new PrestoException(NOT_SUPPORTED, "Writing to bucketed sorted Hive tables is disabled"); } HivePartitioningHandle partitioningHandle = new HivePartitioningHandle( hiveBucketHandle.get().getTableBucketCount(), hiveBucketHandle.get().getColumns().stream() .map(HiveColumnHandle::getHiveType) .collect(Collectors.toList()), OptionalInt.of(hiveBucketHandle.get().getTableBucketCount())); List<String> partitionColumns = hiveBucketHandle.get().getColumns().stream() .map(HiveColumnHandle::getName) .collect(Collectors.toList()); return Optional.of(new ConnectorNewTableLayout(partitioningHandle, partitionColumns)); }
@Override public Map<SchemaTableName, ConnectorViewDefinition> getViews(ConnectorSession session, SchemaTablePrefix prefix) { ImmutableMap.Builder<SchemaTableName, ConnectorViewDefinition> views = ImmutableMap.builder(); List<SchemaTableName> tableNames; if (prefix.getTableName() != null) { tableNames = ImmutableList.of(new SchemaTableName(prefix.getSchemaName(), prefix.getTableName())); } else { tableNames = listViews(session, prefix.getSchemaName()); } for (SchemaTableName schemaTableName : tableNames) { Optional<Table> table = metastore.getTable(schemaTableName.getSchemaName(), schemaTableName.getTableName()); if (table.isPresent() && HiveUtil.isPrestoView(table.get())) { views.put(schemaTableName, new ConnectorViewDefinition( schemaTableName, Optional.ofNullable(table.get().getOwner()), decodeViewData(table.get().getViewOriginalText().get()))); } } return views.build(); }
public Optional<Set<TablePrivilege>> match(String user, SchemaTableName table) { if (userRegex.map(regex -> regex.matcher(user).matches()).orElse(true) && schemaRegex.map(regex -> regex.matcher(table.getSchemaName()).matches()).orElse(true) && tableRegex.map(regex -> regex.matcher(table.getTableName()).matches()).orElse(true)) { return Optional.of(privileges); } return Optional.empty(); }
@Override public Optional<ViewDefinition> getView(Session session, QualifiedObjectName viewName) { Optional<CatalogMetadata> catalog = getOptionalCatalogMetadata(session, viewName.getCatalogName()); if (catalog.isPresent()) { CatalogMetadata catalogMetadata = catalog.get(); ConnectorId connectorId = catalogMetadata.getConnectorId(session, viewName); ConnectorMetadata metadata = catalogMetadata.getMetadataFor(connectorId); Map<SchemaTableName, ConnectorViewDefinition> views = metadata.getViews( session.toConnectorSession(connectorId), viewName.asSchemaTableName().toSchemaTablePrefix()); ConnectorViewDefinition view = views.get(viewName.asSchemaTableName()); if (view != null) { ViewDefinition definition = deserializeView(view.getViewData()); if (view.getOwner().isPresent()) { definition = definition.withOwner(view.getOwner().get()); } return Optional.of(definition); } } return Optional.empty(); }
@Test public void tableIsCreatedAfterCommits() { assertNoTables(); SchemaTableName schemaTableName = new SchemaTableName("default", "temp_table"); ConnectorOutputTableHandle table = metadata.beginCreateTable( SESSION, new ConnectorTableMetadata(schemaTableName, ImmutableList.of(), ImmutableMap.of()), Optional.empty()); metadata.finishCreateTable(SESSION, table, ImmutableList.of(), ImmutableList.of()); List<SchemaTableName> tables = metadata.listTables(SESSION, Optional.empty()); assertTrue(tables.size() == 1, "Expected only one table"); assertTrue(tables.get(0).getTableName().equals("temp_table"), "Expected table with name 'temp_table'"); }
@Test public void testCreateSchema() { assertEquals(metadata.listSchemaNames(SESSION), ImmutableList.of("default")); metadata.createSchema(SESSION, "test", ImmutableMap.of()); assertEquals(metadata.listSchemaNames(SESSION), ImmutableList.of("default", "test")); assertEquals(metadata.listTables(SESSION, "test"), ImmutableList.of()); SchemaTableName tableName = new SchemaTableName("test", "first_table"); metadata.createTable( SESSION, new ConnectorTableMetadata( tableName, ImmutableList.of(), ImmutableMap.of()), false); assertEquals(metadata.listTables(SESSION, Optional.empty()), ImmutableList.of(tableName)); assertEquals(metadata.listTables(SESSION, Optional.of("test")), ImmutableList.of(tableName)); assertEquals(metadata.listTables(SESSION, Optional.of("default")), ImmutableList.of()); }
private Map<SchemaTableName, ElasticsearchTableDescription> createTableDescriptions(ElasticsearchConnectorConfig config, JsonCodec<ElasticsearchTableDescription> codec) { Optional<File[]> files = listFiles(config.getTableDescriptionDirectory()); if (!files.isPresent()) { return ImmutableMap.of(); } ImmutableMap.Builder<SchemaTableName, ElasticsearchTableDescription> builder = ImmutableMap.builder(); for (File file : files.get()) { if (!file.isFile() || !file.getName().endsWith(".json")) { continue; } ElasticsearchTableDescription table; try { table = codec.fromJson(readAllBytes(file.toPath())); } catch (IOException e) { throw new UncheckedIOException(e); } String schemaName = firstNonNull(table.getSchemaName(), config.getDefaultSchema()); builder.put(new SchemaTableName(schemaName, table.getTableName()), table); } return builder.build(); }
public synchronized HivePageSinkMetadata generatePageSinkMetadata(SchemaTableName schemaTableName) { checkReadable(); Optional<Table> table = getTable(schemaTableName.getSchemaName(), schemaTableName.getTableName()); if (!table.isPresent()) { return new HivePageSinkMetadata(schemaTableName, Optional.empty(), ImmutableMap.of()); } Map<List<String>, Action<PartitionAndMore>> partitionActionMap = partitionActions.get(schemaTableName); Map<List<String>, Optional<Partition>> modifiedPartitionMap; if (partitionActionMap == null) { modifiedPartitionMap = ImmutableMap.of(); } else { ImmutableMap.Builder<List<String>, Optional<Partition>> modifiedPartitionMapBuilder = ImmutableMap.builder(); for (Map.Entry<List<String>, Action<PartitionAndMore>> entry : partitionActionMap.entrySet()) { modifiedPartitionMapBuilder.put(entry.getKey(), getPartitionFromPartitionAction(entry.getValue())); } modifiedPartitionMap = modifiedPartitionMapBuilder.build(); } return new HivePageSinkMetadata( schemaTableName, table, modifiedPartitionMap); }
private Optional<ConnectorTableMetadata> getTableMetadata(SchemaTableName tableName) { ElasticsearchTableDescription table = client.getTable(tableName.getSchemaName(), tableName.getTableName()); if (table == null) { return Optional.empty(); } return Optional.of(new ConnectorTableMetadata(tableName, client.getColumnMetadata(table))); }
@Override public List<SchemaTableName> listTables(ConnectorSession session, Optional<String> schemaName) { ImmutableList.Builder<SchemaTableName> builder = ImmutableList.builder(); for (SchemaTableName tableName : tables.keySet()) { if (schemaName.map(tableName.getSchemaName()::equals).orElse(true)) { builder.add(tableName); } } return builder.build(); }
@Test public void testCreateTableInNotExistSchema() { SchemaTableName schemaTableName = new SchemaTableName("schema1", "test_table"); try { metadata.beginCreateTable(SESSION, new ConnectorTableMetadata(schemaTableName, ImmutableList.of(), tableProperties), Optional.empty()); fail("Should fail because schema does not exist"); } catch (PrestoException ex) { assertEquals(ex.getErrorCode(), NOT_FOUND.toErrorCode()); assertTrue(ex.getMessage().equals("Schema schema1 not found")); } }
public List<SchemaTableName> listTables(Optional<String> schemaName) { return tableDescriptions.getAllSchemaTableNames() .stream() .filter(schemaTableName -> !schemaName.isPresent() || schemaTableName.getSchemaName().equals(schemaName.get())) .collect(toImmutableList()); }
@Test public void testCreateOnlyNullsPredicate() { ImmutableList.Builder<HivePartition> partitions = ImmutableList.builder(); for (int i = 0; i < 5; i++) { partitions.add(new HivePartition( new SchemaTableName("test", "test"), Integer.toString(i), ImmutableMap.of(TEST_COLUMN_HANDLE, NullableValue.asNull(VarcharType.VARCHAR)))); } createPredicate(ImmutableList.of(TEST_COLUMN_HANDLE), partitions.build()); }
@Test public void testRenameTable() { SchemaTableName tableName = new SchemaTableName("test_schema", "test_table_to_be_renamed"); metadata.createSchema(SESSION, "test_schema", ImmutableMap.of()); ConnectorOutputTableHandle table = metadata.beginCreateTable( SESSION, new ConnectorTableMetadata(tableName, ImmutableList.of(), ImmutableMap.of()), Optional.empty()); metadata.finishCreateTable(SESSION, table, ImmutableList.of(), ImmutableList.of()); // rename table to schema which does not exist SchemaTableName invalidSchemaTableName = new SchemaTableName("test_schema_not_exist", "test_table_renamed"); ConnectorTableHandle tableHandle = metadata.getTableHandle(SESSION, tableName); Throwable throwable = expectThrows(SchemaNotFoundException.class, () -> metadata.renameTable(SESSION, tableHandle, invalidSchemaTableName)); assertTrue(throwable.getMessage().equals("Schema test_schema_not_exist not found")); // rename table to same schema SchemaTableName sameSchemaTableName = new SchemaTableName("test_schema", "test_renamed"); metadata.renameTable(SESSION, metadata.getTableHandle(SESSION, tableName), sameSchemaTableName); assertEquals(metadata.listTables(SESSION, "test_schema"), ImmutableList.of(sameSchemaTableName)); // rename table to different schema metadata.createSchema(SESSION, "test_different_schema", ImmutableMap.of()); SchemaTableName differentSchemaTableName = new SchemaTableName("test_different_schema", "test_renamed"); metadata.renameTable(SESSION, metadata.getTableHandle(SESSION, sameSchemaTableName), differentSchemaTableName); assertEquals(metadata.listTables(SESSION, "test_schema"), ImmutableList.of()); assertEquals(metadata.listTables(SESSION, "test_different_schema"), ImmutableList.of(differentSchemaTableName)); }
@Override public Map<String, PartitionStatistics> getPartitionStatistics(String databaseName, String tableName, Set<String> partitionNames) { ImmutableMap.Builder<String, PartitionStatistics> result = ImmutableMap.builder(); getPartitionsByNames(databaseName, tableName, ImmutableList.copyOf(partitionNames)).forEach((partitionName, optionalPartition) -> { Partition partition = optionalPartition.orElseThrow(() -> new PartitionNotFoundException(new SchemaTableName(databaseName, tableName), toPartitionValues(partitionName))); PartitionStatistics partitionStatistics = new PartitionStatistics(getHiveBasicStatistics(partition.getParameters()), ImmutableMap.of()); result.put(partitionName, partitionStatistics); }); return result.build(); }
protected String partitionTargetPath(SchemaTableName schemaTableName, String partitionName) { try (Transaction transaction = newTransaction()) { ConnectorSession session = newSession(); SemiTransactionalHiveMetastore metastore = transaction.getMetastore(schemaTableName.getSchemaName()); LocationService locationService = getLocationService(); Table table = metastore.getTable(schemaTableName.getSchemaName(), schemaTableName.getTableName()).get(); LocationHandle handle = locationService.forExistingTable(metastore, session, table); return locationService.getPartitionWriteInfo(handle, Optional.empty(), partitionName).getTargetPath().toString(); } }