Search in sources :

Example 1 with NullableValue

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

the class NativeCassandraSession method getPartitions.

@Override
public List<CassandraPartition> getPartitions(CassandraTable table, List<Object> filterPrefix) {
    Iterable<Row> rows = queryPartitionKeys(table, filterPrefix);
    if (rows == null) {
        // just split the whole partition range
        return ImmutableList.of(CassandraPartition.UNPARTITIONED);
    }
    List<CassandraColumnHandle> partitionKeyColumns = table.getPartitionKeyColumns();
    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();
}
Also used : ColumnHandle(com.facebook.presto.spi.ColumnHandle) HashMap(java.util.HashMap) ImmutableList(com.google.common.collect.ImmutableList) NullableValue(com.facebook.presto.spi.predicate.NullableValue) ByteBuffer(java.nio.ByteBuffer) Row(com.datastax.driver.core.Row) HashSet(java.util.HashSet)

Example 2 with NullableValue

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

the class LocalExecutionPlanner method plan.

public LocalExecutionPlan plan(Session session, PlanNode plan, Map<Symbol, Type> types, PartitioningScheme partitioningScheme, OutputBuffer outputBuffer) {
    List<Symbol> outputLayout = partitioningScheme.getOutputLayout();
    if (partitioningScheme.getPartitioning().getHandle().equals(FIXED_BROADCAST_DISTRIBUTION) || partitioningScheme.getPartitioning().getHandle().equals(FIXED_ARBITRARY_DISTRIBUTION) || partitioningScheme.getPartitioning().getHandle().equals(SINGLE_DISTRIBUTION) || partitioningScheme.getPartitioning().getHandle().equals(COORDINATOR_DISTRIBUTION)) {
        return plan(session, plan, outputLayout, types, new TaskOutputFactory(outputBuffer));
    }
    // We can convert the symbols directly into channels, because the root must be a sink and therefore the layout is fixed
    List<Integer> partitionChannels;
    List<Optional<NullableValue>> partitionConstants;
    List<Type> partitionChannelTypes;
    if (partitioningScheme.getHashColumn().isPresent()) {
        partitionChannels = ImmutableList.of(outputLayout.indexOf(partitioningScheme.getHashColumn().get()));
        partitionConstants = ImmutableList.of(Optional.empty());
        partitionChannelTypes = ImmutableList.of(BIGINT);
    } else {
        partitionChannels = partitioningScheme.getPartitioning().getArguments().stream().map(ArgumentBinding::getColumn).map(outputLayout::indexOf).collect(toImmutableList());
        partitionConstants = partitioningScheme.getPartitioning().getArguments().stream().map(argument -> {
            if (argument.isConstant()) {
                return Optional.of(argument.getConstant());
            }
            return Optional.<NullableValue>empty();
        }).collect(toImmutableList());
        partitionChannelTypes = partitioningScheme.getPartitioning().getArguments().stream().map(argument -> {
            if (argument.isConstant()) {
                return argument.getConstant().getType();
            }
            return types.get(argument.getColumn());
        }).collect(toImmutableList());
    }
    PartitionFunction partitionFunction = nodePartitioningManager.getPartitionFunction(session, partitioningScheme, partitionChannelTypes);
    OptionalInt nullChannel = OptionalInt.empty();
    Set<Symbol> partitioningColumns = partitioningScheme.getPartitioning().getColumns();
    // partitioningColumns expected to have one column in the normal case, and zero columns when partitioning on a constant
    checkArgument(!partitioningScheme.isReplicateNulls() || partitioningColumns.size() <= 1);
    if (partitioningScheme.isReplicateNulls() && partitioningColumns.size() == 1) {
        nullChannel = OptionalInt.of(outputLayout.indexOf(getOnlyElement(partitioningColumns)));
    }
    return plan(session, plan, outputLayout, types, new PartitionedOutputFactory(partitionFunction, partitionChannels, partitionConstants, nullChannel, outputBuffer, maxPagePartitioningBufferSize));
}
Also used : PartitionFunction(com.facebook.presto.operator.PartitionFunction) Optional(java.util.Optional) NullableValue(com.facebook.presto.spi.predicate.NullableValue) OptionalInt(java.util.OptionalInt) PartitionedOutputFactory(com.facebook.presto.operator.PartitionedOutputOperator.PartitionedOutputFactory) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Type(com.facebook.presto.spi.type.Type) TaskOutputFactory(com.facebook.presto.operator.TaskOutputOperator.TaskOutputFactory)

Example 3 with NullableValue

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

the class HivePartitionManager method getPartitions.

