Java Code Examples for io.airlift.slice.Slice#getByte()

The following examples show how to use io.airlift.slice.Slice#getByte() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: SliceAssertions.java    From slice with Apache License 2.0 6 votes vote down vote up
public static void assertSlicesEqual(Slice actual, Slice expected)
{
    if (actual == null && expected == null) {
        return;
    }
    else if (actual == null) {
        throw new AssertionError("Actual is null");
    }
    else if (expected == null) {
        throw new AssertionError("Expected actual to be null");
    }

    if (actual.length() != expected.length()) {
        throw new AssertionError(String.format("Slices differ in size. Actual: %s, expected: %s", actual.length(), expected.length()));
    }

    for (int i = 0; i < actual.length(); i++) {
        if (actual.getByte(i) != expected.getByte(i)) {
            throw new AssertionError(String.format("Slices differ at index %s. Actual: 0x%02x, expected: 0x%02x", i, actual.getUnsignedByte(i), expected.getUnsignedByte(i)));
        }
    }
}
 
Example 2
Source File: Chars.java    From presto with Apache License 2.0 6 votes vote down vote up
public static int byteCountWithoutTrailingSpace(Slice slice, int offset, int length)
{
    requireNonNull(slice, "slice is null");
    if (length < 0) {
        throw new IllegalArgumentException("length must be greater than or equal to zero");
    }
    if (offset < 0 || offset + length > slice.length()) {
        throw new IllegalArgumentException("invalid offset/length");
    }
    for (int i = length + offset; i > offset; i--) {
        if (slice.getByte(i - 1) != ' ') {
            return i - offset;
        }
    }
    return 0;
}
 
Example 3
Source File: RcFileDecoderUtils.java    From presto with Apache License 2.0 6 votes vote down vote up
public static int calculateTruncationLength(Type type, Slice slice, int offset, int length)
{
    requireNonNull(type, "type is null");
    if (type instanceof VarcharType) {
        if (((VarcharType) type).isUnbounded()) {
            return length;
        }
        return calculateTruncationLength(((VarcharType) type).getBoundedLength(), slice, offset, length);
    }
    if (type instanceof CharType) {
        int truncationLength = calculateTruncationLength(((CharType) type).getLength(), slice, offset, length);
        // At run-time char(x) is represented without trailing spaces
        while (truncationLength > 0 && slice.getByte(offset + truncationLength - 1) == ' ') {
            truncationLength--;
        }
        return truncationLength;
    }
    return length;
}
 
Example 4
Source File: LongEncoding.java    From presto with Apache License 2.0 6 votes vote down vote up
private static long parseLong(Slice slice, int start, int length)
{
    if (slice.equals(start, length, MIN_LONG, 0, MIN_LONG.length())) {
        return Long.MIN_VALUE;
    }

    int limit = start + length;

    int sign;
    if (slice.getByte(start) == '-') {
        sign = -1;
        start++;
    }
    else {
        sign = 1;
    }

    long value = slice.getByte(start) - ((int) '0');
    start++;
    while (start < limit) {
        value = value * 10 + (slice.getByte(start) - ((int) '0'));
        start++;
    }

    return value * sign;
}
 
Example 5
Source File: ListEncoding.java    From presto with Apache License 2.0 6 votes vote down vote up
@Override
public void decodeValueInto(int depth, BlockBuilder builder, Slice slice, int offset, int length)
        throws RcFileCorruptionException
{
    byte separator = getSeparator(depth);
    int end = offset + length;

    BlockBuilder arrayBlockBuilder = builder.beginBlockEntry();
    if (length > 0) {
        int elementOffset = offset;
        while (offset < end) {
            byte currentByte = slice.getByte(offset);
            if (currentByte == separator) {
                decodeElementValueInto(depth, arrayBlockBuilder, slice, elementOffset, offset - elementOffset);
                elementOffset = offset + 1;
            }
            else if (isEscapeByte(currentByte) && offset + 1 < length) {
                // ignore the char after escape_char
                offset++;
            }
            offset++;
        }
        decodeElementValueInto(depth, arrayBlockBuilder, slice, elementOffset, offset - elementOffset);
    }
    builder.closeEntry();
}
 
