Repository: apache/flink
File: flink-core/src/main/java/org/apache/flink/types/variant/BinaryVariantInternalBuilder.java (Java)

Code before:

    public void appendDate(int daysSinceEpoch) {
        checkCapacity(1 + 4);
        writeBuffer[writePos++] = primitiveHeader(DATE);
        writeLong(writeBuffer, writePos, daysSinceEpoch, 4);
        writePos += 4;
    }

    public void appendTimestampLtz(long microsSinceEpoch) {
        checkCapacity(1 + 8);
        writeBuffer[writePos++] = primitiveHeader(TIMESTAMP_LTZ);
        writeLong(writeBuffer, writePos, microsSinceEpoch, 8);
        writePos += 8;
    }

    public void appendTimestamp(long microsSinceEpoch) {
        checkCapacity(1 + 8);
        writeBuffer[writePos++] = primitiveHeader(TIMESTAMP);
        writeLong(writeBuffer, writePos, microsSinceEpoch, 8);
        writePos += 8;
    }

    public void appendFloat(float f) {
        checkCapacity(1 + 4);
        writeBuffer[writePos++] = primitiveHeader(FLOAT);
        writeLong(writeBuffer, writePos, Float.floatToIntBits(f), 8);
        writePos += 4;
    }

    public void appendBinary(byte[] binary) {
        checkCapacity(1 + U32_SIZE + binary.length);
        writeBuffer[writePos++] = primitiveHeader(BINARY);
        writeLong(writeBuffer, writePos, binary.length, U32_SIZE);
        writePos += U32_SIZE;
        System.arraycopy(binary, 0, writeBuffer, writePos, binary.length);
        writePos += binary.length;
    }

    // Add a key to the variant dictionary. If the key already exists, the dictionary is not
    // modified.
    // In either case, return the id of the key.
    public int addKey(String key) {
        int id;
        if (dictionary.containsKey(key)) {
            id = dictionary.get(key);
        } else {
            id = dictionaryKeys.size();
            dictionary.put(key, id);
            dictionaryKeys.add(key.getBytes(StandardCharsets.UTF_8));
        }
        return id;

Review comment (bug):
Am I missing something? Long is 8 bytes and it is a writeLong.  Could it be that  `checkCapacity(1 + 4);` is incorrect and it should be  `checkCapacity(1 + 8);` Line 286 is a write long with 8 bytes

Code after:

    public void appendDate(int daysSinceEpoch) {
        checkCapacity(1 + 4);
        writeBuffer[writePos++] = primitiveHeader(DATE);
        writeLong(writeBuffer, writePos, daysSinceEpoch, 4);
        writePos += 4;
    }

    public void appendTimestampLtz(long microsSinceEpoch) {
        checkCapacity(1 + 8);
        writeBuffer[writePos++] = primitiveHeader(TIMESTAMP_LTZ);
        writeLong(writeBuffer, writePos, microsSinceEpoch, 8);
        writePos += 8;
    }

    public void appendTimestamp(long microsSinceEpoch) {
        checkCapacity(1 + 8);
        writeBuffer[writePos++] = primitiveHeader(TIMESTAMP);
        writeLong(writeBuffer, writePos, microsSinceEpoch, 8);
        writePos += 8;
    }

    public void appendFloat(float f) {
        checkCapacity(1 + 4);
        writeBuffer[writePos++] = primitiveHeader(FLOAT);
        writeLong(writeBuffer, writePos, Float.floatToIntBits(f), 4);
        writePos += 4;
    }

    public void appendBinary(byte[] binary) {
        checkCapacity(1 + U32_SIZE + binary.length);
        writeBuffer[writePos++] = primitiveHeader(BINARY);
        writeLong(writeBuffer, writePos, binary.length, U32_SIZE);
        writePos += U32_SIZE;
        System.arraycopy(binary, 0, writeBuffer, writePos, binary.length);
        writePos += binary.length;
    }

    // Add a key to the variant dictionary. If the key already exists, the dictionary is not
    // modified.
    // In either case, return the id of the key.
    public int addKey(String key) {
        int id;
        if (dictionary.containsKey(key)) {
            id = dictionary.get(key);
        } else {
            id = dictionaryKeys.size();
            dictionary.put(key, id);
            dictionaryKeys.add(key.getBytes(StandardCharsets.UTF_8));
        }
        return id;