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