github.com/matrixorigin/matrixone@v0.7.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  
    20  	"github.com/matrixorigin/matrixone/pkg/container/types"
    21  )
    22  
    23  func CompareOrdered[T types.OrderedT](v1, v2 any) int64 {
    24  	a, b := v1.(T), v2.(T)
    25  	if a > b {
    26  		return 1
    27  	} else if a < b {
    28  		return -1
    29  	}
    30  	return 0
    31  }
    32  
    33  func CompareBool(a, b bool) int64 {
    34  	if a && b {
    35  		return 0
    36  	} else if !a && !b {
    37  		return 0
    38  	} else if a {
    39  		return 1
    40  	}
    41  	return 0
    42  }
    43  
    44  func CompareBytes(a, b any) int64 {
    45  	res := bytes.Compare(a.([]byte), b.([]byte))
    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 CompareGeneric(a, b any, t types.Type) int64 {
    56  	switch t.Oid {
    57  	case types.T_bool:
    58  		return CompareBool(a.(bool), b.(bool))
    59  	case types.T_int8:
    60  		return CompareOrdered[int8](a, b)
    61  	case types.T_int16:
    62  		return CompareOrdered[int16](a, b)
    63  	case types.T_int32:
    64  		return CompareOrdered[int32](a, b)
    65  	case types.T_int64:
    66  		return CompareOrdered[int64](a, b)
    67  	case types.T_uint8:
    68  		return CompareOrdered[uint8](a, b)
    69  	case types.T_uint16:
    70  		return CompareOrdered[uint16](a, b)
    71  	case types.T_uint32:
    72  		return CompareOrdered[uint32](a, b)
    73  	case types.T_uint64:
    74  		return CompareOrdered[uint64](a, b)
    75  	case types.T_decimal64:
    76  		return types.CompareDecimal64Decimal64Aligned(a.(types.Decimal64), b.(types.Decimal64))
    77  	case types.T_decimal128:
    78  		return types.CompareDecimal128Decimal128Aligned(a.(types.Decimal128), b.(types.Decimal128))
    79  	case types.T_float32:
    80  		return CompareOrdered[float32](a, b)
    81  	case types.T_float64:
    82  		return CompareOrdered[float64](a, b)
    83  	case types.T_timestamp:
    84  		return CompareOrdered[types.Timestamp](a, b)
    85  	case types.T_date:
    86  		return CompareOrdered[types.Date](a, b)
    87  	case types.T_time:
    88  		return CompareOrdered[types.Time](a, b)
    89  	case types.T_datetime:
    90  		return CompareOrdered[types.Datetime](a, b)
    91  	case types.T_uuid:
    92  		return types.CompareUuid(a.(types.Uuid), b.(types.Uuid))
    93  	case types.T_char, types.T_varchar, types.T_blob, types.T_json, types.T_text:
    94  		return CompareBytes(a, b)
    95  	default:
    96  		panic("unsupported type")
    97  	}
    98  }