Example 6
Source File: StringEncoding.java    From presto with Apache License 2.0 6 votes vote down vote up
@Override
public Block decodeColumn(ColumnData columnData)
{
    int size = columnData.rowCount();
    BlockBuilder builder = type.createBlockBuilder(null, size);

    Slice slice = columnData.getSlice();
    for (int i = 0; i < size; i++) {
        int length = columnData.getLength(i);
        if (length > 0) {
            int offset = columnData.getOffset(i);
            if ((length == 1) && slice.getByte(offset) == HIVE_EMPTY_STRING_BYTE) {
                type.writeSlice(builder, EMPTY_SLICE);
            }
            else {
                length = calculateTruncationLength(type, slice, offset, length);
                type.writeSlice(builder, slice.slice(offset, length));
            }
        }
        else {
            builder.appendNull();
        }
    }
    return builder.build();
}
 
Example 7
Source File: GeoFunctions.java    From presto with Apache License 2.0 5 votes vote down vote up
private static int getWktExportFlags(Slice input)
{
    byte hadoopShapeType = input.getByte(HADOOP_SHAPE_SIZE_WKID);
    if (hadoopShapeType < 0 || hadoopShapeType >= HADOOP_SHAPE_TYPES.length) {
        throw new PrestoException(INVALID_FUNCTION_ARGUMENT, "Invalid Hadoop shape type: " + hadoopShapeType);
    }
    return HADOOP_SHAPE_TYPES[hadoopShapeType];
}
 
Example 8
Source File: RcFileDecoderUtils.java    From presto with Apache License 2.0 5 votes vote down vote up
private static long readVIntInternal(Slice slice, int start, int length)
{
    long value = 0;
    for (int i = 1; i < length; i++) {
        value <<= 8;
        value |= (slice.getByte(start + i) & 0xFF);
    }
    return isNegativeVInt(slice.getByte(start)) ? ~value : value;
}
 
Example 9
Source File: RcFileDecoderUtils.java    From presto with Apache License 2.0 5 votes vote down vote up
public static long readVInt(Slice slice, int start, int length)
{
    if (length == 1) {
        return slice.getByte(start);
    }

    return readVIntInternal(slice, start, length);
}
 
Example 10
Source File: RcFileDecoderUtils.java    From presto with Apache License 2.0 5 votes vote down vote up
public static long readVInt(Slice slice, int start)
{
    byte firstByte = slice.getByte(start);
    int length = decodeVIntSize(firstByte);
    if (length == 1) {
        return firstByte;
    }

    return readVIntInternal(slice, start, length);
}
 
Example 11
Source File: ListEncoding.java    From presto with Apache License 2.0 5 votes vote down vote up
@Override
public void decodeValueInto(BlockBuilder builder, Slice slice, int offset, int length)
{
    // entries in list
    int entries = toIntExact(readVInt(slice, offset));
    offset += decodeVIntSize(slice.getByte(offset));

    // null bytes
    int nullByteCur = offset;
    int nullByteEnd = offset + (entries + 7) / 8;

    // read elements starting after null bytes
    int elementOffset = nullByteEnd;
    BlockBuilder arrayBuilder = builder.beginBlockEntry();
    for (int i = 0; i < entries; i++) {
        if ((slice.getByte(nullByteCur) & (1 << (i % 8))) != 0) {
            int valueOffset = elementEncoding.getValueOffset(slice, elementOffset);
            int valueLength = elementEncoding.getValueLength(slice, elementOffset);

            elementEncoding.decodeValueInto(arrayBuilder, slice, elementOffset + valueOffset, valueLength);

            elementOffset = elementOffset + valueOffset + valueLength;
        }
        else {
            arrayBuilder.appendNull();
        }
        // move onto the next null byte
        if (7 == (i % 8)) {
            nullByteCur++;
        }
    }
    builder.closeEntry();
}
 
