Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

executor: add new format specifier(%# %@ %.) for str_to_date expression (#22790) #22813

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions expression/builtin_time_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1411,6 +1411,15 @@ func (s *testEvaluatorSuite) TestStrToDate(c *C) {
{"15-01-2001 1:9:8.999", "%d-%m-%Y %H:%i:%S.%f", true, time.Date(2001, 1, 15, 1, 9, 8, 999000000, time.Local)},
{"2003-01-02 10:11:12 PM", "%Y-%m-%d %H:%i:%S %p", false, time.Time{}},
{"10:20:10AM", "%H:%i:%S%p", false, time.Time{}},
// test %@(skip alpha), %#(skip number), %.(skip punct)
{"2020-10-10ABCD", "%Y-%m-%d%@", true, time.Date(2020, 10, 10, 0, 0, 0, 0, time.Local)},
{"2020-10-101234", "%Y-%m-%d%#", true, time.Date(2020, 10, 10, 0, 0, 0, 0, time.Local)},
{"2020-10-10....", "%Y-%m-%d%.", true, time.Date(2020, 10, 10, 0, 0, 0, 0, time.Local)},
{"2020-10-10.1", "%Y-%m-%d%.%#%@", true, time.Date(2020, 10, 10, 0, 0, 0, 0, time.Local)},
{"abcd2020-10-10.1", "%@%Y-%m-%d%.%#%@", true, time.Date(2020, 10, 10, 0, 0, 0, 0, time.Local)},
{"abcd-2020-10-10.1", "%@-%Y-%m-%d%.%#%@", true, time.Date(2020, 10, 10, 0, 0, 0, 0, time.Local)},
{"2020-10-10", "%Y-%m-%d%@", true, time.Date(2020, 10, 10, 0, 0, 0, 0, time.Local)},
{"2020-10-10abcde123abcdef", "%Y-%m-%d%@%#", true, time.Date(2020, 10, 10, 0, 0, 0, 0, time.Local)},
}

fc := funcs[ast.StrToDate]
Expand Down
39 changes: 39 additions & 0 deletions types/time.go
Original file line number Diff line number Diff line change
Expand Up @@ -2878,6 +2878,9 @@ var dateFormatParserTable = map[string]dateFormatParser{
"%S": secondsNumeric, // Seconds (00..59)
"%T": time24Hour, // Time, 24-hour (hh:mm:ss)
"%Y": yearNumericFourDigits, // Year, numeric, four digits
"%#": skipAllNums, // Skip all numbers
"%.": skipAllPunct, // Skip all punctation characters
"%@": skipAllAlpha, // Skip all alpha characters
// Deprecated since MySQL 5.7.5
"%y": yearNumericTwoDigits, // Year, numeric (two digits)
// TODO: Add the following...
Expand Down Expand Up @@ -3258,3 +3261,39 @@ func DateTimeIsOverflow(sc *stmtctx.StatementContext, date Time) (bool, error) {
inRange := (t.After(b) || t.Equal(b)) && (t.Before(e) || t.Equal(e))
return !inRange, nil
}

func skipAllNums(t *CoreTime, input string, ctx map[string]int) (string, bool) {
retIdx := 0
for i, ch := range input {
if unicode.IsNumber(ch) {
retIdx = i + 1
} else {
break
}
}
return input[retIdx:], true
}

func skipAllPunct(t *CoreTime, input string, ctx map[string]int) (string, bool) {
retIdx := 0
for i, ch := range input {
if unicode.IsPunct(ch) {
retIdx = i + 1
} else {
break
}
}
return input[retIdx:], true
}

func skipAllAlpha(t *CoreTime, input string, ctx map[string]int) (string, bool) {
retIdx := 0
for i, ch := range input {
if unicode.IsLetter(ch) {
retIdx = i + 1
} else {
break
}
}
return input[retIdx:], true
}