Search in sources :

Example 71 with Optional

use of java.util.Optional in project presto by prestodb.

the class JmxRecordSetProvider method getLiveRow.

public List<Object> getLiveRow(JmxTableHandle tableHandle, List<? extends ColumnHandle> columns, long entryTimestamp) throws JMException {
    ImmutableMap<String, Optional<Object>> attributes = getAttributes(getColumnNames(columns), tableHandle);
    List<Object> row = new ArrayList<>();
    for (ColumnHandle column : columns) {
        JmxColumnHandle jmxColumn = (JmxColumnHandle) column;
        if (jmxColumn.getColumnName().equals(JmxMetadata.NODE_COLUMN_NAME)) {
            row.add(nodeId);
        } else if (jmxColumn.getColumnName().equals(JmxMetadata.TIMESTAMP_COLUMN_NAME)) {
            row.add(entryTimestamp);
        } else {
            Optional<Object> optionalValue = attributes.get(jmxColumn.getColumnName());
            if (optionalValue == null || !optionalValue.isPresent()) {
                row.add(null);
            } else {
                Object value = optionalValue.get();
                Class<?> javaType = jmxColumn.getColumnType().getJavaType();
                if (javaType == boolean.class) {
                    if (value instanceof Boolean) {
                        row.add(value);
                    } else {
                        // mbeans can lie about types
                        row.add(null);
                    }
                } else if (javaType == long.class) {
                    if (value instanceof Number) {
                        row.add(((Number) value).longValue());
                    } else {
                        // mbeans can lie about types
                        row.add(null);
                    }
                } else if (javaType == double.class) {
                    if (value instanceof Number) {
                        row.add(((Number) value).doubleValue());
                    } else {
                        // mbeans can lie about types
                        row.add(null);
                    }
                } else if (javaType == Slice.class) {
                    if (value.getClass().isArray()) {
                        // return a string representation of the array
                        if (value.getClass().getComponentType() == boolean.class) {
                            row.add(Arrays.toString((boolean[]) value));
                        } else if (value.getClass().getComponentType() == byte.class) {
                            row.add(Arrays.toString((byte[]) value));
                        } else if (value.getClass().getComponentType() == char.class) {
                            row.add(Arrays.toString((char[]) value));
                        } else if (value.getClass().getComponentType() == double.class) {
                            row.add(Arrays.toString((double[]) value));
                        } else if (value.getClass().getComponentType() == float.class) {
                            row.add(Arrays.toString((float[]) value));
                        } else if (value.getClass().getComponentType() == int.class) {
                            row.add(Arrays.toString((int[]) value));
                        } else if (value.getClass().getComponentType() == long.class) {
                            row.add(Arrays.toString((long[]) value));
                        } else if (value.getClass().getComponentType() == short.class) {
                            row.add(Arrays.toString((short[]) value));
                        } else {
                            row.add(Arrays.toString((Object[]) value));
                        }
                    } else {
                        row.add(value.toString());
                    }
                }
            }
        }
    }
    return row;
}
Also used : ColumnHandle(com.facebook.presto.spi.ColumnHandle) Optional(java.util.Optional) ArrayList(java.util.ArrayList)

Example 72 with Optional

use of java.util.Optional in project presto by prestodb.

the class MongoSession method guessFieldType.

