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


##########
pkg/query/logical/common.go:
##########
@@ -60,25 +59,30 @@ func ProjectItem(ec executor.ExecutionContext, item 
tsdb.Item, projectionFieldRe
                if len(refs) == 0 {
                        continue
                }
-               tags := make([]*modelv1.Tag, len(refs))
                familyName := refs[0].Tag.getFamilyName()
                parsedTagFamily, err := ec.ParseTagFamily(familyName, item)
                if err != nil {
                        return nil, errors.WithMessage(err, "parse projection")
                }
+
+               var tags []*modelv1.Tag
                if len(refs) > len(parsedTagFamily.Tags) {
-                       return nil, errors.Wrapf(errInvalidData,
-                               "the number of tags %d in %s is less then 
expected %d",
-                               len(parsedTagFamily.Tags), familyName, 
len(refs))
-               }
-               for j, ref := range refs {
-                       if len(parsedTagFamily.GetTags()) > ref.Spec.TagIdx {
-                               tags[j] = 
parsedTagFamily.GetTags()[ref.Spec.TagIdx]
-                       } else {
-                               tags[j] = &modelv1.Tag{Key: ref.Tag.name, 
Value: nullTag}
+                       tags = make([]*modelv1.Tag, 
len(parsedTagFamily.GetTags()))
+                       for j, ref := range refs {
+                               if len(parsedTagFamily.GetTags()) > 
ref.Spec.TagIdx {
+                                       tags[j] = 
parsedTagFamily.GetTags()[ref.Spec.TagIdx]
+                               }
+                       }
+               } else {
+                       tags = make([]*modelv1.Tag, len(refs))
+                       for j, ref := range refs {
+                               if len(parsedTagFamily.GetTags()) > 
ref.Spec.TagIdx {
+                                       tags[j] = 
parsedTagFamily.GetTags()[ref.Spec.TagIdx]
+                               } else {
+                                       tags[j] = &modelv1.Tag{Key: 
ref.Tag.name, Value: nullTag}
+                               }
                        }
                }

Review Comment:
   There is a lot of repeated code and we can clean it up by simplifying the 
condition and the loop body.
   
   ```go
   parsedTagSize := len(parsedTagFamily.GetTags())
   tagRefSize := len(refs)
   
   // Determine maximum size for creating the tags slice
   maxSize := tagRefSize
   if parsedTagSize > tagRefSize {
        maxSize = parsedTagSize
   }
   
   tags := make([]*modelv1.Tag, maxSize)
   
   for j, ref := range refs {
        if parsedTagSize > ref.Spec.TagIdx {
                tags[j] = parsedTagFamily.GetTags()[ref.Spec.TagIdx]
        } else if j < tagRefSize {
                tags[j] = &modelv1.Tag{Key: ref.Tag.name, Value: nullTag}
        }
   }
   ```



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