kou commented on code in PR #44252:
URL: https://github.com/apache/arrow/pull/44252#discussion_r1822016024


##########
cpp/src/arrow/record_batch.cc:
##########
@@ -465,6 +470,163 @@ Result<std::shared_ptr<RecordBatch>> 
RecordBatch::ViewOrCopyTo(
   return Make(schema_, num_rows(), std::move(copied_columns));
 }
 
+Result<std::shared_ptr<Array>> RecordBatch::MakeStatisticsArray(
+    MemoryPool* memory_pool) const {
+  auto enumerate_statistics =
+      [&](std::function<Status(int nth_statistics, bool start_new_column,
+                               std::optional<int32_t> nth_column, const char* 
key,
+                               const std::shared_ptr<DataType>& type,
+                               const ArrayStatistics::ValueType& value)>
+              yield) {
+        int nth_statistics = 0;
+        RETURN_NOT_OK(yield(nth_statistics++, true, std::nullopt,
+                            ARROW_STATISTICS_KEY_ROW_COUNT_EXACT, int64(),
+                            ArrayStatistics::ValueType{num_rows_}));
+
+        int num_fields = schema_->num_fields();
+        for (int nth_column = 0; nth_column < num_fields; ++nth_column) {
+          auto statistics = column(nth_column)->statistics();
+          if (!statistics) {
+            continue;
+          }
+
+          bool start_new_column = true;
+          if (statistics->null_count.has_value()) {
+            RETURN_NOT_OK(yield(
+                nth_statistics++, start_new_column, 
std::optional<int32_t>(nth_column),
+                ARROW_STATISTICS_KEY_NULL_COUNT_EXACT, int64(),
+                ArrayStatistics::ValueType{statistics->null_count.value()}));
+            start_new_column = false;
+          }
+
+          if (statistics->distinct_count.has_value()) {
+            RETURN_NOT_OK(yield(
+                nth_statistics++, start_new_column, 
std::optional<int32_t>(nth_column),
+                ARROW_STATISTICS_KEY_DISTINCT_COUNT_EXACT, int64(),
+                
ArrayStatistics::ValueType{statistics->distinct_count.value()}));
+            start_new_column = false;
+          }
+
+          if (statistics->min.has_value()) {
+            if (statistics->is_min_exact) {
+              RETURN_NOT_OK(yield(nth_statistics++, start_new_column,
+                                  std::optional<int32_t>(nth_column),
+                                  ARROW_STATISTICS_KEY_MIN_VALUE_EXACT,
+                                  statistics->MinArrowType(), 
statistics->min.value()));
+            } else {
+              RETURN_NOT_OK(yield(nth_statistics++, start_new_column,
+                                  std::optional<int32_t>(nth_column),
+                                  ARROW_STATISTICS_KEY_MIN_VALUE_APPROXIMATE,
+                                  statistics->MinArrowType(), 
statistics->min.value()));
+            }
+            start_new_column = false;
+          }
+
+          if (statistics->max.has_value()) {
+            if (statistics->is_max_exact) {
+              RETURN_NOT_OK(yield(nth_statistics++, start_new_column,
+                                  std::optional<int32_t>(nth_column),
+                                  ARROW_STATISTICS_KEY_MAX_VALUE_EXACT,
+                                  statistics->MaxArrowType(), 
statistics->max.value()));
+            } else {
+              RETURN_NOT_OK(yield(nth_statistics++, start_new_column,
+                                  std::optional<int32_t>(nth_column),
+                                  ARROW_STATISTICS_KEY_MAX_VALUE_APPROXIMATE,
+                                  statistics->MaxArrowType(), 
statistics->max.value()));
+            }
+            start_new_column = false;
+          }
+        }
+        return Status::OK();
+      };

Review Comment:
   Extracted.



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