marin-ma commented on code in PR #6475:
URL: https://github.com/apache/incubator-gluten/pull/6475#discussion_r2228842835


##########
cpp/velox/shuffle/VeloxSortShuffleWriter.cc:
##########
@@ -0,0 +1,314 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *    http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include "shuffle/VeloxSortShuffleWriter.h"
+
+#include <arrow/io/memory.h>
+
+#include "memory/ArrowMemory.h"
+#include "memory/VeloxColumnarBatch.h"
+#include "utils/Common.h"
+#include "utils/Timer.h"
+
+namespace gluten {
+
+namespace {
+constexpr uint32_t kMaskLower27Bits = (1 << 27) - 1;
+constexpr uint64_t kMaskLower40Bits = (1UL << 40) - 1;
+
+uint64_t toCompactRowId(uint32_t partitionId, uint32_t pageNumber, uint32_t 
offsetInPage) {
+  // |63 partitionId(24) |39 inputIndex(13) |26 rowIndex(27) |
+  return (uint64_t)partitionId << 40 | (uint64_t)pageNumber << 27 | 
offsetInPage;
+}
+
+uint32_t extractPartitionId(uint64_t compactRowId) {
+  return (uint32_t)(compactRowId >> 40);
+}
+
+std::pair<uint32_t, uint32_t> extractPageNumberAndOffset(uint64_t 
compactRowId) {
+  return {(compactRowId & kMaskLower40Bits) >> 27, compactRowId & 
kMaskLower27Bits};
+}
+} // namespace
+
+arrow::Result<std::shared_ptr<VeloxShuffleWriter>> 
VeloxSortShuffleWriter::create(
+    uint32_t numPartitions,
+    std::unique_ptr<PartitionWriter> partitionWriter,
+    ShuffleWriterOptions options,
+    std::shared_ptr<facebook::velox::memory::MemoryPool> veloxPool,
+    arrow::MemoryPool* arrowPool) {
+  std::shared_ptr<VeloxSortShuffleWriter> writer(new VeloxSortShuffleWriter(
+      numPartitions, std::move(partitionWriter), std::move(options), 
std::move(veloxPool), arrowPool));
+  RETURN_NOT_OK(writer->init());
+  return writer;
+}
+
+VeloxSortShuffleWriter::VeloxSortShuffleWriter(
+    uint32_t numPartitions,
+    std::unique_ptr<PartitionWriter> partitionWriter,
+    ShuffleWriterOptions options,
+    std::shared_ptr<facebook::velox::memory::MemoryPool> veloxPool,
+    arrow::MemoryPool* pool)
+    : VeloxShuffleWriter(numPartitions, std::move(partitionWriter), 
std::move(options), std::move(veloxPool), pool),
+      
allocator_{std::make_unique<facebook::velox::HashStringAllocator>(veloxPool_.get())},
+      array_{SortArray{Allocator(allocator_.get())}} {}
+
+arrow::Status VeloxSortShuffleWriter::write(std::shared_ptr<ColumnarBatch> cb, 
int64_t memLimit) {
+  ARROW_ASSIGN_OR_RAISE(auto rv, getPeeledRowVector(cb));
+  initRowType(rv);
+  RETURN_NOT_OK(insert(rv, memLimit));
+  return arrow::Status::OK();
+}
+
+arrow::Status VeloxSortShuffleWriter::stop() {
+  ARROW_RETURN_IF(evictState_ == EvictState::kUnevictable, 
arrow::Status::Invalid("Unevictable state in stop."));
+
+  EvictGuard evictGuard{evictState_};
+
+  stopped_ = true;
+  RETURN_NOT_OK(evictAllPartitions());
+  RETURN_NOT_OK(partitionWriter_->stop(&metrics_));
+  return arrow::Status::OK();
+}
+
+arrow::Status VeloxSortShuffleWriter::reclaimFixedSize(int64_t size, int64_t* 
actual) {
+  if (evictState_ == EvictState::kUnevictable || offset_ == 0) {
+    *actual = 0;
+    return arrow::Status::OK();
+  }
+  EvictGuard evictGuard{evictState_};
+  auto beforeReclaim = veloxPool_->usedBytes();
+  RETURN_NOT_OK(evictAllPartitions());
+  *actual = beforeReclaim - veloxPool_->usedBytes();
+  return arrow::Status::OK();
+}
+
+arrow::Status VeloxSortShuffleWriter::init() {
+  ARROW_RETURN_IF(
+      options_.partitioning == Partitioning::kSingle,
+      arrow::Status::Invalid("VeloxSortShuffleWriter doesn't support single 
partition."));

Review Comment:
   The sort shuffle writer convert the inputs from column to row, sort the rows 
on the writer side ,and convert the rows back to column on the reader side. For 
single partition, there's no sort operation. We directly write the input 
without any other operations.



-- 
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]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to