Example 12
Source File: StructEncoding.java    From presto with Apache License 2.0 5 votes vote down vote up
@Override
public void decodeValueInto(BlockBuilder builder, Slice slice, int offset, int length)
{
    int fieldId = 0;
    int nullByte = 0;
    int elementOffset = offset;
    BlockBuilder rowBuilder = builder.beginBlockEntry();
    while (fieldId < structFields.size() && elementOffset < offset + length) {
        BinaryColumnEncoding field = structFields.get(fieldId);

        // null byte prefixes every 8 fields
        if ((fieldId % 8) == 0) {
            nullByte = slice.getByte(elementOffset);
            elementOffset++;
        }

        // read field
        if ((nullByte & (1 << (fieldId % 8))) != 0) {
            int valueOffset = field.getValueOffset(slice, elementOffset);
            int valueLength = field.getValueLength(slice, elementOffset);

            field.decodeValueInto(rowBuilder, slice, elementOffset + valueOffset, valueLength);

            elementOffset = elementOffset + valueOffset + valueLength;
        }
        else {
            rowBuilder.appendNull();
        }
        fieldId++;
    }

    // Some times a struct does not have all fields written
    // so we fill with nulls
    while (fieldId < structFields.size()) {
        rowBuilder.appendNull();
        fieldId++;
    }

    builder.closeEntry();
}
 
Example 13
Source File: HiveBucketingV1.java    From presto with Apache License 2.0 5 votes vote down vote up
private static int hashBytes(int initialValue, Slice bytes)
{
    int result = initialValue;
    for (int i = 0; i < bytes.length(); i++) {
        result = result * 31 + bytes.getByte(i);
    }
    return result;
}
 
Example 14
Source File: DecimalEncoding.java    From presto with Apache License 2.0 5 votes vote down vote up
private BigDecimal parseBigDecimal(Slice slice, int offset, int length)
{
    checkArgument(length < buffer.length);
    for (int i = 0; i < length; i++) {
        buffer[i] = (char) slice.getByte(offset + i);
    }

    BigDecimal decimal = new BigDecimal(buffer, 0, length);

    checkState(decimal.scale() <= type.getScale(), "Read decimal value scale larger than column scale");
    decimal = decimal.setScale(type.getScale(), HALF_UP);
    checkState(decimal.precision() <= type.getPrecision(), "Read decimal precision larger than column precision");
    return decimal;
}
 
Example 15
Source File: Utils.java    From hive-third-functions with Apache License 2.0 5 votes vote down vote up
static int indexOf(Slice source, Slice target, int fromIndex) {
    if (fromIndex >= source.length()) {
        return target.length() == 0 ? source.length() : -1;
    }
    if (fromIndex < 0) {
        fromIndex = 0;
    }
    if (target.length() == 0) {
        return fromIndex;
    }

    byte first = target.getByte(0);
    for (int i = fromIndex, max = source.length() - target.length(); i <= max;
         i++) {
        // Look for first byte.
        if (source.getByte(i) != first) {
            while (++i <= max && source.getByte(i) != first) {
            }
        }

        // Found first byte, now look at the rest of v2.
        if (i <= max) {
            int j = i + 1;
            int end = j + target.length() - 1;
            for (int k = 1; j < end && source.getByte(j) == target.getByte(k); j++, k++) {
            }

            if (j == end) {
                return i;  // found whole array
            }
        }
    }
    return -1;
}
 
Example 16
Source File: MapEncoding.java    From presto with Apache License 2.0 5 votes vote down vote up
@Override
public void decodeValueInto(int depth, BlockBuilder builder, Slice slice, int offset, int length)
        throws RcFileCorruptionException
{
    byte elementSeparator = getSeparator(depth);
    byte keyValueSeparator = getSeparator(depth + 1);
    int end = offset + length;

    BlockBuilder mapBuilder = builder.beginBlockEntry();
    if (length > 0) {
        int elementOffset = offset;
        int keyValueSeparatorPosition = -1;
        while (offset < end) {
            byte currentByte = slice.getByte(offset);
            if (currentByte == elementSeparator) {
                decodeEntryInto(depth, mapBuilder, slice, elementOffset, offset - elementOffset, keyValueSeparatorPosition);
                elementOffset = offset + 1;
                keyValueSeparatorPosition = -1;
            }
            else if (currentByte == keyValueSeparator && keyValueSeparatorPosition == -1) {
                keyValueSeparatorPosition = offset;
            }
            else if (isEscapeByte(currentByte) && offset + 1 < length) {
                // ignore the char after escape_char
                offset++;
            }
            offset++;
        }
        decodeEntryInto(depth, mapBuilder, slice, elementOffset, offset - elementOffset, keyValueSeparatorPosition);
    }
    builder.closeEntry();
}
 
