github.com/matrixorigin/matrixone@v1.2.0/pkg/compare/types.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  	"github.com/matrixorigin/matrixone/pkg/container/nulls"
    19  	"github.com/matrixorigin/matrixone/pkg/container/vector"
    20  	"github.com/matrixorigin/matrixone/pkg/vm/process"
    21  )
    22  
    23  type Compare interface {
    24  	Vector() *vector.Vector
    25  	Set(int, *vector.Vector)
    26  	Compare(int, int, int64, int64) int
    27  	Copy(int, int, int64, int64, *process.Process) error
    28  }
    29  
    30  type compare[T any] struct {
    31  	xs          [][]T
    32  	cmp         func(T, T) int
    33  	ns          []*nulls.Nulls
    34  	vs          []*vector.Vector
    35  	isConstNull []bool
    36  	cpy         func([]T, []T, int64, int64)
    37  	nullsLast   bool
    38  }
    39  
    40  type strCompare struct {
    41  	desc        bool
    42  	nullsLast   bool
    43  	vs          []*vector.Vector
    44  	isConstNull []bool
    45  }
    46  
    47  type arrayCompare struct {
    48  	desc        bool
    49  	nullsLast   bool
    50  	vs          []*vector.Vector
    51  	isConstNull []bool
    52  }
    53  
    54  const nullsCompareFlag = 100
    55  
    56  func nullsCompare(n0, n1 bool, nullsLast bool) int {
    57  	if n0 {
    58  		if n1 {
    59  			return nullsCompareFlag
    60  		} else {
    61  			if nullsLast {
    62  				return nullsCompareFlag + 1
    63  			} else {
    64  				return nullsCompareFlag - 1
    65  			}
    66  		}
    67  	} else {
    68  		if n1 {
    69  			if nullsLast {
    70  				return nullsCompareFlag - 1
    71  			} else {
    72  				return nullsCompareFlag + 1
    73  			}
    74  		} else {
    75  			return 0
    76  		}
    77  	}
    78  }