github.com/matrixorigin/matrixone@v0.7.0/pkg/vectorize/sub/sub_test.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 sub
    16  
    17  import (
    18  	"math"
    19  	"testing"
    20  
    21  	"github.com/matrixorigin/matrixone/pkg/container/types"
    22  	"github.com/matrixorigin/matrixone/pkg/container/vector"
    23  	"github.com/matrixorigin/matrixone/pkg/testutil"
    24  	"github.com/matrixorigin/matrixone/pkg/vm/process"
    25  	"github.com/stretchr/testify/require"
    26  )
    27  
    28  func TestI32SubOf(t *testing.T) {
    29  	as := make([]int32, 2)
    30  	bs := make([]int32, 2)
    31  	for i := 0; i < 2; i++ {
    32  		as[i] = math.MaxInt32
    33  		bs[i] = int32(-i)
    34  	}
    35  	cs := make([]int32, 2)
    36  	av := testutil.MakeInt32Vector(as, nil)
    37  	bv := testutil.MakeInt32Vector(bs, nil)
    38  	cv := testutil.MakeInt32Vector(cs, nil)
    39  
    40  	err := NumericSubSigned[int32](av, bv, cv)
    41  	if err == nil {
    42  		t.Fatalf("should have overflowed.")
    43  	}
    44  }
    45  
    46  func TestU32SubOf(t *testing.T) {
    47  	as := make([]uint32, 2)
    48  	bs := make([]uint32, 2)
    49  	for i := 0; i < 2; i++ {
    50  		as[i] = uint32(i)
    51  		bs[i] = math.MaxUint32
    52  	}
    53  	cs := make([]uint32, 2)
    54  	av := testutil.MakeUint32Vector(as, nil)
    55  	bv := testutil.MakeUint32Vector(bs, nil)
    56  	cv := testutil.MakeUint32Vector(cs, nil)
    57  
    58  	err := NumericSubUnsigned[uint32](av, bv, cv)
    59  	if err == nil {
    60  		t.Fatalf("should have overflowed.")
    61  	}
    62  }
    63  
    64  func TestF32Sub(t *testing.T) {
    65  	as := make([]float32, 2)
    66  	bs := make([]float32, 2)
    67  	for i := 0; i < 2; i++ {
    68  		as[i] = 1
    69  		bs[i] = 0.5
    70  	}
    71  	cs := make([]float32, 2)
    72  	av := testutil.MakeFloat32Vector(as, nil)
    73  	bv := testutil.MakeFloat32Vector(bs, nil)
    74  	cv := testutil.MakeFloat32Vector(cs, nil)
    75  
    76  	err := NumericSubFloat[float32](av, bv, cv)
    77  	if err != nil {
    78  		t.Fatalf("should not error.")
    79  	}
    80  
    81  	res := vector.MustTCols[float32](cv)
    82  	for i := 0; i < 2; i++ {
    83  		//fmt.Printf("%+v - %+v \n", as[i], bs[i])
    84  		//fmt.Printf("actual res:%+v\n", res[i])
    85  		//fmt.Printf("expect res:%+v\n", as[i]-bs[i])
    86  		if res[i] != as[i]-bs[i] {
    87  			t.Fatalf("float32 sub wrong result")
    88  		}
    89  	}
    90  }
    91  
    92  func TestDec64Sub(t *testing.T) {
    93  	as := make([]int64, 10)
    94  	bs := make([]int64, 10)
    95  	cs := make([]int64, 10)
    96  	for i := 0; i < 10; i++ {
    97  		as[i] = int64(i)
    98  		bs[i] = int64(3 * i)
    99  	}
   100  
   101  	av := testutil.MakeDecimal64Vector(as, nil, types.T_decimal64.ToType())
   102  	bv := testutil.MakeDecimal64Vector(bs, nil, types.T_decimal64.ToType())
   103  	cv := testutil.MakeDecimal64Vector(cs, nil, types.T_decimal64.ToType())
   104  
   105  	err := Decimal64VecSub(av, bv, cv)
   106  	if err != nil {
   107  		t.Fatalf("decimal64 sub failed")
   108  	}
   109  
   110  	res := vector.MustTCols[types.Decimal64](cv)
   111  	for i := 0; i < 10; i++ {
   112  		d, _ := types.Decimal64_FromInt64(as[i]-bs[i], 64, 0)
   113  		if !res[i].Eq(d) {
   114  			t.Fatalf("decimal64 sub wrong result")
   115  		}
   116  	}
   117  }
   118  
   119  func TestDec128Sub(t *testing.T) {
   120  	as := make([]int64, 10)
   121  	bs := make([]int64, 10)
   122  	cs := make([]int64, 10)
   123  	for i := 0; i < 10; i++ {
   124  		as[i] = int64(i)
   125  		bs[i] = int64(3 * i)
   126  	}
   127  
   128  	av := testutil.MakeDecimal128Vector(as, nil, types.T_decimal128.ToType())
   129  	bv := testutil.MakeDecimal128Vector(bs, nil, types.T_decimal128.ToType())
   130  	cv := testutil.MakeDecimal128Vector(cs, nil, types.T_decimal128.ToType())
   131  
   132  	err := Decimal128VecSub(av, bv, cv)
   133  	if err != nil {
   134  		t.Fatalf("decimal128 sub failed")
   135  	}
   136  
   137  	res := vector.MustTCols[types.Decimal128](cv)
   138  	for i := 0; i < 10; i++ {
   139  		d, _ := types.Decimal128_FromInt64(as[i]-bs[i], 64, 0)
   140  		if !res[i].Eq(d) {
   141  			t.Fatalf("decimal128 sub wrong result")
   142  		}
   143  	}
   144  }
   145  
   146  func TestDec64SubOfOppNumber(t *testing.T) {
   147  	as := make([]int64, 10)
   148  	bs := make([]int64, 10)
   149  	cs := make([]int64, 10)
   150  	for i := 0; i < 10; i++ {
   151  		as[i] = int64(-i)
   152  		bs[i] = int64(i)
   153  	}
   154  
   155  	av := testutil.MakeDecimal64Vector(as, nil, types.T_decimal64.ToType())
   156  	bv := testutil.MakeDecimal64Vector(bs, nil, types.T_decimal64.ToType())
   157  	cv := testutil.MakeDecimal64Vector(cs, nil, types.T_decimal64.ToType())
   158  
   159  	err := Decimal64VecSub(av, bv, cv)
   160  	if err != nil {
   161  		t.Fatalf("decimal64 add failed")
   162  	}
   163  
   164  	res := vector.MustTCols[types.Decimal64](cv)
   165  	for i := 0; i < 10; i++ {
   166  		d, _ := types.Decimal64_FromInt64(as[i]-bs[i], 64, 0)
   167  		if !res[i].Eq(d) {
   168  			t.Fatalf("decimal64 sub wrong result")
   169  		}
   170  	}
   171  }
   172  
   173  func TestDec128SubOfOppNumber(t *testing.T) {
   174  	as := make([]int64, 10)
   175  	bs := make([]int64, 10)
   176  	cs := make([]int64, 10)
   177  	for i := 0; i < 10; i++ {
   178  		as[i] = int64(i)
   179  		bs[i] = int64(-i)
   180  	}
   181  
   182  	av := testutil.MakeDecimal128Vector(as, nil, types.T_decimal128.ToType())
   183  	bv := testutil.MakeDecimal128Vector(bs, nil, types.T_decimal128.ToType())
   184  	cv := testutil.MakeDecimal128Vector(cs, nil, types.T_decimal128.ToType())
   185  
   186  	err := Decimal128VecSub(av, bv, cv)
   187  	if err != nil {
   188  		t.Fatalf("decimal128 sub failed")
   189  	}
   190  
   191  	res := vector.MustTCols[types.Decimal128](cv)
   192  	for i := 0; i < 10; i++ {
   193  		d, _ := types.Decimal128_FromInt64(as[i]-bs[i], 64, 0)
   194  		if !res[i].Eq(d) {
   195  			t.Fatalf("decimal128 sub wrong result")
   196  		}
   197  	}
   198  }
   199  
   200  func TestDec128SubByFloat64(t *testing.T) {
   201  	cases := []struct {
   202  		name string
   203  		a    float64
   204  		b    float64
   205  		want float64
   206  	}{
   207  		{
   208  			name: "test01",
   209  			a:    1,
   210  			b:    0.5,
   211  			want: 0.5,
   212  		},
   213  		{
   214  			name: "test02",
   215  			a:    1,
   216  			b:    0.4,
   217  			want: 0.6,
   218  		},
   219  		{
   220  			name: "test03",
   221  			a:    0,
   222  			b:    0,
   223  			want: 0,
   224  		},
   225  		{
   226  			name: "test04",
   227  			a:    0,
   228  			b:    0.5,
   229  			want: -0.5,
   230  		},
   231  		{
   232  			name: "test05",
   233  			a:    1,
   234  			b:    0,
   235  			want: 1,
   236  		},
   237  	}
   238  
   239  	for _, c := range cases {
   240  		t.Run(c.name, func(t *testing.T) {
   241  			av := testutil.MakeScalarDecimal128ByFloat64(c.a, 1, types.T_decimal128.ToType())
   242  			bv := testutil.MakeScalarDecimal128ByFloat64(c.b, 1, types.T_decimal128.ToType())
   243  			cv := testutil.MakeScalarDecimal128ByFloat64(0, 1, types.T_decimal128.ToType())
   244  			err := Decimal128VecSub(av, bv, cv)
   245  			if err != nil {
   246  				t.Fatalf("decimal128 sub failed")
   247  			}
   248  
   249  			res := vector.MustTCols[types.Decimal128](cv)
   250  			d, _ := types.Decimal128_FromFloat64(c.want, 64, 4)
   251  			if !res[0].Eq(d) {
   252  				t.Fatalf("decimal128 sub wrong result")
   253  			}
   254  		})
   255  	}
   256  }
   257  
   258  func BenchmarkSubI32(b *testing.B) {
   259  	as := make([]int32, 8192)
   260  	bs := make([]int32, 8192)
   261  	for i := 0; i < 8192; i++ {
   262  		as[i] = int32(i)
   263  		bs[i] = 1
   264  	}
   265  	cs := make([]int32, 8192)
   266  
   267  	av := testutil.MakeInt32Vector(as, nil)
   268  	bv := testutil.MakeInt32Vector(bs, nil)
   269  	cv := testutil.MakeInt32Vector(cs, nil)
   270  
   271  	for i := 0; i < b.N; i++ {
   272  		if err := goNumericSubSigned[int32](av, bv, cv); err != nil {
   273  			b.Fail()
   274  		}
   275  	}
   276  }
   277  
   278  func BenchmarkSubI32_C(b *testing.B) {
   279  	as := make([]int32, 8192)
   280  	bs := make([]int32, 8192)
   281  	for i := 0; i < 8192; i++ {
   282  		as[i] = int32(i)
   283  		bs[i] = 1
   284  	}
   285  
   286  	cs := make([]int32, 8192)
   287  
   288  	av := testutil.MakeInt32Vector(as, nil)
   289  	bv := testutil.MakeInt32Vector(bs, nil)
   290  	cv := testutil.MakeInt32Vector(cs, nil)
   291  
   292  	for i := 0; i < b.N; i++ {
   293  		if err := NumericSubSigned[int32](av, bv, cv); err != nil {
   294  			b.Fail()
   295  		}
   296  	}
   297  }
   298  
   299  func BenchmarkSubUI32(b *testing.B) {
   300  	as := make([]uint32, 8192)
   301  	bs := make([]uint32, 8192)
   302  	for i := 0; i < 8192; i++ {
   303  		as[i] = uint32(i)
   304  		bs[i] = 0
   305  	}
   306  
   307  	cs := make([]uint32, 8192)
   308  
   309  	av := testutil.MakeUint32Vector(as, nil)
   310  	bv := testutil.MakeUint32Vector(bs, nil)
   311  	cv := testutil.MakeUint32Vector(cs, nil)
   312  
   313  	for i := 0; i < b.N; i++ {
   314  		if err := goNumericSubUnsigned[uint32](av, bv, cv); err != nil {
   315  			b.Fail()
   316  		}
   317  	}
   318  }
   319  
   320  func BenchmarkSubUI32_C(b *testing.B) {
   321  	as := make([]uint32, 8192)
   322  	bs := make([]uint32, 8192)
   323  	for i := 0; i < 8192; i++ {
   324  		as[i] = uint32(i)
   325  		bs[i] = 0
   326  	}
   327  
   328  	cs := make([]uint32, 8192)
   329  
   330  	av := testutil.MakeUint32Vector(as, nil)
   331  	bv := testutil.MakeUint32Vector(bs, nil)
   332  	cv := testutil.MakeUint32Vector(cs, nil)
   333  
   334  	for i := 0; i < b.N; i++ {
   335  		if err := NumericSubUnsigned[uint32](av, bv, cv); err != nil {
   336  			b.Fail()
   337  		}
   338  	}
   339  }
   340  
   341  func BenchmarkSubF64(b *testing.B) {
   342  	as := make([]float64, 8192)
   343  	bs := make([]float64, 8192)
   344  	for i := 0; i < 8192; i++ {
   345  		as[i] = float64(i)
   346  		bs[i] = 1
   347  	}
   348  
   349  	cs := make([]float64, 8192)
   350  
   351  	av := testutil.MakeFloat64Vector(as, nil)
   352  	bv := testutil.MakeFloat64Vector(bs, nil)
   353  	cv := testutil.MakeFloat64Vector(cs, nil)
   354  
   355  	for i := 0; i < b.N; i++ {
   356  		if err := goNumericSubFloat[float64](av, bv, cv); err != nil {
   357  			b.Fail()
   358  		}
   359  	}
   360  }
   361  
   362  func BenchmarkSubF64_C(b *testing.B) {
   363  	as := make([]float64, 8192)
   364  	bs := make([]float64, 8192)
   365  	for i := 0; i < 8192; i++ {
   366  		as[i] = float64(i)
   367  		bs[i] = 1
   368  	}
   369  
   370  	cs := make([]float64, 8192)
   371  
   372  	av := testutil.MakeFloat64Vector(as, nil)
   373  	bv := testutil.MakeFloat64Vector(bs, nil)
   374  	cv := testutil.MakeFloat64Vector(cs, nil)
   375  
   376  	for i := 0; i < b.N; i++ {
   377  		if err := NumericSubFloat[float64](av, bv, cv); err != nil {
   378  			b.Fail()
   379  		}
   380  	}
   381  }
   382  
   383  func BenchmarkSubDec64(b *testing.B) {
   384  	as := make([]int64, 8192)
   385  	bs := make([]int64, 8192)
   386  	cs := make([]int64, 8192)
   387  	for i := 0; i < 8192; i++ {
   388  		as[i] = int64(i)
   389  		bs[i] = 1
   390  	}
   391  
   392  	av := testutil.MakeDecimal64Vector(as, nil, types.T_decimal64.ToType())
   393  	bv := testutil.MakeDecimal64Vector(bs, nil, types.T_decimal64.ToType())
   394  	cv := testutil.MakeDecimal64Vector(cs, nil, types.T_decimal64.ToType())
   395  
   396  	for i := 0; i < b.N; i++ {
   397  		if err := Decimal64VecSub(av, bv, cv); err != nil {
   398  			b.Fail()
   399  		}
   400  	}
   401  }
   402  
   403  func BenchmarkAddDec128(b *testing.B) {
   404  	as := make([]int64, 8192)
   405  	bs := make([]int64, 8192)
   406  	cs := make([]int64, 8192)
   407  	for i := 0; i < 8192; i++ {
   408  		as[i] = int64(i)
   409  		bs[i] = 1
   410  	}
   411  
   412  	av := testutil.MakeDecimal128Vector(as, nil, types.T_decimal128.ToType())
   413  	bv := testutil.MakeDecimal128Vector(bs, nil, types.T_decimal128.ToType())
   414  	cv := testutil.MakeDecimal128Vector(cs, nil, types.T_decimal128.ToType())
   415  
   416  	for i := 0; i < b.N; i++ {
   417  		if err := Decimal128VecSub(av, bv, cv); err != nil {
   418  			b.Fail()
   419  		}
   420  	}
   421  }
   422  
   423  func TestDatetimeDesc(t *testing.T) {
   424  	cases := []struct {
   425  		name string
   426  		vecs []*vector.Vector
   427  		proc *process.Process
   428  		want int64
   429  	}{
   430  		{
   431  			name: "TEST01",
   432  			vecs: makeDatetimeSubVectors("2018-01-01 7:18:20", "2017-12-01 12:15:12", testutil.NewProc()),
   433  			proc: testutil.NewProc(),
   434  			want: 2660588,
   435  		},
   436  
   437  		{
   438  			name: "TEST02",
   439  			vecs: makeDatetimeSubVectors("2017-12-01 12:15:12", "2018-01-01 7:18:20", testutil.NewProc()),
   440  			proc: testutil.NewProc(),
   441  			want: -2660588,
   442  		},
   443  		{
   444  			name: "TEST03",
   445  			vecs: makeDatetimeSubVectors("2018-01-01 00:00:00", "2018-01-01 00:00:00", testutil.NewProc()),
   446  			proc: testutil.NewProc(),
   447  			want: 0,
   448  		},
   449  		{
   450  			name: "TEST04",
   451  			vecs: makeDatetimeSubVectors("2018-01-01 00:00:01", "2018-01-01 00:00:00", testutil.NewProc()),
   452  			proc: testutil.NewProc(),
   453  			want: 1,
   454  		},
   455  		{
   456  			name: "TEST05",
   457  			vecs: makeDatetimeSubVectors("2018-01-01 00:00:59", "2018-01-01 00:00:00", testutil.NewProc()),
   458  			proc: testutil.NewProc(),
   459  			want: 59,
   460  		},
   461  		{
   462  			name: "TEST06",
   463  			vecs: makeDatetimeSubVectors("2018-01-01 00:01:00", "2018-01-01 00:00:00", testutil.NewProc()),
   464  			proc: testutil.NewProc(),
   465  			want: 60,
   466  		},
   467  	}
   468  
   469  	for _, c := range cases {
   470  		t.Run(c.name, func(t *testing.T) {
   471  			DatetimeSub(c.vecs[0], c.vecs[1], c.vecs[2])
   472  			require.Equal(t, c.want, c.vecs[2].Col.([]int64)[0])
   473  		})
   474  	}
   475  }
   476  
   477  func makeDatetimeSubVectors(firstStr, secondStr string, proc *process.Process) []*vector.Vector {
   478  	vec := make([]*vector.Vector, 3)
   479  
   480  	firstDate, _ := types.ParseDatetime(firstStr, 0)
   481  	secondDate, _ := types.ParseDatetime(secondStr, 0)
   482  
   483  	vec[0] = vector.NewConstFixed(types.T_datetime.ToType(), 1, firstDate, proc.Mp())
   484  	vec[1] = vector.NewConstFixed(types.T_datetime.ToType(), 1, secondDate, proc.Mp())
   485  	vec[2] = proc.AllocScalarVector(types.T_int64.ToType())
   486  
   487  	return vec
   488  }