private Optional<TypeSignature> guessFieldType(Object value) {
    if (value == null) {
        return Optional.empty();
    }
    TypeSignature typeSignature = null;
    if (value instanceof String) {
        typeSignature = createUnboundedVarcharType().getTypeSignature();
    } else if (value instanceof Integer || value instanceof Long) {
        typeSignature = BIGINT.getTypeSignature();
    } else if (value instanceof Boolean) {
        typeSignature = BOOLEAN.getTypeSignature();
    } else if (value instanceof Float || value instanceof Double) {
        typeSignature = DOUBLE.getTypeSignature();
    } else if (value instanceof Date) {
        typeSignature = TIMESTAMP.getTypeSignature();
    } else if (value instanceof ObjectId) {
        typeSignature = OBJECT_ID.getTypeSignature();
    } else if (value instanceof List) {
        List<Optional<TypeSignature>> subTypes = ((List<?>) value).stream().map(this::guessFieldType).collect(toList());
        if (subTypes.isEmpty() || subTypes.stream().anyMatch(t -> !t.isPresent())) {
            return Optional.empty();
        }
        Set<TypeSignature> signatures = subTypes.stream().map(t -> t.get()).collect(toSet());
        if (signatures.size() == 1) {
            typeSignature = new TypeSignature(StandardTypes.ARRAY, signatures.stream().map(s -> TypeSignatureParameter.of(s)).collect(Collectors.toList()));
        } else {
            // TODO: presto cli doesn't handle empty field name row type yet
            typeSignature = new TypeSignature(StandardTypes.ROW, IntStream.range(0, subTypes.size()).mapToObj(idx -> TypeSignatureParameter.of(new NamedTypeSignature(String.format("%s%d", implicitPrefix, idx + 1), subTypes.get(idx).get()))).collect(toList()));
        }
    } else if (value instanceof Document) {
        List<TypeSignatureParameter> parameters = new ArrayList<>();
        for (String key : ((Document) value).keySet()) {
            Optional<TypeSignature> fieldType = guessFieldType(((Document) value).get(key));
            if (!fieldType.isPresent()) {
                return Optional.empty();
            }
            parameters.add(TypeSignatureParameter.of(new NamedTypeSignature(key, fieldType.get())));
        }
        typeSignature = new TypeSignature(StandardTypes.ROW, parameters);
    }
    return Optional.ofNullable(typeSignature);
}
Also used : Document(org.bson.Document) Arrays(java.util.Arrays) LoadingCache(com.google.common.cache.LoadingCache) TypeManager(com.facebook.presto.spi.type.TypeManager) Date(java.util.Date) MongoDatabase(com.mongodb.client.MongoDatabase) BIGINT(com.facebook.presto.spi.type.BigintType.BIGINT) SchemaTableName(com.facebook.presto.spi.SchemaTableName) BOOLEAN(com.facebook.presto.spi.type.BooleanType.BOOLEAN) SchemaNotFoundException(com.facebook.presto.spi.SchemaNotFoundException) Map(java.util.Map) TypeSignatureParameter(com.facebook.presto.spi.type.TypeSignatureParameter) StandardTypes(com.facebook.presto.spi.type.StandardTypes) Collectors.toSet(java.util.stream.Collectors.toSet) ImmutableSet(com.google.common.collect.ImmutableSet) Set(java.util.Set) Collectors(java.util.stream.Collectors) Preconditions.checkState(com.google.common.base.Preconditions.checkState) CacheLoader(com.google.common.cache.CacheLoader) TupleDomain(com.facebook.presto.spi.predicate.TupleDomain) Domain(com.facebook.presto.spi.predicate.Domain) List(java.util.List) FindIterable(com.mongodb.client.FindIterable) Optional(java.util.Optional) CacheBuilder(com.google.common.cache.CacheBuilder) TypeSignature(com.facebook.presto.spi.type.TypeSignature) IntStream(java.util.stream.IntStream) DOUBLE(com.facebook.presto.spi.type.DoubleType.DOUBLE) MongoCollection(com.mongodb.client.MongoCollection) Slice(io.airlift.slice.Slice) Logger(io.airlift.log.Logger) OBJECT_ID(com.facebook.presto.mongodb.ObjectIdType.OBJECT_ID) MINUTES(java.util.concurrent.TimeUnit.MINUTES) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) ImmutableList(com.google.common.collect.ImmutableList) MongoCursor(com.mongodb.client.MongoCursor) Verify.verify(com.google.common.base.Verify.verify) Type(com.facebook.presto.spi.type.Type) UncheckedExecutionException(com.google.common.util.concurrent.UncheckedExecutionException) NamedTypeSignature(com.facebook.presto.spi.type.NamedTypeSignature) Objects.requireNonNull(java.util.Objects.requireNonNull) TIMESTAMP(com.facebook.presto.spi.type.TimestampType.TIMESTAMP) Throwables(com.google.common.base.Throwables) Range(com.facebook.presto.spi.predicate.Range) IndexOptions(com.mongodb.client.model.IndexOptions) ExecutionException(java.util.concurrent.ExecutionException) VarcharType.createUnboundedVarcharType(com.facebook.presto.spi.type.VarcharType.createUnboundedVarcharType) Collectors.toList(java.util.stream.Collectors.toList) TableNotFoundException(com.facebook.presto.spi.TableNotFoundException) ColumnHandle(com.facebook.presto.spi.ColumnHandle) MongoClient(com.mongodb.MongoClient) DeleteResult(com.mongodb.client.result.DeleteResult) ObjectId(org.bson.types.ObjectId) HOURS(java.util.concurrent.TimeUnit.HOURS) VisibleForTesting(com.google.common.annotations.VisibleForTesting) Optional(java.util.Optional) ObjectId(org.bson.types.ObjectId) NamedTypeSignature(com.facebook.presto.spi.type.NamedTypeSignature) Document(org.bson.Document) Date(java.util.Date) TypeSignature(com.facebook.presto.spi.type.TypeSignature) NamedTypeSignature(com.facebook.presto.spi.type.NamedTypeSignature) List(java.util.List) ArrayList(java.util.ArrayList) ImmutableList(com.google.common.collect.ImmutableList) Collectors.toList(java.util.stream.Collectors.toList)

