github.com/matrixorigin/matrixone@v0.7.0/pkg/sql/plan/function/builtin/multi/unix_timestamp_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  	"github.com/smartystreets/goconvey/convey"
    19  	"reflect"
    20  	"testing"
    21  	"time"
    22  
    23  	"github.com/matrixorigin/matrixone/pkg/container/types"
    24  	"github.com/matrixorigin/matrixone/pkg/container/vector"
    25  	"github.com/matrixorigin/matrixone/pkg/testutil"
    26  	"github.com/matrixorigin/matrixone/pkg/vm/process"
    27  	"github.com/stretchr/testify/require"
    28  )
    29  
    30  func TestUnixTimestamp(t *testing.T) {
    31  	UnixtimeCase(t, types.T_int64, MustTimestamp(time.UTC, "2022-01-01 22:23:00"), 1641075780, false)
    32  	UnixtimeCase(t, types.T_int64, MustTimestamp(time.UTC, "2022-01-02 22:23:00"), 1641162180, false)
    33  	UnixtimeCase(t, types.T_int64, MustTimestamp(time.UTC, "2022-01-03 22:23:00"), 1641248580, false)
    34  	UnixtimeCase(t, types.T_int64, MustTimestamp(time.UTC, "2022-02-29 22:23:00"), 0, true)
    35  }
    36  
    37  // func FromUnixTime(lv []*vector.Vector, proc *process.Process) (*vector.Vector, error)
    38  func UnixtimeCase(t *testing.T, typ types.T, src types.Timestamp, res int64, isNull bool) {
    39  	procs := testutil.NewProc()
    40  	cases := []struct {
    41  		name       string
    42  		vecs       []*vector.Vector
    43  		proc       *process.Process
    44  		wantBytes  interface{}
    45  		wantScalar bool
    46  		wantNull   bool
    47  	}{
    48  		{
    49  			name:       "TEST01",
    50  			vecs:       makeVector2(src, true, typ),
    51  			proc:       procs,
    52  			wantBytes:  []int64{res},
    53  			wantScalar: true,
    54  			wantNull:   isNull,
    55  		},
    56  	}
    57  
    58  	for _, c := range cases {
    59  		t.Run(c.name, func(t *testing.T) {
    60  			plus, err := UnixTimestamp(c.vecs, c.proc)
    61  			if err != nil {
    62  				t.Fatal(err)
    63  			}
    64  			if !reflect.DeepEqual(c.wantBytes, plus.Col) {
    65  				t.Errorf("unixtimestamp() want %v but got %v", c.wantBytes, plus.Col)
    66  			}
    67  			require.Equal(t, c.wantNull, plus.ConstVectorIsNull())
    68  			require.Equal(t, c.wantScalar, plus.IsScalar())
    69  		})
    70  	}
    71  }
    72  
    73  func makeVector2(src types.Timestamp, srcScalar bool, t types.T) []*vector.Vector {
    74  	vectors := make([]*vector.Vector, 1)
    75  	vectors[0] = vector.NewConstFixed(types.T_timestamp.ToType(), 1, src, testutil.TestUtilMp)
    76  	return vectors
    77  }
    78  
    79  func TestUnixTimestampVarcharToFloat64(t *testing.T) {
    80  	tests := []struct {
    81  		name   string
    82  		date   string
    83  		expect float64
    84  	}{
    85  		{`Test1`, `2000-01-01 12:00:00.159`, 946728000.159},
    86  		{`Test2`, `2015-11-13 10:20:19.012`, 1447410019.012},
    87  		{"Test3", `2012-11-13 10:20:19.0123456`, 1352802019.012346},
    88  		{"Test3", `2022-11-13 10:20:19.9999999`, 1668334820},
    89  	}
    90  	for _, tt := range tests {
    91  		t.Run(tt.name, func(t *testing.T) {
    92  			scalarVec := testutil.MakeScalarVarchar(tt.date, 1)
    93  			newProcess := testutil.NewProcess()
    94  			newProcess.SessionInfo.TimeZone = time.UTC
    95  			resVec, err := UnixTimestampVarcharToFloat64([]*vector.Vector{scalarVec}, newProcess)
    96  			if err != nil {
    97  				t.Fatal(err)
    98  			}
    99  			cols := vector.MustTCols[float64](resVec)
   100  			require.Equal(t, tt.expect, cols[0])
   101  		})
   102  	}
   103  }
   104  
   105  func TestUnixTimestampVarchar(t *testing.T) {
   106  	convey.Convey("Test01 unix timestamp", t, func() {
   107  		cases := []struct {
   108  			datestr string
   109  			expect  float64
   110  		}{
   111  			{
   112  				datestr: "2013-01-02 08:10:02.123456",
   113  				expect:  1357114202.123456,
   114  			},
   115  			{
   116  				datestr: "2006-01-02 12:19:02.123456",
   117  				expect:  1136204342.123456,
   118  			},
   119  			{
   120  				datestr: "2022-01-02 15:21:02.123456",
   121  				expect:  1641136862.123456,
   122  			},
   123  			{
   124  				datestr: "2006-01-02 12:19:02.666456",
   125  				expect:  1136204342.666456,
   126  			},
   127  			{
   128  				datestr: "2011-01-02 19:31:02.121111",
   129  				expect:  1293996662.121111,
   130  			},
   131  			{
   132  				datestr: "2002-01-02 01:41:02.123459",
   133  				expect:  1009935662.123459,
   134  			},
   135  		}
   136  
   137  		var datestrs []string
   138  		var expects []float64
   139  		for _, c := range cases {
   140  			datestrs = append(datestrs, c.datestr)
   141  			expects = append(expects, c.expect)
   142  		}
   143  
   144  		datestrVector := testutil.MakeVarcharVector(datestrs, nil)
   145  		expectVector := testutil.MakeFloat64Vector(expects, nil)
   146  
   147  		newProc := testutil.NewProc()
   148  		newProc.SessionInfo.TimeZone = time.UTC
   149  		result, err := UnixTimestampVarcharToFloat64([]*vector.Vector{datestrVector}, newProc)
   150  		if err != nil {
   151  			t.Fatal(err)
   152  		}
   153  		convey.So(err, convey.ShouldBeNil)
   154  		compare := testutil.CompareVectors(expectVector, result)
   155  		convey.So(compare, convey.ShouldBeTrue)
   156  	})
   157  
   158  	convey.Convey("Test01 unix timestamp", t, func() {
   159  		cases := []struct {
   160  			datestr string
   161  			expect  int64
   162  		}{
   163  			{
   164  				datestr: "2013-01-02 08:10:02",
   165  				expect:  1357114202,
   166  			},
   167  			{
   168  				datestr: "2006-01-02 12:19:02",
   169  				expect:  1136204342,
   170  			},
   171  			{
   172  				datestr: "2022-01-02 15:21:02",
   173  				expect:  1641136862,
   174  			},
   175  			{
   176  				datestr: "2006-01-02 12:19:02",
   177  				expect:  1136204342,
   178  			},
   179  			{
   180  				datestr: "2011-01-02 19:31:02",
   181  				expect:  1293996662,
   182  			},
   183  			{
   184  				datestr: "2002-01-02 01:41:02",
   185  				expect:  1009935662,
   186  			},
   187  		}
   188  
   189  		var datestrs []string
   190  		var expects []int64
   191  		for _, c := range cases {
   192  			datestrs = append(datestrs, c.datestr)
   193  			expects = append(expects, c.expect)
   194  		}
   195  
   196  		datestrVector := testutil.MakeVarcharVector(datestrs, nil)
   197  		expectVector := testutil.MakeInt64Vector(expects, nil)
   198  
   199  		newProc := testutil.NewProc()
   200  		newProc.SessionInfo.TimeZone = time.UTC
   201  		result, err := UnixTimestampVarcharToInt64([]*vector.Vector{datestrVector}, newProc)
   202  		if err != nil {
   203  			t.Fatal(err)
   204  		}
   205  		convey.So(err, convey.ShouldBeNil)
   206  		compare := testutil.CompareVectors(expectVector, result)
   207  		convey.So(compare, convey.ShouldBeTrue)
   208  	})
   209  }