Search in sources :

Example 11 with TupleDomain

use of com.facebook.presto.spi.predicate.TupleDomain in project presto by prestodb.

the class SystemPageSourceProvider method createPageSource.

@Override
public ConnectorPageSource createPageSource(ConnectorTransactionHandle transactionHandle, ConnectorSession session, ConnectorSplit split, List<ColumnHandle> columns) {
    requireNonNull(columns, "columns is null");
    SystemTransactionHandle systemTransaction = (SystemTransactionHandle) transactionHandle;
    SystemSplit systemSplit = (SystemSplit) split;
    SchemaTableName tableName = systemSplit.getTableHandle().getSchemaTableName();
    SystemTable systemTable = tables.get(tableName);
    checkArgument(systemTable != null, "Table %s does not exist", tableName);
    List<ColumnMetadata> tableColumns = systemTable.getTableMetadata().getColumns();
    Map<String, Integer> columnsByName = new HashMap<>();
    for (int i = 0; i < tableColumns.size(); i++) {
        ColumnMetadata column = tableColumns.get(i);
        if (columnsByName.put(column.getName(), i) != null) {
            throw new PrestoException(GENERIC_INTERNAL_ERROR, "Duplicate column name: " + column.getName());
        }
    }
    ImmutableList.Builder<Integer> userToSystemFieldIndex = ImmutableList.builder();
    for (ColumnHandle column : columns) {
        String columnName = ((SystemColumnHandle) column).getColumnName();
        Integer index = columnsByName.get(columnName);
        if (index == null) {
            throw new PrestoException(GENERIC_INTERNAL_ERROR, format("Column does not exist: %s.%s", tableName, columnName));
        }
        userToSystemFieldIndex.add(index);
    }
    TupleDomain<ColumnHandle> constraint = systemSplit.getConstraint();
    ImmutableMap.Builder<Integer, Domain> newConstraints = ImmutableMap.builder();
    for (Map.Entry<ColumnHandle, Domain> entry : constraint.getDomains().get().entrySet()) {
        String columnName = ((SystemColumnHandle) entry.getKey()).getColumnName();
        newConstraints.put(columnsByName.get(columnName), entry.getValue());
    }
    TupleDomain<Integer> newContraint = withColumnDomains(newConstraints.build());
    try {
        return new MappedPageSource(systemTable.pageSource(systemTransaction.getConnectorTransactionHandle(), session, newContraint), userToSystemFieldIndex.build());
    } catch (UnsupportedOperationException e) {
        return new RecordPageSource(new MappedRecordSet(toRecordSet(systemTransaction.getConnectorTransactionHandle(), systemTable, session, newContraint), userToSystemFieldIndex.build()));
    }
}
Also used : ColumnMetadata(com.facebook.presto.spi.ColumnMetadata) HashMap(java.util.HashMap) ImmutableList(com.google.common.collect.ImmutableList) ImmutableCollectors.toImmutableList(com.facebook.presto.util.ImmutableCollectors.toImmutableList) PrestoException(com.facebook.presto.spi.PrestoException) RecordPageSource(com.facebook.presto.spi.RecordPageSource) MappedRecordSet(com.facebook.presto.split.MappedRecordSet) ColumnHandle(com.facebook.presto.spi.ColumnHandle) SchemaTableName(com.facebook.presto.spi.SchemaTableName) ImmutableMap(com.google.common.collect.ImmutableMap) MappedPageSource(com.facebook.presto.split.MappedPageSource) SystemTable(com.facebook.presto.spi.SystemTable) TupleDomain(com.facebook.presto.spi.predicate.TupleDomain) Domain(com.facebook.presto.spi.predicate.Domain) HashMap(java.util.HashMap) Map(java.util.Map) ImmutableMap(com.google.common.collect.ImmutableMap)

Example 12 with TupleDomain

use of com.facebook.presto.spi.predicate.TupleDomain in project presto by prestodb.

the class RaptorPageSourceProvider method createPageSource.

