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()));
}
}
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);
}
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();
}
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);
}
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));
}
Aggregations