Skip to content

Commit

Permalink
Add count and query test cases
Browse files Browse the repository at this point in the history
Signed-off-by: ThreadDao <[email protected]>
  • Loading branch information
ThreadDao committed Oct 8, 2023
1 parent b1c9ccb commit b5f0b6c
Show file tree
Hide file tree
Showing 2 changed files with 194 additions and 5 deletions.
2 changes: 1 addition & 1 deletion test/testcases/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import (
"github.com/milvus-io/milvus-sdk-go/v2/test/common"
)

var addr = flag.String("addr", "localhost:19530", "server host and port")
var addr = flag.String("addr", "10.102.7.87:19530", "server host and port")

func init() {
log.SetFlags(log.Ldate | log.Ltime | log.Lshortfile)
Expand Down
197 changes: 193 additions & 4 deletions test/testcases/query_test.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
//go:build L0

package testcases

import (
Expand Down Expand Up @@ -449,7 +447,7 @@ func TestQueryJsonDynamicField(t *testing.T) {
// connect
mc := createMilvusClient(ctx, t)

for _, dynamicField := range []bool{true} {
for _, dynamicField := range []bool{true, false} {
// create collection
cp := CollectionParams{
CollectionFieldsType: Int64FloatVecJSON,
Expand Down Expand Up @@ -479,12 +477,18 @@ func TestQueryJsonDynamicField(t *testing.T) {
errLoad := mc.LoadCollection(ctx, collName, false)
common.CheckErr(t, errLoad, true)

// query with count(*) and empty expr
countRes, _ := mc.Query(ctx, collName,
[]string{common.DefaultPartition},
"", []string{"count(*)"})
require.Equal(t, int64(common.DefaultNb), countRes.GetColumn("count(*)").(*entity.ColumnInt64).Data()[0])

outputFields := []string{common.DefaultIntFieldName, common.DefaultJSONFieldName}
if dynamicField {
outputFields = append(outputFields, common.DefaultDynamicFieldName)
}

// query and output json field
// query and output json/dynamic field
pkColumn := entity.NewColumnInt64(common.DefaultIntFieldName, []int64{0, 1})
queryResult, err := mc.QueryByPks(
ctx, collName,
Expand Down Expand Up @@ -536,6 +540,127 @@ func TestQueryJsonDynamicField(t *testing.T) {
}
}

// Test query json and dynamic collection with string expr
func TestQueryCountJsonDynamicExpr(t *testing.T) {
t.Parallel()
ctx := createContext(t, time.Second*common.DefaultTimeout)
// connect
mc := createMilvusClient(ctx, t)

// create collection
cp := CollectionParams{
CollectionFieldsType: Int64FloatVecJSON,
AutoID: false,
EnableDynamicField: true,
ShardsNum: common.DefaultShards,
Dim: common.DefaultDim,
}
collName := createCollection(ctx, t, mc, cp)

// insert
dp := DataParams{
CollectionName: collName,
PartitionName: "",
CollectionFieldsType: Int64FloatVecJSON,
start: 0,
nb: common.DefaultNb,
dim: common.DefaultDim,
EnableDynamicField: true,
}
_, _ = insertData(ctx, t, mc, dp)

idx, _ := entity.NewIndexHNSW(entity.L2, 8, 96)
_ = mc.CreateIndex(ctx, collName, common.DefaultFloatVecFieldName, idx, false)

// Load collection
errLoad := mc.LoadCollection(ctx, collName, false)
common.CheckErr(t, errLoad, true)

// query with different expr and count

// query with
type exprCount struct {
expr string
count int64
}
exprCounts := []exprCount{
{expr: "", count: common.DefaultNb},
{expr: fmt.Sprintf("%s < 1000", common.DefaultIntFieldName), count: 1000},
{expr: fmt.Sprintf("%s in [0, 1, 2]", common.DefaultIntFieldName), count: 3},
{expr: fmt.Sprintf("%s >= 1000 && %s < 2000", common.DefaultIntFieldName, common.DefaultIntFieldName), count: 1000},
{expr: fmt.Sprintf("%s >= 1000 || %s > 2000", common.DefaultIntFieldName, common.DefaultIntFieldName), count: 2000},
//{expr: fmt.Sprintf("%s['number'] < 100", common.DefaultJSONFieldName), count: 100},
{expr: fmt.Sprintf("%s < 100", common.DefaultDynamicNumberField), count: 100},
{expr: fmt.Sprintf("%s['bool'] == true", common.DefaultJSONFieldName), count: 1500},
{expr: fmt.Sprintf("%s == false", common.DefaultDynamicBoolField), count: 2000},
{expr: fmt.Sprintf("%s in ['1', '2'] ", common.DefaultDynamicStringField), count: 2},
{expr: fmt.Sprintf("%s[string] in ['1', '2', '5'] ", common.DefaultJSONFieldName), count: 3},
{expr: fmt.Sprintf("%s[list] == [0, 1] ", common.DefaultJSONFieldName), count: 1},
}

for _, _exprCount := range exprCounts {
countRes, _ := mc.Query(ctx, collName,
[]string{common.DefaultPartition},
_exprCount.expr, []string{"count(*)"})
require.Equal(t, _exprCount.count, countRes.GetColumn("count(*)").(*entity.ColumnInt64).Data()[0])
}
}

func TestQueryJsonDynamicExpr(t *testing.T) {
t.Parallel()
ctx := createContext(t, time.Second*common.DefaultTimeout)
// connect
mc := createMilvusClient(ctx, t)

// create collection
cp := CollectionParams{
CollectionFieldsType: Int64FloatVecJSON,
AutoID: false,
EnableDynamicField: true,
ShardsNum: common.DefaultShards,
Dim: common.DefaultDim,
}
collName := createCollection(ctx, t, mc, cp)

// insert
dp := DataParams{
CollectionName: collName,
PartitionName: "",
CollectionFieldsType: Int64FloatVecJSON,
start: 0,
nb: common.DefaultNb,
dim: common.DefaultDim,
EnableDynamicField: true,
}
_, _ = insertData(ctx, t, mc, dp)

idx, _ := entity.NewIndexHNSW(entity.L2, 8, 96)
_ = mc.CreateIndex(ctx, collName, common.DefaultFloatVecFieldName, idx, false)

// Load collection
errLoad := mc.LoadCollection(ctx, collName, false)
common.CheckErr(t, errLoad, true)

// query with different expr and count
countRes, _ := mc.Query(ctx, collName,
[]string{common.DefaultPartition},
"json['number'] <= 2", []string{"int64", "json"})
for _, column := range countRes {
//log.Println(column.FieldData())
if column.Type() == entity.FieldTypeJSON {
var jsonData []string
for i := 0; i < column.Len(); i++ {
line, err := column.GetAsString(i)
if err != nil {
fmt.Println(err)
}
jsonData = append(jsonData, line)
}
log.Println(jsonData)
}
}
}

// test query and output both json and dynamic field
func TestQueryJsonDynamicFieldRows(t *testing.T) {
ctx := createContext(t, time.Second*common.DefaultTimeout)
Expand Down Expand Up @@ -603,6 +728,70 @@ func TestQueryJsonDynamicFieldRows(t *testing.T) {
common.CheckQueryResult(t, queryResult, []entity.Column{pkColumn, jsonColumn, dynamicColumn})
}

// test query output count(*) fields
func TestQueryOutputCount(t *testing.T) {
t.Parallel()
ctx := createContext(t, time.Second*common.DefaultTimeout)
// connect
mc := createMilvusClient(ctx, t)
for _, enableDynamic := range []bool{true, false} {
// create, insert, index
collName, _ := createCollectionWithDataIndex(ctx, t, mc, true, true,
client.WithEnableDynamicSchema(enableDynamic), client.WithConsistencyLevel(entity.ClStrong))

// Load collection
errLoad := mc.LoadCollection(ctx, collName, false)
common.CheckErr(t, errLoad, true)

queryExpr := fmt.Sprintf("%s >= 0", common.DefaultIntFieldName)

//query with empty output fields []string{}-> output "int64"
queryRes, _ := mc.Query(
ctx, collName, []string{common.DefaultPartition},
queryExpr, []string{"count(*)"})
log.Println(queryRes.GetColumn("count(*)").(*entity.ColumnInt64).Data())
require.Equal(t, int64(common.DefaultNb), queryRes.GetColumn("count(*)").(*entity.ColumnInt64).Data()[0])
}
}

// test query output invalid count(*) fields
func TestQueryOutputInvalidOutputFieldCount(t *testing.T) {
type invalidCountStruct struct {
countField string
errMsg string
}
t.Parallel()
ctx := createContext(t, time.Second*common.DefaultTimeout)
// connect
mc := createMilvusClient(ctx, t)

// create, insert, index
collName, _ := createCollectionWithDataIndex(ctx, t, mc, true, true,
client.WithEnableDynamicSchema(true), client.WithConsistencyLevel(entity.ClStrong))

// Load collection
errLoad := mc.LoadCollection(ctx, collName, false)
common.CheckErr(t, errLoad, true)

// invalid expr
invalidOutputFieldCount := []invalidCountStruct{
{countField: "Count(*)", errMsg: "extra output fields [Count(*)] found and result does not dynamic field"},
{countField: "ccount(*)", errMsg: "field ccount(*) not exist"},
{countField: "count[*]", errMsg: "field count[*] not exist"},
{countField: "count", errMsg: "field count not exist"},
{countField: "count(**)", errMsg: "field count(**) not exist"},
}
for _, invalidCount := range invalidOutputFieldCount {
queryExpr := fmt.Sprintf("%s >= 0", common.DefaultIntFieldName)

//query with empty output fields []string{}-> output "int64"
_, err := mc.Query(
ctx, collName, []string{common.DefaultPartition},
queryExpr, []string{invalidCount.countField})
common.CheckErr(t, err, false, invalidCount.errMsg)
}
}

// TODO offset and limit
// TODO consistency level
// TODO ignore growing

0 comments on commit b5f0b6c

Please sign in to comment.