github.com/matrixorigin/matrixone@v1.2.0/pkg/vm/engine/tae/compute/compare.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 compute
    16  
    17  import (
    18  	"bytes"
    19  	"fmt"
    20  	"unsafe"
    21  
    22  	"github.com/matrixorigin/matrixone/pkg/container/types"
    23  	"github.com/matrixorigin/matrixone/pkg/vectorize/moarray"
    24  )
    25  
    26  func CompareOrdered[T types.OrderedT](a, b T) int {
    27  	if a > b {
    28  		return 1
    29  	} else if a < b {
    30  		return -1
    31  	}
    32  	return 0
    33  }
    34  
    35  func CompareBool(a, b bool) int {
    36  	if a == b {
    37  		return 0
    38  	} else if a {
    39  		return 1
    40  	}
    41  	return -1
    42  }
    43  
    44  func CompareBytes(a, b []byte) int {
    45  	res := bytes.Compare(a, b)
    46  	if res > 0 {
    47  		return 1
    48  	} else if res < 0 {
    49  		return -1
    50  	} else {
    51  		return 0
    52  	}
    53  }
    54  
    55  func compareArrayFromBytes[T types.RealNumbers](a, b []byte) int {
    56  	return moarray.Compare[T](types.BytesToArray[T](a), types.BytesToArray[T](b))
    57  }
    58  
    59  func Compare(a, b []byte, t types.T, scale1, scale2 int32) int {
    60  	switch t {
    61  	case types.T_bool:
    62  		return CompareBool(types.DecodeBool(a), types.DecodeBool(b))
    63  	case types.T_bit:
    64  		return CompareOrdered(types.DecodeUint64(a), types.DecodeUint64(b))
    65  	case types.T_int8:
    66  		return CompareOrdered(types.DecodeInt8(a), types.DecodeInt8(b))
    67  	case types.T_int16:
    68  		return CompareOrdered(types.DecodeInt16(a), types.DecodeInt16(b))
    69  	case types.T_int32:
    70  		return CompareOrdered(types.DecodeInt32(a), types.DecodeInt32(b))
    71  	case types.T_int64:
    72  		return CompareOrdered(types.DecodeInt64(a), types.DecodeInt64(b))
    73  	case types.T_uint8:
    74  		return CompareOrdered(types.DecodeUint8(a), types.DecodeUint8(b))
    75  	case types.T_uint16:
    76  		return CompareOrdered(types.DecodeUint16(a), types.DecodeUint16(b))
    77  	case types.T_uint32:
    78  		return CompareOrdered(types.DecodeUint32(a), types.DecodeUint32(b))
    79  	case types.T_uint64:
    80  		return CompareOrdered(types.DecodeUint64(a), types.DecodeUint64(b))
    81  	case types.T_decimal64:
    82  		return types.CompareDecimal64WithScale(types.DecodeDecimal64(a), types.DecodeDecimal64(b), scale1, scale2)
    83  	case types.T_decimal128:
    84  		return types.CompareDecimal128WithScale(types.DecodeDecimal128(a), types.DecodeDecimal128(b), scale1, scale2)
    85  	case types.T_decimal256:
    86  		return types.CompareDecimal256(*(*types.Decimal256)(unsafe.Pointer(&a[0])), *(*types.Decimal256)(unsafe.Pointer(&b[0])))
    87  	case types.T_float32:
    88  		return CompareOrdered(types.DecodeFloat32(a), types.DecodeFloat32(b))
    89  	case types.T_float64:
    90  		return CompareOrdered(types.DecodeFloat64(a), types.DecodeFloat64(b))
    91  	case types.T_timestamp:
    92  		return CompareOrdered(types.DecodeTimestamp(a), types.DecodeTimestamp(b))
    93  	case types.T_date:
    94  		return CompareOrdered(types.DecodeDate(a), types.DecodeDate(b))
    95  	case types.T_time:
    96  		return CompareOrdered(types.DecodeTime(a), types.DecodeTime(b))
    97  	case types.T_datetime:
    98  		return CompareOrdered(types.DecodeDatetime(a), types.DecodeDatetime(b))
    99  	case types.T_enum:
   100  		return CompareOrdered(types.DecodeEnum(a), types.DecodeEnum(b))
   101  	case types.T_TS:
   102  		return CompareBytes(a, b)
   103  	case types.T_Rowid:
   104  		return CompareBytes(a, b)
   105  	case types.T_uuid:
   106  		return types.CompareUuid(types.DecodeUuid(a), types.DecodeUuid(b))
   107  	case types.T_char, types.T_varchar, types.T_blob,
   108  		types.T_binary, types.T_varbinary, types.T_json, types.T_text:
   109  		return CompareBytes(a, b)
   110  	case types.T_array_float32:
   111  		//Zone map comparator
   112  		return compareArrayFromBytes[float32](a, b)
   113  	case types.T_array_float64:
   114  		return compareArrayFromBytes[float64](a, b)
   115  	case types.T_any:
   116  		return 0
   117  	default:
   118  		panic(fmt.Sprintf("unsupported type: %v", t))
   119  	}
   120  }
   121  
   122  func CompareGeneric(a, b any, t types.T) int {
   123  	switch t {
   124  	case types.T_bool:
   125  		return CompareBool(a.(bool), b.(bool))
   126  	case types.T_bit:
   127  		return CompareOrdered(a.(uint64), b.(uint64))
   128  	case types.T_int8:
   129  		return CompareOrdered(a.(int8), b.(int8))
   130  	case types.T_int16:
   131  		return CompareOrdered(a.(int16), b.(int16))
   132  	case types.T_int32:
   133  		return CompareOrdered(a.(int32), b.(int32))
   134  	case types.T_int64:
   135  		return CompareOrdered(a.(int64), b.(int64))
   136  	case types.T_uint8:
   137  		return CompareOrdered(a.(uint8), b.(uint8))
   138  	case types.T_uint16:
   139  		return CompareOrdered(a.(uint16), b.(uint16))
   140  	case types.T_uint32:
   141  		return CompareOrdered(a.(uint32), b.(uint32))
   142  	case types.T_uint64:
   143  		return CompareOrdered(a.(uint64), b.(uint64))
   144  	case types.T_decimal64:
   145  		return a.(types.Decimal64).Compare(b.(types.Decimal64))
   146  	case types.T_decimal128:
   147  		return a.(types.Decimal128).Compare(b.(types.Decimal128))
   148  	case types.T_decimal256:
   149  		return a.(types.Decimal256).Compare(b.(types.Decimal256))
   150  	case types.T_float32:
   151  		return CompareOrdered(a.(float32), b.(float32))
   152  	case types.T_float64:
   153  		return CompareOrdered(a.(float64), b.(float64))
   154  	case types.T_timestamp:
   155  		return CompareOrdered(a.(types.Timestamp), b.(types.Timestamp))
   156  	case types.T_date:
   157  		return CompareOrdered(a.(types.Date), b.(types.Date))
   158  	case types.T_time:
   159  		return CompareOrdered(a.(types.Time), b.(types.Time))
   160  	case types.T_datetime:
   161  		return CompareOrdered(a.(types.Datetime), b.(types.Datetime))
   162  	case types.T_enum:
   163  		return CompareOrdered(a.(types.Enum), b.(types.Enum))
   164  	case types.T_TS:
   165  		ts1 := b.(types.TS)
   166  		ts2 := a.(types.TS)
   167  		return ts2.Compare(&ts1)
   168  	case types.T_Rowid:
   169  		return CompareBytes(a.([]byte), b.([]byte))
   170  	case types.T_Blockid:
   171  		return CompareBytes(a.([]byte), b.([]byte))
   172  	case types.T_uuid:
   173  		return types.CompareUuid(a.(types.Uuid), b.(types.Uuid))
   174  	case types.T_char, types.T_varchar, types.T_blob,
   175  		types.T_binary, types.T_varbinary, types.T_json, types.T_text:
   176  		return CompareBytes(a.([]byte), b.([]byte))
   177  	case types.T_array_float32:
   178  		// Used by GetRowByFilter in TAE.
   179  		return compareArrayFromBytes[float32](a.([]byte), b.([]byte))
   180  	case types.T_array_float64:
   181  		return compareArrayFromBytes[float64](a.([]byte), b.([]byte))
   182  	case types.T_any:
   183  		return 0
   184  	default:
   185  		panic(fmt.Sprintf("unsupported type: %s", t.String()))
   186  	}
   187  }