You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@parquet.apache.org by we...@apache.org on 2016/09/03 15:11:05 UTC

parquet-cpp git commit: PARQUET-676: Fix incorrect MaxBufferSize for small bit widths

Repository: parquet-cpp
Updated Branches:
  refs/heads/master 5e524d146 -> fa6e47616


PARQUET-676: Fix incorrect MaxBufferSize for small bit widths

This function was implemented incorrectly in the original source code from Impala. The bug never presented itself because Impala allocates a much larger buffer than is required for the data page rather than using the `MaxBufferSize` value.

In a worst case scenario, the RleEncoder can result in a concatenation of short RLE runs of 8 values (the minimum for RLE). For example:

0 0 0 0 0 0 0 0 1 1 1 1 1 1 1 1 0 0 0 0 0 0 0 0 ...

In encoded form, each RLE run occupies 2 bytes (for bit width 1). Thus 1024 values with this structure and bit width 1, at least 256 bytes are required to fully encode. If these were encoded as all literal runs, you can encode up to 504 values in a literal run (plus the indicator byte). Thus, in the same case (bit width 1) only

(504 + 504 + 16)
64 + 64 + 3 = 131

bytes are required. This should also fix PARQUET-698.

Author: Wes McKinney <we...@twosigma.com>

Closes #150 from wesm/PARQUET-676 and squashes the following commits:

6ca44a8 [Wes McKinney] clang-format
e48b0bf [Wes McKinney] Fix maximum buffer sizing for hybrid RLE encoding case where short RLE runs use more space than mostly literal runs


Project: http://git-wip-us.apache.org/repos/asf/parquet-cpp/repo
Commit: http://git-wip-us.apache.org/repos/asf/parquet-cpp/commit/fa6e4761
Tree: http://git-wip-us.apache.org/repos/asf/parquet-cpp/tree/fa6e4761
Diff: http://git-wip-us.apache.org/repos/asf/parquet-cpp/diff/fa6e4761

Branch: refs/heads/master
Commit: fa6e47616e818f33d8d4774ce43ae6d62cdd59bd
Parents: 5e524d1
Author: Wes McKinney <we...@twosigma.com>
Authored: Sat Sep 3 11:10:52 2016 -0400
Committer: Wes McKinney <we...@twosigma.com>
Committed: Sat Sep 3 11:10:52 2016 -0400

----------------------------------------------------------------------
 src/parquet/column/levels-test.cc | 23 +++++++++++++++++++++++
 src/parquet/file/reader.cc        | 13 +++++--------
 src/parquet/util/rle-encoding.h   |  9 ++++++++-
 3 files changed, 36 insertions(+), 9 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/parquet-cpp/blob/fa6e4761/src/parquet/column/levels-test.cc
----------------------------------------------------------------------
diff --git a/src/parquet/column/levels-test.cc b/src/parquet/column/levels-test.cc
index b15c0af..cb7cc38 100644
--- a/src/parquet/column/levels-test.cc
+++ b/src/parquet/column/levels-test.cc
@@ -183,4 +183,27 @@ TEST(TestLevels, TestLevelsDecodeMultipleSetData) {
   }
 }
 
+TEST(TestLevelEncoder, MinimumBufferSize) {
+  // PARQUET-676, PARQUET-698
+  const int kNumToEncode = 1024;
+
+  std::vector<int16_t> levels;
+  for (int i = 0; i < kNumToEncode; ++i) {
+    if (i % 9 == 0) {
+      levels.push_back(0);
+    } else {
+      levels.push_back(1);
+    }
+  }
+
+  std::vector<uint8_t> output(
+      LevelEncoder::MaxBufferSize(Encoding::RLE, 1, kNumToEncode));
+
+  LevelEncoder encoder;
+  encoder.Init(Encoding::RLE, 1, kNumToEncode, output.data(), output.size());
+  int encode_count = encoder.Encode(kNumToEncode, levels.data());
+
+  ASSERT_EQ(kNumToEncode, encode_count);
+}
+
 }  // namespace parquet

http://git-wip-us.apache.org/repos/asf/parquet-cpp/blob/fa6e4761/src/parquet/file/reader.cc
----------------------------------------------------------------------
diff --git a/src/parquet/file/reader.cc b/src/parquet/file/reader.cc
index b6de168..50db1c0 100644
--- a/src/parquet/file/reader.cc
+++ b/src/parquet/file/reader.cc
@@ -162,8 +162,7 @@ void ParquetFileReader::DebugPrint(
       const ColumnStatistics stats = column_chunk->statistics();
 
       const ColumnDescriptor* descr = file_metadata->schema_descriptor()->Column(i);
-      stream << "Column " << i << std::endl
-             << ", values: " << column_chunk->num_values();
+      stream << "Column " << i << std::endl << ", values: " << column_chunk->num_values();
       if (column_chunk->is_stats_set()) {
         stream << ", null values: " << stats.null_count
                << ", distinct values: " << stats.distinct_count << std::endl
@@ -174,17 +173,15 @@ void ParquetFileReader::DebugPrint(
         stream << "  Statistics Not Set";
       }
       stream << std::endl
-             << "  compression: "
-             << compression_to_string(column_chunk->compression())
+             << "  compression: " << compression_to_string(column_chunk->compression())
              << ", encodings: ";
       for (auto encoding : column_chunk->encodings()) {
         stream << encoding_to_string(encoding) << " ";
       }
       stream << std::endl
-             << "  uncompressed size: "
-             << column_chunk->total_uncompressed_size()
-             << ", compressed size: "
-             << column_chunk->total_compressed_size() << std::endl;
+             << "  uncompressed size: " << column_chunk->total_uncompressed_size()
+             << ", compressed size: " << column_chunk->total_compressed_size()
+             << std::endl;
     }
 
     if (!print_values) { continue; }

http://git-wip-us.apache.org/repos/asf/parquet-cpp/blob/fa6e4761/src/parquet/util/rle-encoding.h
----------------------------------------------------------------------
diff --git a/src/parquet/util/rle-encoding.h b/src/parquet/util/rle-encoding.h
index b0fb8d1..15fd550 100644
--- a/src/parquet/util/rle-encoding.h
+++ b/src/parquet/util/rle-encoding.h
@@ -174,7 +174,14 @@ class RleEncoder {
     int bytes_per_run = BitUtil::Ceil(bit_width * MAX_VALUES_PER_LITERAL_RUN, 8.0);
     int num_runs = BitUtil::Ceil(num_values, MAX_VALUES_PER_LITERAL_RUN);
     int literal_max_size = num_runs + num_runs * bytes_per_run;
-    return std::max(MinBufferSize(bit_width), literal_max_size);
+
+    // In the very worst case scenario, the data is a concatenation of repeated
+    // runs of 8 values. Repeated run has a 1 byte varint followed by the
+    // bit-packed repeated value
+    int min_repeated_run_size = 1 + BitUtil::Ceil(bit_width, 8);
+    int repeated_max_size = BitUtil::Ceil(num_values, 8) * min_repeated_run_size;
+
+    return std::max(literal_max_size, repeated_max_size);
   }
 
   /// Encode value.  Returns true if the value fits in buffer, false otherwise.