use of com.facebook.presto.common.predicate.NullableValue in project presto by prestodb.
the class HiveBucketing method getHiveBucketFilter.
public static Optional<HiveBucketFilter> getHiveBucketFilter(Optional<HiveBucketProperty> hiveBucketProperty, List<Column> dataColumns, TupleDomain<ColumnHandle> effectivePredicate) {
if (!hiveBucketProperty.isPresent()) {
return Optional.empty();
}
if (!hiveBucketProperty.get().getBucketFunctionType().equals(HIVE_COMPATIBLE)) {
// bucket filtering is only supported for tables bucketed with HIVE_COMPATIBLE hash function
return Optional.empty();
}
Optional<Map<ColumnHandle, Set<NullableValue>>> bindings = TupleDomain.extractFixedValueSets(effectivePredicate);
if (!bindings.isPresent()) {
return Optional.empty();
}
Optional<Set<Integer>> buckets = getHiveBuckets(hiveBucketProperty, dataColumns, bindings.get());
if (buckets.isPresent()) {
return Optional.of(new HiveBucketFilter(buckets.get()));
}
if (!effectivePredicate.getDomains().isPresent()) {
return Optional.empty();
}
Optional<Domain> domain = effectivePredicate.getDomains().get().entrySet().stream().filter(entry -> ((HiveColumnHandle) entry.getKey()).getName().equals(BUCKET_COLUMN_NAME)).findFirst().map(Entry::getValue);
if (!domain.isPresent()) {
return Optional.empty();
}
ValueSet values = domain.get().getValues();
ImmutableSet.Builder<Integer> builder = ImmutableSet.builder();
int bucketCount = hiveBucketProperty.get().getBucketCount();
for (int i = 0; i < bucketCount; i++) {
if (values.containsValue((long) i)) {
builder.add(i);
}
}
return Optional.of(new HiveBucketFilter(builder.build()));
}
use of com.facebook.presto.common.predicate.NullableValue in project presto by prestodb.
the class MaterializedViewUtils method generateBaseTablePredicates.
public static Map<SchemaTableName, Expression> generateBaseTablePredicates(Map<SchemaTableName, MaterializedViewStatus.MaterializedDataPredicates> predicatesFromBaseTables, Metadata metadata) {
Map<SchemaTableName, Expression> baseTablePredicates = new HashMap<>();
for (SchemaTableName baseTable : predicatesFromBaseTables.keySet()) {
MaterializedViewStatus.MaterializedDataPredicates predicatesInfo = predicatesFromBaseTables.get(baseTable);
List<String> partitionKeys = predicatesInfo.getColumnNames();
ImmutableList<Expression> keyExpressions = partitionKeys.stream().map(Identifier::new).collect(toImmutableList());
List<TupleDomain<String>> predicateDisjuncts = predicatesInfo.getPredicateDisjuncts();
Expression disjunct = null;
for (TupleDomain<String> predicateDisjunct : predicateDisjuncts) {
Expression conjunct = null;
Iterator<Expression> keyExpressionsIterator = keyExpressions.stream().iterator();
Map<String, NullableValue> predicateKeyValue = extractFixedValues(predicateDisjunct).orElseThrow(() -> new IllegalStateException("predicateKeyValue is not present!"));
for (String key : partitionKeys) {
NullableValue nullableValue = predicateKeyValue.get(key);
Expression expression;
if (nullableValue.isNull()) {
expression = new IsNullPredicate(keyExpressionsIterator.next());
} else {
LiteralEncoder literalEncoder = new LiteralEncoder(metadata.getBlockEncodingSerde());
Expression valueExpression = literalEncoder.toExpression(nullableValue.getValue(), nullableValue.getType(), false);
expression = new ComparisonExpression(EQUAL, keyExpressionsIterator.next(), valueExpression);
}
conjunct = conjunct == null ? expression : new LogicalBinaryExpression(AND, conjunct, expression);
}
disjunct = conjunct == null ? disjunct : disjunct == null ? conjunct : new LogicalBinaryExpression(OR, disjunct, conjunct);
}
// If no (fresh) partitions are found for table, that means we should not select from it
if (disjunct == null) {
disjunct = FALSE_LITERAL;
}
baseTablePredicates.put(baseTable, disjunct);
}
return baseTablePredicates;
}
use of com.facebook.presto.common.predicate.NullableValue in project presto by prestodb.
the class NativeCassandraSession method getPartitions.
@Override
public List<CassandraPartition> getPartitions(CassandraTable table, List<Set<Object>> filterPrefixes) {
List<CassandraColumnHandle> partitionKeyColumns = table.getPartitionKeyColumns();
if (filterPrefixes.size() != partitionKeyColumns.size()) {
return ImmutableList.of(CassandraPartition.UNPARTITIONED);
}
Iterable<Row> rows;
if (getCassandraVersion().compareTo(PARTITION_FETCH_WITH_IN_PREDICATE_VERSION) > 0) {
log.debug("Using IN predicate to fetch partitions.");
rows = queryPartitionKeysWithInClauses(table, filterPrefixes);
} else {
log.debug("Using combination of partition values to fetch partitions.");
rows = queryPartitionKeysLegacyWithMultipleQueries(table, filterPrefixes);
}
if (rows == null) {
// just split the whole partition range
return ImmutableList.of(CassandraPartition.UNPARTITIONED);
}
ByteBuffer buffer = ByteBuffer.allocate(1000);
HashMap<ColumnHandle, NullableValue> map = new HashMap<>();
Set<String> uniquePartitionIds = new HashSet<>();
StringBuilder stringBuilder = new StringBuilder();
boolean isComposite = partitionKeyColumns.size() > 1;
ImmutableList.Builder<CassandraPartition> partitions = ImmutableList.builder();
for (Row row : rows) {
buffer.clear();
map.clear();
stringBuilder.setLength(0);
for (int i = 0; i < partitionKeyColumns.size(); i++) {
ByteBuffer component = row.getBytesUnsafe(i);
if (isComposite) {
// build composite key
short len = (short) component.limit();
buffer.putShort(len);
buffer.put(component);
buffer.put((byte) 0);
} else {
buffer.put(component);
}
CassandraColumnHandle columnHandle = partitionKeyColumns.get(i);
NullableValue keyPart = CassandraType.getColumnValueForPartitionKey(row, i, columnHandle.getCassandraType(), columnHandle.getTypeArguments());
map.put(columnHandle, keyPart);
if (i > 0) {
stringBuilder.append(" AND ");
}
stringBuilder.append(CassandraCqlUtils.validColumnName(columnHandle.getName()));
stringBuilder.append(" = ");
stringBuilder.append(CassandraType.getColumnValueForCql(row, i, columnHandle.getCassandraType()));
}
buffer.flip();
byte[] key = new byte[buffer.limit()];
buffer.get(key);
TupleDomain<ColumnHandle> tupleDomain = TupleDomain.fromFixedValues(map);
String partitionId = stringBuilder.toString();
if (uniquePartitionIds.add(partitionId)) {
partitions.add(new CassandraPartition(key, partitionId, tupleDomain, false));
}
}
return partitions.build();
}
use of com.facebook.presto.common.predicate.NullableValue in project presto by prestodb.
the class HiveMetadata method buildColumnDomain.
private static Domain buildColumnDomain(ColumnHandle column, List<HivePartition> partitions) {
checkArgument(!partitions.isEmpty(), "partitions cannot be empty");
boolean hasNull = false;
Set<Object> nonNullValues = new HashSet<>();
Type type = null;
for (HivePartition partition : partitions) {
NullableValue value = partition.getKeys().get(column);
if (value == null) {
throw new PrestoException(HIVE_UNKNOWN_ERROR, format("Partition %s does not have a value for partition column %s", partition, column));
}
if (value.isNull()) {
hasNull = true;
} else {
nonNullValues.add(value.getValue());
}
if (type == null) {
type = value.getType();
}
}
if (!nonNullValues.isEmpty()) {
Domain domain = Domain.multipleValues(type, ImmutableList.copyOf(nonNullValues));
if (hasNull) {
return domain.union(Domain.onlyNull(type));
}
return domain;
}
return Domain.onlyNull(type);
}
use of com.facebook.presto.common.predicate.NullableValue in project presto by prestodb.
the class HivePageSourceProvider method shouldSkipPartition.
private static boolean shouldSkipPartition(TypeManager typeManager, HiveTableLayoutHandle hiveLayout, DateTimeZone hiveStorageTimeZone, HiveSplit hiveSplit, SplitContext splitContext) {
List<HiveColumnHandle> partitionColumns = hiveLayout.getPartitionColumns();
List<Type> partitionTypes = partitionColumns.stream().map(column -> typeManager.getType(column.getTypeSignature())).collect(toList());
List<HivePartitionKey> partitionKeys = hiveSplit.getPartitionKeys();
if (!splitContext.getDynamicFilterPredicate().isPresent() || hiveSplit.getPartitionKeys().isEmpty() || partitionColumns.isEmpty() || partitionColumns.size() != partitionKeys.size()) {
return false;
}
TupleDomain<ColumnHandle> dynamicFilter = splitContext.getDynamicFilterPredicate().get();
Map<ColumnHandle, Domain> domains = dynamicFilter.getDomains().get();
for (int i = 0; i < partitionKeys.size(); i++) {
Type type = partitionTypes.get(i);
HivePartitionKey hivePartitionKey = partitionKeys.get(i);
HiveColumnHandle hiveColumnHandle = partitionColumns.get(i);
Domain allowedDomain = domains.get(hiveColumnHandle);
NullableValue value = parsePartitionValue(hivePartitionKey, type, hiveStorageTimeZone);
if (allowedDomain != null && !allowedDomain.includesNullableValue(value.getValue())) {
return true;
}
}
return false;
}
Aggregations