Search in sources :

Example 21 with VerifyException

use of com.google.common.base.VerifyException in project presto by prestodb.

the class MetastoreHiveStatisticsProvider method calculateNullsFraction.

@VisibleForTesting
static Estimate calculateNullsFraction(String column, Collection<PartitionStatistics> partitionStatistics) {
    List<PartitionStatistics> statisticsWithKnownRowCountAndNullsCount = partitionStatistics.stream().filter(statistics -> {
        if (!statistics.getBasicStatistics().getRowCount().isPresent()) {
            return false;
        }
        HiveColumnStatistics columnStatistics = statistics.getColumnStatistics().get(column);
        if (columnStatistics == null) {
            return false;
        }
        return columnStatistics.getNullsCount().isPresent();
    }).collect(toImmutableList());
    if (statisticsWithKnownRowCountAndNullsCount.isEmpty()) {
        return Estimate.unknown();
    }
    long totalNullsCount = 0;
    long totalRowCount = 0;
    for (PartitionStatistics statistics : statisticsWithKnownRowCountAndNullsCount) {
        long rowCount = statistics.getBasicStatistics().getRowCount().orElseThrow(() -> new VerifyException("rowCount is not present"));
        verify(rowCount >= 0, "rowCount must be greater than or equal to zero");
        HiveColumnStatistics columnStatistics = statistics.getColumnStatistics().get(column);
        verify(columnStatistics != null, "columnStatistics is null");
        long nullsCount = columnStatistics.getNullsCount().orElseThrow(() -> new VerifyException("nullsCount is not present"));
        verify(nullsCount >= 0, "nullsCount must be greater than or equal to zero");
        verify(nullsCount <= rowCount, "nullsCount must be less than or equal to rowCount. nullsCount: %s. rowCount: %s.", nullsCount, rowCount);
        totalNullsCount += nullsCount;
        totalRowCount += rowCount;
    }
    if (totalRowCount == 0) {
        return Estimate.zero();
    }
    verify(totalNullsCount <= totalRowCount, "totalNullsCount must be less than or equal to totalRowCount. totalNullsCount: %s. totalRowCount: %s.", totalNullsCount, totalRowCount);
    return Estimate.of(((double) totalNullsCount) / totalRowCount);
}
Also used : ColumnStatistics(com.facebook.presto.spi.statistics.ColumnStatistics) Collections.unmodifiableList(java.util.Collections.unmodifiableList) MetastoreContext(com.facebook.presto.hive.metastore.MetastoreContext) TableStatistics(com.facebook.presto.spi.statistics.TableStatistics) HiveSessionProperties.isStatisticsEnabled(com.facebook.presto.hive.HiveSessionProperties.isStatisticsEnabled) BigDecimal(java.math.BigDecimal) Preconditions.checkArgument(com.google.common.base.Preconditions.checkArgument) SchemaTableName(com.facebook.presto.spi.SchemaTableName) Maps.immutableEntry(com.google.common.collect.Maps.immutableEntry) IntegerStatistics(com.facebook.presto.hive.metastore.IntegerStatistics) Map(java.util.Map) Varchars.isVarcharType(com.facebook.presto.common.type.Varchars.isVarcharType) HiveBasicStatistics(com.facebook.presto.hive.HiveBasicStatistics) DecimalStatistics(com.facebook.presto.hive.metastore.DecimalStatistics) Double.parseDouble(java.lang.Double.parseDouble) NullableValue(com.facebook.presto.common.predicate.NullableValue) ImmutableMap(com.google.common.collect.ImmutableMap) DOUBLE(com.facebook.presto.common.type.DoubleType.DOUBLE) Collection(java.util.Collection) ImmutableList.toImmutableList(com.google.common.collect.ImmutableList.toImmutableList) Set(java.util.Set) SemiTransactionalHiveMetastore(com.facebook.presto.hive.metastore.SemiTransactionalHiveMetastore) Decimals.isLongDecimal(com.facebook.presto.common.type.Decimals.isLongDecimal) String.format(java.lang.String.format) ConnectorSession(com.facebook.presto.spi.ConnectorSession) Objects(java.util.Objects) DateStatistics(com.facebook.presto.hive.metastore.DateStatistics) List(java.util.List) Decimals.isShortDecimal(com.facebook.presto.common.type.Decimals.isShortDecimal) HiveSessionProperties.isIgnoreCorruptedStatistics(com.facebook.presto.hive.HiveSessionProperties.isIgnoreCorruptedStatistics) INTEGER(com.facebook.presto.common.type.IntegerType.INTEGER) LocalDate(java.time.LocalDate) MetastoreUtil.getMetastoreHeaders(com.facebook.presto.hive.metastore.MetastoreUtil.getMetastoreHeaders) Optional(java.util.Optional) HiveColumnHandle(com.facebook.presto.hive.HiveColumnHandle) HashFunction(com.google.common.hash.HashFunction) Logger(com.facebook.airlift.log.Logger) DecimalType(com.facebook.presto.common.type.DecimalType) Slice(io.airlift.slice.Slice) Chars.isCharType(com.facebook.presto.common.type.Chars.isCharType) TINYINT(com.facebook.presto.common.type.TinyintType.TINYINT) OptionalDouble(java.util.OptionalDouble) Shorts(com.google.common.primitives.Shorts) HiveColumnStatistics(com.facebook.presto.hive.metastore.HiveColumnStatistics) PrestoException(com.facebook.presto.spi.PrestoException) Float.intBitsToFloat(java.lang.Float.intBitsToFloat) DATE(com.facebook.presto.common.type.DateType.DATE) REAL(com.facebook.presto.common.type.RealType.REAL) ArrayList(java.util.ArrayList) UNPARTITIONED_ID(com.facebook.presto.hive.HivePartition.UNPARTITIONED_ID) DoubleRange(com.facebook.presto.spi.statistics.DoubleRange) OptionalLong(java.util.OptionalLong) Verify.verify(com.google.common.base.Verify.verify) Objects.requireNonNull(java.util.Objects.requireNonNull) ImmutableSet.toImmutableSet(com.google.common.collect.ImmutableSet.toImmutableSet) Double.isFinite(java.lang.Double.isFinite) HIVE_CORRUPTED_COLUMN_STATISTICS(com.facebook.presto.hive.HiveErrorCode.HIVE_CORRUPTED_COLUMN_STATISTICS) Type(com.facebook.presto.common.type.Type) VerifyException(com.google.common.base.VerifyException) BIGINT(com.facebook.presto.common.type.BigintType.BIGINT) DoubleStatistics(com.facebook.presto.hive.metastore.DoubleStatistics) SignedBytes(com.google.common.primitives.SignedBytes) Decimals(com.facebook.presto.common.type.Decimals) Hashing.murmur3_128(com.google.common.hash.Hashing.murmur3_128) Ints(com.google.common.primitives.Ints) HiveSessionProperties.getPartitionStatisticsSampleSize(com.facebook.presto.hive.HiveSessionProperties.getPartitionStatisticsSampleSize) HivePartition(com.facebook.presto.hive.HivePartition) SMALLINT(com.facebook.presto.common.type.SmallintType.SMALLINT) ColumnHandle(com.facebook.presto.spi.ColumnHandle) Double.isNaN(java.lang.Double.isNaN) PartitionStatistics(com.facebook.presto.hive.metastore.PartitionStatistics) Estimate(com.facebook.presto.spi.statistics.Estimate) MetastoreUtil.isUserDefinedTypeEncodingEnabled(com.facebook.presto.hive.metastore.MetastoreUtil.isUserDefinedTypeEncodingEnabled) VisibleForTesting(com.google.common.annotations.VisibleForTesting) Comparator(java.util.Comparator) PartitionStatistics(com.facebook.presto.hive.metastore.PartitionStatistics) VerifyException(com.google.common.base.VerifyException) HiveColumnStatistics(com.facebook.presto.hive.metastore.HiveColumnStatistics) VisibleForTesting(com.google.common.annotations.VisibleForTesting)

