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

blob enginetests #1061

Merged
merged 5 commits into from
Jun 22, 2022
Merged
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
96 changes: 55 additions & 41 deletions enginetest/enginetests.go
Original file line number Diff line number Diff line change
Expand Up @@ -477,7 +477,7 @@ func TestQueryErrors(t *testing.T, harness Harness) {
func TestInsertInto(t *testing.T, harness Harness) {
harness.Setup(setup.MydbData, setup.MytableData, setup.Mytable_del_idxData, setup.KeylessData, setup.NiltableData, setup.TypestableData, setup.EmptytableData, setup.AutoincrementData, setup.OthertableData, setup.Othertable_del_idxData)
for _, insertion := range queries.InsertQueries {
runWriteQueryTest(t, harness, insertion)
RunWriteQueryTest(t, harness, insertion)
}

harness.Setup(setup.MydbData)
Expand Down Expand Up @@ -515,7 +515,7 @@ func TestBrokenInsertScripts(t *testing.T, harness Harness) {
func TestSpatialInsertInto(t *testing.T, harness Harness) {
harness.Setup(setup.SpatialSetup...)
for _, tt := range queries.SpatialInsertQueries {
runWriteQueryTest(t, harness, tt)
RunWriteQueryTest(t, harness, tt)
}
}

Expand All @@ -542,7 +542,7 @@ func TestLoadDataFailing(t *testing.T, harness Harness) {
func TestReplaceInto(t *testing.T, harness Harness) {
harness.Setup(setup.MydbData, setup.MytableData, setup.Mytable_del_idxData, setup.TypestableData)
for _, tt := range queries.ReplaceQueries {
runWriteQueryTest(t, harness, tt)
RunWriteQueryTest(t, harness, tt)
}
}

Expand All @@ -556,7 +556,7 @@ func TestReplaceIntoErrors(t *testing.T, harness Harness) {
func TestUpdate(t *testing.T, harness Harness) {
harness.Setup(setup.MydbData, setup.MytableData, setup.Mytable_del_idxData, setup.FloattableData, setup.NiltableData, setup.TypestableData, setup.Pk_tablesData, setup.OthertableData, setup.TabletestData)
for _, tt := range queries.UpdateTests {
runWriteQueryTest(t, harness, tt)
RunWriteQueryTest(t, harness, tt)
}
}

Expand All @@ -579,14 +579,14 @@ func TestUpdateErrors(t *testing.T, harness Harness) {
func TestSpatialUpdate(t *testing.T, harness Harness) {
harness.Setup(setup.SpatialSetup...)
for _, update := range queries.SpatialUpdateTests {
runWriteQueryTest(t, harness, update)
RunWriteQueryTest(t, harness, update)
}
}

func TestDelete(t *testing.T, harness Harness) {
harness.Setup(setup.MydbData, setup.MytableData, setup.TabletestData)
for _, tt := range queries.DeleteTests {
runWriteQueryTest(t, harness, tt)
RunWriteQueryTest(t, harness, tt)
}
}

Expand Down Expand Up @@ -628,7 +628,7 @@ func TestDeleteErrors(t *testing.T, harness Harness) {
func TestSpatialDelete(t *testing.T, harness Harness) {
harness.Setup(setup.SpatialSetup...)
for _, delete := range queries.SpatialDeleteTests {
runWriteQueryTest(t, harness, delete)
RunWriteQueryTest(t, harness, delete)
}
}

Expand Down Expand Up @@ -1683,7 +1683,7 @@ func TestVersionedViewsPrepared(t *testing.T, harness Harness) {
func TestCreateTable(t *testing.T, harness Harness) {
harness.Setup(setup.MydbData, setup.MytableData, setup.FooData)
for _, tt := range queries.CreateTableQueries {
runWriteQueryTest(t, harness, tt)
RunWriteQueryTest(t, harness, tt)
}

harness.Setup(setup.MydbData, setup.MytableData)
Expand Down Expand Up @@ -2612,7 +2612,7 @@ func TestPkOrdinalsDDL(t *testing.T, harness Harness) {
}

for _, tt := range queries.OrdinalDDLWriteQueries {
runWriteQueryTest(t, harness, tt)
RunWriteQueryTest(t, harness, tt)
}
}

Expand Down Expand Up @@ -4401,7 +4401,7 @@ func TestAddDropPks(t *testing.T, harness Harness) {
harness.Setup([]setup.SetupScript{{
"create database mydb",
"use mydb",
"create table t1 (pk text, v text, primary key (pk, v))",
"create table t1 (pk varchar(20), v varchar(20), primary key (pk, v))",
"insert into t1 values ('a1', 'a2'), ('a2', 'a3'), ('a3', 'a4')",
}})
e := mustNewEngine(t, harness)
Expand Down Expand Up @@ -4487,12 +4487,6 @@ func TestAddDropPks(t *testing.T, harness Harness) {
// Assert that a duplicate row causes an alter table error
AssertErr(t, e, harness, `ALTER TABLE t1 ADD PRIMARY KEY (pk, v)`, sql.ErrPrimaryKeyViolation)

// Assert that the schema of t1 is unchanged
TestQueryWithContext(t, ctx, e, `DESCRIBE t1`, []sql.Row{
{"pk", "text", "NO", "", "", ""},
{"v", "text", "NO", "MUL", "", ""},
}, nil, nil)

// Assert that adding a primary key with an unknown column causes an error
AssertErr(t, e, harness, `ALTER TABLE t1 ADD PRIMARY KEY (v2)`, sql.ErrKeyColumnDoesNotExist)

Expand All @@ -4505,8 +4499,8 @@ func TestAddDropPks(t *testing.T, harness Harness) {
// Execute a MultiDDL Alter Statement
RunQuery(t, e, harness, `ALTER TABLE t1 DROP PRIMARY KEY, ADD PRIMARY KEY (v)`)
TestQueryWithContext(t, ctx, e, `DESCRIBE t1`, []sql.Row{
{"pk", "text", "NO", "", "", ""},
{"v", "text", "NO", "PRI", "", ""},
{"pk", "varchar(20)", "NO", "", "", ""},
{"v", "varchar(20)", "NO", "PRI", "", ""},
}, nil, nil)
AssertErr(t, e, harness, `INSERT INTO t1 (pk, v) values ("a100", "a3")`, sql.ErrPrimaryKeyViolation)

Expand All @@ -4521,8 +4515,8 @@ func TestAddDropPks(t *testing.T, harness Harness) {
// https://stackoverflow.com/questions/8301744/mysql-reports-a-primary-key-but-can-not-drop-it-from-the-table
RunQuery(t, e, harness, `ALTER TABLE t1 ADD PRIMARY KEY (pk, v), DROP PRIMARY KEY`)
TestQueryWithContext(t, ctx, e, `DESCRIBE t1`, []sql.Row{
{"pk", "text", "NO", "", "", ""},
{"v", "text", "NO", "", "", ""},
{"pk", "varchar(20)", "NO", "", "", ""},
{"v", "varchar(20)", "NO", "", "", ""},
}, nil, nil)
TestQueryWithContext(t, ctx, e, `SELECT * FROM t1 ORDER BY pk`, []sql.Row{
{"a1", "a2"},
Expand Down Expand Up @@ -4585,29 +4579,31 @@ func (c customFunc) WithChildren(children ...sql.Expression) (sql.Expression, er
}

func TestAlterTable(t *testing.T, harness Harness) {
errorTests := []queries.QueryErrorTest{
{
Query: "ALTER TABLE one_pk_two_idx MODIFY COLUMN v1 BIGINT DEFAULT (pk) AFTER v3",
ExpectedErr: sql.ErrTableColumnNotFound,
},
{
Query: "ALTER TABLE one_pk_two_idx ADD COLUMN v4 BIGINT DEFAULT (pk) AFTER v3",
ExpectedErr: sql.ErrTableColumnNotFound,
},
{
Query: "ALTER TABLE one_pk_two_idx ADD COLUMN v3 BIGINT DEFAULT 5, RENAME COLUMN v3 to v2",
ExpectedErr: sql.ErrTableColumnNotFound,
},
{
Query: "ALTER TABLE one_pk_two_idx ADD COLUMN v3 BIGINT DEFAULT 5, modify column v3 bigint default null",
ExpectedErr: sql.ErrTableColumnNotFound,
},
}

harness.Setup(setup.MydbData)
e := mustNewEngine(t, harness)
defer e.Close()

t.Run("Modify column invalid after", func(t *testing.T) {
RunQuery(t, e, harness, "CREATE TABLE t1008(pk BIGINT DEFAULT (v2) PRIMARY KEY, v1 BIGINT DEFAULT (pk), v2 BIGINT)")
AssertErr(t, e, harness, "ALTER TABLE t1008 MODIFY COLUMN v1 BIGINT DEFAULT (pk) AFTER v3", sql.ErrTableColumnNotFound)
})

t.Run("Add column invalid after", func(t *testing.T) {
RunQuery(t, e, harness, "CREATE TABLE t1009(pk BIGINT DEFAULT (v2) PRIMARY KEY, v1 BIGINT DEFAULT (pk), v2 BIGINT)")
AssertErr(t, e, harness, "ALTER TABLE t1009 ADD COLUMN v4 BIGINT DEFAULT (pk) AFTER v3", sql.ErrTableColumnNotFound)
})

t.Run("rename column added in same statement", func(t *testing.T) {
RunQuery(t, e, harness, "CREATE TABLE t30(pk BIGINT PRIMARY KEY, v1 BIGINT DEFAULT '4')")
AssertErr(t, e, harness, "ALTER TABLE t30 ADD COLUMN v3 BIGINT DEFAULT 5, RENAME COLUMN v3 to v2", sql.ErrTableColumnNotFound)
})

t.Run("modify column added in same statement", func(t *testing.T) {
RunQuery(t, e, harness, "CREATE TABLE t31(pk BIGINT PRIMARY KEY, v1 BIGINT DEFAULT '4')")
AssertErr(t, e, harness, "ALTER TABLE t31 ADD COLUMN v3 BIGINT DEFAULT 5, modify column v3 bigint default null", sql.ErrTableColumnNotFound)
})
for _, tt := range errorTests {
runQueryErrorTest(t, harness, tt)
}

t.Run("variety of alter column statements in a single statement", func(t *testing.T) {
RunQuery(t, e, harness, "CREATE TABLE t32(pk BIGINT PRIMARY KEY, v1 int, v2 int, v3 int, toRename int)")
Expand Down Expand Up @@ -5243,7 +5239,7 @@ func TestPersist(t *testing.T, harness Harness, newPersistableSess func(ctx *sql
func TestKeylessUniqueIndex(t *testing.T, harness Harness) {
harness.Setup(setup.KeylessSetup...)
for _, tt := range queries.InsertIntoKeylessUnique {
runWriteQueryTest(t, harness, tt)
RunWriteQueryTest(t, harness, tt)
}

for _, tt := range queries.InsertIntoKeylessUniqueError {
Expand Down Expand Up @@ -5698,3 +5694,21 @@ func findRole(toUser string, roles []*mysql_db.RoleEdge) *mysql_db.RoleEdge {
}
return nil
}

func TestBlobs(t *testing.T, h Harness) {
h.Setup(setup.MydbData, setup.BlobData, setup.MytableData)
e := mustNewEngine(t, h)
defer e.Close()

for _, tt := range queries.BlobErrors {
runQueryErrorTest(t, h, tt)
}

for _, tt := range queries.BlobQueries {
TestQueryWithEngine(t, h, e, tt)
}

for _, tt := range queries.BlobWriteQueries {
RunWriteQueryTest(t, h, tt)
}
}
6 changes: 3 additions & 3 deletions enginetest/evaluation.go
Original file line number Diff line number Diff line change
Expand Up @@ -261,10 +261,10 @@ func TestQueryWithContext(t *testing.T, ctx *sql.Context, e *sqle.Engine, q stri
require.NoError(err)
}
sch, iter, err := e.QueryWithBindings(ctx, q, bindings)
require.NoError(err, "Unexpected error for query %s", q)
require.NoError(err, "Unexpected error for query %s: %s", q, err)

rows, err := sql.RowIterToRows(ctx, sch, iter)
require.NoError(err, "Unexpected error for query %s", q)
require.NoError(err, "Unexpected error for query %s: %s", q, err)

checkResults(t, require, expected, expectedCols, sch, rows, q)

Expand Down Expand Up @@ -706,7 +706,7 @@ func ExtractQueryNode(node sql.Node) sql.Node {
}
}

func runWriteQueryTest(t *testing.T, harness Harness, tt queries.WriteQueryTest) {
func RunWriteQueryTest(t *testing.T, harness Harness, tt queries.WriteQueryTest) {
t.Run(tt.WriteQuery, func(t *testing.T) {
if sh, ok := harness.(SkippingHarness); ok {
if sh.SkipQueryTest(tt.WriteQuery) {
Expand Down
9 changes: 7 additions & 2 deletions enginetest/memory_engine_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,19 +117,20 @@ func TestSingleQuery(t *testing.T) {

var test queries.QueryTest
test = queries.QueryTest{
Query: `show create table two_pk`,
Query: `CREATE TABLE test (blob_col BLOB, INDEX(blob_col(10)));`,
Expected: []sql.Row{
{1, 2},
},
}

fmt.Sprintf("%v", test)
harness := enginetest.NewMemoryHarness("", 1, testNumPartitions, true, nil)
harness.Setup(setup.Mytable...)
engine, err := harness.NewEngine(t)
if err != nil {
panic(err)
}
enginetest.CreateIndexes(t, harness, engine)

engine.Analyzer.Debug = true
engine.Analyzer.Verbose = true

Expand Down Expand Up @@ -728,6 +729,10 @@ func TestNullRanges(t *testing.T) {
enginetest.TestNullRanges(t, enginetest.NewDefaultMemoryHarness())
}

func TestBlobs(t *testing.T) {
enginetest.TestBlobs(t, enginetest.NewDefaultMemoryHarness())
}

func TestPersist(t *testing.T) {
newSess := func(ctx *sql.Context) sql.PersistableSession {
persistedGlobals := memory.GlobalsMap{}
Expand Down
134 changes: 134 additions & 0 deletions enginetest/queries/blob_queries.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,134 @@
// Copyright 2022 Dolthub, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package queries

import "github.com/dolthub/go-mysql-server/sql"

var BlobQueries = []QueryTest{
{
Query: "select i, hex(b) from blobt",
Expected: []sql.Row{
{1, "666972737420726F77"},
{2, "7365636F6E6420726F77"},
{3, "746869726420726F77"},
},
},
{
Query: "select * from blobt where i = 1",
Expected: []sql.Row{
{1, []byte("first row")},
},
},
{
Query: "select * from blobt order by b desc",
Expected: []sql.Row{
{3, []byte("third row")},
{2, []byte("second row")},
{1, []byte("first row")},
},
},
}

var BlobWriteQueries = []WriteQueryTest{
{
WriteQuery: "insert into blobt values (4, '100000000')",
ExpectedWriteResult: []sql.Row{{sql.NewOkResult(1)}},
SelectQuery: "select * from blobt where i = 4",
ExpectedSelect: []sql.Row{{4, []byte("100000000")}},
},
{
WriteQuery: "update blobt set b = '100000000' where i = 1",
ExpectedWriteResult: []sql.Row{{newUpdateResult(1, 1)}},
SelectQuery: "select * from blobt where i = 1",
ExpectedSelect: []sql.Row{{1, []byte("100000000")}},
},
{
WriteQuery: "delete from blobt where i = 1",
ExpectedWriteResult: []sql.Row{{sql.NewOkResult(1)}},
SelectQuery: "select * from blobt",
ExpectedSelect: []sql.Row{
{2, []byte("second row")},
{3, []byte("third row")},
},
},
{
WriteQuery: "alter table mytable modify s blob",
ExpectedWriteResult: []sql.Row{{sql.NewOkResult(0)}},
SelectQuery: "select * from blobt",
ExpectedSelect: []sql.Row{
{1, []byte("first row")},
{2, []byte("second row")},
{3, []byte("third row")},
},
},
{
WriteQuery: "alter table blobt rename column b to v, add v1 int",
ExpectedWriteResult: []sql.Row{{sql.NewOkResult(0)}},
SelectQuery: "select * from blobt",
ExpectedSelect: []sql.Row{
{1, []byte("first row"), nil},
{2, []byte("second row"), nil},
{3, []byte("third row"), nil},
},
},
{
WriteQuery: "ALTER TABLE blobt ADD COLUMN v2 BIGINT DEFAULT (i + 2) AFTER b",
ExpectedWriteResult: []sql.Row{{sql.NewOkResult(0)}},
SelectQuery: "select * from blobt",
ExpectedSelect: []sql.Row{
{1, []byte("first row"), 3},
{2, []byte("second row"), 4},
{3, []byte("third row"), 5},
},
},
}

var BlobErrors = []QueryErrorTest{
{
Query: "alter table blobt add index bidx (b)",
ExpectedErr: sql.ErrInvalidByteIndex,
},
{
Query: "alter table blobt add column b2 blob default '1'",
ExpectedErr: sql.ErrInvalidTextBlobColumnDefault,
},
{
Query: "create table b (b blob primary key)",
ExpectedErr: sql.ErrInvalidBytePrimaryKey,
},
{
Query: "create table b (b smallblob primary key)",
ExpectedErr: sql.ErrInvalidBytePrimaryKey,
},
{
Query: "create table b (i int primary key, b blob, index bidx(b))",
ExpectedErr: sql.ErrInvalidByteIndex,
},
{
Query: "CREATE TABLE b (pk BIGINT PRIMARY KEY, v1 TEXT, INDEX (v1));",
ExpectedErr: sql.ErrInvalidByteIndex,
},
}

var BlobUnsupported = []QueryTest{
{
Query: "select convert(`b` using utf8) from blobt",
Expected: []sql.Row{
{1, "first row"},
{2, "second row"},
{3, "third row"},
},
},
}
Loading