Repository: milvus-io/milvus-sdk-go
Pull request #468 (2023-05-25 07:50:11)

[2.2.x] Add logical partition num option for create collection
See also milvus-io/milvus#24402
/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/468#" 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/468?src=pr&el=h1&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=milvus-io) Report
> Merging [#468](https://app.codecov.io/gh/milvus-io/milvus-sdk-go/pull/468?src=pr&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=milvus-io) (bc4f33a) into [v2.2.x](https://app.codecov.io/gh/milvus-io/milvus-sdk-go/commit/860ee30be4e0c9481f0e55131f26c7b508b2c883?el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=milvus-io) (860ee30) will **increase** coverage by `0.02%`.
> The diff coverage is `100.00%`.

[![Impacted file tree graph](https://app.codecov.io/gh/milvus-io/milvus-sdk-go/pull/468/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/468?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     #468      +/-   ##
==========================================
+ Coverage   82.15%   82.17%   +0.02%     
==========================================
  Files          32       32              
  Lines        2645     2648       +3     
==========================================
+ Hits         2173     2176       +3     
  Misses        373      373              
  Partials       99       99              
```


| [Impacted Files](https://app.codecov.io/gh/milvus-io/milvus-sdk-go/pull/468?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=milvus-io) | Coverage Δ | |
|---|---|---|
| [client/collection.go](https://app.codecov.io/gh/milvus-io/milvus-sdk-go/pull/468?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=milvus-io#diff-Y2xpZW50L2NvbGxlY3Rpb24uZ28=) | `86.17% <100.00%> (+0.04%)` | :arrow_up: |
| [client/options.go](https://app.codecov.io/gh/milvus-io/milvus-sdk-go/pull/468?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=milvus-io#diff-Y2xpZW50L29wdGlvbnMuZ28=) | `68.00% <100.00%> (+1.33%)` | :arrow_up: |

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/468#" title="Author self-approved">congqixia</a>*, *<a href="https://github.com/milvus-io/milvus-sdk-go/pull/468#pullrequestreview-1443387905" 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/client/collection.go b/client/collection.go
index c2f27e7..9887b57 100644
--- a/client/collection.go
+++ b/client/collection.go
@@ -146,6 +146,7 @@ func (c *GrpcClient) CreateCollection(ctx context.Context, collSchema *entity.Sc
 		// default consistency level is strong
 		// to be consistent with previous version
 		ConsistencyLevel: common.ConsistencyLevel_Strong,
+		NumPartitions:    0,
 	}
 	// apply options on request
 	for _, opt := range opts {
diff --git a/client/options.go b/client/options.go
index 44b9c4f..1211090 100644
--- a/client/options.go
+++ b/client/options.go
@@ -29,6 +29,13 @@ func WithConsistencyLevel(cl entity.ConsistencyLevel) CreateCollectionOption {
 	}
 }
 
+// WithPartitionNum returns a create collection options to set physical partition number when logical partition feature.
+func WithPartitionNum(partitionNums int64) CreateCollectionOption {
+	return func(req *server.CreateCollectionRequest) {
+		req.NumPartitions = partitionNums
+	}
+}
+
 // LoadCollectionOption is an option that is used to modify LoadCollectionRequest
 type LoadCollectionOption func(*server.LoadCollectionRequest)
 


Test patch:
diff --git a/client/options_test.go b/client/options_test.go
index b876238..5938d42 100644
--- a/client/options_test.go
+++ b/client/options_test.go
@@ -12,6 +12,7 @@
 package client
 
 import (
+	"math/rand"
 	"testing"
 
 	common "github.com/milvus-io/milvus-proto/go-api/commonpb"
@@ -32,6 +33,19 @@ func TestCreateCollectionWithConsistencyLevel(t *testing.T) {
 	assert.Equal(t, common.ConsistencyLevel_Bounded, req.GetConsistencyLevel())
 }
 
+func TestCreateCollectionWithPartitionNum(t *testing.T) {
+	partitionNum := rand.Int63n(1000) + 1
+	opt := WithPartitionNum(partitionNum)
+	assert.NotNil(t, opt)
+	req := &server.CreateCollectionRequest{}
+
+	assert.NotPanics(t, func() {
+		opt(req)
+	})
+
+	assert.Equal(t, partitionNum, req.GetNumPartitions())
+}
+
 func TestLoadCollectionWithReplicaNumber(t *testing.T) {
 	opt := WithReplicaNumber(testMultiReplicaNumber)
 	assert.NotNil(t, opt)