PrincipalPrivileges principalPrivileges = buildInitialPrivilegeSet(session.getUser()); Optional<Table> existing = metastore.getTable(viewName.getSchemaName(), viewName.getTableName()); if (existing.isPresent()) { if (!replace || !HiveUtil.isPrestoView(existing.get())) { metastore.replaceView(viewName.getSchemaName(), viewName.getTableName(), table, principalPrivileges); return; metastore.createTable(session, table, principalPrivileges, Optional.empty(), false, new PartitionStatistics(createEmptyStatistics(), ImmutableMap.of()));
@Override public void dropTable(ConnectorSession session, ConnectorTableHandle tableHandle) { HiveTableHandle handle = (HiveTableHandle) tableHandle; SchemaTableName tableName = schemaTableName(tableHandle); Optional<Table> target = metastore.getTable(handle.getSchemaName(), handle.getTableName()); if (!target.isPresent()) { throw new TableNotFoundException(tableName); } metastore.dropTable(session, handle.getSchemaName(), handle.getTableName()); }
private static Map<String, PartitionStatistics> getPartitionsStatistics(SemiTransactionalHiveMetastore metastore, SchemaTableName table, List<HivePartition> hivePartitions) { if (hivePartitions.isEmpty()) { return ImmutableMap.of(); } boolean unpartitioned = hivePartitions.stream().anyMatch(partition -> partition.getPartitionId().equals(UNPARTITIONED_ID)); if (unpartitioned) { checkArgument(hivePartitions.size() == 1, "expected only one hive partition"); return ImmutableMap.of(UNPARTITIONED_ID, metastore.getTableStatistics(table.getSchemaName(), table.getTableName())); } Set<String> partitionNames = hivePartitions.stream() .map(HivePartition::getPartitionId) .collect(toImmutableSet()); return metastore.getPartitionStatistics(table.getSchemaName(), table.getTableName(), partitionNames); }
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 List<HiveColumnHandle> getPartitionColumns(SchemaTableName tableName) { Table sourceTable = metastore.getTable(tableName.getSchemaName(), tableName.getTableName()).get(); return getPartitionKeyColumnHandles(sourceTable); }
private void alterBucketProperty(SchemaTableName schemaTableName, Optional<HiveBucketProperty> bucketProperty) { try (Transaction transaction = newTransaction()) { ConnectorSession session = newSession(); String tableOwner = session.getUser(); String schemaName = schemaTableName.getSchemaName(); String tableName = schemaTableName.getTableName(); Optional<Table> table = transaction.getMetastore(schemaName).getTable(schemaName, tableName); Table.Builder tableBuilder = Table.builder(table.get()); tableBuilder.getStorageBuilder().setBucketProperty(bucketProperty); PrincipalPrivileges principalPrivileges = testingPrincipalPrivilege(tableOwner); // hack: replaceView can be used as replaceTable despite its name transaction.getMetastore(schemaName).replaceView(schemaName, tableName, tableBuilder.build(), principalPrivileges); transaction.commit(); } }
@GuardedBy("this") private Optional<List<String>> doGetPartitionNames(String databaseName, String tableName, Optional<List<String>> parts) checkHoldsLock(); checkReadable(); Optional<Table> table = getTable(databaseName, tableName); if (!table.isPresent()) { return Optional.empty(); TableSource tableSource = getTableSource(databaseName, tableName); switch (tableSource) { case CREATED_IN_THIS_TRANSACTION: if (partitionAction.getType() == ActionType.ADD) { List<String> values = partitionAction.getData().getPartition().getValues(); if (!parts.isPresent() || partitionValuesMatch(values, parts.get())) { resultBuilder.add(makePartName(columnNames, values));
public static List<String> listAllDataPaths(SemiTransactionalHiveMetastore metastore, String schemaName, String tableName) { ImmutableList.Builder<String> locations = ImmutableList.builder(); Table table = metastore.getTable(schemaName, tableName).get(); if (table.getStorage().getLocation() != null) { // For partitioned table, there should be nothing directly under this directory. // But including this location in the set makes the directory content assert more // extensive, which is desirable. locations.add(table.getStorage().getLocation()); } Optional<List<String>> partitionNames = metastore.getPartitionNames(schemaName, tableName); if (partitionNames.isPresent()) { metastore.getPartitionsByNames(schemaName, tableName, partitionNames.get()).values().stream() .map(Optional::get) .map(partition -> partition.getStorage().getLocation()) .filter(location -> !location.startsWith(table.getStorage().getLocation())) .forEach(locations::add); } return locations.build(); }
partitionUpdates = PartitionUpdate.mergePartitionUpdates(partitionUpdates); Optional<Table> table = metastore.getTable(handle.getSchemaName(), handle.getTableName()); if (!table.isPresent()) { throw new TableNotFoundException(new SchemaTableName(handle.getSchemaName(), handle.getTableName())); if (partitionUpdate.getName().isEmpty()) { metastore.finishInsertIntoExistingTable( session, handle.getSchemaName(), metastore.finishInsertIntoExistingPartition( session, handle.getSchemaName(), metastore.dropPartition(session, handle.getSchemaName(), handle.getTableName(), partition.getValues()); metastore.addPartition( session, handle.getSchemaName(),
public synchronized void truncateUnpartitionedTable(ConnectorSession session, String databaseName, String tableName) { checkReadable(); Optional<Table> table = getTable(databaseName, tableName); SchemaTableName schemaTableName = new SchemaTableName(databaseName, tableName); if (!table.isPresent()) { throw new TableNotFoundException(schemaTableName); } if (!table.get().getTableType().equals(MANAGED_TABLE.toString())) { throw new PrestoException(NOT_SUPPORTED, "Cannot delete from non-managed Hive table"); } if (!table.get().getPartitionColumns().isEmpty()) { throw new IllegalArgumentException("Table is partitioned"); } Path path = new Path(table.get().getStorage().getLocation()); HdfsContext context = new HdfsContext(session, databaseName, tableName); setExclusive((delegate, hdfsEnvironment) -> { RecursiveDeleteResult recursiveDeleteResult = recursiveDeleteFiles(hdfsEnvironment, context, path, ImmutableList.of(""), false); if (!recursiveDeleteResult.getNotDeletedEligibleItems().isEmpty()) { throw new PrestoException(HIVE_FILESYSTEM_ERROR, format( "Error deleting from unpartitioned table %s. These items can not be deleted: %s", schemaTableName, recursiveDeleteResult.getNotDeletedEligibleItems())); } }); }
Table table = createSimpleTable(schemaTableName, columns, session, targetPath, "q1"); transaction.getMetastore(schemaName) .createTable(session, table, privileges, Optional.empty(), false, EMPTY_TABLE_STATISTICS); Optional<Table> tableHandle = transaction.getMetastore(schemaName).getTable(schemaName, tableName); assertTrue(tableHandle.isPresent()); transaction.commit(); Table table = createSimpleTable(schemaTableName, columns, session, targetPath.suffix("_2"), "q2"); transaction.getMetastore(schemaName) .createTable(session, table, privileges, Optional.empty(), false, EMPTY_TABLE_STATISTICS); transaction.commit(); fail("Expected exception"); Table table = createSimpleTable(schemaTableName, columns, session, targetPath.suffix("_3"), "q3"); transaction.getMetastore(schemaName) .createTable(session, table, privileges, Optional.empty(), true, EMPTY_TABLE_STATISTICS); transaction.commit(); Table table = createSimpleTable(schemaTableName, columns, session, targetPath.suffix("_4"), "q4"); transaction.getMetastore(schemaName) .createTable(session, table, privileges, Optional.empty(), true, EMPTY_TABLE_STATISTICS); transaction.commit(); fail("Expected exception");
public synchronized Map<String, PartitionStatistics> getPartitionStatistics(String databaseName, String tableName, Set<String> partitionNames) checkReadable(); Optional<Table> table = getTable(databaseName, tableName); if (!table.isPresent()) { return ImmutableMap.of(); TableSource tableSource = getTableSource(databaseName, tableName); Map<List<String>, Action<PartitionAndMore>> partitionActionsOfTable = partitionActions.computeIfAbsent(new SchemaTableName(databaseName, tableName), k -> new HashMap<>()); ImmutableSet.Builder<String> partitionNamesToQuery = ImmutableSet.builder();
@Override public OptionalLong metadataDelete(ConnectorSession session, ConnectorTableHandle tableHandle, ConnectorTableLayoutHandle tableLayoutHandle) { HiveTableHandle handle = (HiveTableHandle) tableHandle; HiveTableLayoutHandle layoutHandle = (HiveTableLayoutHandle) tableLayoutHandle; Optional<Table> table = metastore.getTable(handle.getSchemaName(), handle.getTableName()); if (!table.isPresent()) { throw new TableNotFoundException(handle.getSchemaTableName()); } if (table.get().getPartitionColumns().isEmpty()) { metastore.truncateUnpartitionedTable(session, handle.getSchemaName(), handle.getTableName()); } else { for (HivePartition hivePartition : getOrComputePartitions(layoutHandle, session, tableHandle)) { metastore.dropPartition(session, handle.getSchemaName(), handle.getTableName(), toPartitionValues(hivePartition.getPartitionId())); } } // it is too expensive to determine the exact number of deleted rows return OptionalLong.empty(); }
Optional<Table> table = metastore.getTable(tableName.getSchemaName(), tableName.getTableName()); if (!table.isPresent()) { throw new TableNotFoundException(tableName); handles, session.getQueryId(), metastore.generatePageSinkMetadata(tableName), locationHandle, table.get().getStorage().getBucketProperty(), metastore.declareIntentionToWrite(session, writeInfo.getWriteMode(), writeInfo.getWritePath(), result.getFilePrefix(), tableName); return result;
setShared(); SchemaTableName schemaTableName = new SchemaTableName(databaseName, tableName); Action<TableAndMore> oldTableAction = tableActions.get(schemaTableName); Table table = delegate.getTable(databaseName, tableName) .orElseThrow(() -> new TableNotFoundException(schemaTableName)); PartitionStatistics currentStatistics = getTableStatistics(databaseName, tableName); HdfsContext context = new HdfsContext(session, databaseName, tableName); tableActions.put(
@Override public void dropView(ConnectorSession session, SchemaTableName viewName) { ConnectorViewDefinition view = getViews(session, viewName.toSchemaTablePrefix()).get(viewName); if (view == null) { throw new ViewNotFoundException(viewName); } try { metastore.dropTable(session, viewName.getSchemaName(), viewName.getTableName()); } catch (TableNotFoundException e) { throw new ViewNotFoundException(e.getTableName()); } }
PrincipalPrivileges principalPrivileges = buildInitialPrivilegeSet(table.getOwner()); HiveBasicStatistics basicStatistics = table.getPartitionColumns().isEmpty() ? createZeroStatistics() : createEmptyStatistics(); metastore.createTable( session, table,
metastore.createTable(session, table, principalPrivileges, Optional.of(writeInfo.getWritePath()), false, tableStatistics); Partition partition = buildPartitionObject(session, table, update); PartitionStatistics partitionStatistics = createPartitionStatistics(session, update.getStatistics(), partition.getValues(), columnTypes, partitionComputedStatistics); metastore.addPartition( session, handle.getSchemaName(),
private static HiveBasicStatistics getBasicStatisticsForTable(Transaction transaction, SchemaTableName table) { return transaction .getMetastore(table.getSchemaName()) .getTableStatistics(table.getSchemaName(), table.getTableName()) .getBasicStatistics(); }
private static HiveBasicStatistics getBasicStatisticsForPartition(Transaction transaction, SchemaTableName table, String partitionName) { return transaction .getMetastore(table.getSchemaName()) .getPartitionStatistics(table.getSchemaName(), table.getTableName(), ImmutableSet.of(partitionName)) .get(partitionName) .getBasicStatistics(); }