use of io.trino.spi.predicate.TupleDomain in project trino by trinodb.
the class InformationSchemaMetadata method calculateRoles.
private Optional<Set<String>> calculateRoles(ConnectorSession connectorSession, TupleDomain<ColumnHandle> constraint, Optional<Predicate<Map<ColumnHandle, NullableValue>>> predicate) {
if (constraint.isNone()) {
return Optional.empty();
}
Optional<Set<String>> roles = filterString(constraint, ROLE_NAME_COLUMN_HANDLE);
if (roles.isPresent()) {
Set<String> result = roles.get().stream().filter(this::isLowerCase).filter(role -> predicate.isEmpty() || predicate.get().test(roleAsFixedValues(role))).collect(toImmutableSet());
if (result.isEmpty()) {
return Optional.empty();
}
if (result.size() <= MAX_ROLE_COUNT) {
return Optional.of(result);
}
}
if (predicate.isEmpty()) {
return Optional.empty();
}
Session session = ((FullConnectorSession) connectorSession).getSession();
return Optional.of(metadata.listRoles(session, Optional.of(catalogName)).stream().filter(role -> predicate.get().test(roleAsFixedValues(role))).collect(toImmutableSet()));
}
use of io.trino.spi.predicate.TupleDomain in project trino by trinodb.
the class RuleStatsSystemTable method pageSource.
@Override
public ConnectorPageSource pageSource(ConnectorTransactionHandle transactionHandle, ConnectorSession session, TupleDomain<Integer> constraint) {
checkState(ruleStatsRecorder.isPresent(), "Rule stats system table can return results only on coordinator");
Map<Class<?>, RuleStats> ruleStats = ruleStatsRecorder.get().getStats();
int positionCount = ruleStats.size();
Map<String, BlockBuilder> blockBuilders = ruleStatsTable.getColumns().stream().collect(toImmutableMap(ColumnMetadata::getName, column -> column.getType().createBlockBuilder(null, positionCount)));
for (Map.Entry<Class<?>, RuleStats> entry : ruleStats.entrySet()) {
RuleStats stats = entry.getValue();
VARCHAR.writeString(blockBuilders.get("rule_name"), entry.getKey().getSimpleName());
BIGINT.writeLong(blockBuilders.get("invocations"), stats.getInvocations());
BIGINT.writeLong(blockBuilders.get("matches"), stats.getHits());
BIGINT.writeLong(blockBuilders.get("failures"), stats.getFailures());
DOUBLE.writeDouble(blockBuilders.get("average_time"), stats.getTime().getAvg());
BlockBuilder mapWriter = blockBuilders.get("time_distribution_percentiles").beginBlockEntry();
for (Map.Entry<Double, Double> percentile : stats.getTime().getPercentiles().entrySet()) {
DOUBLE.writeDouble(mapWriter, percentile.getKey());
DOUBLE.writeDouble(mapWriter, percentile.getValue());
}
blockBuilders.get("time_distribution_percentiles").closeEntry();
}
Block[] blocks = ruleStatsTable.getColumns().stream().map(column -> blockBuilders.get(column.getName()).build()).toArray(Block[]::new);
return new FixedPageSource(ImmutableList.of(new Page(positionCount, blocks)));
}
use of io.trino.spi.predicate.TupleDomain in project trino by trinodb.
the class ColumnJdbcTable method applyFilter.
@Override
public TupleDomain<ColumnHandle> applyFilter(ConnectorSession connectorSession, Constraint constraint) {
TupleDomain<ColumnHandle> tupleDomain = constraint.getSummary();
if (tupleDomain.isNone() || constraint.predicate().isEmpty()) {
return tupleDomain;
}
Predicate<Map<ColumnHandle, NullableValue>> predicate = constraint.predicate().get();
Set<ColumnHandle> predicateColumns = constraint.getPredicateColumns().orElseThrow(() -> new VerifyException("columns not present for a predicate"));
boolean hasSchemaPredicate = predicateColumns.contains(TABLE_SCHEMA_COLUMN);
boolean hasTablePredicate = predicateColumns.contains(TABLE_NAME_COLUMN);
if (!hasSchemaPredicate && !hasTablePredicate) {
// No filter on schema name and table name at all.
return tupleDomain;
}
Session session = ((FullConnectorSession) connectorSession).getSession();
Optional<String> catalogFilter = tryGetSingleVarcharValue(tupleDomain, TABLE_CATALOG_COLUMN);
Optional<String> schemaFilter = tryGetSingleVarcharValue(tupleDomain, TABLE_SCHEMA_COLUMN);
Optional<String> tableFilter = tryGetSingleVarcharValue(tupleDomain, TABLE_NAME_COLUMN);
if (schemaFilter.isPresent() && tableFilter.isPresent()) {
// No need to narrow down the domain.
return tupleDomain;
}
List<String> catalogs = listCatalogs(session, metadata, accessControl, catalogFilter).keySet().stream().filter(catalogName -> predicate.test(ImmutableMap.of(TABLE_CATALOG_COLUMN, toNullableValue(catalogName)))).collect(toImmutableList());
List<CatalogSchemaName> schemas = catalogs.stream().flatMap(catalogName -> listSchemas(session, metadata, accessControl, catalogName, schemaFilter).stream().filter(schemaName -> !hasSchemaPredicate || predicate.test(ImmutableMap.of(TABLE_CATALOG_COLUMN, toNullableValue(catalogName), TABLE_SCHEMA_COLUMN, toNullableValue(schemaName)))).map(schemaName -> new CatalogSchemaName(catalogName, schemaName))).collect(toImmutableList());
if (!hasTablePredicate) {
return TupleDomain.withColumnDomains(ImmutableMap.<ColumnHandle, Domain>builder().put(TABLE_CATALOG_COLUMN, schemas.stream().map(CatalogSchemaName::getCatalogName).collect(toVarcharDomain()).simplify(MAX_DOMAIN_SIZE)).put(TABLE_SCHEMA_COLUMN, schemas.stream().map(CatalogSchemaName::getSchemaName).collect(toVarcharDomain()).simplify(MAX_DOMAIN_SIZE)).buildOrThrow());
}
List<CatalogSchemaTableName> tables = schemas.stream().flatMap(schema -> {
QualifiedTablePrefix tablePrefix = tableFilter.isPresent() ? new QualifiedTablePrefix(schema.getCatalogName(), schema.getSchemaName(), tableFilter.get()) : new QualifiedTablePrefix(schema.getCatalogName(), schema.getSchemaName());
return listTables(session, metadata, accessControl, tablePrefix).stream().filter(schemaTableName -> predicate.test(ImmutableMap.of(TABLE_CATALOG_COLUMN, toNullableValue(schema.getCatalogName()), TABLE_SCHEMA_COLUMN, toNullableValue(schemaTableName.getSchemaName()), TABLE_NAME_COLUMN, toNullableValue(schemaTableName.getTableName())))).map(schemaTableName -> new CatalogSchemaTableName(schema.getCatalogName(), schemaTableName.getSchemaName(), schemaTableName.getTableName()));
}).collect(toImmutableList());
return TupleDomain.withColumnDomains(ImmutableMap.<ColumnHandle, Domain>builder().put(TABLE_CATALOG_COLUMN, tables.stream().map(CatalogSchemaTableName::getCatalogName).collect(toVarcharDomain()).simplify(MAX_DOMAIN_SIZE)).put(TABLE_SCHEMA_COLUMN, tables.stream().map(catalogSchemaTableName -> catalogSchemaTableName.getSchemaTableName().getSchemaName()).collect(toVarcharDomain()).simplify(MAX_DOMAIN_SIZE)).put(TABLE_NAME_COLUMN, tables.stream().map(catalogSchemaTableName -> catalogSchemaTableName.getSchemaTableName().getTableName()).collect(toVarcharDomain()).simplify(MAX_DOMAIN_SIZE)).buildOrThrow());
}
use of io.trino.spi.predicate.TupleDomain in project trino by trinodb.
the class FilterUtil method tryGetSingleVarcharValue.
public static <T> Optional<String> tryGetSingleVarcharValue(TupleDomain<T> constraint, T index) {
if (constraint.isNone()) {
return Optional.empty();
}
Domain domain = constraint.getDomains().get().get(index);
if ((domain == null) || !domain.isSingleValue()) {
return Optional.empty();
}
Object value = domain.getSingleValue();
return Optional.of(((Slice) value).toStringUtf8());
}
use of io.trino.spi.predicate.TupleDomain in project trino by trinodb.
the class DynamicFilterSourceOperator method finish.
@Override
public void finish() {
if (finished) {
// NOTE: finish() may be called multiple times (see comment at Driver::processInternal).
return;
}
finished = true;
ImmutableMap.Builder<DynamicFilterId, Domain> domainsBuilder = ImmutableMap.builder();
if (valueSets == null) {
if (minValues == null) {
// else it was notified with 'all' in handleMinMaxCollectionLimitExceeded
return;
}
// valueSets became too large, create TupleDomain from min/max values
for (Integer channelIndex : minMaxChannels) {
Type type = channels.get(channelIndex).type;
if (minValues[channelIndex] == null) {
// all values were null
domainsBuilder.put(channels.get(channelIndex).filterId, Domain.none(type));
continue;
}
Object min = readNativeValue(type, minValues[channelIndex], 0);
Object max = readNativeValue(type, maxValues[channelIndex], 0);
Domain domain = Domain.create(ValueSet.ofRanges(range(type, min, true, max, true)), false);
domainsBuilder.put(channels.get(channelIndex).filterId, domain);
}
minValues = null;
maxValues = null;
dynamicPredicateConsumer.accept(TupleDomain.withColumnDomains(domainsBuilder.buildOrThrow()));
return;
}
for (int channelIndex = 0; channelIndex < channels.size(); ++channelIndex) {
Block block = blockBuilders[channelIndex].build();
Type type = channels.get(channelIndex).type;
domainsBuilder.put(channels.get(channelIndex).filterId, convertToDomain(type, block));
}
valueSets = null;
blockBuilders = null;
dynamicPredicateConsumer.accept(TupleDomain.withColumnDomains(domainsBuilder.buildOrThrow()));
}
Aggregations