github.com/dolthub/go-mysql-server@v0.18.0/sql/expression/function/extract.go (about) 1 // Copyright 2023 Dolthub, Inc. 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 function 16 17 import ( 18 "fmt" 19 "time" 20 21 "github.com/dolthub/go-mysql-server/sql" 22 "github.com/dolthub/go-mysql-server/sql/expression" 23 "github.com/dolthub/go-mysql-server/sql/types" 24 ) 25 26 // Extract takes out the specified unit(s) from the time expression. 27 type Extract struct { 28 expression.BinaryExpressionStub 29 } 30 31 var _ sql.FunctionExpression = (*Extract)(nil) 32 var _ sql.CollationCoercible = (*Extract)(nil) 33 34 // NewExtract creates a new Extract expression. 35 func NewExtract(e1, e2 sql.Expression) sql.Expression { 36 return &Extract{ 37 expression.BinaryExpressionStub{ 38 LeftChild: e1, 39 RightChild: e2, 40 }, 41 } 42 } 43 44 // FunctionName implements sql.FunctionExpression 45 func (td *Extract) FunctionName() string { 46 return "extract" 47 } 48 49 // Description implements sql.FunctionExpression 50 func (td *Extract) Description() string { 51 return "returns the values of the unit(s) specified in the time expression" 52 } 53 54 // Type implements the Expression interface. 55 func (td *Extract) Type() sql.Type { return types.Int64 } 56 57 // CollationCoercibility implements the interface sql.CollationCoercible. 58 func (*Extract) CollationCoercibility(ctx *sql.Context) (collation sql.CollationID, coercibility byte) { 59 return sql.Collation_binary, 5 60 } 61 62 func (td *Extract) String() string { 63 return fmt.Sprintf("%s(%s from %s)", td.FunctionName(), td.LeftChild, td.RightChild) 64 } 65 66 // WithChildren implements the Expression interface. 67 func (td *Extract) WithChildren(children ...sql.Expression) (sql.Expression, error) { 68 if len(children) != 2 { 69 return nil, sql.ErrInvalidChildrenNumber.New(td, len(children), 2) 70 } 71 return NewExtract(children[0], children[1]), nil 72 } 73 74 // Eval implements the Expression interface. 75 func (td *Extract) Eval(ctx *sql.Context, row sql.Row) (interface{}, error) { 76 if td.LeftChild == nil || td.RightChild == nil { 77 return nil, nil 78 } 79 80 left, err := td.LeftChild.Eval(ctx, row) 81 if err != nil { 82 return nil, err 83 } 84 85 if left == nil { 86 return nil, nil 87 } 88 89 unit, ok := left.(string) 90 if !ok { 91 return nil, fmt.Errorf("unit is not string type") 92 } 93 94 right, err := td.RightChild.Eval(ctx, row) 95 if err != nil { 96 return nil, err 97 } 98 99 if right == nil { 100 return nil, nil 101 } 102 103 right, err = types.DatetimeMaxPrecision.ConvertWithoutRangeCheck(right) 104 if err != nil { 105 ctx.Warn(1292, err.Error()) 106 return nil, nil 107 } 108 109 dateTime, ok := right.(time.Time) 110 if !ok { 111 ctx.Warn(1292, "invalid DateTime") 112 return nil, nil 113 } 114 115 switch unit { 116 case "DAY": 117 return dateTime.Day(), nil 118 case "HOUR": 119 return dateTime.Hour(), nil 120 case "MINUTE": 121 return dateTime.Minute(), nil 122 case "SECOND": 123 return dateTime.Second(), nil 124 case "MICROSECOND": 125 return dateTime.Nanosecond() / 1000, nil 126 case "QUARTER": 127 return (int(dateTime.Month())-1)/3 + 1, nil 128 case "MONTH": 129 return int(dateTime.Month()), nil 130 case "WEEK": 131 date, err := getDate(ctx, expression.UnaryExpression{Child: td.RightChild}, row) 132 if err != nil { 133 return nil, err 134 } 135 yyyy, ok := year(date).(int32) 136 if !ok { 137 return nil, sql.ErrInvalidArgumentDetails.New("WEEK", "invalid year") 138 } 139 mm, ok := month(date).(int32) 140 if !ok { 141 return nil, sql.ErrInvalidArgumentDetails.New("WEEK", "invalid month") 142 } 143 dd, ok := day(date).(int32) 144 if !ok { 145 return nil, sql.ErrInvalidArgumentDetails.New("WEEK", "invalid day") 146 } 147 yearForWeek, week := calcWeek(yyyy, mm, dd, weekBehaviourYear) 148 if yearForWeek < yyyy { 149 week = 0 150 } else if yearForWeek > yyyy { 151 week = 53 152 } 153 return int(week), nil 154 case "YEAR": 155 return dateTime.Year(), nil 156 case "DAY_HOUR": 157 dd := dateTime.Day() * 1_00 158 hh := dateTime.Hour() 159 return dd + hh, nil 160 case "DAY_MINUTE": 161 dd := dateTime.Day() * 1_00_00 162 hh := dateTime.Hour() * 1_00 163 mm := dateTime.Minute() 164 return dd + hh + mm, nil 165 case "DAY_SECOND": 166 dd := dateTime.Day() * 1_00_00_00 167 hh := dateTime.Hour() * 1_00_00 168 mm := dateTime.Minute() * 1_00 169 ss := dateTime.Second() 170 return dd + hh + mm + ss, nil 171 case "DAY_MICROSECOND": 172 dd := dateTime.Day() * 1_00_00_00_000000 173 hh := dateTime.Hour() * 1_00_00_000000 174 mm := dateTime.Minute() * 1_00_000000 175 ss := dateTime.Second() * 1_000000 176 mmmmmm := dateTime.Nanosecond() / 1000 177 return dd + hh + mm + ss + mmmmmm, nil 178 case "HOUR_MINUTE": 179 hh := dateTime.Hour() * 1_00 180 mm := dateTime.Minute() 181 return hh + mm, nil 182 case "HOUR_SECOND": 183 hh := dateTime.Hour() * 1_00_00 184 mm := dateTime.Minute() * 1_00 185 ss := dateTime.Second() 186 return hh + mm + ss, nil 187 case "HOUR_MICROSECOND": 188 hh := dateTime.Hour() * 1_00_00_000000 189 mm := dateTime.Minute() * 1_00_000000 190 ss := dateTime.Second() * 1_000000 191 mmmmmm := dateTime.Nanosecond() / 1000 192 return hh + mm + ss + mmmmmm, nil 193 case "MINUTE_SECOND": 194 mm := dateTime.Minute() * 1_00 195 ss := dateTime.Second() 196 return mm + ss, nil 197 case "MINUTE_MICROSECOND": 198 mm := dateTime.Minute() * 1_00_000000 199 ss := dateTime.Second() * 1_000000 200 mmmmmm := dateTime.Nanosecond() / 1000 201 return mm + ss + mmmmmm, nil 202 case "SECOND_MICROSECOND": 203 ss := dateTime.Second() * 1_000000 204 mmmmmm := dateTime.Nanosecond() / 1000 205 return ss + mmmmmm, nil 206 case "YEAR_MONTH": 207 yyyy := dateTime.Year() * 1_00 208 dateTime.Month() 209 mm := int(dateTime.Month()) 210 return yyyy + mm, nil 211 default: 212 return nil, fmt.Errorf("invalid time unit") 213 } 214 }