github.com/cockroachdb/cockroach@v20.2.0-alpha.1+incompatible/pkg/sql/sem/tree/datum_invariants_test.go (about) 1 // Copyright 2018 The Cockroach Authors. 2 // 3 // Use of this software is governed by the Business Source License 4 // included in the file licenses/BSL.txt. 5 // 6 // As of the Change Date specified in that file, in accordance with 7 // the Business Source License, use of this software will be governed 8 // by the Apache License, Version 2.0, included in the file 9 // licenses/APL.txt. 10 11 package tree 12 13 import ( 14 "testing" 15 "time" 16 17 "github.com/cockroachdb/cockroach/pkg/sql/sessiondata" 18 "github.com/cockroachdb/cockroach/pkg/sql/types" 19 "github.com/cockroachdb/cockroach/pkg/util/leaktest" 20 "github.com/cockroachdb/cockroach/pkg/util/timeofday" 21 "github.com/stretchr/testify/assert" 22 "github.com/stretchr/testify/require" 23 ) 24 25 func TestAllTypesCastableToString(t *testing.T) { 26 defer leaktest.AfterTest(t)() 27 for _, typ := range types.Scalar { 28 if ok, _ := isCastDeepValid(typ, types.String); !ok { 29 t.Errorf("%s is not castable to STRING, all types should be", typ) 30 } 31 } 32 } 33 34 func TestAllTypesCastableFromString(t *testing.T) { 35 defer leaktest.AfterTest(t)() 36 for _, typ := range types.Scalar { 37 if ok, _ := isCastDeepValid(types.String, typ); !ok { 38 t.Errorf("%s is not castable from STRING, all types should be", typ) 39 } 40 } 41 } 42 43 func TestCompareTimestamps(t *testing.T) { 44 defer leaktest.AfterTest(t)() 45 46 pacificTimeZone := int32(7 * 60 * 60) 47 sydneyTimeZone := int32(-10 * 60 * 60) 48 49 sydneyFixedZone := time.FixedZone("otan@sydney", -int(sydneyTimeZone)) 50 // kiwiFixedZone is 2 hours ahead of Sydney. 51 kiwiFixedZone := time.FixedZone("otan@auckland", -int(sydneyTimeZone)+2*60*60) 52 53 ddate, err := NewDDateFromTime(time.Date(2019, time.November, 22, 0, 0, 0, 0, time.UTC)) 54 require.NoError(t, err) 55 56 testCases := []struct { 57 desc string 58 left Datum 59 right Datum 60 location *time.Location 61 expected int 62 }{ 63 { 64 desc: "same DTime are equal", 65 left: MakeDTime(timeofday.New(12, 0, 0, 0)), 66 right: MakeDTime(timeofday.New(12, 0, 0, 0)), 67 expected: 0, 68 }, 69 { 70 desc: "same DTimeTZ are equal", 71 left: NewDTimeTZFromOffset(timeofday.New(22, 0, 0, 0), sydneyTimeZone), 72 right: NewDTimeTZFromOffset(timeofday.New(22, 0, 0, 0), sydneyTimeZone), 73 expected: 0, 74 }, 75 { 76 desc: "DTime and DTimeTZ both UTC, and so are equal", 77 left: MakeDTime(timeofday.New(12, 0, 0, 0)), 78 right: NewDTimeTZFromOffset(timeofday.New(12, 0, 0, 0), 0), 79 expected: 0, 80 }, 81 { 82 desc: "DTime and DTimeTZ both Sydney time, and so are equal", 83 left: MakeDTime(timeofday.New(12, 0, 0, 0)), 84 right: NewDTimeTZFromOffset(timeofday.New(12, 0, 0, 0), sydneyTimeZone), 85 location: sydneyFixedZone, 86 expected: 0, 87 }, 88 { 89 desc: "DTimestamp and DTimestampTZ (Sydney) equal in Sydney zone", 90 left: MustMakeDTimestamp(time.Date(2019, time.November, 22, 10, 0, 0, 0, time.UTC), time.Microsecond), 91 right: MustMakeDTimestampTZ(time.Date(2019, time.November, 22, 10, 0, 0, 0, sydneyFixedZone), time.Microsecond), 92 location: sydneyFixedZone, 93 expected: 0, 94 }, 95 { 96 desc: "DTimestamp and DTimestampTZ (Sydney) equal in Sydney+2 zone", 97 left: MustMakeDTimestamp(time.Date(2019, time.November, 22, 12, 0, 0, 0, time.UTC), time.Microsecond), 98 right: MustMakeDTimestampTZ(time.Date(2019, time.November, 22, 10, 0, 0, 0, sydneyFixedZone), time.Microsecond), 99 location: kiwiFixedZone, 100 expected: 0, 101 }, 102 { 103 desc: "Date and DTimestampTZ (Sydney) equal in Sydney zone", 104 left: ddate, 105 right: MustMakeDTimestampTZ(time.Date(2019, time.November, 22, 0, 0, 0, 0, sydneyFixedZone), time.Microsecond), 106 location: sydneyFixedZone, 107 expected: 0, 108 }, 109 { 110 desc: "Date and DTimestampTZ (Sydney) equal in Sydney+2 zone", 111 left: ddate, 112 right: MustMakeDTimestampTZ(time.Date(2019, time.November, 21, 22, 0, 0, 0, sydneyFixedZone), time.Microsecond), 113 location: kiwiFixedZone, 114 expected: 0, 115 }, 116 { 117 desc: "equal wall clock time for DTime and DTimeTZ, with TimeTZ ahead", 118 left: MakeDTime(timeofday.New(12, 0, 0, 0)), 119 right: NewDTimeTZFromOffset(timeofday.New(22, 0, 0, 0), sydneyTimeZone), 120 expected: 1, 121 }, 122 { 123 desc: "equal wall clock time for DTime and DTimeTZ, with TimeTZ behind", 124 left: MakeDTime(timeofday.New(12, 0, 0, 0)), 125 right: NewDTimeTZFromOffset(timeofday.New(5, 0, 0, 0), pacificTimeZone), 126 expected: -1, 127 }, 128 { 129 desc: "equal wall clock time for DTime and DTimeTZ, with TimeTZ ahead", 130 left: NewDTimeTZFromOffset(timeofday.New(22, 0, 0, 0), sydneyTimeZone), 131 right: NewDTimeTZFromOffset(timeofday.New(5, 0, 0, 0), pacificTimeZone), 132 expected: -1, 133 }, 134 { 135 desc: "wall clock time different for DTimeTZ and DTimeTZ", 136 left: NewDTimeTZFromOffset(timeofday.New(23, 0, 0, 0), sydneyTimeZone), 137 right: NewDTimeTZFromOffset(timeofday.New(5, 0, 0, 0), pacificTimeZone), 138 expected: 1, 139 }, 140 } 141 142 for _, tc := range testCases { 143 t.Run( 144 tc.desc, 145 func(t *testing.T) { 146 ctx := &EvalContext{ 147 SessionData: &sessiondata.SessionData{ 148 DataConversion: sessiondata.DataConversionConfig{ 149 Location: tc.location, 150 }, 151 }, 152 } 153 assert.Equal(t, tc.expected, compareTimestamps(ctx, tc.left, tc.right)) 154 assert.Equal(t, -tc.expected, compareTimestamps(ctx, tc.right, tc.left)) 155 }, 156 ) 157 } 158 }