hongzhi-gao commented on code in PR #18533:
URL: https://github.com/apache/iotdb/pull/18533#discussion_r3877518063


##########
iotdb-client/client-cpp/src/session/Session.cpp:
##########
@@ -413,6 +413,83 @@ bool SessionUtils::isTabletContainsSingleDevice(Tablet 
tablet) {
   return true;
 }
 
+static bool isColumnAllNull(const BitMap& bitMap, size_t rowSize) {
+  if (rowSize == 0) {
+    return false;
+  }
+  if (bitMap.getSize() == rowSize && bitMap.isAllMarked()) {
+    return true;
+  }
+  for (size_t row = 0; row < rowSize; row++) {
+    if (!bitMap.isMarked(row)) {
+      return false;
+    }
+  }
+  return true;
+}
+
+std::shared_ptr<const Tablet> SessionUtils::filterNullColumns(const Tablet& 
tablet) {
+  const size_t columnCount = tablet.schemas.size();
+  if (columnCount == 0 || tablet.bitMaps.size() < columnCount) {
+    return std::shared_ptr<const Tablet>(&tablet, [](const Tablet*) {});
+  }
+
+  std::vector<size_t> keptIndices;
+  keptIndices.reserve(columnCount);
+  size_t originalFieldCount = 0;
+  size_t keptFieldCount = 0;
+
+  for (size_t i = 0; i < columnCount; i++) {
+    ColumnCategory category =
+        i < tablet.columnTypes.size() ? tablet.columnTypes[i] : 
ColumnCategory::FIELD;
+    bool isField = category == ColumnCategory::FIELD;
+    if (isField) {
+      originalFieldCount++;
+    }
+    bool drop = isField && isColumnAllNull(tablet.bitMaps[i], tablet.rowSize);
+    if (drop) {
+      continue;
+    }
+    keptIndices.push_back(i);
+    if (isField) {
+      keptFieldCount++;
+    }
+  }
+
+  if (keptIndices.size() == columnCount) {
+    return std::shared_ptr<const Tablet>(&tablet, [](const Tablet*) {});
+  }
+  if (originalFieldCount > 0 && keptFieldCount == 0) {
+    return nullptr;
+  }
+  if (keptIndices.empty()) {
+    return nullptr;
+  }
+
+  std::vector<std::pair<std::string, TSDataType::TSDataType>> keptSchemas;
+  std::vector<ColumnCategory> keptColumnTypes;
+  keptSchemas.reserve(keptIndices.size());
+  keptColumnTypes.reserve(keptIndices.size());
+  for (size_t idx : keptIndices) {
+    keptSchemas.push_back(tablet.schemas[idx]);
+    keptColumnTypes.push_back(idx < tablet.columnTypes.size() ? 
tablet.columnTypes[idx]
+                                                              : 
ColumnCategory::FIELD);
+  }
+
+  auto filteredOut = std::make_shared<Tablet>(tablet.deviceId, keptSchemas, 
keptColumnTypes,
+                                              tablet.maxRowNumber, 
tablet.isAligned);
+  filteredOut->deleteColumns();

Review Comment:
   fixed



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