edponce commented on a change in pull request #11257:
URL: https://github.com/apache/arrow/pull/11257#discussion_r717880947



##########
File path: cpp/src/arrow/compute/kernels/aggregate_basic.cc
##########
@@ -121,6 +122,101 @@ Result<std::unique_ptr<KernelState>> 
CountInit(KernelContext*,
       static_cast<const CountOptions&>(*args.options));
 }
 
+// ----------------------------------------------------------------------
+// Distinct Count implementation
+
+template <typename Type>
+struct CountDistinctImpl : public ScalarAggregator {
+  using MemoTable = typename arrow::internal::HashTraits<Type>::MemoTableType;
+
+  Status consume_memo(const ArrayData& arr) {
+    auto visit_null = [&]() {
+      //return string_builder.AppendNull();
+      return Status::OK();
+    };
+    auto visit_value = [&](typename Type::c_type arg) {
+      //ARROW_ASSIGN_OR_RAISE(auto formatted, formatter(arg));
+      //return string_builder.Append(std::move(formatted));
+      int y;
+      RETURN_NOT_OK(memo_table_->GetOrInsert(arg, &y));
+      //std::cout << "AAAAAAAAAAAAAADFFFFFFFFFFFFFFFFFF\n" << arg << "\n";
+      return Status::OK();
+    };
+    RETURN_NOT_OK(VisitArrayDataInline<Type>(arr, visit_value, visit_null));
+    return Status::OK();
+  }
+
+  explicit CountDistinctImpl(CountOptions options) : 
options(std::move(options)), memo_table_(new MemoTable(default_memory_pool(), 
0)) {
+  }
+
+  Status Consume(KernelContext* ctx, const ExecBatch& batch) override {
+    auto a = batch[0].make_array();
+    //std::cout << "LOTEEEEEEEEEEE\n" << a->ToString() << "\n";
+    
+    const ArrayData& arr = *batch[0].array();
+    
+    consume_memo(arr);
+    
+    //std::cout << "MEMOOOOOOOOOOOO\n" << this->memo_table_->size() << "\n";
+    this->result_countd += this->memo_table_->size();
+    return Status::OK();
+    //RETURN_NOT_OK(lookup_table->GetOrInsert());
+    
+    //ARROW_ASSIGN_OR_RAISE(auto grouper, 
internal::Grouper::Make(batch.GetDescriptors(), ctx->exec_context()));
+    //return grouper->Consume(batch).status();
+//    if (options.mode == CountOptions::ALL) {
+//      this->non_nulls += batch.length;
+//    } else if (batch[0].is_array()) {
+//      const ArrayData& input = *batch[0].array();
+//      const int64_t nulls = input.GetNullCount();
+//      this->nulls += nulls;
+//      this->non_nulls += input.length - nulls;
+//    } else {
+//      const Scalar& input = *batch[0].scalar();
+//      this->nulls += !input.is_valid * batch.length;
+//      this->non_nulls += input.is_valid * batch.length;
+//    }
+//    return Status::OK();
+  }
+
+  Status MergeFrom(KernelContext*, KernelState&& src) override {
+    const auto& other_state = checked_cast<const CountDistinctImpl&>(src);
+    //this->non_nulls += other_state.non_nulls;
+    //this->nulls += other_state.nulls;
+    this->result_countd += other_state.result_countd;
+    return Status::OK();
+  }
+
+  Status Finalize(KernelContext* ctx, Datum* out) override {
+    const auto& state = checked_cast<const CountDistinctImpl&>(*ctx->state());
+    switch (state.options.mode) {
+      case CountOptions::ONLY_VALID:
+      case CountOptions::ALL:
+        // ALL is equivalent since we don't count the null/non-null
+        // separately to avoid potentially computing null count
+        //*out = Datum(state.non_nulls);

Review comment:
       If `ONLY_VALID` == 'ALL` does it even matter to have `ONLY_NULL`? or 
these options at all?
   What is the expected semantics of `count_distinct` based on these options?
   * `ONLY_VALID` - `count_distinct` on non-null values
   * `ONLY_NULL`
     * if `null count` >= 0 then return `min(null count, 1)`
     * if `null count` < 0, then compute null count and return 1
   * `ALL` - return `f(ONLY_VALID) + f(ONLY_NULL)`
   
   *NOTE: There may have been requirements/discussion on this that I may not be 
aware of*.




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