Example 22 with VerifyException

use of com.google.common.base.VerifyException in project presto by prestodb.

the class Driver method processNewSources.

@GuardedBy("exclusiveLock")
private void processNewSources() {
    checkLockHeld("Lock must be held to call processNewSources");
    // only update if the driver is still alive
    if (state.get() != State.ALIVE) {
        return;
    }
    TaskSource sourceUpdate = pendingTaskSourceUpdates.getAndSet(null);
    if (sourceUpdate == null) {
        return;
    }
    // merge the current source and the specified source update
    TaskSource newSource = currentTaskSource.update(sourceUpdate);
    // if the update contains no new data, just return
    if (newSource == currentTaskSource) {
        return;
    }
    // determine new splits to add
    Set<ScheduledSplit> newSplits = Sets.difference(newSource.getSplits(), currentTaskSource.getSplits());
    // add new splits
    SourceOperator sourceOperator = this.sourceOperator.orElseThrow(VerifyException::new);
    for (ScheduledSplit newSplit : newSplits) {
        Split split = newSplit.getSplit();
        if (fragmentResultCacheContext.get().isPresent() && !(split.getConnectorSplit() instanceof RemoteSplit)) {
            checkState(!this.cachedResult.get().isPresent());
            this.fragmentResultCacheContext.set(this.fragmentResultCacheContext.get().map(context -> context.updateRuntimeInformation(split.getConnectorSplit())));
            Optional<Iterator<Page>> pages = fragmentResultCacheContext.get().get().getFragmentResultCacheManager().get(fragmentResultCacheContext.get().get().getHashedCanonicalPlanFragment(), split);
            sourceOperator.getOperatorContext().getRuntimeStats().addMetricValue(pages.isPresent() ? RuntimeMetricName.FRAGMENT_RESULT_CACHE_HIT : RuntimeMetricName.FRAGMENT_RESULT_CACHE_MISS, 1);
            this.cachedResult.set(pages);
            this.split.set(split);
        }
        Supplier<Optional<UpdatablePageSource>> pageSource = sourceOperator.addSplit(split);
        deleteOperator.ifPresent(deleteOperator -> deleteOperator.setPageSource(pageSource));
    }
    // set no more splits
    if (newSource.isNoMoreSplits()) {
        sourceOperator.noMoreSplits();
    }
    currentTaskSource = newSource;
}
Also used : UpdatablePageSource(com.facebook.presto.spi.UpdatablePageSource) PlanNodeId(com.facebook.presto.spi.plan.PlanNodeId) Page(com.facebook.presto.common.Page) Logger(com.facebook.airlift.log.Logger) ListenableFuture(com.google.common.util.concurrent.ListenableFuture) NOT_BLOCKED(com.facebook.presto.operator.Operator.NOT_BLOCKED) NO_PREFERENCE(com.facebook.presto.spi.schedule.NodeSelectionStrategy.NO_PREFERENCE) GENERIC_INTERNAL_ERROR(com.facebook.presto.spi.StandardErrorCode.GENERIC_INTERNAL_ERROR) Throwables.throwIfUnchecked(com.google.common.base.Throwables.throwIfUnchecked) HashMap(java.util.HashMap) PrestoException(com.facebook.presto.spi.PrestoException) SettableFuture(com.google.common.util.concurrent.SettableFuture) AtomicReference(java.util.concurrent.atomic.AtomicReference) Supplier(java.util.function.Supplier) Duration(io.airlift.units.Duration) ArrayList(java.util.ArrayList) Preconditions.checkArgument(com.google.common.base.Preconditions.checkArgument) ImmutableList(com.google.common.collect.ImmutableList) Map(java.util.Map) Objects.requireNonNull(java.util.Objects.requireNonNull) RemoteSplit(com.facebook.presto.split.RemoteSplit) ScheduledSplit(com.facebook.presto.execution.ScheduledSplit) VerifyException(com.google.common.base.VerifyException) ImmutableSet(com.google.common.collect.ImmutableSet) Iterator(java.util.Iterator) ReentrantLock(java.util.concurrent.locks.ReentrantLock) RuntimeMetricName(com.facebook.presto.common.RuntimeMetricName) TaskSource(com.facebook.presto.execution.TaskSource) Set(java.util.Set) GuardedBy(javax.annotation.concurrent.GuardedBy) Sets(com.google.common.collect.Sets) Preconditions.checkState(com.google.common.base.Preconditions.checkState) MoreExecutors.directExecutor(com.google.common.util.concurrent.MoreExecutors.directExecutor) TimeUnit(java.util.concurrent.TimeUnit) FragmentResultCacheContext(com.facebook.presto.execution.FragmentResultCacheContext) List(java.util.List) MoreUninterruptibles.tryLockUninterruptibly(com.facebook.presto.util.MoreUninterruptibles.tryLockUninterruptibly) Closeable(java.io.Closeable) Split(com.facebook.presto.metadata.Split) Optional(java.util.Optional) VisibleForTesting(com.google.common.annotations.VisibleForTesting) TRUE(java.lang.Boolean.TRUE) SpillingUtils.checkSpillSucceeded(com.facebook.presto.operator.SpillingUtils.checkSpillSucceeded) ScheduledSplit(com.facebook.presto.execution.ScheduledSplit) Optional(java.util.Optional) VerifyException(com.google.common.base.VerifyException) RemoteSplit(com.facebook.presto.split.RemoteSplit) Iterator(java.util.Iterator) RemoteSplit(com.facebook.presto.split.RemoteSplit) ScheduledSplit(com.facebook.presto.execution.ScheduledSplit) Split(com.facebook.presto.metadata.Split) TaskSource(com.facebook.presto.execution.TaskSource) GuardedBy(javax.annotation.concurrent.GuardedBy)

