Search in sources :

Example 11 with FieldType

use of io.cdap.cdap.api.dataset.lib.Partitioning.FieldType in project cdap by cdapio.

the class PartitionedFileSetDataset method validatePartitionKey.

/**
 * Validates the partition key against the partitioning.
 */
private static void validatePartitionKey(PartitionKey key, Partitioning partitioning) {
    if (!partitioning.getFields().keySet().equals(key.getFields().keySet())) {
        throw new IllegalArgumentException(String.format("Partition key is invalid: It contains fields %s, but the partitioning requires %s", key.getFields().keySet(), partitioning.getFields().keySet()));
    }
    for (Map.Entry<String, FieldType> entry : partitioning.getFields().entrySet()) {
        String fieldName = entry.getKey();
        FieldType fieldType = entry.getValue();
        Comparable fieldValue = key.getField(fieldName);
        if (fieldValue == null) {
            throw new IllegalArgumentException(String.format("Incomplete partition key: value for field '%s' is missing", fieldName));
        }
        try {
            fieldType.validate(fieldValue);
        } catch (IllegalArgumentException e) {
            throw new IllegalArgumentException(String.format("Invalid partition key: Value for field '%s' is incompatible with the partitioning: %s", fieldName, e.getMessage()));
        }
    }
}
Also used : Map(java.util.Map) ImmutableMap(com.google.common.collect.ImmutableMap) HashMap(java.util.HashMap) FieldType(io.cdap.cdap.api.dataset.lib.Partitioning.FieldType)

Example 12 with FieldType

use of io.cdap.cdap.api.dataset.lib.Partitioning.FieldType in project cdap by cdapio.

the class PartitionedFileSetDataset method generateStopKey.

private byte[] generateStopKey(PartitionFilter filter) {
    if (null == filter) {
        return null;
    }
    // validate partition filter, convert values, and compute size of output
    Map<String, FieldType> partitionFields = partitioning.getFields();
    int totalSize = 0;
    boolean allSingleValue = true;
    ArrayList<byte[]> values = Lists.newArrayListWithCapacity(partitionFields.size());
    for (Map.Entry<String, FieldType> entry : partitionFields.entrySet()) {
        String fieldName = entry.getKey();
        FieldType fieldType = entry.getValue();
        PartitionFilter.Condition<? extends Comparable> condition = filter.getCondition(fieldName);
        if (condition == null) {
            // this field is not present; we can't include any more fields in the stop key
            break;
        }
        Comparable upperValue = condition.getUpper();
        if (upperValue == null) {
            // this field is not present; we can't include any more fields in the stop key
            break;
        }
        try {
            fieldType.validate(upperValue);
        } catch (IllegalArgumentException e) {
            throw new IllegalArgumentException(String.format("Invalid partition filter: Upper bound for field '%s' is incompatible with the partitioning: %s", fieldName, e.getMessage()));
        }
        byte[] bytes = FieldTypes.toBytes(upperValue, fieldType);
        totalSize += bytes.length;
        values.add(bytes);
        if (!condition.isSingleValue()) {
            allSingleValue = false;
            // upper bound for this field, following fields don't matter
            break;
        }
    }
    if (values.isEmpty()) {
        return null;
    }
    // one \0 between each of the fields
    totalSize += values.size() - 1;
    if (allSingleValue) {
        // in this case the start and stop key are equal, we append one \1 to ensure the scan is not empty
        totalSize++;
    }
    byte[] stopKey = new byte[totalSize];
    int offset = 0;
    for (byte[] bytes : values) {
        System.arraycopy(bytes, 0, stopKey, offset, bytes.length);
        // this leaves a \0 byte after the value
        offset += bytes.length + 1;
        if (allSingleValue && offset == stopKey.length) {
            // see above - we \1 instead of \0 at the end, to make sure scan is not empty
            stopKey[offset - 1] = 1;
        }
    }
    return stopKey;
}
Also used : FieldType(io.cdap.cdap.api.dataset.lib.Partitioning.FieldType) PartitionFilter(io.cdap.cdap.api.dataset.lib.PartitionFilter) Map(java.util.Map) ImmutableMap(com.google.common.collect.ImmutableMap) HashMap(java.util.HashMap)

Aggregations

FieldType (io.cdap.cdap.api.dataset.lib.Partitioning.FieldType)12 Map (java.util.Map)12 ImmutableMap (com.google.common.collect.ImmutableMap)10 HashMap (java.util.HashMap)10 VisibleForTesting (com.google.common.annotations.VisibleForTesting)4 PartitionFilter (io.cdap.cdap.api.dataset.lib.PartitionFilter)4 PartitionKey (io.cdap.cdap.api.dataset.lib.PartitionKey)2 Nullable (javax.annotation.Nullable)2