Example 17
Source File: CharType.java    From presto with Apache License 2.0 5 votes vote down vote up
@Override
public void writeSlice(BlockBuilder blockBuilder, Slice value, int offset, int length)
{
    if (length > 0 && value.getByte(offset + length - 1) == ' ') {
        throw new IllegalArgumentException("Slice representing Char should not have trailing spaces");
    }
    blockBuilder.writeBytes(value, offset, length).closeEntry();
}
 
Example 18
Source File: SliceUtils.java    From hive-third-functions with Apache License 2.0 4 votes vote down vote up
static void appendReplacement(SliceOutput so, Slice replacement, Matcher matcher) {
    int idx = 0;

    // Handle the following items:
    // 1. ${name};
    // 2. $0, $1, $123 (group 123, if exists; or group 12, if exists; or group 1);
    // 3. \\, \$, \t (literal 't').
    // 4. Anything that doesn't starts with \ or $ is considered regular bytes
    while (idx < replacement.length()) {
        byte nextByte = replacement.getByte(idx);
        if (nextByte == '$') {
            idx++;
            if (idx == replacement.length()) {
                throw new IllegalArgumentException("Illegal replacement sequence: " + replacement.toStringUtf8());
            }
            nextByte = replacement.getByte(idx);
            int backref;
            if (nextByte == '{') { // case 1 in the above comment
                idx++;
                int startCursor = idx;
                while (idx < replacement.length()) {
                    nextByte = replacement.getByte(idx);
                    if (nextByte == '}') {
                        break;
                    }
                    idx++;
                }
                String groupName = replacement.slice(startCursor, idx - startCursor).toStringUtf8();
                Integer namedGroupIndex = matcher.pattern().re2().namedGroupIndexes.get(groupName);
                if (namedGroupIndex == null) {
                    throw new IndexOutOfBoundsException("Illegal replacement sequence: unknown group " + groupName);
                }
                backref = namedGroupIndex;
                idx++;
            } else { // case 2 in the above comment
                backref = nextByte - '0';
                if (backref < 0 || backref > 9) {
                    throw new IllegalArgumentException("Illegal replacement sequence: " + replacement.toStringUtf8());
                }
                if (matcher.groupCount() < backref) {
                    throw new IndexOutOfBoundsException("Illegal replacement sequence: unknown group " + backref);
                }
                idx++;
                while (idx < replacement.length()) { // Adaptive group number: find largest group num that is not greater than actual number of groups
                    int nextDigit = replacement.getByte(idx) - '0';
                    if (nextDigit < 0 || nextDigit > 9) {
                        break;
                    }
                    int newBackref = (backref * 10) + nextDigit;
                    if (matcher.groupCount() < newBackref) {
                        break;
                    }
                    backref = newBackref;
                    idx++;
                }
            }
            Slice group = matcher.group(backref);
            if (group != null) {
                so.writeBytes(group);
            }
        } else { // case 3 and 4 in the above comment
            if (nextByte == '\\') {
                idx++;
                if (idx == replacement.length()) {
                    throw new IllegalArgumentException("Illegal replacement sequence: " + replacement.toStringUtf8());
                }
                nextByte = replacement.getByte(idx);
            }
            so.appendByte(nextByte);
            idx++;
        }
    }
}
 
Example 19
Source File: MapEncoding.java    From presto with Apache License 2.0 4 votes vote down vote up
@Override
public void decodeValueInto(BlockBuilder builder, Slice slice, int offset, int length)
{
    // entries in list
    int entries = toIntExact(readVInt(slice, offset));
    offset += decodeVIntSize(slice.getByte(offset));

    // null bytes
    int nullByteCur = offset;
    int nullByteEnd = offset + (entries * 2 + 7) / 8;

    // read elements starting after null bytes
    int elementOffset = nullByteEnd;
    BlockBuilder mapBuilder = builder.beginBlockEntry();
    for (int i = 0; i < entries; i++) {
        // read key
        boolean nullKey;
        if ((slice.getByte(nullByteCur) & (1 << ((i * 2) % 8))) != 0) {
            int keyOffset = keyReader.getValueOffset(slice, elementOffset);
            int keyLength = keyReader.getValueLength(slice, elementOffset);

            keyReader.decodeValueInto(mapBuilder, slice, elementOffset + keyOffset, keyLength);
            nullKey = false;

            elementOffset = elementOffset + keyOffset + keyLength;
        }
        else {
            nullKey = true;
        }

        // ignore entries with a null key

        // read value
        if ((slice.getByte(nullByteCur) & (1 << ((i * 2 + 1) % 8))) != 0) {
            int valueOffset = valueReader.getValueOffset(slice, elementOffset);
            int valueLength = valueReader.getValueLength(slice, elementOffset);

            // ignore entries with a null key
            if (!nullKey) {
                valueReader.decodeValueInto(mapBuilder, slice, elementOffset + valueOffset, valueLength);
            }

            elementOffset = elementOffset + valueOffset + valueLength;
        }
        else {
            // ignore entries with a null key
            if (!nullKey) {
                mapBuilder.appendNull();
            }
        }

        // move onto the next null byte
        if (3 == (i % 4)) {
            nullByteCur++;
        }
    }
    builder.closeEntry();
}
 