public HivePartitionResult getPartitions(SemiTransactionalHiveMetastore metastore, ConnectorTableHandle tableHandle, Constraint<ColumnHandle> constraint) {
    HiveTableHandle hiveTableHandle = (HiveTableHandle) tableHandle;
    TupleDomain<ColumnHandle> effectivePredicate = constraint.getSummary();
    SchemaTableName tableName = hiveTableHandle.getSchemaTableName();
    Table table = getTable(metastore, tableName);
    Optional<HiveBucketHandle> hiveBucketHandle = getHiveBucketHandle(connectorId, table);
    List<HiveColumnHandle> partitionColumns = getPartitionKeyColumnHandles(connectorId, table);
    List<HiveBucket> buckets = getHiveBucketNumbers(table, effectivePredicate);
    TupleDomain<HiveColumnHandle> compactEffectivePredicate = toCompactTupleDomain(effectivePredicate, domainCompactionThreshold);
    if (effectivePredicate.isNone()) {
        return new HivePartitionResult(partitionColumns, ImmutableList.of(), TupleDomain.none(), TupleDomain.none(), hiveBucketHandle);
    }
    if (partitionColumns.isEmpty()) {
        return new HivePartitionResult(partitionColumns, ImmutableList.of(new HivePartition(tableName, compactEffectivePredicate, buckets)), effectivePredicate, TupleDomain.none(), hiveBucketHandle);
    }
    List<Type> partitionTypes = partitionColumns.stream().map(column -> typeManager.getType(column.getTypeSignature())).collect(toList());
    List<String> partitionNames = getFilteredPartitionNames(metastore, tableName, partitionColumns, effectivePredicate);
    // do a final pass to filter based on fields that could not be used to filter the partitions
    int partitionCount = 0;
    ImmutableList.Builder<HivePartition> partitions = ImmutableList.builder();
    for (String partitionName : partitionNames) {
        Optional<Map<ColumnHandle, NullableValue>> values = parseValuesAndFilterPartition(partitionName, partitionColumns, partitionTypes, constraint);
        if (values.isPresent()) {
            if (partitionCount == maxPartitions) {
                throw new PrestoException(HIVE_EXCEEDED_PARTITION_LIMIT, format("Query over table '%s' can potentially read more than %s partitions", hiveTableHandle.getSchemaTableName(), maxPartitions));
            }
            partitionCount++;
            partitions.add(new HivePartition(tableName, compactEffectivePredicate, partitionName, values.get(), buckets));
        }
    }
    // All partition key domains will be fully evaluated, so we don't need to include those
    TupleDomain<ColumnHandle> remainingTupleDomain = TupleDomain.withColumnDomains(Maps.filterKeys(effectivePredicate.getDomains().get(), not(Predicates.in(partitionColumns))));
    TupleDomain<ColumnHandle> enforcedTupleDomain = TupleDomain.withColumnDomains(Maps.filterKeys(effectivePredicate.getDomains().get(), Predicates.in(partitionColumns)));
    return new HivePartitionResult(partitionColumns, partitions.build(), remainingTupleDomain, enforcedTupleDomain, hiveBucketHandle);
}
Also used : DateTimeZone(org.joda.time.DateTimeZone) Table(com.facebook.presto.hive.metastore.Table) TypeManager(com.facebook.presto.spi.type.TypeManager) Slice(io.airlift.slice.Slice) HiveUtil.getPartitionKeyColumnHandles(com.facebook.presto.hive.HiveUtil.getPartitionKeyColumnHandles) Strings.isNullOrEmpty(com.google.common.base.Strings.isNullOrEmpty) ConnectorTableHandle(com.facebook.presto.spi.ConnectorTableHandle) ProtectMode(org.apache.hadoop.hive.metastore.ProtectMode) PrestoException(com.facebook.presto.spi.PrestoException) ArrayList(java.util.ArrayList) Inject(javax.inject.Inject) Preconditions.checkArgument(com.google.common.base.Preconditions.checkArgument) SchemaTableName(com.facebook.presto.spi.SchemaTableName) ImmutableList(com.google.common.collect.ImmutableList) Predicates.not(com.google.common.base.Predicates.not) ValueSet(com.facebook.presto.spi.predicate.ValueSet) Type(com.facebook.presto.spi.type.Type) Map(java.util.Map) Objects.requireNonNull(java.util.Objects.requireNonNull) Predicates(com.google.common.base.Predicates) HiveBucket(com.facebook.presto.hive.HiveBucketing.HiveBucket) NullableValue(com.facebook.presto.spi.predicate.NullableValue) HIVE_EXCEEDED_PARTITION_LIMIT(com.facebook.presto.hive.HiveErrorCode.HIVE_EXCEEDED_PARTITION_LIMIT) ImmutableMap(com.google.common.collect.ImmutableMap) ProtectMode.getProtectModeFromString(org.apache.hadoop.hive.metastore.ProtectMode.getProtectModeFromString) Constraint(com.facebook.presto.spi.Constraint) SemiTransactionalHiveMetastore(com.facebook.presto.hive.metastore.SemiTransactionalHiveMetastore) HiveBucketing.getHiveBucketHandle(com.facebook.presto.hive.HiveBucketing.getHiveBucketHandle) Maps(com.google.common.collect.Maps) String.format(java.lang.String.format) TupleDomain(com.facebook.presto.spi.predicate.TupleDomain) Domain(com.facebook.presto.spi.predicate.Domain) List(java.util.List) Collectors.toList(java.util.stream.Collectors.toList) TableNotFoundException(com.facebook.presto.spi.TableNotFoundException) NOT_SUPPORTED(com.facebook.presto.spi.StandardErrorCode.NOT_SUPPORTED) ColumnHandle(com.facebook.presto.spi.ColumnHandle) HiveBucketing.getHiveBucketNumbers(com.facebook.presto.hive.HiveBucketing.getHiveBucketNumbers) FileUtils(org.apache.hadoop.hive.common.FileUtils) Optional(java.util.Optional) HiveUtil.parsePartitionValue(com.facebook.presto.hive.HiveUtil.parsePartitionValue) HiveBucket(com.facebook.presto.hive.HiveBucketing.HiveBucket) ImmutableList(com.google.common.collect.ImmutableList) PrestoException(com.facebook.presto.spi.PrestoException) ProtectMode.getProtectModeFromString(org.apache.hadoop.hive.metastore.ProtectMode.getProtectModeFromString) ColumnHandle(com.facebook.presto.spi.ColumnHandle) Table(com.facebook.presto.hive.metastore.Table) SchemaTableName(com.facebook.presto.spi.SchemaTableName) Constraint(com.facebook.presto.spi.Constraint) Type(com.facebook.presto.spi.type.Type) HiveBucketing.getHiveBucketHandle(com.facebook.presto.hive.HiveBucketing.getHiveBucketHandle) Map(java.util.Map) ImmutableMap(com.google.common.collect.ImmutableMap)

