pitrou commented on code in PR #35727:
URL: https://github.com/apache/arrow/pull/35727#discussion_r1233995618


##########
cpp/src/arrow/compute/kernels/vector_sort.cc:
##########
@@ -974,22 +1030,51 @@ class SortIndicesMetaFunction : public MetaFunction {
   }
 };
 
+void AddLeafFields(const FieldVector& fields, SortOrder order,
+                   std::vector<SortField>* out, std::vector<int>* tmp_indices) 
{
+  if (fields.empty()) {
+    return;
+  }
+
+  tmp_indices->push_back(0);
+  for (const auto& f : fields) {
+    const auto& type = *f->type();
+    if (type.id() == Type::STRUCT) {
+      AddLeafFields(type.fields(), order, out, tmp_indices);
+    } else {
+      out->emplace_back(FieldPath(*tmp_indices), order);
+    }
+    ++tmp_indices->back();
+  }
+  tmp_indices->pop_back();
+}
+
+void AddField(const DataType& type, const FieldPath& path, SortOrder order,
+              std::vector<SortField>* out, std::vector<int>* tmp_indices) {
+  if (type.id() == Type::STRUCT) {
+    *tmp_indices = path.indices();
+    AddLeafFields(type.fields(), order, out, tmp_indices);
+  } else {
+    out->emplace_back(path, order);
+  }
+}
+
 }  // namespace
 
 Result<std::vector<SortField>> FindSortKeys(const Schema& schema,
                                             const std::vector<SortKey>& 
sort_keys) {
   std::vector<SortField> fields;
-  std::unordered_set<int> seen;
+  std::unordered_set<FieldPath> seen;
   fields.reserve(sort_keys.size());
   seen.reserve(sort_keys.size());
 
+  std::vector<int> tmp_indices;
   for (const auto& sort_key : sort_keys) {
-    RETURN_NOT_OK(CheckNonNested(sort_key.target));
-
     ARROW_ASSIGN_OR_RAISE(auto match,
                           
PrependInvalidColumn(sort_key.target.FindOne(schema)));
-    if (seen.insert(match[0]).second) {
-      fields.push_back({match[0], sort_key.order});
+    if (seen.insert(match).second) {
+      ARROW_ASSIGN_OR_RAISE(auto schema_field, match.Get(schema));
+      AddField(*schema_field->type(), match, sort_key.order, &fields, 
&tmp_indices);

Review Comment:
   That said, the fact that you're also passing an inout-parameter that gets 
appended to advocates for a struct carrying that state:
   ```c++
   struct SortFieldPopulator {
    public:
     Result<std::vector<SortField>> FindSortKeys(...)
   
    protected:
     std::vector<SortField> sort_fields_;
     std::unordered_set<FieldPath> seen_;
     std::vector<int> tmp_indices_;
   };
   ```
   



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