Documentation
¶
Overview ¶
Example ¶
package main import ( "context" "fmt" "io" sqle "github.com/dolthub/go-mysql-server" "github.com/dolthub/go-mysql-server/memory" "github.com/dolthub/go-mysql-server/sql" ) func main() { e := sqle.NewDefault() // Create a test memory database and register it to the default engine. e.AddDatabase(createTestDatabase()) ctx := sql.NewContext(context.Background(), sql.WithIndexRegistry(sql.NewIndexRegistry()), sql.WithViewRegistry(sql.NewViewRegistry())).WithCurrentDB("test") _, r, err := e.Query(ctx, `SELECT name, count(*) FROM mytable WHERE name = 'John Doe' GROUP BY name`) checkIfError(err) // Iterate results and print them. for { row, err := r.Next() if err == io.EOF { break } checkIfError(err) name := row[0] count := row[1] fmt.Println(name, count) } } func checkIfError(err error) { if err != nil { panic(err) } } func createTestDatabase() sql.Database { db := memory.NewDatabase("test") table := memory.NewTable("mytable", sql.Schema{ {Name: "name", Type: sql.Text, Source: "mytable"}, {Name: "email", Type: sql.Text, Source: "mytable"}, }) db.AddTable("mytable", table) ctx := sql.NewEmptyContext() rows := []sql.Row{ sql.NewRow("John Doe", "john@doe.com"), sql.NewRow("John Doe", "johnalt@doe.com"), sql.NewRow("Jane Doe", "jane@doe.com"), sql.NewRow("Evil Bob", "evilbob@gmail.com"), } for _, row := range rows { table.Insert(ctx, row) } return db }
Output: John Doe 2
Index ¶
- Variables
- func AssertErr(t *testing.T, e *sqle.Engine, harness Harness, query string, ...)
- func AssertWarning(t *testing.T, e *sqle.Engine, harness Harness, query string, expectedCode int)
- func CreateSubsetTestData(t *testing.T, harness Harness, includedTables []string) []sql.Database
- func CreateTestData(t *testing.T, harness Harness) []sql.Database
- func DeleteRows(t *testing.T, ctx *sql.Context, table sql.DeletableTable, rows ...sql.Row)
- func InsertRows(t *testing.T, ctx *sql.Context, table sql.InsertableTable, rows ...sql.Row)
- func NewBaseSession() sql.Session
- func NewContext(harness Harness) *sql.Context
- func NewContextWithEngine(harness Harness, engine *sqle.Engine) *sql.Context
- func NewEngine(t *testing.T, harness Harness) *sqle.Engine
- func NewEngineWithDbs(t *testing.T, harness Harness, databases []sql.Database, ...) *sqle.Engine
- func RunQuery(t *testing.T, e *sqle.Engine, harness Harness, query string)
- func RunQueryTests(t *testing.T, harness Harness, queries []QueryTest)
- func TestAddColumn(t *testing.T, harness Harness)
- func TestAmbiguousColumnResolution(t *testing.T, harness Harness)
- func TestChecksOnInsert(t *testing.T, harness Harness)
- func TestClearWarnings(t *testing.T, harness Harness)
- func TestColumnAliases(t *testing.T, harness Harness)
- func TestColumnDefaults(t *testing.T, harness Harness)
- func TestCreateCheckConstraints(t *testing.T, harness Harness)
- func TestCreateDatabase(t *testing.T, harness Harness)
- func TestCreateForeignKeys(t *testing.T, harness Harness)
- func TestCreateTable(t *testing.T, harness Harness)
- func TestDelete(t *testing.T, harness Harness)
- func TestDeleteErrors(t *testing.T, harness Harness)
- func TestDisallowedCheckConstraints(t *testing.T, harness Harness)
- func TestDropCheckConstraints(t *testing.T, harness Harness)
- func TestDropColumn(t *testing.T, harness Harness)
- func TestDropDatabase(t *testing.T, harness Harness)
- func TestDropForeignKeys(t *testing.T, harness Harness)
- func TestDropTable(t *testing.T, harness Harness)
- func TestExplode(t *testing.T, harness Harness)
- func TestInfoSchema(t *testing.T, harness Harness)
- func TestInnerNestedInNaturalJoins(t *testing.T, harness Harness)
- func TestInsertInto(t *testing.T, harness Harness)
- func TestInsertIntoErrors(t *testing.T, harness Harness)
- func TestJsonScripts(t *testing.T, harness Harness)
- func TestLoadData(t *testing.T, harness Harness)
- func TestLoadDataErrors(t *testing.T, harness Harness)
- func TestLoadDataFailing(t *testing.T, harness Harness)
- func TestModifyColumn(t *testing.T, harness Harness)
- func TestNaturalJoin(t *testing.T, harness Harness)
- func TestNaturalJoinDisjoint(t *testing.T, harness Harness)
- func TestNaturalJoinEqual(t *testing.T, harness Harness)
- func TestOrderByGroupBy(t *testing.T, harness Harness)
- func TestQueries(t *testing.T, harness Harness)
- func TestQuery(t *testing.T, harness Harness, e *sqle.Engine, q string, expected []sql.Row, ...)
- func TestQueryErrors(t *testing.T, harness Harness)
- func TestQueryPlan(t *testing.T, ctx *sql.Context, engine *sqle.Engine, query string, ...)
- func TestQueryPlans(t *testing.T, harness Harness)
- func TestQueryWithContext(t *testing.T, ctx *sql.Context, e *sqle.Engine, q string, expected []sql.Row, ...)
- func TestReadOnly(t *testing.T, harness Harness)
- func TestRenameColumn(t *testing.T, harness Harness)
- func TestRenameTable(t *testing.T, harness Harness)
- func TestReplaceInto(t *testing.T, harness Harness)
- func TestReplaceIntoErrors(t *testing.T, harness Harness)
- func TestScript(t *testing.T, harness Harness, script ScriptTest) bool
- func TestScriptWithEngine(t *testing.T, e *sqle.Engine, harness Harness, script ScriptTest)
- func TestScripts(t *testing.T, harness Harness)
- func TestSessionSelectLimit(t *testing.T, harness Harness)
- func TestShowTableStatus(t *testing.T, harness Harness)
- func TestStoredProcedures(t *testing.T, harness Harness)
- func TestTracing(t *testing.T, harness Harness)
- func TestTriggerErrors(t *testing.T, harness Harness)
- func TestTriggers(t *testing.T, harness Harness)
- func TestTruncate(t *testing.T, harness Harness)
- func TestUpdate(t *testing.T, harness Harness)
- func TestUpdateErrors(t *testing.T, harness Harness)
- func TestUse(t *testing.T, harness Harness)
- func TestVariableErrors(t *testing.T, harness Harness)
- func TestVariables(t *testing.T, harness Harness)
- func TestVersionedQueries(t *testing.T, harness Harness)
- func TestVersionedViews(t *testing.T, harness Harness)
- func TestViews(t *testing.T, harness Harness)
- func TestWarnings(t *testing.T, harness Harness)
- func WidenRow(sch sql.Schema, row sql.Row) sql.Row
- func WidenRows(sch sql.Schema, rows []sql.Row) []sql.Row
- type ForeignKeyHarness
- type GenericErrorQueryTest
- type Harness
- type IndexDriverHarness
- type IndexDriverInitalizer
- type IndexHarness
- type KeylessTableHarness
- type MemoryHarness
- func (m *MemoryHarness) IndexDriver(dbs []sql.Database) sql.IndexDriver
- func (m *MemoryHarness) NewContext() *sql.Context
- func (m *MemoryHarness) NewDatabase(name string) sql.Database
- func (m *MemoryHarness) NewTable(db sql.Database, name string, schema sql.Schema) (sql.Table, error)
- func (m *MemoryHarness) NewTableAsOf(db sql.VersionedDatabase, name string, schema sql.Schema, asOf interface{}) sql.Table
- func (m *MemoryHarness) Parallelism() int
- func (m *MemoryHarness) SnapshotTable(db sql.VersionedDatabase, name string, asOf interface{}) error
- func (m *MemoryHarness) SupportsForeignKeys() bool
- func (m *MemoryHarness) SupportsKeylessTables() bool
- func (m *MemoryHarness) SupportsNativeIndexCreation() bool
- type QueryErrorTest
- type QueryPlanTest
- type QueryTest
- type ScriptTest
- type ScriptTestAssertion
- type SkippingHarness
- type SkippingMemoryHarness
- type VersionedDBHarness
- type WriteQueryTest
Examples ¶
Constants ¶
This section is empty.
Variables ¶
var BrokenQueries = []QueryTest{ { Query: "SELECT pk1, SUM(c1) FROM two_pk", Expected: []sql.Row{{0, 60.0}}, }, { Query: `SELECT pk, (SELECT max(pk) FROM one_pk WHERE pk < opk.pk) AS x FROM one_pk opk WHERE x > 0 ORDER BY x`, Expected: []sql.Row{ {2, 1}, {3, 2}, }, }, { Query: `SELECT pk, (SELECT max(pk) FROM one_pk WHERE pk < opk.pk) AS min, (SELECT min(pk) FROM one_pk WHERE pk > opk.pk) AS max FROM one_pk opk WHERE max > 1 ORDER BY max;`, Expected: []sql.Row{ {1, 0, 2}, {2, 1, 3}, }, }, { Query: `SELECT pk, (SELECT sum(c1) FROM two_pk WHERE c1 IN (SELECT c4 FROM two_pk WHERE c3 > opk.c5)) AS sum, (SELECT avg(c1) FROM two_pk WHERE pk2 IN (SELECT pk2 FROM two_pk WHERE c1 < opk.c2)) AS avg FROM one_pk opk ORDER BY pk`, Expected: []sql.Row{ {0, 60.0, nil}, {1, 50.0, 10.0}, {2, 30.0, 15.0}, {3, nil, 15.0}, }, }, { Query: `SELECT column_0, sum(column_1) FROM (values row(1,1), row(1,3), row(2,2), row(2,5), row(3,9)) a group by 1 having avg(column_1) > 2 order by 1`, Expected: []sql.Row{ {2, 7.0}, {3, 9.0}, }, }, { Query: "SELECT json_array() FROM dual;", }, { Query: "SELECT json_array_append() FROM dual;", }, { Query: "SELECT json_array_insert() FROM dual;", }, { Query: "SELECT json_contains() FROM dual;", }, { Query: "SELECT json_contains_path() FROM dual;", }, { Query: "SELECT json_depth() FROM dual;", }, { Query: "SELECT json_insert() FROM dual;", }, { Query: "SELECT json_keys() FROM dual;", }, { Query: "SELECT json_length() FROM dual;", }, { Query: "SELECT json_merge_patch() FROM dual;", }, { Query: "SELECT json_merge_preserve() FROM dual;", }, { Query: "SELECT json_object() FROM dual;", }, { Query: "SELECT json_overlaps() FROM dual;", }, { Query: "SELECT json_pretty() FROM dual;", }, { Query: "SELECT json_quote() FROM dual;", }, { Query: "SELECT json_remove() FROM dual;", }, { Query: "SELECT json_replace() FROM dual;", }, { Query: "SELECT json_schema_valid() FROM dual;", }, { Query: "SELECT json_schema_validation_report() FROM dual;", }, { Query: "SELECT json_set() FROM dual;", }, { Query: "SELECT json_search() FROM dual;", }, { Query: "SELECT json_storage_free() FROM dual;", }, { Query: "SELECT json_storage_size() FROM dual;", }, { Query: "SELECT json_type() FROM dual;", }, { Query: "SELECT json_table() FROM dual;", }, { Query: "SELECT json_valid() FROM dual;", }, { Query: "SELECT json_value() FROM dual;", }, { Query: "select date_format(unix_timestamp(i), '%s') from mytable order by 1", }, { Query: `SELECT s as i, i as i from mytable order by i`, }, { Query: "SELECT i, I, s, S FROM mytable;", Expected: []sql.Row{ {1, 1, "first row", "first row"}, {2, 2, "second row", "second row"}, {3, 3, "third row", "third row"}, }, ExpectedColumns: sql.Schema{ { Name: "i", Type: sql.Int64, }, { Name: "I", Type: sql.Int64, }, { Name: "s", Type: sql.MustCreateStringWithDefaults(sqltypes.VarChar, 20), }, { Name: "S", Type: sql.MustCreateStringWithDefaults(sqltypes.VarChar, 20), }, }, }, { Query: "SELECT `i`, `I`, `s`, `S` FROM mytable;", Expected: []sql.Row{ {1, 1, "first row", "first row"}, {2, 2, "second row", "second row"}, {3, 3, "third row", "third row"}, }, ExpectedColumns: sql.Schema{ { Name: "i", Type: sql.Int64, }, { Name: "I", Type: sql.Int64, }, { Name: "s", Type: sql.MustCreateStringWithDefaults(sqltypes.VarChar, 20), }, { Name: "S", Type: sql.MustCreateStringWithDefaults(sqltypes.VarChar, 20), }, }, }, { Query: "SELECT `mytable`.`i`, `mytable`.`I`, `mytable`.`s`, `mytable`.`S` FROM mytable;", Expected: []sql.Row{ {1, 1, "first row", "first row"}, {2, 2, "second row", "second row"}, {3, 3, "third row", "third row"}, }, ExpectedColumns: sql.Schema{ { Name: "i", Type: sql.Int64, }, { Name: "I", Type: sql.Int64, }, { Name: "s", Type: sql.MustCreateStringWithDefaults(sqltypes.VarChar, 20), }, { Name: "S", Type: sql.MustCreateStringWithDefaults(sqltypes.VarChar, 20), }, }, }, }
Queries that are known to be broken in the engine.
var DeleteErrorTests = []GenericErrorQueryTest{
{
Name: "invalid table",
Query: "DELETE FROM invalidtable WHERE x < 1;",
},
{
Name: "invalid column",
Query: "DELETE FROM mytable WHERE z = 'dne';",
},
{
Name: "missing binding",
Query: "DELETE FROM mytable WHERE i = ?;",
},
{
Name: "negative limit",
Query: "DELETE FROM mytable LIMIT -1;",
},
{
Name: "negative offset",
Query: "DELETE FROM mytable LIMIT 1 OFFSET -1;",
},
{
Name: "missing keyword from",
Query: "DELETE mytable WHERE id = 1;",
},
}
var DeleteTests = []WriteQueryTest{ { WriteQuery: "DELETE FROM mytable;", ExpectedWriteResult: []sql.Row{{sql.NewOkResult(3)}}, SelectQuery: "SELECT * FROM mytable;", ExpectedSelect: nil, }, { WriteQuery: "DELETE FROM mytable WHERE i = 2;", ExpectedWriteResult: []sql.Row{{sql.NewOkResult(1)}}, SelectQuery: "SELECT * FROM mytable;", ExpectedSelect: []sql.Row{{int64(1), "first row"}, {int64(3), "third row"}}, }, { WriteQuery: "DELETE FROM mytable WHERE i = ?;", ExpectedWriteResult: []sql.Row{{sql.NewOkResult(1)}}, SelectQuery: "SELECT * FROM mytable;", ExpectedSelect: []sql.Row{{int64(1), "first row"}, {int64(3), "third row"}}, Bindings: map[string]sql.Expression{ "v1": expression.NewLiteral(int64(2), sql.Int64), }, }, { WriteQuery: "DELETE FROM mytable WHERE i < 3;", ExpectedWriteResult: []sql.Row{{sql.NewOkResult(2)}}, SelectQuery: "SELECT * FROM mytable;", ExpectedSelect: []sql.Row{{int64(3), "third row"}}, }, { WriteQuery: "DELETE FROM mytable WHERE i > 1;", ExpectedWriteResult: []sql.Row{{sql.NewOkResult(2)}}, SelectQuery: "SELECT * FROM mytable;", ExpectedSelect: []sql.Row{{int64(1), "first row"}}, }, { WriteQuery: "DELETE FROM mytable WHERE i <= 2;", ExpectedWriteResult: []sql.Row{{sql.NewOkResult(2)}}, SelectQuery: "SELECT * FROM mytable;", ExpectedSelect: []sql.Row{{int64(3), "third row"}}, }, { WriteQuery: "DELETE FROM mytable WHERE i >= 2;", ExpectedWriteResult: []sql.Row{{sql.NewOkResult(2)}}, SelectQuery: "SELECT * FROM mytable;", ExpectedSelect: []sql.Row{{int64(1), "first row"}}, }, { WriteQuery: "DELETE FROM mytable WHERE s = 'first row';", ExpectedWriteResult: []sql.Row{{sql.NewOkResult(1)}}, SelectQuery: "SELECT * FROM mytable;", ExpectedSelect: []sql.Row{{int64(2), "second row"}, {int64(3), "third row"}}, }, { WriteQuery: "DELETE FROM mytable WHERE s <> 'dne';", ExpectedWriteResult: []sql.Row{{sql.NewOkResult(3)}}, SelectQuery: "SELECT * FROM mytable;", ExpectedSelect: nil, }, { WriteQuery: "DELETE FROM mytable WHERE i in (2,3);", ExpectedWriteResult: []sql.Row{{sql.NewOkResult(2)}}, SelectQuery: "SELECT * FROM mytable;", ExpectedSelect: []sql.Row{{int64(1), "first row"}}, }, { WriteQuery: "DELETE FROM mytable WHERE s LIKE '%row';", ExpectedWriteResult: []sql.Row{{sql.NewOkResult(3)}}, SelectQuery: "SELECT * FROM mytable;", ExpectedSelect: nil, }, { WriteQuery: "DELETE FROM mytable WHERE s = 'dne';", ExpectedWriteResult: []sql.Row{{sql.NewOkResult(0)}}, SelectQuery: "SELECT * FROM mytable;", ExpectedSelect: []sql.Row{{int64(1), "first row"}, {int64(2), "second row"}, {int64(3), "third row"}}, }, { WriteQuery: "DELETE FROM mytable WHERE i = 'invalid';", ExpectedWriteResult: []sql.Row{{sql.NewOkResult(0)}}, SelectQuery: "SELECT * FROM mytable;", ExpectedSelect: []sql.Row{{int64(1), "first row"}, {int64(2), "second row"}, {int64(3), "third row"}}, }, { WriteQuery: "DELETE FROM mytable ORDER BY i ASC LIMIT 2;", ExpectedWriteResult: []sql.Row{{sql.NewOkResult(2)}}, SelectQuery: "SELECT * FROM mytable;", ExpectedSelect: []sql.Row{{int64(3), "third row"}}, }, { WriteQuery: "DELETE FROM mytable ORDER BY i DESC LIMIT 1;", ExpectedWriteResult: []sql.Row{{sql.NewOkResult(1)}}, SelectQuery: "SELECT * FROM mytable;", ExpectedSelect: []sql.Row{{int64(1), "first row"}, {int64(2), "second row"}}, }, { WriteQuery: "DELETE FROM mytable ORDER BY i DESC LIMIT 1 OFFSET 1;", ExpectedWriteResult: []sql.Row{{sql.NewOkResult(1)}}, SelectQuery: "SELECT * FROM mytable;", ExpectedSelect: []sql.Row{{int64(1), "first row"}, {int64(3), "third row"}}, }, { WriteQuery: "DELETE FROM mytable WHERE (i,s) = (1, 'first row');", ExpectedWriteResult: []sql.Row{{sql.NewOkResult(1)}}, SelectQuery: "SELECT * FROM mytable;", ExpectedSelect: []sql.Row{{int64(2), "second row"}, {int64(3), "third row"}}, }, }
var ExplodeQueries = []QueryTest{ { Query: `SELECT a, EXPLODE(b), c FROM t`, Expected: []sql.Row{ {int64(1), "a", "first"}, {int64(1), "b", "first"}, {int64(2), "c", "second"}, {int64(2), "d", "second"}, {int64(3), "e", "third"}, {int64(3), "f", "third"}, }, }, { Query: `SELECT a, EXPLODE(b) AS x, c FROM t`, Expected: []sql.Row{ {int64(1), "a", "first"}, {int64(1), "b", "first"}, {int64(2), "c", "second"}, {int64(2), "d", "second"}, {int64(3), "e", "third"}, {int64(3), "f", "third"}, }, }, { Query: `SELECT EXPLODE(SPLIT(c, "")) FROM t LIMIT 5`, Expected: []sql.Row{ {"f"}, {"i"}, {"r"}, {"s"}, {"t"}, }, }, { Query: `SELECT a, EXPLODE(b) AS x, c FROM t WHERE x = 'e'`, Expected: []sql.Row{ {int64(3), "e", "third"}, }, }, { Query: `SELECT HEX(UNHEX(375));`, Expected: []sql.Row{ {"0375"}, }, }, }
var InfoSchemaQueries = []QueryTest{ { Query: "SHOW TABLES", Expected: []sql.Row{ {"auto_increment_tbl"}, {"bigtable"}, {"floattable"}, {"fk_tbl"}, {"mytable"}, {"myview"}, {"newlinetable"}, {"niltable"}, {"othertable"}, {"tabletest"}, {"people"}, }, }, { Query: "SHOW FULL TABLES", Expected: []sql.Row{ {"auto_increment_tbl", "BASE TABLE"}, {"bigtable", "BASE TABLE"}, {"fk_tbl", "BASE TABLE"}, {"floattable", "BASE TABLE"}, {"mytable", "BASE TABLE"}, {"myview", "VIEW"}, {"newlinetable", "BASE TABLE"}, {"niltable", "BASE TABLE"}, {"othertable", "BASE TABLE"}, {"tabletest", "BASE TABLE"}, {"people", "BASE TABLE"}, }, }, { Query: "SHOW TABLES FROM foo", Expected: []sql.Row{ {"other_table"}, }, }, { Query: "SHOW TABLES LIKE '%table'", Expected: []sql.Row{ {"mytable"}, {"othertable"}, {"bigtable"}, {"floattable"}, {"niltable"}, {"newlinetable"}, }, }, { Query: `SHOW COLUMNS FROM mytable`, Expected: []sql.Row{ {"i", "bigint", "NO", "PRI", "", ""}, {"s", "varchar(20)", "NO", "UNI", "", ""}, }, }, { Query: `DESCRIBE mytable`, Expected: []sql.Row{ {"i", "bigint", "NO", "PRI", "", ""}, {"s", "varchar(20)", "NO", "UNI", "", ""}, }, }, { Query: `DESC mytable`, Expected: []sql.Row{ {"i", "bigint", "NO", "PRI", "", ""}, {"s", "varchar(20)", "NO", "UNI", "", ""}, }, }, { Query: `DESCRIBE auto_increment_tbl`, Expected: []sql.Row{ {"pk", "bigint", "NO", "PRI", "", "auto_increment"}, {"c0", "bigint", "YES", "", "", ""}, }, }, { Query: `SHOW COLUMNS FROM mytable WHERE Field = 'i'`, Expected: []sql.Row{ {"i", "bigint", "NO", "PRI", "", ""}, }, }, { Query: `SHOW COLUMNS FROM mytable LIKE 'i'`, Expected: []sql.Row{ {"i", "bigint", "NO", "PRI", "", ""}, }, }, { Query: `SHOW FULL COLUMNS FROM mytable`, Expected: []sql.Row{ {"i", "bigint", nil, "NO", "PRI", "", "", "", ""}, {"s", "varchar(20)", "utf8mb4_0900_ai_ci", "NO", "UNI", "", "", "", "column s"}, }, }, { Query: "SHOW TABLES WHERE `Table` = 'mytable'", Expected: []sql.Row{ {"mytable"}, }, }, { Query: ` SELECT LOGFILE_GROUP_NAME, FILE_NAME, TOTAL_EXTENTS, INITIAL_SIZE, ENGINE, EXTRA FROM INFORMATION_SCHEMA.FILES WHERE FILE_TYPE = 'UNDO LOG' AND FILE_NAME IS NOT NULL AND LOGFILE_GROUP_NAME IS NOT NULL GROUP BY LOGFILE_GROUP_NAME, FILE_NAME, ENGINE, TOTAL_EXTENTS, INITIAL_SIZE ORDER BY LOGFILE_GROUP_NAME `, Expected: nil, }, { Query: ` SELECT DISTINCT TABLESPACE_NAME, FILE_NAME, LOGFILE_GROUP_NAME, EXTENT_SIZE, INITIAL_SIZE, ENGINE FROM INFORMATION_SCHEMA.FILES WHERE FILE_TYPE = 'DATAFILE' ORDER BY TABLESPACE_NAME, LOGFILE_GROUP_NAME `, Expected: nil, }, { Query: ` SELECT COLUMN_NAME, JSON_EXTRACT(HISTOGRAM, '$."number-of-buckets-specified"') FROM information_schema.COLUMN_STATISTICS WHERE SCHEMA_NAME = 'mydb' AND TABLE_NAME = 'mytable' `, Expected: nil, }, { Query: ` SELECT TABLE_NAME FROM information_schema.TABLES WHERE TABLE_SCHEMA='mydb' AND (TABLE_TYPE='BASE TABLE' OR TABLE_TYPE='VIEW') ORDER BY 1 `, Expected: []sql.Row{ {"auto_increment_tbl"}, {"bigtable"}, {"fk_tbl"}, {"floattable"}, {"mytable"}, {"myview"}, {"newlinetable"}, {"niltable"}, {"othertable"}, {"people"}, {"tabletest"}, }, }, { Query: ` SELECT COLUMN_NAME, DATA_TYPE FROM information_schema.COLUMNS WHERE TABLE_SCHEMA='mydb' AND TABLE_NAME='mytable' `, Expected: []sql.Row{ {"s", "varchar(20)"}, {"i", "bigint"}, }, }, { Query: ` SELECT COLUMN_NAME FROM information_schema.COLUMNS WHERE TABLE_SCHEMA=DATABASE() AND TABLE_NAME LIKE '%table' GROUP BY COLUMN_NAME `, Expected: []sql.Row{ {"s"}, {"i"}, {"s2"}, {"i2"}, {"t"}, {"n"}, {"f32"}, {"f64"}, {"b"}, {"f"}, }, }, { Query: ` SELECT COLUMN_NAME FROM information_schema.COLUMNS WHERE TABLE_SCHEMA=DATABASE() AND TABLE_NAME LIKE '%table' GROUP BY 1 `, Expected: []sql.Row{ {"s"}, {"i"}, {"s2"}, {"i2"}, {"t"}, {"n"}, {"f32"}, {"f64"}, {"b"}, {"f"}, }, }, { Query: ` SELECT COLUMN_NAME AS COLUMN_NAME FROM information_schema.COLUMNS WHERE TABLE_SCHEMA=DATABASE() AND TABLE_NAME LIKE '%table' GROUP BY 1 `, Expected: []sql.Row{ {"s"}, {"i"}, {"s2"}, {"i2"}, {"t"}, {"n"}, {"f32"}, {"f64"}, {"b"}, {"f"}, }, }, { Query: ` SELECT COLUMN_NAME AS COLUMN_NAME FROM information_schema.COLUMNS WHERE TABLE_SCHEMA=DATABASE() AND TABLE_NAME LIKE '%table' GROUP BY 1 HAVING SUBSTRING(COLUMN_NAME, 1, 1) = "s" `, Expected: []sql.Row{{"s"}, {"s2"}}, }, { Query: `SHOW INDEXES FROM mytaBLE`, Expected: []sql.Row{ {"mytable", 0, "PRIMARY", 1, "i", nil, 0, nil, nil, "", "BTREE", "", "", "YES", nil}, {"mytable", 0, "mytable_s", 1, "s", nil, 0, nil, nil, "", "BTREE", "", "", "YES", nil}, {"mytable", 1, "mytable_i_s", 1, "i", nil, 0, nil, nil, "", "BTREE", "", "", "YES", nil}, {"mytable", 1, "mytable_i_s", 2, "s", nil, 0, nil, nil, "", "BTREE", "", "", "YES", nil}, }, }, { Query: `SHOW KEYS FROM mytaBLE`, Expected: []sql.Row{ {"mytable", 0, "PRIMARY", 1, "i", nil, 0, nil, nil, "", "BTREE", "", "", "YES", nil}, {"mytable", 0, "mytable_s", 1, "s", nil, 0, nil, nil, "", "BTREE", "", "", "YES", nil}, {"mytable", 1, "mytable_i_s", 1, "i", nil, 0, nil, nil, "", "BTREE", "", "", "YES", nil}, {"mytable", 1, "mytable_i_s", 2, "s", nil, 0, nil, nil, "", "BTREE", "", "", "YES", nil}, }, }, { Query: `SHOW CREATE TABLE mytaBLE`, Expected: []sql.Row{ {"mytable", "CREATE TABLE `mytable` (\n" + " `i` bigint NOT NULL,\n" + " `s` varchar(20) NOT NULL COMMENT 'column s',\n" + " PRIMARY KEY (`i`),\n" + " KEY `mytable_i_s` (`i`,`s`),\n" + " UNIQUE KEY `mytable_s` (`s`)\n" + ") ENGINE=InnoDB DEFAULT CHARSET=utf8mb4"}, }, }, { Query: `SHOW CREATE TABLE fk_TBL`, Expected: []sql.Row{ {"fk_tbl", "CREATE TABLE `fk_tbl` (\n" + " `pk` bigint NOT NULL,\n" + " `a` bigint,\n" + " `b` varchar(20),\n" + " PRIMARY KEY (`pk`),\n" + " CONSTRAINT `fk1` FOREIGN KEY (`a`,`b`) REFERENCES `mytable` (`i`,`s`) ON DELETE CASCADE\n" + ") ENGINE=InnoDB DEFAULT CHARSET=utf8mb4"}, }, }, { Query: "SELECT table_name, `auto_increment` FROM information_schema.tables " + "WHERE TABLE_SCHEMA='mydb' AND TABLE_TYPE='BASE TABLE' ORDER BY 1", Expected: []sql.Row{ {"auto_increment_tbl", 4}, {"bigtable", nil}, {"fk_tbl", nil}, {"floattable", nil}, {"mytable", nil}, {"newlinetable", nil}, {"niltable", nil}, {"othertable", nil}, {"people", nil}, {"tabletest", nil}, }, }, { Query: "SHOW ENGINES", Expected: []sql.Row{ {"InnoDB", "DEFAULT", "Supports transactions, row-level locking, and foreign keys", "YES", "YES", "YES"}, }, }, }
var InfoSchemaScripts = []ScriptTest{ { Name: "describe auto_increment table", SetUpScript: []string{ "create table auto (pk int primary key auto_increment)", }, Assertions: []ScriptTestAssertion{ { Query: "describe auto;", Expected: []sql.Row{ {"pk", "int", "NO", "PRI", "", "auto_increment"}, }, }, }, }, }
var InsertErrorScripts = []ScriptTest{ { Name: "create table with non-pk auto_increment column", Query: "create table bad (pk int primary key, c0 int auto_increment);", ExpectedErr: parse.ErrInvalidAutoIncCols, }, { Name: "create multiple auto_increment columns", Query: "create table bad (pk1 int auto_increment, pk2 int auto_increment, primary key (pk1,pk2));", ExpectedErr: parse.ErrInvalidAutoIncCols, }, { Name: "create auto_increment column with default", Query: "create table bad (pk1 int auto_increment default 10, c0 int);", ExpectedErr: parse.ErrInvalidAutoIncCols, }, }
var InsertErrorTests = []GenericErrorQueryTest{
{
Name: "too few values",
Query: "INSERT INTO mytable (s, i) VALUES ('x');",
},
{
Name: "too many values one column",
Query: "INSERT INTO mytable (s) VALUES ('x', 999);",
},
{
Name: "missing binding",
Query: "INSERT INTO mytable (s) VALUES (?);",
},
{
Name: "too many values two columns",
Query: "INSERT INTO mytable (i, s) VALUES (999, 'x', 'y');",
},
{
Name: "too few values no columns specified",
Query: "INSERT INTO mytable VALUES (999);",
},
{
Name: "too many values no columns specified",
Query: "INSERT INTO mytable VALUES (999, 'x', 'y');",
},
{
Name: "non-existent column values",
Query: "INSERT INTO mytable (i, s, z) VALUES (999, 'x', 999);",
},
{
Name: "non-existent column set",
Query: "INSERT INTO mytable SET i = 999, s = 'x', z = 999;",
},
{
Name: "duplicate column",
Query: "INSERT INTO mytable (i, s, s) VALUES (999, 'x', 'x');",
},
{
Name: "duplicate column set",
Query: "INSERT INTO mytable SET i = 999, s = 'y', s = 'y';",
},
{
Name: "null given to non-nullable",
Query: "INSERT INTO mytable (i, s) VALUES (null, 'y');",
},
{
Name: "incompatible types",
Query: "INSERT INTO mytable (i, s) select * FROM othertable",
},
{
Name: "column count mismatch in select",
Query: "INSERT INTO mytable (i) select * FROM othertable",
},
{
Name: "column count mismatch in select",
Query: "INSERT INTO mytable select s FROM othertable",
},
{
Name: "column count mismatch in join select",
Query: "INSERT INTO mytable (s,i) SELECT * FROM othertable o JOIN mytable m ON m.i=o.i2",
},
{
Name: "duplicate key",
Query: "INSERT INTO mytable (i,s) values (1, 'hello')",
},
{
Name: "duplicate keys",
Query: "INSERT INTO mytable SELECT * from mytable",
},
{
Name: "bad column in on duplicate key update clause",
Query: "INSERT INTO mytable values (10, 'b') ON DUPLICATE KEY UPDATE notExist = 1",
},
}
var InsertQueries = []WriteQueryTest{ { WriteQuery: "INSERT INTO mytable (s, i) VALUES ('x', 999);", ExpectedWriteResult: []sql.Row{{sql.NewOkResult(1)}}, SelectQuery: "SELECT i FROM mytable WHERE s = 'x';", ExpectedSelect: []sql.Row{{int64(999)}}, }, { WriteQuery: "INSERT INTO niltable (i, f) VALUES (10, 10.0), (12, 12.0);", ExpectedWriteResult: []sql.Row{{sql.NewOkResult(2)}}, SelectQuery: "SELECT i,f FROM niltable WHERE f IN (10.0, 12.0) ORDER BY f;", ExpectedSelect: []sql.Row{{int64(10), 10.0}, {int64(12), 12.0}}, }, { WriteQuery: "INSERT INTO mytable SET s = 'x', i = 999;", ExpectedWriteResult: []sql.Row{{sql.NewOkResult(1)}}, SelectQuery: "SELECT i FROM mytable WHERE s = 'x';", ExpectedSelect: []sql.Row{{int64(999)}}, }, { WriteQuery: "INSERT INTO mytable VALUES (999, 'x');", ExpectedWriteResult: []sql.Row{{sql.NewOkResult(1)}}, SelectQuery: "SELECT i FROM mytable WHERE s = 'x';", ExpectedSelect: []sql.Row{{int64(999)}}, }, { WriteQuery: "INSERT INTO mytable VALUES (?, ?);", ExpectedWriteResult: []sql.Row{{sql.NewOkResult(1)}}, SelectQuery: "SELECT i FROM mytable WHERE s = 'x';", ExpectedSelect: []sql.Row{{int64(999)}}, Bindings: map[string]sql.Expression{ "v1": expression.NewLiteral(int64(999), sql.Int64), "v2": expression.NewLiteral("x", sql.Text), }, }, { WriteQuery: "INSERT INTO mytable VALUES (:col1, :col2);", ExpectedWriteResult: []sql.Row{{sql.NewOkResult(1)}}, SelectQuery: "SELECT i FROM mytable WHERE s = 'x';", ExpectedSelect: []sql.Row{{int64(999)}}, Bindings: map[string]sql.Expression{ "col1": expression.NewLiteral(int64(999), sql.Int64), "col2": expression.NewLiteral("x", sql.Text), }, }, { WriteQuery: "INSERT INTO mytable SET i = 999, s = 'x';", ExpectedWriteResult: []sql.Row{{sql.NewOkResult(1)}}, SelectQuery: "SELECT i FROM mytable WHERE s = 'x';", ExpectedSelect: []sql.Row{{int64(999)}}, }, { WriteQuery: `INSERT INTO typestable VALUES ( 999, 127, 32767, 2147483647, 9223372036854775807, 255, 65535, 4294967295, 18446744073709551615, 3.40282346638528859811704183484516925440e+38, 1.797693134862315708145274237317043567981e+308, '2037-04-05 12:51:36', '2231-11-07', 'random text', true, '{"key":"value"}', 'blobdata' );`, ExpectedWriteResult: []sql.Row{{sql.NewOkResult(1)}}, SelectQuery: "SELECT * FROM typestable WHERE id = 999;", ExpectedSelect: []sql.Row{{ int64(999), int8(math.MaxInt8), int16(math.MaxInt16), int32(math.MaxInt32), int64(math.MaxInt64), uint8(math.MaxUint8), uint16(math.MaxUint16), uint32(math.MaxUint32), uint64(math.MaxUint64), float32(math.MaxFloat32), float64(math.MaxFloat64), sql.MustConvert(sql.Timestamp.Convert("2037-04-05 12:51:36")), sql.MustConvert(sql.Date.Convert("2231-11-07")), "random text", sql.True, sql.MustJSON(`{"key":"value"}`), "blobdata", }}, }, { WriteQuery: `INSERT INTO typestable SET id = 999, i8 = 127, i16 = 32767, i32 = 2147483647, i64 = 9223372036854775807, u8 = 255, u16 = 65535, u32 = 4294967295, u64 = 18446744073709551615, f32 = 3.40282346638528859811704183484516925440e+38, f64 = 1.797693134862315708145274237317043567981e+308, ti = '2037-04-05 12:51:36', da = '2231-11-07', te = 'random text', bo = true, js = '{"key":"value"}', bl = 'blobdata' ;`, ExpectedWriteResult: []sql.Row{{sql.NewOkResult(1)}}, SelectQuery: "SELECT * FROM typestable WHERE id = 999;", ExpectedSelect: []sql.Row{{ int64(999), int8(math.MaxInt8), int16(math.MaxInt16), int32(math.MaxInt32), int64(math.MaxInt64), uint8(math.MaxUint8), uint16(math.MaxUint16), uint32(math.MaxUint32), uint64(math.MaxUint64), float32(math.MaxFloat32), float64(math.MaxFloat64), sql.MustConvert(sql.Timestamp.Convert("2037-04-05 12:51:36")), sql.MustConvert(sql.Date.Convert("2231-11-07")), "random text", sql.True, sql.MustJSON(`{"key":"value"}`), "blobdata", }}, }, { WriteQuery: `INSERT INTO typestable VALUES ( 999, -128, -32768, -2147483648, -9223372036854775808, 0, 0, 0, 0, 1.401298464324817070923729583289916131280e-45, 4.940656458412465441765687928682213723651e-324, '0000-00-00 00:00:00', '0000-00-00', '', false, '""', '' );`, ExpectedWriteResult: []sql.Row{{sql.NewOkResult(1)}}, SelectQuery: "SELECT * FROM typestable WHERE id = 999;", ExpectedSelect: []sql.Row{{ int64(999), int8(-math.MaxInt8 - 1), int16(-math.MaxInt16 - 1), int32(-math.MaxInt32 - 1), int64(-math.MaxInt64 - 1), uint8(0), uint16(0), uint32(0), uint64(0), float32(math.SmallestNonzeroFloat32), float64(math.SmallestNonzeroFloat64), sql.Timestamp.Zero(), sql.Date.Zero(), "", sql.False, sql.MustJSON(`""`), "", }}, }, { WriteQuery: `INSERT INTO typestable SET id = 999, i8 = -128, i16 = -32768, i32 = -2147483648, i64 = -9223372036854775808, u8 = 0, u16 = 0, u32 = 0, u64 = 0, f32 = 1.401298464324817070923729583289916131280e-45, f64 = 4.940656458412465441765687928682213723651e-324, ti = '0000-00-00 00:00:00', da = '0000-00-00', te = '', bo = false, js = '""', bl = '' ;`, ExpectedWriteResult: []sql.Row{{sql.NewOkResult(1)}}, SelectQuery: "SELECT * FROM typestable WHERE id = 999;", ExpectedSelect: []sql.Row{{ int64(999), int8(-math.MaxInt8 - 1), int16(-math.MaxInt16 - 1), int32(-math.MaxInt32 - 1), int64(-math.MaxInt64 - 1), uint8(0), uint16(0), uint32(0), uint64(0), float32(math.SmallestNonzeroFloat32), float64(math.SmallestNonzeroFloat64), sql.Timestamp.Zero(), sql.Date.Zero(), "", sql.False, sql.MustJSON(`""`), "", }}, }, { WriteQuery: `INSERT INTO mytable (i,s) VALUES (10, 'NULL')`, ExpectedWriteResult: []sql.Row{{sql.NewOkResult(1)}}, SelectQuery: "SELECT * FROM mytable WHERE i = 10;", ExpectedSelect: []sql.Row{{int64(10), "NULL"}}, }, { WriteQuery: `INSERT INTO typestable VALUES (999, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null);`, ExpectedWriteResult: []sql.Row{{sql.NewOkResult(1)}}, SelectQuery: "SELECT * FROM typestable WHERE id = 999;", ExpectedSelect: []sql.Row{{int64(999), nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil}}, }, { WriteQuery: `INSERT INTO typestable SET id=999, i8=null, i16=null, i32=null, i64=null, u8=null, u16=null, u32=null, u64=null, f32=null, f64=null, ti=null, da=null, te=null, bo=null, js=null, bl=null;`, ExpectedWriteResult: []sql.Row{{sql.NewOkResult(1)}}, SelectQuery: "SELECT * FROM typestable WHERE id = 999;", ExpectedSelect: []sql.Row{{int64(999), nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil}}, }, { WriteQuery: "INSERT INTO mytable SELECT i+100,s FROM mytable", ExpectedWriteResult: []sql.Row{{sql.NewOkResult(3)}}, SelectQuery: "SELECT * FROM mytable ORDER BY i", ExpectedSelect: []sql.Row{ {int64(1), "first row"}, {int64(2), "second row"}, {int64(3), "third row"}, {int64(101), "first row"}, {int64(102), "second row"}, {int64(103), "third row"}, }, }, { WriteQuery: "INSERT INTO emptytable SELECT * FROM mytable", ExpectedWriteResult: []sql.Row{{sql.NewOkResult(3)}}, SelectQuery: "SELECT * FROM emptytable ORDER BY i", ExpectedSelect: []sql.Row{ {int64(1), "first row"}, {int64(2), "second row"}, {int64(3), "third row"}, }, }, { WriteQuery: "INSERT INTO emptytable SELECT * FROM mytable where mytable.i > 2", ExpectedWriteResult: []sql.Row{{sql.NewOkResult(1)}}, SelectQuery: "SELECT * FROM emptytable ORDER BY i", ExpectedSelect: []sql.Row{ {int64(3), "third row"}, }, }, { WriteQuery: "INSERT INTO niltable (i,f) SELECT i+10, NULL FROM mytable where mytable.i > 2", ExpectedWriteResult: []sql.Row{{sql.NewOkResult(1)}}, SelectQuery: "SELECT * FROM niltable where i > 10 ORDER BY i", ExpectedSelect: []sql.Row{ {13, nil, nil, nil}, }, }, { WriteQuery: "INSERT INTO mytable (i,s) SELECT i+10, 'new' FROM mytable", ExpectedWriteResult: []sql.Row{{sql.NewOkResult(3)}}, SelectQuery: "SELECT * FROM mytable ORDER BY i", ExpectedSelect: []sql.Row{ {int64(1), "first row"}, {int64(2), "second row"}, {int64(3), "third row"}, {int64(11), "new"}, {int64(12), "new"}, {int64(13), "new"}, }, }, { WriteQuery: "INSERT INTO mytable SELECT i2+100, s2 FROM othertable", ExpectedWriteResult: []sql.Row{{sql.NewOkResult(3)}}, SelectQuery: "SELECT * FROM mytable ORDER BY i,s", ExpectedSelect: []sql.Row{ {int64(1), "first row"}, {int64(2), "second row"}, {int64(3), "third row"}, {int64(101), "third"}, {int64(102), "second"}, {int64(103), "first"}, }, }, { WriteQuery: "INSERT INTO emptytable (s,i) SELECT * FROM othertable", ExpectedWriteResult: []sql.Row{{sql.NewOkResult(3)}}, SelectQuery: "SELECT * FROM emptytable ORDER BY i,s", ExpectedSelect: []sql.Row{ {int64(1), "third"}, {int64(2), "second"}, {int64(3), "first"}, }, }, { WriteQuery: "INSERT INTO emptytable (s,i) SELECT concat(m.s, o.s2), m.i FROM othertable o JOIN mytable m ON m.i=o.i2", ExpectedWriteResult: []sql.Row{{sql.NewOkResult(3)}}, SelectQuery: "SELECT * FROM emptytable ORDER BY i,s", ExpectedSelect: []sql.Row{ {int64(1), "first rowthird"}, {int64(2), "second rowsecond"}, {int64(3), "third rowfirst"}, }, }, { WriteQuery: `INSERT INTO emptytable (s,i) SELECT s,i from mytable where i = 1 union select s,i from mytable where i = 3`, ExpectedWriteResult: []sql.Row{{sql.NewOkResult(2)}}, SelectQuery: "SELECT * FROM emptytable ORDER BY i,s", ExpectedSelect: []sql.Row{ {int64(1), "first row"}, {int64(3), "third row"}, }, }, { WriteQuery: `INSERT INTO emptytable (s,i) SELECT s,i from mytable where i = 1 union select s,i from mytable where i = 3 union select s,i from mytable where i > 2`, ExpectedWriteResult: []sql.Row{{sql.NewOkResult(2)}}, SelectQuery: "SELECT * FROM emptytable ORDER BY i,s", ExpectedSelect: []sql.Row{ {int64(1), "first row"}, {int64(3), "third row"}, }, }, { WriteQuery: `INSERT INTO emptytable (s,i) SELECT s,i from mytable where i = 1 union all select s,i+1 from mytable where i < 2 union all select s,i+2 from mytable where i in (1)`, ExpectedWriteResult: []sql.Row{{sql.NewOkResult(3)}}, SelectQuery: "SELECT * FROM emptytable ORDER BY i,s", ExpectedSelect: []sql.Row{ {int64(1), "first row"}, {int64(2), "first row"}, {int64(3), "first row"}, }, }, { WriteQuery: "INSERT INTO emptytable (s,i) SELECT distinct s,i from mytable", ExpectedWriteResult: []sql.Row{{sql.NewOkResult(3)}}, SelectQuery: "SELECT * FROM emptytable ORDER BY i,s", ExpectedSelect: []sql.Row{ {int64(1), "first row"}, {int64(2), "second row"}, {int64(3), "third row"}, }, }, { WriteQuery: "INSERT INTO mytable (i,s) SELECT (i + 10.0) / 10.0 + 10 + i, concat(s, ' new') FROM mytable", ExpectedWriteResult: []sql.Row{{sql.NewOkResult(3)}}, SelectQuery: "SELECT * FROM mytable ORDER BY i, s", ExpectedSelect: []sql.Row{ {int64(1), "first row"}, {int64(2), "second row"}, {int64(3), "third row"}, {int64(12), "first row new"}, {int64(13), "second row new"}, {int64(14), "third row new"}, }, }, { WriteQuery: "INSERT INTO mytable (i,s) SELECT CHAR_LENGTH(s), concat('numrows: ', count(*)) from mytable group by 1", ExpectedWriteResult: []sql.Row{{sql.NewOkResult(2)}}, SelectQuery: "SELECT * FROM mytable ORDER BY i, s", ExpectedSelect: []sql.Row{ {1, "first row"}, {2, "second row"}, {3, "third row"}, {9, "numrows: 2"}, {10, "numrows: 1"}, }, }, { WriteQuery: "INSERT INTO mytable (i,s) SELECT CHAR_LENGTH(s), concat('numrows: ', count(*)) from mytable group by 1 HAVING CHAR_LENGTH(s) > 9", ExpectedWriteResult: []sql.Row{{sql.NewOkResult(1)}}, SelectQuery: "SELECT * FROM mytable ORDER BY i, s", ExpectedSelect: []sql.Row{ {1, "first row"}, {2, "second row"}, {3, "third row"}, {10, "numrows: 1"}, }, }, { WriteQuery: "INSERT INTO mytable (i,s) SELECT i * 2, concat(s,s) from mytable order by 1 desc limit 1", ExpectedWriteResult: []sql.Row{{sql.NewOkResult(1)}}, SelectQuery: "SELECT * FROM mytable ORDER BY i, s", ExpectedSelect: []sql.Row{ {1, "first row"}, {2, "second row"}, {3, "third row"}, {6, "third rowthird row"}, }, }, { WriteQuery: "INSERT INTO mytable (i,s) SELECT i + 3, concat(s,s) from mytable order by 1 desc", ExpectedWriteResult: []sql.Row{{sql.NewOkResult(3)}}, SelectQuery: "SELECT * FROM mytable ORDER BY i, s", ExpectedSelect: []sql.Row{ {1, "first row"}, {2, "second row"}, {3, "third row"}, {4, "first rowfirst row"}, {5, "second rowsecond row"}, {6, "third rowthird row"}, }, }, { WriteQuery: `INSERT INTO mytable (i,s) SELECT sub.i + 10, ot.s2 FROM othertable ot INNER JOIN (SELECT i, i2, s2 FROM mytable INNER JOIN othertable ON i = i2) sub ON sub.i = ot.i2 order by 1`, ExpectedWriteResult: []sql.Row{{sql.NewOkResult(3)}}, SelectQuery: "SELECT * FROM mytable where i > 10 ORDER BY i, s", ExpectedSelect: []sql.Row{ {11, "third"}, {12, "second"}, {13, "first"}, }, }, { WriteQuery: `INSERT INTO mytable (i,s) SELECT sub.i + 10, ot.s2 FROM (SELECT i, i2, s2 FROM mytable INNER JOIN othertable ON i = i2) sub INNER JOIN othertable ot ON sub.i = ot.i2 order by 1`, ExpectedWriteResult: []sql.Row{{sql.NewOkResult(3)}}, SelectQuery: "SELECT * FROM mytable where i > 10 ORDER BY i, s", ExpectedSelect: []sql.Row{ {11, "third"}, {12, "second"}, {13, "first"}, }, }, { WriteQuery: "INSERT INTO mytable (i,s) values (1, 'hello') ON DUPLICATE KEY UPDATE s='hello'", ExpectedWriteResult: []sql.Row{{sql.NewOkResult(2)}}, SelectQuery: "SELECT * FROM mytable WHERE i = 1", ExpectedSelect: []sql.Row{{int64(1), "hello"}}, }, { WriteQuery: "INSERT INTO mytable (i,s) values (1, 'hello2') ON DUPLICATE KEY UPDATE s='hello3'", ExpectedWriteResult: []sql.Row{{sql.NewOkResult(2)}}, SelectQuery: "SELECT * FROM mytable WHERE i = 1", ExpectedSelect: []sql.Row{{int64(1), "hello3"}}, }, { WriteQuery: "INSERT INTO mytable (i,s) values (1, 'hello') ON DUPLICATE KEY UPDATE i=10", ExpectedWriteResult: []sql.Row{{sql.NewOkResult(2)}}, SelectQuery: "SELECT * FROM mytable WHERE i = 10", ExpectedSelect: []sql.Row{{int64(10), "first row"}}, }, { WriteQuery: "INSERT INTO mytable (i,s) values (1, 'hello2') ON DUPLICATE KEY UPDATE s='hello3'", ExpectedWriteResult: []sql.Row{{sql.NewOkResult(2)}}, SelectQuery: "SELECT * FROM mytable WHERE i = 1", ExpectedSelect: []sql.Row{{int64(1), "hello3"}}, }, { WriteQuery: "INSERT INTO mytable (i,s) values (1, 'hello2'), (2, 'hello3'), (4, 'no conflict') ON DUPLICATE KEY UPDATE s='hello4'", ExpectedWriteResult: []sql.Row{{sql.NewOkResult(5)}}, SelectQuery: "SELECT * FROM mytable ORDER BY 1", ExpectedSelect: []sql.Row{ {1, "hello4"}, {2, "hello4"}, {3, "third row"}, {4, "no conflict"}, }, }, { WriteQuery: "INSERT INTO mytable (i,s) values (10, 'hello') ON DUPLICATE KEY UPDATE s='hello'", ExpectedWriteResult: []sql.Row{{sql.NewOkResult(1)}}, SelectQuery: "SELECT * FROM mytable ORDER BY 1", ExpectedSelect: []sql.Row{ {1, "first row"}, {2, "second row"}, {3, "third row"}, {10, "hello"}, }, }, { WriteQuery: "INSERT INTO mytable (i,s) values (1,'hi') ON DUPLICATE KEY UPDATE s=VALUES(s)", ExpectedWriteResult: []sql.Row{{sql.NewOkResult(2)}}, SelectQuery: "SELECT * FROM mytable WHERE i = 1", ExpectedSelect: []sql.Row{{int64(1), "hi"}}, }, { WriteQuery: "INSERT INTO mytable (s,i) values ('dup',1) ON DUPLICATE KEY UPDATE s=CONCAT(VALUES(s), 'licate')", ExpectedWriteResult: []sql.Row{{sql.NewOkResult(2)}}, SelectQuery: "SELECT * FROM mytable WHERE i = 1", ExpectedSelect: []sql.Row{{int64(1), "duplicate"}}, }, { WriteQuery: "INSERT INTO mytable (i,s) values (1,'mar'), (2,'par') ON DUPLICATE KEY UPDATE s=CONCAT(VALUES(s), 'tial')", ExpectedWriteResult: []sql.Row{{sql.NewOkResult(4)}}, SelectQuery: "SELECT * FROM mytable WHERE i IN (1,2) ORDER BY i", ExpectedSelect: []sql.Row{{int64(1), "martial"}, {int64(2), "partial"}}, }, { WriteQuery: "INSERT INTO mytable (i,s) values (1,'maybe') ON DUPLICATE KEY UPDATE i=VALUES(i)+8000, s=VALUES(s)", ExpectedWriteResult: []sql.Row{{sql.NewOkResult(2)}}, SelectQuery: "SELECT * FROM mytable WHERE i = 8001", ExpectedSelect: []sql.Row{{int64(8001), "maybe"}}, }, { WriteQuery: "INSERT INTO auto_increment_tbl (c0) values (44)", ExpectedWriteResult: []sql.Row{{sql.NewOkResult(1)}}, SelectQuery: "SELECT * FROM auto_increment_tbl ORDER BY pk", ExpectedSelect: []sql.Row{ {1, 11}, {2, 22}, {3, 33}, {4, 44}, }, }, { WriteQuery: "INSERT INTO auto_increment_tbl (c0) values (44),(55)", ExpectedWriteResult: []sql.Row{{sql.NewOkResult(2)}}, SelectQuery: "SELECT * FROM auto_increment_tbl ORDER BY pk", ExpectedSelect: []sql.Row{ {1, 11}, {2, 22}, {3, 33}, {4, 44}, {5, 55}, }, }, { WriteQuery: "INSERT INTO auto_increment_tbl values (NULL, 44)", ExpectedWriteResult: []sql.Row{{sql.NewOkResult(1)}}, SelectQuery: "SELECT * FROM auto_increment_tbl ORDER BY pk", ExpectedSelect: []sql.Row{ {1, 11}, {2, 22}, {3, 33}, {4, 44}, }, }, { WriteQuery: "INSERT INTO auto_increment_tbl values (0, 44)", ExpectedWriteResult: []sql.Row{{sql.NewOkResult(1)}}, SelectQuery: "SELECT * FROM auto_increment_tbl ORDER BY pk", ExpectedSelect: []sql.Row{ {1, 11}, {2, 22}, {3, 33}, {4, 44}, }, }, { WriteQuery: "INSERT INTO auto_increment_tbl values (5, 44)", ExpectedWriteResult: []sql.Row{{sql.NewOkResult(1)}}, SelectQuery: "SELECT * FROM auto_increment_tbl ORDER BY pk", ExpectedSelect: []sql.Row{ {1, 11}, {2, 22}, {3, 33}, {5, 44}, }, }, { WriteQuery: "INSERT INTO auto_increment_tbl values " + "(NULL, 44), (NULL, 55), (9, 99), (NULL, 110), (NULL, 121)", ExpectedWriteResult: []sql.Row{{sql.NewOkResult(5)}}, SelectQuery: "SELECT * FROM auto_increment_tbl ORDER BY pk", ExpectedSelect: []sql.Row{ {1, 11}, {2, 22}, {3, 33}, {4, 44}, {5, 55}, {9, 99}, {10, 110}, {11, 121}, }, }, }
var InsertScripts = []ScriptTest{ { Name: "insert into sparse auto_increment table", SetUpScript: []string{ "create table auto (pk int primary key auto_increment)", "insert into auto values (10), (20), (30)", "insert into auto values (NULL)", "insert into auto values (40)", "insert into auto values (0)", }, Assertions: []ScriptTestAssertion{ { Query: "select * from auto order by 1", Expected: []sql.Row{ {10}, {20}, {30}, {31}, {40}, {41}, }, }, }, }, { Name: "auto increment table handles deletes", SetUpScript: []string{ "create table auto (pk int primary key auto_increment)", "insert into auto values (10)", "delete from auto where pk = 10", "insert into auto values (NULL)", }, Assertions: []ScriptTestAssertion{ { Query: "select * from auto order by 1", Expected: []sql.Row{ {11}, }, }, }, }, { Name: "create auto_increment table with out-of-line primary key def", SetUpScript: []string{ `create table auto ( pk int auto_increment, c0 int, primary key(pk) );`, "insert into auto values (NULL,10), (NULL,20), (NULL,30)", }, Assertions: []ScriptTestAssertion{ { Query: "select * from auto order by 1", Expected: []sql.Row{ {1, 10}, {2, 20}, {3, 30}, }, }, }, }, { Name: "alter auto_increment value", SetUpScript: []string{ `create table auto ( pk int auto_increment, c0 int, primary key(pk) );`, "insert into auto values (NULL,10), (NULL,20), (NULL,30)", "alter table auto auto_increment 9;", "insert into auto values (NULL,90)", }, Assertions: []ScriptTestAssertion{ { Query: "select * from auto order by 1", Expected: []sql.Row{ {1, 10}, {2, 20}, {3, 30}, {9, 90}, }, }, }, }, { Name: "alter auto_increment value to float", SetUpScript: []string{ `create table auto ( pk int auto_increment, c0 int, primary key(pk) );`, "insert into auto values (NULL,10), (NULL,20), (NULL,30)", "alter table auto auto_increment = 19.9;", "insert into auto values (NULL,190)", }, Assertions: []ScriptTestAssertion{ { Query: "select * from auto order by 1", Expected: []sql.Row{ {1, 10}, {2, 20}, {3, 30}, {19, 190}, }, }, }, }, { Name: "auto increment on tinyint", SetUpScript: []string{ "create table auto (pk tinyint primary key auto_increment)", "insert into auto values (NULL),(10),(0)", }, Assertions: []ScriptTestAssertion{ { Query: "select * from auto order by 1", Expected: []sql.Row{ {1}, {10}, {11}, }, }, }, }, { Name: "auto increment on smallint", SetUpScript: []string{ "create table auto (pk smallint primary key auto_increment)", "insert into auto values (NULL),(10),(0)", }, Assertions: []ScriptTestAssertion{ { Query: "select * from auto order by 1", Expected: []sql.Row{ {1}, {10}, {11}, }, }, }, }, { Name: "auto increment on mediumint", SetUpScript: []string{ "create table auto (pk mediumint primary key auto_increment)", "insert into auto values (NULL),(10),(0)", }, Assertions: []ScriptTestAssertion{ { Query: "select * from auto order by 1", Expected: []sql.Row{ {1}, {10}, {11}, }, }, }, }, { Name: "auto increment on int", SetUpScript: []string{ "create table auto (pk int primary key auto_increment)", "insert into auto values (NULL),(10),(0)", }, Assertions: []ScriptTestAssertion{ { Query: "select * from auto order by 1", Expected: []sql.Row{ {1}, {10}, {11}, }, }, }, }, { Name: "auto increment on bigint", SetUpScript: []string{ "create table auto (pk bigint primary key auto_increment)", "insert into auto values (NULL),(10),(0)", }, Assertions: []ScriptTestAssertion{ { Query: "select * from auto order by 1", Expected: []sql.Row{ {1}, {10}, {11}, }, }, }, }, { Name: "auto increment on tinyint unsigned", SetUpScript: []string{ "create table auto (pk tinyint unsigned primary key auto_increment)", "insert into auto values (NULL),(10),(0)", }, Assertions: []ScriptTestAssertion{ { Query: "select * from auto order by 1", Expected: []sql.Row{ {uint64(1)}, {uint64(10)}, {uint64(11)}, }, }, }, }, { Name: "auto increment on smallint unsigned", SetUpScript: []string{ "create table auto (pk smallint unsigned primary key auto_increment)", "insert into auto values (NULL),(10),(0)", }, Assertions: []ScriptTestAssertion{ { Query: "select * from auto order by 1", Expected: []sql.Row{ {uint64(1)}, {uint64(10)}, {uint64(11)}, }, }, }, }, { Name: "auto increment on mediumint unsigned", SetUpScript: []string{ "create table auto (pk mediumint unsigned primary key auto_increment)", "insert into auto values (NULL),(10),(0)", }, Assertions: []ScriptTestAssertion{ { Query: "select * from auto order by 1", Expected: []sql.Row{ {uint64(1)}, {uint64(10)}, {uint64(11)}, }, }, }, }, { Name: "auto increment on int unsigned", SetUpScript: []string{ "create table auto (pk int unsigned primary key auto_increment)", "insert into auto values (NULL),(10),(0)", }, Assertions: []ScriptTestAssertion{ { Query: "select * from auto order by 1", Expected: []sql.Row{ {uint64(1)}, {uint64(10)}, {uint64(11)}, }, }, }, }, { Name: "auto increment on bigint unsigned", SetUpScript: []string{ "create table auto (pk bigint unsigned primary key auto_increment)", "insert into auto values (NULL),(10),(0)", }, Assertions: []ScriptTestAssertion{ { Query: "select * from auto order by 1", Expected: []sql.Row{ {uint64(1)}, {uint64(10)}, {uint64(11)}, }, }, }, }, { Name: "auto increment on float", SetUpScript: []string{ "create table auto (pk float primary key auto_increment)", "insert into auto values (NULL),(10),(0)", }, Assertions: []ScriptTestAssertion{ { Query: "select * from auto order by 1", Expected: []sql.Row{ {float64(1)}, {float64(10)}, {float64(11)}, }, }, }, }, { Name: "auto increment on double", SetUpScript: []string{ "create table auto (pk double primary key auto_increment)", "insert into auto values (NULL),(10),(0)", }, Assertions: []ScriptTestAssertion{ { Query: "select * from auto order by 1", Expected: []sql.Row{ {float64(1)}, {float64(10)}, {float64(11)}, }, }, }, }, }
var JsonScripts = []ScriptTest{ { Name: "JSON_ARRAYAGG on one column", SetUpScript: []string{ "create table t (o_id int)", "INSERT INTO t VALUES (1),(2)", }, Assertions: []ScriptTestAssertion{ { Query: "SELECT JSON_ARRAYAGG(o_id) FROM t", Expected: []sql.Row{ { sql.MustJSON(`[1,2]`), }, }, }, }, }, { Name: "Simple JSON_ARRAYAGG on two columns", SetUpScript: []string{ "create table t (o_id int, attribute longtext)", "INSERT INTO t VALUES (2, 'color'), (2, 'fabric')", }, Assertions: []ScriptTestAssertion{ { Query: "SELECT JSON_ARRAYAGG(o_id), JSON_ARRAYAGG(attribute) FROM t", Expected: []sql.Row{ { sql.MustJSON(`[2,2]`), sql.MustJSON(`["color","fabric"]`), }, }, }, }, }, { Name: "JSON_ARRAYAGG on column with string values w/ groupby", SetUpScript: []string{ "create table t (o_id int, attribute longtext, value longtext)", "INSERT INTO t VALUES (2, 'color', 'red'), (2, 'fabric', 'silk')", }, Assertions: []ScriptTestAssertion{ { Query: "SELECT o_id, JSON_ARRAYAGG(attribute) FROM t GROUP BY o_id", Expected: []sql.Row{ { 2, sql.MustJSON(`["color","fabric"]`), }, }, }, { Query: "SELECT o_id, JSON_ARRAYAGG(value) FROM t GROUP BY o_id", Expected: []sql.Row{ { 2, sql.MustJSON(`["red","silk"]`), }, }, }, }, }, { Name: "JSON_ARRAYAGG on column with int values w/ groupby", SetUpScript: []string{ "create table t2 (o_id int, val int)", "INSERT INTO t2 VALUES (1,1), (1,2), (1,3)", }, Assertions: []ScriptTestAssertion{ { Query: "SELECT o_id, JSON_ARRAYAGG(val) FROM t2 GROUP BY o_id", Expected: []sql.Row{ { 1, sql.MustJSON(`[1,2,3]`), }, }, }, }, }, { Name: "JSON_ARRAYAGG on unknown column throws error", SetUpScript: []string{ "create table t2 (o_id int, val int)", "INSERT INTO t2 VALUES (1,1), (1,2), (1,3)", }, Assertions: []ScriptTestAssertion{ { Query: "SELECT o_id, JSON_ARRAYAGG(val2) FROM t2 GROUP BY o_id", ExpectedErr: sql.ErrColumnNotFound, }, }, }, { Name: "JSON_ARRAYAGG on column with no rows returns NULL", SetUpScript: []string{ "create table t2 (o_id int)", }, Assertions: []ScriptTestAssertion{ { Query: "SELECT JSON_ARRAYAGG(o_id) FROM t2", Expected: []sql.Row{ { sql.MustJSON(`[]`), }, }, }, }, }, { Name: "JSON_ARRAYAGG on row with 1 value, 1 null is fine", SetUpScript: []string{ "create table x(pk int, c1 int)", "INSERT INTO x VALUES (1,NULL)", }, Assertions: []ScriptTestAssertion{ { Query: "SELECT pk, JSON_ARRAYAGG(c1) FROM x GROUP BY pk", Expected: []sql.Row{ { 1, sql.MustJSON(`[null]`), }, }, }, { Query: "SELECT JSON_ARRAYAGG(c1) FROM x", Expected: []sql.Row{ { sql.MustJSON(`[null]`), }, }, }, }, }, { Name: "JSON_ARRAYAGG and group by use the same field.", SetUpScript: []string{ "create table x(pk int, c1 int)", "INSERT INTO x VALUES (1, 1)", "INSERT INTO x VALUES (1, 2)", "INSERT INTO x VALUES (2, 3)", "INSERT INTO x VALUES (2, 3)", "INSERT INTO x VALUES (3, 5)", }, Assertions: []ScriptTestAssertion{ { Query: "SELECT JSON_ARRAYAGG(pk) FROM x GROUP BY pk", Expected: []sql.Row{ { sql.MustJSON(`[1,1]`), }, { sql.MustJSON(`[2,2]`), }, { sql.MustJSON(`[3]`), }, }, }, }, }, { Name: "JSON_ARRAGG with simple and nested json objects.", SetUpScript: []string{ "create table j(field JSON)", `INSERT INTO j VALUES('{"key1": {"key": "value"}}')`, `INSERT INTO j VALUES('{"key1": "value1", "key2": "value2"}')`, `INSERT INTO j VALUES('{"key1": {"key": [2,3]}}')`, `INSERT INTO j VALUES('["a", 1]')`, }, Assertions: []ScriptTestAssertion{ { Query: "SELECT JSON_ARRAYAGG(field) FROM j GROUP BY field", Expected: []sql.Row{ { sql.MustJSON(`[{"key1": "value1", "key2": "value2"}]`), }, { sql.MustJSON(`[{"key1": {"key": "value"}}]`), }, { sql.MustJSON(`[{"key1":{"key":[2,3]}}]`), }, { sql.MustJSON(`[["a",1]]`), }, }, }, }, }, { Name: "Simple JSON_OBJECTAGG with GROUP BY", SetUpScript: []string{ "create table t2 (o_id int, val int)", "INSERT INTO t2 VALUES (1,1), (1,2), (1,3)", }, Assertions: []ScriptTestAssertion{ { Query: "SELECT JSON_OBJECTAGG(o_id, val) FROM t2 GROUP BY o_id", Expected: []sql.Row{ { sql.MustJSON(`{"1": 3}`), }, }, }, }, }, { Name: "More complex JSON_OBJECTAGG WITH GROUP BY", SetUpScript: []string{ "create table t (o_id int, attribute longtext, value longtext)", "INSERT INTO t VALUES (2, 'color', 'red'), (2, 'fabric', 'silk')", "INSERT INTO t VALUES (3, 'color', 'green'), (3, 'shape', 'square')", }, Assertions: []ScriptTestAssertion{ { Query: `SELECT o_id, JSON_OBJECTAGG(attribute, value) FROM t GROUP BY o_id`, Expected: []sql.Row{ { 2, sql.MustJSON(`{"color": "red", "fabric": "silk"}`), }, { 3, sql.MustJSON(`{"color": "green", "shape": "square"}`), }, }, }, { Query: `SELECT o_id, JSON_OBJECTAGG(o_id, value) FROM t GROUP BY o_id`, Expected: []sql.Row{ { 2, sql.MustJSON(`{"2": "silk"}`), }, { 3, sql.MustJSON(`{"3": "square"}`), }, }, }, }, }, { Name: "3 column table that uses JSON_OBJECTAGG without groupby", SetUpScript: []string{ "create table t (o_id int, attribute longtext, value longtext)", "INSERT INTO t VALUES (2, 'color', 'red'), (2, 'fabric', 'silk')", "INSERT INTO t VALUES (3, 'color', 'green'), (3, 'shape', 'square')", }, Assertions: []ScriptTestAssertion{ { Query: `select JSON_OBJECTAGG(o_id, value) from t`, Expected: []sql.Row{ { sql.MustJSON(`{"2": "silk", "3": "square"}`), }, }, }, { Query: `select JSON_OBJECTAGG(attribute, value) from t`, Expected: []sql.Row{ { sql.MustJSON(`{"color": "green", "fabric": "silk", "shape": "square"}`), }, }, }, }, }, { Name: "JSON_OBJECTAGG and null values", SetUpScript: []string{ `create table test (pk int primary key, val longtext)`, `insert into test values (1, NULL)`, }, Assertions: []ScriptTestAssertion{ { Query: `SELECT JSON_OBJECTAGG(pk, val) from test`, Expected: []sql.Row{ { sql.MustJSON(`{"1": null}`), }, }, }, }, }, { Name: "JSON_OBJECTAGG and nested json values", SetUpScript: []string{ "create table j(pk int, val JSON)", `INSERT INTO j VALUES(1, '{"key1": "value1", "key2": "value2"}')`, `INSERT INTO j VALUES(1, '{"key1": {"key": [2,3]}}')`, `INSERT INTO j VALUES(2, '["a", 1]')`, }, Assertions: []ScriptTestAssertion{ { Query: `SELECT JSON_OBJECTAGG(pk, val) from j`, Expected: []sql.Row{ { sql.MustJSON(`{"1": {"key1": {"key": [2, 3]}}, "2": ["a", 1]}`), }, }, }, }, }, { Name: "JSON_OBJECTAGG correctly returns null when no rows are present", SetUpScript: []string{ `create table test (pk int primary key, val longtext)`, }, Assertions: []ScriptTestAssertion{ { Query: `SELECT JSON_OBJECTAGG(pk, val) from test`, Expected: []sql.Row{ { nil, }, }, }, }, }, { Name: "JSON_OBJECTAGG handles errors appropriately", SetUpScript: []string{ `create table test (pk int, val longtext)`, `insert into test values (1, NULL)`, `insert into test values (NULL, 1)`, }, Assertions: []ScriptTestAssertion{ { Query: `SELECT JSON_OBJECTAGG(pk, notval) from test`, ExpectedErr: sql.ErrColumnNotFound, }, { Query: `SELECT JSON_OBJECTAGG(notpk, val) from test`, ExpectedErr: sql.ErrColumnNotFound, }, { Query: `SELECT JSON_OBJECTAGG(pk, val) from nottest`, ExpectedErr: sql.ErrTableNotFound, }, { Query: `SELECT JSON_OBJECTAGG(pk, val, badarg) from test`, ExpectedErr: sql.ErrInvalidArgumentNumber, }, { Query: `SELECT JSON_OBJECTAGG(pk) from test`, ExpectedErr: sql.ErrInvalidArgumentNumber, }, { Query: `SELECT JSON_OBJECTAGG(pk, val) from test`, ExpectedErr: sql.ErrJSONObjectAggNullKey, }, }, }, }
var KeylessQueries = []QueryTest{ { Query: "SELECT * FROM keyless ORDER BY c0", Expected: []sql.Row{ {0, 0}, {1, 1}, {1, 1}, {2, 2}, }, }, { Query: "SELECT * FROM keyless ORDER BY c1 DESC", Expected: []sql.Row{ {2, 2}, {1, 1}, {1, 1}, {0, 0}, }, }, { Query: "SELECT * FROM keyless JOIN myTable where c0 = i", Expected: []sql.Row{ {1, 1, 1, "first row"}, {1, 1, 1, "first row"}, {2, 2, 2, "second row"}, }, }, { Query: "SELECT * FROM myTable JOIN keyless WHERE i = c0 ORDER BY i", Expected: []sql.Row{ {1, "first row", 1, 1}, {1, "first row", 1, 1}, {2, "second row", 2, 2}, }, }, { Query: "DESCRIBE keyless", Expected: []sql.Row{ {"c0", "bigint", "YES", "", "", ""}, {"c1", "bigint", "YES", "", "", ""}, }, }, { Query: "SHOW COLUMNS FROM keyless", Expected: []sql.Row{ {"c0", "bigint", "YES", "", "", ""}, {"c1", "bigint", "YES", "", "", ""}, }, }, { Query: "SHOW FULL COLUMNS FROM keyless", Expected: []sql.Row{ {"c0", "bigint", nil, "YES", "", "", "", "", ""}, {"c1", "bigint", nil, "YES", "", "", "", "", ""}, }, }, { Query: "SHOW CREATE TABLE keyless", Expected: []sql.Row{ {"keyless", "CREATE TABLE `keyless` (\n `c0` bigint,\n `c1` bigint\n) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4"}, }, }, }
var LoadDataErrorScripts = []ScriptTest{ { Name: "Load data without secure file throws error.", SetUpScript: []string{ "create table loadtable(pk longtext primary key, c1 int)", }, Assertions: []ScriptTestAssertion{ { Query: "LOAD DATA INFILE '/x/ytx' INTO TABLE loadtable", ExpectedErr: sql.ErrSecureFileDirNotSet, }, }, }, { Name: "Load data into table that doesn't exist throws error.", Query: "LOAD DATA INFILE 'test1.txt' INTO TABLE loadtable", ExpectedErr: sql.ErrTableNotFound, }, { Name: "Load data with unknown files throws an error.", SetUpScript: []string{ "create table loadtable(pk longtext primary key, c1 int)", "SET secure_file_priv='./testdata'", }, Assertions: []ScriptTestAssertion{ { Query: "LOAD DATA INFILE '/x/ytx' INTO TABLE loadtable", ExpectedErr: sql.ErrLoadDataCannotOpen, }, }, }, { Name: "Load data with unknown columns throws an error", SetUpScript: []string{ "create table loadtable(pk int primary key)", "SET secure_file_priv='./testdata'", }, Assertions: []ScriptTestAssertion{ { Query: "LOAD DATA INFILE './testdata/test1.txt' INTO TABLE loadtable FIELDS ENCLOSED BY '\"' (bad)", ExpectedErr: plan.ErrInsertIntoNonexistentColumn, }, }, }, { Name: "Load data escaped by terms longer than 1 character throws an error", SetUpScript: []string{ "create table loadtable(pk int primary key)", "SET secure_file_priv='./testdata'", }, Assertions: []ScriptTestAssertion{ { Query: "LOAD DATA INFILE './testdata/test1.txt' INTO TABLE loadtable FIELDS ESCAPED BY 'xx' (pk)", ExpectedErr: sql.ErrLoadDataCharacterLength, }, }, }, { Name: "Load data enclosed by term longer than 1 character throws an error", SetUpScript: []string{ "create table loadtable(pk int primary key)", "SET secure_file_priv='./testdata'", }, Assertions: []ScriptTestAssertion{ { Query: "LOAD DATA INFILE './testdata/test1.txt' INTO TABLE loadtable FIELDS ENCLOSED BY 'xx' (pk)", ExpectedErr: sql.ErrLoadDataCharacterLength, }, }, }, }
var LoadDataFailingScripts = []ScriptTest{ { Name: "Escaped values are correctly parsed.", SetUpScript: []string{ "create table loadtable(pk longtext)", "SET secure_file_priv='./testdata'", "LOAD DATA INFILE 'test5.txt' INTO TABLE loadtable FIELDS ENCLOSED BY '\"' IGNORE 1 LINES", }, Assertions: []ScriptTestAssertion{ { Query: "select * from loadtable", Expected: []sql.Row{{"hi"}, {"hello"}, {nil}, {"TryN"}, {fmt.Sprintf("%c", 26)}, {fmt.Sprintf("%c", 0)}, {"new\n"}}, }, }, }, { Name: "Load and terminate have the same values.", SetUpScript: []string{ "create table loadtable(pk int primary key)", "SET secure_file_priv='./testdata'", "LOAD DATA INFILE 'test1.txt' INTO TABLE loadtable FIELDS TERMINATED BY '\"' ENCLOSED BY '\"'", }, Assertions: []ScriptTestAssertion{ { Query: "select * from loadtable", Expected: []sql.Row{{int8(1)}, {int8(2)}, {int8(3)}, {int8(4)}}, }, }, }, { Name: "Loading value into different column type results in default value.", SetUpScript: []string{ "create table loadtable(pk longtext, c1 int)", "SET secure_file_priv='./testdata'", "LOAD DATA INFILE 'test4.txt' INTO TABLE loadtable FIELDS ENCLOSED BY '\"' (c1)", }, Assertions: []ScriptTestAssertion{ { Query: "select * from loadtable", Expected: []sql.Row{{nil, 0}, {nil, 0}}, }, }, }, { Name: "LOAD DATA handles nulls", SetUpScript: []string{ "create table loadtable(pk longtext, c1 int)", "SET secure_file_priv='./testdata'", "LOAD DATA INFILE 'test4.txt' INTO TABLE loadtable FIELDS ENCLOSED BY '\"'", }, Assertions: []ScriptTestAssertion{ { Query: "select * from loadtable", Expected: []sql.Row{{"hi", 1}, {"hello", nil}}, }, }, }, { Name: "LOAD DATA can handle a differing column order", SetUpScript: []string{ "create table loadtable(pk int, c1 string) ", "SET secure_file_priv='./testdata'", "LOAD DATA INFILE 'test4.txt' INTO TABLE loadtable FIELDS ENCLOSED BY '\"' (c1, pk)", }, Assertions: []ScriptTestAssertion{ { Query: "select * from loadtable", Expected: []sql.Row{{1, "hi"}, {nil, "hello"}}, }, }, }, }
var LoadDataScripts = []ScriptTest{ { Name: "Basic load data with enclosed values.", SetUpScript: []string{ "create table loadtable(pk int primary key)", "SET secure_file_priv='./testdata'", "LOAD DATA INFILE 'test1.txt' INTO TABLE loadtable FIELDS ENCLOSED BY '\"'", }, Assertions: []ScriptTestAssertion{ { Query: "select * from loadtable", Expected: []sql.Row{{int8(1)}, {int8(2)}, {int8(3)}, {int8(4)}}, }, }, }, { Name: "Load data with csv", SetUpScript: []string{ "create table loadtable(pk int primary key, c1 longtext)", "SET secure_file_priv='./testdata'", "LOAD DATA INFILE 'test2.csv' INTO TABLE loadtable FIELDS TERMINATED BY ',' IGNORE 1 LINES", }, Assertions: []ScriptTestAssertion{ { Query: "select * from loadtable", Expected: []sql.Row{{int8(1), "hi"}, {int8(2), "hello"}}, }, }, }, { Name: "Load data with csv with prefix.", SetUpScript: []string{ "create table loadtable(pk longtext primary key, c1 int)", "SET secure_file_priv='./testdata'", "LOAD DATA INFILE 'test3.csv' INTO TABLE loadtable FIELDS TERMINATED BY ',' LINES STARTING BY 'xxx' IGNORE 1 LINES (`pk`, `c1`)", }, Assertions: []ScriptTestAssertion{ { Query: "select * from loadtable", Expected: []sql.Row{{"\"abc\"", int8(1)}, {"\"def\"", int8(2)}, {"\"hello\"", nil}}, }, }, }, { Name: "Table has more columns than import.", SetUpScript: []string{ "create table loadtable(pk int primary key, c1 int)", "SET secure_file_priv='./testdata'", "LOAD DATA INFILE 'test1.txt' INTO TABLE loadtable FIELDS ENCLOSED BY '\"'", }, Assertions: []ScriptTestAssertion{ { Query: "select * from loadtable ORDER BY pk", Expected: []sql.Row{{1, nil}, {2, nil}, {3, nil}, {4, nil}}, }, }, }, }
var PlanTests = []QueryPlanTest{}/* 102 elements not displayed */
QueryPlanTest is a test of generating the right query plans for different queries in the presence of indexes and other features. These tests are fragile because they rely on string representations of query plans, but they're much easier to construct this way.
var ProcedureCallTests = []ScriptTest{ { Name: "OUT param with SET", SetUpScript: []string{ "SET @outparam = 5", "CREATE PROCEDURE testabc(OUT x BIGINT) SET x = 9", "CALL testabc(@outparam)", }, Assertions: []ScriptTestAssertion{ { Query: "SELECT @outparam", Expected: []sql.Row{ { int64(9), }, }, }, }, }, { Name: "OUT param without SET", SetUpScript: []string{ "SET @outparam = 5", "CREATE PROCEDURE testabc(OUT x BIGINT) SELECT x", "CALL testabc(@outparam)", }, Assertions: []ScriptTestAssertion{ { Query: "SELECT @outparam", Expected: []sql.Row{ { nil, }, }, }, }, }, { Name: "INOUT param with SET", SetUpScript: []string{ "SET @outparam = 5", "CREATE PROCEDURE testabc(INOUT x BIGINT) BEGIN SET x = x + 1; SET x = x + 3; END;", "CALL testabc(@outparam)", }, Assertions: []ScriptTestAssertion{ { Query: "SELECT @outparam", Expected: []sql.Row{ { int64(9), }, }, }, }, }, { Name: "INOUT param without SET", SetUpScript: []string{ "SET @outparam = 5", "CREATE PROCEDURE testabc(INOUT x BIGINT) SELECT x", "CALL testabc(@outparam)", }, Assertions: []ScriptTestAssertion{ { Query: "SELECT @outparam", Expected: []sql.Row{ { int64(5), }, }, }, }, }, { Name: "Nested CALL with INOUT param", SetUpScript: []string{ "SET @outparam = 5", "CREATE PROCEDURE p3(INOUT z INT) BEGIN SET z = z * 111; END;", "CREATE PROCEDURE p2(INOUT y DOUBLE) BEGIN SET y = y + 4; CALL p3(y); END;", "CREATE PROCEDURE p1(INOUT x BIGINT) BEGIN SET x = 3; CALL p2(x); END;", "CALL p1(@outparam)", }, Assertions: []ScriptTestAssertion{ { Query: "SELECT @outparam", Expected: []sql.Row{ { int64(777), }, }, }, }, }, { Name: "OUT param without SET", SetUpScript: []string{ "SET @outparam = 5", "CREATE PROCEDURE testabc(OUT x BIGINT) SELECT x", "CALL testabc(@outparam)", }, Assertions: []ScriptTestAssertion{ { Query: "SELECT @outparam", Expected: []sql.Row{ { nil, }, }, }, }, }, { Name: "Incompatible type for parameter", SetUpScript: []string{ "CREATE PROCEDURE p1(x DATETIME) SELECT x", }, Assertions: []ScriptTestAssertion{ { Query: "CALL p1('hi')", ExpectedErr: sql.ErrConvertingToTime, }, }, }, { Name: "Incorrect parameter count", SetUpScript: []string{ "CREATE PROCEDURE p1(x BIGINT, y BIGINT) SELECT x + y", }, Assertions: []ScriptTestAssertion{ { Query: "CALL p1(1)", ExpectedErr: sql.ErrCallIncorrectParameterCount, }, { Query: "CALL p1(1, 2, 3)", ExpectedErr: sql.ErrCallIncorrectParameterCount, }, }, }, }
var ProcedureDropTests = []ScriptTest{ { Name: "DROP procedures", SetUpScript: []string{ "CREATE PROCEDURE p1() SELECT 5", "CREATE PROCEDURE p2() SELECT 6", }, Assertions: []ScriptTestAssertion{ { Query: "CALL p1", Expected: []sql.Row{ { int64(5), }, }, }, { Query: "CALL p2", Expected: []sql.Row{ { int64(6), }, }, }, { Query: "DROP PROCEDURE p1", Expected: []sql.Row{}, }, { Query: "CALL p1", ExpectedErr: sql.ErrStoredProcedureDoesNotExist, }, { Query: "DROP PROCEDURE IF EXISTS p2", Expected: []sql.Row{}, }, { Query: "CALL p2", ExpectedErr: sql.ErrStoredProcedureDoesNotExist, }, { Query: "DROP PROCEDURE p3", ExpectedErr: sql.ErrStoredProcedureDoesNotExist, }, { Query: "DROP PROCEDURE IF EXISTS p4", Expected: []sql.Row{}, }, }, }, }
var ProcedureLogicTests = []ScriptTest{ { Name: "Simple SELECT", SetUpScript: []string{ "CREATE PROCEDURE testabc(x DOUBLE, y DOUBLE) SELECT x*y", }, Assertions: []ScriptTestAssertion{ { Query: "CALL testabc(2, 3)", Expected: []sql.Row{ { float64(6), }, }, }, { Query: "CALL testabc(9, 9.5)", Expected: []sql.Row{ { float64(85.5), }, }, }, }, }, { Name: "Multiple SELECTs", SetUpScript: []string{ "CREATE TABLE t1(pk VARCHAR(20) PRIMARY KEY)", "INSERT INTO t1 VALUES (3), (4), (50)", `CREATE PROCEDURE p1() BEGIN SELECT * FROM t1; UPDATE t1 SET pk = CONCAT(pk, '0'); SELECT * FROM t1; INSERT INTO t1 VALUES (1), (2); SELECT * FROM t1; REPLACE INTO t1 VALUES (1), (30); DELETE FROM t1 WHERE pk LIKE '%00'; END;`, }, Assertions: []ScriptTestAssertion{ { Query: "CALL p1()", Expected: []sql.Row{ {"1"}, {"2"}, {"30"}, {"40"}, {"500"}, }, }, { Query: "SELECT * FROM t1 ORDER BY 1", Expected: []sql.Row{ {"1"}, {"2"}, {"30"}, {"40"}, }, }, }, }, { Name: "IF/ELSE with 1 SELECT at end", SetUpScript: []string{ "SET @outparam = ''", `CREATE PROCEDURE p1(OUT s VARCHAR(200), N DOUBLE, m DOUBLE) BEGIN SET s = ''; IF n = m THEN SET s = 'equals'; ELSE IF n > m THEN SET s = 'greater'; ELSE SET s = 'less'; END IF; SET s = CONCAT('is ', s, ' than'); END IF; SET s = CONCAT(n, ' ', s, ' ', m, '.'); SELECT s; END;`, `CREATE PROCEDURE p2(s VARCHAR(200), N DOUBLE, m DOUBLE) BEGIN SET s = ''; IF n = m THEN SET s = 'equals'; ELSE IF n > m THEN SET s = 'greater'; ELSE SET s = 'less'; END IF; SET s = CONCAT('is ', s, ' than'); END IF; SET s = CONCAT(n, ' ', s, ' ', m, '.'); SELECT s; END;`, }, Assertions: []ScriptTestAssertion{ { Query: "CALL p1(@outparam, 1, 2)", Expected: []sql.Row{ { "1 is less than 2.", }, }, }, { Query: "SELECT @outparam", Expected: []sql.Row{ { "1 is less than 2.", }, }, }, { Query: "CALL p1(@outparam, 7, 4)", Expected: []sql.Row{ { "7 is greater than 4.", }, }, }, { Query: "SELECT @outparam", Expected: []sql.Row{ { "7 is greater than 4.", }, }, }, { Query: "CALL p1(@outparam, 5, 5)", Expected: []sql.Row{ { "5 equals 5.", }, }, }, { Query: "SELECT @outparam", Expected: []sql.Row{ { "5 equals 5.", }, }, }, { Query: "CALL p2(@outparam, 9, 3)", Expected: []sql.Row{ { "9 is greater than 3.", }, }, }, { Query: "SELECT @outparam", Expected: []sql.Row{ { "5 equals 5.", }, }, }, }, }, { Name: "IF/ELSE with nested SELECT in branches", SetUpScript: []string{ "CREATE TABLE t1(pk BIGINT PRIMARY KEY)", `CREATE PROCEDURE p1(x BIGINT) BEGIN DELETE FROM t1; IF x < 10 THEN IF x = 0 THEN SELECT 1000; ELSEIF x = 1 THEN SELECT 1001; ELSE INSERT INTO t1 VALUES (3), (4), (5); END IF; ELSEIF x < 20 THEN IF x = 10 THEN INSERT INTO t1 VALUES (1), (2), (6), (7); ELSEIF x = 11 THEN INSERT INTO t1 VALUES (8), (9), (10), (11), (12); SELECT * FROM t1; ELSE SELECT 2002; SELECT 2003; END IF; END IF; INSERT INTO t1 VALUES (1), (2); END;`, }, Assertions: []ScriptTestAssertion{ { Query: "CALL p1(0)", Expected: []sql.Row{ { int64(1000), }, }, }, { Query: "CALL p1(1)", Expected: []sql.Row{ { int64(1001), }, }, }, { Query: "CALL p1(2)", Expected: []sql.Row{ { sql.NewOkResult(2), }, }, }, { Query: "CALL p1(10)", ExpectedErr: sql.ErrPrimaryKeyViolation, }, { Query: "CALL p1(11)", Expected: []sql.Row{ {int64(8)}, {int64(9)}, {int64(10)}, {int64(11)}, {int64(12)}, }, }, { Query: "CALL p1(12)", Expected: []sql.Row{ { int64(2003), }, }, }, }, }, { Name: "SELECT with JOIN and table aliases", SetUpScript: []string{ "CREATE TABLE foo(a BIGINT PRIMARY KEY, b VARCHAR(20))", "INSERT INTO foo VALUES (1, 'd'), (2, 'e'), (3, 'f')", "CREATE TABLE bar(b VARCHAR(30) PRIMARY KEY, c BIGINT)", "INSERT INTO bar VALUES ('x', 3), ('y', 2), ('z', 1)", "CREATE PROCEDURE p1() SELECT f.a, bar.b, f.b FROM foo f INNER JOIN bar ON f.a = bar.c ORDER BY 1", "CREATE PROCEDURE p2() BEGIN SELECT f.a, bar.b, f.b FROM foo f INNER JOIN bar ON f.a = bar.c ORDER BY 1; END;", "CREATE PROCEDURE p3() IF 0 = 0 THEN SELECT f.a, bar.b, f.b FROM foo f INNER JOIN bar ON f.a = bar.c ORDER BY 1; END IF;", "CREATE PROCEDURE p4() BEGIN SELECT 7; SELECT f.a, bar.b, f.b FROM foo f INNER JOIN bar ON f.a = bar.c ORDER BY 1; END;", "CREATE PROCEDURE p5() IF 0 = 0 THEN SELECT 7; SELECT f.a, bar.b, f.b FROM foo f INNER JOIN bar ON f.a = bar.c ORDER BY 1; END IF;", }, Assertions: []ScriptTestAssertion{ { Query: "SELECT f.a, bar.b, f.b FROM foo f INNER JOIN bar ON f.a = bar.c ORDER BY 1", Expected: []sql.Row{ {int64(1), "z", "d"}, {int64(2), "y", "e"}, {int64(3), "x", "f"}, }, }, { Query: "CALL p1()", Expected: []sql.Row{ {int64(1), "z", "d"}, {int64(2), "y", "e"}, {int64(3), "x", "f"}, }, }, { Query: "CALL p2()", Expected: []sql.Row{ {int64(1), "z", "d"}, {int64(2), "y", "e"}, {int64(3), "x", "f"}, }, }, { Query: "CALL p3()", Expected: []sql.Row{ {int64(1), "z", "d"}, {int64(2), "y", "e"}, {int64(3), "x", "f"}, }, }, { Query: "CALL p4()", Expected: []sql.Row{ {int64(1), "z", "d"}, {int64(2), "y", "e"}, {int64(3), "x", "f"}, }, }, { Query: "CALL p5()", Expected: []sql.Row{ {int64(1), "z", "d"}, {int64(2), "y", "e"}, {int64(3), "x", "f"}, }, }, }, }, { Name: "Nested CALL in IF/ELSE branch", SetUpScript: []string{ "CREATE TABLE t1(pk BIGINT PRIMARY KEY)", "INSERT INTO t1 VALUES (2), (3)", "CREATE PROCEDURE p1(INOUT x BIGINT) BEGIN IF X = 1 THEN CALL p2(10); ELSEIF x = 2 THEN CALL p2(100); ELSE CALL p2(X); END IF; END;", "CREATE PROCEDURE p2(INOUT y BIGINT) BEGIN SELECT pk * y FROM t1 ORDER BY 1; END;", }, Assertions: []ScriptTestAssertion{ { Query: "CALL p1(1)", Expected: []sql.Row{ {int64(20)}, {int64(30)}, }, }, { Query: "CALL p1(2)", Expected: []sql.Row{ {int64(200)}, {int64(300)}, }, }, { Query: "CALL p1(5)", Expected: []sql.Row{ {int64(10)}, {int64(15)}, }, }, }, }, { Name: "INSERT INTO SELECT doesn't override SELECT", SetUpScript: []string{ "CREATE TABLE t1(pk BIGINT PRIMARY KEY)", "CREATE TABLE t2(pk BIGINT PRIMARY KEY)", "INSERT INTO t1 VALUES (2), (3)", "INSERT INTO t2 VALUES (1)", `CREATE PROCEDURE p1(x BIGINT) BEGIN DELETE FROM t2 WHERE pk > 1; INSERT INTO t2 SELECT pk FROM t1; SELECT * FROM t2; INSERT INTO t2 SELECT pk + 10 FROM t1; IF x = 1 THEN SELECT * FROM t2; END IF; END;`, }, Assertions: []ScriptTestAssertion{ { Query: "CALL p1(0)", Expected: []sql.Row{ {int64(1)}, {int64(2)}, {int64(3)}, }, }, { Query: "CALL p1(1)", Expected: []sql.Row{ {int64(1)}, {int64(2)}, {int64(3)}, {int64(12)}, {int64(13)}, }, }, }, }, { Name: "Subquery on SET user variable captures parameter", SetUpScript: []string{ `CREATE PROCEDURE p1(x VARCHAR(20)) BEGIN SET @randomvar = (SELECT LENGTH(x)); SELECT @randomvar; END;`, }, Assertions: []ScriptTestAssertion{ { Query: "CALL p1('hi')", Expected: []sql.Row{ {int64(2)}, }, }, { Query: "CALL p1('hello')", Expected: []sql.Row{ {int64(5)}, }, }, }, }, { Name: "DECLARE CONDITION", SetUpScript: []string{ `CREATE PROCEDURE p1(x INT) BEGIN DECLARE specialty CONDITION FOR SQLSTATE '45000'; DECLARE specialty2 CONDITION FOR SQLSTATE '02000'; IF x = 0 THEN SIGNAL SQLSTATE '01000'; ELSEIF x = 1 THEN SIGNAL SQLSTATE '45000' SET MESSAGE_TEXT = 'A custom error occurred 1'; ELSEIF x = 2 THEN SIGNAL specialty SET MESSAGE_TEXT = 'A custom error occurred 2', MYSQL_ERRNO = 1002; ELSEIF x = 3 THEN SIGNAL specialty; ELSEIF x = 4 THEN SIGNAL specialty2; ELSE SIGNAL SQLSTATE '01000' SET MESSAGE_TEXT = 'A warning occurred', MYSQL_ERRNO = 1000; SIGNAL SQLSTATE '45000' SET MESSAGE_TEXT = 'An error occurred', MYSQL_ERRNO = 1001; END IF; BEGIN DECLARE specialty3 CONDITION FOR SQLSTATE '45000'; END; END;`, }, Assertions: []ScriptTestAssertion{ { Query: "CALL p1(0)", ExpectedErrStr: "warnings not yet implemented", }, { Query: "CALL p1(1)", ExpectedErrStr: "A custom error occurred 1 (errno 1644) (sqlstate 45000)", }, { Query: "CALL p1(2)", ExpectedErrStr: "A custom error occurred 2 (errno 1002) (sqlstate 45000)", }, { Query: "CALL p1(3)", ExpectedErrStr: "Unhandled user-defined exception condition (errno 1644) (sqlstate 45000)", }, { Query: "CALL p1(4)", ExpectedErrStr: "Unhandled user-defined not found condition (errno 1643) (sqlstate 02000)", }, }, }, { Name: "DECLARE CONDITION nesting priority", SetUpScript: []string{ `CREATE PROCEDURE p1(x INT) BEGIN DECLARE cond_name CONDITION FOR SQLSTATE '02000'; BEGIN DECLARE cond_name CONDITION FOR SQLSTATE '45000'; IF x = 0 THEN SIGNAL cond_name; END IF; END; SIGNAL cond_name; END;`, `CREATE PROCEDURE p2(x INT) BEGIN DECLARE cond_name CONDITION FOR SQLSTATE '45000'; BEGIN DECLARE cond_name CONDITION FOR SQLSTATE '02000'; IF x = 0 THEN SIGNAL cond_name; END IF; END; SIGNAL cond_name; END;`, }, Assertions: []ScriptTestAssertion{ { Query: "CALL p1(0)", ExpectedErrStr: "Unhandled user-defined exception condition (errno 1644) (sqlstate 45000)", }, { Query: "CALL p1(1)", ExpectedErrStr: "Unhandled user-defined not found condition (errno 1643) (sqlstate 02000)", }, { Query: "CALL p2(0)", ExpectedErrStr: "Unhandled user-defined not found condition (errno 1643) (sqlstate 02000)", }, { Query: "CALL p2(1)", ExpectedErrStr: "Unhandled user-defined exception condition (errno 1644) (sqlstate 45000)", }, }, }, { Name: "Duplicate parameter names", Query: "CREATE PROCEDURE p1(abc DATETIME, abc DOUBLE) SELECT abc", ExpectedErr: sql.ErrProcedureDuplicateParameterName, }, { Name: "Duplicate parameter names mixed casing", Query: "CREATE PROCEDURE p1(abc DATETIME, ABC DOUBLE) SELECT abc", ExpectedErr: sql.ErrProcedureDuplicateParameterName, }, { Name: "Invalid parameter type", Query: "CREATE PROCEDURE p1(x FAKETYPE) SELECT x", ExpectedErr: sql.ErrSyntaxError, }, { Name: "Invalid USE statement", Query: `CREATE PROCEDURE p1() USE mydb`, ExpectedErr: sql.ErrSyntaxError, }, { Name: "Invalid LOCK/UNLOCK statements", SetUpScript: []string{ "CREATE TABLE t1(pk BIGINT PRIMARY KEY)", }, Assertions: []ScriptTestAssertion{ { Query: "CREATE PROCEDURE p1(x BIGINT) LOCK TABLES t1 READ", ExpectedErr: sql.ErrSyntaxError, }, { Query: "CREATE PROCEDURE p1(x BIGINT) UNLOCK TABLES", ExpectedErr: sql.ErrSyntaxError, }, }, }, { Name: "DECLARE CONDITION wrong positions", Assertions: []ScriptTestAssertion{ { Query: `CREATE PROCEDURE p1(x INT) BEGIN SELECT x; DECLARE cond_name CONDITION FOR SQLSTATE '45000'; END;`, ExpectedErr: sql.ErrDeclareOrderInvalid, }, { Query: `CREATE PROCEDURE p1(x INT) BEGIN BEGIN SELECT x; DECLARE cond_name CONDITION FOR SQLSTATE '45000'; END; END;`, ExpectedErr: sql.ErrDeclareOrderInvalid, }, { Query: `CREATE PROCEDURE p1(x INT) BEGIN IF x = 0 THEN DECLARE cond_name CONDITION FOR SQLSTATE '45000'; END IF; END;`, ExpectedErr: sql.ErrDeclareOrderInvalid, }, { Query: `CREATE PROCEDURE p1(x INT) BEGIN IF x = 0 THEN SELECT x; ELSE DECLARE cond_name CONDITION FOR SQLSTATE '45000'; END IF; END;`, ExpectedErr: sql.ErrDeclareOrderInvalid, }, }, }, { Name: "DECLARE CONDITION duplicate name", Query: `CREATE PROCEDURE p1() BEGIN DECLARE cond_name CONDITION FOR SQLSTATE '45000'; DECLARE cond_name CONDITION FOR SQLSTATE '45000'; END;`, ExpectedErr: sql.ErrDeclareConditionDuplicate, }, { Name: "SIGNAL references condition name for MySQL error code", Query: `CREATE PROCEDURE p1(x INT) BEGIN DECLARE mysql_err_code CONDITION FOR 1000; SIGNAL mysql_err_code; END;`, ExpectedErr: parse.ErrUnsupportedSyntax, }, { Name: "SIGNAL non-existent condition name", Query: `CREATE PROCEDURE p1(x INT) BEGIN DECLARE abcdefg CONDITION FOR SQLSTATE '45000'; SIGNAL abcdef; END;`, ExpectedErr: sql.ErrDeclareConditionNotFound, }, }
var ProcedureShowStatus = []ScriptTest{ { Name: "SHOW procedures", SetUpScript: []string{ "CREATE PROCEDURE p1() COMMENT 'hi' DETERMINISTIC SELECT 6", "CREATE definer=user PROCEDURE p2() SQL SECURITY INVOKER SELECT 7", "CREATE PROCEDURE p21() SQL SECURITY DEFINER SELECT 8", }, Assertions: []ScriptTestAssertion{ { Query: "SHOW PROCEDURE STATUS", Expected: []sql.Row{ { "mydb", "p1", "PROCEDURE", "", time.Unix(0, 0).UTC(), time.Unix(0, 0).UTC(), "DEFINER", "hi", "utf8mb4", "utf8mb4_0900_ai_ci", "utf8mb4_0900_ai_ci", }, { "mydb", "p2", "PROCEDURE", "user", time.Unix(0, 0).UTC(), time.Unix(0, 0).UTC(), "INVOKER", "", "utf8mb4", "utf8mb4_0900_ai_ci", "utf8mb4_0900_ai_ci", }, { "mydb", "p21", "PROCEDURE", "", time.Unix(0, 0).UTC(), time.Unix(0, 0).UTC(), "DEFINER", "", "utf8mb4", "utf8mb4_0900_ai_ci", "utf8mb4_0900_ai_ci", }, }, }, { Query: "SHOW PROCEDURE STATUS LIKE 'p2%'", Expected: []sql.Row{ { "mydb", "p2", "PROCEDURE", "user", time.Unix(0, 0).UTC(), time.Unix(0, 0).UTC(), "INVOKER", "", "utf8mb4", "utf8mb4_0900_ai_ci", "utf8mb4_0900_ai_ci", }, { "mydb", "p21", "PROCEDURE", "", time.Unix(0, 0).UTC(), time.Unix(0, 0).UTC(), "DEFINER", "", "utf8mb4", "utf8mb4_0900_ai_ci", "utf8mb4_0900_ai_ci", }, }, }, { Query: "SHOW PROCEDURE STATUS LIKE 'p4'", Expected: []sql.Row{}, }, { Query: "SHOW PROCEDURE STATUS WHERE Db = 'mydb'", Expected: []sql.Row{ { "mydb", "p1", "PROCEDURE", "", time.Unix(0, 0).UTC(), time.Unix(0, 0).UTC(), "DEFINER", "hi", "utf8mb4", "utf8mb4_0900_ai_ci", "utf8mb4_0900_ai_ci", }, { "mydb", "p2", "PROCEDURE", "user", time.Unix(0, 0).UTC(), time.Unix(0, 0).UTC(), "INVOKER", "", "utf8mb4", "utf8mb4_0900_ai_ci", "utf8mb4_0900_ai_ci", }, { "mydb", "p21", "PROCEDURE", "", time.Unix(0, 0).UTC(), time.Unix(0, 0).UTC(), "DEFINER", "", "utf8mb4", "utf8mb4_0900_ai_ci", "utf8mb4_0900_ai_ci", }, }, }, { Query: "SHOW PROCEDURE STATUS WHERE Name LIKE '%1'", Expected: []sql.Row{ { "mydb", "p1", "PROCEDURE", "", time.Unix(0, 0).UTC(), time.Unix(0, 0).UTC(), "DEFINER", "hi", "utf8mb4", "utf8mb4_0900_ai_ci", "utf8mb4_0900_ai_ci", }, { "mydb", "p21", "PROCEDURE", "", time.Unix(0, 0).UTC(), time.Unix(0, 0).UTC(), "DEFINER", "", "utf8mb4", "utf8mb4_0900_ai_ci", "utf8mb4_0900_ai_ci", }, }, }, { Query: "SHOW PROCEDURE STATUS WHERE Security_type = 'INVOKER'", Expected: []sql.Row{ { "mydb", "p2", "PROCEDURE", "user", time.Unix(0, 0).UTC(), time.Unix(0, 0).UTC(), "INVOKER", "", "utf8mb4", "utf8mb4_0900_ai_ci", "utf8mb4_0900_ai_ci", }, }, }, }, }, }
var QueryTests = []QueryTest{}/* 689 elements not displayed */
var ReplaceErrorTests = []GenericErrorQueryTest{
{
Name: "too few values",
Query: "REPLACE INTO mytable (s, i) VALUES ('x');",
},
{
Name: "too many values one column",
Query: "REPLACE INTO mytable (s) VALUES ('x', 999);",
},
{
Name: "too many values two columns",
Query: "REPLACE INTO mytable (i, s) VALUES (999, 'x', 'y');",
},
{
Name: "too few values no columns specified",
Query: "REPLACE INTO mytable VALUES (999);",
},
{
Name: "too many values no columns specified",
Query: "REPLACE INTO mytable VALUES (999, 'x', 'y');",
},
{
Name: "non-existent column values",
Query: "REPLACE INTO mytable (i, s, z) VALUES (999, 'x', 999);",
},
{
Name: "non-existent column set",
Query: "REPLACE INTO mytable SET i = 999, s = 'x', z = 999;",
},
{
Name: "duplicate column values",
Query: "REPLACE INTO mytable (i, s, s) VALUES (999, 'x', 'x');",
},
{
Name: "duplicate column set",
Query: "REPLACE INTO mytable SET i = 999, s = 'y', s = 'y';",
},
{
Name: "null given to non-nullable values",
Query: "INSERT INTO mytable (i, s) VALUES (null, 'y');",
},
{
Name: "null given to non-nullable set",
Query: "INSERT INTO mytable SET i = null, s = 'y';",
},
}
var ReplaceQueries = []WriteQueryTest{ { WriteQuery: "REPLACE INTO mytable VALUES (1, 'first row');", ExpectedWriteResult: []sql.Row{{sql.NewOkResult(2)}}, SelectQuery: "SELECT s FROM mytable WHERE i = 1;", ExpectedSelect: []sql.Row{{"first row"}}, }, { WriteQuery: "REPLACE INTO mytable SET i = 1, s = 'first row';", ExpectedWriteResult: []sql.Row{{sql.NewOkResult(2)}}, SelectQuery: "SELECT s FROM mytable WHERE i = 1;", ExpectedSelect: []sql.Row{{"first row"}}, }, { WriteQuery: "REPLACE INTO mytable VALUES (1, 'new row same i');", ExpectedWriteResult: []sql.Row{{sql.NewOkResult(2)}}, SelectQuery: "SELECT s FROM mytable WHERE i = 1;", ExpectedSelect: []sql.Row{{"new row same i"}}, }, { WriteQuery: "REPLACE INTO mytable SET i = 1, s = 'new row same i';", ExpectedWriteResult: []sql.Row{{sql.NewOkResult(2)}}, SelectQuery: "SELECT s FROM mytable WHERE i = 1;", ExpectedSelect: []sql.Row{{"new row same i"}}, }, { WriteQuery: "REPLACE INTO mytable (s, i) VALUES ('x', 999);", ExpectedWriteResult: []sql.Row{{sql.NewOkResult(1)}}, SelectQuery: "SELECT i FROM mytable WHERE s = 'x';", ExpectedSelect: []sql.Row{{int64(999)}}, }, { WriteQuery: "REPLACE INTO mytable SET s = 'x', i = 999;", ExpectedWriteResult: []sql.Row{{sql.NewOkResult(1)}}, SelectQuery: "SELECT i FROM mytable WHERE s = 'x';", ExpectedSelect: []sql.Row{{int64(999)}}, }, { WriteQuery: "REPLACE INTO mytable VALUES (999, 'x');", ExpectedWriteResult: []sql.Row{{sql.NewOkResult(1)}}, SelectQuery: "SELECT i FROM mytable WHERE s = 'x';", ExpectedSelect: []sql.Row{{int64(999)}}, }, { WriteQuery: "REPLACE INTO mytable SET i = 999, s = 'x';", ExpectedWriteResult: []sql.Row{{sql.NewOkResult(1)}}, SelectQuery: "SELECT i FROM mytable WHERE s = 'x';", ExpectedSelect: []sql.Row{{int64(999)}}, }, { WriteQuery: `REPLACE INTO typestable VALUES ( 999, 127, 32767, 2147483647, 9223372036854775807, 255, 65535, 4294967295, 18446744073709551615, 3.40282346638528859811704183484516925440e+38, 1.797693134862315708145274237317043567981e+308, '2037-04-05 12:51:36', '2231-11-07', 'random text', true, '{"key":"value"}', 'blobdata' );`, ExpectedWriteResult: []sql.Row{{sql.NewOkResult(1)}}, SelectQuery: "SELECT * FROM typestable WHERE id = 999;", ExpectedSelect: []sql.Row{{ int64(999), int8(math.MaxInt8), int16(math.MaxInt16), int32(math.MaxInt32), int64(math.MaxInt64), uint8(math.MaxUint8), uint16(math.MaxUint16), uint32(math.MaxUint32), uint64(math.MaxUint64), float32(math.MaxFloat32), float64(math.MaxFloat64), sql.MustConvert(sql.Timestamp.Convert("2037-04-05 12:51:36")), sql.MustConvert(sql.Date.Convert("2231-11-07")), "random text", sql.True, sql.MustJSON(`{"key":"value"}`), "blobdata", }}, }, { WriteQuery: `REPLACE INTO typestable SET id = 999, i8 = 127, i16 = 32767, i32 = 2147483647, i64 = 9223372036854775807, u8 = 255, u16 = 65535, u32 = 4294967295, u64 = 18446744073709551615, f32 = 3.40282346638528859811704183484516925440e+38, f64 = 1.797693134862315708145274237317043567981e+308, ti = '2037-04-05 12:51:36', da = '2231-11-07', te = 'random text', bo = true, js = '{"key":"value"}', bl = 'blobdata' ;`, ExpectedWriteResult: []sql.Row{{sql.NewOkResult(1)}}, SelectQuery: "SELECT * FROM typestable WHERE id = 999;", ExpectedSelect: []sql.Row{{ int64(999), int8(math.MaxInt8), int16(math.MaxInt16), int32(math.MaxInt32), int64(math.MaxInt64), uint8(math.MaxUint8), uint16(math.MaxUint16), uint32(math.MaxUint32), uint64(math.MaxUint64), float32(math.MaxFloat32), float64(math.MaxFloat64), sql.MustConvert(sql.Timestamp.Convert("2037-04-05 12:51:36")), sql.MustConvert(sql.Date.Convert("2231-11-07")), "random text", sql.True, sql.MustJSON(`{"key":"value"}`), "blobdata", }}, }, { WriteQuery: `REPLACE INTO typestable VALUES ( 999, -128, -32768, -2147483648, -9223372036854775808, 0, 0, 0, 0, 1.401298464324817070923729583289916131280e-45, 4.940656458412465441765687928682213723651e-324, '0000-00-00 00:00:00', '0000-00-00', '', false, '""', '' );`, ExpectedWriteResult: []sql.Row{{sql.NewOkResult(1)}}, SelectQuery: "SELECT * FROM typestable WHERE id = 999;", ExpectedSelect: []sql.Row{{ int64(999), int8(-math.MaxInt8 - 1), int16(-math.MaxInt16 - 1), int32(-math.MaxInt32 - 1), int64(-math.MaxInt64 - 1), uint8(0), uint16(0), uint32(0), uint64(0), float32(math.SmallestNonzeroFloat32), float64(math.SmallestNonzeroFloat64), sql.Timestamp.Zero(), sql.Date.Zero(), "", sql.False, sql.MustJSON(`""`), "", }}, }, { WriteQuery: `REPLACE INTO typestable SET id = 999, i8 = -128, i16 = -32768, i32 = -2147483648, i64 = -9223372036854775808, u8 = 0, u16 = 0, u32 = 0, u64 = 0, f32 = 1.401298464324817070923729583289916131280e-45, f64 = 4.940656458412465441765687928682213723651e-324, ti = '0000-00-00 00:00:00', da = '0000-00-00', te = '', bo = false, js = '""', bl = '' ;`, ExpectedWriteResult: []sql.Row{{sql.NewOkResult(1)}}, SelectQuery: "SELECT * FROM typestable WHERE id = 999;", ExpectedSelect: []sql.Row{{ int64(999), int8(-math.MaxInt8 - 1), int16(-math.MaxInt16 - 1), int32(-math.MaxInt32 - 1), int64(-math.MaxInt64 - 1), uint8(0), uint16(0), uint32(0), uint64(0), float32(math.SmallestNonzeroFloat32), float64(math.SmallestNonzeroFloat64), sql.Timestamp.Zero(), sql.Date.Zero(), "", sql.False, sql.MustJSON(`""`), "", }}, }, { WriteQuery: `REPLACE INTO typestable VALUES (999, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null);`, ExpectedWriteResult: []sql.Row{{sql.NewOkResult(1)}}, SelectQuery: "SELECT * FROM typestable WHERE id = 999;", ExpectedSelect: []sql.Row{{int64(999), nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil}}, }, { WriteQuery: `REPLACE INTO typestable SET id=999, i8=null, i16=null, i32=null, i64=null, u8=null, u16=null, u32=null, u64=null, f32=null, f64=null, ti=null, da=null, te=null, bo=null, js=null, bl=null;`, ExpectedWriteResult: []sql.Row{{sql.NewOkResult(1)}}, SelectQuery: "SELECT * FROM typestable WHERE id = 999;", ExpectedSelect: []sql.Row{{int64(999), nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil}}, }, }
TODO: none of these tests insert into tables without primary key columns, which have different semantics for
REPLACE INTO queries. Add some tables / data without primary keys.
var ScriptTests = []ScriptTest{ { Name: "delete with in clause", SetUpScript: []string{ "create table a (x int primary key)", "insert into a values (1), (3), (5)", "delete from a where x in (1, 3)", }, Query: "select x from a order by 1", Expected: []sql.Row{ {5}, }, }, { Name: "sqllogictest evidence/slt_lang_aggfunc.test", SetUpScript: []string{ "CREATE TABLE t1( x INTEGER, y VARCHAR(8) )", "INSERT INTO t1 VALUES(1,'true')", "INSERT INTO t1 VALUES(0,'false')", "INSERT INTO t1 VALUES(NULL,'NULL')", }, Query: "SELECT count(DISTINCT x) FROM t1", Expected: []sql.Row{ {2}, }, }, { Name: "sqllogictest index/commute/10/slt_good_1.test", SetUpScript: []string{ "CREATE TABLE tab0(pk INTEGER PRIMARY KEY, col0 INTEGER, col1 FLOAT, col2 TEXT, col3 INTEGER, col4 FLOAT, col5 TEXT)", "INSERT INTO tab0 VALUES(0,42,58.92,'fnbtk',54,68.41,'xmttf')", "INSERT INTO tab0 VALUES(1,31,46.55,'sksjf',46,53.20,'wiuva')", "INSERT INTO tab0 VALUES(2,30,31.11,'oldqn',41,5.26,'ulaay')", "INSERT INTO tab0 VALUES(3,77,44.90,'pmsir',70,84.14,'vcmyo')", "INSERT INTO tab0 VALUES(4,23,95.26,'qcwxh',32,48.53,'rvtbr')", "INSERT INTO tab0 VALUES(5,43,6.75,'snvwg',3,14.38,'gnfxz')", "INSERT INTO tab0 VALUES(6,47,98.26,'bzzva',60,15.2,'imzeq')", "INSERT INTO tab0 VALUES(7,98,40.9,'lsrpi',78,66.30,'ephwy')", "INSERT INTO tab0 VALUES(8,19,15.16,'ycvjz',55,38.70,'dnkkz')", "INSERT INTO tab0 VALUES(9,7,84.4,'ptovf',17,2.46,'hrxsf')", "CREATE TABLE tab1(pk INTEGER PRIMARY KEY, col0 INTEGER, col1 FLOAT, col2 TEXT, col3 INTEGER, col4 FLOAT, col5 TEXT)", "CREATE INDEX idx_tab1_0 on tab1 (col0)", "CREATE INDEX idx_tab1_1 on tab1 (col1)", "CREATE INDEX idx_tab1_3 on tab1 (col3)", "CREATE INDEX idx_tab1_4 on tab1 (col4)", "INSERT INTO tab1 SELECT * FROM tab0", "CREATE TABLE tab2(pk INTEGER PRIMARY KEY, col0 INTEGER, col1 FLOAT, col2 TEXT, col3 INTEGER, col4 FLOAT, col5 TEXT)", "CREATE UNIQUE INDEX idx_tab2_1 ON tab2 (col4 DESC,col3)", "CREATE UNIQUE INDEX idx_tab2_2 ON tab2 (col3 DESC,col0)", "CREATE UNIQUE INDEX idx_tab2_3 ON tab2 (col3 DESC,col1)", "INSERT INTO tab2 SELECT * FROM tab0", "CREATE TABLE tab3(pk INTEGER PRIMARY KEY, col0 INTEGER, col1 FLOAT, col2 TEXT, col3 INTEGER, col4 FLOAT, col5 TEXT)", "CREATE INDEX idx_tab3_0 ON tab3 (col3 DESC)", "INSERT INTO tab3 SELECT * FROM tab0", "CREATE TABLE tab4(pk INTEGER PRIMARY KEY, col0 INTEGER, col1 FLOAT, col2 TEXT, col3 INTEGER, col4 FLOAT, col5 TEXT)", "CREATE INDEX idx_tab4_0 ON tab4 (col0 DESC)", "CREATE UNIQUE INDEX idx_tab4_2 ON tab4 (col4 DESC,col3)", "CREATE INDEX idx_tab4_3 ON tab4 (col3 DESC)", "INSERT INTO tab4 SELECT * FROM tab0", }, Query: "SELECT pk FROM tab2 WHERE 78 < col0 AND 19 < col3", Expected: []sql.Row{ {7}, }, }, { Name: "3 tables, linear join", SetUpScript: []string{ "create table a (xa int primary key, ya int, za int)", "create table b (xb int primary key, yb int, zb int)", "create table c (xc int primary key, yc int, zc int)", "insert into a values (1,2,3)", "insert into b values (1,2,3)", "insert into c values (1,2,3)", }, Assertions: []ScriptTestAssertion{ { Query: "select ya from a join b on ya - 1= xb join c on xc = zb - 2", Expected: []sql.Row{{2}}, }, }, }, { Name: "3 tables, v join", SetUpScript: []string{ "create table a (xa int primary key, ya int, za int)", "create table b (xb int primary key, yb int, zb int)", "create table c (xc int primary key, yc int, zc int)", "insert into a values (1,2,3)", "insert into b values (1,2,3)", "insert into c values (1,2,3)", }, Assertions: []ScriptTestAssertion{ { Query: "select za from a join b on ya - 1 = xb join c on xa = xc", Expected: []sql.Row{{3}}, }, }, }, { Name: "3 tables, linear join, indexes on A,C", SetUpScript: []string{ "create table a (xa int primary key, ya int, za int)", "create table b (xb int primary key, yb int, zb int)", "create table c (xc int primary key, yc int, zc int)", "insert into a values (1,2,3)", "insert into b values (1,2,3)", "insert into c values (1,2,3)", }, Assertions: []ScriptTestAssertion{ { Query: "select xa from a join b on xa = yb - 1 join c on yb - 1 = xc", Expected: []sql.Row{{1}}, }, }, }, { Name: "4 tables, linear join", SetUpScript: []string{ "create table a (xa int primary key, ya int, za int)", "create table b (xb int primary key, yb int, zb int)", "create table c (xc int primary key, yc int, zc int)", "create table d (xd int primary key, yd int, zd int)", "insert into a values (1,2,3)", "insert into b values (1,2,3)", "insert into c values (1,2,3)", "insert into d values (1,2,3)", }, Assertions: []ScriptTestAssertion{ { Query: "select xa from a join b on ya - 1 = xb join c on xb = xc join d on xc = xd", Expected: []sql.Row{{1}}, }, }, }, { Name: "4 tables, linear join, index on D", SetUpScript: []string{ "create table a (xa int primary key, ya int, za int)", "create table b (xb int primary key, yb int, zb int)", "create table c (xc int primary key, yc int, zc int)", "create table d (xd int primary key, yd int, zd int)", "insert into a values (1,2,3)", "insert into b values (1,2,3)", "insert into c values (1,2,3)", "insert into d values (1,2,3)", }, Assertions: []ScriptTestAssertion{ { Query: "select xa from a join b on ya = yb join c on yb = yc join d on yc - 1 = xd", Expected: []sql.Row{{1}}, }, }, }, { Name: "4 tables, left join, indexes on all tables", SetUpScript: []string{ "create table a (xa int primary key, ya int, za int)", "create table b (xb int primary key, yb int, zb int)", "create table c (xc int primary key, yc int, zc int)", "create table d (xd int primary key, yd int, zd int)", "insert into a values (1,2,3)", "insert into b values (1,2,3)", "insert into c values (1,2,3)", "insert into d values (1,2,3)", }, Assertions: []ScriptTestAssertion{ { Query: "select xa from a left join b on ya = yb left join c on yb = yc left join d on yc - 1 = xd", Expected: []sql.Row{{1}}, }, }, }, { Name: "4 tables, linear join, index on B, D", SetUpScript: []string{ "create table a (xa int primary key, ya int, za int)", "create table b (xb int primary key, yb int, zb int)", "create table c (xc int primary key, yc int, zc int)", "create table d (xd int primary key, yd int, zd int)", "insert into a values (1,2,3)", "insert into b values (1,2,3)", "insert into c values (1,2,3)", "insert into d values (1,2,3)", }, Assertions: []ScriptTestAssertion{ { Query: "select xa from a join b on ya - 1 = xb join c on yc = za - 1 join d on yc - 1 = xd", Expected: []sql.Row{{1}}, }, }, }, { Name: "4 tables, all joined to A", SetUpScript: []string{ "create table a (xa int primary key, ya int, za int)", "create table b (xb int primary key, yb int, zb int)", "create table c (xc int primary key, yc int, zc int)", "create table d (xd int primary key, yd int, zd int)", "insert into a values (1,2,3)", "insert into b values (1,2,3)", "insert into c values (1,2,3)", "insert into d values (1,2,3)", }, Assertions: []ScriptTestAssertion{ { Query: "select xa from a join b on xa = xb join c on ya - 1 = xc join d on za - 2 = xd", Expected: []sql.Row{{1}}, }, }, }, { Name: "4 tables, all joined to D", SetUpScript: []string{ "create table a (xa int primary key, ya int, za int)", "create table b (xb int primary key, yb int, zb int)", "create table c (xc int primary key, yc int, zc int)", "create table d (xd int primary key, yd int, zd int)", "insert into a values (1,2,3)", "insert into b values (1,2,3)", "insert into c values (1,2,3)", "insert into d values (1,2,3)", }, Assertions: []ScriptTestAssertion{ { Query: "select xa from d join a on yd - 1 = xa join c on zd - 2 = xc join b on xb = zd - 2", Expected: []sql.Row{{1}}, }, }, }, { Name: "5 tables, complex join conditions", SetUpScript: []string{ "create table a (xa int primary key, ya int, za int)", "create table b (xb int primary key, yb int, zb int)", "create table c (xc int primary key, yc int, zc int)", "create table d (xd int primary key, yd int, zd int)", "create table e (xe int, ye int, ze int, primary key(xe, ye))", "insert into a values (1,2,3)", "insert into b values (1,2,3)", "insert into c values (1,2,3)", "insert into d values (1,2,3)", "insert into e values (1,2,3)", }, Assertions: []ScriptTestAssertion{ { Query: `select xa from a join b on ya - 1 = xb join c on xc = za - 2 join d on xd = yb - 1 join e on xe = zb - 2 and ye = yc`, Expected: []sql.Row{{1}}, }, }, }, { Name: "UUIDs used in the wild.", SetUpScript: []string{ "SET @uuid = '6ccd780c-baba-1026-9564-5b8c656024db'", "SET @binuuid = '0011223344556677'", }, Assertions: []ScriptTestAssertion{ { Query: `SELECT IS_UUID(UUID())`, Expected: []sql.Row{{int8(1)}}, }, { Query: `SELECT IS_UUID(@uuid)`, Expected: []sql.Row{{int8(1)}}, }, { Query: `SELECT BIN_TO_UUID(UUID_TO_BIN(@uuid))`, Expected: []sql.Row{{"6ccd780c-baba-1026-9564-5b8c656024db"}}, }, { Query: `SELECT BIN_TO_UUID(UUID_TO_BIN(@uuid, 1), 1)`, Expected: []sql.Row{{"6ccd780c-baba-1026-9564-5b8c656024db"}}, }, { Query: `SELECT UUID_TO_BIN(NULL)`, Expected: []sql.Row{{nil}}, }, { Query: `SELECT HEX(UUID_TO_BIN(@uuid))`, Expected: []sql.Row{{"6CCD780CBABA102695645B8C656024DB"}}, }, { Query: `SELECT UUID_TO_BIN(123)`, ExpectedErr: sql.ErrUuidUnableToParse, }, { Query: `SELECT BIN_TO_UUID(123)`, ExpectedErr: sql.ErrUuidUnableToParse, }, { Query: `SELECT BIN_TO_UUID(X'00112233445566778899aabbccddeeff')`, Expected: []sql.Row{{"00112233-4455-6677-8899-aabbccddeeff"}}, }, { Query: `SELECT BIN_TO_UUID('0011223344556677')`, Expected: []sql.Row{{"30303131-3232-3333-3434-353536363737"}}, }, { Query: `SELECT BIN_TO_UUID(@binuuid)`, Expected: []sql.Row{{"30303131-3232-3333-3434-353536363737"}}, }, }, }, { Name: "CrossDB Queries", SetUpScript: []string{ "CREATE DATABASE test", "CREATE TABLE test.x (pk int primary key)", "insert into test.x values (1),(2),(3)", "DELETE FROM test.x WHERE pk=2", "UPDATE test.x set pk=300 where pk=3", "create table a (xa int primary key, ya int, za int)", "insert into a values (1,2,3)", }, Assertions: []ScriptTestAssertion{ { Query: "SELECT pk from test.x", Expected: []sql.Row{{1}, {300}}, }, { Query: "SELECT * from a", Expected: []sql.Row{{1, 2, 3}}, }, }, }, { Name: "Top-level DECLARE statements", Assertions: []ScriptTestAssertion{ { Query: "DECLARE no_such_table CONDITION FOR SQLSTATE '42S02'", ExpectedErr: sql.ErrSyntaxError, }, { Query: "DECLARE no_such_table CONDITION FOR 1051", ExpectedErr: sql.ErrSyntaxError, }, { Query: "DECLARE a CHAR(16)", ExpectedErr: sql.ErrSyntaxError, }, { Query: "DECLARE cur2 CURSOR FOR SELECT i FROM test.t2", ExpectedErr: sql.ErrSyntaxError, }, { Query: "DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = TRUE", ExpectedErr: sql.ErrSyntaxError, }, }, }, { Name: "last_insert_id() behavior", SetUpScript: []string{ "create table a (x int primary key auto_increment, y int)", "create table b (x int primary key)", }, Assertions: []ScriptTestAssertion{ { Query: "select last_insert_id()", Expected: []sql.Row{{0}}, }, { Query: "insert into a (y) values (1)", Expected: []sql.Row{{sql.NewOkResult(1)}}, }, { Query: "select last_insert_id()", Expected: []sql.Row{{1}}, }, { Query: "insert into a (y) values (2), (3)", Expected: []sql.Row{{sql.NewOkResult(2)}}, }, { Query: "select last_insert_id()", Expected: []sql.Row{{2}}, }, { Query: "insert into b (x) values (1), (2)", Expected: []sql.Row{{sql.NewOkResult(2)}}, }, { Query: "select last_insert_id()", Expected: []sql.Row{{2}}, }, }, }, { Name: "row_count() behavior", SetUpScript: []string{ "create table b (x int primary key)", "insert into b values (1), (2), (3), (4)", }, Assertions: []ScriptTestAssertion{ { Query: "select row_count()", Expected: []sql.Row{{4}}, }, { Query: "replace into b values (1)", Expected: []sql.Row{{sql.NewOkResult(2)}}, }, { Query: "select row_count()", Expected: []sql.Row{{2}}, }, { Query: "select row_count()", Expected: []sql.Row{{-1}}, }, { Query: "select count(*) from b", Expected: []sql.Row{{4}}, }, { Query: "select row_count()", Expected: []sql.Row{{-1}}, }, { Query: "update b set x = x + 10 where x <> 2", Expected: []sql.Row{{sql.OkResult{ RowsAffected: 3, Info: plan.UpdateInfo{ Matched: 3, Updated: 3, }, }}}, }, { Query: "select row_count()", Expected: []sql.Row{{3}}, }, { Query: "select row_count()", Expected: []sql.Row{{-1}}, }, { Query: "delete from b where x <> 2", Expected: []sql.Row{{sql.NewOkResult(3)}}, }, { Query: "select row_count()", Expected: []sql.Row{{3}}, }, { Query: "select row_count()", Expected: []sql.Row{{-1}}, }, { Query: "alter table b add column y int null", Expected: []sql.Row{}, }, { Query: "select row_count()", Expected: []sql.Row{{0}}, }, { Query: "select row_count()", Expected: []sql.Row{{-1}}, }, }, }, { Name: "found_rows() behavior", SetUpScript: []string{ "create table b (x int primary key)", "insert into b values (1), (2), (3), (4)", }, Assertions: []ScriptTestAssertion{ { Query: "select found_rows()", Expected: []sql.Row{{1}}, }, { Query: "select * from b", Expected: []sql.Row{{1}, {2}, {3}, {4}}, }, { Query: "select found_rows()", Expected: []sql.Row{{4}}, }, { Query: "select found_rows()", Expected: []sql.Row{{1}}, }, { Query: "select * from b order by x limit 3", Expected: []sql.Row{{1}, {2}, {3}}, }, { Query: "select found_rows()", Expected: []sql.Row{{3}}, }, { Query: "select found_rows()", Expected: []sql.Row{{1}}, }, { Query: "select sql_calc_found_rows * from b order by x limit 3", Expected: []sql.Row{{1}, {2}, {3}}, }, { Query: "select found_rows()", Expected: []sql.Row{{4}}, }, { Query: "select found_rows()", Expected: []sql.Row{{1}}, }, { Query: "select sql_calc_found_rows * from b where x <= 2 order by x limit 1", Expected: []sql.Row{{1}}, }, { Query: "select found_rows()", Expected: []sql.Row{{2}}, }, { Query: "select sql_calc_found_rows * from b where x <= 2 order by x limit 1", Expected: []sql.Row{{1}}, }, { Query: "insert into b values (10), (11), (12), (13)", Expected: []sql.Row{{sql.NewOkResult(4)}}, }, { Query: "select found_rows()", Expected: []sql.Row{{2}}, }, }, }, }
Unlike other engine tests, ScriptTests must be self-contained. No other tables are created outside the definition of the tests.
var ShowTableStatusQueries = []QueryTest{ { Query: `SHOW TABLE STATUS FROM mydb`, Expected: []sql.Row{ {"auto_increment_tbl", "InnoDB", "10", "Fixed", uint64(3), uint64(16), uint64(48), uint64(0), int64(0), int64(0), int64(4), nil, nil, nil, "utf8mb4_0900_ai_ci", nil, nil, nil}, {"mytable", "InnoDB", "10", "Fixed", uint64(3), uint64(88), uint64(264), uint64(0), int64(0), int64(0), nil, nil, nil, nil, "utf8mb4_0900_ai_ci", nil, nil, nil}, {"othertable", "InnoDB", "10", "Fixed", uint64(3), uint64(65540), uint64(196620), uint64(0), int64(0), int64(0), nil, nil, nil, nil, "utf8mb4_0900_ai_ci", nil, nil, nil}, {"tabletest", "InnoDB", "10", "Fixed", uint64(3), uint64(65540), uint64(196620), uint64(0), int64(0), int64(0), nil, nil, nil, nil, "utf8mb4_0900_ai_ci", nil, nil, nil}, {"bigtable", "InnoDB", "10", "Fixed", uint64(14), uint64(65540), uint64(917560), uint64(0), int64(0), int64(0), nil, nil, nil, nil, "utf8mb4_0900_ai_ci", nil, nil, nil}, {"floattable", "InnoDB", "10", "Fixed", uint64(6), uint64(24), uint64(144), uint64(0), int64(0), int64(0), nil, nil, nil, nil, "utf8mb4_0900_ai_ci", nil, nil, nil}, {"fk_tbl", "InnoDB", "10", "Fixed", uint64(3), uint64(96), uint64(288), uint64(0), int64(0), int64(0), nil, nil, nil, nil, "utf8mb4_0900_ai_ci", nil, nil, nil}, {"niltable", "InnoDB", "10", "Fixed", uint64(6), uint64(32), uint64(192), uint64(0), int64(0), int64(0), nil, nil, nil, nil, "utf8mb4_0900_ai_ci", nil, nil, nil}, {"newlinetable", "InnoDB", "10", "Fixed", uint64(5), uint64(65540), uint64(327700), uint64(0), int64(0), int64(0), nil, nil, nil, nil, "utf8mb4_0900_ai_ci", nil, nil, nil}, {"people", "InnoDB", "10", "Fixed", uint64(5), uint64(196620), uint64(983100), uint64(0), int64(0), int64(0), nil, nil, nil, nil, "utf8mb4_0900_ai_ci", nil, nil, nil}, }, }, { Query: `SHOW TABLE STATUS LIKE '%table'`, Expected: []sql.Row{ {"mytable", "InnoDB", "10", "Fixed", uint64(3), uint64(88), uint64(264), uint64(0), int64(0), int64(0), nil, nil, nil, nil, "utf8mb4_0900_ai_ci", nil, nil, nil}, {"othertable", "InnoDB", "10", "Fixed", uint64(3), uint64(65540), uint64(196620), uint64(0), int64(0), int64(0), nil, nil, nil, nil, "utf8mb4_0900_ai_ci", nil, nil, nil}, {"bigtable", "InnoDB", "10", "Fixed", uint64(14), uint64(65540), uint64(917560), uint64(0), int64(0), int64(0), nil, nil, nil, nil, "utf8mb4_0900_ai_ci", nil, nil, nil}, {"floattable", "InnoDB", "10", "Fixed", uint64(6), uint64(24), uint64(144), uint64(0), int64(0), int64(0), nil, nil, nil, nil, "utf8mb4_0900_ai_ci", nil, nil, nil}, {"niltable", "InnoDB", "10", "Fixed", uint64(6), uint64(32), uint64(192), uint64(0), int64(0), int64(0), nil, nil, nil, nil, "utf8mb4_0900_ai_ci", nil, nil, nil}, {"newlinetable", "InnoDB", "10", "Fixed", uint64(5), uint64(65540), uint64(327700), uint64(0), int64(0), int64(0), nil, nil, nil, nil, "utf8mb4_0900_ai_ci", nil, nil, nil}, }, }, { Query: `SHOW TABLE STATUS FROM mydb LIKE 'othertable'`, Expected: []sql.Row{ {"othertable", "InnoDB", "10", "Fixed", uint64(3), uint64(65540), uint64(196620), uint64(0), int64(0), int64(0), nil, nil, nil, nil, "utf8mb4_0900_ai_ci", nil, nil, nil}, }, }, { Query: `SHOW TABLE STATUS WHERE Name = 'mytable'`, Expected: []sql.Row{ {"mytable", "InnoDB", "10", "Fixed", uint64(3), uint64(88), uint64(264), uint64(0), int64(0), int64(0), nil, nil, nil, nil, "utf8mb4_0900_ai_ci", nil, nil, nil}, }, }, { Query: `SHOW TABLE STATUS`, Expected: []sql.Row{ {"auto_increment_tbl", "InnoDB", "10", "Fixed", uint64(3), uint64(16), uint64(48), uint64(0), int64(0), int64(0), int64(4), nil, nil, nil, "utf8mb4_0900_ai_ci", nil, nil, nil}, {"mytable", "InnoDB", "10", "Fixed", uint64(3), uint64(88), uint64(264), uint64(0), int64(0), int64(0), nil, nil, nil, nil, "utf8mb4_0900_ai_ci", nil, nil, nil}, {"othertable", "InnoDB", "10", "Fixed", uint64(3), uint64(65540), uint64(196620), uint64(0), int64(0), int64(0), nil, nil, nil, nil, "utf8mb4_0900_ai_ci", nil, nil, nil}, {"tabletest", "InnoDB", "10", "Fixed", uint64(3), uint64(65540), uint64(196620), uint64(0), int64(0), int64(0), nil, nil, nil, nil, "utf8mb4_0900_ai_ci", nil, nil, nil}, {"bigtable", "InnoDB", "10", "Fixed", uint64(14), uint64(65540), uint64(917560), uint64(0), int64(0), int64(0), nil, nil, nil, nil, "utf8mb4_0900_ai_ci", nil, nil, nil}, {"floattable", "InnoDB", "10", "Fixed", uint64(6), uint64(24), uint64(144), uint64(0), int64(0), int64(0), nil, nil, nil, nil, "utf8mb4_0900_ai_ci", nil, nil, nil}, {"fk_tbl", "InnoDB", "10", "Fixed", uint64(3), uint64(96), uint64(288), uint64(0), int64(0), int64(0), nil, nil, nil, nil, "utf8mb4_0900_ai_ci", nil, nil, nil}, {"niltable", "InnoDB", "10", "Fixed", uint64(6), uint64(32), uint64(192), uint64(0), int64(0), int64(0), nil, nil, nil, nil, "utf8mb4_0900_ai_ci", nil, nil, nil}, {"newlinetable", "InnoDB", "10", "Fixed", uint64(5), uint64(65540), uint64(327700), uint64(0), int64(0), int64(0), nil, nil, nil, nil, "utf8mb4_0900_ai_ci", nil, nil, nil}, {"people", "InnoDB", "10", "Fixed", uint64(5), uint64(196620), uint64(983100), uint64(0), int64(0), int64(0), nil, nil, nil, nil, "utf8mb4_0900_ai_ci", nil, nil, nil}, }, }, { Query: `SHOW TABLE STATUS FROM mydb LIKE 'othertable'`, Expected: []sql.Row{ {"othertable", "InnoDB", "10", "Fixed", uint64(3), uint64(65540), uint64(196620), uint64(0), int64(0), int64(0), nil, nil, nil, nil, "utf8mb4_0900_ai_ci", nil, nil, nil}, }, }, }
var TriggerErrorTests = []ScriptTest{ { Name: "table doesn't exist", SetUpScript: []string{ "create table x (a int primary key, b int, c int)", }, Query: "create trigger not_found before insert on y for each row set new.a = new.a + 1", ExpectedErr: sql.ErrTableNotFound, }, { Name: "trigger errors on execution", SetUpScript: []string{ "create table x (a int primary key, b int)", "create table y (c int primary key not null)", "create trigger trigger_has_error before insert on x for each row insert into y values (null)", }, Query: "insert into x values (1,2)", ExpectedErr: plan.ErrInsertIntoNonNullableProvidedNull, }, { Name: "self update on insert", SetUpScript: []string{ "create table a (x int primary key)", "create trigger a1 before insert on a for each row insert into a values (new.x * 2)", }, Query: "insert into a values (1), (2), (3)", ExpectedErr: sql.ErrTriggerTableInUse, }, { Name: "self update on delete", SetUpScript: []string{ "create table a (x int primary key)", "create trigger a1 before delete on a for each row delete from a", }, Query: "delete from a", ExpectedErr: sql.ErrTriggerTableInUse, }, { Name: "self update on update", SetUpScript: []string{ "create table a (x int primary key)", "create trigger a1 before update on a for each row update a set x = 1", }, Query: "update a set x = 2", ExpectedErr: sql.ErrTriggerTableInUse, }, { Name: "circular dependency", SetUpScript: []string{ "create table a (x int primary key)", "create table b (y int primary key)", "create trigger a1 before insert on a for each row insert into b values (new.x * 2)", "create trigger b1 before insert on b for each row insert into a values (new.y * 7)", }, Query: "insert into a values (1), (2), (3)", ExpectedErr: sql.ErrTriggerTableInUse, }, { Name: "circular dependency, nested two deep", SetUpScript: []string{ "create table a (x int primary key)", "create table b (y int primary key)", "create table c (z int primary key)", "create trigger a1 before insert on a for each row insert into b values (new.x * 2)", "create trigger b1 before insert on b for each row insert into c values (new.y * 5)", "create trigger c1 before insert on c for each row insert into a values (new.z * 7)", }, Query: "insert into a values (1), (2), (3)", ExpectedErr: sql.ErrTriggerTableInUse, }, { Name: "reference to old on insert", SetUpScript: []string{ "create table x (a int primary key, b int, c int)", }, Query: "create trigger old_on_insert before insert on x for each row set new.c = old.a + 1", ExpectedErr: sql.ErrInvalidUseOfOldNew, }, { Name: "reference to new on delete", SetUpScript: []string{ "create table x (a int primary key, b int, c int)", }, Query: "create trigger new_on_delete before delete on x for each row set new.c = old.a + 1", ExpectedErr: sql.ErrInvalidUseOfOldNew, }, { Name: "set old row on update", SetUpScript: []string{ "create table x (a int primary key, b int, c int)", }, Query: "create trigger update_old before update on x for each row set old.c = new.a + 1", ExpectedErr: sql.ErrInvalidUpdateOfOldRow, }, { Name: "set old row on update, begin block", SetUpScript: []string{ "create table x (a int primary key, b int, c int)", }, Query: "create trigger update_old before update on x for each row BEGIN set old.c = new.a + 1; END", ExpectedErr: sql.ErrInvalidUpdateOfOldRow, }, { Name: "set new row after insert", SetUpScript: []string{ "create table x (a int primary key, b int, c int)", }, Query: "create trigger update_new after insert on x for each row set new.c = new.a + 1", ExpectedErr: sql.ErrInvalidUpdateInAfterTrigger, }, { Name: "set new row after update", SetUpScript: []string{ "create table x (a int primary key, b int, c int)", }, Query: "create trigger update_new after update on x for each row set new.c = new.a + 1", ExpectedErr: sql.ErrInvalidUpdateInAfterTrigger, }, { Name: "set new row after update, begin block", SetUpScript: []string{ "create table x (a int primary key, b int, c int)", }, Query: "create trigger update_new after update on x for each row BEGIN set new.c = new.a + 1; END", ExpectedErr: sql.ErrInvalidUpdateInAfterTrigger, }, { Name: "source column doesn't exist", SetUpScript: []string{ "create table x (a int primary key, b int, c int)", }, Query: "create trigger not_found before insert on x for each row set new.d = new.d + 1", ExpectedErr: sql.ErrTableColumnNotFound, }, }
var TriggerTests = []ScriptTest{ { Name: "trigger after insert, insert into other table", SetUpScript: []string{ "create table a (x int primary key)", "create table b (y int primary key)", "create trigger insert_into_b after insert on a for each row insert into b values (new.x + 1)", "insert into a values (1), (3), (5)", }, Assertions: []ScriptTestAssertion{ { Query: "select x from a order by 1", Expected: []sql.Row{ {1}, {3}, {5}, }, }, { Query: "select y from b order by 1", Expected: []sql.Row{ {2}, {4}, {6}, }, }, { Query: "insert into a values (7), (9)", Expected: []sql.Row{ {sql.OkResult{RowsAffected: 2}}, }, }, }, }, { Name: "trigger after insert, delete from other table", SetUpScript: []string{ "create table a (x int primary key)", "create table b (y int primary key)", "insert into b values (0), (2), (4), (6), (8)", "create trigger insert_into_b after insert on a for each row delete from b where y = (new.x + 1)", "insert into a values (1), (3), (5)", }, Assertions: []ScriptTestAssertion{ { Query: "select x from a order by 1", Expected: []sql.Row{ {1}, {3}, {5}, }, }, { Query: "select y from b order by 1", Expected: []sql.Row{ {0}, {8}, }, }, { Query: "insert into a values (7), (9)", Expected: []sql.Row{ {sql.OkResult{RowsAffected: 2}}, }, }, }, }, { Name: "trigger after insert, update other table", SetUpScript: []string{ "create table a (x int primary key)", "create table b (y int primary key)", "insert into b values (0), (2), (4), (6), (8)", "create trigger insert_into_b after insert on a for each row update b set y = new.x where y = new.x + 1", "insert into a values (1), (3), (5)", }, Assertions: []ScriptTestAssertion{ { Query: "select x from a order by 1", Expected: []sql.Row{ {1}, {3}, {5}, }, }, { Query: "select y from b order by 1", Expected: []sql.Row{ {0}, {1}, {3}, {5}, {8}, }, }, }, }, { Name: "trigger before insert, insert into other table", SetUpScript: []string{ "create table a (x int primary key)", "create table b (y int primary key)", "create trigger insert_into_b before insert on a for each row insert into b values (new.x + 1)", "insert into a values (1), (3), (5)", }, Assertions: []ScriptTestAssertion{ { Query: "select x from a order by 1", Expected: []sql.Row{ {1}, {3}, {5}, }, }, { Query: "select y from b order by 1", Expected: []sql.Row{ {2}, {4}, {6}, }, }, { Query: "insert into a values (7), (9)", Expected: []sql.Row{ {sql.OkResult{RowsAffected: 2}}, }, }, }, }, { Name: "trigger before insert, delete from other table", SetUpScript: []string{ "create table a (x int primary key)", "create table b (y int primary key)", "insert into b values (0), (2), (4), (6), (8)", "create trigger insert_into_b before insert on a for each row delete from b where y = (new.x + 1)", "insert into a values (1), (3), (5)", }, Assertions: []ScriptTestAssertion{ { Query: "select x from a order by 1", Expected: []sql.Row{ {1}, {3}, {5}, }, }, { Query: "select y from b order by 1", Expected: []sql.Row{ {0}, {8}, }, }, { Query: "insert into a values (7), (9)", Expected: []sql.Row{ {sql.OkResult{RowsAffected: 2}}, }, }, }, }, { Name: "trigger before insert, update other table", SetUpScript: []string{ "create table a (x int primary key)", "create table b (y int primary key)", "insert into b values (0), (2), (4), (6), (8)", "create trigger insert_into_b before insert on a for each row update b set y = new.x where y = new.x + 1", "insert into a values (1), (3), (5)", }, Assertions: []ScriptTestAssertion{ { Query: "select x from a order by 1", Expected: []sql.Row{ {1}, {3}, {5}, }, }, { Query: "select y from b order by 1", Expected: []sql.Row{ {0}, {1}, {3}, {5}, {8}, }, }, }, }, { Name: "trigger before insert, alter inserted value", SetUpScript: []string{ "create table a (x int primary key)", "create trigger insert_into_a before insert on a for each row set new.x = new.x + 1", "insert into a values (1)", }, Query: "select x from a order by 1", Expected: []sql.Row{ {2}, }, }, { Name: "trigger before insert, alter inserted value, multiple columns", SetUpScript: []string{ "create table x (a int primary key, b int, c int)", "create trigger insert_into_x before insert on x for each row set new.a = new.a + 1, new.b = new.c, new.c = 0", "insert into x values (1, 10, 100)", }, Query: "select * from x order by 1", Expected: []sql.Row{ {2, 100, 0}, }, }, { Name: "trigger before insert, alter inserted value, multiple columns, system var", SetUpScript: []string{ "create table x (a int primary key, b int, c int)", "set @@auto_increment_increment = 1", "create trigger insert_into_x before insert on x for each row " + "set new.a = new.a + 1, new.b = new.c, new.c = 0, @@auto_increment_increment = @@auto_increment_increment + 1", "insert into x values (1, 10, 100), (2, 20, 200)", }, Query: "select *, @@auto_increment_increment from x order by 1", Expected: []sql.Row{ {2, 100, 0, 3}, {3, 200, 0, 3}, }, }, { Name: "trigger before insert, alter inserted value, out of order insertion", SetUpScript: []string{ "create table a (x int primary key, y int)", "create trigger a1 before insert on a for each row set new.x = new.x * 2, new.y = new.y * 3", }, Assertions: []ScriptTestAssertion{ { Query: "insert into a (y, x) values (5,7), (9,11)", Expected: []sql.Row{ {sql.OkResult{RowsAffected: 2}}, }, }, { Query: "select x, y from a order by 1", Expected: []sql.Row{ {14, 15}, {22, 27}, }, }, }, }, { Name: "trigger before insert, alter inserted value, incomplete insertion", SetUpScript: []string{ "create table a (x int primary key, y int, z int default 5)", "create trigger a1 before insert on a for each row set new.x = new.x * 2, new.y = new.y * 3, new.z = new.z * 5", }, Assertions: []ScriptTestAssertion{ { Query: "insert into a (y, x) values (5,7), (9,11)", Expected: []sql.Row{ {sql.OkResult{RowsAffected: 2}}, }, }, { Query: "select x, y, z from a order by 1", Expected: []sql.Row{ {14, 15, 25}, {22, 27, 25}, }, }, }, }, { Name: "trigger before insert, begin block with multiple set statements", SetUpScript: []string{ "CREATE TABLE test(pk BIGINT PRIMARY KEY, v1 BIGINT);", "INSERT INTO test VALUES (0,2),(1,3)", "CREATE TRIGGER tt BEFORE INSERT ON test FOR EACH ROW BEGIN SET NEW.v1 = NEW.v1 * 11; SET NEW.v1 = NEW.v1 * -10; END;", "INSERT INTO test VALUES (2,4);", }, Assertions: []ScriptTestAssertion{ { Query: "SELECT * FROM test ORDER BY 1", Expected: []sql.Row{ {0, 2}, {1, 3}, {2, -440}, }, }, }, }, { Name: "Create a trigger on a new database and verify that the trigger works when selected on another database", SetUpScript: []string{ "create database test", "create table test.a (x int primary key)", "create table test.b (y int primary key)", "use test", "create trigger insert_into_b after insert on test.a for each row insert into test.b values (new.x + 1)", "use mydb", "insert into test.a values (1), (3), (5)", }, Assertions: []ScriptTestAssertion{ { Query: "select x from test.a order by 1", Expected: []sql.Row{ {1}, {3}, {5}, }, }, { Query: "select y from test.b order by 1", Expected: []sql.Row{ {2}, {4}, {6}, }, }, { Query: "insert into test.a values (7), (9)", Expected: []sql.Row{ {sql.OkResult{RowsAffected: 2}}, }, }, }, }, { Name: "trigger after update, insert into other table", SetUpScript: []string{ "create table a (x int primary key)", "create table b (y int primary key)", "insert into a values (1), (3), (5)", "create trigger insert_into_b after update on a for each row insert into b values (old.x + new.x + 1)", "update a set x = x + 1 where x in (1, 3)", }, Assertions: []ScriptTestAssertion{ { Query: "select x from a order by 1", Expected: []sql.Row{ {2}, {4}, {5}, }, }, { Query: "select y from b order by 1", Expected: []sql.Row{ {4}, {8}, }, }, { Query: "update a set x = x + 1 where x = 5", Expected: []sql.Row{ {sql.OkResult{ RowsAffected: 1, Info: plan.UpdateInfo{ Matched: 1, Updated: 1, }, }}, }, }, }, }, { Name: "trigger after update, delete from other table", SetUpScript: []string{ "create table a (x int primary key)", "create table b (y int primary key)", "insert into a values (0), (2), (4), (6), (8)", "insert into b values (1), (3), (5), (7), (9)", "create trigger delete_from_b after update on a for each row delete from b where y = old.x + new.x", "update a set x = x + 1 where x in (2,4)", }, Assertions: []ScriptTestAssertion{ { Query: "select x from a order by 1", Expected: []sql.Row{ {0}, {3}, {5}, {6}, {8}, }, }, { Query: "select y from b order by 1", Expected: []sql.Row{ {1}, {3}, {7}, }, }, }, }, { Name: "trigger after update, update other table", SetUpScript: []string{ "create table a (x int primary key)", "create table b (y int primary key)", "insert into a values (0), (2), (4), (6), (8)", "insert into b values (0), (2), (4), (8)", "create trigger update_b after update on a for each row update b set y = old.x + new.x + 1 where y = old.x", "update a set x = x + 1 where x in (2, 4)", }, Assertions: []ScriptTestAssertion{ { Query: "select x from a order by 1", Expected: []sql.Row{ {0}, {3}, {5}, {6}, {8}, }, }, { Query: "select y from b order by 1", Expected: []sql.Row{ {0}, {6}, {8}, {10}, }, }, }, }, { Name: "trigger before update, insert into other table", SetUpScript: []string{ "create table a (x int primary key)", "create table b (y int primary key)", "insert into a values (1), (3), (5)", "create trigger insert_into_b before update on a for each row insert into b values (old.x + new.x + 1)", "update a set x = x + 1 where x in (1, 3)", }, Assertions: []ScriptTestAssertion{ { Query: "select x from a order by 1", Expected: []sql.Row{ {2}, {4}, {5}, }, }, { Query: "select y from b order by 1", Expected: []sql.Row{ {4}, {8}, }, }, { Query: "update a set x = x + 1 where x = 5", Expected: []sql.Row{ {sql.OkResult{ RowsAffected: 1, Info: plan.UpdateInfo{ Matched: 1, Updated: 1, }, }}, }, }, }, }, { Name: "trigger before update, delete from other table", SetUpScript: []string{ "create table a (x int primary key)", "create table b (y int primary key)", "insert into a values (0), (2), (4), (6), (8)", "insert into b values (1), (3), (5), (7), (9)", "create trigger delete_from_b before update on a for each row delete from b where y = old.x + new.x", "update a set x = x + 1 where x in (2,4)", }, Assertions: []ScriptTestAssertion{ { Query: "select x from a order by 1", Expected: []sql.Row{ {0}, {3}, {5}, {6}, {8}, }, }, { Query: "select y from b order by 1", Expected: []sql.Row{ {1}, {3}, {7}, }, }, }, }, { Name: "trigger before update, update other table", SetUpScript: []string{ "create table a (x int primary key)", "create table b (y int primary key)", "insert into a values (0), (2), (4), (6), (8)", "insert into b values (0), (2), (4), (8)", "create trigger update_b before update on a for each row update b set y = old.x + new.x + 1 where y = old.x", "update a set x = x + 1 where x in (2, 4)", }, Assertions: []ScriptTestAssertion{ { Query: "select x from a order by 1", Expected: []sql.Row{ {0}, {3}, {5}, {6}, {8}, }, }, { Query: "select y from b order by 1", Expected: []sql.Row{ {0}, {6}, {8}, {10}, }, }, }, }, { Name: "trigger before update, set new value", SetUpScript: []string{ "create table a (x int primary key)", "insert into a values (1), (10)", "create trigger update_a before update on a for each row set new.x = new.x + old.x", "update a set x = x + 1", }, Query: "select x from a order by 1", Expected: []sql.Row{ {3}, {21}, }, }, { Name: "trigger before update, set new value to old value", SetUpScript: []string{ "create table a (x int primary key)", "insert into a values (1), (10)", "create trigger no_step_on_snek before update on a for each row set new.x = old.x", "update a set x = x + 1", }, Query: "select x from a order by 1", Expected: []sql.Row{ {1}, {10}, }, }, { Name: "trigger before update, set new values, multiple cols", SetUpScript: []string{ "create table a (x int primary key, y int)", "insert into a values (1,3), (10,20)", "create trigger update_a before update on a for each row set new.x = new.x + old.y, new.y = new.y + old.x", "update a set x = x + 1, y = y + 1", }, Query: "select x, y from a order by 1", Expected: []sql.Row{ {5, 5}, {31, 31}, }, }, { Name: "trigger before update, set new values, multiple cols (2)", SetUpScript: []string{ "create table a (x int primary key, y int)", "insert into a values (1,3), (10,20)", "create trigger update_a before update on a for each row set new.x = new.x + new.y, new.y = new.y + old.y", "update a set x = x + 1, y = y + 1", }, Query: "select x, y from a order by 1", Expected: []sql.Row{ {6, 7}, {32, 41}, }, }, { Name: "trigger before update, with indexed update", SetUpScript: []string{ "create table a (x int primary key, y int, unique key (y))", "create table b (z int primary key)", "insert into a values (1,3), (10,20)", "create trigger insert_b before update on a for each row insert into b values (old.x * 10)", "update a set x = x + 1 where y = 20", }, Assertions: []ScriptTestAssertion{ { Query: "select x, y from a order by 1", Expected: []sql.Row{ {1, 3}, {11, 20}, }, }, { Query: "select z from b", Expected: []sql.Row{ {100}, }, }, }, }, { Name: "trigger before update, begin block with multiple set statements", SetUpScript: []string{ "CREATE TABLE test(pk BIGINT PRIMARY KEY, v1 BIGINT);", "INSERT INTO test VALUES (0,2),(1,3)", "CREATE TRIGGER tt BEFORE UPDATE ON test FOR EACH ROW BEGIN SET NEW.v1 = (OLD.v1 * 2) + NEW.v1; SET NEW.v1 = NEW.v1 * -10; END;", "UPDATE test SET v1 = v1 + 1;", }, Assertions: []ScriptTestAssertion{ { Query: "SELECT * FROM test ORDER BY 1", Expected: []sql.Row{ {0, -70}, {1, -100}, }, }, }, }, { Name: "trigger after delete, insert into other table", SetUpScript: []string{ "create table a (x int primary key)", "create table b (y int primary key)", "insert into a values (1), (3), (5)", "create trigger insert_into_b after delete on a for each row insert into b values (old.x + 1)", "delete from a where x in (1, 3)", }, Assertions: []ScriptTestAssertion{ { Query: "select x from a order by 1", Expected: []sql.Row{ {5}, }, }, { Query: "select y from b order by 1", Expected: []sql.Row{ {2}, {4}, }, }, { Query: "delete from a where x = 5", Expected: []sql.Row{ {sql.OkResult{RowsAffected: 1}}, }, }, }, }, { Name: "trigger after delete, delete from other table", SetUpScript: []string{ "create table a (x int primary key)", "create table b (y int primary key)", "insert into a values (0), (2), (4), (6), (8)", "insert into b values (0), (2), (4), (6), (8)", "create trigger delete_from_b after delete on a for each row delete from b where y = old.x", "delete from a where x in (2,4,6)", }, Assertions: []ScriptTestAssertion{ { Query: "select x from a order by 1", Expected: []sql.Row{ {0}, {8}, }, }, { Query: "select y from b order by 1", Expected: []sql.Row{ {0}, {8}, }, }, }, }, { Name: "trigger after delete, update other table", SetUpScript: []string{ "create table a (x int primary key)", "create table b (y int primary key)", "insert into a values (0), (2), (4), (6), (8)", "insert into b values (0), (2), (4), (6), (8)", "create trigger update_b after delete on a for each row update b set y = old.x + 1 where y = old.x", "delete from a where x in (2,4,6)", }, Assertions: []ScriptTestAssertion{ { Query: "select x from a order by 1", Expected: []sql.Row{ {0}, {8}, }, }, { Query: "select y from b order by 1", Expected: []sql.Row{ {0}, {3}, {5}, {7}, {8}, }, }, }, }, { Name: "trigger before delete, insert into other table", SetUpScript: []string{ "create table a (x int primary key)", "create table b (y int primary key)", "insert into a values (0), (2), (4), (6), (8)", "create trigger insert_into_b before delete on a for each row insert into b values (old.x + 1)", "delete from a where x in (2, 4, 6)", }, Assertions: []ScriptTestAssertion{ { Query: "select x from a order by 1", Expected: []sql.Row{ {0}, {8}, }, }, { Query: "select y from b order by 1", Expected: []sql.Row{ {3}, {5}, {7}, }, }, { Query: "delete from a where x = 0", Expected: []sql.Row{ {sql.OkResult{RowsAffected: 1}}, }, }, }, }, { Name: "trigger before delete, delete from other table", SetUpScript: []string{ "create table a (x int primary key)", "create table b (y int primary key)", "insert into a values (0), (2), (4), (6), (8)", "insert into b values (1), (3), (5), (7), (9)", "create trigger delete_from_b before delete on a for each row delete from b where y = (old.x + 1)", "delete from a where x in (2, 4, 6)", }, Assertions: []ScriptTestAssertion{ { Query: "select x from a order by 1", Expected: []sql.Row{ {0}, {8}, }, }, { Query: "select y from b order by 1", Expected: []sql.Row{ {1}, {9}, }, }, }, }, { Name: "trigger before delete, update other table", SetUpScript: []string{ "create table a (x int primary key)", "create table b (y int primary key)", "insert into a values (0), (2), (4), (6), (8)", "insert into b values (1), (3), (5), (7), (9)", "create trigger update_b before delete on a for each row update b set y = old.x where y = old.x + 1", "delete from a where x in (2, 4, 6)", }, Assertions: []ScriptTestAssertion{ { Query: "select x from a order by 1", Expected: []sql.Row{ {0}, {8}, }, }, { Query: "select y from b order by 1", Expected: []sql.Row{ {1}, {2}, {4}, {6}, {9}, }, }, }, }, { Name: "trigger before delete, delete with index", SetUpScript: []string{ "create table a (x int primary key, z int, unique key (z))", "create table b (y int primary key)", "insert into a values (0,1), (2,3), (4,5)", "create trigger insert_b before delete on a for each row insert into b values (old.x * 2)", "delete from a where z > 2", }, Assertions: []ScriptTestAssertion{ { Query: "select x from a order by 1", Expected: []sql.Row{ {0}, }, }, { Query: "select y from b order by 1", Expected: []sql.Row{ {4}, {8}, }, }, }, }, { Name: "triggers before and after insert", SetUpScript: []string{ "create table a (x int primary key)", "create table b (y int primary key)", "create trigger a1 before insert on a for each row insert into b values (NEW.x * 7)", "create trigger a2 after insert on a for each row insert into b values (New.x * 11)", }, Assertions: []ScriptTestAssertion{ { Query: "insert into a values (2), (3), (5)", Expected: []sql.Row{ {sql.NewOkResult(3)}, }, }, { Query: "select x from a order by 1", Expected: []sql.Row{ {2}, {3}, {5}, }, }, { Query: "select y from b order by 1", Expected: []sql.Row{ {14}, {21}, {22}, {33}, {35}, {55}, }, }, }, }, { Name: "multiple triggers before insert", SetUpScript: []string{ "create table a (x int primary key)", "create trigger a1 before insert on a for each row set new.x = New.x + 1", "create trigger a2 before insert on a for each row set new.x = New.x * 2", "create trigger a3 before insert on a for each row set new.x = New.x - 5", }, Assertions: []ScriptTestAssertion{ { Query: "insert into a values (1), (3)", Expected: []sql.Row{ {sql.NewOkResult(2)}, }, }, { Query: "select x from a order by 1", Expected: []sql.Row{ {-1}, {3}, }, }, }, }, { Name: "multiple triggers before insert, with precedes / follows", SetUpScript: []string{ "create table a (x int primary key)", "create trigger a1 before insert on a for each row set new.x = New.x + 1", "create trigger a2 before insert on a for each row precedes a1 set new.x = New.x * 2", "create trigger a3 before insert on a for each row precedes a2 set new.x = New.x - 5", "create trigger a4 before insert on a for each row follows a2 set new.x = New.x * 3", }, Assertions: []ScriptTestAssertion{ { Query: "insert into a values (1), (3)", Expected: []sql.Row{ {sql.NewOkResult(2)}, }, }, { Query: "select x from a order by 1", Expected: []sql.Row{ {-23}, {-11}, }, }, }, }, { Name: "triggers before and after update", SetUpScript: []string{ "create table a (x int primary key)", "create table b (y int primary key)", "create trigger a1 before update on a for each row insert into b values (old.x * 7)", "create trigger a2 after update on a for each row insert into b values (old.x * 11)", "insert into a values (2), (3), (5)", }, Assertions: []ScriptTestAssertion{ { Query: "update a set x = x * 2", Expected: []sql.Row{ {sql.OkResult{ RowsAffected: 3, Info: plan.UpdateInfo{ Matched: 3, Updated: 3, }, }}, }, }, { Query: "select x from a order by 1", Expected: []sql.Row{ {4}, {6}, {10}, }, }, { Query: "select y from b order by 1", Expected: []sql.Row{ {14}, {21}, {22}, {33}, {35}, {55}, }, }, }, }, { Name: "multiple triggers before and after update", SetUpScript: []string{ "create table a (x int primary key)", "create table b (y int primary key)", "create trigger a1 before update on a for each row insert into b values (old.x * 7)", "create trigger a2 after update on a for each row insert into b values (old.x * 11)", "create trigger a3 before update on a for each row insert into b values (old.x * 13)", "create trigger a4 after update on a for each row insert into b values (old.x * 17)", "insert into a values (2), (3), (5)", }, Assertions: []ScriptTestAssertion{ { Query: "update a set x = x * 2", Expected: []sql.Row{ {sql.OkResult{ RowsAffected: 3, Info: plan.UpdateInfo{ Matched: 3, Updated: 3, }, }}, }, }, { Query: "select x from a order by 1", Expected: []sql.Row{ {4}, {6}, {10}, }, }, { Query: "select y from b order by 1", Expected: []sql.Row{ {14}, {21}, {22}, {26}, {33}, {34}, {35}, {39}, {51}, {55}, {65}, {85}, }, }, }, }, { Name: "triggers before and after delete", SetUpScript: []string{ "create table a (x int primary key)", "create table b (y int primary key)", "create trigger a1 before delete on a for each row insert into b values (old.x * 7)", "create trigger a2 after delete on a for each row insert into b values (old.x * 11)", "insert into a values (2), (3), (5)", }, Assertions: []ScriptTestAssertion{ { Query: "delete from a", Expected: []sql.Row{ {sql.NewOkResult(3)}, }, }, { Query: "select x from a order by 1", Expected: []sql.Row{}, }, { Query: "select y from b order by 1", Expected: []sql.Row{ {14}, {21}, {22}, {33}, {35}, {55}, }, }, }, }, { Name: "multiple triggers before and after delete", SetUpScript: []string{ "create table a (x int primary key)", "create table b (y int primary key)", "create trigger a1 before delete on a for each row insert into b values (old.x * 7)", "create trigger a2 after delete on a for each row insert into b values (old.x * 11)", "create trigger a3 before delete on a for each row insert into b values (old.x * 13)", "create trigger a4 after delete on a for each row insert into b values (old.x * 17)", "insert into a values (2), (3), (5)", }, Assertions: []ScriptTestAssertion{ { Query: "delete from a", Expected: []sql.Row{ {sql.NewOkResult(3)}, }, }, { Query: "select x from a order by 1", Expected: []sql.Row{}, }, { Query: "select y from b order by 1", Expected: []sql.Row{ {14}, {21}, {22}, {26}, {33}, {34}, {35}, {39}, {51}, {55}, {65}, {85}, }, }, }, }, { Name: "multiple triggers before and after insert, with precedes / follows", SetUpScript: []string{ "create table a (x int primary key)", "create table b (y int primary key)", "insert into b values (1), (3)", "create trigger a1 before insert on a for each row set new.x = New.x + 1", "create trigger a2 before insert on a for each row precedes a1 set new.x = New.x * 2", "create trigger a3 before insert on a for each row precedes a2 set new.x = New.x - 5", "create trigger a4 before insert on a for each row follows a2 set new.x = New.x * 3", "create trigger a5 after insert on a for each row update b set y = y + 1 order by y asc", "create trigger a6 after insert on a for each row precedes a5 update b set y = y * 2 order by y asc", "create trigger a7 after insert on a for each row precedes a6 update b set y = y - 5 order by y asc", "create trigger a8 after insert on a for each row follows a6 update b set y = y * 3 order by y asc", }, Assertions: []ScriptTestAssertion{ { Query: "insert into a values (1), (3)", Expected: []sql.Row{ {sql.NewOkResult(2)}, }, }, { Query: "select x from a order by 1", Expected: []sql.Row{ {-23}, {-11}, }, }, { Query: "select y from b order by 1", Expected: []sql.Row{ {-167}, {-95}, }, }, }, }, { Name: "trigger before insert, multiple triggers defined", SetUpScript: []string{ "create table a (x int primary key)", "create table b (y int primary key)", "create table c (z int primary key)", "create trigger a1 before insert on a for each row insert into b values (new.x * 2)", "create trigger a2 before update on a for each row insert into b values (new.x * 3)", "create trigger a3 before delete on a for each row insert into b values (old.x * 5)", "create trigger b1 before insert on b for each row insert into c values (new.y * 7)", "create trigger b2 before update on b for each row insert into c values (new.y * 11)", "create trigger b3 before delete on b for each row insert into c values (old.y * 13)", "insert into a values (1), (2), (3)", }, Assertions: []ScriptTestAssertion{ { Query: "select x from a order by 1", Expected: []sql.Row{ {1}, {2}, {3}, }, }, { Query: "select y from b order by 1", Expected: []sql.Row{ {2}, {4}, {6}, }, }, { Query: "select z from c order by 1", Expected: []sql.Row{ {14}, {28}, {42}, }, }, }, }, { Name: "trigger with signal", SetUpScript: []string{ "create table a (x int primary key)", "create table b (y int primary key)", "create table c (z int primary key)", "insert into c values (-1)", `create trigger trig_with_signal before insert on a for each row begin declare cond_name condition for sqlstate '45000'; if new.x = 5 then signal cond_name set message_text = 'trig err'; end if; insert into b values (new.x + 1); update c set z = new.x; end;`, }, Assertions: []ScriptTestAssertion{ { Query: "insert into a values (1), (3)", Expected: []sql.Row{ {sql.OkResult{RowsAffected: 2}}, }, }, { Query: "insert into a values (5)", ExpectedErrStr: "trig err (errno 1644) (sqlstate 45000)", }, { Query: "select x from a order by 1", Expected: []sql.Row{ {1}, {3}, }, }, { Query: "select y from b order by 1", Expected: []sql.Row{ {2}, {4}, }, }, { Query: "select z from c order by 1", Expected: []sql.Row{ {3}, }, }, }, }, { Name: "infoschema for multiple triggers before and after insert, with precedes / follows", SetUpScript: []string{ "create table a (x int primary key)", "create table b (y int primary key)", "insert into b values (1), (3)", "create trigger a1 before insert on a for each row set new.x = New.x + 1", "create trigger a2 before insert on a for each row precedes a1 set new.x = New.x * 2", "create trigger a3 before insert on a for each row precedes a2 set new.x = New.x - 5", "create trigger a4 before insert on a for each row follows a2 set new.x = New.x * 3", "create trigger a5 after insert on a for each row update b set y = y + 1 order by y asc", "create trigger a6 after insert on a for each row precedes a5 update b set y = y * 2 order by y asc", "create trigger a7 after insert on a for each row precedes a6 update b set y = y - 5 order by y asc", "create trigger a8 after insert on a for each row follows a6 update b set y = y * 3 order by y asc", "insert into a values (1), (3)", }, Assertions: []ScriptTestAssertion{ { Query: "select * from information_schema.triggers", Expected: []sql.Row{ { "def", "mydb", "a1", "INSERT", "def", "mydb", "a", int64(4), nil, "set new.x = New.x + 1", "ROW", "BEFORE", nil, nil, "OLD", "NEW", time.Unix(0, 0).UTC(), "", "", sql.Collation_Default.CharacterSet().String(), sql.Collation_Default.String(), sql.Collation_Default.String(), }, { "def", "mydb", "a2", "INSERT", "def", "mydb", "a", int64(2), nil, "set new.x = New.x * 2", "ROW", "BEFORE", nil, nil, "OLD", "NEW", time.Unix(0, 0).UTC(), "", "", sql.Collation_Default.CharacterSet().String(), sql.Collation_Default.String(), sql.Collation_Default.String(), }, { "def", "mydb", "a3", "INSERT", "def", "mydb", "a", int64(1), nil, "set new.x = New.x - 5", "ROW", "BEFORE", nil, nil, "OLD", "NEW", time.Unix(0, 0).UTC(), "", "", sql.Collation_Default.CharacterSet().String(), sql.Collation_Default.String(), sql.Collation_Default.String(), }, { "def", "mydb", "a4", "INSERT", "def", "mydb", "a", int64(3), nil, "set new.x = New.x * 3", "ROW", "BEFORE", nil, nil, "OLD", "NEW", time.Unix(0, 0).UTC(), "", "", sql.Collation_Default.CharacterSet().String(), sql.Collation_Default.String(), sql.Collation_Default.String(), }, { "def", "mydb", "a5", "INSERT", "def", "mydb", "a", int64(4), nil, "update b set y = y + 1 order by y asc", "ROW", "AFTER", nil, nil, "OLD", "NEW", time.Unix(0, 0).UTC(), "", "", sql.Collation_Default.CharacterSet().String(), sql.Collation_Default.String(), sql.Collation_Default.String(), }, { "def", "mydb", "a6", "INSERT", "def", "mydb", "a", int64(2), nil, "update b set y = y * 2 order by y asc", "ROW", "AFTER", nil, nil, "OLD", "NEW", time.Unix(0, 0).UTC(), "", "", sql.Collation_Default.CharacterSet().String(), sql.Collation_Default.String(), sql.Collation_Default.String(), }, { "def", "mydb", "a7", "INSERT", "def", "mydb", "a", int64(1), nil, "update b set y = y - 5 order by y asc", "ROW", "AFTER", nil, nil, "OLD", "NEW", time.Unix(0, 0).UTC(), "", "", sql.Collation_Default.CharacterSet().String(), sql.Collation_Default.String(), sql.Collation_Default.String(), }, { "def", "mydb", "a8", "INSERT", "def", "mydb", "a", int64(3), nil, "update b set y = y * 3 order by y asc", "ROW", "AFTER", nil, nil, "OLD", "NEW", time.Unix(0, 0).UTC(), "", "", sql.Collation_Default.CharacterSet().String(), sql.Collation_Default.String(), sql.Collation_Default.String(), }, }, }, }, }, { Name: "show create triggers", SetUpScript: []string{ "create table a (x int primary key)", "create trigger a1 before insert on a for each row set new.x = new.x + 1", "create table b (y int primary key)", "create trigger b1 before insert on b for each row set new.y = new.y + 2", }, Assertions: []ScriptTestAssertion{ { Query: "show create trigger a1", Expected: []sql.Row{ { "a1", "", "create trigger a1 before insert on a for each row set new.x = new.x + 1", sql.Collation_Default.CharacterSet().String(), sql.Collation_Default.String(), sql.Collation_Default.String(), time.Unix(0, 0).UTC(), }, }, }, { Query: "show create trigger b1", Expected: []sql.Row{ { "b1", "", "create trigger b1 before insert on b for each row set new.y = new.y + 2", sql.Collation_Default.CharacterSet().String(), sql.Collation_Default.String(), sql.Collation_Default.String(), time.Unix(0, 0).UTC(), }, }, }, { Query: "show create trigger b2", ExpectedErr: sql.ErrTriggerDoesNotExist, }, }, }, { Name: "show triggers", SetUpScript: []string{ "create table abb (x int primary key)", "create table acc (y int primary key)", "create trigger t1 before insert on abb for each row set new.x = new.x + 1", "create trigger t2 before insert on abb for each row set new.x = new.x + 2", "create trigger t3 after insert on acc for each row insert into abb values (new.y)", "create trigger t4 before update on acc for each row set new.y = old.y + 2", }, Assertions: []ScriptTestAssertion{ { Query: "show triggers", Expected: []sql.Row{ { "t1", "INSERT", "abb", "set new.x = new.x + 1", "BEFORE", time.Unix(0, 0).UTC(), "", "", sql.Collation_Default.CharacterSet().String(), sql.Collation_Default.String(), sql.Collation_Default.String(), }, { "t2", "INSERT", "abb", "set new.x = new.x + 2", "BEFORE", time.Unix(0, 0).UTC(), "", "", sql.Collation_Default.CharacterSet().String(), sql.Collation_Default.String(), sql.Collation_Default.String(), }, { "t3", "INSERT", "acc", "insert into abb values (new.y)", "AFTER", time.Unix(0, 0).UTC(), "", "", sql.Collation_Default.CharacterSet().String(), sql.Collation_Default.String(), sql.Collation_Default.String(), }, { "t4", "UPDATE", "acc", "set new.y = old.y + 2", "BEFORE", time.Unix(0, 0).UTC(), "", "", sql.Collation_Default.CharacterSet().String(), sql.Collation_Default.String(), sql.Collation_Default.String(), }, }, }, { Query: "show triggers from mydb", Expected: []sql.Row{ { "t1", "INSERT", "abb", "set new.x = new.x + 1", "BEFORE", time.Unix(0, 0).UTC(), "", "", sql.Collation_Default.CharacterSet().String(), sql.Collation_Default.String(), sql.Collation_Default.String(), }, { "t2", "INSERT", "abb", "set new.x = new.x + 2", "BEFORE", time.Unix(0, 0).UTC(), "", "", sql.Collation_Default.CharacterSet().String(), sql.Collation_Default.String(), sql.Collation_Default.String(), }, { "t3", "INSERT", "acc", "insert into abb values (new.y)", "AFTER", time.Unix(0, 0).UTC(), "", "", sql.Collation_Default.CharacterSet().String(), sql.Collation_Default.String(), sql.Collation_Default.String(), }, { "t4", "UPDATE", "acc", "set new.y = old.y + 2", "BEFORE", time.Unix(0, 0).UTC(), "", "", sql.Collation_Default.CharacterSet().String(), sql.Collation_Default.String(), sql.Collation_Default.String(), }, }, }, { Query: "show triggers like '%cc'", Expected: []sql.Row{ { "t3", "INSERT", "acc", "insert into abb values (new.y)", "AFTER", time.Unix(0, 0).UTC(), "", "", sql.Collation_Default.CharacterSet().String(), sql.Collation_Default.String(), sql.Collation_Default.String(), }, { "t4", "UPDATE", "acc", "set new.y = old.y + 2", "BEFORE", time.Unix(0, 0).UTC(), "", "", sql.Collation_Default.CharacterSet().String(), sql.Collation_Default.String(), sql.Collation_Default.String(), }, }, }, { Query: "show triggers where event = 'INSERT'", Expected: []sql.Row{ { "t1", "INSERT", "abb", "set new.x = new.x + 1", "BEFORE", time.Unix(0, 0).UTC(), "", "", sql.Collation_Default.CharacterSet().String(), sql.Collation_Default.String(), sql.Collation_Default.String(), }, { "t2", "INSERT", "abb", "set new.x = new.x + 2", "BEFORE", time.Unix(0, 0).UTC(), "", "", sql.Collation_Default.CharacterSet().String(), sql.Collation_Default.String(), sql.Collation_Default.String(), }, { "t3", "INSERT", "acc", "insert into abb values (new.y)", "AFTER", time.Unix(0, 0).UTC(), "", "", sql.Collation_Default.CharacterSet().String(), sql.Collation_Default.String(), sql.Collation_Default.String(), }, }, }, { Query: "show triggers where timing = 'AFTER'", Expected: []sql.Row{ { "t3", "INSERT", "acc", "insert into abb values (new.y)", "AFTER", time.Unix(0, 0).UTC(), "", "", sql.Collation_Default.CharacterSet().String(), sql.Collation_Default.String(), sql.Collation_Default.String(), }, }, }, { Query: "show triggers where timing = 'BEFORE' and `Table` like '%bb'", Expected: []sql.Row{ { "t1", "INSERT", "abb", "set new.x = new.x + 1", "BEFORE", time.Unix(0, 0).UTC(), "", "", sql.Collation_Default.CharacterSet().String(), sql.Collation_Default.String(), sql.Collation_Default.String(), }, { "t2", "INSERT", "abb", "set new.x = new.x + 2", "BEFORE", time.Unix(0, 0).UTC(), "", "", sql.Collation_Default.CharacterSet().String(), sql.Collation_Default.String(), sql.Collation_Default.String(), }, }, }, }, }, { Name: "drop trigger", SetUpScript: []string{ "create table a (x int primary key)", "create trigger t1 before insert on a for each row set new.x = new.x * 1", "create trigger t2 before insert on a for each row follows t1 set new.x = new.x * 2", "create trigger t3 before insert on a for each row set new.x = new.x * 3", "create trigger t4 before insert on a for each row precedes t3 set new.x = new.x * 5", }, Assertions: []ScriptTestAssertion{ { Query: "drop trigger t1", ExpectedErr: sql.ErrTriggerCannotBeDropped, }, { Query: "drop trigger t3", ExpectedErr: sql.ErrTriggerCannotBeDropped, }, { Query: "drop trigger t4", Expected: []sql.Row{}, }, { Query: "drop trigger t3", Expected: []sql.Row{}, }, { Query: "drop trigger if exists t5", Expected: []sql.Row{}, }, { Query: "drop trigger t5", ExpectedErr: sql.ErrTriggerDoesNotExist, }, { Query: "select trigger_name from information_schema.triggers order by 1", Expected: []sql.Row{ {"t1"}, {"t2"}, }, }, { Query: "drop trigger if exists t2", Expected: []sql.Row{}, }, { Query: "select trigger_name from information_schema.triggers order by 1", Expected: []sql.Row{ {"t1"}, }, }, }, }, { Name: "drop table referenced in triggers", SetUpScript: []string{ "create table a (w int primary key)", "create table b (x int primary key)", "create table c (y int primary key)", "create table d (z int primary key)", "create trigger t1 before insert on a for each row set new.w = new.w", "create trigger t2 before insert on a for each row set new.w = new.w * 100", "create trigger t3 before insert on b for each row set new.x = new.x", "create trigger t4 before insert on b for each row set new.x = new.x * 100", "create trigger t5 before insert on c for each row set new.y = new.y", "create trigger t6 before insert on c for each row set new.y = new.y * 100", "create trigger t7 before insert on d for each row set new.z = new.z", "create trigger t8 before insert on d for each row set new.z = new.z * 100", }, Assertions: []ScriptTestAssertion{ { Query: "drop table a", Expected: []sql.Row{}, }, { Query: "select trigger_name from information_schema.triggers order by 1", Expected: []sql.Row{ {"t3"}, {"t4"}, {"t5"}, {"t6"}, {"t7"}, {"t8"}, }, }, { Query: "drop table if exists b, d, e", Expected: []sql.Row{}, }, { Query: "select trigger_name from information_schema.triggers order by 1", Expected: []sql.Row{ {"t5"}, {"t6"}, }, }, }, }, { Name: "drop table referenced in triggers with follows/precedes", SetUpScript: []string{ "create table a (x int primary key)", "create trigger t1 before insert on a for each row set new.x = new.x", "create trigger t2 before insert on a for each row follows t1 set new.x = new.x * 10", "create trigger t3 before insert on a for each row precedes t1 set new.x = new.x * 100", "create trigger t4 before insert on a for each row follows t3 set new.x = new.x * 1000", "create trigger t5 before insert on a for each row precedes t2 set new.x = new.x * 10000", "create trigger t6 before insert on a for each row follows t4 set new.x = new.x * 100000", "create trigger t7 before insert on a for each row precedes t1 set new.x = new.x * 1000000", "create trigger t8 before insert on a for each row follows t6 set new.x = new.x * 10000000", }, Assertions: []ScriptTestAssertion{ { Query: "drop table a", Expected: []sql.Row{}, }, { Query: "show triggers", Expected: []sql.Row{}, }, }, }, }
var UpdateErrorTests = []GenericErrorQueryTest{
{
Name: "invalid table",
Query: "UPDATE doesnotexist SET i = 0;",
},
{
Name: "missing binding",
Query: "UPDATE mytable SET i = ?;",
},
{
Name: "wrong number of columns",
Query: `UPDATE mytable SET i = ("one", "two");`,
},
{
Name: "type mismatch: string -> int",
Query: `UPDATE mytable SET i = "one"`,
},
{
Name: "type mismatch: string -> float",
Query: `UPDATE floattable SET f64 = "one"`,
},
{
Name: "type mismatch: string -> uint",
Query: `UPDATE typestable SET f64 = "one"`,
},
{
Name: "invalid column set",
Query: "UPDATE mytable SET z = 0;",
},
{
Name: "invalid column set value",
Query: "UPDATE mytable SET i = z;",
},
{
Name: "invalid column where",
Query: "UPDATE mytable SET s = 'hi' WHERE z = 1;",
},
{
Name: "invalid column order by",
Query: "UPDATE mytable SET s = 'hi' ORDER BY z;",
},
{
Name: "negative limit",
Query: "UPDATE mytable SET s = 'hi' LIMIT -1;",
},
{
Name: "negative offset",
Query: "UPDATE mytable SET s = 'hi' LIMIT 1 OFFSET -1;",
},
{
Name: "set null on non-nullable",
Query: "UPDATE mytable SET s = NULL;",
},
}
var UpdateTests = []WriteQueryTest{ { WriteQuery: "UPDATE mytable SET s = 'updated';", ExpectedWriteResult: []sql.Row{{newUpdateResult(3, 3)}}, SelectQuery: "SELECT * FROM mytable;", ExpectedSelect: []sql.Row{{int64(1), "updated"}, {int64(2), "updated"}, {int64(3), "updated"}}, }, { WriteQuery: "UPDATE mytable SET s = ?;", ExpectedWriteResult: []sql.Row{{newUpdateResult(3, 3)}}, SelectQuery: "SELECT * FROM mytable;", ExpectedSelect: []sql.Row{{int64(1), "updated"}, {int64(2), "updated"}, {int64(3), "updated"}}, Bindings: map[string]sql.Expression{ "v1": expression.NewLiteral("updated", sql.Text), }, }, { WriteQuery: "UPDATE mytable SET s = 'updated' WHERE i > 9999;", ExpectedWriteResult: []sql.Row{{newUpdateResult(0, 0)}}, SelectQuery: "SELECT * FROM mytable;", ExpectedSelect: []sql.Row{{int64(1), "first row"}, {int64(2), "second row"}, {int64(3), "third row"}}, }, { WriteQuery: "UPDATE mytable SET s = 'updated' WHERE i = 1;", ExpectedWriteResult: []sql.Row{{newUpdateResult(1, 1)}}, SelectQuery: "SELECT * FROM mytable;", ExpectedSelect: []sql.Row{{int64(1), "updated"}, {int64(2), "second row"}, {int64(3), "third row"}}, }, { WriteQuery: "UPDATE mytable SET s = 'updated' WHERE i <> 9999;", ExpectedWriteResult: []sql.Row{{newUpdateResult(3, 3)}}, SelectQuery: "SELECT * FROM mytable;", ExpectedSelect: []sql.Row{{int64(1), "updated"}, {int64(2), "updated"}, {int64(3), "updated"}}, }, { WriteQuery: "UPDATE floattable SET f32 = f32 + f32, f64 = f32 * f64 WHERE i = 2;", ExpectedWriteResult: []sql.Row{{newUpdateResult(1, 1)}}, SelectQuery: "SELECT * FROM floattable WHERE i = 2;", ExpectedSelect: []sql.Row{{int64(2), float32(3.0), float64(4.5)}}, }, { WriteQuery: "UPDATE floattable SET f32 = 5, f32 = 4 WHERE i = 1;", ExpectedWriteResult: []sql.Row{{newUpdateResult(1, 1)}}, SelectQuery: "SELECT f32 FROM floattable WHERE i = 1;", ExpectedSelect: []sql.Row{{float32(4.0)}}, }, { WriteQuery: "UPDATE mytable SET s = 'first row' WHERE i = 1;", ExpectedWriteResult: []sql.Row{{newUpdateResult(1, 0)}}, SelectQuery: "SELECT * FROM mytable;", ExpectedSelect: []sql.Row{{int64(1), "first row"}, {int64(2), "second row"}, {int64(3), "third row"}}, }, { WriteQuery: "UPDATE niltable SET b = NULL WHERE f IS NULL;", ExpectedWriteResult: []sql.Row{{newUpdateResult(3, 2)}}, SelectQuery: "SELECT i,b FROM niltable WHERE f IS NULL;", ExpectedSelect: []sql.Row{{int64(1), nil}, {int64(2), nil}, {int64(3), nil}}, }, { WriteQuery: "UPDATE mytable SET s = 'updated' ORDER BY i ASC LIMIT 2;", ExpectedWriteResult: []sql.Row{{newUpdateResult(2, 2)}}, SelectQuery: "SELECT * FROM mytable;", ExpectedSelect: []sql.Row{{int64(1), "updated"}, {int64(2), "updated"}, {int64(3), "third row"}}, }, { WriteQuery: "UPDATE mytable SET s = 'updated' ORDER BY i DESC LIMIT 2;", ExpectedWriteResult: []sql.Row{{newUpdateResult(2, 2)}}, SelectQuery: "SELECT * FROM mytable;", ExpectedSelect: []sql.Row{{int64(1), "first row"}, {int64(2), "updated"}, {int64(3), "updated"}}, }, { WriteQuery: "UPDATE mytable SET s = 'updated' ORDER BY i LIMIT 1 OFFSET 1;", ExpectedWriteResult: []sql.Row{{newUpdateResult(1, 1)}}, SelectQuery: "SELECT * FROM mytable;", ExpectedSelect: []sql.Row{{int64(1), "first row"}, {int64(2), "updated"}, {int64(3), "third row"}}, }, { WriteQuery: "UPDATE mytable SET s = 'updated';", ExpectedWriteResult: []sql.Row{{newUpdateResult(3, 3)}}, SelectQuery: "SELECT * FROM mytable;", ExpectedSelect: []sql.Row{{int64(1), "updated"}, {int64(2), "updated"}, {int64(3), "updated"}}, }, { WriteQuery: "UPDATE typestable SET ti = '2020-03-06 00:00:00';", ExpectedWriteResult: []sql.Row{{newUpdateResult(1, 1)}}, SelectQuery: "SELECT * FROM typestable;", ExpectedSelect: []sql.Row{{ int64(1), int8(2), int16(3), int32(4), int64(5), uint8(6), uint16(7), uint32(8), uint64(9), float32(10), float64(11), sql.MustConvert(sql.Timestamp.Convert("2020-03-06 00:00:00")), sql.MustConvert(sql.Date.Convert("2019-12-31")), "fourteen", 0, nil, nil}}, }, { WriteQuery: "UPDATE typestable SET ti = '2020-03-06 00:00:00', da = '2020-03-06';", ExpectedWriteResult: []sql.Row{{newUpdateResult(1, 1)}}, SelectQuery: "SELECT * FROM typestable;", ExpectedSelect: []sql.Row{{ int64(1), int8(2), int16(3), int32(4), int64(5), uint8(6), uint16(7), uint32(8), uint64(9), float32(10), float64(11), sql.MustConvert(sql.Timestamp.Convert("2020-03-06 00:00:00")), sql.MustConvert(sql.Date.Convert("2020-03-06")), "fourteen", 0, nil, nil}}, }, { WriteQuery: "UPDATE typestable SET da = '0000-00-00', ti = '0000-00-00 00:00:00';", ExpectedWriteResult: []sql.Row{{newUpdateResult(1, 1)}}, SelectQuery: "SELECT * FROM typestable;", ExpectedSelect: []sql.Row{{ int64(1), int8(2), int16(3), int32(4), int64(5), uint8(6), uint16(7), uint32(8), uint64(9), float32(10), float64(11), sql.Timestamp.Zero(), sql.Date.Zero(), "fourteen", 0, nil, nil}}, }, }
var VariableErrorTests = []QueryErrorTest{ { Query: "set @myvar = bareword", ExpectedErr: sql.ErrColumnNotFound, }, }
var VariableQueries = []ScriptTest{ { Name: "set system variables", SetUpScript: []string{ "set @@auto_increment_increment = 100, sql_select_limit = 1", }, Query: "SELECT @@auto_increment_increment, @@sql_select_limit", Expected: []sql.Row{ {100, 1}, }, }, { Name: "set system variables mixed case", SetUpScript: []string{ "set @@auto_increment_INCREMENT = 100, sql_select_LIMIT = 1", }, Query: "SELECT @@auto_increment_increment, @@sql_select_limit", Expected: []sql.Row{ {100, 1}, }, }, { Name: "set system variable defaults", SetUpScript: []string{ "set @@auto_increment_increment = 100, sql_select_limit = 1", "set @@auto_increment_increment = default, sql_select_limit = default", }, Query: "SELECT @@auto_increment_increment, @@sql_select_limit", Expected: []sql.Row{ {1, math.MaxInt32}, }, }, { Name: "set system variable ON / OFF", SetUpScript: []string{ "set @@autocommit = ON, sql_mode = OFF", }, Query: "SELECT @@autocommit, @@session.sql_mode", Expected: []sql.Row{ {1, 0}, }, }, { Name: "set system variable true / false quoted", SetUpScript: []string{ `set @@autocommit = "true", sql_mode = "false"`, }, Query: "SELECT @@autocommit, @@session.sql_mode", Expected: []sql.Row{ {1, 0}, }, }, { Name: "set system variable true / false", SetUpScript: []string{ `set @@autocommit = true, sql_mode = false`, }, Query: "SELECT @@autocommit, @@session.sql_mode", Expected: []sql.Row{ {1, 0}, }, }, { Name: "set system variable with expressions", SetUpScript: []string{ `set sql_mode = "123", @@auto_increment_increment = 1`, `set sql_mode = concat(@@sql_mode, "456"), @@auto_increment_increment = @@auto_increment_increment + 3`, }, Query: "SELECT @@sql_mode, @@auto_increment_increment", Expected: []sql.Row{ {"123456", 4}, }, }, { Name: "set system variable to another system variable", SetUpScript: []string{ `set @@auto_increment_increment = 123`, `set @@sql_select_limit = @@auto_increment_increment`, }, Query: "SELECT @@sql_select_limit", Expected: []sql.Row{ {123}, }, }, { Name: "set names", SetUpScript: []string{ `set names utf8mb4`, }, Query: "SELECT @@character_set_client, @@character_set_connection, @@character_set_results", Expected: []sql.Row{ {"utf8mb4", "utf8mb4", "utf8mb4"}, }, }, { Name: "set names quoted", SetUpScript: []string{ `set NAMES "charset"`, }, Query: "SELECT @@character_set_client, @@character_set_connection, @@character_set_results", Expected: []sql.Row{ {"charset", "charset", "charset"}, }, }, { Name: "set system variable to bareword", SetUpScript: []string{ `set @@sql_mode = some_mode`, }, Query: "SELECT @@sql_mode", Expected: []sql.Row{ {"some_mode"}, }, }, { Name: "set system variable to bareword, unqualified", SetUpScript: []string{ `set sql_mode = some_mode`, }, Query: "SELECT @@sql_mode", Expected: []sql.Row{ {"some_mode"}, }, }, { Name: "set unknown system variable", SetUpScript: []string{ `set dne = "hello"`, }, Query: "SELECT @@dne", Expected: []sql.Row{ {"hello"}, }, }, { Name: "set user var", SetUpScript: []string{ `set @myvar = "hello"`, }, Query: "SELECT @myvar", Expected: []sql.Row{ {"hello"}, }, }, { Name: "set user var, integer type", SetUpScript: []string{ `set @myvar = 123`, }, Query: "SELECT @myvar", Expected: []sql.Row{ {123}, }, }, { Name: "set user var, floating point", SetUpScript: []string{ `set @myvar = 123.4`, }, Query: "SELECT @myvar", Expected: []sql.Row{ {123.4}, }, }, { Name: "set user var and sys var in same statement", SetUpScript: []string{ `set @myvar = 123.4, @@auto_increment_increment = 1234`, }, Query: "SELECT @myvar, @@auto_increment_increment", Expected: []sql.Row{ {123.4, 1234}, }, }, { Name: "set sys var to user var", SetUpScript: []string{ `set @myvar = 1234`, `set auto_increment_increment = @myvar`, }, Query: "SELECT @myvar, @@auto_increment_increment", Expected: []sql.Row{ {1234, 1234}, }, }, }
var VersionedQueries = []QueryTest{ { Query: "SELECT * FROM myhistorytable AS OF '2019-01-01' AS foo ORDER BY i", Expected: []sql.Row{ {int64(1), "first row, 1"}, {int64(2), "second row, 1"}, {int64(3), "third row, 1"}, }, }, { Query: "SELECT * FROM myhistorytable AS OF '2019-01-02' foo ORDER BY i", Expected: []sql.Row{ {int64(1), "first row, 2"}, {int64(2), "second row, 2"}, {int64(3), "third row, 2"}, }, }, { Query: "SELECT * FROM myhistorytable AS OF GREATEST('2019-01-02','2019-01-01','') foo ORDER BY i", Expected: []sql.Row{ {int64(1), "first row, 2"}, {int64(2), "second row, 2"}, {int64(3), "third row, 2"}, }, }, { Query: "SELECT * FROM myhistorytable ORDER BY i", Expected: []sql.Row{ {int64(1), "first row, 2"}, {int64(2), "second row, 2"}, {int64(3), "third row, 2"}, }, }, { Query: "SHOW TABLES AS OF '2019-01-02' LIKE 'myhistorytable'", Expected: []sql.Row{ {"myhistorytable"}, }, }, { Query: "SHOW TABLES FROM mydb AS OF '2019-01-02' LIKE 'myhistorytable'", Expected: []sql.Row{ {"myhistorytable"}, }, }, }
var VersionedViewTests = []QueryTest{ { Query: "SELECT * FROM myview1 ORDER BY i", Expected: []sql.Row{ sql.NewRow(int64(1), "first row, 2"), sql.NewRow(int64(2), "second row, 2"), sql.NewRow(int64(3), "third row, 2"), }, }, { Query: "SELECT t.* FROM myview1 AS t ORDER BY i", Expected: []sql.Row{ sql.NewRow(int64(1), "first row, 2"), sql.NewRow(int64(2), "second row, 2"), sql.NewRow(int64(3), "third row, 2"), }, }, { Query: "SELECT t.i FROM myview1 AS t ORDER BY i", Expected: []sql.Row{ sql.NewRow(int64(1)), sql.NewRow(int64(2)), sql.NewRow(int64(3)), }, }, { Query: "SELECT * FROM myview1 AS OF '2019-01-01' ORDER BY i", Expected: []sql.Row{ sql.NewRow(int64(1), "first row, 1"), sql.NewRow(int64(2), "second row, 1"), sql.NewRow(int64(3), "third row, 1"), }, }, { Query: "SELECT * FROM myview2", Expected: []sql.Row{ sql.NewRow(int64(1), "first row, 2"), }, }, { Query: "SELECT i FROM myview2", Expected: []sql.Row{ sql.NewRow(int64(1)), }, }, { Query: "SELECT myview2.i FROM myview2", Expected: []sql.Row{ sql.NewRow(int64(1)), }, }, { Query: "SELECT myview2.* FROM myview2", Expected: []sql.Row{ sql.NewRow(int64(1), "first row, 2"), }, }, { Query: "SELECT t.* FROM myview2 as t", Expected: []sql.Row{ sql.NewRow(int64(1), "first row, 2"), }, }, { Query: "SELECT t.i FROM myview2 as t", Expected: []sql.Row{ sql.NewRow(int64(1)), }, }, { Query: "SELECT * FROM myview2 AS OF '2019-01-01'", Expected: []sql.Row{ sql.NewRow(int64(1), "first row, 1"), }, }, { Query: "select * from information_schema.views where table_schema = 'mydb'", Expected: []sql.Row{ sql.NewRow("def", "mydb", "myview", "SELECT * FROM mytable", "NONE", "YES", "", "DEFINER", "utf8mb4", "utf8mb4_0900_ai_ci"), sql.NewRow("def", "mydb", "myview1", "SELECT * FROM myhistorytable", "NONE", "YES", "", "DEFINER", "utf8mb4", "utf8mb4_0900_ai_ci"), sql.NewRow("def", "mydb", "myview2", "SELECT * FROM myview1 WHERE i = 1", "NONE", "YES", "", "DEFINER", "utf8mb4", "utf8mb4_0900_ai_ci"), }, }, { Query: "select table_name from information_schema.tables where table_schema = 'mydb' and table_type = 'VIEW' order by 1", Expected: []sql.Row{ sql.NewRow("myview"), sql.NewRow("myview1"), sql.NewRow("myview2"), }, }, }
var ViewTests = []QueryTest{ { Query: "SELECT * FROM myview ORDER BY i", Expected: []sql.Row{ sql.NewRow(int64(1), "first row"), sql.NewRow(int64(2), "second row"), sql.NewRow(int64(3), "third row"), }, }, { Query: "SELECT myview.* FROM myview ORDER BY i", Expected: []sql.Row{ sql.NewRow(int64(1), "first row"), sql.NewRow(int64(2), "second row"), sql.NewRow(int64(3), "third row"), }, }, { Query: "SELECT i FROM myview ORDER BY i", Expected: []sql.Row{ sql.NewRow(int64(1)), sql.NewRow(int64(2)), sql.NewRow(int64(3)), }, }, { Query: "SELECT t.* FROM myview AS t ORDER BY i", Expected: []sql.Row{ sql.NewRow(int64(1), "first row"), sql.NewRow(int64(2), "second row"), sql.NewRow(int64(3), "third row"), }, }, { Query: "SELECT t.i FROM myview AS t ORDER BY i", Expected: []sql.Row{ sql.NewRow(int64(1)), sql.NewRow(int64(2)), sql.NewRow(int64(3)), }, }, { Query: "SELECT * FROM myview2", Expected: []sql.Row{ sql.NewRow(int64(1), "first row"), }, }, { Query: "SELECT i FROM myview2", Expected: []sql.Row{ sql.NewRow(int64(1)), }, }, { Query: "SELECT myview2.i FROM myview2", Expected: []sql.Row{ sql.NewRow(int64(1)), }, }, { Query: "SELECT myview2.* FROM myview2", Expected: []sql.Row{ sql.NewRow(int64(1), "first row"), }, }, { Query: "SELECT t.* FROM myview2 as t", Expected: []sql.Row{ sql.NewRow(int64(1), "first row"), }, }, { Query: "SELECT t.i FROM myview2 as t", Expected: []sql.Row{ sql.NewRow(int64(1)), }, }, { Query: "select * from information_schema.views where table_schema = 'mydb'", Expected: []sql.Row{ sql.NewRow("def", "mydb", "myview", "SELECT * FROM mytable", "NONE", "YES", "", "DEFINER", "utf8mb4", "utf8mb4_0900_ai_ci"), sql.NewRow("def", "mydb", "myview2", "SELECT * FROM myview WHERE i = 1", "NONE", "YES", "", "DEFINER", "utf8mb4", "utf8mb4_0900_ai_ci"), }, }, { Query: "select table_name from information_schema.tables where table_schema = 'mydb' and table_type = 'VIEW' order by 1", Expected: []sql.Row{ sql.NewRow("myview"), sql.NewRow("myview2"), }, }, }
Functions ¶
func AssertErr ¶
func AssertErr(t *testing.T, e *sqle.Engine, harness Harness, query string, expectedErrKind *errors.Kind, errStrs ...string)
AssertErr asserts that the given query returns an error during its execution, optionally specifying a type of error.
func AssertWarning ¶ added in v0.9.0
func CreateSubsetTestData ¶
createSubsetTestData creates test tables and data. Passing a non-nil slice for includedTables will restrict the table creation to just those tables named.
func CreateTestData ¶
createTestData uses the provided harness to create test tables and data for many of the other tests.
func DeleteRows ¶
func InsertRows ¶
func NewBaseSession ¶
Returns a new BaseSession compatible with these tests. Most tests will work with any session implementation, but for full compatibility use a session based on this one.
func NewContext ¶
func NewContextWithEngine ¶
func NewEngineWithDbs ¶
func NewEngineWithDbs(t *testing.T, harness Harness, databases []sql.Database, driver sql.IndexDriver) *sqle.Engine
NewEngineWithDbs returns a new engine with the databases provided. This is useful if you don't want to implement a full harness but want to run your own tests on DBs you create.
func RunQueryTests ¶
Runs the query tests given after setting up the engine. Useful for testing out a smaller subset of queries during debugging.
func TestAddColumn ¶
func TestChecksOnInsert ¶ added in v0.9.0
func TestClearWarnings ¶
func TestColumnAliases ¶
TestColumnAliases exercises the logic for naming and referring to column aliases, and unlike other tests in this file checks that the name of the columns in the result schema is correct.
func TestColumnDefaults ¶
func TestCreateCheckConstraints ¶ added in v0.9.0
func TestCreateDatabase ¶ added in v0.9.0
func TestCreateForeignKeys ¶
func TestCreateTable ¶
func TestDelete ¶
func TestDeleteErrors ¶
func TestDisallowedCheckConstraints ¶ added in v0.9.0
func TestDropCheckConstraints ¶ added in v0.9.0
func TestDropColumn ¶
func TestDropDatabase ¶ added in v0.9.0
func TestDropForeignKeys ¶
func TestDropTable ¶
func TestExplode ¶
func TestInfoSchema ¶
Runs tests of the information_schema database.
func TestInsertInto ¶
func TestInsertIntoErrors ¶
func TestJsonScripts ¶ added in v0.9.0
func TestLoadData ¶ added in v0.9.0
func TestLoadDataErrors ¶ added in v0.9.0
func TestLoadDataFailing ¶ added in v0.9.0
func TestModifyColumn ¶
func TestNaturalJoin ¶
func TestNaturalJoinDisjoint ¶
func TestNaturalJoinEqual ¶
func TestOrderByGroupBy ¶
func TestQueries ¶
Tests a variety of queries against databases and tables provided by the given harness.
func TestQuery ¶
func TestQuery(t *testing.T, harness Harness, e *sqle.Engine, q string, expected []sql.Row, expectedCols []*sql.Column, bindings map[string]sql.Expression)
TestQuery runs a query on the engine given and asserts that results are as expected.
func TestQueryErrors ¶
func TestQueryPlan ¶
func TestQueryPlan(t *testing.T, ctx *sql.Context, engine *sqle.Engine, query string, expectedPlan string)
TestQueryPlan analyzes the query given and asserts that its printed plan matches the expected one.
func TestQueryPlans ¶
Tests generating the correct query plans for various queries using databases and tables provided by the given harness.
func TestQueryWithContext ¶
func TestReadOnly ¶
func TestRenameColumn ¶
func TestRenameTable ¶
func TestReplaceInto ¶
func TestReplaceIntoErrors ¶
func TestScript ¶
func TestScript(t *testing.T, harness Harness, script ScriptTest) bool
TestScript runs the test script given, making any assertions given
func TestScriptWithEngine ¶
TestScriptWithEngine runs the test script given with the engine provided.
func TestScripts ¶
func TestSessionSelectLimit ¶
func TestShowTableStatus ¶ added in v0.9.0
Runs tests on SHOW TABLE STATUS queries.
func TestStoredProcedures ¶ added in v0.9.0
func TestTracing ¶
func TestTriggerErrors ¶
func TestTriggers ¶
func TestTruncate ¶
func TestUpdate ¶
func TestUpdateErrors ¶
func TestVariableErrors ¶
func TestVariables ¶
func TestVersionedQueries ¶
Tests a variety of queries against databases and tables provided by the given harness.
func TestVersionedViews ¶
func TestWarnings ¶
func WidenRows ¶
For a variety of reasons, the widths of various primitive types can vary when passed through different SQL queries (and different database implementations). We may eventually decide that this undefined behavior is a problem, but for now it's mostly just an issue when comparing results in tests. To get around this, we widen every type to its widest value in actual and expected results.
Types ¶
type ForeignKeyHarness ¶
type ForeignKeyHarness interface { Harness // SupportsForeignKeys returns whether this harness should accept CREATE FOREIGN KEY statements as part of test // setup. SupportsForeignKeys() bool }
ForeignKeyHarness is an extension to Harness that lets an integrator test their implementation with foreign keys. Integrator tables must implement sql.ForeignKeyAlterableTable and sql.ForeignKeyTable.
type GenericErrorQueryTest ¶
type GenericErrorQueryTest struct { Name string Query string Bindings map[string]sql.Expression }
GenericErrorQueryTest is a query test that is used to assert an error occurs for some query, without specifying what the error was.
type Harness ¶
type Harness interface { // Parallelism returns how many parallel go routines to use when constructing an engine for test. Parallelism() int // NewDatabase returns a new sql.Database to use for a test. NewDatabase(name string) sql.Database // NewTable takes a database previously created by NewDatabase and returns a table created with the given schema. NewTable(db sql.Database, name string, schema sql.Schema) (sql.Table, error) // NewContext allows a harness to specify any sessions or context variables necessary for the proper functioning of // their engine implementation. Every harnessed engine test uses the context created by this method, with some // additional information (e.g. current DB) set uniformly. To replicated the behavior of tests during setup, // harnesses should generally dispatch to enginetest.NewContext(harness), rather than calling this method themselves. NewContext() *sql.Context }
Harness provides a way for database integrators to validate their implementation against the standard set of queries used to develop and test the engine itself. See memory_engine_test.go for an example.
type IndexDriverHarness ¶
type IndexDriverHarness interface { Harness // IndexDriver returns an index driver for the databases given, which will have been created by calls to // NewDatabase(). IndexDriver(dbs []sql.Database) sql.IndexDriver }
IndexDriverHarness is an extension to Harness that lets an integrator test their implementation alongside an index driver they provide.
type IndexDriverInitalizer ¶
type IndexDriverInitalizer func([]sql.Database) sql.IndexDriver
type IndexHarness ¶
type IndexHarness interface { Harness // SupportsNativeIndexCreation returns whether this harness should accept CREATE INDEX statements as part of test // setup. SupportsNativeIndexCreation() bool }
IndexHarness is an extension to Harness that lets an integrator test their implementation with native (table-supplied) indexes. Integrator tables must implement sql.IndexAlterableTable.
type KeylessTableHarness ¶
type KeylessTableHarness interface { Harness // SupportsKeylessTables indicates integrator support for keyless tables. SupportsKeylessTables() bool }
KeylessTableHarness is an extension to Harness that lets an integrator test their implementation with keyless tables.
type MemoryHarness ¶
type MemoryHarness struct {
// contains filtered or unexported fields
}
func NewDefaultMemoryHarness ¶
func NewDefaultMemoryHarness() *MemoryHarness
func NewMemoryHarness ¶
func NewMemoryHarness(name string, parallelism int, numTablePartitions int, useNativeIndexes bool, indexDriverInitalizer IndexDriverInitalizer) *MemoryHarness
func (*MemoryHarness) IndexDriver ¶
func (m *MemoryHarness) IndexDriver(dbs []sql.Database) sql.IndexDriver
func (*MemoryHarness) NewContext ¶
func (m *MemoryHarness) NewContext() *sql.Context
func (*MemoryHarness) NewDatabase ¶
func (m *MemoryHarness) NewDatabase(name string) sql.Database
func (*MemoryHarness) NewTableAsOf ¶
func (m *MemoryHarness) NewTableAsOf(db sql.VersionedDatabase, name string, schema sql.Schema, asOf interface{}) sql.Table
func (*MemoryHarness) Parallelism ¶
func (m *MemoryHarness) Parallelism() int
func (*MemoryHarness) SnapshotTable ¶
func (m *MemoryHarness) SnapshotTable(db sql.VersionedDatabase, name string, asOf interface{}) error
func (*MemoryHarness) SupportsForeignKeys ¶
func (m *MemoryHarness) SupportsForeignKeys() bool
func (*MemoryHarness) SupportsKeylessTables ¶
func (m *MemoryHarness) SupportsKeylessTables() bool
func (*MemoryHarness) SupportsNativeIndexCreation ¶
func (m *MemoryHarness) SupportsNativeIndexCreation() bool
type QueryErrorTest ¶
type QueryErrorTest struct { Query string ExpectedErr *errors.Kind }
type QueryPlanTest ¶
type ScriptTest ¶
type ScriptTest struct { // Name of the script test Name string // The sql statements to execute as setup, in order. Results are not checked, but statements must not error. SetUpScript []string // The set of assertions to make after setup, in order Assertions []ScriptTestAssertion // For tests that make a single assertion, Query can be set for the single assertion Query string // For tests that make a single assertion, Expected can be set for the single assertion Expected []sql.Row // For tests that make a single assertion, ExpectedErr can be set for the expected error ExpectedErr *errors.Kind }
type ScriptTestAssertion ¶
type ScriptTestAssertion struct { Query string Expected []sql.Row ExpectedErr *errors.Kind // ExpectedErrStr should be set for tests that expect a specific error string this is not linked to a custom error. // In most cases, errors should be linked to a custom error, however there are exceptions where this is not possible, // such as the use of the SIGNAL statement. ExpectedErrStr string }
type SkippingHarness ¶
type SkippingHarness interface { // SkipQueryTest returns whether to skip a test of the provided query string. SkipQueryTest(query string) bool }
SkippingHarness provides a way for integrators to skip tests that are known to be broken. E.g., integrators that can't handle every possible SQL type.
type SkippingMemoryHarness ¶
type SkippingMemoryHarness struct {
MemoryHarness
}
func NewSkippingMemoryHarness ¶
func NewSkippingMemoryHarness() *SkippingMemoryHarness
func (SkippingMemoryHarness) SkipQueryTest ¶
func (s SkippingMemoryHarness) SkipQueryTest(query string) bool
type VersionedDBHarness ¶
type VersionedDBHarness interface { Harness // NewTableAsOf creates a new table with the given name and schema, optionally handling snapshotting with the asOf // identifier. NewTableAsOf must ignore tables that already exist in the database. Tables returned by this method do // not need to have any previously created data in them, but they can. This behavior is implementation specific, and // the harness works either way. NewTableAsOf(db sql.VersionedDatabase, name string, schema sql.Schema, asOf interface{}) sql.Table // SnapshotTable creates a snapshot of the table named with the given asOf label. Depending on the implementation, // NewTableAsOf might do all the necessary work to create such snapshots, so this could be a no-op. SnapshotTable(db sql.VersionedDatabase, name string, asOf interface{}) error }
VersionedDBHarness is an extension to Harness that lets an integrator test their implementation of versioned (AS OF) queries. Integrators must implement sql.VersionedDatabase. For each table version being created, there will be a call to NewTableAsOf, some number of Delete and Insert operations, and then a call to SnapshotTable.
type WriteQueryTest ¶
type WriteQueryTest struct { WriteQuery string ExpectedWriteResult []sql.Row SelectQuery string ExpectedSelect []sql.Row Bindings map[string]sql.Expression }
WriteQueryTest is a query test for INSERT, UPDATE, etc. statements. It has a query to run and a select query to validate the results.