github.com/matrixorigin/matrixone@v0.7.0/pkg/sql/plan/function/builtin/unary/month_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/smartystreets/goconvey/convey"
    24  )
    25  
    26  func TestMonthFunction(t *testing.T) {
    27  	convey.Convey("DateToMonthCase", t, func() {
    28  		type kase struct {
    29  			s    string
    30  			want uint8
    31  		}
    32  
    33  		kases := []kase{
    34  			{
    35  				s:    "2004-04-03",
    36  				want: 4,
    37  			},
    38  			{
    39  				s:    "2004-08-03",
    40  				want: 8,
    41  			},
    42  			{
    43  				s:    "2004-01-03",
    44  				want: 1,
    45  			},
    46  		}
    47  
    48  		var inStrs []string
    49  		var wantUint8 []uint8
    50  		for _, k := range kases {
    51  			inStrs = append(inStrs, k.s)
    52  			wantUint8 = append(wantUint8, k.want)
    53  		}
    54  
    55  		inVector := testutil.MakeDateVector(inStrs, nil)
    56  		wantVector := testutil.MakeUint8Vector(wantUint8, nil)
    57  		proc := testutil.NewProc()
    58  		res, err := DateToMonth([]*vector.Vector{inVector}, proc)
    59  		convey.So(err, convey.ShouldBeNil)
    60  		compare := testutil.CompareVectors(wantVector, res)
    61  		convey.So(compare, convey.ShouldBeTrue)
    62  	})
    63  
    64  	convey.Convey("DateToMonthCaseScalar", t, func() {
    65  		type kase struct {
    66  			s    string
    67  			want uint8
    68  		}
    69  
    70  		k := kase{
    71  			s:    "2004-04-03",
    72  			want: 4,
    73  		}
    74  
    75  		inVector := testutil.MakeScalarDate(k.s, 10)
    76  		wantVector := testutil.MakeScalarUint8(k.want, 10)
    77  		proc := testutil.NewProc()
    78  		res, err := DateToMonth([]*vector.Vector{inVector}, proc)
    79  		convey.So(err, convey.ShouldBeNil)
    80  		compare := testutil.CompareVectors(wantVector, res)
    81  		convey.So(compare, convey.ShouldBeTrue)
    82  	})
    83  
    84  	convey.Convey("DateToMonthCaseScalarNull", t, func() {
    85  		inVector := testutil.MakeScalarNull(types.T_date, 10)
    86  		wantVector := testutil.MakeScalarNull(types.T_uint8, 10)
    87  		proc := testutil.NewProc()
    88  		res, err := DateToMonth([]*vector.Vector{inVector}, proc)
    89  		convey.So(err, convey.ShouldBeNil)
    90  		compare := testutil.CompareVectors(wantVector, res)
    91  		convey.So(compare, convey.ShouldBeTrue)
    92  	})
    93  
    94  	convey.Convey("DatetimeToMonthCase", t, func() {
    95  		type kase struct {
    96  			s    string
    97  			want uint8
    98  		}
    99  
   100  		kases := []kase{
   101  			{
   102  				s:    "2004-04-03 13:11:10",
   103  				want: 4,
   104  			},
   105  			{
   106  				s:    "1999-08-05 11:01:02",
   107  				want: 8,
   108  			},
   109  			{
   110  				s:    "2004-01-03 23:15:08",
   111  				want: 1,
   112  			},
   113  		}
   114  
   115  		var inStrs []string
   116  		var wantUint8 []uint8
   117  		for _, k := range kases {
   118  			inStrs = append(inStrs, k.s)
   119  			wantUint8 = append(wantUint8, k.want)
   120  		}
   121  		inVector := testutil.MakeDateTimeVector(inStrs, nil)
   122  		wantVector := testutil.MakeUint8Vector(wantUint8, nil)
   123  		proc := testutil.NewProc()
   124  		res, err := DatetimeToMonth([]*vector.Vector{inVector}, proc)
   125  		convey.So(err, convey.ShouldBeNil)
   126  		compare := testutil.CompareVectors(wantVector, res)
   127  		convey.So(compare, convey.ShouldBeTrue)
   128  	})
   129  
   130  	convey.Convey("DatetimeToMonthCaseScalar", t, func() {
   131  		type kase struct {
   132  			s    string
   133  			want uint8
   134  		}
   135  
   136  		k := kase{
   137  			s:    "2004-01-03 23:15:08",
   138  			want: 1,
   139  		}
   140  
   141  		inVector := testutil.MakeScalarDateTime(k.s, 10)
   142  		wantVector := testutil.MakeScalarUint8(k.want, 10)
   143  		proc := testutil.NewProc()
   144  		res, err := DatetimeToMonth([]*vector.Vector{inVector}, proc)
   145  		convey.So(err, convey.ShouldBeNil)
   146  		compare := testutil.CompareVectors(wantVector, res)
   147  		convey.So(compare, convey.ShouldBeTrue)
   148  	})
   149  
   150  	convey.Convey("DatetimeToMonthCaseScalarNull", t, func() {
   151  		inVector := testutil.MakeScalarNull(types.T_datetime, 10)
   152  		wantVector := testutil.MakeScalarNull(types.T_uint8, 10)
   153  		proc := testutil.NewProc()
   154  		res, err := DatetimeToMonth([]*vector.Vector{inVector}, proc)
   155  		convey.So(err, convey.ShouldBeNil)
   156  		compare := testutil.CompareVectors(wantVector, res)
   157  		convey.So(compare, convey.ShouldBeTrue)
   158  	})
   159  
   160  	convey.Convey("DateStringToMonthCase", t, func() {
   161  		type kase struct {
   162  			s    string
   163  			want uint8
   164  		}
   165  
   166  		kases := []kase{
   167  			{
   168  				s:    "2014-04-03",
   169  				want: 4,
   170  			},
   171  			{
   172  				s:    "2009-11-03",
   173  				want: 11,
   174  			},
   175  			{
   176  				s:    "2012-07-03",
   177  				want: 7,
   178  			},
   179  			{
   180  				s:    "2012-02-03 18:23:15",
   181  				want: 2,
   182  			},
   183  		}
   184  
   185  		var inStrs []string
   186  		var wantUint8 []uint8
   187  		for _, k := range kases {
   188  			inStrs = append(inStrs, k.s)
   189  			wantUint8 = append(wantUint8, k.want)
   190  		}
   191  
   192  		inVector := testutil.MakeVarcharVector(inStrs, nil)
   193  		wantVector := testutil.MakeUint8Vector(wantUint8, nil)
   194  		proc := testutil.NewProc()
   195  		res, err := DateStringToMonth([]*vector.Vector{inVector}, proc)
   196  		convey.So(err, convey.ShouldBeNil)
   197  		compare := testutil.CompareVectors(wantVector, res)
   198  		convey.So(compare, convey.ShouldBeTrue)
   199  	})
   200  
   201  	convey.Convey("DateStringToMonthCaseScalar", t, func() {
   202  		type kase struct {
   203  			s    string
   204  			want uint8
   205  		}
   206  
   207  		k := kase{
   208  			s:    "2004-01-03 23:15:08",
   209  			want: 1,
   210  		}
   211  
   212  		inVector := testutil.MakeScalarChar(k.s, 10)
   213  		wantVector := testutil.MakeScalarUint8(k.want, 10)
   214  		proc := testutil.NewProc()
   215  		res, err := DateStringToMonth([]*vector.Vector{inVector}, proc)
   216  		convey.So(err, convey.ShouldBeNil)
   217  		compare := testutil.CompareVectors(wantVector, res)
   218  		convey.So(compare, convey.ShouldBeTrue)
   219  	})
   220  
   221  	convey.Convey("DateStringToMonthCaseScalarNull", t, func() {
   222  		inVector := testutil.MakeScalarNull(types.T_char, 10)
   223  		wantVector := testutil.MakeScalarNull(types.T_uint8, 10)
   224  		proc := testutil.NewProc()
   225  		res, err := DateStringToMonth([]*vector.Vector{inVector}, proc)
   226  		convey.So(err, convey.ShouldBeNil)
   227  		compare := testutil.CompareVectors(wantVector, res)
   228  		convey.So(compare, convey.ShouldBeTrue)
   229  	})
   230  
   231  }