R-JunmingChen commented on code in PR #37418:
URL: https://github.com/apache/arrow/pull/37418#discussion_r1336567976


##########
cpp/src/arrow/compute/kernels/vector_dictionary.cc:
##########
@@ -0,0 +1,201 @@
+// 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 "arrow/array/array_base.h"
+#include "arrow/array/array_dict.h"
+#include "arrow/array/builder_primitive.h"
+#include "arrow/array/dict_internal.h"
+#include "arrow/compute/api_vector.h"
+#include "arrow/compute/kernels/common_internal.h"
+#include "arrow/result.h"
+
+namespace arrow {
+
+namespace compute {
+namespace internal {
+
+namespace {
+
+// Dictionary compaction implementation
+
+const FunctionDoc dictionary_compact_doc{
+    "Compact dictionary array",
+    ("Return a compacted version of the dictionary array input,\n"
+     "which removes unused values in dictionary.\n"
+     "The function assumes every indices has its corresponding value\n"
+     "in dictionary."),
+    {"dictionary_array"}};
+
+class DictionaryCompactKernel : public KernelState {
+ public:
+  virtual Result<std::shared_ptr<Array>> Exec(std::shared_ptr<Array> 
dict_array,
+                                              ExecContext* ctx) const = 0;
+};
+
+template <typename IndiceArrowType>
+class DictionaryCompactKernelImpl : public DictionaryCompactKernel {
+  using BuilderType = NumericBuilder<IndiceArrowType>;
+  using CType = typename IndiceArrowType::c_type;
+
+ public:
+  Result<std::shared_ptr<Array>> Exec(std::shared_ptr<Array> dict_array,
+                                      ExecContext* ctx) const override {
+    const DictionaryArray& casted_dict_array =
+        checked_cast<const DictionaryArray&>(*dict_array);
+    const std::shared_ptr<Array>& dict = casted_dict_array.dictionary();
+    if (dict->length() == 0) {
+      return dict_array;
+    }
+    const std::shared_ptr<Array>& indices = casted_dict_array.indices();
+    if (indices->length() == 0) {
+      ARROW_ASSIGN_OR_RAISE(auto empty_dict,
+                            MakeEmptyArray(dict->type(), ctx->memory_pool()));
+      return DictionaryArray::FromArrays(dict_array->type(), indices, 
empty_dict);
+    }
+    const CType* indices_data = indices->data()->GetValues<CType>(1);
+
+    // check whether the input is compacted
+    std::vector<bool> dict_used(dict->length(), false);
+    int64_t dict_used_count = 0;
+    for (int64_t i = 0; i < indices->length(); i++) {
+      if (indices->IsNull(i)) {
+        continue;

Review Comment:
   Hi, another curious question, why the helpers is more efficient than a for 
loop? since the lambda function have some extra heads in runtime.
   
   Besides, with a for loop, we can end in advance when `dict_used_count == 
dict->length()`.
   ```
           if (dict_used_count == dict->length()) {  // input is already 
compacted
             out->value = dict_array->data();
             return Status::OK();
           }
   ```
   
   I think it's ok for the following code to make this change:
   ```
       for (int64_t i = 0; i < indices->length(); i++) {
         if (indices->IsNull(i)) {
           ARROW_RETURN_NOT_OK(indices_builder.AppendNull());
         } else {
           CType current_index = indices_data[i];
           ARROW_RETURN_NOT_OK(
               indices_builder.Append(current_index - 
index_minus_number[current_index]));
         }
       }
   ```



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