@Override
public ConnectorPageSource createPageSource(ConnectorTransactionHandle transactionHandle, ConnectorSession session, ConnectorSplit split, List<ColumnHandle> columns) {
    RaptorSplit raptorSplit = (RaptorSplit) split;
    OptionalInt bucketNumber = raptorSplit.getBucketNumber();
    TupleDomain<RaptorColumnHandle> predicate = raptorSplit.getEffectivePredicate();
    ReaderAttributes attributes = ReaderAttributes.from(session);
    OptionalLong transactionId = raptorSplit.getTransactionId();
    if (raptorSplit.getShardUuids().size() == 1) {
        UUID shardUuid = raptorSplit.getShardUuids().iterator().next();
        return createPageSource(shardUuid, bucketNumber, columns, predicate, attributes, transactionId);
    }
    Iterator<ConnectorPageSource> iterator = raptorSplit.getShardUuids().stream().map(shardUuid -> createPageSource(shardUuid, bucketNumber, columns, predicate, attributes, transactionId)).iterator();
    return new ConcatPageSource(iterator);
}
Also used : ConcatPageSource(com.facebook.presto.raptor.util.ConcatPageSource) Iterator(java.util.Iterator) UUID(java.util.UUID) OptionalInt(java.util.OptionalInt) ConnectorTransactionHandle(com.facebook.presto.spi.connector.ConnectorTransactionHandle) ConnectorSession(com.facebook.presto.spi.ConnectorSession) Inject(javax.inject.Inject) TupleDomain(com.facebook.presto.spi.predicate.TupleDomain) OptionalLong(java.util.OptionalLong) ConnectorSplit(com.facebook.presto.spi.ConnectorSplit) List(java.util.List) Collectors.toList(java.util.stream.Collectors.toList) ConnectorPageSource(com.facebook.presto.spi.ConnectorPageSource) ColumnHandle(com.facebook.presto.spi.ColumnHandle) Type(com.facebook.presto.spi.type.Type) Objects.requireNonNull(java.util.Objects.requireNonNull) ReaderAttributes(com.facebook.presto.raptor.storage.ReaderAttributes) StorageManager(com.facebook.presto.raptor.storage.StorageManager) ConnectorPageSourceProvider(com.facebook.presto.spi.connector.ConnectorPageSourceProvider) ConcatPageSource(com.facebook.presto.raptor.util.ConcatPageSource) ReaderAttributes(com.facebook.presto.raptor.storage.ReaderAttributes) OptionalLong(java.util.OptionalLong) OptionalInt(java.util.OptionalInt) UUID(java.util.UUID) ConnectorPageSource(com.facebook.presto.spi.ConnectorPageSource)

Example 13 with TupleDomain

use of com.facebook.presto.spi.predicate.TupleDomain in project presto by prestodb.

the class PreparedStatementBuilder method getWhereClause.

@SuppressWarnings("OptionalGetWithoutIsPresent")
private static String getWhereClause(TupleDomain<Integer> tupleDomain, List<String> columnNames, List<Type> types, Set<Integer> uuidColumnIndexes, List<ValueBuffer> bindValues) {
    if (tupleDomain.isNone()) {
        return "";
    }
    ImmutableList.Builder<String> conjunctsBuilder = ImmutableList.builder();
    Map<Integer, Domain> domainMap = tupleDomain.getDomains().get();
    for (Map.Entry<Integer, Domain> entry : domainMap.entrySet()) {
        int index = entry.getKey();
        String columnName = columnNames.get(index);
        Type type = types.get(index);
        conjunctsBuilder.add(toPredicate(index, columnName, type, entry.getValue(), uuidColumnIndexes, bindValues));
    }
    List<String> conjuncts = conjunctsBuilder.build();
    if (conjuncts.isEmpty()) {
        return "";
    }
    StringBuilder where = new StringBuilder("WHERE ");
    return Joiner.on(" AND\n").appendTo(where, conjuncts).toString();
}
Also used : Type(com.facebook.presto.spi.type.Type) Varchars.isVarcharType(com.facebook.presto.spi.type.Varchars.isVarcharType) ImmutableList(com.google.common.collect.ImmutableList) UUID.fromString(java.util.UUID.fromString) TupleDomain(com.facebook.presto.spi.predicate.TupleDomain) Domain(com.facebook.presto.spi.predicate.Domain) Map(java.util.Map)

Example 14 with TupleDomain

use of com.facebook.presto.spi.predicate.TupleDomain in project presto by prestodb.

the class TestShardMetadataRecordCursor method testSimple.

