github.com/matrixorigin/matrixone@v1.2.0/pkg/testutil/util_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 testutil
    16  
    17  import (
    18  	"bytes"
    19  	"reflect"
    20  
    21  	"github.com/matrixorigin/matrixone/pkg/container/nulls"
    22  	"github.com/matrixorigin/matrixone/pkg/container/vector"
    23  )
    24  
    25  func CompareVectors(expected *vector.Vector, got *vector.Vector) bool {
    26  	if expected.IsConst() {
    27  		if !got.IsConst() {
    28  			return false
    29  		}
    30  		if expected.IsConstNull() {
    31  			return got.IsConstNull()
    32  		} else {
    33  			if expected.GetType().IsVarlen() {
    34  				v1 := vector.MustStrCol(expected)
    35  				v2 := vector.MustStrCol(got)
    36  				return reflect.DeepEqual(v1[0], v2[0])
    37  			} else {
    38  				return bytes.Equal(expected.UnsafeGetRawData(), got.UnsafeGetRawData())
    39  			}
    40  		}
    41  	} else {
    42  		if got.IsConst() {
    43  			return false
    44  		}
    45  		// expected length and got length
    46  		expectedLength := expected.Length()
    47  		gotLength := got.Length()
    48  		if expectedLength != gotLength {
    49  			return false
    50  		}
    51  		if nulls.Any(expected.GetNulls()) {
    52  			k := uint64(0)
    53  			if !nulls.Any(got.GetNulls()) {
    54  				return false
    55  			}
    56  			for k = 0; k < uint64(expectedLength); k++ {
    57  				c1 := nulls.Contains(expected.GetNulls(), k)
    58  				c2 := nulls.Contains(got.GetNulls(), k)
    59  				if c1 != c2 {
    60  					return false
    61  				}
    62  			}
    63  		} else if nulls.Any(got.GetNulls()) {
    64  			return false
    65  		}
    66  
    67  		if expected.GetType().IsVarlen() {
    68  			v1 := vector.MustStrCol(expected)
    69  			v2 := vector.MustStrCol(got)
    70  			for i, v := range v1 {
    71  				if nulls.Contains(expected.GetNulls(), uint64(i)) {
    72  					if !nulls.Contains(got.GetNulls(), uint64(i)) {
    73  						return false
    74  					}
    75  				} else {
    76  					if nulls.Contains(got.GetNulls(), uint64(i)) {
    77  						return false
    78  					}
    79  					vv := v2[i]
    80  					if v != vv {
    81  						return false
    82  					}
    83  				}
    84  			}
    85  			return true
    86  		} else {
    87  			return bytes.Equal(expected.UnsafeGetRawData(), got.UnsafeGetRawData())
    88  		}
    89  	}
    90  }