Example 4 with NullableValue

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

the class HivePartitionManager method parseValuesAndFilterPartition.

private Optional<Map<ColumnHandle, NullableValue>> parseValuesAndFilterPartition(String partitionName, List<HiveColumnHandle> partitionColumns, List<Type> partitionTypes, Constraint<ColumnHandle> constraint) {
    List<String> partitionValues = extractPartitionKeyValues(partitionName);
    Map<ColumnHandle, Domain> domains = constraint.getSummary().getDomains().get();
    ImmutableMap.Builder<ColumnHandle, NullableValue> builder = ImmutableMap.builder();
    for (int i = 0; i < partitionColumns.size(); i++) {
        HiveColumnHandle column = partitionColumns.get(i);
        NullableValue parsedValue = parsePartitionValue(partitionName, partitionValues.get(i), partitionTypes.get(i), timeZone);
        Domain allowedDomain = domains.get(column);
        if (allowedDomain != null && !allowedDomain.includesNullableValue(parsedValue.getValue())) {
            return Optional.empty();
        }
        builder.put(column, parsedValue);
    }
    Map<ColumnHandle, NullableValue> values = builder.build();
    if (!constraint.predicate().test(values)) {
        return Optional.empty();
    }
    return Optional.of(values);
}
Also used : ColumnHandle(com.facebook.presto.spi.ColumnHandle) NullableValue(com.facebook.presto.spi.predicate.NullableValue) ProtectMode.getProtectModeFromString(org.apache.hadoop.hive.metastore.ProtectMode.getProtectModeFromString) TupleDomain(com.facebook.presto.spi.predicate.TupleDomain) Domain(com.facebook.presto.spi.predicate.Domain) ImmutableMap(com.google.common.collect.ImmutableMap) Constraint(com.facebook.presto.spi.Constraint)

Example 5 with NullableValue

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

the class HiveBucketing method getHiveBucket.

