Skip to content

Commit c7325c1

Browse files
committed
truncate datetime strings, add tests
1 parent fd5847d commit c7325c1

File tree

5 files changed

+36
-17
lines changed

5 files changed

+36
-17
lines changed

enginetest/enginetests.go

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -61,10 +61,8 @@ func TestQueries(t *testing.T, harness Harness) {
6161
ctx := NewContext(harness)
6262
for _, tt := range queries.QueryTests {
6363
t.Run(tt.Query, func(t *testing.T) {
64-
if sh, ok := harness.(SkippingHarness); ok {
65-
if sh.SkipQueryTest(tt.Query) {
66-
t.Skipf("Skipping query plan for %s", tt.Query)
67-
}
64+
if sh, ok := harness.(SkippingHarness); tt.Skip || (ok && sh.SkipQueryTest(tt.Query)) {
65+
t.Skipf("Skipping query test for %s", tt.Query)
6866
}
6967
if IsServerEngine(e) && tt.SkipServerEngine {
7068
t.Skip("skipping for server engine")
@@ -222,7 +220,7 @@ func TestQueriesPrepared(t *testing.T, harness Harness) {
222220
defer e.Close()
223221
t.Run("query prepared tests", func(t *testing.T) {
224222
for _, tt := range queries.QueryTests {
225-
if tt.SkipPrepared {
223+
if tt.Skip || tt.SkipPrepared {
226224
continue
227225
}
228226
t.Run(tt.Query, func(t *testing.T) {
@@ -233,7 +231,7 @@ func TestQueriesPrepared(t *testing.T, harness Harness) {
233231

234232
t.Run("function query prepared tests", func(t *testing.T) {
235233
for _, tt := range queries.FunctionQueryTests {
236-
if tt.SkipPrepared {
234+
if tt.Skip || tt.SkipPrepared {
237235
continue
238236
}
239237
t.Run(tt.Query, func(t *testing.T) {
@@ -265,7 +263,7 @@ func TestQueriesPrepared(t *testing.T, harness Harness) {
265263
func TestJoinQueriesPrepared(t *testing.T, harness Harness) {
266264
harness.Setup(setup.MydbData, setup.MytableData, setup.Pk_tablesData, setup.OthertableData, setup.NiltableData, setup.XyData, setup.FooData, setup.Comp_index_tablesData)
267265
for _, tt := range queries.JoinQueryTests {
268-
if tt.SkipPrepared {
266+
if tt.Skip || tt.SkipPrepared {
269267
continue
270268
}
271269
TestPreparedQuery(t, harness, tt.Query, tt.Expected, tt.ExpectedColumns)

enginetest/evaluation.go

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -400,10 +400,9 @@ func TestQuery2(t *testing.T, harness Harness, e QueryEngine, q string, expected
400400
// TODO: collapse into TestQuery
401401
func TestQueryWithEngine(t *testing.T, harness Harness, e QueryEngine, tt queries.QueryTest) {
402402
t.Run(tt.Query, func(t *testing.T) {
403-
if sh, ok := harness.(SkippingHarness); ok {
404-
if sh.SkipQueryTest(tt.Query) {
405-
t.Skipf("Skipping query %s", tt.Query)
406-
}
403+
if sh, ok := harness.(SkippingHarness); tt.Skip || (IsServerEngine(e) && tt.SkipServerEngine) ||
404+
(ok && sh.SkipQueryTest(tt.Query)) {
405+
t.Skipf("Skipping query %s", tt.Query)
407406
}
408407

409408
ctx := NewContext(harness)
@@ -413,9 +412,6 @@ func TestQueryWithEngine(t *testing.T, harness Harness, e QueryEngine, tt querie
413412
} else if tt.ExpectedErrStr != "" {
414413
AssertErrWithCtx(t, e, harness, ctx, tt.Query, tt.Bindings, nil, tt.ExpectedErrStr)
415414
} else if tt.ExpectedWarning != 0 {
416-
if IsServerEngine(e) && tt.SkipServerEngine {
417-
t.Skip()
418-
}
419415
AssertWarningAndTestQuery(t, e, ctx, harness,
420416
tt.Query,
421417
tt.Expected,

enginetest/queries/insert_queries.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2707,6 +2707,14 @@ var InsertErrorScripts = []ScriptTest{
27072707
Query: "insert into bad values (repeat('0', 65536))",
27082708
ExpectedErr: types.ErrLengthBeyondLimit,
27092709
},
2710+
{
2711+
Name: "try inserting incorrect datetime value",
2712+
SetUpScript: []string{
2713+
"create table t (d datetime)",
2714+
},
2715+
Query: "insert into t values ('2020-01-01 a')",
2716+
ExpectedErr: sql.ErrInvalidValue,
2717+
},
27102718
}
27112719

27122720
var InsertIgnoreScripts = []ScriptTest{

enginetest/queries/queries.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ type QueryTest struct {
5050
// such as the use of the SIGNAL statement.
5151
ExpectedErrStr string
5252
// ExpectedWarning contains the expected warning code when a query generates warnings but not errors.
53+
// The ExpectedWarningsCount field must be set as well.
5354
ExpectedWarning int
5455
// ExpectedWarningsCount is used to test the expected number of warnings generated by a query.
5556
// The ExpectedWarning field must be set for warning counts to be checked.
@@ -62,6 +63,8 @@ type QueryTest struct {
6263
ExpectedColumns sql.Schema
6364
// Bindings are the bind values for the query, if provided
6465
Bindings map[string]sqlparser.Expr
66+
// Skip indicates that the query should be skipped
67+
Skip bool
6568
// SkipPrepared indicates that the query should be skipped when testing prepared statements
6669
SkipPrepared bool
6770
// SkipServerEngine indicates that the query should be skipped when testing a server engine (as opposed to the
@@ -4175,9 +4178,19 @@ SELECT * FROM cte WHERE d = 2;`,
41754178
Expected: []sql.Row{{"9999-12-31 23:59:59.999999"}},
41764179
},
41774180
{
4181+
// This returns nil in MySQL but we are able to truncate the string and convert to a datetime
4182+
Skip: true,
41784183
Query: "SELECT date_add('9999-12-31:23:59:59.99999944444444444-', INTERVAL 0 day);",
41794184
Expected: []sql.Row{{nil}},
41804185
},
4186+
{
4187+
// https://github.com/dolthub/dolt/issues/9917
4188+
Query: "select cast('2020-01-01 a' as datetime)",
4189+
ExpectedWarning: 1292,
4190+
ExpectedWarningsCount: 1,
4191+
Expected: []sql.Row{{"2020-01-01 00:00:00"}},
4192+
},
4193+
{},
41814194
{
41824195
Query: `SELECT * FROM (SELECT * FROM (SELECT * FROM (SELECT * FROM othertable) othertable_one) othertable_two) othertable_three WHERE s2 = 'first'`,
41834196
Expected: []sql.Row{

sql/types/datetime.go

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -365,10 +365,14 @@ func (t datetimeType) ConvertWithoutRangeCheck(ctx context.Context, v interface{
365365
}
366366

367367
func parseDatetime(value string) (time.Time, bool) {
368-
for _, layout := range TimestampDatetimeLayouts {
369-
if t, err := time.Parse(layout, value); err == nil {
370-
return t.UTC(), true
368+
end := len(value)
369+
for end > 0 {
370+
for _, layout := range TimestampDatetimeLayouts {
371+
if t, err := time.Parse(layout, value[0:end]); err == nil {
372+
return t.UTC(), true
373+
}
371374
}
375+
end--
372376
}
373377
return time.Time{}, false
374378
}

0 commit comments

Comments
 (0)