github.com/matrixorigin/matrixone@v1.2.0/pkg/sql/colexec/evalExpression.go (about)

     1  // Copyright 2021 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 colexec
    16  
    17  import (
    18  	"context"
    19  	"fmt"
    20  	"math"
    21  
    22  	"github.com/matrixorigin/matrixone/pkg/common/moerr"
    23  	"github.com/matrixorigin/matrixone/pkg/common/mpool"
    24  	"github.com/matrixorigin/matrixone/pkg/common/reuse"
    25  	util2 "github.com/matrixorigin/matrixone/pkg/common/util"
    26  	"github.com/matrixorigin/matrixone/pkg/container/batch"
    27  	"github.com/matrixorigin/matrixone/pkg/container/types"
    28  	"github.com/matrixorigin/matrixone/pkg/container/vector"
    29  	"github.com/matrixorigin/matrixone/pkg/objectio"
    30  	"github.com/matrixorigin/matrixone/pkg/pb/plan"
    31  	"github.com/matrixorigin/matrixone/pkg/sql/plan/function"
    32  	"github.com/matrixorigin/matrixone/pkg/sql/util"
    33  	"github.com/matrixorigin/matrixone/pkg/vm/engine/tae/index"
    34  	"github.com/matrixorigin/matrixone/pkg/vm/process"
    35  )
    36  
    37  var (
    38  	constBType          = types.T_bool.ToType()
    39  	constI8Type         = types.T_int8.ToType()
    40  	constI16Type        = types.T_int16.ToType()
    41  	constI32Type        = types.T_int32.ToType()
    42  	constI64Type        = types.T_int64.ToType()
    43  	constU8Type         = types.T_uint8.ToType()
    44  	constU16Type        = types.T_uint16.ToType()
    45  	constU32Type        = types.T_uint32.ToType()
    46  	constU64Type        = types.T_uint64.ToType()
    47  	constFType          = types.T_float32.ToType()
    48  	constDType          = types.T_float64.ToType()
    49  	constSType          = types.T_varchar.ToType()
    50  	constBinType        = types.T_varbinary.ToType()
    51  	constDateType       = types.T_date.ToType()
    52  	constTimeType       = types.T_time.ToType()
    53  	constDatetimeType   = types.T_datetime.ToType()
    54  	constEnumType       = types.T_enum.ToType()
    55  	constTimestampTypes = []types.Type{
    56  		types.New(types.T_timestamp, 0, 0),
    57  		types.New(types.T_timestamp, 0, 1),
    58  		types.New(types.T_timestamp, 0, 2),
    59  		types.New(types.T_timestamp, 0, 3),
    60  		types.New(types.T_timestamp, 0, 4),
    61  		types.New(types.T_timestamp, 0, 5),
    62  		types.New(types.T_timestamp, 0, 6),
    63  	}
    64  	//No need to add T_array here, as Array is cast from varchar.
    65  )
    66  
    67  // ExpressionExecutor
    68  // generated from plan.Expr, can evaluate the result from vectors directly.
    69  type ExpressionExecutor interface {
    70  	// Eval will return the result vector of expression.
    71  	// the result memory is reused, so it should not be modified or saved.
    72  	// If it needs, it should be copied by vector.Dup().
    73  	Eval(proc *process.Process, batches []*batch.Batch) (*vector.Vector, error)
    74  
    75  	// EvalWithoutResultReusing is the same as Eval, but it will not reuse the memory of result vector.
    76  	// so you can save the result vector directly. but should be careful about memory leak.
    77  	// and watch out that maybe the vector is one of the input vectors of batches.
    78  	EvalWithoutResultReusing(proc *process.Process, batches []*batch.Batch) (*vector.Vector, error)
    79  
    80  	// Free should release all memory of executor.
    81  	// it will be called after query has done.
    82  	Free()
    83  
    84  	IsColumnExpr() bool
    85  }
    86  
    87  func NewExpressionExecutorsFromPlanExpressions(proc *process.Process, planExprs []*plan.Expr) (executors []ExpressionExecutor, err error) {
    88  	executors = make([]ExpressionExecutor, len(planExprs))
    89  	for i := range executors {
    90  		executors[i], err = NewExpressionExecutor(proc, planExprs[i])
    91  		if err != nil {
    92  			for j := 0; j < i; j++ {
    93  				executors[j].Free()
    94  			}
    95  			return nil, err
    96  		}
    97  	}
    98  	return executors, err
    99  }
   100  
   101  func NewExpressionExecutor(proc *process.Process, planExpr *plan.Expr) (ExpressionExecutor, error) {
   102  	switch t := planExpr.Expr.(type) {
   103  	case *plan.Expr_Lit:
   104  		typ := types.New(types.T(planExpr.Typ.Id), planExpr.Typ.Width, planExpr.Typ.Scale)
   105  		vec, err := generateConstExpressionExecutor(proc, typ, t.Lit)
   106  		if err != nil {
   107  			return nil, err
   108  		}
   109  		return NewFixedVectorExpressionExecutor(proc.Mp(), false, vec), nil
   110  
   111  	case *plan.Expr_T:
   112  		typ := types.New(types.T(planExpr.Typ.Id), planExpr.Typ.Width, planExpr.Typ.Scale)
   113  		vec := vector.NewConstNull(typ, 1, proc.Mp())
   114  		return NewFixedVectorExpressionExecutor(proc.Mp(), false, vec), nil
   115  
   116  	case *plan.Expr_Col:
   117  		typ := types.New(types.T(planExpr.Typ.Id), planExpr.Typ.Width, planExpr.Typ.Scale)
   118  		ce := NewColumnExpressionExecutor()
   119  		*ce = ColumnExpressionExecutor{
   120  			mp:       proc.Mp(),
   121  			relIndex: int(t.Col.RelPos),
   122  			colIndex: int(t.Col.ColPos),
   123  			typ:      typ,
   124  		}
   125  		return ce, nil
   126  
   127  	case *plan.Expr_P:
   128  		return NewParamExpressionExecutor(proc.Mp(), int(t.P.Pos), types.T_text.ToType()), nil
   129  
   130  	case *plan.Expr_V:
   131  		typ := types.New(types.T(planExpr.Typ.Id), planExpr.Typ.Width, planExpr.Typ.Scale)
   132  		ve := NewVarExpressionExecutor()
   133  		*ve = VarExpressionExecutor{
   134  			mp:     proc.Mp(),
   135  			name:   t.V.Name,
   136  			system: t.V.System,
   137  			global: t.V.Global,
   138  			typ:    typ,
   139  		}
   140  		return ve, nil
   141  
   142  	case *plan.Expr_Vec:
   143  		vec := vector.NewVec(types.T_any.ToType())
   144  		err := vec.UnmarshalBinary(t.Vec.Data)
   145  		if err != nil {
   146  			return nil, err
   147  		}
   148  		return NewFixedVectorExpressionExecutor(proc.Mp(), true, vec), nil
   149  
   150  	case *plan.Expr_F:
   151  		overload, err := function.GetFunctionById(proc.Ctx, t.F.GetFunc().GetObj())
   152  		if err != nil {
   153  			return nil, err
   154  		}
   155  
   156  		executor := NewFunctionExpressionExecutor()
   157  		typ := types.New(types.T(planExpr.Typ.Id), planExpr.Typ.Width, planExpr.Typ.Scale)
   158  		fn, fnFree := overload.GetExecuteMethod()
   159  		if err = executor.Init(proc, len(t.F.Args), typ, fn, fnFree); err != nil {
   160  			executor.Free()
   161  			return nil, err
   162  		}
   163  
   164  		for i := range executor.parameterExecutor {
   165  			subExecutor, paramErr := NewExpressionExecutor(proc, t.F.Args[i])
   166  			if paramErr != nil {
   167  				executor.Free()
   168  				return nil, paramErr
   169  			}
   170  			executor.SetParameter(i, subExecutor)
   171  		}
   172  
   173  		// IF all parameters here were constant. and this function can be folded.
   174  		// 	there is a better way to convert it as a FixedVectorExpressionExecutor.
   175  		if !overload.CannotFold() && !overload.IsRealTimeRelated() && ifAllArgsAreConstant(executor) {
   176  			for i := range executor.parameterExecutor {
   177  				fixExe := executor.parameterExecutor[i].(*FixedVectorExpressionExecutor)
   178  				executor.parameterResults[i] = fixExe.resultVector
   179  				if !fixExe.fixed {
   180  					executor.parameterResults[i].SetLength(1)
   181  				}
   182  			}
   183  
   184  			execLen := 1
   185  			if len(executor.parameterResults) > 0 {
   186  				firstParam := executor.parameterResults[0]
   187  				if !firstParam.IsConst() {
   188  					execLen = firstParam.Length()
   189  				}
   190  			}
   191  
   192  			if err = executor.resultVector.PreExtendAndReset(execLen); err != nil {
   193  				executor.Free()
   194  				return nil, err
   195  			}
   196  
   197  			err = executor.evalFn(executor.parameterResults, executor.resultVector, proc, execLen)
   198  			if err == nil {
   199  				mp := proc.Mp()
   200  
   201  				result := executor.resultVector.GetResultVector()
   202  				fixed := NewFixedVectorExpressionExecutor(mp, false, nil)
   203  
   204  				if execLen == 1 {
   205  					// ToConst just returns a new pointer to the same memory.
   206  					// so we need to duplicate it.
   207  					fixed.resultVector, err = result.ToConst(0, 1, mp).Dup(mp)
   208  				} else {
   209  					fixed.fixed = true
   210  					fixed.resultVector = result
   211  					executor.resultVector.SetResultVector(nil)
   212  				}
   213  				executor.Free()
   214  				if err != nil {
   215  					return nil, err
   216  				}
   217  				return fixed, nil
   218  			}
   219  			executor.Free()
   220  			return nil, err
   221  		}
   222  
   223  		return executor, nil
   224  	}
   225  
   226  	return nil, moerr.NewNYI(proc.Ctx, fmt.Sprintf("unsupported expression executor for %v now", planExpr))
   227  }
   228  
   229  func EvalExpressionOnce(proc *process.Process, planExpr *plan.Expr, batches []*batch.Batch) (*vector.Vector, error) {
   230  	executor, err := NewExpressionExecutor(proc, planExpr)
   231  	defer func() {
   232  		if executor != nil {
   233  			executor.Free()
   234  		}
   235  	}()
   236  	if err != nil {
   237  		return nil, err
   238  	}
   239  
   240  	vec, err := executor.Eval(proc, batches)
   241  	if err != nil {
   242  		return nil, err
   243  	}
   244  
   245  	// if memory reuse, we can get it directly because we do only one evaluate.
   246  	if e, ok := executor.(*FunctionExpressionExecutor); ok {
   247  		e.resultVector = nil
   248  		return vec, nil
   249  	}
   250  	if e, ok := executor.(*FixedVectorExpressionExecutor); ok {
   251  		e.resultVector = nil
   252  		return vec, nil
   253  	}
   254  
   255  	// I'm not sure if dup is good. but if not.
   256  	// we should check batch's cnt first, get if it's 1, dup if not.
   257  	nv, er := vec.Dup(proc.Mp())
   258  	if er != nil {
   259  		return nil, er
   260  	}
   261  	return nv, nil
   262  }
   263  
   264  func ifAllArgsAreConstant(executor *FunctionExpressionExecutor) bool {
   265  	for _, paramE := range executor.parameterExecutor {
   266  		if _, ok := paramE.(*FixedVectorExpressionExecutor); !ok {
   267  			return false
   268  		}
   269  	}
   270  	return true
   271  }
   272  
   273  // FixedVectorExpressionExecutor
   274  // the content of its vector is fixed.
   275  // e.g.
   276  //
   277  //	ConstVector [1, 1, 1, 1, 1]
   278  //	ConstVector [null, null, null]
   279  //	ListVector  ["1", "2", "3", null, "5"]
   280  type FixedVectorExpressionExecutor struct {
   281  	m *mpool.MPool
   282  
   283  	fixed        bool
   284  	resultVector *vector.Vector
   285  }
   286  
   287  type FunctionExpressionExecutor struct {
   288  	m *mpool.MPool
   289  
   290  	resultVector vector.FunctionResultWrapper
   291  	// parameters related
   292  	parameterResults  []*vector.Vector
   293  	parameterExecutor []ExpressionExecutor
   294  
   295  	evalFn func(
   296  		params []*vector.Vector,
   297  		result vector.FunctionResultWrapper,
   298  		proc *process.Process,
   299  		length int) error
   300  
   301  	freeFn func() error
   302  }
   303  
   304  type ColumnExpressionExecutor struct {
   305  	mp       *mpool.MPool
   306  	relIndex int
   307  	colIndex int
   308  
   309  	// result type.
   310  	typ types.Type
   311  	// we should new and cache a null vector here.
   312  	// because we need to change its type when doing the execution for const null vector.
   313  	// but other process may using its type at the same time.
   314  	nullVecCache *vector.Vector
   315  }
   316  
   317  func (expr *ColumnExpressionExecutor) GetRelIndex() int {
   318  	return expr.relIndex
   319  }
   320  
   321  func (expr *ColumnExpressionExecutor) GetColIndex() int {
   322  	return expr.colIndex
   323  }
   324  
   325  type ParamExpressionExecutor struct {
   326  	mp   *mpool.MPool
   327  	null *vector.Vector
   328  	vec  *vector.Vector
   329  	pos  int
   330  	typ  types.Type
   331  }
   332  
   333  func (expr *ParamExpressionExecutor) Eval(proc *process.Process, batches []*batch.Batch) (*vector.Vector, error) {
   334  	val, err := proc.GetPrepareParamsAt(expr.pos)
   335  	if err != nil {
   336  		return nil, err
   337  	}
   338  
   339  	if val == nil {
   340  		if expr.null == nil {
   341  			expr.null = vector.NewConstNull(expr.typ, 1, proc.GetMPool())
   342  		}
   343  		return expr.null, nil
   344  	}
   345  
   346  	if expr.vec == nil {
   347  		expr.vec, err = vector.NewConstBytes(expr.typ, val, 1, proc.Mp())
   348  	} else {
   349  		err = vector.SetConstBytes(expr.vec, val, 1, proc.GetMPool())
   350  	}
   351  	return expr.vec, err
   352  }
   353  
   354  func (expr *ParamExpressionExecutor) EvalWithoutResultReusing(proc *process.Process, batches []*batch.Batch) (*vector.Vector, error) {
   355  	vec, err := expr.Eval(proc, batches)
   356  	if err != nil {
   357  		return nil, err
   358  	}
   359  	if vec == expr.null {
   360  		expr.null = nil
   361  		return vec, nil
   362  	}
   363  	expr.vec = nil
   364  	return vec, nil
   365  }
   366  
   367  func (expr *ParamExpressionExecutor) Free() {
   368  	if expr == nil {
   369  		return
   370  	}
   371  
   372  	if expr.vec != nil {
   373  		expr.vec.Free(expr.mp)
   374  		expr.vec = nil
   375  	}
   376  	if expr.null != nil {
   377  		expr.null.Free(expr.mp)
   378  		expr.null = nil
   379  	}
   380  	reuse.Free[ParamExpressionExecutor](expr, nil)
   381  }
   382  
   383  func (expr *ParamExpressionExecutor) IsColumnExpr() bool {
   384  	return false
   385  }
   386  
   387  type VarExpressionExecutor struct {
   388  	mp   *mpool.MPool
   389  	null *vector.Vector
   390  	vec  *vector.Vector
   391  
   392  	name   string
   393  	system bool
   394  	global bool
   395  	typ    types.Type
   396  }
   397  
   398  func (expr *VarExpressionExecutor) Eval(proc *process.Process, batches []*batch.Batch) (*vector.Vector, error) {
   399  	val, err := proc.GetResolveVariableFunc()(expr.name, expr.system, expr.global)
   400  	if err != nil {
   401  		return nil, err
   402  	}
   403  
   404  	if val == nil {
   405  		if expr.null == nil {
   406  			expr.null, err = util.GenVectorByVarValue(proc, expr.typ, nil)
   407  		}
   408  		return expr.null, err
   409  	}
   410  
   411  	if expr.vec == nil {
   412  		expr.vec, err = util.GenVectorByVarValue(proc, expr.typ, val)
   413  	} else {
   414  		switch v := val.(type) {
   415  		case []byte:
   416  			err = vector.SetConstBytes(expr.vec, v, 1, proc.GetMPool())
   417  		case string:
   418  			err = vector.SetConstBytes(expr.vec, util2.UnsafeStringToBytes(v), 1, proc.GetMPool())
   419  		default:
   420  			err = vector.SetConstBytes(expr.vec, util2.UnsafeStringToBytes(fmt.Sprintf("%v", v)), 1, proc.GetMPool())
   421  		}
   422  	}
   423  	return expr.vec, err
   424  }
   425  
   426  func (expr *VarExpressionExecutor) EvalWithoutResultReusing(proc *process.Process, batches []*batch.Batch) (*vector.Vector, error) {
   427  	vec, err := expr.Eval(proc, batches)
   428  	if err != nil {
   429  		return nil, err
   430  	}
   431  	if vec == expr.null {
   432  		expr.null = nil
   433  		return vec, nil
   434  	}
   435  	expr.vec = nil
   436  	return vec, nil
   437  }
   438  
   439  func (expr *VarExpressionExecutor) Free() {
   440  	if expr == nil {
   441  		return
   442  	}
   443  	if expr.vec != nil {
   444  		expr.vec.Free(expr.mp)
   445  		expr.vec = nil
   446  	}
   447  	if expr.null != nil {
   448  		expr.null.Free(expr.mp)
   449  		expr.null = nil
   450  	}
   451  	reuse.Free[VarExpressionExecutor](expr, nil)
   452  }
   453  
   454  func (expr *VarExpressionExecutor) IsColumnExpr() bool {
   455  	return false
   456  }
   457  
   458  func (expr *FunctionExpressionExecutor) Init(
   459  	proc *process.Process,
   460  	parameterNum int,
   461  	retType types.Type,
   462  	fn func(
   463  		params []*vector.Vector,
   464  		result vector.FunctionResultWrapper,
   465  		proc *process.Process,
   466  		length int) error,
   467  	freeFn func() error) (err error) {
   468  	m := proc.Mp()
   469  
   470  	expr.m = m
   471  	expr.evalFn = fn
   472  	expr.freeFn = freeFn
   473  	expr.parameterResults = make([]*vector.Vector, parameterNum)
   474  	expr.parameterExecutor = make([]ExpressionExecutor, parameterNum)
   475  
   476  	expr.resultVector = vector.NewFunctionResultWrapper(proc.GetVector, proc.PutVector, retType, m)
   477  	return err
   478  }
   479  
   480  func (expr *FunctionExpressionExecutor) Eval(proc *process.Process, batches []*batch.Batch) (*vector.Vector, error) {
   481  	var err error
   482  	for i := range expr.parameterExecutor {
   483  		expr.parameterResults[i], err = expr.parameterExecutor[i].Eval(proc, batches)
   484  		if err != nil {
   485  			return nil, err
   486  		}
   487  	}
   488  
   489  	if err = expr.resultVector.PreExtendAndReset(batches[0].RowCount()); err != nil {
   490  		return nil, err
   491  	}
   492  
   493  	if err = expr.evalFn(
   494  		expr.parameterResults, expr.resultVector, proc, batches[0].RowCount()); err != nil {
   495  		return nil, err
   496  	}
   497  	return expr.resultVector.GetResultVector(), nil
   498  }
   499  
   500  func (expr *FunctionExpressionExecutor) EvalWithoutResultReusing(proc *process.Process, batches []*batch.Batch) (*vector.Vector, error) {
   501  	vec, err := expr.Eval(proc, batches)
   502  	if err != nil {
   503  		return nil, err
   504  	}
   505  	expr.resultVector.SetResultVector(nil)
   506  	return vec, nil
   507  }
   508  
   509  func (expr *FunctionExpressionExecutor) Free() {
   510  	if expr == nil {
   511  		return
   512  	}
   513  	if expr.resultVector != nil {
   514  		expr.resultVector.Free()
   515  		expr.resultVector = nil
   516  	}
   517  	for _, p := range expr.parameterExecutor {
   518  		if p != nil {
   519  			p.Free()
   520  		}
   521  	}
   522  	if expr.freeFn != nil {
   523  		_ = expr.freeFn()
   524  	}
   525  	reuse.Free[FunctionExpressionExecutor](expr, nil)
   526  }
   527  
   528  func (expr *FunctionExpressionExecutor) SetParameter(index int, executor ExpressionExecutor) {
   529  	expr.parameterExecutor[index] = executor
   530  }
   531  
   532  func (expr *FunctionExpressionExecutor) IsColumnExpr() bool {
   533  	return false
   534  }
   535  
   536  func (expr *ColumnExpressionExecutor) Eval(proc *process.Process, batches []*batch.Batch) (*vector.Vector, error) {
   537  	relIndex := expr.relIndex
   538  	// XXX it's a bad hack here. root cause is pipeline set a wrong relation index here.
   539  	if len(batches) == 1 {
   540  		relIndex = 0
   541  	}
   542  
   543  	// protected code. In fact, we shouldn't receive a wrong index here.
   544  	// if happens, it means it's a bad expression for the input data that we cannot calculate it.
   545  	if len(batches) <= relIndex || len(batches[relIndex].Vecs) <= expr.colIndex {
   546  		return nil, moerr.NewInternalError(proc.Ctx, "unexpected input batch for column expression")
   547  	}
   548  
   549  	vec := batches[relIndex].Vecs[expr.colIndex]
   550  	if vec.IsConstNull() {
   551  		vec = expr.getConstNullVec(expr.typ, vec.Length())
   552  	}
   553  	return vec, nil
   554  }
   555  
   556  func (expr *ColumnExpressionExecutor) getConstNullVec(typ types.Type, length int) *vector.Vector {
   557  	if expr.nullVecCache != nil {
   558  		expr.nullVecCache.SetType(typ)
   559  		expr.nullVecCache.SetLength(length)
   560  	} else {
   561  		expr.nullVecCache = vector.NewConstNull(typ, length, expr.mp)
   562  	}
   563  	return expr.nullVecCache
   564  }
   565  
   566  func (expr *ColumnExpressionExecutor) EvalWithoutResultReusing(proc *process.Process, batches []*batch.Batch) (*vector.Vector, error) {
   567  	vec, err := expr.Eval(proc, batches)
   568  	if vec == expr.nullVecCache {
   569  		expr.nullVecCache = nil
   570  	}
   571  	return vec, err
   572  }
   573  
   574  func (expr *ColumnExpressionExecutor) Free() {
   575  	if expr == nil {
   576  		return
   577  	}
   578  	if expr.nullVecCache != nil {
   579  		expr.nullVecCache.Free(expr.mp)
   580  		expr.nullVecCache = nil
   581  	}
   582  	reuse.Free[ColumnExpressionExecutor](expr, nil)
   583  }
   584  
   585  func (expr *ColumnExpressionExecutor) IsColumnExpr() bool {
   586  	return true
   587  }
   588  
   589  func (expr *FixedVectorExpressionExecutor) Eval(_ *process.Process, batches []*batch.Batch) (*vector.Vector, error) {
   590  	if !expr.fixed {
   591  		expr.resultVector.SetLength(batches[0].RowCount())
   592  	}
   593  	return expr.resultVector, nil
   594  }
   595  
   596  func (expr *FixedVectorExpressionExecutor) EvalWithoutResultReusing(proc *process.Process, batches []*batch.Batch) (*vector.Vector, error) {
   597  	vec, err := expr.Eval(proc, batches)
   598  	if err != nil {
   599  		return nil, err
   600  	}
   601  	return vec.Dup(proc.Mp())
   602  }
   603  
   604  func (expr *FixedVectorExpressionExecutor) Free() {
   605  	if expr == nil {
   606  		return
   607  	}
   608  	defer reuse.Free[FixedVectorExpressionExecutor](expr, nil)
   609  	if expr.resultVector == nil {
   610  		return
   611  	}
   612  	expr.resultVector.Free(expr.m)
   613  	expr.resultVector = nil
   614  }
   615  
   616  func (expr *FixedVectorExpressionExecutor) IsColumnExpr() bool {
   617  	return false
   618  }
   619  
   620  func generateConstExpressionExecutor(proc *process.Process, typ types.Type, con *plan.Literal) (vec *vector.Vector, err error) {
   621  	if con.GetIsnull() {
   622  		vec = vector.NewConstNull(typ, 1, proc.Mp())
   623  	} else {
   624  		switch val := con.GetValue().(type) {
   625  		case *plan.Literal_Bval:
   626  			vec, err = vector.NewConstFixed(constBType, val.Bval, 1, proc.Mp())
   627  		case *plan.Literal_I8Val:
   628  			vec, err = vector.NewConstFixed(constI8Type, int8(val.I8Val), 1, proc.Mp())
   629  		case *plan.Literal_I16Val:
   630  			vec, err = vector.NewConstFixed(constI16Type, int16(val.I16Val), 1, proc.Mp())
   631  		case *plan.Literal_I32Val:
   632  			vec, err = vector.NewConstFixed(constI32Type, val.I32Val, 1, proc.Mp())
   633  		case *plan.Literal_I64Val:
   634  			vec, err = vector.NewConstFixed(constI64Type, val.I64Val, 1, proc.Mp())
   635  		case *plan.Literal_U8Val:
   636  			vec, err = vector.NewConstFixed(constU8Type, uint8(val.U8Val), 1, proc.Mp())
   637  		case *plan.Literal_U16Val:
   638  			vec, err = vector.NewConstFixed(constU16Type, uint16(val.U16Val), 1, proc.Mp())
   639  		case *plan.Literal_U32Val:
   640  			vec, err = vector.NewConstFixed(constU32Type, val.U32Val, 1, proc.Mp())
   641  		case *plan.Literal_U64Val:
   642  			vec, err = vector.NewConstFixed(constU64Type, val.U64Val, 1, proc.Mp())
   643  		case *plan.Literal_Fval:
   644  			vec, err = vector.NewConstFixed(constFType, val.Fval, 1, proc.Mp())
   645  		case *plan.Literal_Dval:
   646  			vec, err = vector.NewConstFixed(constDType, val.Dval, 1, proc.Mp())
   647  		case *plan.Literal_Dateval:
   648  			vec, err = vector.NewConstFixed(constDateType, types.Date(val.Dateval), 1, proc.Mp())
   649  		case *plan.Literal_Timeval:
   650  			vec, err = vector.NewConstFixed(typ, types.Time(val.Timeval), 1, proc.Mp())
   651  		case *plan.Literal_Datetimeval:
   652  			vec, err = vector.NewConstFixed(typ, types.Datetime(val.Datetimeval), 1, proc.Mp())
   653  		case *plan.Literal_Decimal64Val:
   654  			cd64 := val.Decimal64Val
   655  			d64 := types.Decimal64(cd64.A)
   656  			vec, err = vector.NewConstFixed(typ, d64, 1, proc.Mp())
   657  		case *plan.Literal_Decimal128Val:
   658  			cd128 := val.Decimal128Val
   659  			d128 := types.Decimal128{B0_63: uint64(cd128.A), B64_127: uint64(cd128.B)}
   660  			vec, err = vector.NewConstFixed(typ, d128, 1, proc.Mp())
   661  		case *plan.Literal_Timestampval:
   662  			scale := typ.Scale
   663  			if scale < 0 || scale > 6 {
   664  				return nil, moerr.NewInternalError(proc.Ctx, "invalid timestamp scale")
   665  			}
   666  			vec, err = vector.NewConstFixed(constTimestampTypes[scale], types.Timestamp(val.Timestampval), 1, proc.Mp())
   667  		case *plan.Literal_Sval:
   668  			sval := val.Sval
   669  			// Distinguish binary with non-binary string.
   670  			if typ.Oid == types.T_binary || typ.Oid == types.T_varbinary || typ.Oid == types.T_blob {
   671  				vec, err = vector.NewConstBytes(constBinType, []byte(sval), 1, proc.Mp())
   672  			} else if typ.Oid == types.T_array_float32 {
   673  				array, err1 := types.StringToArray[float32](sval)
   674  				if err1 != nil {
   675  					return nil, err1
   676  				}
   677  				vec, err = vector.NewConstArray(typ, array, 1, proc.Mp())
   678  			} else if typ.Oid == types.T_array_float64 {
   679  				array, err1 := types.StringToArray[float64](sval)
   680  				if err1 != nil {
   681  					return nil, err1
   682  				}
   683  				vec, err = vector.NewConstArray(typ, array, 1, proc.Mp())
   684  			} else {
   685  				vec, err = vector.NewConstBytes(constSType, []byte(sval), 1, proc.Mp())
   686  			}
   687  		case *plan.Literal_Defaultval:
   688  			defaultVal := val.Defaultval
   689  			vec, err = vector.NewConstFixed(constBType, defaultVal, 1, proc.Mp())
   690  		case *plan.Literal_EnumVal:
   691  			vec, err = vector.NewConstFixed(constEnumType, types.Enum(val.EnumVal), 1, proc.Mp())
   692  		default:
   693  			return nil, moerr.NewNYI(proc.Ctx, fmt.Sprintf("const expression %v", con.GetValue()))
   694  		}
   695  		vec.SetIsBin(con.IsBin)
   696  	}
   697  	return vec, err
   698  }
   699  
   700  func GenerateConstListExpressionExecutor(proc *process.Process, exprs []*plan.Expr) (*vector.Vector, error) {
   701  	lenList := len(exprs)
   702  	vec, err := proc.AllocVectorOfRows(types.New(types.T(exprs[0].Typ.Id), exprs[0].Typ.Width, exprs[0].Typ.Scale), lenList, nil)
   703  	if err != nil {
   704  		return nil, err
   705  	}
   706  	for i := 0; i < lenList; i++ {
   707  		expr := exprs[i]
   708  		t := expr.GetLit()
   709  		if t == nil {
   710  			return nil, moerr.NewInternalError(proc.Ctx, "args in list must be constant")
   711  		}
   712  		if t.GetIsnull() {
   713  			vec.GetNulls().Set(uint64(i))
   714  		} else {
   715  			switch val := t.GetValue().(type) {
   716  			case *plan.Literal_Bval:
   717  				veccol := vector.MustFixedCol[bool](vec)
   718  				veccol[i] = val.Bval
   719  			case *plan.Literal_I8Val:
   720  				veccol := vector.MustFixedCol[int8](vec)
   721  				veccol[i] = int8(val.I8Val)
   722  			case *plan.Literal_I16Val:
   723  				veccol := vector.MustFixedCol[int16](vec)
   724  				veccol[i] = int16(val.I16Val)
   725  			case *plan.Literal_I32Val:
   726  				veccol := vector.MustFixedCol[int32](vec)
   727  				veccol[i] = val.I32Val
   728  			case *plan.Literal_I64Val:
   729  				veccol := vector.MustFixedCol[int64](vec)
   730  				veccol[i] = val.I64Val
   731  			case *plan.Literal_U8Val:
   732  				veccol := vector.MustFixedCol[uint8](vec)
   733  				veccol[i] = uint8(val.U8Val)
   734  			case *plan.Literal_U16Val:
   735  				veccol := vector.MustFixedCol[uint16](vec)
   736  				veccol[i] = uint16(val.U16Val)
   737  			case *plan.Literal_U32Val:
   738  				veccol := vector.MustFixedCol[uint32](vec)
   739  				veccol[i] = val.U32Val
   740  			case *plan.Literal_U64Val:
   741  				veccol := vector.MustFixedCol[uint64](vec)
   742  				veccol[i] = val.U64Val
   743  			case *plan.Literal_Fval:
   744  				veccol := vector.MustFixedCol[float32](vec)
   745  				veccol[i] = val.Fval
   746  			case *plan.Literal_Dval:
   747  				veccol := vector.MustFixedCol[float64](vec)
   748  				veccol[i] = val.Dval
   749  			case *plan.Literal_Dateval:
   750  				veccol := vector.MustFixedCol[types.Date](vec)
   751  				veccol[i] = types.Date(val.Dateval)
   752  			case *plan.Literal_Timeval:
   753  				veccol := vector.MustFixedCol[types.Time](vec)
   754  				veccol[i] = types.Time(val.Timeval)
   755  			case *plan.Literal_Datetimeval:
   756  				veccol := vector.MustFixedCol[types.Datetime](vec)
   757  				veccol[i] = types.Datetime(val.Datetimeval)
   758  			case *plan.Literal_Decimal64Val:
   759  				cd64 := val.Decimal64Val
   760  				d64 := types.Decimal64(cd64.A)
   761  				veccol := vector.MustFixedCol[types.Decimal64](vec)
   762  				veccol[i] = d64
   763  			case *plan.Literal_Decimal128Val:
   764  				cd128 := val.Decimal128Val
   765  				d128 := types.Decimal128{B0_63: uint64(cd128.A), B64_127: uint64(cd128.B)}
   766  				veccol := vector.MustFixedCol[types.Decimal128](vec)
   767  				veccol[i] = d128
   768  			case *plan.Literal_Timestampval:
   769  				scale := expr.Typ.Scale
   770  				if scale < 0 || scale > 6 {
   771  					return nil, moerr.NewInternalError(proc.Ctx, "invalid timestamp scale")
   772  				}
   773  				veccol := vector.MustFixedCol[types.Timestamp](vec)
   774  				veccol[i] = types.Timestamp(val.Timestampval)
   775  			case *plan.Literal_Sval:
   776  				sval := val.Sval
   777  				err = vector.SetStringAt(vec, i, sval, proc.Mp())
   778  				if err != nil {
   779  					return nil, err
   780  				}
   781  			case *plan.Literal_Defaultval:
   782  				defaultVal := val.Defaultval
   783  				veccol := vector.MustFixedCol[bool](vec)
   784  				veccol[i] = defaultVal
   785  			default:
   786  				return nil, moerr.NewNYI(proc.Ctx, fmt.Sprintf("const expression %v", t.GetValue()))
   787  			}
   788  			vec.SetIsBin(t.IsBin)
   789  		}
   790  	}
   791  	return vec, nil
   792  }
   793  
   794  // FixProjectionResult set result vector for rbat.
   795  // sbat is the source batch.
   796  func FixProjectionResult(proc *process.Process,
   797  	executors []ExpressionExecutor,
   798  	uafs []func(v, w *vector.Vector) error,
   799  	rbat *batch.Batch, sbat *batch.Batch) (dupSize int, err error) {
   800  	dupSize = 0
   801  
   802  	alreadySet := make([]bool, len(rbat.Vecs))
   803  	for i := range alreadySet {
   804  		alreadySet[i] = false
   805  	}
   806  
   807  	getNewVec := func(idx int, oldVec *vector.Vector) (*vector.Vector, error) {
   808  		if uafs[idx] != nil {
   809  			retVec := proc.GetVector(*oldVec.GetType())
   810  			retErr := uafs[idx](retVec, oldVec)
   811  			return retVec, retErr
   812  		} else {
   813  			return oldVec.Dup(proc.Mp())
   814  		}
   815  	}
   816  
   817  	for i, oldVec := range rbat.Vecs {
   818  		if !alreadySet[i] {
   819  			newVec := (*vector.Vector)(nil)
   820  			if columnExpr, ok := executors[i].(*ColumnExpressionExecutor); ok {
   821  				if sbat.GetCnt() == 1 {
   822  					if columnExpr.nullVecCache != nil && oldVec == columnExpr.nullVecCache {
   823  						newVec = vector.NewConstNull(columnExpr.typ, oldVec.Length(), proc.Mp())
   824  						dupSize += newVec.Size()
   825  						rbat.Vecs[i] = newVec
   826  					} else {
   827  						rplTimes := 0
   828  						for j := range rbat.Vecs {
   829  							if rbat.Vecs[j] == oldVec {
   830  								if rplTimes > 0 {
   831  									newVec, err = getNewVec(i, oldVec)
   832  									if err != nil {
   833  										if newVec != nil {
   834  											newVec.Free(proc.Mp())
   835  										}
   836  										return
   837  									}
   838  									dupSize += newVec.Size()
   839  									rbat.Vecs[j] = newVec
   840  								}
   841  								rplTimes++
   842  								alreadySet[j] = true
   843  							}
   844  						}
   845  						sbat.ReplaceVector(oldVec, nil)
   846  					}
   847  				} else {
   848  					newVec, err = getNewVec(i, oldVec)
   849  					if err != nil {
   850  						if newVec != nil {
   851  							newVec.Free(proc.Mp())
   852  						}
   853  						return
   854  					}
   855  					dupSize += newVec.Size()
   856  					rbat.Vecs[i] = newVec
   857  				}
   858  			} else if functionExpr, ok := executors[i].(*FunctionExpressionExecutor); ok {
   859  				// if projection, we can get the result directly
   860  				// newVec = functionExpr.resultVector.GetResultVector()
   861  				functionExpr.resultVector.SetResultVector(nil)
   862  			} else {
   863  				newVec, err = getNewVec(i, oldVec)
   864  				if err != nil {
   865  					if newVec != nil {
   866  						newVec.Free(proc.Mp())
   867  					}
   868  					return
   869  				}
   870  				dupSize += newVec.Size()
   871  				rbat.Vecs[i] = newVec
   872  			}
   873  			alreadySet[i] = true
   874  		}
   875  	}
   876  
   877  	return dupSize, nil
   878  }
   879  
   880  func NewJoinBatch(bat *batch.Batch, mp *mpool.MPool) (*batch.Batch,
   881  	[]func(*vector.Vector, *vector.Vector, int64, int) error) {
   882  	rbat := batch.NewWithSize(bat.VectorCount())
   883  	cfs := make([]func(*vector.Vector, *vector.Vector, int64, int) error, bat.VectorCount())
   884  	for i, vec := range bat.Vecs {
   885  		typ := *vec.GetType()
   886  		rbat.Vecs[i] = vector.NewConstNull(typ, 0, nil)
   887  		cfs[i] = vector.GetConstSetFunction(typ, mp)
   888  	}
   889  	return rbat, cfs
   890  }
   891  
   892  func SetJoinBatchValues(joinBat, bat *batch.Batch, sel int64, length int,
   893  	cfs []func(*vector.Vector, *vector.Vector, int64, int) error) error {
   894  	for i, vec := range bat.Vecs {
   895  		if err := cfs[i](joinBat.Vecs[i], vec, sel, length); err != nil {
   896  			return err
   897  		}
   898  	}
   899  	joinBat.SetRowCount(length)
   900  	return nil
   901  }
   902  
   903  var noColumnBatchForZoneMap = []*batch.Batch{batch.NewWithSize(0)}
   904  
   905  func getConstZM(
   906  	ctx context.Context,
   907  	expr *plan.Expr,
   908  	proc *process.Process,
   909  ) (zm index.ZM, err error) {
   910  	c := expr.GetLit()
   911  	typ := expr.Typ
   912  	if c.GetIsnull() {
   913  		zm = index.NewZM(types.T(typ.Id), typ.Scale)
   914  		return
   915  	}
   916  	switch val := c.GetValue().(type) {
   917  	case *plan.Literal_Bval:
   918  		zm = index.NewZM(constBType.Oid, 0)
   919  		v := val.Bval
   920  		index.UpdateZM(zm, types.EncodeBool(&v))
   921  	case *plan.Literal_I8Val:
   922  		zm = index.NewZM(constI8Type.Oid, 0)
   923  		v := int8(val.I8Val)
   924  		index.UpdateZM(zm, types.EncodeInt8(&v))
   925  	case *plan.Literal_I16Val:
   926  		zm = index.NewZM(constI16Type.Oid, 0)
   927  		v := int16(val.I16Val)
   928  		index.UpdateZM(zm, types.EncodeInt16(&v))
   929  	case *plan.Literal_I32Val:
   930  		zm = index.NewZM(constI32Type.Oid, 0)
   931  		v := val.I32Val
   932  		index.UpdateZM(zm, types.EncodeInt32(&v))
   933  	case *plan.Literal_I64Val:
   934  		zm = index.NewZM(constI64Type.Oid, 0)
   935  		v := val.I64Val
   936  		index.UpdateZM(zm, types.EncodeInt64(&v))
   937  	case *plan.Literal_U8Val:
   938  		zm = index.NewZM(constU8Type.Oid, 0)
   939  		v := uint8(val.U8Val)
   940  		index.UpdateZM(zm, types.EncodeUint8(&v))
   941  	case *plan.Literal_U16Val:
   942  		zm = index.NewZM(constU16Type.Oid, 0)
   943  		v := uint16(val.U16Val)
   944  		index.UpdateZM(zm, types.EncodeUint16(&v))
   945  	case *plan.Literal_U32Val:
   946  		zm = index.NewZM(constU32Type.Oid, 0)
   947  		v := val.U32Val
   948  		index.UpdateZM(zm, types.EncodeUint32(&v))
   949  	case *plan.Literal_U64Val:
   950  		zm = index.NewZM(constU64Type.Oid, 0)
   951  		v := val.U64Val
   952  		index.UpdateZM(zm, types.EncodeUint64(&v))
   953  	case *plan.Literal_Fval:
   954  		zm = index.NewZM(constFType.Oid, 0)
   955  		v := val.Fval
   956  		index.UpdateZM(zm, types.EncodeFloat32(&v))
   957  	case *plan.Literal_Dval:
   958  		zm = index.NewZM(constDType.Oid, 0)
   959  		v := val.Dval
   960  		index.UpdateZM(zm, types.EncodeFloat64(&v))
   961  	case *plan.Literal_Dateval:
   962  		zm = index.NewZM(constDateType.Oid, 0)
   963  		v := val.Dateval
   964  		index.UpdateZM(zm, types.EncodeInt32(&v))
   965  	case *plan.Literal_Timeval:
   966  		zm = index.NewZM(constTimeType.Oid, 0)
   967  		v := val.Timeval
   968  		index.UpdateZM(zm, types.EncodeInt64(&v))
   969  	case *plan.Literal_Datetimeval:
   970  		zm = index.NewZM(constDatetimeType.Oid, 0)
   971  		v := val.Datetimeval
   972  		index.UpdateZM(zm, types.EncodeInt64(&v))
   973  	case *plan.Literal_Decimal64Val:
   974  		v := val.Decimal64Val
   975  		zm = index.NewZM(types.T_decimal64, typ.Scale)
   976  		d64 := types.Decimal64(v.A)
   977  		index.UpdateZM(zm, types.EncodeDecimal64(&d64))
   978  	case *plan.Literal_Decimal128Val:
   979  		v := val.Decimal128Val
   980  		zm = index.NewZM(types.T_decimal128, typ.Scale)
   981  		d128 := types.Decimal128{B0_63: uint64(v.A), B64_127: uint64(v.B)}
   982  		index.UpdateZM(zm, types.EncodeDecimal128(&d128))
   983  	case *plan.Literal_Timestampval:
   984  		v := val.Timestampval
   985  		scale := typ.Scale
   986  		if scale < 0 || scale > 6 {
   987  			err = moerr.NewInternalError(proc.Ctx, "invalid timestamp scale")
   988  			return
   989  		}
   990  		zm = index.NewZM(constTimestampTypes[0].Oid, scale)
   991  		index.UpdateZM(zm, types.EncodeInt64(&v))
   992  	case *plan.Literal_Sval:
   993  		zm = index.NewZM(constSType.Oid, 0)
   994  		v := val.Sval
   995  		index.UpdateZM(zm, []byte(v))
   996  	case *plan.Literal_Defaultval:
   997  		zm = index.NewZM(constBType.Oid, 0)
   998  		v := val.Defaultval
   999  		index.UpdateZM(zm, types.EncodeBool(&v))
  1000  	case *plan.Literal_EnumVal:
  1001  		zm = index.NewZM(constEnumType.Oid, 0)
  1002  		v := types.Enum(val.EnumVal)
  1003  		index.UpdateZM(zm, types.EncodeEnum(&v))
  1004  	default:
  1005  		err = moerr.NewNYI(ctx, fmt.Sprintf("const expression %v", c.GetValue()))
  1006  	}
  1007  	return
  1008  }
  1009  
  1010  func EvaluateFilterByZoneMap(
  1011  	ctx context.Context, // why we need a context here, to escape trace?
  1012  	proc *process.Process,
  1013  	expr *plan.Expr,
  1014  	meta objectio.ColumnMetaFetcher,
  1015  	columnMap map[int]int,
  1016  	zms []objectio.ZoneMap,
  1017  	vecs []*vector.Vector) (selected bool) {
  1018  	if expr == nil {
  1019  		selected = true
  1020  		return
  1021  	}
  1022  
  1023  	if len(columnMap) == 0 {
  1024  		// XXX should we need to check expr.oid = bool or not ?
  1025  
  1026  		vec, err := EvalExpressionOnce(proc, expr, noColumnBatchForZoneMap)
  1027  		if err != nil {
  1028  			return true
  1029  		}
  1030  		cols := vector.MustFixedCol[bool](vec)
  1031  		for _, isNeed := range cols {
  1032  			if isNeed {
  1033  				vec.Free(proc.Mp())
  1034  				return true
  1035  			}
  1036  		}
  1037  		vec.Free(proc.Mp())
  1038  		return false
  1039  	}
  1040  
  1041  	zm := GetExprZoneMap(ctx, proc, expr, meta, columnMap, zms, vecs)
  1042  	if !zm.IsInited() || zm.GetType() != types.T_bool {
  1043  		selected = false
  1044  	} else {
  1045  		selected = types.DecodeBool(zm.GetMaxBuf())
  1046  	}
  1047  
  1048  	// clean the vector.
  1049  	for i := range vecs {
  1050  		if vecs[i] != nil {
  1051  			vecs[i].Free(proc.Mp())
  1052  			vecs[i] = nil
  1053  		}
  1054  	}
  1055  	return
  1056  }
  1057  
  1058  func GetExprZoneMap(
  1059  	ctx context.Context,
  1060  	proc *process.Process,
  1061  	expr *plan.Expr,
  1062  	meta objectio.ColumnMetaFetcher,
  1063  	columnMap map[int]int,
  1064  	zms []objectio.ZoneMap,
  1065  	vecs []*vector.Vector) (v objectio.ZoneMap) {
  1066  	var err error
  1067  
  1068  	switch t := expr.Expr.(type) {
  1069  	case *plan.Expr_Lit:
  1070  		if zms[expr.AuxId] == nil {
  1071  			if zms[expr.AuxId], err = getConstZM(ctx, expr, proc); err != nil {
  1072  				zms[expr.AuxId] = objectio.NewZM(types.T_bool, 0)
  1073  			}
  1074  		}
  1075  
  1076  	case *plan.Expr_Col:
  1077  		zms[expr.AuxId] = meta.MustGetColumn(uint16(columnMap[int(t.Col.ColPos)])).ZoneMap()
  1078  
  1079  	case *plan.Expr_F:
  1080  		id := t.F.GetFunc().GetObj()
  1081  		if overload, errGetFunc := function.GetFunctionById(ctx, id); errGetFunc != nil {
  1082  			zms[expr.AuxId].Reset()
  1083  
  1084  		} else {
  1085  			args := t.F.Args
  1086  
  1087  			// Some expressions need to be handled specifically
  1088  			switch t.F.Func.ObjName {
  1089  			case "isnull", "is_null":
  1090  				switch exprImpl := args[0].Expr.(type) {
  1091  				case *plan.Expr_Col:
  1092  					nullCnt := meta.MustGetColumn(uint16(columnMap[int(exprImpl.Col.ColPos)])).NullCnt()
  1093  					zms[expr.AuxId] = index.SetBool(zms[expr.AuxId], nullCnt > 0)
  1094  					return zms[expr.AuxId]
  1095  				default:
  1096  					zms[expr.AuxId].Reset()
  1097  					return zms[expr.AuxId]
  1098  				}
  1099  			case "isnotnull", "is_not_null":
  1100  				switch exprImpl := args[0].Expr.(type) {
  1101  				case *plan.Expr_Col:
  1102  					zm := meta.MustGetColumn(uint16(columnMap[int(exprImpl.Col.ColPos)])).ZoneMap()
  1103  					zms[expr.AuxId] = index.SetBool(zms[expr.AuxId], zm.IsInited())
  1104  					return zms[expr.AuxId]
  1105  				default:
  1106  					zms[expr.AuxId].Reset()
  1107  					return zms[expr.AuxId]
  1108  				}
  1109  			case "in":
  1110  				rid := args[1].AuxId
  1111  				if vecs[rid] == nil {
  1112  					if data, ok := args[1].Expr.(*plan.Expr_Vec); ok {
  1113  						vec := proc.GetVector(types.T_any.ToType())
  1114  						vec.UnmarshalBinary(data.Vec.Data)
  1115  						vecs[rid] = vec
  1116  					} else {
  1117  						zms[expr.AuxId].Reset()
  1118  						vecs[rid] = vector.NewConstNull(types.T_any.ToType(), math.MaxInt, proc.Mp())
  1119  						return zms[expr.AuxId]
  1120  					}
  1121  				}
  1122  
  1123  				if vecs[rid].IsConstNull() && vecs[rid].Length() == math.MaxInt {
  1124  					zms[expr.AuxId].Reset()
  1125  					return zms[expr.AuxId]
  1126  				}
  1127  
  1128  				lhs := GetExprZoneMap(ctx, proc, args[0], meta, columnMap, zms, vecs)
  1129  				if !lhs.IsInited() {
  1130  					zms[expr.AuxId].Reset()
  1131  					return zms[expr.AuxId]
  1132  				}
  1133  
  1134  				zms[expr.AuxId] = index.SetBool(zms[expr.AuxId], lhs.AnyIn(vecs[rid]))
  1135  				return zms[expr.AuxId]
  1136  
  1137  			case "prefix_eq":
  1138  				lhs := GetExprZoneMap(ctx, proc, args[0], meta, columnMap, zms, vecs)
  1139  				if !lhs.IsInited() {
  1140  					zms[expr.AuxId].Reset()
  1141  					return zms[expr.AuxId]
  1142  				}
  1143  
  1144  				s := []byte(args[1].GetLit().GetSval())
  1145  
  1146  				zms[expr.AuxId] = index.SetBool(zms[expr.AuxId], lhs.PrefixEq(s))
  1147  				return zms[expr.AuxId]
  1148  
  1149  			case "prefix_between":
  1150  				lhs := GetExprZoneMap(ctx, proc, args[0], meta, columnMap, zms, vecs)
  1151  				if !lhs.IsInited() {
  1152  					zms[expr.AuxId].Reset()
  1153  					return zms[expr.AuxId]
  1154  				}
  1155  
  1156  				lb := []byte(args[1].GetLit().GetSval())
  1157  				ub := []byte(args[2].GetLit().GetSval())
  1158  
  1159  				zms[expr.AuxId] = index.SetBool(zms[expr.AuxId], lhs.PrefixBetween(lb, ub))
  1160  				return zms[expr.AuxId]
  1161  
  1162  			case "prefix_in":
  1163  				rid := args[1].AuxId
  1164  				if vecs[rid] == nil {
  1165  					if data, ok := args[1].Expr.(*plan.Expr_Vec); ok {
  1166  						vec := proc.GetVector(types.T_any.ToType())
  1167  						vec.UnmarshalBinary(data.Vec.Data)
  1168  						vecs[rid] = vec
  1169  					} else {
  1170  						zms[expr.AuxId].Reset()
  1171  						vecs[rid] = vector.NewConstNull(types.T_any.ToType(), math.MaxInt, proc.Mp())
  1172  						return zms[expr.AuxId]
  1173  					}
  1174  				}
  1175  
  1176  				if vecs[rid].IsConstNull() && vecs[rid].Length() == math.MaxInt {
  1177  					zms[expr.AuxId].Reset()
  1178  					return zms[expr.AuxId]
  1179  				}
  1180  
  1181  				lhs := GetExprZoneMap(ctx, proc, args[0], meta, columnMap, zms, vecs)
  1182  				if !lhs.IsInited() {
  1183  					zms[expr.AuxId].Reset()
  1184  					return zms[expr.AuxId]
  1185  				}
  1186  
  1187  				zms[expr.AuxId] = index.SetBool(zms[expr.AuxId], lhs.PrefixIn(vecs[rid]))
  1188  				return zms[expr.AuxId]
  1189  			}
  1190  
  1191  			f := func() bool {
  1192  				for _, arg := range args {
  1193  					zms[arg.AuxId] = GetExprZoneMap(ctx, proc, arg, meta, columnMap, zms, vecs)
  1194  					if !zms[arg.AuxId].IsInited() {
  1195  						zms[expr.AuxId].Reset()
  1196  						return true
  1197  					}
  1198  				}
  1199  				return false
  1200  			}
  1201  
  1202  			var res, ok bool
  1203  			switch t.F.Func.ObjName {
  1204  			case ">":
  1205  				if f() {
  1206  					return zms[expr.AuxId]
  1207  				}
  1208  				if res, ok = zms[args[0].AuxId].AnyGT(zms[args[1].AuxId]); !ok {
  1209  					zms[expr.AuxId].Reset()
  1210  				} else {
  1211  					zms[expr.AuxId] = index.SetBool(zms[expr.AuxId], res)
  1212  				}
  1213  
  1214  			case "<":
  1215  				if f() {
  1216  					return zms[expr.AuxId]
  1217  				}
  1218  				if res, ok = zms[args[0].AuxId].AnyLT(zms[args[1].AuxId]); !ok {
  1219  					zms[expr.AuxId].Reset()
  1220  				} else {
  1221  					zms[expr.AuxId] = index.SetBool(zms[expr.AuxId], res)
  1222  				}
  1223  
  1224  			case ">=":
  1225  				if f() {
  1226  					return zms[expr.AuxId]
  1227  				}
  1228  				if res, ok = zms[args[0].AuxId].AnyGE(zms[args[1].AuxId]); !ok {
  1229  					zms[expr.AuxId].Reset()
  1230  				} else {
  1231  					zms[expr.AuxId] = index.SetBool(zms[expr.AuxId], res)
  1232  				}
  1233  
  1234  			case "<=":
  1235  				if f() {
  1236  					return zms[expr.AuxId]
  1237  				}
  1238  				if res, ok = zms[args[0].AuxId].AnyLE(zms[args[1].AuxId]); !ok {
  1239  					zms[expr.AuxId].Reset()
  1240  				} else {
  1241  					zms[expr.AuxId] = index.SetBool(zms[expr.AuxId], res)
  1242  				}
  1243  
  1244  			case "=":
  1245  				if f() {
  1246  					return zms[expr.AuxId]
  1247  				}
  1248  				if res, ok = zms[args[0].AuxId].Intersect(zms[args[1].AuxId]); !ok {
  1249  					zms[expr.AuxId].Reset()
  1250  				} else {
  1251  					zms[expr.AuxId] = index.SetBool(zms[expr.AuxId], res)
  1252  				}
  1253  
  1254  			case "between":
  1255  				if f() {
  1256  					return zms[expr.AuxId]
  1257  				}
  1258  				if res, ok = zms[args[0].AuxId].AnyBetween(zms[args[1].AuxId], zms[args[2].AuxId]); !ok {
  1259  					zms[expr.AuxId].Reset()
  1260  				} else {
  1261  					zms[expr.AuxId] = index.SetBool(zms[expr.AuxId], res)
  1262  				}
  1263  
  1264  			case "and":
  1265  				if f() {
  1266  					return zms[expr.AuxId]
  1267  				}
  1268  				if res, ok = zms[args[0].AuxId].And(zms[args[1].AuxId]); !ok {
  1269  					zms[expr.AuxId].Reset()
  1270  				} else {
  1271  					zms[expr.AuxId] = index.SetBool(zms[expr.AuxId], res)
  1272  				}
  1273  
  1274  			case "or":
  1275  				if f() {
  1276  					return zms[expr.AuxId]
  1277  				}
  1278  				if res, ok = zms[args[0].AuxId].Or(zms[args[1].AuxId]); !ok {
  1279  					zms[expr.AuxId].Reset()
  1280  				} else {
  1281  					zms[expr.AuxId] = index.SetBool(zms[expr.AuxId], res)
  1282  				}
  1283  
  1284  			case "+":
  1285  				if f() {
  1286  					return zms[expr.AuxId]
  1287  				}
  1288  				zms[expr.AuxId] = index.ZMPlus(zms[args[0].AuxId], zms[args[1].AuxId], zms[expr.AuxId])
  1289  
  1290  			case "-":
  1291  				if f() {
  1292  					return zms[expr.AuxId]
  1293  				}
  1294  				zms[expr.AuxId] = index.ZMMinus(zms[args[0].AuxId], zms[args[1].AuxId], zms[expr.AuxId])
  1295  
  1296  			case "*":
  1297  				if f() {
  1298  					return zms[expr.AuxId]
  1299  				}
  1300  				zms[expr.AuxId] = index.ZMMulti(zms[args[0].AuxId], zms[args[1].AuxId], zms[expr.AuxId])
  1301  
  1302  			default:
  1303  				ivecs := make([]*vector.Vector, len(args))
  1304  				if isAllConst(args) { // constant fold
  1305  					for i, arg := range args {
  1306  						if vecs[arg.AuxId] != nil {
  1307  							vecs[arg.AuxId].Free(proc.Mp())
  1308  						}
  1309  						if vecs[arg.AuxId], err = EvalExpressionOnce(proc, arg, []*batch.Batch{batch.EmptyForConstFoldBatch}); err != nil {
  1310  							zms[expr.AuxId].Reset()
  1311  							return zms[expr.AuxId]
  1312  						}
  1313  						ivecs[i] = vecs[arg.AuxId]
  1314  					}
  1315  				} else {
  1316  					if f() {
  1317  						return zms[expr.AuxId]
  1318  					}
  1319  					for i, arg := range args {
  1320  						if vecs[arg.AuxId] != nil {
  1321  							vecs[arg.AuxId].Free(proc.Mp())
  1322  						}
  1323  						if vecs[arg.AuxId], err = index.ZMToVector(zms[arg.AuxId], vecs[arg.AuxId], proc.Mp()); err != nil {
  1324  							zms[expr.AuxId].Reset()
  1325  							return zms[expr.AuxId]
  1326  						}
  1327  						ivecs[i] = vecs[arg.AuxId]
  1328  					}
  1329  				}
  1330  				fn, fnFree := overload.GetExecuteMethod()
  1331  				typ := types.New(types.T(expr.Typ.Id), expr.Typ.Width, expr.Typ.Scale)
  1332  
  1333  				result := vector.NewFunctionResultWrapper(proc.GetVector, proc.PutVector, typ, proc.Mp())
  1334  				if err = result.PreExtendAndReset(2); err != nil {
  1335  					zms[expr.AuxId].Reset()
  1336  					result.Free()
  1337  					if fnFree != nil {
  1338  						// NOTE: fnFree is only applicable for serial and serial_full.
  1339  						// if fnFree is not nil, then make sure to call it after fn() is done.
  1340  						_ = fnFree()
  1341  					}
  1342  					return zms[expr.AuxId]
  1343  				}
  1344  				if err = fn(ivecs, result, proc, 2); err != nil {
  1345  					zms[expr.AuxId].Reset()
  1346  					result.Free()
  1347  					if fnFree != nil {
  1348  						// NOTE: fnFree is only applicable for serial and serial_full.
  1349  						// if fnFree is not nil, then make sure to call it after fn() is done.
  1350  						_ = fnFree()
  1351  					}
  1352  					return zms[expr.AuxId]
  1353  				}
  1354  				if fnFree != nil {
  1355  					// NOTE: fnFree is only applicable for serial and serial_full.
  1356  					// if fnFree is not nil, then make sure to call it after fn() is done.
  1357  					_ = fnFree()
  1358  				}
  1359  				zms[expr.AuxId] = index.VectorToZM(result.GetResultVector(), zms[expr.AuxId])
  1360  				result.Free()
  1361  			}
  1362  		}
  1363  
  1364  	default:
  1365  		zms[expr.AuxId].Reset()
  1366  	}
  1367  
  1368  	return zms[expr.AuxId]
  1369  }
  1370  
  1371  // RewriteFilterExprList will convert an expression list to be an AndExpr
  1372  func RewriteFilterExprList(list []*plan.Expr) *plan.Expr {
  1373  	l := len(list)
  1374  	if l == 0 {
  1375  		return nil
  1376  	} else if l == 1 {
  1377  		return list[0]
  1378  	} else {
  1379  		left := list[0]
  1380  		right := RewriteFilterExprList(list[1:])
  1381  		return &plan.Expr{
  1382  			Typ:  left.Typ,
  1383  			Expr: makeAndExpr(left, right),
  1384  		}
  1385  	}
  1386  }
  1387  
  1388  func SplitAndExprs(list []*plan.Expr) []*plan.Expr {
  1389  	exprs := make([]*plan.Expr, 0, len(list))
  1390  	for i := range list {
  1391  		exprs = append(exprs, splitAndExpr(list[i])...)
  1392  	}
  1393  	return exprs
  1394  }
  1395  
  1396  func splitAndExpr(expr *plan.Expr) []*plan.Expr {
  1397  	if expr == nil {
  1398  		return nil
  1399  	}
  1400  	exprs := make([]*plan.Expr, 0, 1)
  1401  	if e, ok := expr.Expr.(*plan.Expr_F); ok {
  1402  		fid, _ := function.DecodeOverloadID(e.F.Func.GetObj())
  1403  		if fid == function.AND {
  1404  			exprs = append(exprs, splitAndExpr(e.F.Args[0])...)
  1405  			exprs = append(exprs, splitAndExpr(e.F.Args[1])...)
  1406  			return exprs
  1407  		}
  1408  	}
  1409  	exprs = append(exprs, expr)
  1410  	return exprs
  1411  }
  1412  
  1413  func makeAndExpr(left, right *plan.Expr) *plan.Expr_F {
  1414  	return &plan.Expr_F{
  1415  		F: &plan.Function{
  1416  			Func: &plan.ObjectRef{
  1417  				Obj:     function.AndFunctionEncodedID,
  1418  				ObjName: function.AndFunctionName,
  1419  			},
  1420  			Args: []*plan.Expr{left, right},
  1421  		},
  1422  	}
  1423  }
  1424  
  1425  func isAllConst(exprs []*plan.Expr) bool {
  1426  	for _, expr := range exprs {
  1427  		if !isConst(expr) {
  1428  			return false
  1429  		}
  1430  	}
  1431  	return true
  1432  }
  1433  
  1434  func isConst(expr *plan.Expr) bool {
  1435  	switch t := expr.Expr.(type) {
  1436  	case *plan.Expr_Col:
  1437  		return false
  1438  	case *plan.Expr_F:
  1439  		return isAllConst(t.F.Args)
  1440  	default:
  1441  		return true
  1442  	}
  1443  }