github.com/matrixorigin/matrixone@v1.2.0/pkg/compare/strcompare.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/vector"
    22  	"github.com/matrixorigin/matrixone/pkg/vm/process"
    23  )
    24  
    25  func (c *strCompare) Vector() *vector.Vector {
    26  	return c.vs[0]
    27  }
    28  
    29  func (c *strCompare) Set(idx int, v *vector.Vector) {
    30  	c.vs[idx] = v
    31  	c.isConstNull[idx] = v.IsConstNull()
    32  }
    33  
    34  func (c *strCompare) Copy(vecSrc, vecDst int, src, dst int64, proc *process.Process) error {
    35  	if c.isConstNull[vecSrc] || c.vs[vecSrc].GetNulls().Contains(uint64(src)) {
    36  		nulls.Add(c.vs[vecDst].GetNulls(), uint64(dst))
    37  		return nil
    38  	} else {
    39  		nulls.Del(c.vs[vecDst].GetNulls(), uint64(dst))
    40  		return c.vs[vecDst].Copy(c.vs[vecSrc], dst, src, proc.Mp())
    41  	}
    42  }
    43  
    44  func (c *strCompare) Compare(veci, vecj int, vi, vj int64) int {
    45  	n0 := c.isConstNull[veci] || c.vs[veci].GetNulls().Contains(uint64(vi))
    46  	n1 := c.isConstNull[vecj] || c.vs[vecj].GetNulls().Contains(uint64(vj))
    47  	cmp := nullsCompare(n0, n1, c.nullsLast)
    48  	if cmp != 0 {
    49  		return cmp - nullsCompareFlag
    50  	}
    51  	x := c.vs[veci].GetBytesAt(int(vi))
    52  	y := c.vs[vecj].GetBytesAt(int(vj))
    53  	if c.desc {
    54  		return bytes.Compare(y, x)
    55  	}
    56  	return bytes.Compare(x, y)
    57  }