Example 23 with VerifyException

use of com.google.common.base.VerifyException in project presto by prestodb.

the class ResultSetValues method extractValues.

int extractValues(ResultSet resultSet, Set<Integer> uuidColumns, Set<Integer> hexColumns) throws SQLException {
    checkArgument(resultSet != null, "resultSet is null");
    int completedBytes = 0;
    for (int i = 0; i < types.size(); i++) {
        Class<?> javaType = types.get(i).getJavaType();
        if (javaType == boolean.class) {
            booleans[i] = resultSet.getBoolean(i + 1);
            nulls[i] = resultSet.wasNull();
            if (!nulls[i]) {
                completedBytes += SIZE_OF_BYTE;
            }
        } else if (javaType == long.class) {
            longs[i] = resultSet.getLong(i + 1);
            nulls[i] = resultSet.wasNull();
            if (!nulls[i]) {
                completedBytes += SIZE_OF_LONG;
            }
        } else if (javaType == double.class) {
            doubles[i] = resultSet.getDouble(i + 1);
            nulls[i] = resultSet.wasNull();
            if (!nulls[i]) {
                completedBytes += SIZE_OF_DOUBLE;
            }
        } else if (javaType == Slice.class) {
            if (uuidColumns.contains(i)) {
                byte[] bytes = resultSet.getBytes(i + 1);
                nulls[i] = resultSet.wasNull();
                strings[i] = nulls[i] ? null : uuidFromBytes(bytes).toString().toLowerCase(ENGLISH);
            } else if (hexColumns.contains(i)) {
                long value = resultSet.getLong(i + 1);
                nulls[i] = resultSet.wasNull();
                strings[i] = nulls[i] ? null : format("%016x", value);
            } else {
                String value = resultSet.getString(i + 1);
                nulls[i] = resultSet.wasNull();
                strings[i] = nulls[i] ? null : value;
            }
            if (!nulls[i]) {
                completedBytes += strings[i].length();
            }
        } else {
            throw new VerifyException("Unknown Java type: " + javaType);
        }
    }
    return completedBytes;
}
Also used : VerifyException(com.google.common.base.VerifyException) Slice(io.airlift.slice.Slice)

