rok commented on code in PR #55251:
URL: https://github.com/apache/arrow/pull/55251#discussion_r4747665593


##########
cpp/src/parquet/arrow/writer.cc:
##########
@@ -135,6 +137,39 @@ Status GetSchemaMetadata(const ::arrow::Schema& schema, 
::arrow::MemoryPool* poo
   return Status::OK();
 }
 
+// Check that all chunks contain supported fixed-size lists with no missing 
values
+bool CanWriteFixedSizeLists(const std::vector<std::shared_ptr<Array>>& arrays,
+                            bool from_extension) {
+  if (arrays.empty() || arrays.front()->type_id() != 
::arrow::Type::FIXED_SIZE_LIST) {
+    return false;
+  }
+  const auto& type =
+      checked_cast<const ::arrow::FixedSizeListType&>(*arrays.front()->type());
+  if (type.list_size() <= 0) {
+    return false;
+  }
+  const auto value_type = type.value_type()->id();
+  if (!::arrow::is_fixed_width(value_type) || 
::arrow::is_dictionary(value_type)) {
+    return false;
+  }
+  for (const auto& array : arrays) {
+    // Chunks share a type, but extension types define their own equality
+    if (from_extension && !array->type()->Equals(type)) {

Review Comment:
   We can drop `from_extension` and this per-chunk `Equals` 
check—`ChunkedArray` chunks already share a type, and checking `data.type()` 
once before the loop is enough.



##########
cpp/src/parquet/column_writer.cc:
##########
@@ -1420,10 +1543,40 @@ class TypedColumnWriterImpl : public ColumnWriterImpl,
                 WriteChunk, [this]() { return num_buffered_rows_; });
   }
 
+  Status WriteArrowFixedSizeList(int32_t list_size, const Array& values,
+                                 ArrowWriteContext* ctx) override {
+    BEGIN_PARQUET_CATCH_EXCEPTIONS
+    const auto arrow_type = values.type_id();
+    if (!::arrow::is_fixed_width(arrow_type) || 
::arrow::is_dictionary(arrow_type)) {
+      return Status::Invalid(
+          "Fixed-size-list writing requires a non-dictionary fixed-width 
type");
+    }
+    const int64_t num_levels = values.length();
+    if (closed_ || content_defined_chunker_ != nullptr ||
+        descr_->max_repetition_level() != 1 || list_size <= 0 ||
+        num_levels % list_size != 0 || values.null_count() != 0) {
+      return Status::Invalid("Invalid input for fixed-size-list writing");
+    }
+    if ((fixed_size_list_size_ == 0 && num_buffered_values_ != 0) ||

Review Comment:
   Also check `rows_written_ != 0` so a generic write that already flushed a 
page can't switch to the fast path:
   ```suggestion
       if ((fixed_size_list_size_ == 0 &&
            (rows_written_ != 0 || num_buffered_values_ != 0)) ||
   ```



##########
cpp/src/parquet/arrow/reader_writer_benchmark.cc:
##########
@@ -222,6 +222,63 @@ BENCHMARK_TEMPLATE2(BM_WriteColumn, true, DoubleType);
 BENCHMARK_TEMPLATE2(BM_WriteColumn, false, BooleanType);
 BENCHMARK_TEMPLATE2(BM_WriteColumn, true, BooleanType);
 
+static void WriteFixedSizeFloatLists(::benchmark::State& state, int64_t 
num_lists) {
+  const int32_t list_size = static_cast<int32_t>(state.range(0));

Review Comment:
   Writing only 1–2 lists makes small widths (`1`–`4096`) measure 
`WriteTable`/footer overhead instead of level encoding/caching, while 
`100'000'000` allocates 400 MB. Fix total floats to `BENCHMARK_SIZE` (like the 
other benchmarks here and #55577) with a flat baseline so small widths span 
multiple pages.



##########
cpp/src/parquet/arrow/writer.cc:
##########
@@ -165,6 +200,27 @@ class ArrowColumnWriterV2 {
       } else {
         PARQUET_CATCH_NOT_OK(column_writer = row_group_writer_->NextColumn());
       }
+      if (!fixed_size_lists_.empty()) {

Review Comment:
   Maybe make the `fixed_size_lists_` vs `level_builders_` branch an explicit 
`if`/`else`. Checking `content_defined_chunking_enabled()` up front in `Make()` 
(via `FileWriterImpl::properties()`) would also let us drop `is_nullable_`, 
this lazy fallback, and the second `values()->Slice()`.



##########
cpp/src/parquet/arrow/writer.cc:
##########
@@ -298,15 +356,35 @@ class ArrowColumnWriterV2 {
       }
       values_written += chunk_write_size;
     }
-    return std::make_unique<ArrowColumnWriterV2>(std::move(builders), 
column_index,
-                                                 leaf_count, row_group_writer);
+    const bool from_extension = data.type()->id() == ::arrow::Type::EXTENSION;
+    // Buffered row groups accept more batches later, so we cannot check all 
their
+    // chunks up front and pick one writing path for the whole column
+    if (row_group_writer->buffered() || !CanWriteFixedSizeLists(arrays, 
from_extension)) {

Review Comment:
   Check `data.type()`, `!row_group_writer->buffered()`, and 
`!properties.content_defined_chunking_enabled()` before the chunk loop so 
non-eligible columns stay on the single-pass `builders` path without 
allocating/clearing `arrays`, and fast-path chunks slice `lists.values()` only 
once (falling back inline if a chunk has nulls).



##########
cpp/src/parquet/column_writer.cc:
##########
@@ -935,31 +987,53 @@ class ColumnWriterImpl {
 // return the size of the encoded buffer
 int64_t ColumnWriterImpl::RleEncodeLevels(const void* src_buffer,
                                           ResizableBuffer* dest_buffer, 
int16_t max_level,
-                                          bool include_length_prefix) {
-  // V1 DataPage includes the length of the RLE level as a prefix.
-  int32_t prefix_size = include_length_prefix ? sizeof(int32_t) : 0;
-
-  // TODO: This only works with due to some RLE specifics
-  int64_t rle_size = LevelEncoder::MaxBufferSize(Encoding::RLE, max_level,

Review Comment:
   Why remove these comments?



##########
cpp/src/arrow/util/rle_encoding_internal.h:
##########
@@ -1526,6 +1529,27 @@ inline bool RleBitPackedEncoder::Put(uint64_t value) {
   return true;
 }
 
+inline int RleBitPackedEncoder::PutRepeated(uint64_t value, int count) {
+  ARROW_DCHECK_GE(count, 0);
+  ARROW_DCHECK(bit_width_ == 64 || value < (1ULL << bit_width_));
+
+  int encoded = 0;
+  while (encoded < count && !(current_value_ == value && repeat_count_ > 8)) {
+    if (!Put(value)) {
+      return encoded;
+    }
+    ++encoded;
+  }
+  if (encoded < count && !buffer_full_) {
+    // Extend an existing repeat by adding to its count instead of visiting 
each value
+    const int added =
+        std::min(count - encoded, std::numeric_limits<int>::max() - 
repeat_count_);
+    repeat_count_ += added;
+    encoded += added;
+  }
+  return encoded;

Review Comment:
   Pulling the repeat-run check out as a fast path simplifies the loop. Also 
drop the `int::max()` clamp (`FlushRepeatedRun()` already shifts `repeat_count_ 
<< 1` and `Put()` doesn't saturate):
   ```suggestion
     if (ARROW_PREDICT_FALSE(buffer_full_) || count == 0) {
       return 0;
     }
     if (current_value_ == value && repeat_count_ > 8) {
       repeat_count_ += count;
       return count;
     }
     if (!Put(value)) {
       return 0;
     }
     int encoded = 1;
     while (encoded < count && repeat_count_ <= 8) {
       if (!Put(value)) {
         return encoded;
       }
       ++encoded;
     }
     repeat_count_ += count - encoded;
     return count;
   ```



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to