zhztheplayer commented on code in PR #5952:
URL: https://github.com/apache/incubator-gluten/pull/5952#discussion_r1694396211


##########
cpp/velox/operators/serializer/VeloxColumnarToRowConverter.cc:
##########
@@ -16,46 +16,66 @@
  */
 
 #include "VeloxColumnarToRowConverter.h"
+#include <velox/common/base/SuccinctPrinter.h>
+#include <cstdint>
 
 #include "memory/VeloxColumnarBatch.h"
+#include "utils/exception.h"
 #include "velox/row/UnsafeRowDeserializers.h"
 #include "velox/row/UnsafeRowFast.h"
 
 using namespace facebook;
 
 namespace gluten {
 
-void VeloxColumnarToRowConverter::refreshStates(facebook::velox::RowVectorPtr 
rowVector) {
-  numRows_ = rowVector->size();
+void VeloxColumnarToRowConverter::refreshStates(
+    facebook::velox::RowVectorPtr rowVector,
+    int64_t rowId,
+    int64_t memoryThreshold) {
+  auto vectorLength = rowVector->size();
   numCols_ = rowVector->childrenSize();
 
   fast_ = std::make_unique<velox::row::UnsafeRowFast>(rowVector);
 
   size_t totalMemorySize = 0;
   if (auto fixedRowSize = 
velox::row::UnsafeRowFast::fixedRowSize(velox::asRowType(rowVector->type()))) {
-    totalMemorySize += fixedRowSize.value() * numRows_;
+    if (memoryThreshold < fixedRowSize.value()) {
+      memoryThreshold = fixedRowSize.value();
+      LOG(ERROR) << "spark.gluten.sql.columnarToRowMemoryThreshold(" + 
velox::succinctBytes(memoryThreshold) +
+              ") is too small, it can't hold even one row(" + 
velox::succinctBytes(fixedRowSize.value()) + ")";
+    }
+    auto rowSize = fixedRowSize.value();
+    numRows_ = std::min<int64_t>(memoryThreshold / rowSize, vectorLength - 
rowId);
+    totalMemorySize = rowSize * numRows_;
   } else {
-    for (auto i = 0; i < numRows_; ++i) {
-      totalMemorySize += fast_->rowSize(i);
+    int64_t i = rowId;
+    for (; i < vectorLength; ++i) {
+      auto rowSize = fast_->rowSize(i);
+      if (UNLIKELY(totalMemorySize + rowSize > memoryThreshold)) {
+        if (i == rowId) {
+          memoryThreshold = rowSize;
+          LOG(INFO) << "spark.gluten.sql.columnarToRowMemoryThreshold(" + 
velox::succinctBytes(memoryThreshold) +
+                  ") is too small, it can't hold even one row(" + 
velox::succinctBytes(rowSize) + ")";
+        }
+        break;
+      } else {
+        totalMemorySize += rowSize;
+      }
     }
+    numRows_ = i - rowId;
   }
 
   if (veloxBuffers_ == nullptr) {

Review Comment:
   Passing a memory threshold for a iterator's `next` operation is not an 
optimal way if the iterator holds some memory across invocations. I am afraid 
we should redesign the code more or less.



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