github.com/matrixorigin/matrixone@v1.2.0/pkg/compare/compare.go (about)

     1  // Copyright 2021 - 2022 Matrix Origin
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //      http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package compare
    16  
    17  import (
    18  	"bytes"
    19  
    20  	"github.com/matrixorigin/matrixone/pkg/container/nulls"
    21  	"github.com/matrixorigin/matrixone/pkg/container/types"
    22  	"github.com/matrixorigin/matrixone/pkg/container/vector"
    23  	"github.com/matrixorigin/matrixone/pkg/vm/process"
    24  )
    25  
    26  func New(typ types.Type, desc, nullsLast bool) Compare {
    27  	switch typ.Oid {
    28  	case types.T_bool:
    29  		if desc {
    30  			return newCompare(boolDescCompare, boolCopy[bool], nullsLast)
    31  		}
    32  		return newCompare(boolAscCompare, boolCopy[bool], nullsLast)
    33  	case types.T_bit:
    34  		if desc {
    35  			return newCompare(genericDescCompare[uint64], genericCopy[uint64], nullsLast)
    36  		}
    37  		return newCompare(genericAscCompare[uint64], genericCopy[uint64], nullsLast)
    38  	case types.T_int8:
    39  		if desc {
    40  			return newCompare(genericDescCompare[int8], genericCopy[int8], nullsLast)
    41  		}
    42  		return newCompare(genericAscCompare[int8], genericCopy[int8], nullsLast)
    43  	case types.T_int16:
    44  		if desc {
    45  			return newCompare(genericDescCompare[int16], genericCopy[int16], nullsLast)
    46  		}
    47  		return newCompare(genericAscCompare[int16], genericCopy[int16], nullsLast)
    48  	case types.T_int32:
    49  		if desc {
    50  			return newCompare(genericDescCompare[int32], genericCopy[int32], nullsLast)
    51  		}
    52  		return newCompare(genericAscCompare[int32], genericCopy[int32], nullsLast)
    53  	case types.T_int64:
    54  		if desc {
    55  			return newCompare(genericDescCompare[int64], genericCopy[int64], nullsLast)
    56  		}
    57  		return newCompare(genericAscCompare[int64], genericCopy[int64], nullsLast)
    58  	case types.T_uint8:
    59  		if desc {
    60  			return newCompare(genericDescCompare[uint8], genericCopy[uint8], nullsLast)
    61  		}
    62  		return newCompare(genericAscCompare[uint8], genericCopy[uint8], nullsLast)
    63  	case types.T_uint16:
    64  		if desc {
    65  			return newCompare(genericDescCompare[uint16], genericCopy[uint16], nullsLast)
    66  		}
    67  		return newCompare(genericAscCompare[uint16], genericCopy[uint16], nullsLast)
    68  	case types.T_uint32:
    69  		if desc {
    70  			return newCompare(genericDescCompare[uint32], genericCopy[uint32], nullsLast)
    71  		}
    72  		return newCompare(genericAscCompare[uint32], genericCopy[uint32], nullsLast)
    73  	case types.T_uint64:
    74  		if desc {
    75  			return newCompare(genericDescCompare[uint64], genericCopy[uint64], nullsLast)
    76  		}
    77  		return newCompare(genericAscCompare[uint64], genericCopy[uint64], nullsLast)
    78  	case types.T_float32:
    79  		if desc {
    80  			return newCompare(genericDescCompare[float32], genericCopy[float32], nullsLast)
    81  		}
    82  		return newCompare(genericAscCompare[float32], genericCopy[float32], nullsLast)
    83  	case types.T_float64:
    84  		if desc {
    85  			return newCompare(genericDescCompare[float64], genericCopy[float64], nullsLast)
    86  		}
    87  		return newCompare(genericAscCompare[float64], genericCopy[float64], nullsLast)
    88  	case types.T_date:
    89  		if desc {
    90  			return newCompare(genericDescCompare[types.Date], genericCopy[types.Date], nullsLast)
    91  		}
    92  		return newCompare(genericAscCompare[types.Date], genericCopy[types.Date], nullsLast)
    93  	case types.T_datetime:
    94  		if desc {
    95  			return newCompare(genericDescCompare[types.Datetime], genericCopy[types.Datetime], nullsLast)
    96  		}
    97  		return newCompare(genericAscCompare[types.Datetime], genericCopy[types.Datetime], nullsLast)
    98  	case types.T_time:
    99  		if desc {
   100  			return newCompare(genericDescCompare[types.Time], genericCopy[types.Time], nullsLast)
   101  		}
   102  		return newCompare(genericAscCompare[types.Time], genericCopy[types.Time], nullsLast)
   103  	case types.T_timestamp:
   104  		if desc {
   105  			return newCompare(genericDescCompare[types.Timestamp], genericCopy[types.Timestamp], nullsLast)
   106  		}
   107  		return newCompare(genericAscCompare[types.Timestamp], genericCopy[types.Timestamp], nullsLast)
   108  	case types.T_decimal64:
   109  		if desc {
   110  			return newCompare(decimal64DescCompare, decimal64Copy, nullsLast)
   111  		}
   112  		return newCompare(decimal64AscCompare, decimal64Copy, nullsLast)
   113  	case types.T_decimal128:
   114  		if desc {
   115  			return newCompare(decimal128DescCompare, decimal128Copy, nullsLast)
   116  		}
   117  		return newCompare(decimal128AscCompare, decimal128Copy, nullsLast)
   118  	case types.T_TS:
   119  		if desc {
   120  			return newCompare(txntsDescCompare, txntsCopy, nullsLast)
   121  		}
   122  		return newCompare(txntsAscCompare, txntsCopy, nullsLast)
   123  	case types.T_Rowid:
   124  		if desc {
   125  			return newCompare(rowidDescCompare, rowidCopy, nullsLast)
   126  		}
   127  		return newCompare(rowidAscCompare, rowidCopy, nullsLast)
   128  	case types.T_Blockid:
   129  		if desc {
   130  			return newCompare(blockidDescCompare, blockidCopy, nullsLast)
   131  		}
   132  		return newCompare(blockidAscCompare, blockidCopy, nullsLast)
   133  	case types.T_uuid:
   134  		if desc {
   135  			return newCompare(uuidDescCompare, uuidCopy, nullsLast)
   136  		}
   137  		return newCompare(uuidAscCompare, uuidCopy, nullsLast)
   138  	case types.T_enum:
   139  		if desc {
   140  			return newCompare(genericDescCompare[types.Enum], genericCopy[types.Enum], nullsLast)
   141  		}
   142  		return newCompare(genericAscCompare[types.Enum], genericCopy[types.Enum], nullsLast)
   143  	case types.T_char, types.T_varchar, types.T_blob,
   144  		types.T_binary, types.T_varbinary, types.T_json, types.T_text:
   145  		return &strCompare{
   146  			desc:        desc,
   147  			nullsLast:   nullsLast,
   148  			vs:          make([]*vector.Vector, 2),
   149  			isConstNull: make([]bool, 2),
   150  		}
   151  	case types.T_array_float32, types.T_array_float64:
   152  		//NOTE: Used by merge_order, merge_top, top agg operators.
   153  		return &arrayCompare{
   154  			desc:        desc,
   155  			nullsLast:   nullsLast,
   156  			vs:          make([]*vector.Vector, 2),
   157  			isConstNull: make([]bool, 2),
   158  		}
   159  	}
   160  	return nil
   161  }
   162  
   163  func boolAscCompare(x, y bool) int {
   164  	if x == y {
   165  		return 0
   166  	}
   167  	if !x && y {
   168  		return -1
   169  	}
   170  	return 1
   171  }
   172  
   173  func decimal64AscCompare(x, y types.Decimal64) int {
   174  	return x.Compare(y)
   175  }
   176  func decimal128AscCompare(x, y types.Decimal128) int {
   177  	return x.Compare(y)
   178  }
   179  
   180  func uuidAscCompare(x, y types.Uuid) int {
   181  	return x.Compare(y)
   182  }
   183  
   184  func txntsAscCompare(x, y types.TS) int {
   185  	return bytes.Compare(x[:], y[:])
   186  }
   187  func rowidAscCompare(x, y types.Rowid) int {
   188  	return bytes.Compare(x[:], y[:])
   189  }
   190  
   191  func blockidAscCompare(x, y types.Blockid) int {
   192  	return bytes.Compare(x[:], y[:])
   193  }
   194  
   195  func genericAscCompare[T types.OrderedT](x, y T) int {
   196  	if x == y {
   197  		return 0
   198  	}
   199  	if x < y {
   200  		return -1
   201  	}
   202  	return 1
   203  }
   204  
   205  func boolDescCompare(x, y bool) int {
   206  	if x == y {
   207  		return 0
   208  	}
   209  	if !x && y {
   210  		return 1
   211  	}
   212  	return -1
   213  }
   214  
   215  func decimal64DescCompare(x, y types.Decimal64) int {
   216  	return -x.Compare(y)
   217  }
   218  func decimal128DescCompare(x, y types.Decimal128) int {
   219  	return -x.Compare(y)
   220  }
   221  
   222  func uuidDescCompare(x, y types.Uuid) int {
   223  	return -x.Compare(y)
   224  }
   225  
   226  func txntsDescCompare(x, y types.TS) int {
   227  	return bytes.Compare(y[:], x[:])
   228  }
   229  func rowidDescCompare(x, y types.Rowid) int {
   230  	return bytes.Compare(y[:], x[:])
   231  }
   232  
   233  func blockidDescCompare(x, y types.Blockid) int {
   234  	return bytes.Compare(y[:], x[:])
   235  }
   236  
   237  func genericDescCompare[T types.OrderedT](x, y T) int {
   238  	if x == y {
   239  		return 0
   240  	}
   241  	if x < y {
   242  		return 1
   243  	}
   244  	return -1
   245  }
   246  
   247  func boolCopy[T bool](vecDst, vecSrc []T, dst, src int64) {
   248  	vecDst[dst] = vecSrc[src]
   249  }
   250  
   251  func decimal64Copy(vecDst, vecSrc []types.Decimal64, dst, src int64) {
   252  	vecDst[dst] = vecSrc[src]
   253  }
   254  
   255  func decimal128Copy(vecDst, vecSrc []types.Decimal128, dst, src int64) {
   256  	vecDst[dst] = vecSrc[src]
   257  }
   258  
   259  func uuidCopy(vecDst, vecSrc []types.Uuid, dst, src int64) {
   260  	vecDst[dst] = vecSrc[src]
   261  }
   262  
   263  func txntsCopy(vecDst, vecSrc []types.TS, dst, src int64) {
   264  	vecDst[dst] = vecSrc[src]
   265  }
   266  func rowidCopy(vecDst, vecSrc []types.Rowid, dst, src int64) {
   267  	vecDst[dst] = vecSrc[src]
   268  }
   269  
   270  func blockidCopy(vecDst, vecSrc []types.Blockid, dst, src int64) {
   271  	vecDst[dst] = vecSrc[src]
   272  }
   273  
   274  func genericCopy[T types.OrderedT](vecDst, vecSrc []T, dst, src int64) {
   275  	vecDst[dst] = vecSrc[src]
   276  }
   277  
   278  func newCompare[T any](cmp func(T, T) int, cpy func([]T, []T, int64, int64), nullsLast bool) *compare[T] {
   279  	return &compare[T]{
   280  		cmp:         cmp,
   281  		cpy:         cpy,
   282  		xs:          make([][]T, 2),
   283  		ns:          make([]*nulls.Nulls, 2),
   284  		vs:          make([]*vector.Vector, 2),
   285  		isConstNull: make([]bool, 2),
   286  		nullsLast:   nullsLast,
   287  	}
   288  }
   289  
   290  func (c *compare[T]) Vector() *vector.Vector {
   291  	return c.vs[0]
   292  }
   293  
   294  func (c *compare[T]) Set(idx int, vec *vector.Vector) {
   295  	c.vs[idx] = vec
   296  	c.ns[idx] = vec.GetNulls()
   297  	c.xs[idx] = vector.ExpandFixedCol[T](vec)
   298  	c.isConstNull[idx] = vec.IsConstNull()
   299  }
   300  
   301  func (c *compare[T]) Compare(veci, vecj int, vi, vj int64) int {
   302  	n0 := c.isConstNull[veci] || c.ns[veci].Contains(uint64(vi))
   303  	n1 := c.isConstNull[vecj] || c.ns[vecj].Contains(uint64(vj))
   304  	cmp := nullsCompare(n0, n1, c.nullsLast)
   305  	if cmp != 0 {
   306  		return cmp - nullsCompareFlag
   307  	}
   308  	return c.cmp(c.xs[veci][vi], c.xs[vecj][vj])
   309  }
   310  
   311  func (c *compare[T]) Copy(vecSrc, vecDst int, src, dst int64, _ *process.Process) error {
   312  	if c.isConstNull[vecSrc] || c.ns[vecSrc].Contains(uint64(src)) {
   313  		nulls.Add(c.ns[vecDst], uint64(dst))
   314  	} else {
   315  		nulls.Del(c.ns[vecDst], uint64(dst))
   316  		c.cpy(c.xs[vecDst], c.xs[vecSrc], dst, src)
   317  	}
   318  	return nil
   319  }