Example 24 with VerifyException

use of com.google.common.base.VerifyException in project presto by prestodb.

the class BucketBalancer method computeAssignmentChanges.

private static Multimap<String, BucketAssignment> computeAssignmentChanges(ClusterState clusterState) {
    Multimap<String, BucketAssignment> sourceToAllocationChanges = HashMultimap.create();
    Map<String, Long> allocationBytes = new HashMap<>(clusterState.getAssignedBytes());
    Set<String> activeNodes = clusterState.getActiveNodes();
    for (Distribution distribution : clusterState.getDistributionAssignments().keySet()) {
        // number of buckets in this distribution assigned to a node
        Multiset<String> allocationCounts = HashMultiset.create();
        Collection<BucketAssignment> distributionAssignments = clusterState.getDistributionAssignments().get(distribution);
        distributionAssignments.stream().map(BucketAssignment::getNodeIdentifier).forEach(allocationCounts::add);
        int currentMin = allocationBytes.keySet().stream().mapToInt(allocationCounts::count).min().getAsInt();
        int currentMax = allocationBytes.keySet().stream().mapToInt(allocationCounts::count).max().getAsInt();
        int numBuckets = distributionAssignments.size();
        int targetMin = (int) Math.floor((numBuckets * 1.0) / clusterState.getActiveNodes().size());
        int targetMax = (int) Math.ceil((numBuckets * 1.0) / clusterState.getActiveNodes().size());
        log.info("Distribution %s: Current bucket skew: min %s, max %s. Target bucket skew: min %s, max %s", distribution.getId(), currentMin, currentMax, targetMin, targetMax);
        for (String source : ImmutableSet.copyOf(allocationCounts)) {
            List<BucketAssignment> existingAssignments = distributionAssignments.stream().filter(assignment -> assignment.getNodeIdentifier().equals(source)).collect(toList());
            for (BucketAssignment existingAssignment : existingAssignments) {
                if (activeNodes.contains(source) && allocationCounts.count(source) <= targetMin) {
                    break;
                }
                // identify nodes with bucket counts lower than the computed target, and greedily select from this set based on projected disk utilization.
                // greediness means that this may produce decidedly non-optimal results if one looks at the global distribution of buckets->nodes.
                // also, this assumes that nodes in a cluster have identical storage capacity
                String target = activeNodes.stream().filter(candidate -> !candidate.equals(source) && allocationCounts.count(candidate) < targetMax).sorted(comparingInt(allocationCounts::count)).min(Comparator.comparingDouble(allocationBytes::get)).orElseThrow(() -> new VerifyException("unable to find target for rebalancing"));
                long bucketSize = clusterState.getDistributionBucketSize().get(distribution);
                // only move bucket if it reduces imbalance
                if (activeNodes.contains(source) && (allocationCounts.count(source) == targetMax && allocationCounts.count(target) == targetMin)) {
                    break;
                }
                allocationCounts.remove(source);
                allocationCounts.add(target);
                allocationBytes.compute(source, (k, v) -> v - bucketSize);
                allocationBytes.compute(target, (k, v) -> v + bucketSize);
                sourceToAllocationChanges.put(existingAssignment.getNodeIdentifier(), new BucketAssignment(existingAssignment.getDistributionId(), existingAssignment.getBucketNumber(), target));
            }
        }
    }
    return sourceToAllocationChanges;
}
Also used : Nested(org.weakref.jmx.Nested) Logger(com.facebook.airlift.log.Logger) NodeSupplier(com.facebook.presto.raptor.NodeSupplier) MetadataConfig(com.facebook.presto.raptor.metadata.MetadataConfig) Multiset(com.google.common.collect.Multiset) CounterStat(com.facebook.airlift.stats.CounterStat) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) HashMap(java.util.HashMap) Multimap(com.google.common.collect.Multimap) Duration(io.airlift.units.Duration) Inject(javax.inject.Inject) PreDestroy(javax.annotation.PreDestroy) Executors.newSingleThreadScheduledExecutor(java.util.concurrent.Executors.newSingleThreadScheduledExecutor) HashMultimap(com.google.common.collect.HashMultimap) Node(com.facebook.presto.spi.Node) Managed(org.weakref.jmx.Managed) Collectors.toMap(java.util.stream.Collectors.toMap) HashMultiset(com.google.common.collect.HashMultiset) Map(java.util.Map) Objects.requireNonNull(java.util.Objects.requireNonNull) ScheduledExecutorService(java.util.concurrent.ScheduledExecutorService) ShardManager(com.facebook.presto.raptor.metadata.ShardManager) ImmutableMultimap(com.google.common.collect.ImmutableMultimap) BackupService(com.facebook.presto.raptor.backup.BackupService) Collectors.toSet(java.util.stream.Collectors.toSet) VerifyException(com.google.common.base.VerifyException) Distribution(com.facebook.presto.raptor.metadata.Distribution) Comparator.comparingInt(java.util.Comparator.comparingInt) ImmutableSet(com.google.common.collect.ImmutableSet) NodeManager(com.facebook.presto.spi.NodeManager) ImmutableMap(com.google.common.collect.ImmutableMap) Collection(java.util.Collection) Set(java.util.Set) MILLISECONDS(java.util.concurrent.TimeUnit.MILLISECONDS) String.format(java.lang.String.format) Threads.daemonThreadsNamed(com.facebook.airlift.concurrent.Threads.daemonThreadsNamed) BucketNode(com.facebook.presto.raptor.metadata.BucketNode) List(java.util.List) Collectors.toList(java.util.stream.Collectors.toList) RaptorConnectorId(com.facebook.presto.raptor.RaptorConnectorId) PostConstruct(javax.annotation.PostConstruct) VisibleForTesting(com.google.common.annotations.VisibleForTesting) Comparator(java.util.Comparator) HashMap(java.util.HashMap) VerifyException(com.google.common.base.VerifyException) Distribution(com.facebook.presto.raptor.metadata.Distribution)