Example 20
Source File: JoniRegexpFunctions.java    From presto with Apache License 2.0 4 votes vote down vote up
private static void appendReplacement(SliceOutput result, Slice source, Regex pattern, Region region, Slice replacement)
{
    // Handle the following items:
    // 1. ${name};
    // 2. $0, $1, $123 (group 123, if exists; or group 12, if exists; or group 1);
    // 3. \\, \$, \t (literal 't').
    // 4. Anything that doesn't starts with \ or $ is considered regular bytes

    int idx = 0;

    while (idx < replacement.length()) {
        byte nextByte = replacement.getByte(idx);
        if (nextByte == '$') {
            idx++;
            if (idx == replacement.length()) { // not using checkArgument because `.toStringUtf8` is expensive
                throw new PrestoException(INVALID_FUNCTION_ARGUMENT, "Illegal replacement sequence: " + replacement.toStringUtf8());
            }
            nextByte = replacement.getByte(idx);
            int backref;
            if (nextByte == '{') { // case 1 in the above comment
                idx++;
                int startCursor = idx;
                while (idx < replacement.length()) {
                    nextByte = replacement.getByte(idx);
                    if (nextByte == '}') {
                        break;
                    }
                    idx++;
                }
                byte[] groupName = replacement.getBytes(startCursor, idx - startCursor);
                try {
                    backref = pattern.nameToBackrefNumber(groupName, 0, groupName.length, region);
                }
                catch (ValueException e) {
                    throw new PrestoException(INVALID_FUNCTION_ARGUMENT, "Illegal replacement sequence: unknown group { " + new String(groupName, StandardCharsets.UTF_8) + " }");
                }
                idx++;
            }
            else { // case 2 in the above comment
                backref = nextByte - '0';
                if (backref < 0 || backref > 9) { // not using checkArgument because `.toStringUtf8` is expensive
                    throw new PrestoException(INVALID_FUNCTION_ARGUMENT, "Illegal replacement sequence: " + replacement.toStringUtf8());
                }
                if (region.numRegs <= backref) {
                    throw new PrestoException(INVALID_FUNCTION_ARGUMENT, "Illegal replacement sequence: unknown group " + backref);
                }
                idx++;
                while (idx < replacement.length()) { // Adaptive group number: find largest group num that is not greater than actual number of groups
                    int nextDigit = replacement.getByte(idx) - '0';
                    if (nextDigit < 0 || nextDigit > 9) {
                        break;
                    }
                    int newBackref = (backref * 10) + nextDigit;
                    if (region.numRegs <= newBackref) {
                        break;
                    }
                    backref = newBackref;
                    idx++;
                }
            }
            int beg = region.beg[backref];
            int end = region.end[backref];
            if (beg != -1 && end != -1) { // the specific group doesn't exist in the current match, skip
                result.appendBytes(source.slice(beg, end - beg));
            }
        }
        else { // case 3 and 4 in the above comment
            if (nextByte == '\\') {
                idx++;
                if (idx == replacement.length()) { // not using checkArgument because `.toStringUtf8` is expensive
                    throw new PrestoException(INVALID_FUNCTION_ARGUMENT, "Illegal replacement sequence: " + replacement.toStringUtf8());
                }
                nextByte = replacement.getByte(idx);
            }
            result.appendByte(nextByte);
            idx++;
        }
    }
}