Repository: milvus-io/milvus-sdk-go
Pull request #450 (2023-05-04 07:52:19)

Handle LongData returned when queryresult is empty
See also https://github.com/milvus-io/milvus/issues/23850
/kind improvement

sre-ci-robot: [APPROVALNOTIFIER] This PR is **APPROVED**

This pull-request has been approved by: *<a href="https://github.com/milvus-io/milvus-sdk-go/pull/450#" title="Author self-approved">congqixia</a>*

The full list of commands accepted by this bot can be found [here](https://go.k8s.io/bot-commands?repo=milvus-io%2Fmilvus-sdk-go).

The pull request process is described [here](https://git.k8s.io/community/contributors/guide/owners.md#the-code-review-process)

<details >
Needs approval from an approver in each of these files:

- ~~[OWNERS](https://github.com/milvus-io/milvus-sdk-go/blob/v2.2.x/OWNERS)~~ [congqixia]

Approvers can indicate their approval by writing `/approve` in a comment
Approvers can cancel approval by writing `/approve cancel` in a comment
</details>
<!-- META={"approvers":[]} -->
codecov[bot]: ## [Codecov](https://app.codecov.io/gh/milvus-io/milvus-sdk-go/pull/450?src=pr&el=h1&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=milvus-io) Report
> Merging [#450](https://app.codecov.io/gh/milvus-io/milvus-sdk-go/pull/450?src=pr&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=milvus-io) (d8ad975) into [v2.2.x](https://app.codecov.io/gh/milvus-io/milvus-sdk-go/commit/0f8c85b825bebb41bcdc9278d0e13597698f2438?el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=milvus-io) (0f8c85b) will **decrease** coverage by `0.17%`.
> The diff coverage is `57.14%`.

[![Impacted file tree graph](https://app.codecov.io/gh/milvus-io/milvus-sdk-go/pull/450/graphs/tree.svg?width=650&height=150&src=pr&token=WJK43PV390&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=milvus-io)](https://app.codecov.io/gh/milvus-io/milvus-sdk-go/pull/450?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=milvus-io)

```diff
@@            Coverage Diff             @@
##           v2.2.x     #450      +/-   ##
==========================================
- Coverage   87.45%   87.28%   -0.17%     
==========================================
  Files          31       31              
  Lines        2837     2848      +11     
==========================================
+ Hits         2481     2486       +5     
- Misses        269      275       +6     
  Partials       87       87              
```


| [Impacted Files](https://app.codecov.io/gh/milvus-io/milvus-sdk-go/pull/450?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=milvus-io) | Coverage Δ | |
|---|---|---|
| [entity/columns.go](https://app.codecov.io/gh/milvus-io/milvus-sdk-go/pull/450?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=milvus-io#diff-ZW50aXR5L2NvbHVtbnMuZ28=) | `91.17% <57.14%> (-4.03%)` | :arrow_down: |

sre-ci-robot: [APPROVALNOTIFIER] This PR is **APPROVED**

This pull-request has been approved by: *<a href="https://github.com/milvus-io/milvus-sdk-go/pull/450#" title="Author self-approved">congqixia</a>*, *<a href="https://github.com/milvus-io/milvus-sdk-go/pull/450#pullrequestreview-1412593801" title="LGTM">yah01</a>*

The full list of commands accepted by this bot can be found [here](https://go.k8s.io/bot-commands?repo=milvus-io%2Fmilvus-sdk-go).

The pull request process is described [here](https://git.k8s.io/community/contributors/guide/owners.md#the-code-review-process)

<details >
Needs approval from an approver in each of these files:

- ~~[OWNERS](https://github.com/milvus-io/milvus-sdk-go/blob/v2.2.x/OWNERS)~~ [congqixia]

Approvers can indicate their approval by writing `/approve` in a comment
Approvers can cancel approval by writing `/approve cancel` in a comment
</details>
<!-- META={"approvers":[]} -->

Patch:
diff --git a/entity/columns.go b/entity/columns.go
index 47a778d..d9ea388 100644
--- a/entity/columns.go
+++ b/entity/columns.go
@@ -124,7 +124,7 @@ func FieldDataColumn(fd *schema.FieldData, begin, end int) (Column, error) {
 		return NewColumnBool(fd.GetFieldName(), data.BoolData.GetData()[begin:end]), nil
 
 	case schema.DataType_Int8:
-		data, ok := fd.GetScalars().GetData().(*schema.ScalarField_IntData)
+		data, ok := getIntData(fd)
 		if !ok {
 			return nil, errFieldDataTypeNotMatch
 		}
@@ -140,7 +140,7 @@ func FieldDataColumn(fd *schema.FieldData, begin, end int) (Column, error) {
 		return NewColumnInt8(fd.GetFieldName(), values[begin:end]), nil
 
 	case schema.DataType_Int16:
-		data, ok := fd.GetScalars().GetData().(*schema.ScalarField_IntData)
+		data, ok := getIntData(fd)
 		if !ok {
 			return nil, errFieldDataTypeNotMatch
 		}
@@ -155,7 +155,7 @@ func FieldDataColumn(fd *schema.FieldData, begin, end int) (Column, error) {
 		return NewColumnInt16(fd.GetFieldName(), values[begin:end]), nil
 
 	case schema.DataType_Int32:
-		data, ok := fd.GetScalars().GetData().(*schema.ScalarField_IntData)
+		data, ok := getIntData(fd)
 		if !ok {
 			return nil, errFieldDataTypeNotMatch
 		}
@@ -218,6 +218,25 @@ func FieldDataColumn(fd *schema.FieldData, begin, end int) (Column, error) {
 	}
 }
 
+// getIntData get int32 slice from result field data
+// also handles LongData bug (see also https://github.com/milvus-io/milvus/issues/23850)
+func getIntData(fd *schema.FieldData) (*schema.ScalarField_IntData, bool) {
+	switch data := fd.GetScalars().GetData().(type) {
+	case *schema.ScalarField_IntData:
+		return data, true
+	case *schema.ScalarField_LongData:
+		// only alway empty LongData for backward compatibility
+		if len(data.LongData.GetData()) == 0 {
+			return &schema.ScalarField_IntData{
+				IntData: &schema.IntArray{},
+			}, true
+		}
+		return nil, false
+	default:
+		return nil, false
+	}
+}
+
 // FieldDataColumn converts schema.FieldData to vector Column
 func FieldDataVector(fd *schema.FieldData) (Column, error) {
 	switch fd.GetType() {


Test patch:
diff --git a/entity/columns_test.go b/entity/columns_test.go
index 171a873..1975c07 100644
--- a/entity/columns_test.go
+++ b/entity/columns_test.go
@@ -95,3 +95,78 @@ func TestIDColumns(t *testing.T) {
 		assert.Equal(t, dataLen, column.Len())
 	})
 }
+
+func TestGetIntData(t *testing.T) {
+	type testCase struct {
+		tag      string
+		fd       *schema.FieldData
+		expectOK bool
+	}
+
+	cases := []testCase{
+		{
+			tag: "normal_IntData",
+			fd: &schema.FieldData{
+				Field: &schema.FieldData_Scalars{
+					Scalars: &schema.ScalarField{
+						Data: &schema.ScalarField_IntData{
+							IntData: &schema.IntArray{Data: []int32{1, 2, 3}},
+						},
+					},
+				},
+			},
+			expectOK: true,
+		},
+		{
+			tag: "empty_LongData",
+			fd: &schema.FieldData{
+				Field: &schema.FieldData_Scalars{
+					Scalars: &schema.ScalarField{
+						Data: &schema.ScalarField_LongData{
+							LongData: &schema.LongArray{Data: nil},
+						},
+					},
+				},
+			},
+			expectOK: true,
+		},
+		{
+			tag: "nonempty_LongData",
+			fd: &schema.FieldData{
+				Field: &schema.FieldData_Scalars{
+					Scalars: &schema.ScalarField{
+						Data: &schema.ScalarField_LongData{
+							LongData: &schema.LongArray{Data: []int64{1, 2, 3}},
+						},
+					},
+				},
+			},
+			expectOK: false,
+		},
+		{
+			tag: "other_data",
+			fd: &schema.FieldData{
+				Field: &schema.FieldData_Scalars{
+					Scalars: &schema.ScalarField{
+						Data: &schema.ScalarField_BoolData{},
+					},
+				},
+			},
+			expectOK: false,
+		},
+		{
+			tag: "vector_data",
+			fd: &schema.FieldData{
+				Field: &schema.FieldData_Vectors{},
+			},
+			expectOK: false,
+		},
+	}
+
+	for _, tc := range cases {
+		t.Run(tc.tag, func(t *testing.T) {
+			_, ok := getIntData(tc.fd)
+			assert.Equal(t, tc.expectOK, ok)
+		})
+	}
+}