use of java.util.Optional in project presto by prestodb.
the class HivePartitionManager method getPartitions.
public HivePartitionResult getPartitions(SemiTransactionalHiveMetastore metastore, ConnectorTableHandle tableHandle, Constraint<ColumnHandle> constraint) {
HiveTableHandle hiveTableHandle = (HiveTableHandle) tableHandle;
TupleDomain<ColumnHandle> effectivePredicate = constraint.getSummary();
SchemaTableName tableName = hiveTableHandle.getSchemaTableName();
Table table = getTable(metastore, tableName);
Optional<HiveBucketHandle> hiveBucketHandle = getHiveBucketHandle(connectorId, table);
List<HiveColumnHandle> partitionColumns = getPartitionKeyColumnHandles(connectorId, table);
List<HiveBucket> buckets = getHiveBucketNumbers(table, effectivePredicate);
TupleDomain<HiveColumnHandle> compactEffectivePredicate = toCompactTupleDomain(effectivePredicate, domainCompactionThreshold);
if (effectivePredicate.isNone()) {
return new HivePartitionResult(partitionColumns, ImmutableList.of(), TupleDomain.none(), TupleDomain.none(), hiveBucketHandle);
}
if (partitionColumns.isEmpty()) {
return new HivePartitionResult(partitionColumns, ImmutableList.of(new HivePartition(tableName, compactEffectivePredicate, buckets)), effectivePredicate, TupleDomain.none(), hiveBucketHandle);
}
List<Type> partitionTypes = partitionColumns.stream().map(column -> typeManager.getType(column.getTypeSignature())).collect(toList());
List<String> partitionNames = getFilteredPartitionNames(metastore, tableName, partitionColumns, effectivePredicate);
// do a final pass to filter based on fields that could not be used to filter the partitions
int partitionCount = 0;
ImmutableList.Builder<HivePartition> partitions = ImmutableList.builder();
for (String partitionName : partitionNames) {
Optional<Map<ColumnHandle, NullableValue>> values = parseValuesAndFilterPartition(partitionName, partitionColumns, partitionTypes, constraint);
if (values.isPresent()) {
if (partitionCount == maxPartitions) {
throw new PrestoException(HIVE_EXCEEDED_PARTITION_LIMIT, format("Query over table '%s' can potentially read more than %s partitions", hiveTableHandle.getSchemaTableName(), maxPartitions));
}
partitionCount++;
partitions.add(new HivePartition(tableName, compactEffectivePredicate, partitionName, values.get(), buckets));
}
}
// All partition key domains will be fully evaluated, so we don't need to include those
TupleDomain<ColumnHandle> remainingTupleDomain = TupleDomain.withColumnDomains(Maps.filterKeys(effectivePredicate.getDomains().get(), not(Predicates.in(partitionColumns))));
TupleDomain<ColumnHandle> enforcedTupleDomain = TupleDomain.withColumnDomains(Maps.filterKeys(effectivePredicate.getDomains().get(), Predicates.in(partitionColumns)));
return new HivePartitionResult(partitionColumns, partitions.build(), remainingTupleDomain, enforcedTupleDomain, hiveBucketHandle);
}
use of java.util.Optional in project presto by prestodb.
the class HivePartitionManager method toCompactTupleDomain.
private static TupleDomain<HiveColumnHandle> toCompactTupleDomain(TupleDomain<ColumnHandle> effectivePredicate, int threshold) {
checkArgument(effectivePredicate.getDomains().isPresent());
ImmutableMap.Builder<HiveColumnHandle, Domain> builder = ImmutableMap.builder();
for (Map.Entry<ColumnHandle, Domain> entry : effectivePredicate.getDomains().get().entrySet()) {
HiveColumnHandle hiveColumnHandle = (HiveColumnHandle) entry.getKey();
ValueSet values = entry.getValue().getValues();
ValueSet compactValueSet = values.getValuesProcessor().<Optional<ValueSet>>transform(ranges -> ranges.getRangeCount() > threshold ? Optional.of(ValueSet.ofRanges(ranges.getSpan())) : Optional.empty(), discreteValues -> discreteValues.getValues().size() > threshold ? Optional.of(ValueSet.all(values.getType())) : Optional.empty(), allOrNone -> Optional.empty()).orElse(values);
builder.put(hiveColumnHandle, Domain.create(compactValueSet, entry.getValue().isNullAllowed()));
}
return TupleDomain.withColumnDomains(builder.build());
}
use of java.util.Optional in project presto by prestodb.
the class FileHiveMetastore method getPartitionNames.
@Override
public synchronized Optional<List<String>> getPartitionNames(String databaseName, String tableName) {
requireNonNull(databaseName, "databaseName is null");
requireNonNull(tableName, "tableName is null");
Optional<Table> tableReference = getTable(databaseName, tableName);
if (!tableReference.isPresent()) {
return Optional.empty();
}
Table table = tableReference.get();
Path tableMetadataDirectory = getTableMetadataDirectory(table);
List<ArrayDeque<String>> partitions = listPartitions(tableMetadataDirectory, table.getPartitionColumns());
List<String> partitionNames = partitions.stream().map(partitionValues -> makePartName(table.getPartitionColumns(), ImmutableList.copyOf(partitionValues))).collect(toList());
return Optional.of(ImmutableList.copyOf(partitionNames));
}
use of java.util.Optional in project presto by prestodb.
the class InMemoryHiveMetastore method listAllDataPaths.
private static List<String> listAllDataPaths(HiveMetastore metastore, String schemaName, String tableName) {
ImmutableList.Builder<String> locations = ImmutableList.builder();
Table table = metastore.getTable(schemaName, tableName).get();
if (table.getSd().getLocation() != null) {
// For unpartitioned 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.getSd().getLocation());
}
Optional<List<String>> partitionNames = metastore.getPartitionNames(schemaName, tableName);
if (partitionNames.isPresent()) {
metastore.getPartitionsByNames(schemaName, tableName, partitionNames.get()).stream().map(partition -> partition.getSd().getLocation()).filter(location -> !location.startsWith(table.getSd().getLocation())).forEach(locations::add);
}
return locations.build();
}
use of java.util.Optional in project presto by prestodb.
the class JmxMetadata method getJmxTableHandle.
private JmxTableHandle getJmxTableHandle(SchemaTableName tableName) {
try {
String canonicalName = new ObjectName(tableName.getTableName()).getCanonicalName();
Optional<ObjectName> objectName = mbeanServer.queryNames(WILDCARD, null).stream().filter(name -> canonicalName.equalsIgnoreCase(name.getCanonicalName())).findFirst();
if (!objectName.isPresent()) {
return null;
}
MBeanInfo mbeanInfo = mbeanServer.getMBeanInfo(objectName.get());
ImmutableList.Builder<JmxColumnHandle> columns = ImmutableList.builder();
columns.add(new JmxColumnHandle(NODE_COLUMN_NAME, createUnboundedVarcharType()));
// Since this method is being called on all nodes in the cluster, we must ensure (by sorting)
// that attributes are in the same order on all of them.
Arrays.stream(mbeanInfo.getAttributes()).filter(MBeanAttributeInfo::isReadable).map(attribute -> new JmxColumnHandle(attribute.getName(), getColumnType(attribute))).sorted((column1, column2) -> column1.getColumnName().compareTo(column2.getColumnName())).forEach(columns::add);
return new JmxTableHandle(objectName.get().toString(), columns.build(), true);
} catch (JMException e) {
return null;
}
}
Aggregations