Example 25 with VerifyException

use of com.google.common.base.VerifyException in project presto by prestodb.

the class ColumnRangesSystemTable method pageSource.

@Override
public ConnectorPageSource pageSource(ConnectorTransactionHandle transactionHandle, ConnectorSession session, TupleDomain<Integer> constraint) {
    String metadataSqlQuery = getColumnRangesMetadataSqlQuery(sourceTable, indexedRaptorColumns);
    List<Type> columnTypes = tableMetadata.getColumns().stream().map(ColumnMetadata::getType).collect(toImmutableList());
    PageListBuilder pageListBuilder = new PageListBuilder(columnTypes);
    try (Connection connection = dbi.open().getConnection();
        Statement statement = connection.createStatement();
        ResultSet resultSet = statement.executeQuery(metadataSqlQuery)) {
        if (resultSet.next()) {
            pageListBuilder.beginRow();
            for (int i = 0; i < columnTypes.size(); ++i) {
                BlockBuilder blockBuilder = pageListBuilder.nextBlockBuilder();
                Type columnType = columnTypes.get(i);
                if (columnType.equals(BIGINT) || columnType.equals(DATE) || columnType.equals(TIMESTAMP)) {
                    long value = resultSet.getLong(i + 1);
                    if (!resultSet.wasNull()) {
                        columnType.writeLong(blockBuilder, value);
                    } else {
                        blockBuilder.appendNull();
                    }
                } else if (columnType.equals(BOOLEAN)) {
                    boolean value = resultSet.getBoolean(i + 1);
                    if (!resultSet.wasNull()) {
                        BOOLEAN.writeBoolean(blockBuilder, value);
                    } else {
                        blockBuilder.appendNull();
                    }
                } else {
                    throw new VerifyException("Unknown or unsupported column type: " + columnType);
                }
            }
        }
    } catch (SQLException | DBIException e) {
        throw metadataError(e);
    }
    return new FixedPageSource(pageListBuilder.build());
}
Also used : SQLException(java.sql.SQLException) Statement(java.sql.Statement) Connection(java.sql.Connection) FixedPageSource(com.facebook.presto.spi.FixedPageSource) Type(com.facebook.presto.common.type.Type) VerifyException(com.google.common.base.VerifyException) ResultSet(java.sql.ResultSet) DBIException(org.skife.jdbi.v2.exceptions.DBIException) BlockBuilder(com.facebook.presto.common.block.BlockBuilder)

Aggregations

VerifyException (com.google.common.base.VerifyException)25 ImmutableMap (com.google.common.collect.ImmutableMap)8 PrestoException (com.facebook.presto.spi.PrestoException)6 List (java.util.List)6 Map (java.util.Map)6 Objects.requireNonNull (java.util.Objects.requireNonNull)6 Logger (com.facebook.airlift.log.Logger)5 Type (com.facebook.presto.common.type.Type)5 VisibleForTesting (com.google.common.annotations.VisibleForTesting)5 ImmutableList (com.google.common.collect.ImmutableList)5 ImmutableList.toImmutableList (com.google.common.collect.ImmutableList.toImmutableList)5 Slice (io.airlift.slice.Slice)5 ArrayList (java.util.ArrayList)5 Optional (java.util.Optional)5 Set (java.util.Set)5 BIGINT (com.facebook.presto.common.type.BigintType.BIGINT)4 Preconditions.checkArgument (com.google.common.base.Preconditions.checkArgument)4 String.format (java.lang.String.format)4 Collection (java.util.Collection)4 FieldDefinition (com.facebook.presto.bytecode.FieldDefinition)3