Example 73 with Optional

use of java.util.Optional in project presto by prestodb.

the class RaptorMetadata method beginInsert.

@Override
public ConnectorInsertTableHandle beginInsert(ConnectorSession session, ConnectorTableHandle tableHandle) {
    RaptorTableHandle handle = (RaptorTableHandle) tableHandle;
    long tableId = handle.getTableId();
    ImmutableList.Builder<RaptorColumnHandle> columnHandles = ImmutableList.builder();
    ImmutableList.Builder<Type> columnTypes = ImmutableList.builder();
    for (TableColumn column : dao.listTableColumns(tableId)) {
        columnHandles.add(new RaptorColumnHandle(connectorId, column.getColumnName(), column.getColumnId(), column.getDataType()));
        columnTypes.add(column.getDataType());
    }
    long transactionId = shardManager.beginTransaction();
    setTransactionId(transactionId);
    Optional<String> externalBatchId = getExternalBatchId(session);
    List<RaptorColumnHandle> sortColumnHandles = getSortColumnHandles(tableId);
    List<RaptorColumnHandle> bucketColumnHandles = getBucketColumnHandles(tableId);
    Optional<RaptorColumnHandle> temporalColumnHandle = Optional.ofNullable(dao.getTemporalColumnId(tableId)).map(temporalColumnId -> getOnlyElement(columnHandles.build().stream().filter(columnHandle -> columnHandle.getColumnId() == temporalColumnId).collect(toList())));
    return new RaptorInsertTableHandle(connectorId, transactionId, tableId, columnHandles.build(), columnTypes.build(), externalBatchId, sortColumnHandles, nCopies(sortColumnHandles.size(), ASC_NULLS_FIRST), handle.getBucketCount(), bucketColumnHandles, temporalColumnHandle);
}
Also used : SHARD_UUID_COLUMN_TYPE(com.facebook.presto.raptor.RaptorColumnHandle.SHARD_UUID_COLUMN_TYPE) NOT_FOUND(com.facebook.presto.spi.StandardErrorCode.NOT_FOUND) DatabaseUtil.runIgnoringConstraintViolation(com.facebook.presto.raptor.util.DatabaseUtil.runIgnoringConstraintViolation) Map(java.util.Map) ShardManager(com.facebook.presto.raptor.metadata.ShardManager) INVALID_TABLE_PROPERTY(com.facebook.presto.spi.StandardErrorCode.INVALID_TABLE_PROPERTY) RaptorTableProperties.getSortColumns(com.facebook.presto.raptor.RaptorTableProperties.getSortColumns) ColumnInfo(com.facebook.presto.raptor.metadata.ColumnInfo) ViewResult(com.facebook.presto.raptor.metadata.ViewResult) Set(java.util.Set) ConnectorSession(com.facebook.presto.spi.ConnectorSession) ImmutableListMultimap(com.google.common.collect.ImmutableListMultimap) RaptorColumnHandle.shardUuidColumnHandle(com.facebook.presto.raptor.RaptorColumnHandle.shardUuidColumnHandle) ConnectorPartitioningHandle(com.facebook.presto.spi.connector.ConnectorPartitioningHandle) Slice(io.airlift.slice.Slice) ConnectorOutputTableHandle(com.facebook.presto.spi.ConnectorOutputTableHandle) Multimaps(com.google.common.collect.Multimaps) ArrayList(java.util.ArrayList) OptionalLong(java.util.OptionalLong) Type(com.facebook.presto.spi.type.Type) BUCKET_NUMBER_COLUMN_NAME(com.facebook.presto.raptor.RaptorColumnHandle.BUCKET_NUMBER_COLUMN_NAME) RaptorSessionProperties.getExternalBatchId(com.facebook.presto.raptor.RaptorSessionProperties.getExternalBatchId) TIMESTAMP(com.facebook.presto.spi.type.TimestampType.TIMESTAMP) DatabaseUtil.daoTransaction(com.facebook.presto.raptor.util.DatabaseUtil.daoTransaction) Nullable(javax.annotation.Nullable) RaptorTableProperties.isOrganized(com.facebook.presto.raptor.RaptorTableProperties.isOrganized) ConnectorTableMetadata(com.facebook.presto.spi.ConnectorTableMetadata) Distribution(com.facebook.presto.raptor.metadata.Distribution) BUCKETED_ON_PROPERTY(com.facebook.presto.raptor.RaptorTableProperties.BUCKETED_ON_PROPERTY) ORDERING_PROPERTY(com.facebook.presto.raptor.RaptorTableProperties.ORDERING_PROPERTY) RaptorSessionProperties.getOneSplitPerBucketThreshold(com.facebook.presto.raptor.RaptorSessionProperties.getOneSplitPerBucketThreshold) ColumnIdentity(com.facebook.presto.spi.ColumnIdentity) Constraint(com.facebook.presto.spi.Constraint) Iterables.getOnlyElement(com.google.common.collect.Iterables.getOnlyElement) RaptorTableProperties.getBucketColumns(com.facebook.presto.raptor.RaptorTableProperties.getBucketColumns) MetadataDao(com.facebook.presto.raptor.metadata.MetadataDao) JsonCodec.jsonCodec(io.airlift.json.JsonCodec.jsonCodec) ConnectorTableLayoutResult(com.facebook.presto.spi.ConnectorTableLayoutResult) RAPTOR_ERROR(com.facebook.presto.raptor.RaptorErrorCode.RAPTOR_ERROR) SchemaTablePrefix(com.facebook.presto.spi.SchemaTablePrefix) TreeMap(java.util.TreeMap) ColumnHandle(com.facebook.presto.spi.ColumnHandle) ConnectorNewTableLayout(com.facebook.presto.spi.ConnectorNewTableLayout) ConnectorViewDefinition(com.facebook.presto.spi.ConnectorViewDefinition) BUCKET_COUNT_PROPERTY(com.facebook.presto.raptor.RaptorTableProperties.BUCKET_COUNT_PROPERTY) ViewNotFoundException(com.facebook.presto.spi.ViewNotFoundException) ShardDelta(com.facebook.presto.raptor.metadata.ShardDelta) IDBI(org.skife.jdbi.v2.IDBI) RaptorBucketFunction.validateBucketType(com.facebook.presto.raptor.RaptorBucketFunction.validateBucketType) Preconditions.checkArgument(com.google.common.base.Preconditions.checkArgument) SchemaTableName(com.facebook.presto.spi.SchemaTableName) TableColumn(com.facebook.presto.raptor.metadata.TableColumn) DatabaseUtil.runTransaction(com.facebook.presto.raptor.util.DatabaseUtil.runTransaction) ImmutableSet(com.google.common.collect.ImmutableSet) RaptorTableProperties.getDistributionName(com.facebook.presto.raptor.RaptorTableProperties.getDistributionName) ImmutableMap(com.google.common.collect.ImmutableMap) DISTRIBUTION_NAME_PROPERTY(com.facebook.presto.raptor.RaptorTableProperties.DISTRIBUTION_NAME_PROPERTY) Collections.nCopies(java.util.Collections.nCopies) Collection(java.util.Collection) RaptorTableProperties.getTemporalColumn(com.facebook.presto.raptor.RaptorTableProperties.getTemporalColumn) UUID(java.util.UUID) RaptorColumnHandle.isHiddenColumn(com.facebook.presto.raptor.RaptorColumnHandle.isHiddenColumn) RaptorColumnHandle.bucketNumberColumnHandle(com.facebook.presto.raptor.RaptorColumnHandle.bucketNumberColumnHandle) String.format(java.lang.String.format) Preconditions.checkState(com.google.common.base.Preconditions.checkState) TupleDomain(com.facebook.presto.spi.predicate.TupleDomain) List(java.util.List) RaptorTableProperties.getBucketCount(com.facebook.presto.raptor.RaptorTableProperties.getBucketCount) ColumnMetadata(com.facebook.presto.spi.ColumnMetadata) NOT_SUPPORTED(com.facebook.presto.spi.StandardErrorCode.NOT_SUPPORTED) TEMPORAL_COLUMN_PROPERTY(com.facebook.presto.raptor.RaptorTableProperties.TEMPORAL_COLUMN_PROPERTY) Optional(java.util.Optional) INTEGER(com.facebook.presto.spi.type.IntegerType.INTEGER) SortedMap(java.util.SortedMap) JsonCodec(io.airlift.json.JsonCodec) ConnectorMetadata(com.facebook.presto.spi.connector.ConnectorMetadata) ORGANIZED_PROPERTY(com.facebook.presto.raptor.RaptorTableProperties.ORGANIZED_PROPERTY) Logger(io.airlift.log.Logger) ConnectorTableLayoutHandle(com.facebook.presto.spi.ConnectorTableLayoutHandle) ConnectorTableHandle(com.facebook.presto.spi.ConnectorTableHandle) PrestoException(com.facebook.presto.spi.PrestoException) OptionalInt(java.util.OptionalInt) AtomicReference(java.util.concurrent.atomic.AtomicReference) Collectors.toCollection(java.util.stream.Collectors.toCollection) ImmutableList(com.google.common.collect.ImmutableList) ALREADY_EXISTS(com.facebook.presto.spi.StandardErrorCode.ALREADY_EXISTS) ASC_NULLS_FIRST(com.facebook.presto.spi.block.SortOrder.ASC_NULLS_FIRST) SHARD_UUID_COLUMN_NAME(com.facebook.presto.raptor.RaptorColumnHandle.SHARD_UUID_COLUMN_NAME) Objects.requireNonNull(java.util.Objects.requireNonNull) DatabaseUtil.onDemandDao(com.facebook.presto.raptor.util.DatabaseUtil.onDemandDao) ConnectorNodePartitioning(com.facebook.presto.spi.ConnectorNodePartitioning) Table(com.facebook.presto.raptor.metadata.Table) ConnectorInsertTableHandle(com.facebook.presto.spi.ConnectorInsertTableHandle) ConnectorOutputMetadata(com.facebook.presto.spi.connector.ConnectorOutputMetadata) ConnectorTableLayout(com.facebook.presto.spi.ConnectorTableLayout) Maps(com.google.common.collect.Maps) ShardInfo(com.facebook.presto.raptor.metadata.ShardInfo) LongConsumer(java.util.function.LongConsumer) Collectors.toList(java.util.stream.Collectors.toList) TableNotFoundException(com.facebook.presto.spi.TableNotFoundException) DATE(com.facebook.presto.spi.type.DateType.DATE) RaptorColumnHandle.shardRowIdHandle(com.facebook.presto.raptor.RaptorColumnHandle.shardRowIdHandle) ImmutableList(com.google.common.collect.ImmutableList) TableColumn(com.facebook.presto.raptor.metadata.TableColumn) Type(com.facebook.presto.spi.type.Type) RaptorBucketFunction.validateBucketType(com.facebook.presto.raptor.RaptorBucketFunction.validateBucketType)

