github.com/matrixorigin/matrixone@v1.2.0/pkg/vm/engine/disttae/util_test.go (about)

     1  // Copyright 2022 Matrix Origin
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //	http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package disttae
    16  
    17  import (
    18  	"bytes"
    19  	"context"
    20  	"math/rand"
    21  	"testing"
    22  
    23  	"github.com/stretchr/testify/assert"
    24  	"github.com/stretchr/testify/require"
    25  
    26  	"github.com/matrixorigin/matrixone/pkg/common/mpool"
    27  	"github.com/matrixorigin/matrixone/pkg/common/util"
    28  	"github.com/matrixorigin/matrixone/pkg/container/types"
    29  	"github.com/matrixorigin/matrixone/pkg/container/vector"
    30  	"github.com/matrixorigin/matrixone/pkg/objectio"
    31  	"github.com/matrixorigin/matrixone/pkg/pb/plan"
    32  	"github.com/matrixorigin/matrixone/pkg/sql/colexec"
    33  	plan2 "github.com/matrixorigin/matrixone/pkg/sql/plan"
    34  	"github.com/matrixorigin/matrixone/pkg/sql/plan/function"
    35  	"github.com/matrixorigin/matrixone/pkg/testutil"
    36  	"github.com/matrixorigin/matrixone/pkg/vm/engine/tae/index"
    37  	"github.com/matrixorigin/matrixone/pkg/vm/engine/tae/options"
    38  )
    39  
    40  func makeColExprForTest(idx int32, typ types.T) *plan.Expr {
    41  	schema := []string{"a", "b", "c", "d"}
    42  	containerType := typ.ToType()
    43  	exprType := plan2.MakePlan2Type(&containerType)
    44  
    45  	return &plan.Expr{
    46  		Typ: exprType,
    47  		Expr: &plan.Expr_Col{
    48  			Col: &plan.ColRef{
    49  				RelPos: 0,
    50  				ColPos: idx,
    51  				Name:   schema[idx],
    52  			},
    53  		},
    54  	}
    55  }
    56  
    57  func makeFunctionExprForTest(name string, args []*plan.Expr) *plan.Expr {
    58  	argTypes := make([]types.Type, len(args))
    59  	for i, arg := range args {
    60  		argTypes[i] = plan2.MakeTypeByPlan2Expr(arg)
    61  	}
    62  
    63  	finfo, err := function.GetFunctionByName(context.TODO(), name, argTypes)
    64  	if err != nil {
    65  		panic(err)
    66  	}
    67  
    68  	retTyp := finfo.GetReturnType()
    69  
    70  	return &plan.Expr{
    71  		Typ: plan2.MakePlan2Type(&retTyp),
    72  		Expr: &plan.Expr_F{
    73  			F: &plan.Function{
    74  				Func: &plan.ObjectRef{
    75  					Obj:     finfo.GetEncodedOverloadID(),
    76  					ObjName: name,
    77  				},
    78  				Args: args,
    79  			},
    80  		},
    81  	}
    82  }
    83  
    84  func makeInExprForTest[T any](arg0 *plan.Expr, vals []T, oid types.T, mp *mpool.MPool) *plan.Expr {
    85  	vec := vector.NewVec(oid.ToType())
    86  	for _, val := range vals {
    87  		_ = vector.AppendAny(vec, val, false, mp)
    88  	}
    89  	data, _ := vec.MarshalBinary()
    90  	vec.Free(mp)
    91  	return &plan.Expr{
    92  		Typ: plan.Type{
    93  			Id:          int32(types.T_bool),
    94  			NotNullable: true,
    95  		},
    96  		Expr: &plan.Expr_F{
    97  			F: &plan.Function{
    98  				Func: &plan.ObjectRef{
    99  					Obj:     function.InFunctionEncodedID,
   100  					ObjName: function.InFunctionName,
   101  				},
   102  				Args: []*plan.Expr{
   103  					arg0,
   104  					{
   105  						Typ: plan.Type{
   106  							Id: int32(types.T_tuple),
   107  						},
   108  						Expr: &plan.Expr_Vec{
   109  							Vec: &plan.LiteralVec{
   110  								Len:  int32(len(vals)),
   111  								Data: data,
   112  							},
   113  						},
   114  					},
   115  				},
   116  			},
   117  		},
   118  	}
   119  }
   120  
   121  func TestBlockMetaMarshal(t *testing.T) {
   122  	location := []byte("test")
   123  	var info objectio.BlockInfo
   124  	info.SetMetaLocation(location)
   125  	data := objectio.EncodeBlockInfo(info)
   126  	info2 := objectio.DecodeBlockInfo(data)
   127  	require.Equal(t, info, *info2)
   128  }
   129  
   130  func TestCheckExprIsZonemappable(t *testing.T) {
   131  	type asserts = struct {
   132  		result bool
   133  		expr   *plan.Expr
   134  	}
   135  	testCases := []asserts{
   136  		// a > 1  -> true
   137  		{true, makeFunctionExprForTest(">", []*plan.Expr{
   138  			makeColExprForTest(0, types.T_int64),
   139  			plan2.MakePlan2Int64ConstExprWithType(10),
   140  		})},
   141  		// a >= b -> true
   142  		{true, makeFunctionExprForTest(">=", []*plan.Expr{
   143  			makeColExprForTest(0, types.T_int64),
   144  			makeColExprForTest(1, types.T_int64),
   145  		})},
   146  		// abs(a) -> false
   147  		{false, makeFunctionExprForTest("abs", []*plan.Expr{
   148  			makeColExprForTest(0, types.T_int64),
   149  		})},
   150  	}
   151  
   152  	t.Run("test checkExprIsZonemappable", func(t *testing.T) {
   153  		for i, testCase := range testCases {
   154  			zonemappable := plan2.ExprIsZonemappable(context.TODO(), testCase.expr)
   155  			if zonemappable != testCase.result {
   156  				t.Fatalf("checkExprIsZonemappable testExprs[%d] is different with expected", i)
   157  			}
   158  		}
   159  	})
   160  }
   161  
   162  func TestEvalZonemapFilter(t *testing.T) {
   163  	m := mpool.MustNewNoFixed(t.Name())
   164  	proc := testutil.NewProcessWithMPool(m)
   165  	type myCase = struct {
   166  		exprs  []*plan.Expr
   167  		meta   objectio.BlockObject
   168  		desc   []string
   169  		expect []bool
   170  	}
   171  
   172  	zm0 := index.NewZM(types.T_float64, 0)
   173  	zm0.Update(float64(-10))
   174  	zm0.Update(float64(20))
   175  	zm1 := index.NewZM(types.T_float64, 0)
   176  	zm1.Update(float64(5))
   177  	zm1.Update(float64(25))
   178  	zm2 := index.NewZM(types.T_varchar, 0)
   179  	zm2.Update([]byte("abc"))
   180  	zm2.Update([]byte("opq"))
   181  	zm3 := index.NewZM(types.T_varchar, 0)
   182  	zm3.Update([]byte("efg"))
   183  	zm3.Update(index.MaxBytesValue)
   184  	cases := []myCase{
   185  		{
   186  			desc: []string{
   187  				"a>10", "a>30", "a<=-10", "a<-10", "a+b>60", "a+b<-5", "a-b<-34", "a-b<-35", "a-b<=-35", "a>b",
   188  				"a>b+15", "a>=b+15", "a>100 or b>10", "a>100 and b<0", "d>xyz", "d<=efg", "d<efg", "c>d", "c<d",
   189  			},
   190  			exprs: []*plan.Expr{
   191  				makeFunctionExprForTest(">", []*plan.Expr{
   192  					makeColExprForTest(0, types.T_float64),
   193  					plan2.MakePlan2Float64ConstExprWithType(10),
   194  				}),
   195  				makeFunctionExprForTest(">", []*plan.Expr{
   196  					makeColExprForTest(0, types.T_float64),
   197  					plan2.MakePlan2Float64ConstExprWithType(30),
   198  				}),
   199  				makeFunctionExprForTest("<=", []*plan.Expr{
   200  					makeColExprForTest(0, types.T_float64),
   201  					plan2.MakePlan2Float64ConstExprWithType(-10),
   202  				}),
   203  				makeFunctionExprForTest("<", []*plan.Expr{
   204  					makeColExprForTest(0, types.T_float64),
   205  					plan2.MakePlan2Float64ConstExprWithType(-10),
   206  				}),
   207  				makeFunctionExprForTest(">", []*plan.Expr{
   208  					makeFunctionExprForTest("+", []*plan.Expr{
   209  						makeColExprForTest(0, types.T_float64),
   210  						makeColExprForTest(1, types.T_float64),
   211  					}),
   212  					plan2.MakePlan2Float64ConstExprWithType(60),
   213  				}),
   214  				makeFunctionExprForTest("<", []*plan.Expr{
   215  					makeFunctionExprForTest("+", []*plan.Expr{
   216  						makeColExprForTest(0, types.T_float64),
   217  						makeColExprForTest(1, types.T_float64),
   218  					}),
   219  					plan2.MakePlan2Float64ConstExprWithType(-5),
   220  				}),
   221  				makeFunctionExprForTest("<", []*plan.Expr{
   222  					makeFunctionExprForTest("-", []*plan.Expr{
   223  						makeColExprForTest(0, types.T_float64),
   224  						makeColExprForTest(1, types.T_float64),
   225  					}),
   226  					plan2.MakePlan2Float64ConstExprWithType(-34),
   227  				}),
   228  				makeFunctionExprForTest("<", []*plan.Expr{
   229  					makeFunctionExprForTest("-", []*plan.Expr{
   230  						makeColExprForTest(0, types.T_float64),
   231  						makeColExprForTest(1, types.T_float64),
   232  					}),
   233  					plan2.MakePlan2Float64ConstExprWithType(-35),
   234  				}),
   235  				makeFunctionExprForTest("<=", []*plan.Expr{
   236  					makeFunctionExprForTest("-", []*plan.Expr{
   237  						makeColExprForTest(0, types.T_float64),
   238  						makeColExprForTest(1, types.T_float64),
   239  					}),
   240  					plan2.MakePlan2Float64ConstExprWithType(-35),
   241  				}),
   242  				makeFunctionExprForTest(">", []*plan.Expr{
   243  					makeColExprForTest(0, types.T_float64),
   244  					makeColExprForTest(1, types.T_float64),
   245  				}),
   246  				makeFunctionExprForTest(">", []*plan.Expr{
   247  					makeColExprForTest(0, types.T_float64),
   248  					makeFunctionExprForTest("+", []*plan.Expr{
   249  						makeColExprForTest(1, types.T_float64),
   250  						plan2.MakePlan2Float64ConstExprWithType(15),
   251  					}),
   252  				}),
   253  				makeFunctionExprForTest(">=", []*plan.Expr{
   254  					makeColExprForTest(0, types.T_float64),
   255  					makeFunctionExprForTest("+", []*plan.Expr{
   256  						makeColExprForTest(1, types.T_float64),
   257  						plan2.MakePlan2Float64ConstExprWithType(15),
   258  					}),
   259  				}),
   260  				makeFunctionExprForTest("or", []*plan.Expr{
   261  					makeFunctionExprForTest(">", []*plan.Expr{
   262  						makeColExprForTest(0, types.T_float64),
   263  						plan2.MakePlan2Float64ConstExprWithType(100),
   264  					}),
   265  					makeFunctionExprForTest(">", []*plan.Expr{
   266  						makeColExprForTest(1, types.T_float64),
   267  						plan2.MakePlan2Float64ConstExprWithType(10),
   268  					}),
   269  				}),
   270  				makeFunctionExprForTest("and", []*plan.Expr{
   271  					makeFunctionExprForTest(">", []*plan.Expr{
   272  						makeColExprForTest(0, types.T_float64),
   273  						plan2.MakePlan2Float64ConstExprWithType(100),
   274  					}),
   275  					makeFunctionExprForTest("<", []*plan.Expr{
   276  						makeColExprForTest(1, types.T_float64),
   277  						plan2.MakePlan2Float64ConstExprWithType(0),
   278  					}),
   279  				}),
   280  				makeFunctionExprForTest(">", []*plan.Expr{
   281  					makeColExprForTest(3, types.T_varchar),
   282  					plan2.MakePlan2StringConstExprWithType("xyz"),
   283  				}),
   284  				makeFunctionExprForTest("<=", []*plan.Expr{
   285  					makeColExprForTest(3, types.T_varchar),
   286  					plan2.MakePlan2StringConstExprWithType("efg"),
   287  				}),
   288  				makeFunctionExprForTest("<", []*plan.Expr{
   289  					makeColExprForTest(3, types.T_varchar),
   290  					plan2.MakePlan2StringConstExprWithType("efg"),
   291  				}),
   292  				makeFunctionExprForTest(">", []*plan.Expr{
   293  					makeColExprForTest(2, types.T_varchar),
   294  					makeColExprForTest(3, types.T_varchar),
   295  				}),
   296  				makeFunctionExprForTest("<", []*plan.Expr{
   297  					makeColExprForTest(2, types.T_varchar),
   298  					makeColExprForTest(3, types.T_varchar),
   299  				}),
   300  			},
   301  			meta: func() objectio.BlockObject {
   302  				objDataMeta := objectio.BuildMetaData(1, 4)
   303  				meta := objDataMeta.GetBlockMeta(0)
   304  				meta.MustGetColumn(0).SetZoneMap(zm0)
   305  				meta.MustGetColumn(1).SetZoneMap(zm1)
   306  				meta.MustGetColumn(2).SetZoneMap(zm2)
   307  				meta.MustGetColumn(3).SetZoneMap(zm3)
   308  				return meta
   309  			}(),
   310  			expect: []bool{
   311  				true, false, true, false, false, false, true, false, true, true,
   312  				false, true, true, false, true, true, false, true, true,
   313  			},
   314  		},
   315  	}
   316  
   317  	columnMap := map[int]int{0: 0, 1: 1, 2: 2, 3: 3}
   318  
   319  	for _, tc := range cases {
   320  		for i, expr := range tc.exprs {
   321  			cnt := plan2.AssignAuxIdForExpr(expr, 0)
   322  			zms := make([]objectio.ZoneMap, cnt)
   323  			vecs := make([]*vector.Vector, cnt)
   324  			zm := colexec.EvaluateFilterByZoneMap(context.Background(), proc, expr, tc.meta, columnMap, zms, vecs)
   325  			require.Equal(t, tc.expect[i], zm, tc.desc[i])
   326  		}
   327  	}
   328  	require.Zero(t, m.CurrNB())
   329  }
   330  
   331  func TestMustGetFullCompositePK(t *testing.T) {
   332  	m := mpool.MustNewNoFixed(t.Name())
   333  	proc := testutil.NewProcessWithMPool(m)
   334  	packer := types.NewPacker(m)
   335  	type myCase struct {
   336  		desc    []string
   337  		exprs   []*plan.Expr
   338  		can     []bool
   339  		expects []func(bool, []byte) bool
   340  	}
   341  	// a, b, c, d are columns of table t1
   342  	// d,a,c are composite primary keys
   343  	// b is the serialized primary key
   344  
   345  	var placeHolder func(bool, []byte) bool
   346  	returnTrue := func(bool, []byte) bool { return true }
   347  
   348  	var val_1_2_3 []byte
   349  	packer.EncodeInt64(1)
   350  	packer.EncodeInt64(2)
   351  	packer.EncodeInt64(3)
   352  	val_1_2_3 = packer.Bytes()
   353  	packer.Reset()
   354  	packer.EncodeInt64(4)
   355  	packer.EncodeInt64(5)
   356  	packer.EncodeInt64(6)
   357  	val_4_5_6 := packer.Bytes()
   358  	packer.Reset()
   359  
   360  	tc := myCase{
   361  		// (d,a,c) => b
   362  		desc: []string{
   363  			"1. c=1 and (d=2 and a=3)",
   364  			"2. c=1 and d=2",
   365  			"3. d=1 and b=(1:2:3)",
   366  			"4. d=1 and b in ((1:2:3),(4:5:6))",
   367  			"5. (d=1 and a=2) or c=1",
   368  			"6. (d=1 and a=2) or (d=1 and a=2 and c=3)",
   369  			"7. (d=1 and a=2 and c=3) or (d=4 and a=5 and c=6)",
   370  		},
   371  		exprs: []*plan.Expr{
   372  			makeFunctionExprForTest("and", []*plan.Expr{
   373  				makeFunctionExprForTest("=", []*plan.Expr{
   374  					makeColExprForTest(2, types.T_int64),
   375  					plan2.MakePlan2Int64ConstExprWithType(1),
   376  				}),
   377  				makeFunctionExprForTest("and", []*plan.Expr{
   378  					makeFunctionExprForTest("=", []*plan.Expr{
   379  						makeColExprForTest(3, types.T_int64),
   380  						plan2.MakePlan2Int64ConstExprWithType(2),
   381  					}),
   382  					makeFunctionExprForTest("=", []*plan.Expr{
   383  						makeColExprForTest(0, types.T_int64),
   384  						plan2.MakePlan2Int64ConstExprWithType(3),
   385  					}),
   386  				}),
   387  			}),
   388  			makeFunctionExprForTest("and", []*plan.Expr{
   389  				makeFunctionExprForTest("=", []*plan.Expr{
   390  					makeColExprForTest(2, types.T_int64),
   391  					plan2.MakePlan2Int64ConstExprWithType(1),
   392  				}),
   393  				makeFunctionExprForTest("=", []*plan.Expr{
   394  					makeColExprForTest(3, types.T_int64),
   395  					plan2.MakePlan2Int64ConstExprWithType(2),
   396  				}),
   397  			}),
   398  			makeFunctionExprForTest("and", []*plan.Expr{
   399  				makeFunctionExprForTest("=", []*plan.Expr{
   400  					makeColExprForTest(3, types.T_int64),
   401  					plan2.MakePlan2Int64ConstExprWithType(1),
   402  				}),
   403  				makeFunctionExprForTest("=", []*plan.Expr{
   404  					makeColExprForTest(1, types.T_varchar),
   405  					plan2.MakePlan2StringConstExprWithType(string(util.UnsafeBytesToString(val_1_2_3))),
   406  				}),
   407  			}),
   408  			makeFunctionExprForTest("and", []*plan.Expr{
   409  				makeFunctionExprForTest("=", []*plan.Expr{
   410  					makeColExprForTest(3, types.T_int64),
   411  					plan2.MakePlan2Int64ConstExprWithType(1),
   412  				}),
   413  				makeFunctionExprForTest("in", []*plan.Expr{
   414  					makeColExprForTest(1, types.T_varchar),
   415  					plan2.MakePlan2StringVecExprWithType(m, util.UnsafeBytesToString(val_1_2_3), util.UnsafeBytesToString(val_4_5_6)),
   416  				}),
   417  			}),
   418  			// "5. (d=1 and a=2) or c=1",
   419  			makeFunctionExprForTest("or", []*plan.Expr{
   420  				makeFunctionExprForTest("and", []*plan.Expr{
   421  					makeFunctionExprForTest("=", []*plan.Expr{
   422  						makeColExprForTest(3, types.T_int64),
   423  						plan2.MakePlan2Int64ConstExprWithType(1),
   424  					}),
   425  					makeFunctionExprForTest("=", []*plan.Expr{
   426  						makeColExprForTest(0, types.T_int64),
   427  						plan2.MakePlan2Int64ConstExprWithType(2),
   428  					}),
   429  				}),
   430  				makeFunctionExprForTest("=", []*plan.Expr{
   431  					makeColExprForTest(2, types.T_int64),
   432  					plan2.MakePlan2Int64ConstExprWithType(1),
   433  				}),
   434  			}),
   435  			// "6. (d=1 and a=2) or (d=1 and a=2 and c=3)",
   436  			makeFunctionExprForTest("or", []*plan.Expr{
   437  				makeFunctionExprForTest("and", []*plan.Expr{
   438  					makeFunctionExprForTest("=", []*plan.Expr{
   439  						makeColExprForTest(3, types.T_int64),
   440  						plan2.MakePlan2Int64ConstExprWithType(1),
   441  					}),
   442  					makeFunctionExprForTest("=", []*plan.Expr{
   443  						makeColExprForTest(0, types.T_int64),
   444  						plan2.MakePlan2Int64ConstExprWithType(2),
   445  					}),
   446  				}),
   447  				makeFunctionExprForTest("and", []*plan.Expr{
   448  					makeFunctionExprForTest("=", []*plan.Expr{
   449  						makeColExprForTest(3, types.T_int64),
   450  						plan2.MakePlan2Int64ConstExprWithType(1),
   451  					}),
   452  					makeFunctionExprForTest("and", []*plan.Expr{
   453  						makeFunctionExprForTest("=", []*plan.Expr{
   454  							makeColExprForTest(0, types.T_int64),
   455  							plan2.MakePlan2Int64ConstExprWithType(2),
   456  						}),
   457  						makeFunctionExprForTest("=", []*plan.Expr{
   458  							makeColExprForTest(2, types.T_int64),
   459  							plan2.MakePlan2Int64ConstExprWithType(3),
   460  						}),
   461  					}),
   462  				}),
   463  			}),
   464  			// "7. (d=1 and a=2 and c=3) or (d=4 and a=5 and c=6)",
   465  			makeFunctionExprForTest("or", []*plan.Expr{
   466  				makeFunctionExprForTest("and", []*plan.Expr{
   467  					makeFunctionExprForTest("=", []*plan.Expr{
   468  						makeColExprForTest(3, types.T_int64),
   469  						plan2.MakePlan2Int64ConstExprWithType(1),
   470  					}),
   471  					makeFunctionExprForTest("and", []*plan.Expr{
   472  						makeFunctionExprForTest("=", []*plan.Expr{
   473  							makeColExprForTest(0, types.T_int64),
   474  							plan2.MakePlan2Int64ConstExprWithType(2),
   475  						}),
   476  						makeFunctionExprForTest("=", []*plan.Expr{
   477  							makeColExprForTest(2, types.T_int64),
   478  							plan2.MakePlan2Int64ConstExprWithType(3),
   479  						}),
   480  					}),
   481  				}),
   482  				makeFunctionExprForTest("and", []*plan.Expr{
   483  					makeFunctionExprForTest("=", []*plan.Expr{
   484  						makeColExprForTest(3, types.T_int64),
   485  						plan2.MakePlan2Int64ConstExprWithType(4),
   486  					}),
   487  					makeFunctionExprForTest("and", []*plan.Expr{
   488  						makeFunctionExprForTest("=", []*plan.Expr{
   489  							makeColExprForTest(0, types.T_int64),
   490  							plan2.MakePlan2Int64ConstExprWithType(5),
   491  						}),
   492  						makeFunctionExprForTest("=", []*plan.Expr{
   493  							makeColExprForTest(2, types.T_int64),
   494  							plan2.MakePlan2Int64ConstExprWithType(6),
   495  						}),
   496  					}),
   497  				}),
   498  			}),
   499  		},
   500  		can: []bool{
   501  			true, false, true, true, false, false, true,
   502  		},
   503  		expects: []func(bool, []byte) bool{
   504  			placeHolder, returnTrue, placeHolder, placeHolder, returnTrue,
   505  			returnTrue, placeHolder,
   506  		},
   507  	}
   508  
   509  	tc.expects[0] = func(isVec bool, actual []byte) bool {
   510  		require.False(t, isVec)
   511  		packer.Reset()
   512  		packer.EncodeInt64(2)
   513  		packer.EncodeInt64(3)
   514  		packer.EncodeInt64(1)
   515  		expect := packer.Bytes()
   516  		packer.Reset()
   517  		return bytes.Equal(expect, actual)
   518  	}
   519  	tc.expects[2] = func(isVec bool, actual []byte) bool {
   520  		require.False(t, isVec)
   521  		return bytes.Equal(val_1_2_3, actual)
   522  	}
   523  	tc.expects[3] = func(isVec bool, actual []byte) bool {
   524  		require.True(t, isVec)
   525  		vec := vector.NewVec(types.T_any.ToType())
   526  		vec.UnmarshalBinary(actual)
   527  		return bytes.Equal(val_1_2_3, vec.GetBytesAt(0)) && bytes.Equal(val_4_5_6, vec.GetBytesAt(1))
   528  	}
   529  	tc.expects[6] = func(isVec bool, actual []byte) bool {
   530  		require.True(t, isVec)
   531  		vec := vector.NewVec(types.T_any.ToType())
   532  		vec.UnmarshalBinary(actual)
   533  		return bytes.Equal(val_1_2_3, vec.GetBytesAt(0)) && bytes.Equal(val_4_5_6, vec.GetBytesAt(1))
   534  	}
   535  
   536  	pkName := "b"
   537  	keys := []string{"d", "a", "c"}
   538  	for i := 0; i < len(tc.desc); i++ {
   539  		expr := tc.exprs[i]
   540  		can, isVec, data := MustGetFullCompositePKValue(
   541  			expr, pkName, keys, packer, proc,
   542  		)
   543  		require.Equalf(t, tc.can[i], can, tc.desc[i])
   544  		require.Truef(t, tc.expects[i](isVec, data), tc.desc[i])
   545  	}
   546  	packer.FreeMem()
   547  	proc.FreeVectors()
   548  	require.Zero(t, m.CurrNB())
   549  }
   550  
   551  func TestGetCompositePkValueByExpr(t *testing.T) {
   552  	type myCase struct {
   553  		desc    []string
   554  		exprs   []*plan.Expr
   555  		expect  []int
   556  		hasNull []bool
   557  	}
   558  	// a, b, c, d are columns of table t1
   559  	// d,c,b are composite primary key
   560  	tc := myCase{
   561  		desc: []string{
   562  			"a=10", "a=20 and b=10", "a=20 and d=10", "b=20 and c=10",
   563  			"b=10 and d=20", "b=10 and c=20 and d=30",
   564  			"c=10 and d=20", "d=10 or a=10", "d=10 or c=20 and a=30",
   565  			"d=10 or c=20 and d=30", "d=10 and c=20 or d=30",
   566  			"d=null", "c=null", "b=null", "null=b", "c=null and a=10",
   567  		},
   568  		hasNull: []bool{
   569  			false, false, false, false, false, false, false, false, false, false, false,
   570  			true, true, true, true, true,
   571  		},
   572  		expect: []int{
   573  			0, 0, 1, 0, 1, 3, 2, 0, 0, 1, 0,
   574  			0, 0, 0, 0, 0,
   575  		},
   576  		exprs: []*plan.Expr{
   577  			makeFunctionExprForTest("=", []*plan.Expr{
   578  				makeColExprForTest(0, types.T_float64),
   579  				plan2.MakePlan2Float64ConstExprWithType(10),
   580  			}),
   581  			makeFunctionExprForTest("and", []*plan.Expr{
   582  				makeFunctionExprForTest("=", []*plan.Expr{
   583  					makeColExprForTest(0, types.T_float64),
   584  					plan2.MakePlan2Float64ConstExprWithType(20),
   585  				}),
   586  				makeFunctionExprForTest("=", []*plan.Expr{
   587  					makeColExprForTest(1, types.T_float64),
   588  					plan2.MakePlan2Float64ConstExprWithType(10),
   589  				}),
   590  			}),
   591  			makeFunctionExprForTest("and", []*plan.Expr{
   592  				makeFunctionExprForTest("=", []*plan.Expr{
   593  					makeColExprForTest(0, types.T_float64),
   594  					plan2.MakePlan2Float64ConstExprWithType(20),
   595  				}),
   596  				makeFunctionExprForTest("=", []*plan.Expr{
   597  					makeColExprForTest(3, types.T_float64),
   598  					plan2.MakePlan2Float64ConstExprWithType(10),
   599  				}),
   600  			}),
   601  			makeFunctionExprForTest("and", []*plan.Expr{
   602  				makeFunctionExprForTest("=", []*plan.Expr{
   603  					makeColExprForTest(1, types.T_float64),
   604  					plan2.MakePlan2Float64ConstExprWithType(20),
   605  				}),
   606  				makeFunctionExprForTest("=", []*plan.Expr{
   607  					makeColExprForTest(2, types.T_float64),
   608  					plan2.MakePlan2Float64ConstExprWithType(10),
   609  				}),
   610  			}),
   611  			makeFunctionExprForTest("and", []*plan.Expr{
   612  				makeFunctionExprForTest("=", []*plan.Expr{
   613  					makeColExprForTest(1, types.T_float64),
   614  					plan2.MakePlan2Float64ConstExprWithType(10),
   615  				}),
   616  				makeFunctionExprForTest("=", []*plan.Expr{
   617  					makeColExprForTest(3, types.T_float64),
   618  					plan2.MakePlan2Float64ConstExprWithType(20),
   619  				}),
   620  			}),
   621  			makeFunctionExprForTest("and", []*plan.Expr{
   622  				makeFunctionExprForTest("=", []*plan.Expr{
   623  					makeColExprForTest(1, types.T_float64),
   624  					plan2.MakePlan2Float64ConstExprWithType(10),
   625  				}),
   626  				makeFunctionExprForTest("and", []*plan.Expr{
   627  					makeFunctionExprForTest("=", []*plan.Expr{
   628  						makeColExprForTest(2, types.T_float64),
   629  						plan2.MakePlan2Float64ConstExprWithType(20),
   630  					}),
   631  					makeFunctionExprForTest("=", []*plan.Expr{
   632  						makeColExprForTest(3, types.T_float64),
   633  						plan2.MakePlan2Float64ConstExprWithType(30),
   634  					}),
   635  				}),
   636  			}),
   637  			makeFunctionExprForTest("and", []*plan.Expr{
   638  				makeFunctionExprForTest("=", []*plan.Expr{
   639  					makeColExprForTest(2, types.T_float64),
   640  					plan2.MakePlan2Float64ConstExprWithType(10),
   641  				}),
   642  				makeFunctionExprForTest("=", []*plan.Expr{
   643  					makeColExprForTest(3, types.T_float64),
   644  					plan2.MakePlan2Float64ConstExprWithType(20),
   645  				}),
   646  			}),
   647  			makeFunctionExprForTest("or", []*plan.Expr{
   648  				makeFunctionExprForTest("=", []*plan.Expr{
   649  					makeColExprForTest(2, types.T_float64),
   650  					plan2.MakePlan2Float64ConstExprWithType(20),
   651  				}),
   652  				makeFunctionExprForTest("=", []*plan.Expr{
   653  					makeColExprForTest(0, types.T_float64),
   654  					plan2.MakePlan2Float64ConstExprWithType(0),
   655  				}),
   656  			}),
   657  			makeFunctionExprForTest("and", []*plan.Expr{
   658  				makeFunctionExprForTest("or", []*plan.Expr{
   659  					makeFunctionExprForTest("=", []*plan.Expr{
   660  						makeColExprForTest(3, types.T_float64),
   661  						plan2.MakePlan2Float64ConstExprWithType(10),
   662  					}),
   663  					makeFunctionExprForTest("=", []*plan.Expr{
   664  						makeColExprForTest(2, types.T_float64),
   665  						plan2.MakePlan2Float64ConstExprWithType(20),
   666  					}),
   667  				}),
   668  				makeFunctionExprForTest("=", []*plan.Expr{
   669  					makeColExprForTest(0, types.T_float64),
   670  					plan2.MakePlan2Float64ConstExprWithType(30),
   671  				}),
   672  			}),
   673  			makeFunctionExprForTest("and", []*plan.Expr{
   674  				makeFunctionExprForTest("or", []*plan.Expr{
   675  					makeFunctionExprForTest("=", []*plan.Expr{
   676  						makeColExprForTest(3, types.T_float64),
   677  						plan2.MakePlan2Float64ConstExprWithType(10),
   678  					}),
   679  					makeFunctionExprForTest("=", []*plan.Expr{
   680  						makeColExprForTest(2, types.T_float64),
   681  						plan2.MakePlan2Float64ConstExprWithType(20),
   682  					}),
   683  				}),
   684  				makeFunctionExprForTest("=", []*plan.Expr{
   685  					makeColExprForTest(3, types.T_float64),
   686  					plan2.MakePlan2Float64ConstExprWithType(30),
   687  				}),
   688  			}),
   689  			makeFunctionExprForTest("or", []*plan.Expr{
   690  				makeFunctionExprForTest("and", []*plan.Expr{
   691  					makeFunctionExprForTest("=", []*plan.Expr{
   692  						makeColExprForTest(3, types.T_float64),
   693  						plan2.MakePlan2Float64ConstExprWithType(10),
   694  					}),
   695  					makeFunctionExprForTest("=", []*plan.Expr{
   696  						makeColExprForTest(2, types.T_float64),
   697  						plan2.MakePlan2Float64ConstExprWithType(20),
   698  					}),
   699  				}),
   700  				makeFunctionExprForTest("=", []*plan.Expr{
   701  					makeColExprForTest(3, types.T_float64),
   702  					plan2.MakePlan2Float64ConstExprWithType(30),
   703  				}),
   704  			}),
   705  		},
   706  	}
   707  	pks := []string{"d", "c", "b"}
   708  	for i, expr := range tc.exprs {
   709  		vals := make([]*plan.Literal, len(pks))
   710  		ok, hasNull := getCompositPKVals(expr, pks, vals, nil)
   711  		cnt := 0
   712  		require.Equal(t, tc.hasNull[i], hasNull)
   713  		if hasNull {
   714  			require.False(t, ok)
   715  			continue
   716  		}
   717  		if ok {
   718  			for _, val := range vals {
   719  				t.Logf("val: %v", val)
   720  			}
   721  			cnt = getValidCompositePKCnt(vals)
   722  		}
   723  		require.Equal(t, tc.expect[i], cnt)
   724  	}
   725  }
   726  
   727  func TestGetNonIntPkValueByExpr(t *testing.T) {
   728  	type asserts = struct {
   729  		result bool
   730  		data   any
   731  		expr   *plan.Expr
   732  		typ    types.T
   733  	}
   734  
   735  	testCases := []asserts{
   736  		// a > "a"  false   only 'and', '=' function is supported
   737  		{false, 0, makeFunctionExprForTest(">", []*plan.Expr{
   738  			makeColExprForTest(0, types.T_int64),
   739  			plan2.MakePlan2StringConstExprWithType("a"),
   740  		}), types.T_int64},
   741  		// a = 100  true
   742  		{true, int64(100),
   743  			makeFunctionExprForTest("=", []*plan.Expr{
   744  				makeColExprForTest(0, types.T_int64),
   745  				plan2.MakePlan2Int64ConstExprWithType(100),
   746  			}), types.T_int64},
   747  		// b > 10 and a = "abc"  true
   748  		{true, []byte("abc"),
   749  			makeFunctionExprForTest("and", []*plan.Expr{
   750  				makeFunctionExprForTest(">", []*plan.Expr{
   751  					makeColExprForTest(1, types.T_int64),
   752  					plan2.MakePlan2Int64ConstExprWithType(10),
   753  				}),
   754  				makeFunctionExprForTest("=", []*plan.Expr{
   755  					makeColExprForTest(0, types.T_int64),
   756  					plan2.MakePlan2StringConstExprWithType("abc"),
   757  				}),
   758  			}), types.T_char},
   759  	}
   760  
   761  	t.Run("test getPkValueByExpr", func(t *testing.T) {
   762  		for i, testCase := range testCases {
   763  			result, _, _, data := getPkValueByExpr(testCase.expr, "a", testCase.typ, true, nil)
   764  			if result != testCase.result {
   765  				t.Fatalf("test getPkValueByExpr at cases[%d], get result is different with expected", i)
   766  			}
   767  			if result {
   768  				if a, ok := data.([]byte); ok {
   769  					b := testCase.data.([]byte)
   770  					if !bytes.Equal(a, b) {
   771  						t.Fatalf("test getPkValueByExpr at cases[%d], data is not match", i)
   772  					}
   773  				} else {
   774  					if data != testCase.data {
   775  						t.Fatalf("test getPkValueByExpr at cases[%d], data is not match", i)
   776  					}
   777  				}
   778  			}
   779  		}
   780  	})
   781  }
   782  
   783  func mockStatsList(t *testing.T, statsCnt int) (statsList []objectio.ObjectStats) {
   784  	for idx := 0; idx < statsCnt; idx++ {
   785  		stats := objectio.NewObjectStats()
   786  		blkCnt := rand.Uint32()%100 + 1
   787  		require.Nil(t, objectio.SetObjectStatsBlkCnt(stats, blkCnt))
   788  		require.Nil(t, objectio.SetObjectStatsRowCnt(stats, options.DefaultBlockMaxRows*(blkCnt-1)+options.DefaultBlockMaxRows*6/10))
   789  		require.Nil(t, objectio.SetObjectStatsObjectName(stats, objectio.BuildObjectName(objectio.NewSegmentid(), uint16(blkCnt))))
   790  		require.Nil(t, objectio.SetObjectStatsExtent(stats, objectio.NewExtent(0, 0, 0, 0)))
   791  		require.Nil(t, objectio.SetObjectStatsSortKeyZoneMap(stats, index.NewZM(types.T_bool, 1)))
   792  
   793  		statsList = append(statsList, *stats)
   794  	}
   795  
   796  	return
   797  }
   798  
   799  func TestNewStatsBlkIter(t *testing.T) {
   800  	stats := mockStatsList(t, 1)[0]
   801  	blks := UnfoldBlkInfoFromObjStats(&stats)
   802  
   803  	iter := NewStatsBlkIter(&stats, nil)
   804  	for iter.Next() {
   805  		actual := iter.Entry()
   806  		id := actual.BlockID.Sequence()
   807  		require.Equal(t, blks[id].BlockID, actual.BlockID)
   808  
   809  		loc1 := objectio.Location(blks[id].MetaLoc[:])
   810  		loc2 := objectio.Location(actual.MetaLoc[:])
   811  		require.Equal(t, loc1.Name(), loc2.Name())
   812  		require.Equal(t, loc1.Extent(), loc2.Extent())
   813  		require.Equal(t, loc1.ID(), loc2.ID())
   814  		require.Equal(t, loc1.Rows(), loc2.Rows())
   815  	}
   816  }
   817  
   818  func TestForeachBlkInObjStatsList(t *testing.T) {
   819  	statsList := mockStatsList(t, 100)
   820  
   821  	count := 0
   822  	ForeachBlkInObjStatsList(false, nil, func(blk objectio.BlockInfo, _ objectio.BlockObject) bool {
   823  		count++
   824  		return false
   825  	}, statsList...)
   826  
   827  	require.Equal(t, count, 1)
   828  
   829  	count = 0
   830  	ForeachBlkInObjStatsList(true, nil, func(blk objectio.BlockInfo, _ objectio.BlockObject) bool {
   831  		count++
   832  		return false
   833  	}, statsList...)
   834  
   835  	require.Equal(t, count, len(statsList))
   836  
   837  	count = 0
   838  	ForeachBlkInObjStatsList(true, nil, func(blk objectio.BlockInfo, _ objectio.BlockObject) bool {
   839  		count++
   840  		return true
   841  	}, statsList...)
   842  
   843  	objectio.ForeachObjectStats(func(stats *objectio.ObjectStats) bool {
   844  		count -= int(stats.BlkCnt())
   845  		return true
   846  	}, statsList...)
   847  
   848  	require.Equal(t, count, 0)
   849  
   850  	count = 0
   851  	ForeachBlkInObjStatsList(false, nil, func(blk objectio.BlockInfo, _ objectio.BlockObject) bool {
   852  		count++
   853  		return true
   854  	}, statsList...)
   855  
   856  	objectio.ForeachObjectStats(func(stats *objectio.ObjectStats) bool {
   857  		count -= int(stats.BlkCnt())
   858  		return true
   859  	}, statsList...)
   860  
   861  	require.Equal(t, count, 0)
   862  }
   863  
   864  func TestGetNonCompositePKSerachFuncByExpr(t *testing.T) {
   865  	m := mpool.MustNewNoFixed(t.Name())
   866  	proc := testutil.NewProcessWithMPool(m)
   867  
   868  	bat := makeBatchForTest(m, 0, 2, 3, 4)
   869  
   870  	vals := vector.NewVec(types.T_int64.ToType())
   871  	ints := []int64{3, 4, 2}
   872  	for _, n := range ints {
   873  		vector.AppendFixed(vals, n, false, m)
   874  	}
   875  	colExpr := newColumnExpr(0, plan2.MakePlan2Type(vals.GetType()), "pk")
   876  
   877  	//vals must be sorted
   878  	vals.InplaceSort()
   879  	bytes, _ := vals.MarshalBinary()
   880  	vals.Free(m)
   881  
   882  	inExpr := plan2.MakeInExpr(
   883  		context.Background(),
   884  		colExpr,
   885  		int32(vals.Length()),
   886  		bytes,
   887  		false)
   888  	_, _, filter := getNonCompositePKSearchFuncByExpr(
   889  		inExpr,
   890  		"pk",
   891  		proc)
   892  	sels := filter(bat.Vecs)
   893  	require.Equal(t, 3, len(sels))
   894  	require.True(t, sels[0] == 1)
   895  	require.True(t, sels[1] == 2)
   896  	require.True(t, sels[2] == 3)
   897  }
   898  
   899  func TestGetPKExpr(t *testing.T) {
   900  	m := mpool.MustNewNoFixed(t.Name())
   901  	proc := testutil.NewProcessWithMPool(m)
   902  	type myCase struct {
   903  		desc     []string
   904  		exprs    []*plan.Expr
   905  		valExprs []*plan.Expr
   906  	}
   907  	tc := myCase{
   908  		desc: []string{
   909  			"a=10",
   910  			"a=20 and a=10",
   911  			"30=a and 20=a",
   912  			"a in (1,2)",
   913  			"b=40 and a=50",
   914  			"a=60 or b=70",
   915  			"b=80 and c=90",
   916  			"a=60 or a=70",
   917  			"a=60 or (a in (70,80))",
   918  			"(a=10 or b=20) or a=30",
   919  			"(a=10 or b=20) and a=30",
   920  			"(b=10 and a=20) or a=30",
   921  		},
   922  		exprs: []*plan.Expr{
   923  			// a=10
   924  			makeFunctionExprForTest("=", []*plan.Expr{
   925  				makeColExprForTest(0, types.T_int64),
   926  				plan2.MakePlan2Int64ConstExprWithType(10),
   927  			}),
   928  			// a=20 and a=10
   929  			makeFunctionExprForTest("and", []*plan.Expr{
   930  				makeFunctionExprForTest("=", []*plan.Expr{
   931  					makeColExprForTest(0, types.T_int64),
   932  					plan2.MakePlan2Int64ConstExprWithType(20),
   933  				}),
   934  				makeFunctionExprForTest("=", []*plan.Expr{
   935  					makeColExprForTest(0, types.T_int64),
   936  					plan2.MakePlan2Int64ConstExprWithType(10),
   937  				}),
   938  			}),
   939  			// 30=a and 20=a
   940  			makeFunctionExprForTest("and", []*plan.Expr{
   941  				makeFunctionExprForTest("=", []*plan.Expr{
   942  					plan2.MakePlan2Int64ConstExprWithType(30),
   943  					makeColExprForTest(0, types.T_int64),
   944  				}),
   945  				makeFunctionExprForTest("=", []*plan.Expr{
   946  					plan2.MakePlan2Int64ConstExprWithType(20),
   947  					makeColExprForTest(0, types.T_int64),
   948  				}),
   949  			}),
   950  			// a in (1,2)
   951  			makeInExprForTest[int64](
   952  				makeColExprForTest(0, types.T_int64),
   953  				[]int64{1, 2},
   954  				types.T_int64,
   955  				m,
   956  			),
   957  			makeFunctionExprForTest("and", []*plan.Expr{
   958  				makeFunctionExprForTest("=", []*plan.Expr{
   959  					makeColExprForTest(1, types.T_int64),
   960  					plan2.MakePlan2Int64ConstExprWithType(40),
   961  				}),
   962  				makeFunctionExprForTest("=", []*plan.Expr{
   963  					makeColExprForTest(0, types.T_int64),
   964  					plan2.MakePlan2Int64ConstExprWithType(50),
   965  				}),
   966  			}),
   967  			makeFunctionExprForTest("or", []*plan.Expr{
   968  				makeFunctionExprForTest("=", []*plan.Expr{
   969  					makeColExprForTest(0, types.T_int64),
   970  					plan2.MakePlan2Int64ConstExprWithType(60),
   971  				}),
   972  				makeFunctionExprForTest("=", []*plan.Expr{
   973  					makeColExprForTest(1, types.T_int64),
   974  					plan2.MakePlan2Int64ConstExprWithType(70),
   975  				}),
   976  			}),
   977  			makeFunctionExprForTest("and", []*plan.Expr{
   978  				makeFunctionExprForTest("=", []*plan.Expr{
   979  					makeColExprForTest(1, types.T_int64),
   980  					plan2.MakePlan2Int64ConstExprWithType(80),
   981  				}),
   982  				makeFunctionExprForTest("=", []*plan.Expr{
   983  					makeColExprForTest(2, types.T_int64),
   984  					plan2.MakePlan2Int64ConstExprWithType(90),
   985  				}),
   986  			}),
   987  			makeFunctionExprForTest("or", []*plan.Expr{
   988  				makeFunctionExprForTest("=", []*plan.Expr{
   989  					makeColExprForTest(0, types.T_int64),
   990  					plan2.MakePlan2Int64ConstExprWithType(60),
   991  				}),
   992  				makeFunctionExprForTest("=", []*plan.Expr{
   993  					makeColExprForTest(0, types.T_int64),
   994  					plan2.MakePlan2Int64ConstExprWithType(70),
   995  				}),
   996  			}),
   997  			makeFunctionExprForTest("or", []*plan.Expr{
   998  				makeFunctionExprForTest("=", []*plan.Expr{
   999  					makeColExprForTest(0, types.T_int64),
  1000  					plan2.MakePlan2Int64ConstExprWithType(60),
  1001  				}),
  1002  				makeInExprForTest[int64](
  1003  					makeColExprForTest(0, types.T_int64),
  1004  					[]int64{70, 80},
  1005  					types.T_int64,
  1006  					m,
  1007  				),
  1008  			}),
  1009  			makeFunctionExprForTest("or", []*plan.Expr{
  1010  				makeFunctionExprForTest("or", []*plan.Expr{
  1011  					makeFunctionExprForTest("=", []*plan.Expr{
  1012  						makeColExprForTest(0, types.T_int64),
  1013  						plan2.MakePlan2Int64ConstExprWithType(10),
  1014  					}),
  1015  					makeFunctionExprForTest("=", []*plan.Expr{
  1016  						makeColExprForTest(1, types.T_int64),
  1017  						plan2.MakePlan2Int64ConstExprWithType(20),
  1018  					}),
  1019  				}),
  1020  				makeFunctionExprForTest("=", []*plan.Expr{
  1021  					makeColExprForTest(0, types.T_int64),
  1022  					plan2.MakePlan2Int64ConstExprWithType(30),
  1023  				}),
  1024  			}),
  1025  			makeFunctionExprForTest("and", []*plan.Expr{
  1026  				makeFunctionExprForTest("or", []*plan.Expr{
  1027  					makeFunctionExprForTest("=", []*plan.Expr{
  1028  						makeColExprForTest(0, types.T_int64),
  1029  						plan2.MakePlan2Int64ConstExprWithType(10),
  1030  					}),
  1031  					makeFunctionExprForTest("=", []*plan.Expr{
  1032  						makeColExprForTest(1, types.T_int64),
  1033  						plan2.MakePlan2Int64ConstExprWithType(20),
  1034  					}),
  1035  				}),
  1036  				makeFunctionExprForTest("=", []*plan.Expr{
  1037  					makeColExprForTest(0, types.T_int64),
  1038  					plan2.MakePlan2Int64ConstExprWithType(30),
  1039  				}),
  1040  			}),
  1041  			makeFunctionExprForTest("or", []*plan.Expr{
  1042  				makeFunctionExprForTest("and", []*plan.Expr{
  1043  					makeFunctionExprForTest("=", []*plan.Expr{
  1044  						makeColExprForTest(1, types.T_int64),
  1045  						plan2.MakePlan2Int64ConstExprWithType(10),
  1046  					}),
  1047  					makeFunctionExprForTest("=", []*plan.Expr{
  1048  						makeColExprForTest(0, types.T_int64),
  1049  						plan2.MakePlan2Int64ConstExprWithType(20),
  1050  					}),
  1051  				}),
  1052  				makeFunctionExprForTest("=", []*plan.Expr{
  1053  					makeColExprForTest(0, types.T_int64),
  1054  					plan2.MakePlan2Int64ConstExprWithType(30),
  1055  				}),
  1056  			}),
  1057  		},
  1058  		valExprs: []*plan.Expr{
  1059  			plan2.MakePlan2Int64ConstExprWithType(10),
  1060  			plan2.MakePlan2Int64ConstExprWithType(20),
  1061  			plan2.MakePlan2Int64ConstExprWithType(30),
  1062  			plan2.MakePlan2Int64VecExprWithType(m, int64(1), int64(2)),
  1063  			plan2.MakePlan2Int64ConstExprWithType(50),
  1064  			nil,
  1065  			nil,
  1066  			{
  1067  				Expr: &plan.Expr_List{
  1068  					List: &plan.ExprList{
  1069  						List: []*plan.Expr{
  1070  							plan2.MakePlan2Int64ConstExprWithType(60),
  1071  							plan2.MakePlan2Int64ConstExprWithType(70),
  1072  						},
  1073  					},
  1074  				},
  1075  				Typ: plan.Type{
  1076  					Id: int32(types.T_tuple),
  1077  				},
  1078  			},
  1079  			{
  1080  				Expr: &plan.Expr_List{
  1081  					List: &plan.ExprList{
  1082  						List: []*plan.Expr{
  1083  							plan2.MakePlan2Int64ConstExprWithType(60),
  1084  							plan2.MakePlan2Int64VecExprWithType(m, int64(70), int64(80)),
  1085  						},
  1086  					},
  1087  				},
  1088  				Typ: plan.Type{
  1089  					Id: int32(types.T_tuple),
  1090  				},
  1091  			},
  1092  			nil,
  1093  			plan2.MakePlan2Int64ConstExprWithType(30),
  1094  			{
  1095  				Expr: &plan.Expr_List{
  1096  					List: &plan.ExprList{
  1097  						List: []*plan.Expr{
  1098  							plan2.MakePlan2Int64ConstExprWithType(20),
  1099  							plan2.MakePlan2Int64ConstExprWithType(30),
  1100  						},
  1101  					},
  1102  				},
  1103  				Typ: plan.Type{
  1104  					Id: int32(types.T_tuple),
  1105  				},
  1106  			},
  1107  		},
  1108  	}
  1109  	pkName := "a"
  1110  	for i, expr := range tc.exprs {
  1111  		rExpr := getPkExpr(expr, pkName, proc)
  1112  		// if rExpr != nil {
  1113  		// 	t.Logf("%s||||%s||||%s", plan2.FormatExpr(expr), plan2.FormatExpr(rExpr), tc.desc[i])
  1114  		// }
  1115  		require.Equalf(t, tc.valExprs[i], rExpr, tc.desc[i])
  1116  	}
  1117  	require.Zero(t, m.CurrNB())
  1118  }
  1119  
  1120  func TestGetPkExprValue(t *testing.T) {
  1121  	m := mpool.MustNewZeroNoFixed()
  1122  	proc := testutil.NewProcessWithMPool(m)
  1123  	type testCase struct {
  1124  		desc       []string
  1125  		exprs      []*plan.Expr
  1126  		expectVals [][]int64
  1127  		canEvals   []bool
  1128  		hasNull    []bool
  1129  	}
  1130  	equalToVecFn := func(expect []int64, actual any) bool {
  1131  		vec := vector.NewVec(types.T_any.ToType())
  1132  		_ = vec.UnmarshalBinary(actual.([]byte))
  1133  		actualVals := vector.MustFixedCol[int64](vec)
  1134  		if len(expect) != len(actualVals) {
  1135  			return false
  1136  		}
  1137  		for i := range expect {
  1138  			if expect[i] != actualVals[i] {
  1139  				return false
  1140  			}
  1141  		}
  1142  		return true
  1143  	}
  1144  	equalToValFn := func(expect []int64, actual any) bool {
  1145  		if len(expect) != 1 {
  1146  			return false
  1147  		}
  1148  		actualVal := actual.(int64)
  1149  		return expect[0] == actualVal
  1150  	}
  1151  
  1152  	nullExpr := plan2.MakePlan2Int64ConstExprWithType(0)
  1153  	nullExpr.Expr.(*plan.Expr_Lit).Lit.Isnull = true
  1154  
  1155  	tc := testCase{
  1156  		desc: []string{
  1157  			"a=2 and a=1",
  1158  			"a in vec(1,2)",
  1159  			"a=2 or a=1 or a=3",
  1160  			"a in vec(1,10) or a=5 or (a=6 and a=7)",
  1161  			"a=null",
  1162  			"a=1 or a=null or a=2",
  1163  		},
  1164  		canEvals: []bool{
  1165  			true, true, true, true, false, true,
  1166  		},
  1167  		hasNull: []bool{
  1168  			false, false, false, false, true, false,
  1169  		},
  1170  		exprs: []*plan.Expr{
  1171  			makeFunctionExprForTest("and", []*plan.Expr{
  1172  				makeFunctionExprForTest("=", []*plan.Expr{
  1173  					makeColExprForTest(0, types.T_int64),
  1174  					plan2.MakePlan2Int64ConstExprWithType(2),
  1175  				}),
  1176  				makeFunctionExprForTest("=", []*plan.Expr{
  1177  					makeColExprForTest(0, types.T_int64),
  1178  					plan2.MakePlan2Int64ConstExprWithType(1),
  1179  				}),
  1180  			}),
  1181  			makeFunctionExprForTest("in", []*plan.Expr{
  1182  				makeColExprForTest(0, types.T_int64),
  1183  				plan2.MakePlan2Int64VecExprWithType(m, int64(1), int64(2)),
  1184  			}),
  1185  			makeFunctionExprForTest("or", []*plan.Expr{
  1186  				makeFunctionExprForTest("or", []*plan.Expr{
  1187  					makeFunctionExprForTest("=", []*plan.Expr{
  1188  						makeColExprForTest(0, types.T_int64),
  1189  						plan2.MakePlan2Int64ConstExprWithType(2),
  1190  					}),
  1191  					makeFunctionExprForTest("=", []*plan.Expr{
  1192  						makeColExprForTest(0, types.T_int64),
  1193  						plan2.MakePlan2Int64ConstExprWithType(1),
  1194  					}),
  1195  				}),
  1196  				makeFunctionExprForTest("=", []*plan.Expr{
  1197  					makeColExprForTest(0, types.T_int64),
  1198  					plan2.MakePlan2Int64ConstExprWithType(3),
  1199  				}),
  1200  			}),
  1201  			makeFunctionExprForTest("or", []*plan.Expr{
  1202  				makeFunctionExprForTest("or", []*plan.Expr{
  1203  					makeFunctionExprForTest("in", []*plan.Expr{
  1204  						makeColExprForTest(0, types.T_int64),
  1205  						plan2.MakePlan2Int64VecExprWithType(m, int64(1), int64(10)),
  1206  					}),
  1207  					makeFunctionExprForTest("=", []*plan.Expr{
  1208  						makeColExprForTest(0, types.T_int64),
  1209  						plan2.MakePlan2Int64ConstExprWithType(5),
  1210  					}),
  1211  				}),
  1212  				makeFunctionExprForTest("and", []*plan.Expr{
  1213  					makeFunctionExprForTest("=", []*plan.Expr{
  1214  						makeColExprForTest(0, types.T_int64),
  1215  						plan2.MakePlan2Int64ConstExprWithType(6),
  1216  					}),
  1217  					makeFunctionExprForTest("=", []*plan.Expr{
  1218  						makeColExprForTest(0, types.T_int64),
  1219  						plan2.MakePlan2Int64ConstExprWithType(7),
  1220  					}),
  1221  				}),
  1222  			}),
  1223  			makeFunctionExprForTest("=", []*plan.Expr{
  1224  				makeColExprForTest(0, types.T_int64),
  1225  				nullExpr,
  1226  			}),
  1227  			makeFunctionExprForTest("or", []*plan.Expr{
  1228  				makeFunctionExprForTest("or", []*plan.Expr{
  1229  					makeFunctionExprForTest("=", []*plan.Expr{
  1230  						makeColExprForTest(0, types.T_int64),
  1231  						plan2.MakePlan2Int64ConstExprWithType(1),
  1232  					}),
  1233  					makeFunctionExprForTest("=", []*plan.Expr{
  1234  						makeColExprForTest(0, types.T_int64),
  1235  						nullExpr,
  1236  					}),
  1237  				}),
  1238  				makeFunctionExprForTest("=", []*plan.Expr{
  1239  					makeColExprForTest(0, types.T_int64),
  1240  					plan2.MakePlan2Int64ConstExprWithType(2),
  1241  				}),
  1242  			}),
  1243  		},
  1244  		expectVals: [][]int64{
  1245  			{2}, {1, 2}, {1, 2, 3}, {1, 5, 6, 10}, {}, {1, 2},
  1246  		},
  1247  	}
  1248  	for i, expr := range tc.exprs {
  1249  		canEval, isNull, isVec, val := getPkValueByExpr(expr, "a", types.T_int64, false, proc)
  1250  		require.Equalf(t, tc.hasNull[i], isNull, tc.desc[i])
  1251  		require.Equalf(t, tc.canEvals[i], canEval, tc.desc[i])
  1252  		if !canEval {
  1253  			continue
  1254  		}
  1255  		if isVec {
  1256  			require.Truef(t, equalToVecFn(tc.expectVals[i], val), tc.desc[i])
  1257  		} else {
  1258  			require.Truef(t, equalToValFn(tc.expectVals[i], val), tc.desc[i])
  1259  		}
  1260  	}
  1261  	expr := makeFunctionExprForTest("in", []*plan.Expr{
  1262  		makeColExprForTest(0, types.T_int64),
  1263  		plan2.MakePlan2Int64VecExprWithType(m, int64(1), int64(10)),
  1264  	})
  1265  	canEval, _, _, _ := getPkValueByExpr(expr, "a", types.T_int64, true, proc)
  1266  	require.False(t, canEval)
  1267  	canEval, _, _, _ = getPkValueByExpr(expr, "a", types.T_int64, false, proc)
  1268  	require.True(t, canEval)
  1269  
  1270  	expr = makeFunctionExprForTest("in", []*plan.Expr{
  1271  		makeColExprForTest(0, types.T_int64),
  1272  		plan2.MakePlan2Int64VecExprWithType(m, int64(1)),
  1273  	})
  1274  	canEval, _, _, val := getPkValueByExpr(expr, "a", types.T_int64, true, proc)
  1275  	require.True(t, canEval)
  1276  	require.True(t, equalToValFn([]int64{1}, val))
  1277  
  1278  	proc.FreeVectors()
  1279  	require.Zero(t, m.CurrNB())
  1280  }
  1281  
  1282  func TestEvalExprListToVec(t *testing.T) {
  1283  	m := mpool.MustNewZeroNoFixed()
  1284  	proc := testutil.NewProcessWithMPool(m)
  1285  	type testCase struct {
  1286  		desc     []string
  1287  		oids     []types.T
  1288  		exprs    []*plan.Expr_List
  1289  		canEvals []bool
  1290  		expects  []*vector.Vector
  1291  	}
  1292  	tc := testCase{
  1293  		desc: []string{
  1294  			"nil",
  1295  			"[i64(2), i64(1)]",
  1296  			"[i64(1), i64(2)]",
  1297  			"[i64(2), i64vec(1,10), [i64vec(4,8), i64(5)]]",
  1298  		},
  1299  		oids: []types.T{
  1300  			types.T_int64, types.T_int64, types.T_int64, types.T_int64,
  1301  		},
  1302  		exprs: []*plan.Expr_List{
  1303  			nil,
  1304  			{
  1305  				List: &plan.ExprList{
  1306  					List: []*plan.Expr{
  1307  						plan2.MakePlan2Int64ConstExprWithType(2),
  1308  						plan2.MakePlan2Int64ConstExprWithType(1),
  1309  					},
  1310  				},
  1311  			},
  1312  			{
  1313  				List: &plan.ExprList{
  1314  					List: []*plan.Expr{
  1315  						plan2.MakePlan2Int64ConstExprWithType(1),
  1316  						plan2.MakePlan2Int64ConstExprWithType(2),
  1317  					},
  1318  				},
  1319  			},
  1320  			{
  1321  				List: &plan.ExprList{
  1322  					List: []*plan.Expr{
  1323  						plan2.MakePlan2Int64ConstExprWithType(2),
  1324  						plan2.MakePlan2Int64VecExprWithType(m, int64(1), int64(10)),
  1325  						{
  1326  							Expr: &plan.Expr_List{
  1327  								List: &plan.ExprList{
  1328  									List: []*plan.Expr{
  1329  										plan2.MakePlan2Int64VecExprWithType(m, int64(4), int64(8)),
  1330  										plan2.MakePlan2Int64ConstExprWithType(5),
  1331  									},
  1332  								},
  1333  							},
  1334  						},
  1335  					},
  1336  				},
  1337  			},
  1338  		},
  1339  		canEvals: []bool{
  1340  			false, true, true, true,
  1341  		},
  1342  	}
  1343  	tc.expects = append(tc.expects, nil)
  1344  	vec := vector.NewVec(types.T_int64.ToType())
  1345  	vector.AppendAny(vec, int64(1), false, m)
  1346  	vector.AppendAny(vec, int64(2), false, m)
  1347  	tc.expects = append(tc.expects, vec)
  1348  	vec = vector.NewVec(types.T_int64.ToType())
  1349  	vector.AppendAny(vec, int64(1), false, m)
  1350  	vector.AppendAny(vec, int64(2), false, m)
  1351  	tc.expects = append(tc.expects, vec)
  1352  	vec = vector.NewVec(types.T_int64.ToType())
  1353  	vector.AppendAny(vec, int64(1), false, m)
  1354  	vector.AppendAny(vec, int64(2), false, m)
  1355  	vector.AppendAny(vec, int64(4), false, m)
  1356  	vector.AppendAny(vec, int64(5), false, m)
  1357  	vector.AppendAny(vec, int64(8), false, m)
  1358  	vector.AppendAny(vec, int64(10), false, m)
  1359  	tc.expects = append(tc.expects, vec)
  1360  
  1361  	for i, expr := range tc.exprs {
  1362  		// for _, e2 := range expr.List.List {
  1363  		// 	t.Log(plan2.FormatExpr(e2))
  1364  		// }
  1365  		canEval, vec, put := evalExprListToVec(tc.oids[i], expr, proc)
  1366  		require.Equalf(t, tc.canEvals[i], canEval, tc.desc[i])
  1367  		if canEval {
  1368  			require.NotNil(t, vec)
  1369  			require.Equal(t, tc.expects[i].String(), vec.String())
  1370  		} else {
  1371  			require.Equal(t, tc.expects[i], vec)
  1372  		}
  1373  		if put != nil {
  1374  			put()
  1375  		}
  1376  		if tc.expects[i] != nil {
  1377  			tc.expects[i].Free(m)
  1378  		}
  1379  	}
  1380  	proc.FreeVectors()
  1381  	require.Zero(t, m.CurrNB())
  1382  }
  1383  
  1384  func Test_removeIf(t *testing.T) {
  1385  	strs := []string{"abc", "bc", "def"}
  1386  
  1387  	del1 := make(map[string]struct{})
  1388  	del1["abc"] = struct{}{}
  1389  	res1 := removeIf[string](strs, func(t string) bool {
  1390  		return find[string](del1, t)
  1391  	})
  1392  	assert.Equal(t, []string{"bc", "def"}, res1)
  1393  
  1394  	del2 := make(map[string]struct{})
  1395  	for _, str := range strs {
  1396  		del2[str] = struct{}{}
  1397  	}
  1398  	res2 := removeIf[string](strs, func(t string) bool {
  1399  		return find[string](del2, t)
  1400  	})
  1401  	assert.Equal(t, []string{}, res2)
  1402  
  1403  	assert.Equal(t, []string(nil), removeIf[string](nil, nil))
  1404  }