Skip to content

[C++][Parquet] DeltaBitPackDecoder expects all miniblock bitwidths to be present for the last block #14923

Closed

Description

@rok

Describe the bug, including details regarding any error messages, version, and platform.

This is a follow-up to this discussion.
Arrow's reader is not compliant with the spec, and requires all miniblock bitwidth values of the last block even if not necessary. This forces arrow writer to write all miniblock bitwidths to be able to roundtrip which is not a big issue. However it does mean Arrow reader would not be able to read bitpacked buffer written by another parquet writer that does not write all miniblock bitwidths.

Component(s)

C++, Parquet

Activity

  1. pitrou commented on Dec 13, 2022

    @pitrou
    Member
  2. added this to the 11.0.0 milestone on Dec 13, 2022
  3. rok commented on Dec 13, 2022

    @rok
    MemberAuthor

    The decoder in question is here.

  4. pitrou commented on Dec 13, 2022

    @pitrou
    Member

    @rok Is it possible for you to generate a small data page that would exhibit the issue?

  5. rok commented on Dec 13, 2022

    @rok
    MemberAuthor

    @pitrou sure. By that you mean a single parquet file?

  6. pitrou commented on Dec 13, 2022

    @pitrou
    Member

    That would work, but it can also be a small piece of data suitable for Decoder::SetData(...)

  7. rok commented on Dec 20, 2022

    @rok
    MemberAuthor

    Sorry this took a while, here's a test with examples generated with this->Execute(65, 1);:

    class DeltaBitPackEncoding : public TestArrowBuilderDecoding {
     public:
      void SetupEncoderDecoder() override {}
    };
    
    TEST_F(DeltaBitPackEncoding, Example) {
      std::shared_ptr<ColumnDescriptor> descr_ = ExampleDescr<Int32Type>();
      auto decoder = MakeTypedDecoder<Int32Type>(Encoding::DELTA_BINARY_PACKED, descr_.get());
      using c_type = parquet::Int32Type::c_type;
    
      unsigned char good_data [] = "\200\001\004A\237\224\316\362\r\242\220\203-  ";
      int encode_buffer_size = 273;
      int num_values = 65;
      std::vector<uint8_t> output_bytes = std::vector<uint8_t>(num_values * sizeof(c_type));
      auto decode_buf = reinterpret_cast<c_type*>(output_bytes.data());
      decoder->SetData(num_values, &good_data[0], encode_buffer_size);
      int values_decoded = decoder->Decode(decode_buf, num_values);
      ASSERT_EQ(num_values, values_decoded);
    
      unsigned char bad_data[] =
          "\200\001\004A\237\224\316\362\r\242\220\203-  "
          "\245\245\304;`\210'\313\r\270D\316\306h㖀~\372\255\360A\254}\211L\343\373_"
          "\034®\312Y\036\233<\203\035P\202)\307Y\356\327\024\302!\232\036,"
          "\271\b\331\353\037e\333\332\315Crm\203\350בOo\001\347\305Z\203G\037\263Y\254\366_"
          "\"\v\276\242Y\002\374\300\226\231\252C\240\363ۙ\r\334E\314\f\002\255\227\273\307"
          "\305'\"\033\235\374\250\243\244F\266\254\350\203\304U\036X\331&\210/"
          "\037\322\321s.\031e/"
          "\232\340\363\366\306\030\243,5\337\031\005bw\021\017wj\003#Q`\371ʉ\323\300+~="
          "\232W\232\374p\336$\022\211VQ\237>\v1gە'\224\207\262f\247h\363A!"
          "\255\271f\026\274\033_\333)4";
      output_bytes = std::vector<uint8_t>(num_values * sizeof(c_type));
      decode_buf = reinterpret_cast<c_type*>(output_bytes.data());
      decoder->SetData(num_values, &bad_data[0], encode_buffer_size);
    }
    
  8. mapleFU commented on Dec 23, 2022

    @mapleFU
    Member

    Sorry this took a while, here's a test with examples generated with this->Execute(65, 1);:

    Hi rok, can you provide the environment you're using and the error messages? Since that I cannot reproduce the fatal on my environment (ARROW_SIMD_LEVEL=NEON in aarch64) @rok

  9. rok commented on Dec 23, 2022

    @rok
    MemberAuthor

    Hey @mapleFU. I'm on Ryzen 7 Linux desko 5.15.0-56-generic #62-Ubuntu SMP Tue Nov 22 19:54:14 UTC 2022 x86_64 x86_64 x86_64 GNU/Linux.

    If you just apply the change below and run arrow test suite you should hit the same issue.

    diff --git a/cpp/src/parquet/encoding.cc b/cpp/src/parquet/encoding.cc
    index 720b6fcb1..aef079da3 100644
    --- a/cpp/src/parquet/encoding.cc
    +++ b/cpp/src/parquet/encoding.cc
    @@ -2248,9 +2248,6 @@ void DeltaBitPackEncoder<DType>::FlushBlock() {
       // needed to store the values, the bytes storing the bit widths of the unneeded
       // miniblocks are still present, their value should be zero, but readers must accept
       // arbitrary values as well.
    -  for (uint32_t i = num_miniblocks; i < mini_blocks_per_block_; i++) {
    -    bit_width_data[i] = 0;
    -  }
       DCHECK_EQ(values_current_block_, 0);
  10. mapleFU commented on Dec 28, 2022

    @mapleFU
    Member

    Cannot reproduce this problem on my macos, guess maybe the problem comes from SIMD unpack or other. I will keep the diff and try it on my pc tonight.

    diff --git a/cpp/src/parquet/encoding.cc b/cpp/src/parquet/encoding.cc
    index 9761dfd30..06cf385cc 100644
    --- a/cpp/src/parquet/encoding.cc
    +++ b/cpp/src/parquet/encoding.cc
    @@ -2248,9 +2248,9 @@ void DeltaBitPackEncoder<DType>::FlushBlock() {
       // needed to store the values, the bytes storing the bit widths of the unneeded
       // miniblocks are still present, their value should be zero, but readers must accept
       // arbitrary values as well.
    -  for (uint32_t i = num_miniblocks; i < mini_blocks_per_block_; i++) {
    -    bit_width_data[i] = 0;
    -  }
    +  // for (uint32_t i = num_miniblocks; i < mini_blocks_per_block_; i++) {
    +  //  bit_width_data[i] = 0;
    +  // }
       DCHECK_EQ(values_current_block_, 0);
     
       bit_writer_.Flush();
    diff --git a/cpp/src/parquet/encoding_test.cc b/cpp/src/parquet/encoding_test.cc
    index 3b4cafab8..26a5c8d5b 100644
    --- a/cpp/src/parquet/encoding_test.cc
    +++ b/cpp/src/parquet/encoding_test.cc
    @@ -1057,6 +1057,42 @@ TEST_F(DictEncoding, CheckDecodeIndicesNoNulls) {
       CheckDict(actual_num_values, *builder);
     }
     
    +class DeltaBitPackEncoding : public TestArrowBuilderDecoding {
    + public:
    +  void SetupEncoderDecoder() override {}
    +};
    +
    +TEST_F(DeltaBitPackEncoding, Example) {
    +  std::shared_ptr<ColumnDescriptor> descr_ = ExampleDescr<Int32Type>();
    +  auto decoder = MakeTypedDecoder<Int32Type>(Encoding::DELTA_BINARY_PACKED, descr_.get());
    +  using c_type = parquet::Int32Type::c_type;
    +
    +  unsigned char good_data [] = "\200\001\004A\237\224\316\362\r\242\220\203-  ";
    +  int encode_buffer_size = 273;
    +  int num_values = 65;
    +  std::vector<uint8_t> output_bytes = std::vector<uint8_t>(num_values * sizeof(c_type));
    +  auto decode_buf = reinterpret_cast<c_type*>(output_bytes.data());
    +  decoder->SetData(num_values, &good_data[0], encode_buffer_size);
    +  int values_decoded = decoder->Decode(decode_buf, num_values);
    +  ASSERT_EQ(num_values, values_decoded);
    +
    +  unsigned char bad_data[] =
    +      "\200\001\004A\237\224\316\362\r\242\220\203-  "
    +      "\245\245\304;`\210'\313\r\270D\316\306h㖀~\372\255\360A\254}\211L\343\373_"
    +      "\034®\312Y\036\233<\203\035P\202)\307Y\356\327\024\302!\232\036,"
    +      "\271\b\331\353\037e\333\332\315Crm\203\350בOo\001\347\305Z\203G\037\263Y\254\366_"
    +      "\"\v\276\242Y\002\374\300\226\231\252C\240\363ۙ\r\334E\314\f\002\255\227\273\307"
    +      "\305'\"\033\235\374\250\243\244F\266\254\350\203\304U\036X\331&\210/"
    +      "\037\322\321s.\031e/"
    +      "\232\340\363\366\306\030\243,5\337\031\005bw\021\017wj\003#Q`\371ʉ\323\300+~="
    +      "\232W\232\374p\336$\022\211VQ\237>\v1gە'\224\207\262f\247h\363A!"
    +      "\255\271f\026\274\033_\333)4";
    +  output_bytes = std::vector<uint8_t>(num_values * sizeof(c_type));
    +  decode_buf = reinterpret_cast<c_type*>(output_bytes.data());
    +  decoder->SetData(num_values, &bad_data[0], encode_buffer_size);
    +}
    +
    +
     // ----------------------------------------------------------------------
     // BYTE_STREAM_SPLIT encode/decode tests.
     
    @@ -1388,6 +1424,7 @@ TYPED_TEST(TestDeltaBitPackEncoding, BasicRoundTrip) {
       ASSERT_NO_FATAL_FAILURE(
           this->Execute((values_per_mini_block * values_per_block) + 1, 10));
       ASSERT_NO_FATAL_FAILURE(this->Execute(0, 0));
    +  ASSERT_NO_FATAL_FAILURE(this->Execute(65, 1));
       ASSERT_NO_FATAL_FAILURE(this->ExecuteSpaced(
           /*nvalues*/ 1234, /*repeats*/ 1, /*valid_bits_offset*/ 64,
           /*null_probability*/ 0.1));
  11. mapleFU commented on Dec 28, 2022

    @mapleFU
    Member

    @rok I reproduce this problem on my pc by applying the change below:

    -  for (uint32_t i = num_miniblocks; i < mini_blocks_per_block_; i++) {
    -    bit_width_data[i] = 0;
    -  }
    +  // for (uint32_t i = num_miniblocks; i < mini_blocks_per_block_; i++) {
    +  //  bit_width_data[i] = 0;
    +  // }

    But I think the code just break our standard, by unuse bit_width_data[i] = 0;, we will leak some mini-blocks, the real code would be:

     for (uint32_t i = num_miniblocks; i < mini_blocks_per_block_; i++) {
        bit_width_data[i] = 1 /* or other random bit width for miniblock */;
        for (uint32_t j = 0; j < values_per_mini_block_; j++) {
           bit_writer_.PutValue(0, bit_width_data[i]);
        }
     }

    And previous case would lead to:

    • write less bits than expected for the last block
    • Last block bit_width_data random caused by uninitialized memory

    So I think here is no bug. If I'm wrong, please correct me @rok

  12. mapleFU commented on Dec 28, 2022

    @mapleFU
    Member

    And after gothrough the code in DeltaBitPackDecoder, I found it a bit hacking for border handling. Get internal will have:

      int GetInternal(T* buffer, int max_values) {
        max_values = static_cast<int>(std::min<int64_t>(max_values, total_value_count_));
        if (max_values == 0) {
          return 0;
        }

    So, once it reaches the last block, it will just read the last mini-block with data, the remaining buffer will not be read or checked.

  13. rok commented on Dec 28, 2022

    @rok
    MemberAuthor

    @mapleFU I am worried about this part of the spec:

    If, in the last block, less than miniblocks are needed to store the values, the bytes storing the bit widths of the unneeded miniblocks are still present, their value should be zero, but readers must accept arbitrary values as well.

    I'm not sure how other encoder authors would interpret this, but I expected that uninitialized bit_width_data[i] would produce a random int that wouldn't cause problems roundtripping. My interpretation is probably wrong. Maybe we should propose to make the spec clearer or stricter?

    So, once it reaches the last block, it will just read the last mini-block with data, the remaining buffer will not be read or checked.

    That sounds like a good idea!

  14. mapleFU commented on Dec 29, 2022

    @mapleFU
    Member

    @mapleFU I am worried about this part of the spec:
    I'm not sure how other encoder authors would interpret this, but I expected that uninitialized bit_width_data[i] would produce a random int that wouldn't cause problems roundtripping. My interpretation is probably wrong. Maybe we should propose to make the spec clearer or stricter?

    Hi, I think the arbitrary value means the bit_width could be 1, 2... But once the bit width is not zero, the miniblock should have corresbond bits.

    And I met the exceptions because:

    • The bit_width is greater than 32

    I didn't meet the case that it reaches EOF, because the implemention already handle this cases.

    @rok

  15. pitrou commented on Jan 3, 2023

    @pitrou
    Member

    @mapleFU Please read the spec again:

    If, in the last block, less than miniblocks are needed to store the values, the bytes storing the bit widths of the unneeded miniblocks are still present, their value should be zero, but readers must accept arbitrary values as well. There are no additional padding bytes for the miniblock bodies though, as if their bit widths were 0 (regardless of the actual byte values). The reader knows when to stop reading by keeping track of the number of values read.

  16. mapleFU commented on Jan 3, 2023

    @mapleFU
    Member

    Damn...It's really trickey, maybe I can see how parquet-mr implements it.

    Seems that DELTA_BINARY_PACK is really a bug and ub creator

  17. pitrou commented on Jan 3, 2023

    @pitrou
    Member

    It should actually be quite simple:

    diff --git a/cpp/src/parquet/encoding.cc b/cpp/src/parquet/encoding.cc
    index b761de69d..6f7e1e9e0 100644
    --- a/cpp/src/parquet/encoding.cc
    +++ b/cpp/src/parquet/encoding.cc
    @@ -2454,13 +2454,17 @@ class DeltaBitPackDecoder : public DecoderImpl, virtual public TypedDecoder<DTyp
     
         // read the bitwidth of each miniblock
         uint8_t* bit_width_data = delta_bit_widths_->mutable_data();
    +    uint32_t current_num_values = 0;
    +
         for (uint32_t i = 0; i < mini_blocks_per_block_; ++i) {
           if (!decoder_->GetAligned<uint8_t>(1, bit_width_data + i)) {
             ParquetException::EofException();
           }
    -      if (bit_width_data[i] > kMaxDeltaBitWidth) {
    +      if (ARROW_PREDICT_FALSE(bit_width_data[i] > kMaxDeltaBitWidth &&
    +                              current_num_values < total_value_count_)) {
             throw ParquetException("delta bit width larger than integer bit width");
           }
    +      current_num_values += values_per_mini_block_;
         }
         mini_block_idx_ = 0;
         delta_bit_width_ = bit_width_data[0];
  18. mapleFU commented on Jan 3, 2023

    @mapleFU
    Member

    The code looks simple and great. parquet-mr will not check the bit-length. It's really trickey :(

  19. pitrou commented on Jan 3, 2023

    @pitrou
    Member

    parquet-mr will not check the bit-length

    What do you mean?

  20. mapleFU commented on Jan 3, 2023

    @mapleFU
    Member

    The code is like:

      private void loadNewBlockToBuffer() throws IOException {
        try {
          minDeltaInCurrentBlock = BytesUtils.readZigZagVarLong(in);
        } catch (IOException e) {
          throw new ParquetDecodingException("can not read min delta in current block", e);
        }
    
        readBitWidthsForMiniBlocks();
    
        // mini block is atomic for reading, we read a mini block when there are more values left
        int i;
        for (i = 0; i < config.miniBlockNumInABlock && valuesBuffered < totalValueCount; i++) {
          BytePackerForLong packer = Packer.LITTLE_ENDIAN.newBytePackerForLong(bitWidths[i]);
          unpackMiniBlock(packer);
        }
    
        //calculate values from deltas unpacked for current block
        int valueUnpacked=i*config.miniBlockSizeInValues;
        for (int j = valuesBuffered-valueUnpacked; j < valuesBuffered; j++) {
          int index = j;
          valuesBuffer[index] += minDeltaInCurrentBlock + valuesBuffer[index - 1];
        }
      }
    
      private void readBitWidthsForMiniBlocks() {
        for (int i = 0; i < config.miniBlockNumInABlock; i++) {
          try {
            bitWidths[i] = BytesUtils.readIntLittleEndianOnOneByte(in);
          } catch (IOException e) {
            throw new ParquetDecodingException("Can not decode bitwidth in block header", e);
          }
        }
      }
    
      private int[] bitWidths;
    • when readBitWidthsForMiniBlocks, it will not check it's size
    • Only when valuesBuffered < totalValueCount, it will use bitWidths and unpack mini block

    I think your fixing is much better, let's make it into our codebase :)

  21. raulcd commented on Jan 11, 2023

    @raulcd
    Member

    I am going to remove the milestone in preparation for the release, please if this is a blocker add the label Priority: Blocker

  22. removed this from the 11.0.0 milestone on Jan 11, 2023
  23. added a commit that references this issue on Jan 18, 2023
  24. added this to the 12.0.0 milestone on Jan 18, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions