github.com/matrixorigin/matrixone@v0.7.0/pkg/sql/plan/function/builtin/multi/date_add_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  
    15  package multi
    16  
    17  import (
    18  	"testing"
    19  
    20  	"github.com/matrixorigin/matrixone/pkg/common/moerr"
    21  	"github.com/matrixorigin/matrixone/pkg/common/mpool"
    22  	"github.com/matrixorigin/matrixone/pkg/container/types"
    23  	"github.com/matrixorigin/matrixone/pkg/container/vector"
    24  	"github.com/matrixorigin/matrixone/pkg/testutil"
    25  	"github.com/matrixorigin/matrixone/pkg/vm/process"
    26  	"github.com/stretchr/testify/require"
    27  )
    28  
    29  func TestDateAdd(t *testing.T) {
    30  	cases := []struct {
    31  		name string
    32  		vecs []*vector.Vector
    33  		proc *process.Process
    34  		want string
    35  	}{
    36  		{
    37  			name: "TEST01",
    38  			vecs: makeDateAddVectors("2022-01-01", true, 1, types.Day, proc.Mp()),
    39  			proc: testutil.NewProc(),
    40  			want: "2022-01-02",
    41  		},
    42  	}
    43  
    44  	for _, c := range cases {
    45  		t.Run(c.name, func(t *testing.T) {
    46  			date, err := DateAdd(c.vecs, c.proc)
    47  			if err != nil {
    48  				t.Fatal(err)
    49  			}
    50  			require.Equal(t, c.want, date.Col.([]types.Date)[0].String())
    51  		})
    52  	}
    53  
    54  }
    55  
    56  func TestDatetimeAdd(t *testing.T) {
    57  	cases := []struct {
    58  		name string
    59  		vecs []*vector.Vector
    60  		proc *process.Process
    61  		want string
    62  	}{
    63  		{
    64  			name: "TEST01",
    65  			vecs: makeDatetimeAddVectors("2022-01-01 00:00:00", true, 1, types.Day, proc.Mp()),
    66  			proc: testutil.NewProc(),
    67  			want: "2022-01-02 00:00:00",
    68  		},
    69  	}
    70  
    71  	for _, c := range cases {
    72  		t.Run(c.name, func(t *testing.T) {
    73  			date, err := DatetimeAdd(c.vecs, c.proc)
    74  			if err != nil {
    75  				t.Fatal(err)
    76  			}
    77  			require.Equal(t, c.want, date.Col.([]types.Datetime)[0].String())
    78  		})
    79  	}
    80  
    81  }
    82  
    83  func TestDateStringAdd(t *testing.T) {
    84  	cases := []struct {
    85  		name string
    86  		vecs []*vector.Vector
    87  		proc *process.Process
    88  		want string
    89  		err  uint16
    90  	}{
    91  		{
    92  			name: "TEST01",
    93  			vecs: makeDateStringAddVectors("2022-01-01", true, 1, types.Day, proc.Mp()),
    94  			proc: testutil.NewProc(),
    95  			want: "2022-01-02 00:00:00",
    96  			err:  0,
    97  		},
    98  		{
    99  			name: "TEST02",
   100  			vecs: makeDateStringAddVectors("2022-01-01 00:00:00", true, 1, types.Day, proc.Mp()),
   101  			proc: testutil.NewProc(),
   102  			want: "2022-01-02 00:00:00",
   103  			err:  0,
   104  		},
   105  		{
   106  			name: "TEST03",
   107  			vecs: makeDateStringAddVectors("2022-01-01", true, 1, types.Second, proc.Mp()),
   108  			proc: testutil.NewProc(),
   109  			want: "2022-01-01 00:00:01",
   110  			err:  0,
   111  		},
   112  		{
   113  			name: "TEST04",
   114  			vecs: makeDateStringAddVectors("xxxx", true, 1, types.Second, proc.Mp()),
   115  			proc: testutil.NewProc(),
   116  			want: "0001-01-01 00:00:00",
   117  			err:  moerr.ErrInvalidInput,
   118  		},
   119  	}
   120  
   121  	for _, c := range cases {
   122  		t.Run(c.name, func(t *testing.T) {
   123  			date, err := DateStringAdd(c.vecs, c.proc)
   124  			require.Equal(t, c.want, date.Col.([]types.Datetime)[0].String())
   125  			require.True(t, moerr.IsMoErrCode(err, c.err))
   126  		})
   127  	}
   128  
   129  }
   130  
   131  func makeDateAddVectors(str string, isConst bool, num int64, unit types.IntervalType, mp *mpool.MPool) []*vector.Vector {
   132  	vec := make([]*vector.Vector, 3)
   133  
   134  	date, _ := types.ParseDateCast(str)
   135  
   136  	vec[0] = vector.NewConstFixed(types.T_date.ToType(), 1, date, mp)
   137  	vec[1] = vector.NewConstFixed(types.T_int64.ToType(), 1, num, mp)
   138  	vec[2] = vector.NewConstFixed(types.T_int64.ToType(), 1, int64(unit), mp)
   139  	return vec
   140  }
   141  
   142  func makeDatetimeAddVectors(str string, isConst bool, num int64, unit types.IntervalType, mp *mpool.MPool) []*vector.Vector {
   143  	vec := make([]*vector.Vector, 3)
   144  
   145  	datetime, _ := types.ParseDatetime(str, 0)
   146  
   147  	vec[0] = vector.NewConstFixed(types.T_datetime.ToType(), 1, datetime, mp)
   148  	vec[1] = vector.NewConstFixed(types.T_int64.ToType(), 1, num, mp)
   149  	vec[2] = vector.NewConstFixed(types.T_int64.ToType(), 1, int64(unit), mp)
   150  	return vec
   151  }
   152  
   153  func makeDateStringAddVectors(str string, isConst bool, num int64, unit types.IntervalType, mp *mpool.MPool) []*vector.Vector {
   154  	vec := make([]*vector.Vector, 3)
   155  	vec[0] = vector.NewConstString(types.T_varchar.ToType(), 1, str, mp)
   156  	vec[1] = vector.NewConstFixed(types.T_int64.ToType(), 1, num, mp)
   157  	vec[2] = vector.NewConstFixed(types.T_int64.ToType(), 1, int64(unit), mp)
   158  	return vec
   159  }