@Test
public void testSimple() throws Exception {
    ShardManager shardManager = createShardManager(dbi);
    // Add shards to the table
    long tableId = 1;
    OptionalInt bucketNumber = OptionalInt.empty();
    UUID uuid1 = UUID.randomUUID();
    UUID uuid2 = UUID.randomUUID();
    UUID uuid3 = UUID.randomUUID();
    ShardInfo shardInfo1 = new ShardInfo(uuid1, bucketNumber, ImmutableSet.of("node1"), ImmutableList.of(), 1, 10, 100);
    ShardInfo shardInfo2 = new ShardInfo(uuid2, bucketNumber, ImmutableSet.of("node2"), ImmutableList.of(), 2, 20, 200);
    ShardInfo shardInfo3 = new ShardInfo(uuid3, bucketNumber, ImmutableSet.of("node3"), ImmutableList.of(), 3, 30, 300);
    List<ShardInfo> shards = ImmutableList.of(shardInfo1, shardInfo2, shardInfo3);
    long transactionId = shardManager.beginTransaction();
    shardManager.commitShards(transactionId, tableId, ImmutableList.of(new ColumnInfo(1, BIGINT), new ColumnInfo(2, DATE)), shards, Optional.empty(), 0);
    Slice schema = utf8Slice(DEFAULT_TEST_ORDERS.getSchemaName());
    Slice table = utf8Slice(DEFAULT_TEST_ORDERS.getTableName());
    DateTime date1 = DateTime.parse("2015-01-01T00:00");
    DateTime date2 = DateTime.parse("2015-01-02T00:00");
    TupleDomain<Integer> tupleDomain = TupleDomain.withColumnDomains(ImmutableMap.<Integer, Domain>builder().put(0, Domain.singleValue(createVarcharType(10), schema)).put(1, Domain.create(ValueSet.ofRanges(lessThanOrEqual(createVarcharType(10), table)), true)).put(6, Domain.create(ValueSet.ofRanges(lessThanOrEqual(BIGINT, date1.getMillis()), greaterThan(BIGINT, date2.getMillis())), true)).put(7, Domain.create(ValueSet.ofRanges(lessThanOrEqual(BIGINT, date1.getMillis()), greaterThan(BIGINT, date2.getMillis())), true)).build());
    List<MaterializedRow> actual;
    try (RecordCursor cursor = new ShardMetadataSystemTable(dbi).cursor(null, SESSION, tupleDomain)) {
        actual = getMaterializedResults(cursor, SHARD_METADATA.getColumns());
    }
    assertEquals(actual.size(), 3);
    List<MaterializedRow> expected = ImmutableList.of(new MaterializedRow(DEFAULT_PRECISION, schema, table, utf8Slice(uuid1.toString()), null, 100L, 10L, 1L, null, null), new MaterializedRow(DEFAULT_PRECISION, schema, table, utf8Slice(uuid2.toString()), null, 200L, 20L, 2L, null, null), new MaterializedRow(DEFAULT_PRECISION, schema, table, utf8Slice(uuid3.toString()), null, 300L, 30L, 3L, null, null));
    assertEquals(actual, expected);
}
Also used : RecordCursor(com.facebook.presto.spi.RecordCursor) ColumnInfo(com.facebook.presto.raptor.metadata.ColumnInfo) ShardManager(com.facebook.presto.raptor.metadata.ShardManager) TestDatabaseShardManager.createShardManager(com.facebook.presto.raptor.metadata.TestDatabaseShardManager.createShardManager) OptionalInt(java.util.OptionalInt) DateTime(org.joda.time.DateTime) Slice(io.airlift.slice.Slice) Slices.utf8Slice(io.airlift.slice.Slices.utf8Slice) UUID(java.util.UUID) TupleDomain(com.facebook.presto.spi.predicate.TupleDomain) Domain(com.facebook.presto.spi.predicate.Domain) MaterializedRow(com.facebook.presto.testing.MaterializedRow) ShardInfo(com.facebook.presto.raptor.metadata.ShardInfo) Test(org.testng.annotations.Test)

Example 15 with TupleDomain

use of com.facebook.presto.spi.predicate.TupleDomain 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

TupleDomain (com.facebook.presto.spi.predicate.TupleDomain)36 Domain (com.facebook.presto.spi.predicate.Domain)30 ImmutableList (com.google.common.collect.ImmutableList)22 ColumnHandle (com.facebook.presto.spi.ColumnHandle)18 Map (java.util.Map)18 List (java.util.List)15 Objects.requireNonNull (java.util.Objects.requireNonNull)15 ImmutableMap (com.google.common.collect.ImmutableMap)14 Optional (java.util.Optional)13 Collectors.toList (java.util.stream.Collectors.toList)13 Type (com.facebook.presto.spi.type.Type)12 ImmutableSet (com.google.common.collect.ImmutableSet)12 PrestoException (com.facebook.presto.spi.PrestoException)11 Range (com.facebook.presto.spi.predicate.Range)11 Set (java.util.Set)11 ConnectorSession (com.facebook.presto.spi.ConnectorSession)10 TypeManager (com.facebook.presto.spi.type.TypeManager)10 Preconditions.checkArgument (com.google.common.base.Preconditions.checkArgument)10 Test (org.testng.annotations.Test)10 ConnectorTableHandle (com.facebook.presto.spi.ConnectorTableHandle)9