github.com/matrixorigin/matrixone@v1.2.0/pkg/vm/engine/tae/index/zm.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 index
    16  
    17  import (
    18  	"bytes"
    19  	"encoding/hex"
    20  	"fmt"
    21  	"math"
    22  	"sort"
    23  	"strconv"
    24  	"strings"
    25  
    26  	"github.com/matrixorigin/matrixone/pkg/common/moerr"
    27  	"github.com/matrixorigin/matrixone/pkg/common/mpool"
    28  	"github.com/matrixorigin/matrixone/pkg/container/types"
    29  	"github.com/matrixorigin/matrixone/pkg/container/vector"
    30  	"github.com/matrixorigin/matrixone/pkg/vectorize/moarray"
    31  	"github.com/matrixorigin/matrixone/pkg/vm/engine/tae/compute"
    32  	"github.com/matrixorigin/matrixone/pkg/vm/engine/tae/containers"
    33  )
    34  
    35  const (
    36  	ZMSize = 64
    37  )
    38  
    39  var MaxBytesValue []byte
    40  var zeroZM = make([]byte, ZMSize)
    41  
    42  func init() {
    43  	MaxBytesValue = bytes.Repeat([]byte{0xff}, 31)
    44  }
    45  
    46  // [0,...29, 30, 31,...60, 61, 62, 63]
    47  //
    48  //	-------  --  --------  --  --  --
    49  //	  min     |     max    |    |   |
    50  //	       len(min)    len(max) |   |
    51  //	                       reserved |
    52  //	                              type
    53  
    54  type ZM []byte
    55  
    56  func NewZM(t types.T, scale int32) ZM {
    57  	zm := ZM(make([]byte, ZMSize))
    58  	zm.SetType(t)
    59  	zm.SetScale(scale)
    60  	return zm
    61  }
    62  
    63  func BuildZM(t types.T, v []byte) ZM {
    64  	zm := ZM(make([]byte, ZMSize))
    65  	zm.SetType(t)
    66  	zm.doInit(v)
    67  	return zm
    68  }
    69  
    70  func (zm ZM) ResetMinMax() {
    71  	t := zm.GetType()
    72  	scale := zm.GetScale()
    73  	copy(zm[:], zeroZM)
    74  	zm.SetType(t)
    75  	zm.SetScale(scale)
    76  }
    77  
    78  func (zm ZM) doInit(v []byte) {
    79  	if zm.IsString() {
    80  		zm.updateMinString(v)
    81  		zm.updateMaxString(v)
    82  	} else {
    83  		zm.updateMinFixed(v)
    84  		zm.updateMaxFixed(v)
    85  	}
    86  	zm.setInited()
    87  }
    88  
    89  func (zm ZM) innerString(f func([]byte) string) string {
    90  	var b strings.Builder
    91  	if zm.IsString() {
    92  		smin, smax := f(zm.GetMinBuf()), f(zm.GetMaxBuf())
    93  		_, _ = b.WriteString(fmt.Sprintf("ZM(%s)%d[%v,%v]",
    94  			zm.GetType().String(), zm.GetScale(), smin, smax))
    95  	} else {
    96  		_, _ = b.WriteString(fmt.Sprintf("ZM(%s)%d[%v,%v]",
    97  			zm.GetType().String(), zm.GetScale(), zm.GetMin(), zm.GetMax()))
    98  		if zm.supportSum() {
    99  			_, _ = b.WriteString(fmt.Sprintf("SUM:%v", zm.GetSum()))
   100  		}
   101  	}
   102  	if zm.MaxTruncated() {
   103  		_ = b.WriteByte('+')
   104  	}
   105  	if !zm.IsInited() {
   106  		_, _ = b.WriteString("--")
   107  	}
   108  	return b.String()
   109  }
   110  
   111  func (zm ZM) StringForCompose() string {
   112  	return zm.innerString(func(b []byte) string {
   113  		if len(b) >= 30 {
   114  			return hex.EncodeToString(b)
   115  		}
   116  		if r, _, _, e := types.DecodeTuple(b); e == nil {
   117  			return r.ErrString(nil)
   118  		}
   119  		return string(b)
   120  	})
   121  }
   122  
   123  func (zm ZM) String() string {
   124  	return zm.innerString(func(b []byte) string {
   125  		for _, c := range b {
   126  			if !strconv.IsPrint(rune(c)) {
   127  				return hex.EncodeToString(b)
   128  			}
   129  		}
   130  		return string(b)
   131  	})
   132  }
   133  
   134  func (zm ZM) StringForHex() string {
   135  	return zm.innerString(func(b []byte) string {
   136  		return hex.EncodeToString(b)
   137  	})
   138  }
   139  
   140  func (zm ZM) supportSum() bool {
   141  	t := zm.GetType()
   142  	return t.IsInteger() || t.IsFloat() || t == types.T_decimal64 || t == types.T_bit
   143  }
   144  
   145  func (zm ZM) Clone() ZM {
   146  	cloned := make([]byte, ZMSize)
   147  	copy(cloned[:], zm[:])
   148  	return cloned
   149  }
   150  
   151  func (zm ZM) GetType() types.T {
   152  	return types.T(zm[63])
   153  }
   154  
   155  func (zm ZM) IsString() bool {
   156  	return zm.GetType().FixedLength() < 0
   157  }
   158  
   159  func (zm ZM) IsArray() bool {
   160  	return zm.GetType().IsArrayRelate()
   161  }
   162  
   163  func (zm ZM) Valid() bool {
   164  	return len(zm) == ZMSize && zm.IsInited()
   165  }
   166  
   167  func (zm ZM) SetType(t types.T) {
   168  	zm[63] &= 0x00
   169  	zm[63] |= byte(t)
   170  	sz := t.FixedLength()
   171  	if sz <= 0 {
   172  		return
   173  	}
   174  	zm[61] = byte(sz)
   175  	zm[30] = byte(sz)
   176  }
   177  
   178  func (zm ZM) SetScale(scale int32) {
   179  	sz := 0x3f & byte(scale)
   180  	zm[62] &= 0xc0
   181  	zm[62] |= sz
   182  }
   183  
   184  func (zm ZM) GetScale() int32 {
   185  	sz := 0x3f & zm[62]
   186  	return int32(sz)
   187  }
   188  
   189  func (zm ZM) GetMin() any {
   190  	if !zm.IsInited() {
   191  		return nil
   192  	}
   193  	buf := zm.GetMinBuf()
   194  	return zm.getValue(buf)
   195  }
   196  
   197  func (zm ZM) GetMax() any {
   198  	if !zm.IsInited() {
   199  		return nil
   200  	}
   201  	buf := zm.GetMaxBuf()
   202  	return zm.getValue(buf)
   203  }
   204  
   205  func (zm ZM) SetSum(v []byte) {
   206  	copy(zm.GetSumBuf(), v)
   207  }
   208  
   209  func (zm ZM) HasSum() bool {
   210  	return zm.GetSum() != 0
   211  }
   212  
   213  func (zm ZM) GetSum() any {
   214  	if !zm.IsInited() {
   215  		return nil
   216  	}
   217  	return zm.decodeSum()
   218  }
   219  
   220  func (zm ZM) decodeSum() any {
   221  	switch types.T(zm[63]) {
   222  	case types.T_int8, types.T_int16, types.T_int32, types.T_int64:
   223  		return types.DecodeInt64(zm.GetSumBuf())
   224  	case types.T_uint8, types.T_uint16, types.T_uint32, types.T_uint64:
   225  		return types.DecodeUint64(zm.GetSumBuf())
   226  	case types.T_float32, types.T_float64:
   227  		return types.DecodeFloat64(zm.GetSumBuf())
   228  	case types.T_decimal64:
   229  		return types.DecodeDecimal64(zm.GetSumBuf())
   230  	case types.T_bit:
   231  		return types.DecodeUint64(zm.GetSumBuf())
   232  	}
   233  	return nil
   234  }
   235  
   236  func (zm ZM) GetMinBuf() []byte {
   237  	return zm[0 : zm[30]&0x1f]
   238  }
   239  
   240  func (zm ZM) GetMaxBuf() []byte {
   241  	return zm[31 : 31+zm[61]&0x1f]
   242  }
   243  
   244  func (zm ZM) GetSumBuf() []byte {
   245  	if zm.supportSum() {
   246  		return zm[8:16]
   247  	}
   248  	return nil
   249  }
   250  
   251  func (zm ZM) GetBuf() []byte {
   252  	return zm
   253  }
   254  
   255  func (zm ZM) MaxTruncated() bool {
   256  	return zm[61]&0x80 != 0
   257  }
   258  func (zm ZM) SetMaxTruncated() {
   259  	zm[61] |= 0x80
   260  }
   261  func (zm ZM) Encode() []byte {
   262  	return zm[:]
   263  }
   264  
   265  func (zm ZM) Marshal() ([]byte, error) {
   266  	buf := make([]byte, ZMSize)
   267  	copy(buf, zm[:])
   268  	return buf, nil
   269  }
   270  
   271  func (zm ZM) Unmarshal(buf []byte) (err error) {
   272  	copy(zm[:], buf[:ZMSize])
   273  	return
   274  }
   275  
   276  // TODO: remove me later
   277  func (zm ZM) Update(v any) (err error) {
   278  	UpdateZMAny(zm, v)
   279  	return
   280  }
   281  
   282  func (zm ZM) FastContainsAny(keys *vector.Vector) (ok bool) {
   283  	if !zm.IsInited() {
   284  		return false
   285  	}
   286  	var op containers.ItOpT[[]byte]
   287  	if zm.IsString() {
   288  		op = func(key []byte, isNull bool, _ int) (err error) {
   289  			if isNull || zm.containsString(key) {
   290  				err = moerr.GetOkExpectedEOB()
   291  				ok = true
   292  			}
   293  			return
   294  		}
   295  		containers.ForeachWindowBytes(keys, 0, keys.Length(), op, nil)
   296  	} else {
   297  		op = func(key []byte, isNull bool, _ int) (err error) {
   298  			if isNull || zm.containsBytes(key) {
   299  				err = moerr.GetOkExpectedEOB()
   300  				ok = true
   301  			}
   302  			return
   303  		}
   304  		containers.ForeachWindowBytes(keys, 0, keys.Length(), op, nil)
   305  	}
   306  	return
   307  }
   308  
   309  // Optimize me later
   310  func (zm ZM) containsBytes(k []byte) bool {
   311  	t := types.T(zm[63])
   312  	return compute.Compare(k, zm.GetMinBuf(), t, 0, 0) >= 0 &&
   313  		compute.Compare(k, zm.GetMaxBuf(), t, 0, 0) <= 0
   314  }
   315  
   316  func (zm ZM) containsString(k []byte) bool {
   317  	if zm.MaxTruncated() {
   318  		return true
   319  	}
   320  	return compute.CompareBytes(k, zm.GetMinBuf()) >= 0 &&
   321  		compute.CompareBytes(k, zm.GetMaxBuf()) <= 0
   322  }
   323  
   324  // TODO: remove me later
   325  func (zm ZM) Contains(k any) bool {
   326  	if !zm.IsInited() {
   327  		return false
   328  	}
   329  	if zm.IsString() {
   330  		return zm.containsString(k.([]byte))
   331  	}
   332  
   333  	t := types.T(zm[63])
   334  	v := types.EncodeValue(k, t)
   335  	return zm.containsBytes(v)
   336  }
   337  
   338  func (zm ZM) ContainsKey(k []byte) bool {
   339  	if !zm.IsInited() {
   340  		return false
   341  	}
   342  	if zm.IsString() {
   343  		return zm.containsString(k)
   344  	}
   345  	t := types.T(zm[63])
   346  	return compute.Compare(k, zm.GetMinBuf(), t, 0, 0) >= 0 &&
   347  		compute.Compare(k, zm.GetMaxBuf(), t, 0, 0) <= 0
   348  }
   349  
   350  // zm.min < k
   351  func (zm ZM) AnyLTByValue(k []byte) bool {
   352  	if !zm.IsInited() {
   353  		return false
   354  	}
   355  	if !zm.IsString() || len(k) < 31 {
   356  		return compute.Compare(zm.GetMinBuf(), k, zm.GetType(), 0, 0) < 0
   357  	}
   358  	zm2 := BuildZM(zm.GetType(), k)
   359  	ret, _ := zm.AnyLT(zm2)
   360  	return ret
   361  }
   362  
   363  // zm.max > k
   364  func (zm ZM) AnyGTByValue(k []byte) bool {
   365  	if !zm.IsInited() {
   366  		return false
   367  	}
   368  	if !zm.IsString() || len(k) < 31 {
   369  		return compute.Compare(zm.GetMaxBuf(), k, zm.GetType(), 0, 0) > 0
   370  	}
   371  	zm2 := BuildZM(zm.GetType(), k)
   372  	ret, _ := zm.AnyGT(zm2)
   373  	return ret
   374  }
   375  
   376  func (zm ZM) IsInited() bool {
   377  	return len(zm) == ZMSize && zm[62]&0x80 != 0
   378  }
   379  
   380  func (zm ZM) Reset() {
   381  	if len(zm) == ZMSize {
   382  		zm[62] &= 0x7f
   383  	}
   384  }
   385  
   386  func (zm ZM) setInited() {
   387  	zm[62] |= 0x80
   388  }
   389  
   390  func (zm ZM) getValue(buf []byte) any {
   391  	switch types.T(zm[63]) {
   392  	case types.T_bool:
   393  		return types.DecodeFixed[bool](buf)
   394  	case types.T_bit:
   395  		return types.DecodeFixed[uint64](buf)
   396  	case types.T_int8:
   397  		return types.DecodeFixed[int8](buf)
   398  	case types.T_int16:
   399  		return types.DecodeFixed[int16](buf)
   400  	case types.T_int32:
   401  		return types.DecodeFixed[int32](buf)
   402  	case types.T_int64:
   403  		return types.DecodeFixed[int64](buf)
   404  	case types.T_uint8:
   405  		return types.DecodeFixed[uint8](buf)
   406  	case types.T_uint16:
   407  		return types.DecodeFixed[uint16](buf)
   408  	case types.T_uint32:
   409  		return types.DecodeFixed[uint32](buf)
   410  	case types.T_uint64:
   411  		return types.DecodeFixed[uint64](buf)
   412  	case types.T_float32:
   413  		return types.DecodeFixed[float32](buf)
   414  	case types.T_float64:
   415  		return types.DecodeFixed[float64](buf)
   416  	case types.T_date:
   417  		return types.DecodeFixed[types.Date](buf)
   418  	case types.T_time:
   419  		return types.DecodeFixed[types.Time](buf)
   420  	case types.T_datetime:
   421  		return types.DecodeFixed[types.Datetime](buf)
   422  	case types.T_timestamp:
   423  		return types.DecodeFixed[types.Timestamp](buf)
   424  	case types.T_enum:
   425  		return types.DecodeFixed[types.Enum](buf)
   426  	case types.T_decimal64:
   427  		return types.DecodeFixed[types.Decimal64](buf)
   428  	case types.T_decimal128:
   429  		return types.DecodeFixed[types.Decimal128](buf)
   430  	case types.T_uuid:
   431  		return types.DecodeFixed[types.Uuid](buf)
   432  	case types.T_TS:
   433  		return types.DecodeFixed[types.TS](buf)
   434  	case types.T_Rowid:
   435  		return types.DecodeFixed[types.Rowid](buf)
   436  	case types.T_Blockid:
   437  		return types.DecodeFixed[types.Rowid](buf)
   438  	case types.T_char, types.T_varchar, types.T_json,
   439  		types.T_binary, types.T_varbinary, types.T_blob, types.T_text:
   440  		return buf
   441  	case types.T_array_float32:
   442  		// Used by MO_TABLE_COL_MAX and ZoneMap.String()
   443  		return types.BytesToArray[float32](buf)
   444  	case types.T_array_float64:
   445  		return types.BytesToArray[float64](buf)
   446  	}
   447  	panic(fmt.Sprintf("unsupported type: %v", zm.GetType()))
   448  }
   449  
   450  func (zm ZM) updateMinString(v []byte) {
   451  	size := len(v)
   452  	if size > 30 {
   453  		size = 30
   454  	}
   455  	copy(zm[:], v[:size])
   456  	zm[30] = byte(size)
   457  }
   458  
   459  func (zm ZM) updateMaxString(v []byte) {
   460  	size := len(v)
   461  	var flag byte
   462  	if size > 30 {
   463  		size = 30
   464  		copy(zm[31:], v[:size])
   465  		if hasMaxPrefix(v) {
   466  			flag |= 0x80
   467  		} else {
   468  			adjustBytes(zm[31:61])
   469  		}
   470  	} else {
   471  		copy(zm[31:], v[:size])
   472  	}
   473  	flag |= byte(size)
   474  	zm[61] = flag
   475  }
   476  
   477  func (zm ZM) updateMinFixed(v []byte) {
   478  	copy(zm[:], v)
   479  	zm[30] = byte(len(v))
   480  }
   481  
   482  func (zm ZM) updateMaxFixed(v []byte) {
   483  	copy(zm[31:], v)
   484  	zm[61] = byte(len(v))
   485  }
   486  
   487  func (zm ZM) compareCheck(o ZM) (ok bool) {
   488  	if !zm.IsInited() || !o.IsInited() {
   489  		return false
   490  	}
   491  	return zm.GetType() == o.GetType() || (zm.IsString() && o.IsString())
   492  }
   493  
   494  // caller need to do compareCheck
   495  func (zm ZM) CompareMax(o ZM) int {
   496  	return compute.Compare(zm.GetMaxBuf(), o.GetMaxBuf(), zm.GetType(), zm.GetScale(), o.GetScale())
   497  }
   498  
   499  func (zm ZM) CompareMin(o ZM) int {
   500  	return compute.Compare(zm.GetMinBuf(), o.GetMinBuf(), zm.GetType(), zm.GetScale(), o.GetScale())
   501  }
   502  
   503  func (zm ZM) AnyGT(o ZM) (res bool, ok bool) {
   504  	if !zm.compareCheck(o) {
   505  		ok = false
   506  		return
   507  	}
   508  	// zm.max > o.min
   509  	ok = true
   510  	res = compute.Compare(zm.GetMaxBuf(), o.GetMinBuf(), zm.GetType(), zm.GetScale(), o.GetScale()) > 0
   511  	return
   512  }
   513  
   514  func (zm ZM) AnyGE(o ZM) (res bool, ok bool) {
   515  	if !zm.compareCheck(o) {
   516  		ok = false
   517  		return
   518  	}
   519  	// zm.max >= o.min
   520  	ok = true
   521  	res = compute.Compare(zm.GetMaxBuf(), o.GetMinBuf(), zm.GetType(), zm.GetScale(), o.GetScale()) >= 0
   522  	return
   523  }
   524  
   525  // zm.min >= k
   526  func (zm ZM) AnyGEByValue(k []byte) bool {
   527  	if !zm.IsInited() {
   528  		return false
   529  	}
   530  	if !zm.IsString() || len(k) < 31 {
   531  		return compute.Compare(zm.GetMaxBuf(), k, zm.GetType(), 0, 0) >= 0
   532  	}
   533  	zm2 := BuildZM(zm.GetType(), k)
   534  	ret, _ := zm.AnyGE(zm2)
   535  	return ret
   536  }
   537  
   538  // zm.min <= k
   539  func (zm ZM) AnyLEByValue(k []byte) bool {
   540  	if !zm.IsInited() {
   541  		return false
   542  	}
   543  	if !zm.IsString() || len(k) < 31 {
   544  		return compute.Compare(zm.GetMinBuf(), k, zm.GetType(), 0, 0) <= 0
   545  	}
   546  	zm2 := BuildZM(zm.GetType(), k)
   547  	ret, _ := zm.AnyLE(zm2)
   548  	return ret
   549  }
   550  
   551  func (zm ZM) AnyLT(o ZM) (res bool, ok bool) {
   552  	if !zm.compareCheck(o) {
   553  		ok = false
   554  		return
   555  	}
   556  	// zm.min < o.max
   557  	ok = true
   558  	res = compute.Compare(zm.GetMinBuf(), o.GetMaxBuf(), zm.GetType(), zm.GetScale(), o.GetScale()) < 0
   559  	return
   560  }
   561  
   562  func (zm ZM) AnyLE(o ZM) (res bool, ok bool) {
   563  	if !zm.compareCheck(o) {
   564  		ok = false
   565  		return
   566  	}
   567  	// zm.min <= o.max
   568  	ok = true
   569  	res = compute.Compare(zm.GetMinBuf(), o.GetMaxBuf(), zm.GetType(), zm.GetScale(), o.GetScale()) <= 0
   570  	return
   571  }
   572  
   573  func (zm ZM) AnyBetween(lb, ub ZM) (res bool, ok bool) {
   574  	if !zm.compareCheck(lb) || !zm.compareCheck(ub) {
   575  		ok = false
   576  		return
   577  	}
   578  	// zm.max >= lb.min && zm.min <= ub.max
   579  	ok = true
   580  	res = compute.Compare(zm.GetMaxBuf(), lb.GetMinBuf(), zm.GetType(), zm.GetScale(), lb.GetScale()) >= 0 &&
   581  		compute.Compare(zm.GetMinBuf(), ub.GetMaxBuf(), zm.GetType(), zm.GetScale(), ub.GetScale()) <= 0
   582  	return
   583  }
   584  
   585  func (zm ZM) FastIntersect(o ZM) (res bool) {
   586  	t := zm.GetType()
   587  	// zm.max >= o.min && zm.min <= v2.max
   588  	res = compute.Compare(zm.GetMaxBuf(), o.GetMinBuf(), t, zm.GetScale(), o.GetScale()) >= 0 &&
   589  		compute.Compare(zm.GetMinBuf(), o.GetMaxBuf(), t, zm.GetScale(), o.GetScale()) <= 0
   590  	return
   591  }
   592  
   593  func (zm ZM) Intersect(o ZM) (res bool, ok bool) {
   594  	if !zm.compareCheck(o) {
   595  		ok = false
   596  		return
   597  	}
   598  	ok = true
   599  	res = zm.FastIntersect(o)
   600  	return
   601  }
   602  
   603  // both zm should be of type bool, otherwise, ok is false
   604  // res is true only when zm.min == true and o.min == true
   605  func (zm ZM) And(o ZM) (res bool, ok bool) {
   606  	if !zm.compareCheck(o) {
   607  		ok = false
   608  		return
   609  	}
   610  	t := zm.GetType()
   611  	if t != types.T_bool {
   612  		ok = false
   613  		return
   614  	}
   615  	ok = true
   616  	if !types.DecodeBool(zm.GetMinBuf()) {
   617  		return
   618  	}
   619  	res = types.DecodeBool(o.GetMinBuf())
   620  	return
   621  }
   622  
   623  // both zm should be of type bool, otherwise, ok is false
   624  // res is false only when zm.max == false and o.max == false
   625  func (zm ZM) Or(o ZM) (res bool, ok bool) {
   626  	if !zm.compareCheck(o) {
   627  		ok = false
   628  		return
   629  	}
   630  	t := zm.GetType()
   631  	if t != types.T_bool {
   632  		ok = false
   633  		return
   634  	}
   635  	res, ok = true, true
   636  	if !types.DecodeBool(zm.GetMaxBuf()) && !types.DecodeBool(o.GetMaxBuf()) {
   637  		res = false
   638  	}
   639  	return
   640  }
   641  
   642  func (zm ZM) PrefixEq(s []byte) bool {
   643  	zmin := zm.GetMinBuf()
   644  	zmax := zm.GetMaxBuf()
   645  
   646  	return types.PrefixCompare(zmin, s) <= 0 && types.PrefixCompare(zmax, s) >= 0
   647  }
   648  
   649  func (zm ZM) PrefixBetween(lb, ub []byte) bool {
   650  	zmin := zm.GetMinBuf()
   651  	zmax := zm.GetMaxBuf()
   652  
   653  	return types.PrefixCompare(zmin, ub) <= 0 && types.PrefixCompare(zmax, lb) >= 0
   654  }
   655  
   656  func (zm ZM) Between(lb, ub []byte) bool {
   657  	oth := BuildZM(zm.GetType(), lb)
   658  	if zm.IsString() {
   659  		oth.updateMinString(lb)
   660  		oth.updateMaxString(ub)
   661  	} else {
   662  		oth.updateMinFixed(lb)
   663  		oth.updateMaxFixed(ub)
   664  	}
   665  
   666  	ok1, ok2 := zm.Intersect(oth)
   667  	return ok1 && ok2
   668  }
   669  
   670  func (zm ZM) PrefixIn(vec *vector.Vector) bool {
   671  	col, area := vector.MustVarlenaRawData(vec)
   672  	minVal, maxVal := zm.GetMinBuf(), zm.GetMaxBuf()
   673  	lowerBound := sort.Search(len(col), func(i int) bool {
   674  		return types.PrefixCompare(minVal, col[i].GetByteSlice(area)) <= 0
   675  	})
   676  
   677  	return lowerBound < len(col) && types.PrefixCompare(maxVal, col[lowerBound].GetByteSlice(area)) >= 0
   678  }
   679  
   680  // anyIn has been called, so there must be a subvector in this zonemap
   681  // return lower bound and upper bound
   682  func (zm ZM) SubVecIn(vec *vector.Vector) (int, int) {
   683  	if vec.Length() <= 3 {
   684  		return 0, vec.Length()
   685  	}
   686  	switch vec.GetType().Oid {
   687  	case types.T_bool:
   688  		col := vector.MustFixedCol[bool](vec)
   689  		minVal, _ := types.DecodeBool(zm.GetMinBuf()), types.DecodeBool(zm.GetMaxBuf())
   690  		lowerBound := sort.Search(len(col), func(i int) bool {
   691  			return !minVal || col[i]
   692  		})
   693  		return lowerBound, len(col)
   694  
   695  	case types.T_bit:
   696  		col := vector.MustFixedCol[uint64](vec)
   697  		minVal, maxVal := types.DecodeUint64(zm.GetMinBuf()), types.DecodeUint64(zm.GetMaxBuf())
   698  		lowerBound := sort.Search(len(col), func(i int) bool {
   699  			return minVal <= col[i]
   700  		})
   701  		upperBound := sort.Search(len(col), func(i int) bool {
   702  			return maxVal < col[i]
   703  		})
   704  		return lowerBound, upperBound
   705  
   706  	case types.T_int8:
   707  		col := vector.MustFixedCol[int8](vec)
   708  		minVal, maxVal := types.DecodeInt8(zm.GetMinBuf()), types.DecodeInt8(zm.GetMaxBuf())
   709  		lowerBound := sort.Search(len(col), func(i int) bool {
   710  			return minVal <= col[i]
   711  		})
   712  		upperBound := sort.Search(len(col), func(i int) bool {
   713  			return maxVal < col[i]
   714  		})
   715  		return lowerBound, upperBound
   716  
   717  	case types.T_int16:
   718  		col := vector.MustFixedCol[int16](vec)
   719  		minVal, maxVal := types.DecodeInt16(zm.GetMinBuf()), types.DecodeInt16(zm.GetMaxBuf())
   720  		lowerBound := sort.Search(len(col), func(i int) bool {
   721  			return minVal <= col[i]
   722  		})
   723  		upperBound := sort.Search(len(col), func(i int) bool {
   724  			return maxVal < col[i]
   725  		})
   726  		return lowerBound, upperBound
   727  
   728  	case types.T_int32:
   729  		col := vector.MustFixedCol[int32](vec)
   730  		minVal, maxVal := types.DecodeInt32(zm.GetMinBuf()), types.DecodeInt32(zm.GetMaxBuf())
   731  		lowerBound := sort.Search(len(col), func(i int) bool {
   732  			return minVal <= col[i]
   733  		})
   734  		upperBound := sort.Search(len(col), func(i int) bool {
   735  			return maxVal < col[i]
   736  		})
   737  		return lowerBound, upperBound
   738  
   739  	case types.T_int64:
   740  		col := vector.MustFixedCol[int64](vec)
   741  		minVal, maxVal := types.DecodeInt64(zm.GetMinBuf()), types.DecodeInt64(zm.GetMaxBuf())
   742  		lowerBound := sort.Search(len(col), func(i int) bool {
   743  			return minVal <= col[i]
   744  		})
   745  		upperBound := sort.Search(len(col), func(i int) bool {
   746  			return maxVal < col[i]
   747  		})
   748  		return lowerBound, upperBound
   749  
   750  	case types.T_uint8:
   751  		col := vector.MustFixedCol[uint8](vec)
   752  		minVal, maxVal := types.DecodeUint8(zm.GetMinBuf()), types.DecodeUint8(zm.GetMaxBuf())
   753  		lowerBound := sort.Search(len(col), func(i int) bool {
   754  			return minVal <= col[i]
   755  		})
   756  		upperBound := sort.Search(len(col), func(i int) bool {
   757  			return maxVal < col[i]
   758  		})
   759  		return lowerBound, upperBound
   760  
   761  	case types.T_uint16:
   762  		col := vector.MustFixedCol[uint16](vec)
   763  		minVal, maxVal := types.DecodeUint16(zm.GetMinBuf()), types.DecodeUint16(zm.GetMaxBuf())
   764  		lowerBound := sort.Search(len(col), func(i int) bool {
   765  			return minVal <= col[i]
   766  		})
   767  		upperBound := sort.Search(len(col), func(i int) bool {
   768  			return maxVal < col[i]
   769  		})
   770  		return lowerBound, upperBound
   771  
   772  	case types.T_uint32:
   773  		col := vector.MustFixedCol[uint32](vec)
   774  		minVal, maxVal := types.DecodeUint32(zm.GetMinBuf()), types.DecodeUint32(zm.GetMaxBuf())
   775  		lowerBound := sort.Search(len(col), func(i int) bool {
   776  			return minVal <= col[i]
   777  		})
   778  		upperBound := sort.Search(len(col), func(i int) bool {
   779  			return maxVal < col[i]
   780  		})
   781  		return lowerBound, upperBound
   782  
   783  	case types.T_uint64:
   784  		col := vector.MustFixedCol[uint64](vec)
   785  		minVal, maxVal := types.DecodeUint64(zm.GetMinBuf()), types.DecodeUint64(zm.GetMaxBuf())
   786  		lowerBound := sort.Search(len(col), func(i int) bool {
   787  			return minVal <= col[i]
   788  		})
   789  		upperBound := sort.Search(len(col), func(i int) bool {
   790  			return maxVal < col[i]
   791  		})
   792  		return lowerBound, upperBound
   793  
   794  	case types.T_float32:
   795  		col := vector.MustFixedCol[float32](vec)
   796  		minVal, maxVal := types.DecodeFloat32(zm.GetMinBuf()), types.DecodeFloat32(zm.GetMaxBuf())
   797  		lowerBound := sort.Search(len(col), func(i int) bool {
   798  			return minVal <= col[i]
   799  		})
   800  		upperBound := sort.Search(len(col), func(i int) bool {
   801  			return maxVal < col[i]
   802  		})
   803  		return lowerBound, upperBound
   804  
   805  	case types.T_float64:
   806  		col := vector.MustFixedCol[float64](vec)
   807  		minVal, maxVal := types.DecodeFloat64(zm.GetMinBuf()), types.DecodeFloat64(zm.GetMaxBuf())
   808  		lowerBound := sort.Search(len(col), func(i int) bool {
   809  			return minVal <= col[i]
   810  		})
   811  		upperBound := sort.Search(len(col), func(i int) bool {
   812  			return maxVal < col[i]
   813  		})
   814  		return lowerBound, upperBound
   815  
   816  	case types.T_date:
   817  		col := vector.MustFixedCol[types.Date](vec)
   818  		minVal, maxVal := types.DecodeDate(zm.GetMinBuf()), types.DecodeDate(zm.GetMaxBuf())
   819  		lowerBound := sort.Search(len(col), func(i int) bool {
   820  			return minVal <= col[i]
   821  		})
   822  		upperBound := sort.Search(len(col), func(i int) bool {
   823  			return maxVal < col[i]
   824  		})
   825  		return lowerBound, upperBound
   826  
   827  	case types.T_datetime:
   828  		col := vector.MustFixedCol[types.Datetime](vec)
   829  		minVal, maxVal := types.DecodeDatetime(zm.GetMinBuf()), types.DecodeDatetime(zm.GetMaxBuf())
   830  		lowerBound := sort.Search(len(col), func(i int) bool {
   831  			return minVal <= col[i]
   832  		})
   833  		upperBound := sort.Search(len(col), func(i int) bool {
   834  			return maxVal < col[i]
   835  		})
   836  		return lowerBound, upperBound
   837  
   838  	case types.T_time:
   839  		col := vector.MustFixedCol[types.Time](vec)
   840  		minVal, maxVal := types.DecodeTime(zm.GetMinBuf()), types.DecodeTime(zm.GetMaxBuf())
   841  		lowerBound := sort.Search(len(col), func(i int) bool {
   842  			return minVal <= col[i]
   843  		})
   844  		upperBound := sort.Search(len(col), func(i int) bool {
   845  			return maxVal < col[i]
   846  		})
   847  		return lowerBound, upperBound
   848  
   849  	case types.T_timestamp:
   850  		col := vector.MustFixedCol[types.Timestamp](vec)
   851  		minVal, maxVal := types.DecodeTimestamp(zm.GetMinBuf()), types.DecodeTimestamp(zm.GetMaxBuf())
   852  		lowerBound := sort.Search(len(col), func(i int) bool {
   853  			return minVal <= col[i]
   854  		})
   855  		upperBound := sort.Search(len(col), func(i int) bool {
   856  			return maxVal < col[i]
   857  		})
   858  		return lowerBound, upperBound
   859  
   860  	case types.T_enum:
   861  		col := vector.MustFixedCol[types.Enum](vec)
   862  		minVal, maxVal := types.DecodeEnum(zm.GetMinBuf()), types.DecodeEnum(zm.GetMaxBuf())
   863  		lowerBound := sort.Search(len(col), func(i int) bool {
   864  			return minVal <= col[i]
   865  		})
   866  		upperBound := sort.Search(len(col), func(i int) bool {
   867  			return maxVal < col[i]
   868  		})
   869  		return lowerBound, upperBound
   870  
   871  	case types.T_decimal64:
   872  		col := vector.MustFixedCol[types.Decimal64](vec)
   873  		minVal, maxVal := types.DecodeDecimal64(zm.GetMinBuf()), types.DecodeDecimal64(zm.GetMaxBuf())
   874  		lowerBound := sort.Search(len(col), func(i int) bool {
   875  			return !col[i].Less(minVal)
   876  		})
   877  		upperBound := sort.Search(len(col), func(i int) bool {
   878  			return maxVal.Less(col[i])
   879  		})
   880  		return lowerBound, upperBound
   881  
   882  	case types.T_decimal128:
   883  		col := vector.MustFixedCol[types.Decimal128](vec)
   884  		minVal, maxVal := types.DecodeDecimal128(zm.GetMinBuf()), types.DecodeDecimal128(zm.GetMaxBuf())
   885  		lowerBound := sort.Search(len(col), func(i int) bool {
   886  			return !col[i].Less(minVal)
   887  		})
   888  		upperBound := sort.Search(len(col), func(i int) bool {
   889  			return maxVal.Less(col[i])
   890  		})
   891  		return lowerBound, upperBound
   892  
   893  	case types.T_TS:
   894  		col := vector.MustFixedCol[types.TS](vec)
   895  		minVal, maxVal := types.DecodeFixed[types.TS](zm.GetMinBuf()), types.DecodeFixed[types.TS](zm.GetMaxBuf())
   896  		lowerBound := sort.Search(len(col), func(i int) bool {
   897  			return minVal.LessEq(&col[i])
   898  		})
   899  		upperBound := sort.Search(len(col), func(i int) bool {
   900  			return maxVal.Less(&col[i])
   901  		})
   902  		return lowerBound, upperBound
   903  
   904  	case types.T_uuid:
   905  		col := vector.MustFixedCol[types.Uuid](vec)
   906  		minVal, maxVal := types.DecodeUuid(zm.GetMinBuf()), types.DecodeUuid(zm.GetMaxBuf())
   907  		lowerBound := sort.Search(len(col), func(i int) bool {
   908  			return minVal.Le(col[i])
   909  		})
   910  		upperBound := sort.Search(len(col), func(i int) bool {
   911  			return maxVal.Lt(col[i])
   912  		})
   913  		return lowerBound, upperBound
   914  
   915  	case types.T_Rowid:
   916  		col := vector.MustFixedCol[types.Rowid](vec)
   917  		minVal, maxVal := types.DecodeFixed[types.Rowid](zm.GetMinBuf()), types.DecodeFixed[types.Rowid](zm.GetMaxBuf())
   918  		lowerBound := sort.Search(len(col), func(i int) bool {
   919  			return minVal.Le(col[i])
   920  		})
   921  		upperBound := sort.Search(len(col), func(i int) bool {
   922  			return maxVal.Less(col[i])
   923  		})
   924  		return lowerBound, upperBound
   925  
   926  	case types.T_char, types.T_varchar, types.T_json, types.T_binary, types.T_varbinary, types.T_blob, types.T_text:
   927  		col, area := vector.MustVarlenaRawData(vec)
   928  		minVal, maxVal := zm.GetMinBuf(), zm.GetMaxBuf()
   929  		lowerBound := sort.Search(len(col), func(i int) bool {
   930  			return types.PrefixCompare(minVal, col[i].GetByteSlice(area)) <= 0
   931  		})
   932  		upperBound := sort.Search(len(col), func(i int) bool {
   933  			return types.PrefixCompare(maxVal, col[i].GetByteSlice(area)) < 0
   934  		})
   935  		return lowerBound, upperBound
   936  
   937  	case types.T_array_float32:
   938  		col := vector.MustArrayCol[float32](vec)
   939  		minVal, maxVal := types.BytesToArray[float32](zm.GetMinBuf()), types.BytesToArray[float32](zm.GetMaxBuf())
   940  		lowerBound := sort.Search(len(col), func(i int) bool {
   941  			return moarray.Compare[float32](minVal, col[i]) <= 0
   942  		})
   943  		upperBound := sort.Search(len(col), func(i int) bool {
   944  			return moarray.Compare[float32](maxVal, col[i]) < 0
   945  		})
   946  		return lowerBound, upperBound
   947  
   948  	case types.T_array_float64:
   949  		col := vector.MustArrayCol[float64](vec)
   950  		minVal, maxVal := types.BytesToArray[float64](zm.GetMinBuf()), types.BytesToArray[float64](zm.GetMaxBuf())
   951  		lowerBound := sort.Search(len(col), func(i int) bool {
   952  			return moarray.Compare[float64](minVal, col[i]) <= 0
   953  		})
   954  		upperBound := sort.Search(len(col), func(i int) bool {
   955  			return moarray.Compare[float64](maxVal, col[i]) < 0
   956  		})
   957  		return lowerBound, upperBound
   958  
   959  	default:
   960  		return 0, vec.Length()
   961  	}
   962  }
   963  
   964  func (zm ZM) AnyIn(vec *vector.Vector) bool {
   965  	switch vec.GetType().Oid {
   966  	case types.T_bool:
   967  		col := vector.MustFixedCol[bool](vec)
   968  		minVal, maxVal := types.DecodeBool(zm.GetMinBuf()), types.DecodeBool(zm.GetMaxBuf())
   969  		lowerBound := sort.Search(len(col), func(i int) bool {
   970  			return !minVal || col[i]
   971  		})
   972  
   973  		return lowerBound < len(col) && (maxVal || !col[lowerBound])
   974  
   975  	case types.T_bit:
   976  		col := vector.MustFixedCol[uint64](vec)
   977  		minVal, maxVal := types.DecodeUint64(zm.GetMinBuf()), types.DecodeUint64(zm.GetMaxBuf())
   978  		lowerBound := sort.Search(len(col), func(i int) bool {
   979  			return minVal <= col[i]
   980  		})
   981  
   982  		return lowerBound < len(col) && maxVal >= col[lowerBound]
   983  
   984  	case types.T_int8:
   985  		col := vector.MustFixedCol[int8](vec)
   986  		minVal, maxVal := types.DecodeInt8(zm.GetMinBuf()), types.DecodeInt8(zm.GetMaxBuf())
   987  		lowerBound := sort.Search(len(col), func(i int) bool {
   988  			return minVal <= col[i]
   989  		})
   990  
   991  		return lowerBound < len(col) && maxVal >= col[lowerBound]
   992  
   993  	case types.T_int16:
   994  		col := vector.MustFixedCol[int16](vec)
   995  		minVal, maxVal := types.DecodeInt16(zm.GetMinBuf()), types.DecodeInt16(zm.GetMaxBuf())
   996  		lowerBound := sort.Search(len(col), func(i int) bool {
   997  			return minVal <= col[i]
   998  		})
   999  
  1000  		return lowerBound < len(col) && maxVal >= col[lowerBound]
  1001  
  1002  	case types.T_int32:
  1003  		col := vector.MustFixedCol[int32](vec)
  1004  		minVal, maxVal := types.DecodeInt32(zm.GetMinBuf()), types.DecodeInt32(zm.GetMaxBuf())
  1005  		lowerBound := sort.Search(len(col), func(i int) bool {
  1006  			return minVal <= col[i]
  1007  		})
  1008  
  1009  		return lowerBound < len(col) && maxVal >= col[lowerBound]
  1010  
  1011  	case types.T_int64:
  1012  		col := vector.MustFixedCol[int64](vec)
  1013  		minVal, maxVal := types.DecodeInt64(zm.GetMinBuf()), types.DecodeInt64(zm.GetMaxBuf())
  1014  		lowerBound := sort.Search(len(col), func(i int) bool {
  1015  			return minVal <= col[i]
  1016  		})
  1017  
  1018  		return lowerBound < len(col) && maxVal >= col[lowerBound]
  1019  
  1020  	case types.T_uint8:
  1021  		col := vector.MustFixedCol[uint8](vec)
  1022  		minVal, maxVal := types.DecodeUint8(zm.GetMinBuf()), types.DecodeUint8(zm.GetMaxBuf())
  1023  		lowerBound := sort.Search(len(col), func(i int) bool {
  1024  			return minVal <= col[i]
  1025  		})
  1026  
  1027  		return lowerBound < len(col) && maxVal >= col[lowerBound]
  1028  
  1029  	case types.T_uint16:
  1030  		col := vector.MustFixedCol[uint16](vec)
  1031  		minVal, maxVal := types.DecodeUint16(zm.GetMinBuf()), types.DecodeUint16(zm.GetMaxBuf())
  1032  		lowerBound := sort.Search(len(col), func(i int) bool {
  1033  			return minVal <= col[i]
  1034  		})
  1035  
  1036  		return lowerBound < len(col) && maxVal >= col[lowerBound]
  1037  
  1038  	case types.T_uint32:
  1039  		col := vector.MustFixedCol[uint32](vec)
  1040  		minVal, maxVal := types.DecodeUint32(zm.GetMinBuf()), types.DecodeUint32(zm.GetMaxBuf())
  1041  		lowerBound := sort.Search(len(col), func(i int) bool {
  1042  			return minVal <= col[i]
  1043  		})
  1044  
  1045  		return lowerBound < len(col) && maxVal >= col[lowerBound]
  1046  
  1047  	case types.T_uint64:
  1048  		col := vector.MustFixedCol[uint64](vec)
  1049  		minVal, maxVal := types.DecodeUint64(zm.GetMinBuf()), types.DecodeUint64(zm.GetMaxBuf())
  1050  		lowerBound := sort.Search(len(col), func(i int) bool {
  1051  			return minVal <= col[i]
  1052  		})
  1053  
  1054  		return lowerBound < len(col) && maxVal >= col[lowerBound]
  1055  
  1056  	case types.T_float32:
  1057  		col := vector.MustFixedCol[float32](vec)
  1058  		minVal, maxVal := types.DecodeFloat32(zm.GetMinBuf()), types.DecodeFloat32(zm.GetMaxBuf())
  1059  		lowerBound := sort.Search(len(col), func(i int) bool {
  1060  			return minVal <= col[i]
  1061  		})
  1062  
  1063  		return lowerBound < len(col) && maxVal >= col[lowerBound]
  1064  
  1065  	case types.T_float64:
  1066  		col := vector.MustFixedCol[float64](vec)
  1067  		minVal, maxVal := types.DecodeFloat64(zm.GetMinBuf()), types.DecodeFloat64(zm.GetMaxBuf())
  1068  		lowerBound := sort.Search(len(col), func(i int) bool {
  1069  			return minVal <= col[i]
  1070  		})
  1071  
  1072  		return lowerBound < len(col) && maxVal >= col[lowerBound]
  1073  
  1074  	case types.T_date:
  1075  		col := vector.MustFixedCol[types.Date](vec)
  1076  		minVal, maxVal := types.DecodeDate(zm.GetMinBuf()), types.DecodeDate(zm.GetMaxBuf())
  1077  		lowerBound := sort.Search(len(col), func(i int) bool {
  1078  			return minVal <= col[i]
  1079  		})
  1080  
  1081  		return lowerBound < len(col) && maxVal >= col[lowerBound]
  1082  
  1083  	case types.T_datetime:
  1084  		col := vector.MustFixedCol[types.Datetime](vec)
  1085  		minVal, maxVal := types.DecodeDatetime(zm.GetMinBuf()), types.DecodeDatetime(zm.GetMaxBuf())
  1086  		lowerBound := sort.Search(len(col), func(i int) bool {
  1087  			return minVal <= col[i]
  1088  		})
  1089  
  1090  		return lowerBound < len(col) && maxVal >= col[lowerBound]
  1091  
  1092  	case types.T_time:
  1093  		col := vector.MustFixedCol[types.Time](vec)
  1094  		minVal, maxVal := types.DecodeTime(zm.GetMinBuf()), types.DecodeTime(zm.GetMaxBuf())
  1095  		lowerBound := sort.Search(len(col), func(i int) bool {
  1096  			return minVal <= col[i]
  1097  		})
  1098  
  1099  		return lowerBound < len(col) && maxVal >= col[lowerBound]
  1100  
  1101  	case types.T_timestamp:
  1102  		col := vector.MustFixedCol[types.Timestamp](vec)
  1103  		minVal, maxVal := types.DecodeTimestamp(zm.GetMinBuf()), types.DecodeTimestamp(zm.GetMaxBuf())
  1104  		lowerBound := sort.Search(len(col), func(i int) bool {
  1105  			return minVal <= col[i]
  1106  		})
  1107  
  1108  		return lowerBound < len(col) && maxVal >= col[lowerBound]
  1109  
  1110  	case types.T_enum:
  1111  		col := vector.MustFixedCol[types.Enum](vec)
  1112  		minVal, maxVal := types.DecodeEnum(zm.GetMinBuf()), types.DecodeEnum(zm.GetMaxBuf())
  1113  		lowerBound := sort.Search(len(col), func(i int) bool {
  1114  			return minVal <= col[i]
  1115  		})
  1116  
  1117  		return lowerBound < len(col) && maxVal >= col[lowerBound]
  1118  
  1119  	case types.T_decimal64:
  1120  		col := vector.MustFixedCol[types.Decimal64](vec)
  1121  		minVal, maxVal := types.DecodeDecimal64(zm.GetMinBuf()), types.DecodeDecimal64(zm.GetMaxBuf())
  1122  		lowerBound := sort.Search(len(col), func(i int) bool {
  1123  			return !col[i].Less(minVal)
  1124  		})
  1125  
  1126  		return lowerBound < len(col) && !maxVal.Less(col[lowerBound])
  1127  
  1128  	case types.T_decimal128:
  1129  		col := vector.MustFixedCol[types.Decimal128](vec)
  1130  		minVal, maxVal := types.DecodeDecimal128(zm.GetMinBuf()), types.DecodeDecimal128(zm.GetMaxBuf())
  1131  		lowerBound := sort.Search(len(col), func(i int) bool {
  1132  			return !col[i].Less(minVal)
  1133  		})
  1134  
  1135  		return lowerBound < len(col) && !maxVal.Less(col[lowerBound])
  1136  
  1137  	case types.T_TS:
  1138  		col := vector.MustFixedCol[types.TS](vec)
  1139  		minVal, maxVal := types.DecodeFixed[types.TS](zm.GetMinBuf()), types.DecodeFixed[types.TS](zm.GetMaxBuf())
  1140  		lowerBound := sort.Search(len(col), func(i int) bool {
  1141  			return minVal.LessEq(&col[i])
  1142  		})
  1143  
  1144  		return lowerBound < len(col) && col[lowerBound].LessEq(&maxVal)
  1145  
  1146  	case types.T_uuid:
  1147  		col := vector.MustFixedCol[types.Uuid](vec)
  1148  		minVal, maxVal := types.DecodeUuid(zm.GetMinBuf()), types.DecodeUuid(zm.GetMaxBuf())
  1149  		lowerBound := sort.Search(len(col), func(i int) bool {
  1150  			return minVal.Le(col[i])
  1151  		})
  1152  
  1153  		return lowerBound < len(col) && col[lowerBound].Le(maxVal)
  1154  
  1155  	case types.T_Rowid:
  1156  		col := vector.MustFixedCol[types.Rowid](vec)
  1157  		minVal, maxVal := types.DecodeFixed[types.Rowid](zm.GetMinBuf()), types.DecodeFixed[types.Rowid](zm.GetMaxBuf())
  1158  		lowerBound := sort.Search(len(col), func(i int) bool {
  1159  			return minVal.Le(col[i])
  1160  		})
  1161  
  1162  		return lowerBound < len(col) && col[lowerBound].Le(maxVal)
  1163  
  1164  	case types.T_char, types.T_varchar, types.T_json, types.T_binary, types.T_varbinary, types.T_blob, types.T_text:
  1165  		col, area := vector.MustVarlenaRawData(vec)
  1166  		minVal, maxVal := zm.GetMinBuf(), zm.GetMaxBuf()
  1167  		lowerBound := sort.Search(len(col), func(i int) bool {
  1168  			return types.PrefixCompare(minVal, col[i].GetByteSlice(area)) <= 0
  1169  		})
  1170  
  1171  		return lowerBound < len(col) && types.PrefixCompare(maxVal, col[lowerBound].GetByteSlice(area)) >= 0
  1172  
  1173  	case types.T_array_float32:
  1174  		col := vector.MustArrayCol[float32](vec)
  1175  		minVal, maxVal := types.BytesToArray[float32](zm.GetMinBuf()), types.BytesToArray[float32](zm.GetMaxBuf())
  1176  		lowerBound := sort.Search(len(col), func(i int) bool {
  1177  			return moarray.Compare[float32](minVal, col[i]) <= 0
  1178  		})
  1179  
  1180  		return lowerBound < len(col) && moarray.Compare[float32](maxVal, col[lowerBound]) >= 0
  1181  
  1182  	case types.T_array_float64:
  1183  		col := vector.MustArrayCol[float64](vec)
  1184  		minVal, maxVal := types.BytesToArray[float64](zm.GetMinBuf()), types.BytesToArray[float64](zm.GetMaxBuf())
  1185  		lowerBound := sort.Search(len(col), func(i int) bool {
  1186  			return moarray.Compare[float64](minVal, col[i]) <= 0
  1187  		})
  1188  
  1189  		return lowerBound < len(col) && moarray.Compare[float64](maxVal, col[lowerBound]) >= 0
  1190  
  1191  	default:
  1192  		return true
  1193  	}
  1194  }
  1195  
  1196  // max = v1.max+v2.max
  1197  // min = v1.min+v2.min
  1198  func ZMPlus(v1, v2, res ZM) ZM {
  1199  	res.Reset()
  1200  	if !v1.compareCheck(v2) {
  1201  		return res
  1202  	}
  1203  	// check supported type
  1204  	if len(res) != ZMSize {
  1205  		res = NewZM(v1.GetType(), v1.GetScale())
  1206  	}
  1207  	if !applyArithmetic(v1, v2, res, '+', v1.GetScale(), v2.GetScale()) {
  1208  		res.Reset()
  1209  	}
  1210  	return res
  1211  }
  1212  
  1213  // max = v1.max-v2.min
  1214  // min = v1.max-v2.min
  1215  func ZMMinus(v1, v2, res ZM) ZM {
  1216  	res.Reset()
  1217  	if !v1.compareCheck(v2) {
  1218  		return res
  1219  	}
  1220  	// check supported type
  1221  	if len(res) != ZMSize {
  1222  		res = NewZM(v1.GetType(), v1.GetScale())
  1223  	}
  1224  	if !applyArithmetic(v1, v2, res, '-', v1.GetScale(), v2.GetScale()) {
  1225  		res.Reset()
  1226  	}
  1227  	return res
  1228  }
  1229  
  1230  // v1 product v2 => p[r0,r1,r2,r3]
  1231  // min,max = Min(p),Max(p)
  1232  func ZMMulti(v1, v2, res ZM) ZM {
  1233  	res.Reset()
  1234  	if !v1.compareCheck(v2) {
  1235  		return res
  1236  	}
  1237  	// check supported type
  1238  	if len(res) != ZMSize {
  1239  		res = NewZM(v1.GetType(), v1.GetScale())
  1240  	}
  1241  	if !applyArithmetic(v1, v2, res, '*', v1.GetScale(), v2.GetScale()) {
  1242  		res.Reset()
  1243  	}
  1244  	return res
  1245  }
  1246  
  1247  func applyArithmetic(v1, v2, res ZM, op byte, scale1, scale2 int32) (ok bool) {
  1248  	ok = true
  1249  	switch v1.GetType() {
  1250  	case types.T_bit:
  1251  		var minv, maxv uint64
  1252  		switch op {
  1253  		case '+':
  1254  			maxv = types.DecodeUint64(v1.GetMaxBuf()) + types.DecodeUint64(v2.GetMaxBuf())
  1255  			minv = types.DecodeUint64(v1.GetMinBuf()) + types.DecodeUint64(v2.GetMinBuf())
  1256  		case '-':
  1257  			maxv = types.DecodeUint64(v1.GetMaxBuf()) - types.DecodeUint64(v2.GetMinBuf())
  1258  			minv = types.DecodeUint64(v1.GetMinBuf()) - types.DecodeUint64(v2.GetMaxBuf())
  1259  		case '*':
  1260  			v1_0, v1_1 := types.DecodeUint64(v1.GetMinBuf()), types.DecodeUint64(v1.GetMaxBuf())
  1261  			v2_0, v2_1 := types.DecodeUint64(v2.GetMinBuf()), types.DecodeUint64(v2.GetMaxBuf())
  1262  			minv, maxv = compute.GetOrderedMinAndMax(v1_0*v2_0, v1_0*v2_1, v1_1*v2_0, v1_1*v2_1)
  1263  		default:
  1264  			ok = false
  1265  			return
  1266  		}
  1267  		UpdateZM(res, types.EncodeUint64(&minv))
  1268  		UpdateZM(res, types.EncodeUint64(&maxv))
  1269  	case types.T_int8:
  1270  		var minv, maxv int8
  1271  		switch op {
  1272  		case '+':
  1273  			maxv = types.DecodeInt8(v1.GetMaxBuf()) + types.DecodeInt8(v2.GetMaxBuf())
  1274  			minv = types.DecodeInt8(v1.GetMinBuf()) + types.DecodeInt8(v2.GetMinBuf())
  1275  		case '-':
  1276  			maxv = types.DecodeInt8(v1.GetMaxBuf()) - types.DecodeInt8(v2.GetMinBuf())
  1277  			minv = types.DecodeInt8(v1.GetMinBuf()) - types.DecodeInt8(v2.GetMaxBuf())
  1278  		case '*':
  1279  			v1_0, v1_1 := types.DecodeInt8(v1.GetMinBuf()), types.DecodeInt8(v1.GetMaxBuf())
  1280  			v2_0, v2_1 := types.DecodeInt8(v2.GetMinBuf()), types.DecodeInt8(v2.GetMaxBuf())
  1281  			minv, maxv = compute.GetOrderedMinAndMax(v1_0*v2_0, v1_0*v2_1, v1_1*v2_0, v1_1*v2_1)
  1282  		default:
  1283  			ok = false
  1284  			return
  1285  		}
  1286  		UpdateZM(res, types.EncodeInt8(&minv))
  1287  		UpdateZM(res, types.EncodeInt8(&maxv))
  1288  	case types.T_int16:
  1289  		var minv, maxv int16
  1290  		switch op {
  1291  		case '+':
  1292  			maxv = types.DecodeInt16(v1.GetMaxBuf()) + types.DecodeInt16(v2.GetMaxBuf())
  1293  			minv = types.DecodeInt16(v1.GetMinBuf()) + types.DecodeInt16(v2.GetMinBuf())
  1294  		case '-':
  1295  			maxv = types.DecodeInt16(v1.GetMaxBuf()) - types.DecodeInt16(v2.GetMinBuf())
  1296  			minv = types.DecodeInt16(v1.GetMinBuf()) - types.DecodeInt16(v2.GetMaxBuf())
  1297  		case '*':
  1298  			v1_0, v1_1 := types.DecodeInt16(v1.GetMinBuf()), types.DecodeInt16(v1.GetMaxBuf())
  1299  			v2_0, v2_1 := types.DecodeInt16(v2.GetMinBuf()), types.DecodeInt16(v2.GetMaxBuf())
  1300  			minv, maxv = compute.GetOrderedMinAndMax(v1_0*v2_0, v1_0*v2_1, v1_1*v2_0, v1_1*v2_1)
  1301  		default:
  1302  			ok = false
  1303  			return
  1304  		}
  1305  		UpdateZM(res, types.EncodeInt16(&minv))
  1306  		UpdateZM(res, types.EncodeInt16(&maxv))
  1307  	case types.T_int32:
  1308  		var minv, maxv int32
  1309  		switch op {
  1310  		case '+':
  1311  			maxv = types.DecodeInt32(v1.GetMaxBuf()) + types.DecodeInt32(v2.GetMaxBuf())
  1312  			minv = types.DecodeInt32(v1.GetMinBuf()) + types.DecodeInt32(v2.GetMinBuf())
  1313  		case '-':
  1314  			maxv = types.DecodeInt32(v1.GetMaxBuf()) - types.DecodeInt32(v2.GetMinBuf())
  1315  			minv = types.DecodeInt32(v1.GetMinBuf()) - types.DecodeInt32(v2.GetMaxBuf())
  1316  		case '*':
  1317  			v1_0, v1_1 := types.DecodeInt32(v1.GetMinBuf()), types.DecodeInt32(v1.GetMaxBuf())
  1318  			v2_0, v2_1 := types.DecodeInt32(v2.GetMinBuf()), types.DecodeInt32(v2.GetMaxBuf())
  1319  			minv, maxv = compute.GetOrderedMinAndMax(v1_0*v2_0, v1_0*v2_1, v1_1*v2_0, v1_1*v2_1)
  1320  		default:
  1321  			ok = false
  1322  			return
  1323  		}
  1324  		UpdateZM(res, types.EncodeInt32(&minv))
  1325  		UpdateZM(res, types.EncodeInt32(&maxv))
  1326  	case types.T_int64:
  1327  		var minv, maxv int64
  1328  		switch op {
  1329  		case '+':
  1330  			maxv = types.DecodeInt64(v1.GetMaxBuf()) + types.DecodeInt64(v2.GetMaxBuf())
  1331  			minv = types.DecodeInt64(v1.GetMinBuf()) + types.DecodeInt64(v2.GetMinBuf())
  1332  		case '-':
  1333  			maxv = types.DecodeInt64(v1.GetMaxBuf()) - types.DecodeInt64(v2.GetMinBuf())
  1334  			minv = types.DecodeInt64(v1.GetMinBuf()) - types.DecodeInt64(v2.GetMaxBuf())
  1335  		case '*':
  1336  			v1_0, v1_1 := types.DecodeInt64(v1.GetMinBuf()), types.DecodeInt64(v1.GetMaxBuf())
  1337  			v2_0, v2_1 := types.DecodeInt64(v2.GetMinBuf()), types.DecodeInt64(v2.GetMaxBuf())
  1338  			minv, maxv = compute.GetOrderedMinAndMax(v1_0*v2_0, v1_0*v2_1, v1_1*v2_0, v1_1*v2_1)
  1339  		default:
  1340  			ok = false
  1341  			return
  1342  		}
  1343  		UpdateZM(res, types.EncodeInt64(&minv))
  1344  		UpdateZM(res, types.EncodeInt64(&maxv))
  1345  	case types.T_uint8:
  1346  		var minv, maxv uint8
  1347  		switch op {
  1348  		case '+':
  1349  			maxv = types.DecodeUint8(v1.GetMaxBuf()) + types.DecodeUint8(v2.GetMaxBuf())
  1350  			minv = types.DecodeUint8(v1.GetMinBuf()) + types.DecodeUint8(v2.GetMinBuf())
  1351  		case '-':
  1352  			maxv = types.DecodeUint8(v1.GetMaxBuf()) - types.DecodeUint8(v2.GetMinBuf())
  1353  			minv = types.DecodeUint8(v1.GetMinBuf()) - types.DecodeUint8(v2.GetMaxBuf())
  1354  		case '*':
  1355  			v1_0, v1_1 := types.DecodeUint8(v1.GetMinBuf()), types.DecodeUint8(v1.GetMaxBuf())
  1356  			v2_0, v2_1 := types.DecodeUint8(v2.GetMinBuf()), types.DecodeUint8(v2.GetMaxBuf())
  1357  			minv, maxv = compute.GetOrderedMinAndMax(v1_0*v2_0, v1_0*v2_1, v1_1*v2_0, v1_1*v2_1)
  1358  		default:
  1359  			ok = false
  1360  			return
  1361  		}
  1362  		UpdateZM(res, types.EncodeUint8(&minv))
  1363  		UpdateZM(res, types.EncodeUint8(&maxv))
  1364  	case types.T_uint16:
  1365  		var minv, maxv uint16
  1366  		switch op {
  1367  		case '+':
  1368  			maxv = types.DecodeUint16(v1.GetMaxBuf()) + types.DecodeUint16(v2.GetMaxBuf())
  1369  			minv = types.DecodeUint16(v1.GetMinBuf()) + types.DecodeUint16(v2.GetMinBuf())
  1370  		case '-':
  1371  			maxv = types.DecodeUint16(v1.GetMaxBuf()) - types.DecodeUint16(v2.GetMinBuf())
  1372  			minv = types.DecodeUint16(v1.GetMinBuf()) - types.DecodeUint16(v2.GetMaxBuf())
  1373  		case '*':
  1374  			v1_0, v1_1 := types.DecodeUint16(v1.GetMinBuf()), types.DecodeUint16(v1.GetMaxBuf())
  1375  			v2_0, v2_1 := types.DecodeUint16(v2.GetMinBuf()), types.DecodeUint16(v2.GetMaxBuf())
  1376  			minv, maxv = compute.GetOrderedMinAndMax(v1_0*v2_0, v1_0*v2_1, v1_1*v2_0, v1_1*v2_1)
  1377  		default:
  1378  			ok = false
  1379  			return
  1380  		}
  1381  		UpdateZM(res, types.EncodeUint16(&minv))
  1382  		UpdateZM(res, types.EncodeUint16(&maxv))
  1383  	case types.T_uint32:
  1384  		var minv, maxv uint32
  1385  		switch op {
  1386  		case '+':
  1387  			maxv = types.DecodeUint32(v1.GetMaxBuf()) + types.DecodeUint32(v2.GetMaxBuf())
  1388  			minv = types.DecodeUint32(v1.GetMinBuf()) + types.DecodeUint32(v2.GetMinBuf())
  1389  		case '-':
  1390  			maxv = types.DecodeUint32(v1.GetMaxBuf()) - types.DecodeUint32(v2.GetMinBuf())
  1391  			minv = types.DecodeUint32(v1.GetMinBuf()) - types.DecodeUint32(v2.GetMaxBuf())
  1392  		case '*':
  1393  			v1_0, v1_1 := types.DecodeUint32(v1.GetMinBuf()), types.DecodeUint32(v1.GetMaxBuf())
  1394  			v2_0, v2_1 := types.DecodeUint32(v2.GetMinBuf()), types.DecodeUint32(v2.GetMaxBuf())
  1395  			minv, maxv = compute.GetOrderedMinAndMax(v1_0*v2_0, v1_0*v2_1, v1_1*v2_0, v1_1*v2_1)
  1396  		default:
  1397  			ok = false
  1398  			return
  1399  		}
  1400  		UpdateZM(res, types.EncodeUint32(&minv))
  1401  		UpdateZM(res, types.EncodeUint32(&maxv))
  1402  	case types.T_uint64:
  1403  		var minv, maxv uint64
  1404  		switch op {
  1405  		case '+':
  1406  			maxv = types.DecodeUint64(v1.GetMaxBuf()) + types.DecodeUint64(v2.GetMaxBuf())
  1407  			minv = types.DecodeUint64(v1.GetMinBuf()) + types.DecodeUint64(v2.GetMinBuf())
  1408  		case '-':
  1409  			maxv = types.DecodeUint64(v1.GetMaxBuf()) - types.DecodeUint64(v2.GetMinBuf())
  1410  			minv = types.DecodeUint64(v1.GetMinBuf()) - types.DecodeUint64(v2.GetMaxBuf())
  1411  		case '*':
  1412  			v1_0, v1_1 := types.DecodeUint64(v1.GetMinBuf()), types.DecodeUint64(v1.GetMaxBuf())
  1413  			v2_0, v2_1 := types.DecodeUint64(v2.GetMinBuf()), types.DecodeUint64(v2.GetMaxBuf())
  1414  			minv, maxv = compute.GetOrderedMinAndMax(v1_0*v2_0, v1_0*v2_1, v1_1*v2_0, v1_1*v2_1)
  1415  		default:
  1416  			ok = false
  1417  			return
  1418  		}
  1419  		UpdateZM(res, types.EncodeUint64(&minv))
  1420  		UpdateZM(res, types.EncodeUint64(&maxv))
  1421  	case types.T_float32:
  1422  		var minv, maxv float32
  1423  		switch op {
  1424  		case '+':
  1425  			maxv = types.DecodeFloat32(v1.GetMaxBuf()) + types.DecodeFloat32(v2.GetMaxBuf())
  1426  			minv = types.DecodeFloat32(v1.GetMinBuf()) + types.DecodeFloat32(v2.GetMinBuf())
  1427  		case '-':
  1428  			maxv = types.DecodeFloat32(v1.GetMaxBuf()) - types.DecodeFloat32(v2.GetMinBuf())
  1429  			minv = types.DecodeFloat32(v1.GetMinBuf()) - types.DecodeFloat32(v2.GetMaxBuf())
  1430  		case '*':
  1431  			v1_0, v1_1 := types.DecodeFloat32(v1.GetMinBuf()), types.DecodeFloat32(v1.GetMaxBuf())
  1432  			v2_0, v2_1 := types.DecodeFloat32(v2.GetMinBuf()), types.DecodeFloat32(v2.GetMaxBuf())
  1433  			minv, maxv = compute.GetOrderedMinAndMax(v1_0*v2_0, v1_0*v2_1, v1_1*v2_0, v1_1*v2_1)
  1434  		default:
  1435  			ok = false
  1436  			return
  1437  		}
  1438  		UpdateZM(res, types.EncodeFloat32(&minv))
  1439  		UpdateZM(res, types.EncodeFloat32(&maxv))
  1440  	case types.T_float64:
  1441  		var minv, maxv float64
  1442  		switch op {
  1443  		case '+':
  1444  			maxv = types.DecodeFloat64(v1.GetMaxBuf()) + types.DecodeFloat64(v2.GetMaxBuf())
  1445  			minv = types.DecodeFloat64(v1.GetMinBuf()) + types.DecodeFloat64(v2.GetMinBuf())
  1446  		case '-':
  1447  			maxv = types.DecodeFloat64(v1.GetMaxBuf()) - types.DecodeFloat64(v2.GetMinBuf())
  1448  			minv = types.DecodeFloat64(v1.GetMinBuf()) - types.DecodeFloat64(v2.GetMaxBuf())
  1449  		case '*':
  1450  			v1_0, v1_1 := types.DecodeFloat64(v1.GetMinBuf()), types.DecodeFloat64(v1.GetMaxBuf())
  1451  			v2_0, v2_1 := types.DecodeFloat64(v2.GetMinBuf()), types.DecodeFloat64(v2.GetMaxBuf())
  1452  			minv, maxv = compute.GetOrderedMinAndMax(v1_0*v2_0, v1_0*v2_1, v1_1*v2_0, v1_1*v2_1)
  1453  		default:
  1454  			ok = false
  1455  			return
  1456  		}
  1457  		UpdateZM(res, types.EncodeFloat64(&minv))
  1458  		UpdateZM(res, types.EncodeFloat64(&maxv))
  1459  	case types.T_date:
  1460  		var minv, maxv types.Date
  1461  		switch op {
  1462  		case '+':
  1463  			maxv = types.DecodeDate(v1.GetMaxBuf()) + types.DecodeDate(v2.GetMaxBuf())
  1464  			minv = types.DecodeDate(v1.GetMinBuf()) + types.DecodeDate(v2.GetMinBuf())
  1465  		case '-':
  1466  			maxv = types.DecodeDate(v1.GetMaxBuf()) - types.DecodeDate(v2.GetMinBuf())
  1467  			minv = types.DecodeDate(v1.GetMinBuf()) - types.DecodeDate(v2.GetMaxBuf())
  1468  		case '*':
  1469  			v1_0, v1_1 := types.DecodeDate(v1.GetMinBuf()), types.DecodeDate(v1.GetMaxBuf())
  1470  			v2_0, v2_1 := types.DecodeDate(v2.GetMinBuf()), types.DecodeDate(v2.GetMaxBuf())
  1471  			minv, maxv = compute.GetOrderedMinAndMax(v1_0*v2_0, v1_0*v2_1, v1_1*v2_0, v1_1*v2_1)
  1472  		default:
  1473  			ok = false
  1474  			return
  1475  		}
  1476  		UpdateZM(res, types.EncodeDate(&minv))
  1477  		UpdateZM(res, types.EncodeDate(&maxv))
  1478  	case types.T_datetime:
  1479  		var minv, maxv types.Datetime
  1480  		switch op {
  1481  		case '+':
  1482  			maxv = types.DecodeDatetime(v1.GetMaxBuf()) + types.DecodeDatetime(v2.GetMaxBuf())
  1483  			minv = types.DecodeDatetime(v1.GetMinBuf()) + types.DecodeDatetime(v2.GetMinBuf())
  1484  		case '-':
  1485  			maxv = types.DecodeDatetime(v1.GetMaxBuf()) - types.DecodeDatetime(v2.GetMinBuf())
  1486  			minv = types.DecodeDatetime(v1.GetMinBuf()) - types.DecodeDatetime(v2.GetMaxBuf())
  1487  		case '*':
  1488  			v1_0, v1_1 := types.DecodeDatetime(v1.GetMinBuf()), types.DecodeDatetime(v1.GetMaxBuf())
  1489  			v2_0, v2_1 := types.DecodeDatetime(v2.GetMinBuf()), types.DecodeDatetime(v2.GetMaxBuf())
  1490  			minv, maxv = compute.GetOrderedMinAndMax(v1_0*v2_0, v1_0*v2_1, v1_1*v2_0, v1_1*v2_1)
  1491  		default:
  1492  			ok = false
  1493  			return
  1494  		}
  1495  		UpdateZM(res, types.EncodeDatetime(&minv))
  1496  		UpdateZM(res, types.EncodeDatetime(&maxv))
  1497  	case types.T_decimal64:
  1498  		var minv, maxv types.Decimal64
  1499  		var newScale int32
  1500  		var err error
  1501  		switch op {
  1502  		case '+':
  1503  			maxv = types.DecodeDecimal64(v1.GetMaxBuf())
  1504  			if maxv, newScale, err = maxv.Add(types.DecodeDecimal64(v2.GetMaxBuf()), scale1, scale2); err != nil {
  1505  				ok = false
  1506  				return
  1507  			}
  1508  			minv = types.DecodeDecimal64(v1.GetMinBuf())
  1509  			if minv, _, err = minv.Add(types.DecodeDecimal64(v2.GetMinBuf()), scale1, scale2); err != nil {
  1510  				ok = false
  1511  				return
  1512  			}
  1513  		case '-':
  1514  			maxv = types.DecodeDecimal64(v1.GetMaxBuf())
  1515  			if maxv, newScale, err = maxv.Sub(types.DecodeDecimal64(v2.GetMinBuf()), scale1, scale2); err != nil {
  1516  				ok = false
  1517  				return
  1518  			}
  1519  			minv = types.DecodeDecimal64(v1.GetMinBuf())
  1520  			if minv, _, err = minv.Sub(types.DecodeDecimal64(v2.GetMaxBuf()), scale1, scale2); err != nil {
  1521  				ok = false
  1522  				return
  1523  			}
  1524  		case '*':
  1525  			rs := make([]types.Decimal64, 4)
  1526  			v1_0, v1_1 := types.DecodeDecimal64(v1.GetMinBuf()), types.DecodeDecimal64(v1.GetMaxBuf())
  1527  			v2_0, v2_1 := types.DecodeDecimal64(v2.GetMinBuf()), types.DecodeDecimal64(v2.GetMaxBuf())
  1528  			if rs[0], newScale, err = v1_0.Mul(v2_0, scale1, scale2); err != nil {
  1529  				ok = false
  1530  				return
  1531  			}
  1532  			if rs[1], _, err = v1_1.Mul(v2_0, scale1, scale2); err != nil {
  1533  				ok = false
  1534  				return
  1535  			}
  1536  			if rs[2], _, err = v1_0.Mul(v2_1, scale1, scale2); err != nil {
  1537  				ok = false
  1538  				return
  1539  			}
  1540  			if rs[3], _, err = v1_1.Mul(v2_1, scale1, scale2); err != nil {
  1541  				ok = false
  1542  				return
  1543  			}
  1544  			minv, maxv = compute.GetDecimal64MinAndMax(rs)
  1545  		default:
  1546  			ok = false
  1547  			return
  1548  		}
  1549  		res.SetScale(newScale)
  1550  		UpdateZM(res, types.EncodeDecimal64(&minv))
  1551  		UpdateZM(res, types.EncodeDecimal64(&maxv))
  1552  	case types.T_decimal128:
  1553  		var newScale int32
  1554  		var (
  1555  			err        error
  1556  			minv, maxv types.Decimal128
  1557  		)
  1558  		switch op {
  1559  		case '+':
  1560  			maxv = types.DecodeDecimal128(v1.GetMaxBuf())
  1561  			if maxv, newScale, err = maxv.Add(types.DecodeDecimal128(v2.GetMaxBuf()), scale1, scale2); err != nil {
  1562  				ok = false
  1563  				return
  1564  			}
  1565  			minv = types.DecodeDecimal128(v1.GetMinBuf())
  1566  			if minv, _, err = minv.Add(types.DecodeDecimal128(v2.GetMinBuf()), scale1, scale2); err != nil {
  1567  				ok = false
  1568  				return
  1569  			}
  1570  		case '-':
  1571  			maxv = types.DecodeDecimal128(v1.GetMaxBuf())
  1572  			if maxv, newScale, err = maxv.Sub(types.DecodeDecimal128(v2.GetMinBuf()), scale1, scale2); err != nil {
  1573  				ok = false
  1574  				return
  1575  			}
  1576  			minv = types.DecodeDecimal128(v1.GetMinBuf())
  1577  			if minv, _, err = minv.Sub(types.DecodeDecimal128(v2.GetMaxBuf()), scale1, scale2); err != nil {
  1578  				ok = false
  1579  				return
  1580  			}
  1581  		case '*':
  1582  			rs := make([]types.Decimal128, 4)
  1583  			v1_0, v1_1 := types.DecodeDecimal128(v1.GetMinBuf()), types.DecodeDecimal128(v1.GetMaxBuf())
  1584  			v2_0, v2_1 := types.DecodeDecimal128(v2.GetMinBuf()), types.DecodeDecimal128(v2.GetMaxBuf())
  1585  			if rs[0], newScale, err = v1_0.Mul(v2_0, scale1, scale2); err != nil {
  1586  				ok = false
  1587  				return
  1588  			}
  1589  			if rs[1], _, err = v1_1.Mul(v2_0, scale1, scale2); err != nil {
  1590  				ok = false
  1591  				return
  1592  			}
  1593  			if rs[2], _, err = v1_0.Mul(v2_1, scale1, scale2); err != nil {
  1594  				ok = false
  1595  				return
  1596  			}
  1597  			if rs[3], _, err = v1_1.Mul(v2_1, scale1, scale2); err != nil {
  1598  				ok = false
  1599  				return
  1600  			}
  1601  			minv, maxv = compute.GetDecimal128MinAndMax(rs)
  1602  		default:
  1603  			ok = false
  1604  			return
  1605  		}
  1606  		res.SetScale(newScale)
  1607  		UpdateZM(res, types.EncodeDecimal128(&minv))
  1608  		UpdateZM(res, types.EncodeDecimal128(&maxv))
  1609  	}
  1610  	return
  1611  }
  1612  
  1613  func hasMaxPrefix(bs []byte) bool {
  1614  	for i := 0; i < 3; i++ {
  1615  		if types.DecodeFixed[uint64](bs[i*8:(i+1)*8]) != math.MaxUint64 {
  1616  			return false
  1617  		}
  1618  	}
  1619  	if types.DecodeFixed[uint32](bs[24:28]) != math.MaxUint32 {
  1620  		return false
  1621  	}
  1622  	return types.DecodeFixed[uint16](bs[28:30]) == math.MaxUint16
  1623  }
  1624  
  1625  func adjustBytes(bs []byte) {
  1626  	for i := len(bs) - 1; i >= 0; i-- {
  1627  		bs[i] += 1
  1628  		if bs[i] != 0 {
  1629  			break
  1630  		}
  1631  	}
  1632  }
  1633  
  1634  func UpdateZM(zm ZM, v []byte) {
  1635  	if !zm.IsInited() {
  1636  		if zm.IsArray() {
  1637  			// If the zm is of type ARRAY, we don't init it.
  1638  			// vector index will be handled separately using HNSW library etc.
  1639  			return
  1640  		}
  1641  		zm.doInit(v)
  1642  		return
  1643  	}
  1644  	if zm.IsString() {
  1645  		if compute.CompareBytes(v, zm.GetMinBuf()) < 0 {
  1646  			zm.updateMinString(v)
  1647  		} else if compute.CompareBytes(v, zm.GetMaxBuf()) > 0 {
  1648  			zm.updateMaxString(v)
  1649  		}
  1650  		return
  1651  	}
  1652  	t := zm.GetType()
  1653  	scale := zm.GetScale()
  1654  	if compute.Compare(v, zm.GetMinBuf(), t, scale, scale) < 0 {
  1655  		zm.updateMinFixed(v)
  1656  	} else if compute.Compare(v, zm.GetMaxBuf(), t, scale, scale) > 0 {
  1657  		zm.updateMaxFixed(v)
  1658  	}
  1659  }
  1660  
  1661  // it is only used for test
  1662  func UpdateZMAny(zm ZM, v any) {
  1663  	vv := types.EncodeValue(v, zm.GetType())
  1664  	UpdateZM(zm, vv)
  1665  }
  1666  
  1667  func BatchUpdateZM(zm ZM, vec *vector.Vector) (err error) {
  1668  	if ok, minv, maxv := vec.GetMinMaxValue(); ok {
  1669  		UpdateZM(zm, minv)
  1670  		UpdateZM(zm, maxv)
  1671  	}
  1672  	return
  1673  }
  1674  
  1675  func SetZMSum(zm ZM, vec *vector.Vector) {
  1676  	if ok, sumv := vec.GetSumValue(); ok {
  1677  		zm.SetSum(sumv)
  1678  	}
  1679  }
  1680  
  1681  func EncodeZM(zm *ZM) []byte {
  1682  	return *zm
  1683  }
  1684  
  1685  func DecodeZM(buf []byte) ZM {
  1686  	return buf[:ZMSize]
  1687  }
  1688  
  1689  func SetBool(zm ZM, v bool) ZM {
  1690  	if len(zm) != ZMSize {
  1691  		zm = NewZM(types.T_bool, 0)
  1692  	}
  1693  	buf := types.EncodeBool(&v)
  1694  	zm.doInit(buf)
  1695  	return zm
  1696  }
  1697  
  1698  func MustZMToVector(zm ZM, vec *vector.Vector, m *mpool.MPool) *vector.Vector {
  1699  	var err error
  1700  	if vec, err = ZMToVector(zm, vec, m); err != nil {
  1701  		t := zm.GetType().ToType()
  1702  		t.Scale = zm.GetScale()
  1703  		vector.SetConstNull(vec, 2, m)
  1704  	}
  1705  	return vec
  1706  }
  1707  
  1708  // if zm is not initialized, return a const null vector
  1709  // if zm is of type varlen and truncated, the max value is null
  1710  func ZMToVector(zm ZM, vec *vector.Vector, m *mpool.MPool) (*vector.Vector, error) {
  1711  	var err error
  1712  
  1713  	t := zm.GetType().ToType()
  1714  	t.Scale = zm.GetScale()
  1715  	if vec == nil {
  1716  		vec = vector.NewVec(t)
  1717  	}
  1718  
  1719  	if !zm.IsInited() {
  1720  		vector.SetConstNull(vec, 2, m)
  1721  		return vec, err
  1722  	}
  1723  
  1724  	vec.SetClass(vector.FLAT)
  1725  	vec.SetLength(0)
  1726  	appendFn := vector.MakeAppendBytesFunc(vec)
  1727  	if err = appendFn(zm.GetMinBuf(), false, m); err != nil {
  1728  		return vec, err
  1729  	}
  1730  
  1731  	null := false
  1732  	if t.IsVarlen() && zm.MaxTruncated() {
  1733  		null = true
  1734  	}
  1735  	err = appendFn(zm.GetMaxBuf(), null, m)
  1736  
  1737  	return vec, err
  1738  }
  1739  
  1740  // if zm is not of length 2, return not initilized zm
  1741  func VectorToZM(vec *vector.Vector, zm ZM) ZM {
  1742  	t := vec.GetType()
  1743  	zm.Reset()
  1744  	if vec.Length() != 2 || vec.IsConstNull() || vec.GetNulls().Count() == 2 {
  1745  		return zm
  1746  	}
  1747  	if len(zm) != ZMSize {
  1748  		zm = NewZM(t.Oid, t.Scale)
  1749  	}
  1750  	if t.IsVarlen() {
  1751  		UpdateZM(zm, vec.GetBytesAt(0))
  1752  		nsp := vec.GetNulls()
  1753  		if nsp.Contains(1) {
  1754  			zm.updateMaxString(MaxBytesValue)
  1755  		} else {
  1756  			UpdateZM(zm, vec.GetBytesAt(1))
  1757  		}
  1758  	} else {
  1759  		data := vec.UnsafeGetRawData()
  1760  		if vec.IsConst() {
  1761  			UpdateZM(zm, data)
  1762  		} else {
  1763  			UpdateZM(zm, data[:len(data)/2])
  1764  			UpdateZM(zm, data[len(data)/2:])
  1765  		}
  1766  	}
  1767  	return zm
  1768  }