private static Optional<HiveBucket> getHiveBucket(Table table, Map<ColumnHandle, NullableValue> bindings) {
    if (bindings.isEmpty()) {
        return Optional.empty();
    }
    List<String> bucketColumns = table.getStorage().getBucketProperty().get().getBucketedBy();
    Map<String, ObjectInspector> objectInspectors = new HashMap<>();
    // Get column name to object inspector mapping
    for (StructField field : getTableStructFields(table)) {
        objectInspectors.put(field.getFieldName(), field.getFieldObjectInspector());
    }
    // Verify the bucket column types are supported
    for (String column : bucketColumns) {
        ObjectInspector inspector = objectInspectors.get(column);
        if ((inspector == null) || (inspector.getCategory() != Category.PRIMITIVE)) {
            return Optional.empty();
        }
        if (!SUPPORTED_TYPES.contains(((PrimitiveObjectInspector) inspector).getPrimitiveCategory())) {
            return Optional.empty();
        }
    }
    // Get bindings for bucket columns
    Map<String, Object> bucketBindings = new HashMap<>();
    for (Entry<ColumnHandle, NullableValue> entry : bindings.entrySet()) {
        HiveColumnHandle colHandle = (HiveColumnHandle) entry.getKey();
        if (!entry.getValue().isNull() && bucketColumns.contains(colHandle.getName())) {
            bucketBindings.put(colHandle.getName(), entry.getValue().getValue());
        }
    }
    // Check that we have bindings for all bucket columns
    if (bucketBindings.size() != bucketColumns.size()) {
        return Optional.empty();
    }
    // Get bindings of bucket columns
    ImmutableList.Builder<Entry<ObjectInspector, Object>> columnBindings = ImmutableList.builder();
    for (String column : bucketColumns) {
        columnBindings.add(immutableEntry(objectInspectors.get(column), bucketBindings.get(column)));
    }
    return getHiveBucket(columnBindings.build(), table.getStorage().getBucketProperty().get().getBucketCount());
}
Also used : ColumnHandle(com.facebook.presto.spi.ColumnHandle) PrimitiveObjectInspectorFactory.javaByteObjectInspector(org.apache.hadoop.hive.serde2.objectinspector.primitive.PrimitiveObjectInspectorFactory.javaByteObjectInspector) PrimitiveObjectInspectorFactory.javaLongObjectInspector(org.apache.hadoop.hive.serde2.objectinspector.primitive.PrimitiveObjectInspectorFactory.javaLongObjectInspector) ObjectInspector(org.apache.hadoop.hive.serde2.objectinspector.ObjectInspector) PrimitiveObjectInspectorFactory.javaIntObjectInspector(org.apache.hadoop.hive.serde2.objectinspector.primitive.PrimitiveObjectInspectorFactory.javaIntObjectInspector) PrimitiveObjectInspectorFactory.javaBooleanObjectInspector(org.apache.hadoop.hive.serde2.objectinspector.primitive.PrimitiveObjectInspectorFactory.javaBooleanObjectInspector) IntObjectInspector(org.apache.hadoop.hive.serde2.objectinspector.primitive.IntObjectInspector) PrimitiveObjectInspectorFactory.javaShortObjectInspector(org.apache.hadoop.hive.serde2.objectinspector.primitive.PrimitiveObjectInspectorFactory.javaShortObjectInspector) PrimitiveObjectInspector(org.apache.hadoop.hive.serde2.objectinspector.PrimitiveObjectInspector) PrimitiveObjectInspectorFactory.javaStringObjectInspector(org.apache.hadoop.hive.serde2.objectinspector.primitive.PrimitiveObjectInspectorFactory.javaStringObjectInspector) HashMap(java.util.HashMap) ImmutableList(com.google.common.collect.ImmutableList) NullableValue(com.facebook.presto.spi.predicate.NullableValue) Maps.immutableEntry(com.google.common.collect.Maps.immutableEntry) Entry(java.util.Map.Entry) StructField(org.apache.hadoop.hive.serde2.objectinspector.StructField) PrimitiveObjectInspector(org.apache.hadoop.hive.serde2.objectinspector.PrimitiveObjectInspector) DeferredJavaObject(org.apache.hadoop.hive.ql.udf.generic.GenericUDF.DeferredJavaObject) DeferredObject(org.apache.hadoop.hive.ql.udf.generic.GenericUDF.DeferredObject)

Aggregations

NullableValue (com.facebook.presto.spi.predicate.NullableValue)9 ColumnHandle (com.facebook.presto.spi.ColumnHandle)7 ImmutableList (com.google.common.collect.ImmutableList)5 ImmutableMap (com.google.common.collect.ImmutableMap)4 ConnectorSession (com.facebook.presto.spi.ConnectorSession)3 Constraint (com.facebook.presto.spi.Constraint)3 TupleDomain (com.facebook.presto.spi.predicate.TupleDomain)3 HashMap (java.util.HashMap)3 List (java.util.List)3 Objects.requireNonNull (java.util.Objects.requireNonNull)3 Optional (java.util.Optional)3 ConnectorSplit (com.facebook.presto.spi.ConnectorSplit)2 ConnectorSplitSource (com.facebook.presto.spi.ConnectorSplitSource)2 ConnectorTableLayoutHandle (com.facebook.presto.spi.ConnectorTableLayoutHandle)2 FixedSplitSource (com.facebook.presto.spi.FixedSplitSource)2 Domain (com.facebook.presto.spi.predicate.Domain)2 Type (com.facebook.presto.spi.type.Type)2 Map (java.util.Map)2 Entry (java.util.Map.Entry)2 ProtectMode.getProtectModeFromString (org.apache.hadoop.hive.metastore.ProtectMode.getProtectModeFromString)2