github.com/matrixorigin/matrixone@v0.7.0/pkg/sql/plan/function/builtin/unary/day_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 unary
    16  
    17  import (
    18  	"testing"
    19  
    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 TestDateToDayFunc(t *testing.T) {
    28  	procs := testutil.NewProc()
    29  	cases := []struct {
    30  		name     string
    31  		proc     *process.Process
    32  		inputstr []string
    33  		inputNsp []uint64
    34  		expected []uint8
    35  		isScalar bool
    36  	}{
    37  		{
    38  			name:     "Date to day test - normal",
    39  			proc:     procs,
    40  			inputstr: []string{"2021-01-01", "2022-06-05", "2022-12-31"},
    41  			expected: []uint8{1, 5, 31},
    42  			isScalar: false,
    43  		},
    44  		{
    45  			name:     "Date to day test - normal with null",
    46  			proc:     procs,
    47  			inputstr: []string{"2021-01-01", "2022-06-05", "2022-10-18", "2022-12-31"},
    48  			inputNsp: []uint64{1, 2},
    49  			expected: []uint8{1, 1, 1, 31},
    50  			isScalar: false,
    51  		},
    52  		{
    53  			name:     "Date to day test - scalar",
    54  			proc:     procs,
    55  			inputstr: []string{"2021-01-01"},
    56  			expected: []uint8{1},
    57  			isScalar: true,
    58  		},
    59  		{
    60  			name:     "Date to day test - null",
    61  			proc:     procs,
    62  			expected: []uint8{0},
    63  			isScalar: true,
    64  		},
    65  	}
    66  
    67  	for _, c := range cases {
    68  		t.Run(c.name, func(t *testing.T) {
    69  			vecs := make([]*vector.Vector, 1)
    70  			if c.inputstr != nil {
    71  				vecs[0] = testutil.MakeDateVector(c.inputstr, c.inputNsp)
    72  				if c.isScalar {
    73  					vecs[0].MakeScalar(1)
    74  				}
    75  			} else {
    76  				vecs[0] = testutil.MakeScalarNull(types.T_date, 0)
    77  			}
    78  
    79  			result, err := DateToDay(vecs, c.proc)
    80  			if err != nil {
    81  				t.Fatal(err)
    82  			}
    83  			col := result.Col.([]uint8)
    84  			require.Equal(t, c.expected, col)
    85  			require.Equal(t, c.isScalar, result.IsScalar())
    86  		})
    87  	}
    88  }
    89  
    90  func TestDatetimeToDayFunc(t *testing.T) {
    91  	procs := testutil.NewProc()
    92  	cases := []struct {
    93  		name     string
    94  		proc     *process.Process
    95  		inputstr []string
    96  		inputNsp []uint64
    97  		expected []uint8
    98  		isScalar bool
    99  	}{
   100  		{
   101  			name:     "Datetime to day test - normal",
   102  			proc:     procs,
   103  			inputstr: []string{"2021-01-01 13:11:10", "2022-06-05 07:00:22", "2022-12-31 22:53:40"},
   104  			expected: []uint8{1, 5, 31},
   105  			isScalar: false,
   106  		},
   107  		{
   108  			name:     "Datetime to day test - normal with null",
   109  			proc:     procs,
   110  			inputstr: []string{"2021-01-01 13:11:10", "2022-06-05 07:00:22", "2022-12-31 22:53:40"},
   111  			inputNsp: []uint64{1},
   112  			expected: []uint8{1, 1, 31},
   113  			isScalar: false,
   114  		},
   115  		{
   116  			name:     "Datetime to day test - scalar",
   117  			proc:     procs,
   118  			inputstr: []string{"2021-01-01 13:11:10"},
   119  			expected: []uint8{1},
   120  			isScalar: true,
   121  		},
   122  		{
   123  			name:     "Datetime to day test - null",
   124  			proc:     procs,
   125  			expected: []uint8{0},
   126  			isScalar: true,
   127  		},
   128  	}
   129  
   130  	for _, c := range cases {
   131  		t.Run(c.name, func(t *testing.T) {
   132  			vecs := make([]*vector.Vector, 1)
   133  			if c.inputstr != nil {
   134  				vecs[0] = testutil.MakeDateTimeVector(c.inputstr, c.inputNsp)
   135  				if c.isScalar {
   136  					vecs[0].MakeScalar(1)
   137  				}
   138  			} else {
   139  				vecs[0] = testutil.MakeScalarNull(types.T_datetime, 0)
   140  			}
   141  
   142  			result, err := DatetimeToDay(vecs, c.proc)
   143  			if err != nil {
   144  				t.Fatal(err)
   145  			}
   146  			col := result.Col.([]uint8)
   147  			require.Equal(t, c.expected, col)
   148  			require.Equal(t, c.isScalar, result.IsScalar())
   149  		})
   150  	}
   151  }