Example 74 with Optional

use of java.util.Optional in project presto by prestodb.

the class ConnectorMetadata method getInsertLayout.

/**
     * Get the physical layout for a inserting into an existing table.
     */
default default Optional<ConnectorNewTableLayout> getInsertLayout(ConnectorSession session, ConnectorTableHandle tableHandle) {
    List<ConnectorTableLayout> layouts = getTableLayouts(session, tableHandle, new Constraint<>(TupleDomain.all(), map -> true), Optional.empty()).stream().map(ConnectorTableLayoutResult::getTableLayout).filter(layout -> layout.getNodePartitioning().isPresent()).collect(toList());
    if (layouts.isEmpty()) {
        return Optional.empty();
    }
    if (layouts.size() > 1) {
        throw new PrestoException(NOT_SUPPORTED, "Tables with multiple layouts can not be written");
    }
    ConnectorTableLayout layout = layouts.get(0);
    ConnectorPartitioningHandle partitioningHandle = layout.getNodePartitioning().get().getPartitioningHandle();
    Map<ColumnHandle, String> columnNamesByHandle = getColumnHandles(session, tableHandle).entrySet().stream().collect(Collectors.toMap(Map.Entry::getValue, Map.Entry::getKey));
    List<String> partitionColumns = layout.getNodePartitioning().get().getPartitioningColumns().stream().map(columnNamesByHandle::get).collect(toList());
    return Optional.of(new ConnectorNewTableLayout(partitioningHandle, partitionColumns));
}
Also used : Slice(io.airlift.slice.Slice) ConnectorTableLayoutHandle(com.facebook.presto.spi.ConnectorTableLayoutHandle) GENERIC_INTERNAL_ERROR(com.facebook.presto.spi.StandardErrorCode.GENERIC_INTERNAL_ERROR) ConnectorTableHandle(com.facebook.presto.spi.ConnectorTableHandle) ConnectorOutputTableHandle(com.facebook.presto.spi.ConnectorOutputTableHandle) PrestoException(com.facebook.presto.spi.PrestoException) TableIdentity(com.facebook.presto.spi.TableIdentity) Privilege(com.facebook.presto.spi.security.Privilege) OptionalLong(java.util.OptionalLong) SchemaTableName(com.facebook.presto.spi.SchemaTableName) Map(java.util.Map) ConnectorInsertTableHandle(com.facebook.presto.spi.ConnectorInsertTableHandle) ConnectorTableMetadata(com.facebook.presto.spi.ConnectorTableMetadata) Collections.emptyMap(java.util.Collections.emptyMap) Collections.emptyList(java.util.Collections.emptyList) ConnectorTableLayout(com.facebook.presto.spi.ConnectorTableLayout) Collection(java.util.Collection) ColumnIdentity(com.facebook.presto.spi.ColumnIdentity) Set(java.util.Set) Constraint(com.facebook.presto.spi.Constraint) ConnectorResolvedIndex(com.facebook.presto.spi.ConnectorResolvedIndex) Collectors(java.util.stream.Collectors) ConnectorSession(com.facebook.presto.spi.ConnectorSession) TupleDomain(com.facebook.presto.spi.predicate.TupleDomain) ConnectorTableLayoutResult(com.facebook.presto.spi.ConnectorTableLayoutResult) List(java.util.List) Collectors.toList(java.util.stream.Collectors.toList) ColumnMetadata(com.facebook.presto.spi.ColumnMetadata) SchemaTablePrefix(com.facebook.presto.spi.SchemaTablePrefix) NOT_SUPPORTED(com.facebook.presto.spi.StandardErrorCode.NOT_SUPPORTED) ColumnHandle(com.facebook.presto.spi.ColumnHandle) ConnectorNewTableLayout(com.facebook.presto.spi.ConnectorNewTableLayout) Optional(java.util.Optional) ConnectorViewDefinition(com.facebook.presto.spi.ConnectorViewDefinition) ColumnHandle(com.facebook.presto.spi.ColumnHandle) PrestoException(com.facebook.presto.spi.PrestoException) ConnectorNewTableLayout(com.facebook.presto.spi.ConnectorNewTableLayout) ConnectorTableLayoutResult(com.facebook.presto.spi.ConnectorTableLayoutResult) ConnectorTableLayout(com.facebook.presto.spi.ConnectorTableLayout) Map(java.util.Map) Collections.emptyMap(java.util.Collections.emptyMap)

