hanahmily commented on code in PR #861:
URL: 
https://github.com/apache/skywalking-banyandb/pull/861#discussion_r2558180885


##########
pkg/index/inverted/query.go:
##########
@@ -468,6 +477,37 @@ func (m *matchAllNode) String() string {
        return "matchAll"
 }
 
+type topNNode struct {

Review Comment:
   Please don't add this node. The sorting is not a part of the "query". 



##########
pkg/index/inverted/query.go:
##########
@@ -468,6 +477,37 @@ func (m *matchAllNode) String() string {
        return "matchAll"
 }
 
+type topNNode struct {

Review Comment:
   You should consider using "SeriesSort" instead of "Search." There are 
several benefits to this approach:
   
   1. There is no need to modify the query node tree.
   2. No additional branches need to be added to the Search function.
   3. The return values include "SortedValue," which can simplify the sorting 
logic for both liaison and shard.



##########
banyand/property/shard.go:
##########
@@ -152,7 +152,7 @@ func (s *shard) buildUpdateDocument(id []byte, property 
*propertyv1.Property, de
                }
                tagField := index.NewBytesField(index.FieldKey{IndexRuleID: 
uint32(convert.HashStr(t.Key))}, tv)
                tagField.Index = true
-               tagField.NoSort = true
+               tagField.NoSort = false // Enable sorting on tag fields

Review Comment:
   database.query should sort properties from several shards. 



##########
banyand/liaison/grpc/property.go:
##########
@@ -762,3 +772,115 @@ type repairInProcessKey struct {
        entity  string
        modTime int64
 }
+
+// sortProperties sorts properties based on the specified order_by field.
+func sortProperties(properties []*propertyv1.Property, orderBy 
*propertyv1.QueryOrder) []*propertyv1.Property {
+       if len(properties) == 0 || orderBy == nil || orderBy.TagName == "" {
+               return properties
+       }
+
+       isDesc := orderBy.Sort == modelv1.Sort_SORT_DESC
+
+       sort.Slice(properties, func(i, j int) bool {
+               // Extract tag values
+               valI := getPropertyTagValue(properties[i], orderBy.TagName)
+               valJ := getPropertyTagValue(properties[j], orderBy.TagName)
+
+               // Handle nil values - put them at the end
+               if valI == nil {
+                       return false
+               }
+               if valJ == nil {
+                       return true
+               }
+
+               // Compare values
+               cmp := comparePropertyTagValues(valI, valJ)
+
+               if isDesc {
+                       return cmp > 0
+               }
+               return cmp < 0
+       })
+
+       return properties
+}
+
+// getPropertyTagValue retrieves a tag value by name from a property.
+func getPropertyTagValue(prop *propertyv1.Property, tagName string) 
*modelv1.TagValue {

Review Comment:
   Add the sorting tag as an independent tag to propertyv1.Property. The 
sorting tag might not be in the projection. Using such a tag helps us handle 
sorting without returning the tag through the projection tag list.



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