github.com/matrixorigin/matrixone@v0.7.0/pkg/sql/plan/function/builtin/binary/datediff_test.go (about)

     1  // Copyright 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  package binary
    15  
    16  import (
    17  	"testing"
    18  
    19  	"github.com/matrixorigin/matrixone/pkg/common/mpool"
    20  	"github.com/matrixorigin/matrixone/pkg/container/types"
    21  	"github.com/matrixorigin/matrixone/pkg/container/vector"
    22  	"github.com/matrixorigin/matrixone/pkg/testutil"
    23  	"github.com/matrixorigin/matrixone/pkg/vm/process"
    24  	"github.com/stretchr/testify/require"
    25  )
    26  
    27  func TestDateDiff(t *testing.T) {
    28  	procs := testutil.NewProc()
    29  	cases := []struct {
    30  		name string
    31  		vecs []*vector.Vector
    32  		proc *process.Process
    33  		want int64
    34  	}{
    35  		{
    36  			name: "TEST01",
    37  			vecs: makeDateDiffVectors("2017-08-17", "2017-08-17", procs.Mp()),
    38  			proc: testutil.NewProc(),
    39  			want: 0,
    40  		},
    41  		{
    42  			name: "TEST02",
    43  			vecs: makeDateDiffVectors("2017-08-17", "2017-08-08", procs.Mp()),
    44  			proc: testutil.NewProc(),
    45  			want: 9,
    46  		},
    47  		{
    48  			name: "TEST03",
    49  			vecs: makeDateDiffVectors("2017-08-08", "2017-08-17", procs.Mp()),
    50  			proc: testutil.NewProc(),
    51  			want: -9,
    52  		},
    53  		{
    54  			name: "TEST04",
    55  			vecs: makeDateDiffVectors("2022-10-9", "2012-10-11", procs.Mp()),
    56  			proc: testutil.NewProc(),
    57  			want: 3650,
    58  		},
    59  		{
    60  			name: "TEST05",
    61  			vecs: makeDateDiffVectors("2022-10-9", "2004-04-24", procs.Mp()),
    62  			proc: testutil.NewProc(),
    63  			want: 6742,
    64  		},
    65  		{
    66  			name: "TEST06",
    67  			vecs: makeDateDiffVectors("2022-10-9", "2008-12-04", procs.Mp()),
    68  			proc: testutil.NewProc(),
    69  			want: 5057,
    70  		},
    71  		{
    72  			name: "TEST07",
    73  			vecs: makeDateDiffVectors("2022-10-9", "2012-03-23", procs.Mp()),
    74  			proc: testutil.NewProc(),
    75  			want: 3852,
    76  		},
    77  		{
    78  			name: "TEST08",
    79  			vecs: makeDateDiffVectors("2022-10-9", "2000-03-23", procs.Mp()),
    80  			proc: testutil.NewProc(),
    81  			want: 8235,
    82  		},
    83  		{
    84  			name: "TEST09",
    85  			vecs: makeDateDiffVectors("2022-10-9", "2030-03-23", procs.Mp()),
    86  			proc: testutil.NewProc(),
    87  			want: -2722,
    88  		},
    89  		{
    90  			name: "TEST03",
    91  			vecs: makeDateDiffVectors("2022-10-9", "2040-03-23", procs.Mp()),
    92  			proc: testutil.NewProc(),
    93  			want: -6375,
    94  		},
    95  	}
    96  
    97  	for _, c := range cases {
    98  		t.Run(c.name, func(t *testing.T) {
    99  			date, err := DateDiff(c.vecs, c.proc)
   100  			if err != nil {
   101  				t.Fatal(err)
   102  			}
   103  			require.Equal(t, c.want, date.Col.([]int64)[0])
   104  		})
   105  	}
   106  }
   107  
   108  func makeDateDiffVectors(firstStr, secondStr string, mp *mpool.MPool) []*vector.Vector {
   109  	vec := make([]*vector.Vector, 2)
   110  
   111  	firstDate, _ := types.ParseDateCast(firstStr)
   112  	secondDate, _ := types.ParseDateCast(secondStr)
   113  
   114  	vec[0] = vector.NewConstFixed(types.T_date.ToType(), 1, firstDate, mp)
   115  	vec[1] = vector.NewConstFixed(types.T_date.ToType(), 1, secondDate, mp)
   116  	return vec
   117  }