Example 75 with Optional

use of java.util.Optional in project presto by prestodb.

the class TpchIndexMetadata method resolveIndex.

@Override
public Optional<ConnectorResolvedIndex> resolveIndex(ConnectorSession session, ConnectorTableHandle tableHandle, Set<ColumnHandle> indexableColumns, Set<ColumnHandle> outputColumns, TupleDomain<ColumnHandle> tupleDomain) {
    TpchTableHandle tpchTableHandle = (TpchTableHandle) tableHandle;
    // Keep the fixed values that don't overlap with the indexableColumns
    // Note: technically we could more efficiently utilize the overlapped columns, but this way is simpler for now
    Map<ColumnHandle, NullableValue> fixedValues = TupleDomain.extractFixedValues(tupleDomain).orElse(ImmutableMap.of()).entrySet().stream().filter(entry -> !indexableColumns.contains(entry.getKey())).filter(// strip nulls since meaningless in index join lookups
    entry -> !entry.getValue().isNull()).collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue));
    // determine all columns available for index lookup
    Set<String> lookupColumnNames = ImmutableSet.<String>builder().addAll(handleToNames(ImmutableList.copyOf(indexableColumns))).addAll(handleToNames(ImmutableList.copyOf(fixedValues.keySet()))).build();
    // do we have an index?
    if (!indexedData.getIndexedTable(tpchTableHandle.getTableName(), tpchTableHandle.getScaleFactor(), lookupColumnNames).isPresent()) {
        return Optional.empty();
    }
    TupleDomain<ColumnHandle> filteredTupleDomain = tupleDomain;
    if (!tupleDomain.isNone()) {
        filteredTupleDomain = TupleDomain.withColumnDomains(Maps.filterKeys(tupleDomain.getDomains().get(), not(in(fixedValues.keySet()))));
    }
    TpchIndexHandle indexHandle = new TpchIndexHandle(tpchTableHandle.getTableName(), tpchTableHandle.getScaleFactor(), lookupColumnNames, TupleDomain.fromFixedValues(fixedValues));
    return Optional.of(new ConnectorResolvedIndex(indexHandle, filteredTupleDomain));
}
Also used : ImmutableSet(com.google.common.collect.ImmutableSet) ImmutableMap(com.google.common.collect.ImmutableMap) TpchIndexProvider.handleToNames(com.facebook.presto.tests.tpch.TpchIndexProvider.handleToNames) Set(java.util.Set) ConnectorTableHandle(com.facebook.presto.spi.ConnectorTableHandle) ConnectorResolvedIndex(com.facebook.presto.spi.ConnectorResolvedIndex) TpchMetadata(com.facebook.presto.tpch.TpchMetadata) Maps(com.google.common.collect.Maps) Collectors(java.util.stream.Collectors) ConnectorSession(com.facebook.presto.spi.ConnectorSession) TupleDomain(com.facebook.presto.spi.predicate.TupleDomain) TpchTableHandle(com.facebook.presto.tpch.TpchTableHandle) Predicates.in(com.google.common.base.Predicates.in) ImmutableList(com.google.common.collect.ImmutableList) Predicates.not(com.google.common.base.Predicates.not) ColumnHandle(com.facebook.presto.spi.ColumnHandle) Map(java.util.Map) Objects.requireNonNull(java.util.Objects.requireNonNull) Optional(java.util.Optional) NullableValue(com.facebook.presto.spi.predicate.NullableValue) ColumnHandle(com.facebook.presto.spi.ColumnHandle) ConnectorResolvedIndex(com.facebook.presto.spi.ConnectorResolvedIndex) NullableValue(com.facebook.presto.spi.predicate.NullableValue) ImmutableMap(com.google.common.collect.ImmutableMap) Map(java.util.Map) TpchTableHandle(com.facebook.presto.tpch.TpchTableHandle)

Aggregations

Optional (java.util.Optional)414 List (java.util.List)208 Map (java.util.Map)138 ArrayList (java.util.ArrayList)107 Set (java.util.Set)95 Collectors (java.util.stream.Collectors)92 IOException (java.io.IOException)85 ImmutableList (com.google.common.collect.ImmutableList)82 ImmutableMap (com.google.common.collect.ImmutableMap)75 Test (org.junit.Test)69 ImmutableSet (com.google.common.collect.ImmutableSet)65 HashMap (java.util.HashMap)65 Path (java.nio.file.Path)55 Logger (org.slf4j.Logger)54 LoggerFactory (org.slf4j.LoggerFactory)53 Collections (java.util.Collections)51 Collection (java.util.Collection)48 Arrays (java.util.Arrays)42 BuildTarget (com.facebook.buck.model.BuildTarget)40 HashSet (java.util.HashSet)37