github.com/dolthub/go-mysql-server@v0.18.0/enginetest/queries/charset_collation_wire.go (about)

     1  // Copyright 2022 Dolthub, Inc.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package queries
    16  
    17  import (
    18  	"github.com/dolthub/go-mysql-server/sql"
    19  	"github.com/dolthub/go-mysql-server/sql/types"
    20  )
    21  
    22  // CharsetCollationWireTest is used to test character sets.
    23  type CharsetCollationWireTest struct {
    24  	Name        string
    25  	SetUpScript []string
    26  	Queries     []CharsetCollationWireTestQuery
    27  }
    28  
    29  // CharsetCollationWireTestQuery is a query within a CharsetCollationWireTest.
    30  type CharsetCollationWireTestQuery struct {
    31  	Query    string
    32  	Expected []sql.Row
    33  	Error    bool
    34  	// ExpectedCollations is an optional field, and when populated the test framework will assert that
    35  	// the MySQL field metadata has these expected collation IDs.
    36  	ExpectedCollations []sql.CollationID
    37  }
    38  
    39  // CharsetCollationWireTests are used to ensure that character sets and collations have the correct behavior over the
    40  // wire. Return values should all have the table encoding, as it's returning the table's encoding type.
    41  var CharsetCollationWireTests = []CharsetCollationWireTest{
    42  	{
    43  		Name: "Uppercase and lowercase collations",
    44  		Queries: []CharsetCollationWireTestQuery{
    45  			{
    46  				Query:    "CREATE TABLE test1 (v1 VARCHAR(255) COLLATE utf16_unicode_ci, v2 VARCHAR(255) COLLATE UTF16_UNICODE_CI);",
    47  				Expected: []sql.Row{{types.NewOkResult(0)}},
    48  			},
    49  			{
    50  				Query:    "CREATE TABLE test2 (v1 VARCHAR(255) CHARACTER SET utf16, v2 VARCHAR(255) CHARACTER SET UTF16);",
    51  				Expected: []sql.Row{{types.NewOkResult(0)}},
    52  			},
    53  		},
    54  	},
    55  	{
    56  		Name: "Insert multiple character sets",
    57  		SetUpScript: []string{
    58  			"SET character_set_results = 'binary';",
    59  			"CREATE TABLE test (v1 VARCHAR(255) COLLATE utf16_unicode_ci);",
    60  		},
    61  		Queries: []CharsetCollationWireTestQuery{
    62  			{
    63  				Query:    "INSERT INTO test VALUES ('hey');",
    64  				Expected: []sql.Row{{types.NewOkResult(1)}},
    65  			},
    66  			{
    67  				Query:    "INSERT INTO test VALUES (_utf16'\x00h\x00i');",
    68  				Expected: []sql.Row{{types.NewOkResult(1)}},
    69  			},
    70  			{
    71  				Query:    "INSERT INTO test VALUES (_utf8mb4'\x68\x65\x6c\x6c\x6f');",
    72  				Expected: []sql.Row{{types.NewOkResult(1)}},
    73  			},
    74  			{
    75  				Query:    "SELECT * FROM test ORDER BY 1;",
    76  				Expected: []sql.Row{{"\x00h\x00e\x00l\x00l\x00o"}, {"\x00h\x00e\x00y"}, {"\x00h\x00i"}},
    77  			},
    78  		},
    79  	},
    80  	{
    81  		Name: "Sorting differences",
    82  		SetUpScript: []string{
    83  			"SET character_set_results = 'binary';",
    84  			"CREATE TABLE test1 (v1 VARCHAR(255) COLLATE utf8mb4_0900_bin);",
    85  			"CREATE TABLE test2 (v1 VARCHAR(255) COLLATE utf16_unicode_ci);",
    86  		},
    87  		Queries: []CharsetCollationWireTestQuery{
    88  			{
    89  				Query:    "INSERT INTO test1 VALUES ('HEY2'), ('hey1');",
    90  				Expected: []sql.Row{{types.NewOkResult(2)}},
    91  			},
    92  			{
    93  				Query:    "INSERT INTO test2 VALUES ('HEY2'), ('hey1');",
    94  				Expected: []sql.Row{{types.NewOkResult(2)}},
    95  			},
    96  			{
    97  				Query:    "SELECT * FROM test1 ORDER BY 1;",
    98  				Expected: []sql.Row{{"HEY2"}, {"hey1"}},
    99  			},
   100  			{
   101  				Query:    "SELECT * FROM test2 ORDER BY 1;",
   102  				Expected: []sql.Row{{"\x00h\x00e\x00y\x001"}, {"\x00H\x00E\x00Y\x002"}},
   103  			},
   104  		},
   105  	},
   106  	{
   107  		Name: "Order by behaves differently according to case-sensitivity",
   108  		SetUpScript: []string{
   109  			"SET character_set_results = 'binary';",
   110  			"CREATE TABLE test1 (pk BIGINT PRIMARY KEY, v1 VARCHAR(255) COLLATE utf16_unicode_ci, INDEX(v1));",
   111  			"CREATE TABLE test2 (pk BIGINT PRIMARY KEY, v1 VARCHAR(255) COLLATE utf8mb4_0900_bin, INDEX(v1));",
   112  			"INSERT INTO test1 VALUES (1, 'abc'), (2, 'ABC'), (3, 'aBc'), (4, 'AbC');",
   113  			"INSERT INTO test2 VALUES (1, 'abc'), (2, 'ABC'), (3, 'aBc'), (4, 'AbC');",
   114  		},
   115  		Queries: []CharsetCollationWireTestQuery{
   116  			{
   117  				Query: "SELECT v1, pk FROM test1 ORDER BY pk;",
   118  				Expected: []sql.Row{
   119  					{"\x00a\x00b\x00c", "1"}, {"\x00A\x00B\x00C", "2"}, {"\x00a\x00B\x00c", "3"}, {"\x00A\x00b\x00C", "4"},
   120  				},
   121  			},
   122  			{
   123  				Query: "SELECT v1, pk FROM test1 ORDER BY v1, pk;",
   124  				Expected: []sql.Row{
   125  					{"\x00a\x00b\x00c", "1"}, {"\x00A\x00B\x00C", "2"}, {"\x00a\x00B\x00c", "3"}, {"\x00A\x00b\x00C", "4"},
   126  				},
   127  			},
   128  			{
   129  				Query: "SELECT v1, pk FROM test2 ORDER BY pk;",
   130  				Expected: []sql.Row{
   131  					{"abc", "1"}, {"ABC", "2"}, {"aBc", "3"}, {"AbC", "4"},
   132  				},
   133  			},
   134  			{
   135  				Query: "SELECT v1, pk FROM test2 ORDER BY v1, pk;",
   136  				Expected: []sql.Row{
   137  					{"ABC", "2"}, {"AbC", "4"}, {"aBc", "3"}, {"abc", "1"},
   138  				},
   139  			},
   140  		},
   141  	},
   142  	{
   143  		Name: "Proper index access",
   144  		SetUpScript: []string{
   145  			"SET character_set_results = 'binary';",
   146  			"CREATE TABLE test1 (pk BIGINT PRIMARY KEY, v1 VARCHAR(255) COLLATE utf16_unicode_ci, INDEX(v1));",
   147  			"CREATE TABLE test2 (pk BIGINT PRIMARY KEY, v1 VARCHAR(255) COLLATE utf8mb4_0900_bin, INDEX(v1));",
   148  			"INSERT INTO test1 VALUES (1, 'abc'), (2, 'ABC'), (3, 'aBc'), (4, 'AbC');",
   149  			"INSERT INTO test2 VALUES (1, 'abc'), (2, 'ABC'), (3, 'aBc'), (4, 'AbC');",
   150  		},
   151  		Queries: []CharsetCollationWireTestQuery{
   152  			{
   153  				Query:    "SELECT v1, pk FROM test1 WHERE v1 > 'AbC' ORDER BY v1, pk;",
   154  				Expected: []sql.Row(nil),
   155  			},
   156  			{
   157  				Query: "SELECT v1, pk FROM test1 WHERE v1 >= 'AbC' ORDER BY v1, pk;",
   158  				Expected: []sql.Row{
   159  					{"\x00a\x00b\x00c", "1"}, {"\x00A\x00B\x00C", "2"}, {"\x00a\x00B\x00c", "3"}, {"\x00A\x00b\x00C", "4"},
   160  				},
   161  			},
   162  			{
   163  				Query: "SELECT v1, pk FROM test1 WHERE v1 <= 'aBc' ORDER BY v1, pk;",
   164  				Expected: []sql.Row{
   165  					{"\x00a\x00b\x00c", "1"}, {"\x00A\x00B\x00C", "2"}, {"\x00a\x00B\x00c", "3"}, {"\x00A\x00b\x00C", "4"},
   166  				},
   167  			},
   168  			{
   169  				Query: "SELECT v1, pk FROM test1 WHERE v1 = 'ABC' ORDER BY v1, pk;",
   170  				Expected: []sql.Row{
   171  					{"\x00a\x00b\x00c", "1"}, {"\x00A\x00B\x00C", "2"}, {"\x00a\x00B\x00c", "3"}, {"\x00A\x00b\x00C", "4"},
   172  				},
   173  			},
   174  			{
   175  				Query: "SELECT v1, pk FROM test1 WHERE v1 BETWEEN 'ABC' AND 'AbC' ORDER BY v1, pk;",
   176  				Expected: []sql.Row{
   177  					{"\x00a\x00b\x00c", "1"}, {"\x00A\x00B\x00C", "2"}, {"\x00a\x00B\x00c", "3"}, {"\x00A\x00b\x00C", "4"},
   178  				},
   179  			},
   180  			{
   181  				Query: "SELECT v1, pk FROM test1 WHERE v1 IN ('abc') ORDER BY v1, pk;",
   182  				Expected: []sql.Row{
   183  					{"\x00a\x00b\x00c", "1"}, {"\x00A\x00B\x00C", "2"}, {"\x00a\x00B\x00c", "3"}, {"\x00A\x00b\x00C", "4"},
   184  				},
   185  			},
   186  			{
   187  				Query: "SELECT v1, pk FROM test2 WHERE v1 > 'AbC' ORDER BY v1, pk;",
   188  				Expected: []sql.Row{
   189  					{"aBc", "3"}, {"abc", "1"},
   190  				},
   191  			},
   192  			{
   193  				Query: "SELECT v1, pk FROM test2 WHERE v1 >= 'AbC' ORDER BY v1, pk;",
   194  				Expected: []sql.Row{
   195  					{"AbC", "4"}, {"aBc", "3"}, {"abc", "1"},
   196  				},
   197  			},
   198  			{
   199  				Query: "SELECT v1, pk FROM test2 WHERE v1 <= 'aBc' ORDER BY v1, pk;",
   200  				Expected: []sql.Row{
   201  					{"ABC", "2"}, {"AbC", "4"}, {"aBc", "3"},
   202  				},
   203  			},
   204  			{
   205  				Query: "SELECT v1, pk FROM test2 WHERE v1 = 'ABC' ORDER BY v1, pk;",
   206  				Expected: []sql.Row{
   207  					{"ABC", "2"},
   208  				},
   209  			},
   210  			{
   211  				Query: "SELECT v1, pk FROM test2 WHERE v1 BETWEEN 'ABC' AND 'AbC' ORDER BY v1, pk;",
   212  				Expected: []sql.Row{
   213  					{"ABC", "2"}, {"AbC", "4"},
   214  				},
   215  			},
   216  			{
   217  				Query: "SELECT v1, pk FROM test2 WHERE v1 IN ('abc') ORDER BY v1, pk;",
   218  				Expected: []sql.Row{
   219  					{"abc", "1"},
   220  				},
   221  			},
   222  		},
   223  	},
   224  	{
   225  		Name: "SET NAMES does not interfere with column charset",
   226  		SetUpScript: []string{
   227  			"SET NAMES utf8mb3;",
   228  			"CREATE TABLE test(pk BIGINT PRIMARY KEY, v1 VARCHAR(100) COLLATE utf8mb4_0900_bin);",
   229  			"INSERT INTO test VALUES (1, 'a'), (2, 'b');",
   230  		},
   231  		Queries: []CharsetCollationWireTestQuery{
   232  			{
   233  				Query:    "SELECT * FROM test ORDER BY v1 COLLATE utf8mb4_bin ASC;",
   234  				Expected: []sql.Row{{"1", "a"}, {"2", "b"}},
   235  			},
   236  			{
   237  				Query: "SELECT * FROM test ORDER BY v1 COLLATE utf8mb3_bin ASC;",
   238  				Error: true,
   239  			},
   240  			{
   241  				Query:    "SELECT 'a' COLLATE utf8mb3_bin;",
   242  				Expected: []sql.Row{{"a"}},
   243  			},
   244  			{
   245  				Query: "SELECT 'a' COLLATE utf8mb4_bin;",
   246  				Error: true,
   247  			},
   248  		},
   249  	},
   250  	{
   251  		Name: "SET validates character set and collation variables",
   252  		Queries: []CharsetCollationWireTestQuery{
   253  			{
   254  				Query: "SET character_set_client = 'does_not_exist';",
   255  				Error: true,
   256  			},
   257  			{
   258  				Query: "SET character_set_connection = 'invalid_charset';",
   259  				Error: true,
   260  			},
   261  			{
   262  				Query: "SET character_set_results = 'whoops';",
   263  				Error: true,
   264  			},
   265  			{
   266  				Query: "SET collation_connection = 'cant_be';",
   267  				Error: true,
   268  			},
   269  			{
   270  				Query: "SET collation_database = 'something_else';",
   271  				Error: true,
   272  			},
   273  			{
   274  				Query: "SET collation_server = 'why_try';",
   275  				Error: true,
   276  			},
   277  			{
   278  				Query: "SET NAMES outside_correct;",
   279  				Error: true,
   280  			},
   281  		},
   282  	},
   283  	{
   284  		Name: "Coercibility test using HEX",
   285  		SetUpScript: []string{
   286  			"SET NAMES utf8mb4;",
   287  		},
   288  		Queries: []CharsetCollationWireTestQuery{
   289  			{
   290  				Query:    "SELECT HEX(UNHEX('c0a80000')) = 'c0a80000'",
   291  				Expected: []sql.Row{{"1"}},
   292  			},
   293  			{
   294  				Query:    "SET collation_connection = 'utf8mb4_0900_bin';",
   295  				Expected: []sql.Row{{types.NewOkResult(0)}},
   296  			},
   297  			{
   298  				Query:    "SELECT HEX(UNHEX('c0a80000')) = 'c0a80000'",
   299  				Expected: []sql.Row{{"0"}},
   300  			},
   301  		},
   302  	},
   303  	{
   304  		Name: "ENUM collation handling",
   305  		SetUpScript: []string{
   306  			"SET character_set_results = 'binary';",
   307  			"CREATE TABLE test1 (pk BIGINT PRIMARY KEY, v1 ENUM('abc','def','ghi') COLLATE utf16_unicode_ci);",
   308  			"CREATE TABLE test2 (pk BIGINT PRIMARY KEY, v1 ENUM('abc','def','ghi') COLLATE utf8mb4_0900_bin);",
   309  		},
   310  		Queries: []CharsetCollationWireTestQuery{
   311  			{
   312  				Query: "INSERT INTO test1 VALUES (1, 'ABC');",
   313  				Expected: []sql.Row{
   314  					{types.NewOkResult(1)},
   315  				},
   316  			},
   317  			{
   318  				Query: "INSERT INTO test2 VALUES (1, 'ABC');",
   319  				Error: true,
   320  			},
   321  			{
   322  				Query: "INSERT INTO test1 VALUES (2, _utf16'\x00d\x00e\x00f' COLLATE utf16_unicode_ci);",
   323  				Expected: []sql.Row{
   324  					{types.NewOkResult(1)},
   325  				},
   326  			},
   327  			{
   328  				Query: "INSERT INTO test2 VALUES (2, _utf16'\x00d\x00e\x00f' COLLATE utf16_unicode_ci);",
   329  				Expected: []sql.Row{
   330  					{types.NewOkResult(1)},
   331  				},
   332  			},
   333  			{
   334  				Query: "SELECT * FROM test1 ORDER BY pk;",
   335  				Expected: []sql.Row{
   336  					{"1", "\x00a\x00b\x00c"}, {"2", "\x00d\x00e\x00f"},
   337  				},
   338  			},
   339  			{
   340  				Query: "SELECT * FROM test2 ORDER BY pk;",
   341  				Expected: []sql.Row{
   342  					{"2", "def"},
   343  				},
   344  			},
   345  		},
   346  	},
   347  	{
   348  		Name: "SET collation handling",
   349  		SetUpScript: []string{
   350  			"SET character_set_results = 'binary';",
   351  			"CREATE TABLE test1 (pk BIGINT PRIMARY KEY, v1 SET('a','b','c') COLLATE utf16_unicode_ci);",
   352  			"CREATE TABLE test2 (pk BIGINT PRIMARY KEY, v1 SET('a','b','c') COLLATE utf8mb4_0900_bin);",
   353  		},
   354  		Queries: []CharsetCollationWireTestQuery{
   355  			{
   356  				Query: "INSERT INTO test1 VALUES (1, 'A');",
   357  				Expected: []sql.Row{
   358  					{types.NewOkResult(1)},
   359  				},
   360  			},
   361  			{
   362  				Query: "INSERT INTO test2 VALUES (1, 'A');",
   363  				Error: true,
   364  			},
   365  			{
   366  				Query: "INSERT INTO test1 VALUES (2, _utf16'\x00b\x00,\x00c' COLLATE utf16_unicode_ci);",
   367  				Expected: []sql.Row{
   368  					{types.NewOkResult(1)},
   369  				},
   370  			},
   371  			{
   372  				Query: "INSERT INTO test2 VALUES (2, _utf16'\x00b\x00,\x00c' COLLATE utf16_unicode_ci);",
   373  				Expected: []sql.Row{
   374  					{types.NewOkResult(1)},
   375  				},
   376  			},
   377  			{
   378  				Query: "SELECT * FROM test1 ORDER BY pk;",
   379  				Expected: []sql.Row{
   380  					{"1", "\x00a"}, {"2", "\x00b\x00,\x00c"},
   381  				},
   382  			},
   383  			{
   384  				Query: "SELECT * FROM test2 ORDER BY pk;",
   385  				Expected: []sql.Row{
   386  					{"2", "b,c"},
   387  				},
   388  			},
   389  		},
   390  	},
   391  	{
   392  		Name: "Correct behavior with `character_set_results`",
   393  		SetUpScript: []string{
   394  			"SET character_set_results = 'binary';",
   395  			"CREATE TABLE test (v1 VARCHAR(255) COLLATE utf16_unicode_ci);",
   396  			"INSERT INTO test VALUES (_utf8mb4'hey');",
   397  		},
   398  		Queries: []CharsetCollationWireTestQuery{
   399  			{
   400  				Query:              "SELECT * FROM test;",
   401  				Expected:           []sql.Row{{"\x00h\x00e\x00y"}},
   402  				ExpectedCollations: []sql.CollationID{sql.Collation_binary},
   403  			},
   404  			{
   405  				Query:    "SET character_set_results = 'utf8mb4';",
   406  				Expected: []sql.Row{{types.NewOkResult(0)}},
   407  			},
   408  			{
   409  				Query:              "SELECT * FROM test;",
   410  				Expected:           []sql.Row{{"hey"}},
   411  				ExpectedCollations: []sql.CollationID{sql.Collation_utf8mb4_0900_ai_ci},
   412  			},
   413  			{
   414  				Query:    "SET character_set_results = 'utf32';",
   415  				Expected: []sql.Row{{types.NewOkResult(0)}},
   416  			},
   417  			{
   418  				Query:              "SELECT * FROM test;",
   419  				Expected:           []sql.Row{{"\x00\x00\x00h\x00\x00\x00e\x00\x00\x00y"}},
   420  				ExpectedCollations: []sql.CollationID{sql.Collation_utf32_general_ci},
   421  			},
   422  			{
   423  				Query:    "SET character_set_results = NULL;",
   424  				Expected: []sql.Row{{types.NewOkResult(0)}},
   425  			},
   426  			{
   427  				Query:              "SELECT * FROM test;",
   428  				Expected:           []sql.Row{{"\x00h\x00e\x00y"}},
   429  				ExpectedCollations: []sql.CollationID{sql.Collation_utf16_general_ci},
   430  			},
   431  		},
   432  	},
   433  	{
   434  		Name: "LIKE respects table collations",
   435  		SetUpScript: []string{
   436  			"SET NAMES utf8mb4;",
   437  			"CREATE TABLE test(v1 VARCHAR(100) COLLATE utf8mb4_0900_bin, v2 VARCHAR(100) COLLATE utf8mb4_0900_ai_ci);",
   438  			"INSERT INTO test VALUES ('abc', 'abc'), ('ABC', 'ABC');",
   439  		},
   440  		Queries: []CharsetCollationWireTestQuery{
   441  			{
   442  				Query: "SELECT COUNT(*) FROM test WHERE v1 LIKE 'ABC';",
   443  				Expected: []sql.Row{
   444  					{"1"},
   445  				},
   446  			},
   447  			{
   448  				Query: "SELECT COUNT(*) FROM test WHERE v2 LIKE 'ABC';",
   449  				Expected: []sql.Row{
   450  					{"2"},
   451  				},
   452  			},
   453  			{
   454  				Query: "SELECT COUNT(*) FROM test WHERE v1 LIKE 'A%';",
   455  				Expected: []sql.Row{
   456  					{"1"},
   457  				},
   458  			},
   459  			{
   460  				Query: "SELECT COUNT(*) FROM test WHERE v2 LIKE 'A%';",
   461  				Expected: []sql.Row{
   462  					{"2"},
   463  				},
   464  			},
   465  			{
   466  				Query: "SELECT COUNT(*) FROM test WHERE v1 LIKE '%C';",
   467  				Expected: []sql.Row{
   468  					{"1"},
   469  				},
   470  			},
   471  			{
   472  				Query: "SELECT COUNT(*) FROM test WHERE v2 LIKE '%C';",
   473  				Expected: []sql.Row{
   474  					{"2"},
   475  				},
   476  			},
   477  			{
   478  				Query:    "SET collation_connection = 'utf8mb4_0900_bin';",
   479  				Expected: []sql.Row{{}},
   480  			},
   481  			{
   482  				Query: "SELECT COUNT(*) FROM test WHERE v1 LIKE 'ABC';",
   483  				Expected: []sql.Row{
   484  					{"1"},
   485  				},
   486  			},
   487  			{
   488  				Query: "SELECT COUNT(*) FROM test WHERE v2 LIKE 'ABC';",
   489  				Expected: []sql.Row{
   490  					{"2"},
   491  				},
   492  			},
   493  			{
   494  				Query: "SELECT COUNT(*) FROM test WHERE v1 LIKE 'ABC' COLLATE utf8mb4_0900_ai_ci;",
   495  				Expected: []sql.Row{
   496  					{"2"},
   497  				},
   498  			},
   499  		},
   500  	},
   501  	{
   502  		Name: "LIKE respects connection collation",
   503  		SetUpScript: []string{
   504  			"SET NAMES utf8mb4;",
   505  		},
   506  		Queries: []CharsetCollationWireTestQuery{
   507  			{
   508  				Query: "SELECT 'abc' LIKE 'ABC';",
   509  				Expected: []sql.Row{
   510  					{"1"},
   511  				},
   512  			},
   513  			{
   514  				Query: "SELECT 'abc' COLLATE utf8mb4_0900_bin LIKE 'ABC';",
   515  				Expected: []sql.Row{
   516  					{"0"},
   517  				},
   518  			},
   519  			{
   520  				Query: "SELECT 'abc' LIKE 'ABC' COLLATE utf8mb4_0900_bin;",
   521  				Expected: []sql.Row{
   522  					{"0"},
   523  				},
   524  			},
   525  			{
   526  				Query: "SELECT 'abc' COLLATE utf8mb4_0900_ai_ci LIKE 'ABC';",
   527  				Expected: []sql.Row{
   528  					{"1"},
   529  				},
   530  			},
   531  			{
   532  				Query: "SELECT 'abc' LIKE 'ABC' COLLATE utf8mb4_0900_ai_ci;",
   533  				Expected: []sql.Row{
   534  					{"1"},
   535  				},
   536  			},
   537  			{
   538  				Query:    "SET collation_connection = 'utf8mb4_0900_bin';",
   539  				Expected: []sql.Row{{}},
   540  			},
   541  			{
   542  				Query: "SELECT 'abc' LIKE 'ABC';",
   543  				Expected: []sql.Row{
   544  					{"0"},
   545  				},
   546  			},
   547  			{
   548  				Query: "SELECT 'abc' COLLATE utf8mb4_0900_ai_ci LIKE 'ABC';",
   549  				Expected: []sql.Row{
   550  					{"1"},
   551  				},
   552  			},
   553  			{
   554  				Query: "SELECT 'abc' LIKE 'ABC' COLLATE utf8mb4_0900_ai_ci;",
   555  				Expected: []sql.Row{
   556  					{"1"},
   557  				},
   558  			},
   559  			{
   560  				Query: "SELECT 'abc' COLLATE utf8mb4_0900_bin LIKE 'ABC';",
   561  				Expected: []sql.Row{
   562  					{"0"},
   563  				},
   564  			},
   565  			{
   566  				Query: "SELECT 'abc' LIKE 'ABC' COLLATE utf8mb4_0900_bin;",
   567  				Expected: []sql.Row{
   568  					{"0"},
   569  				},
   570  			},
   571  			{
   572  				Query: "SELECT _utf8mb4'abc' LIKE 'ABC';",
   573  				Expected: []sql.Row{
   574  					{"0"},
   575  				},
   576  			},
   577  			{
   578  				Query: "SELECT 'abc' LIKE _utf8mb4'ABC';",
   579  				Expected: []sql.Row{
   580  					{"0"},
   581  				},
   582  			},
   583  		},
   584  	},
   585  	{
   586  		Name: "STRCMP() function",
   587  		Queries: []CharsetCollationWireTestQuery{
   588  			// TODO: returning different results from MySQL
   589  			/*{
   590  				// collation with the lowest coercibility is used
   591  				Query: "SELECT STRCMP(_utf8mb4'A' COLLATE utf8mb4_0900_ai_ci, 'a')",
   592  				Expected: []sql.Row{
   593  					{"0"},
   594  				},
   595  			},
   596  			{
   597  				// same coercibility, both unicode
   598  				Query:   "SELECT STRCMP(_utf8mb4'A' COLLATE utf8mb4_0900_ai_ci, _utf8mb4'a' COLLATE utf8mb4_0900_as_cs)",
   599  				Error: true,
   600  			},
   601  			{
   602  				// same coercibility, both not unicode
   603  				Query: "SELECT STRCMP(_latin1'A' COLLATE latin1_general_ci, _latin1'a' COLLATE latin1_german1_ci)",
   604  				Error: true,
   605  			},*/
   606  			{
   607  				// same coercibility, one unicode and one not unicode
   608  				Query: "SELECT STRCMP(_utf8mb4'A' COLLATE utf8mb4_0900_ai_ci, _latin1'b' COLLATE latin1_general_cs)",
   609  				Expected: []sql.Row{
   610  					{"-1"},
   611  				},
   612  			},
   613  		},
   614  	},
   615  	{
   616  		Name: "LENGTH() function",
   617  		Queries: []CharsetCollationWireTestQuery{
   618  			{
   619  				Query: "SELECT LENGTH(_utf16'\x00a\x00b\x00c' COLLATE utf16_unicode_ci);",
   620  				Expected: []sql.Row{
   621  					{"6"},
   622  				},
   623  			},
   624  			{
   625  				Query: "SELECT LENGTH(_utf8mb4'abc' COLLATE utf8mb4_0900_bin);",
   626  				Expected: []sql.Row{
   627  					{"3"},
   628  				},
   629  			},
   630  			{
   631  				Query: "SELECT LENGTH(_utf8mb4'\x00a\x00b\x00c' COLLATE utf8mb4_0900_bin);",
   632  				Expected: []sql.Row{
   633  					{"6"},
   634  				},
   635  			},
   636  		},
   637  	},
   638  	{
   639  		Name: "CHAR_LENGTH() function",
   640  		Queries: []CharsetCollationWireTestQuery{
   641  			{
   642  				Query: "SELECT CHAR_LENGTH(_utf16'\x00a\x00b\x00c' COLLATE utf16_unicode_ci);",
   643  				Expected: []sql.Row{
   644  					{"3"},
   645  				},
   646  			},
   647  			{
   648  				Query: "SELECT CHAR_LENGTH(_utf8mb4'abc' COLLATE utf8mb4_0900_bin);",
   649  				Expected: []sql.Row{
   650  					{"3"},
   651  				},
   652  			},
   653  			{
   654  				Query: "SELECT CHAR_LENGTH(_utf8mb4'\x00a\x00b\x00c' COLLATE utf8mb4_0900_bin);",
   655  				Expected: []sql.Row{
   656  					{"6"},
   657  				},
   658  			},
   659  		},
   660  	},
   661  	{
   662  		Name: "UPPER() function",
   663  		SetUpScript: []string{
   664  			"SET character_set_results = 'binary';",
   665  		},
   666  		Queries: []CharsetCollationWireTestQuery{
   667  			{
   668  				Query: "SELECT UPPER(_utf16'\x00a\x00B\x00c' COLLATE utf16_unicode_ci);",
   669  				Expected: []sql.Row{
   670  					{"\x00A\x00B\x00C"},
   671  				},
   672  			},
   673  			{
   674  				Query: "SELECT UPPER(_utf8mb4'aBc' COLLATE utf8mb4_0900_bin);",
   675  				Expected: []sql.Row{
   676  					{"ABC"},
   677  				},
   678  			},
   679  			{
   680  				Query: "SELECT UPPER(_utf8mb4'\x00a\x00B\x00c' COLLATE utf8mb4_0900_bin);",
   681  				Expected: []sql.Row{
   682  					{"\x00A\x00B\x00C"},
   683  				},
   684  			},
   685  		},
   686  	},
   687  	{
   688  		Name: "LOWER() function",
   689  		SetUpScript: []string{
   690  			"SET character_set_results = 'binary';",
   691  		},
   692  		Queries: []CharsetCollationWireTestQuery{
   693  			{
   694  				Query: "SELECT LOWER(_utf16'\x00A\x00b\x00C' COLLATE utf16_unicode_ci);",
   695  				Expected: []sql.Row{
   696  					{"\x00a\x00b\x00c"},
   697  				},
   698  			},
   699  			{
   700  				Query: "SELECT LOWER(_utf8mb4'AbC' COLLATE utf8mb4_0900_bin);",
   701  				Expected: []sql.Row{
   702  					{"abc"},
   703  				},
   704  			},
   705  			{
   706  				Query: "SELECT LOWER(_utf8mb4'\x00A\x00b\x00C' COLLATE utf8mb4_0900_bin);",
   707  				Expected: []sql.Row{
   708  					{"\x00a\x00b\x00c"},
   709  				},
   710  			},
   711  		},
   712  	},
   713  	{
   714  		Name: "RPAD() function",
   715  		Queries: []CharsetCollationWireTestQuery{
   716  			{
   717  				Query: "SELECT RPAD(_utf16'\x00a\x00b\x00c' COLLATE utf16_unicode_ci, 6, 'z');",
   718  				Expected: []sql.Row{
   719  					{"abczzz"},
   720  				},
   721  			},
   722  			{
   723  				Query: "SELECT RPAD(_utf16'\x00a\x00b\x00c' COLLATE utf16_unicode_ci, 6, _utf8mb4'z' COLLATE utf8mb4_0900_bin);",
   724  				Expected: []sql.Row{
   725  					{"abczzz"},
   726  				},
   727  			},
   728  			{
   729  				Query: "SELECT RPAD(_utf16'\x00a\x00b\x00c' COLLATE utf16_unicode_ci, 6, _utf16'\x00z' COLLATE utf16_unicode_ci);",
   730  				Expected: []sql.Row{
   731  					{"abczzz"},
   732  				},
   733  			},
   734  			{
   735  				Query: "SELECT RPAD(_utf8mb4'abc' COLLATE utf8mb4_0900_bin, 6, _utf8mb4'z' COLLATE utf8mb4_0900_bin);",
   736  				Expected: []sql.Row{
   737  					{"abczzz"},
   738  				},
   739  			},
   740  			{
   741  				Query: "SELECT RPAD(_utf8mb4'abc' COLLATE utf8mb4_0900_bin, 6, _utf16'\x00z' COLLATE utf16_unicode_ci);",
   742  				Expected: []sql.Row{
   743  					{"abczzz"},
   744  				},
   745  			},
   746  			{
   747  				Query: "SELECT RPAD(_utf8mb4'\x00a\x00b\x00c' COLLATE utf8mb4_0900_bin, 6, _utf16'\x00z' COLLATE utf16_unicode_ci);",
   748  				Expected: []sql.Row{
   749  					{"\x00a\x00b\x00c"},
   750  				},
   751  			},
   752  			{
   753  				Query: "SELECT RPAD(_utf8mb4'\x00a\x00b\x00c' COLLATE utf8mb4_0900_bin, 9, _utf16'\x00z' COLLATE utf16_unicode_ci);",
   754  				Expected: []sql.Row{
   755  					{"\x00a\x00b\x00czzz"},
   756  				},
   757  			},
   758  		},
   759  	},
   760  	{
   761  		Name: "LPAD() function",
   762  		Queries: []CharsetCollationWireTestQuery{
   763  			{
   764  				Query: "SELECT LPAD(_utf16'\x00a\x00b\x00c' COLLATE utf16_unicode_ci, 6, 'z');",
   765  				Expected: []sql.Row{
   766  					{"zzzabc"},
   767  				},
   768  			},
   769  			{
   770  				Query: "SELECT LPAD(_utf16'\x00a\x00b\x00c' COLLATE utf16_unicode_ci, 6, _utf8mb4'z' COLLATE utf8mb4_0900_bin);",
   771  				Expected: []sql.Row{
   772  					{"zzzabc"},
   773  				},
   774  			},
   775  			{
   776  				Query: "SELECT LPAD(_utf16'\x00a\x00b\x00c' COLLATE utf16_unicode_ci, 6, _utf16'\x00z' COLLATE utf16_unicode_ci);",
   777  				Expected: []sql.Row{
   778  					{"zzzabc"},
   779  				},
   780  			},
   781  			{
   782  				Query: "SELECT LPAD(_utf8mb4'abc' COLLATE utf8mb4_0900_bin, 6, _utf8mb4'z' COLLATE utf8mb4_0900_bin);",
   783  				Expected: []sql.Row{
   784  					{"zzzabc"},
   785  				},
   786  			},
   787  			{
   788  				Query: "SELECT LPAD(_utf8mb4'abc' COLLATE utf8mb4_0900_bin, 6, _utf16'\x00z' COLLATE utf16_unicode_ci);",
   789  				Expected: []sql.Row{
   790  					{"zzzabc"},
   791  				},
   792  			},
   793  			{
   794  				Query: "SELECT LPAD(_utf8mb4'\x00a\x00b\x00c' COLLATE utf8mb4_0900_bin, 6, _utf16'\x00z' COLLATE utf16_unicode_ci);",
   795  				Expected: []sql.Row{
   796  					{"\x00a\x00b\x00c"},
   797  				},
   798  			},
   799  			{
   800  				Query: "SELECT LPAD(_utf8mb4'\x00a\x00b\x00c' COLLATE utf8mb4_0900_bin, 9, _utf16'\x00z' COLLATE utf16_unicode_ci);",
   801  				Expected: []sql.Row{
   802  					{"zzz\x00a\x00b\x00c"},
   803  				},
   804  			},
   805  		},
   806  	},
   807  	{
   808  		Name: "HEX() function",
   809  		Queries: []CharsetCollationWireTestQuery{
   810  			{
   811  				Query: "SELECT HEX(_utf16'\x00a\x00b\x00c' COLLATE utf16_unicode_ci);",
   812  				Expected: []sql.Row{
   813  					{"006100620063"},
   814  				},
   815  			},
   816  			{
   817  				Query: "SELECT HEX(_utf8mb4'abc' COLLATE utf8mb4_0900_bin);",
   818  				Expected: []sql.Row{
   819  					{"616263"},
   820  				},
   821  			},
   822  			{
   823  				Query: "SELECT HEX(_utf8mb4'\x00a\x00b\x00c' COLLATE utf8mb4_0900_bin);",
   824  				Expected: []sql.Row{
   825  					{"006100620063"},
   826  				},
   827  			},
   828  		},
   829  	},
   830  	{
   831  		Name: "UNHEX() function",
   832  		Queries: []CharsetCollationWireTestQuery{
   833  			{
   834  				Query: "SELECT UNHEX(_utf16'\x006\x001\x006\x002\x006\x003' COLLATE utf16_unicode_ci);",
   835  				Expected: []sql.Row{
   836  					{"abc"},
   837  				},
   838  			},
   839  			{
   840  				Query: "SELECT UNHEX(_utf8mb4'616263' COLLATE utf8mb4_0900_bin);",
   841  				Expected: []sql.Row{
   842  					{"abc"},
   843  				},
   844  			},
   845  		},
   846  	},
   847  	{
   848  		Name: "SUBSTRING() function",
   849  		SetUpScript: []string{
   850  			"SET character_set_results = 'binary';",
   851  		},
   852  		Queries: []CharsetCollationWireTestQuery{
   853  			{
   854  				Query: "SELECT SUBSTRING(_utf16'\x00a\x00b\x00c\x00d' COLLATE utf16_unicode_ci, 2, 2);",
   855  				Expected: []sql.Row{
   856  					{"\x00b\x00c"},
   857  				},
   858  			},
   859  			{
   860  				Query: "SELECT SUBSTRING(_utf8mb4'abcd' COLLATE utf8mb4_0900_bin, 2, 2);",
   861  				Expected: []sql.Row{
   862  					{"bc"},
   863  				},
   864  			},
   865  			{
   866  				Query: "SELECT SUBSTRING(_utf8mb4'\x00a\x00b\x00c\x00d' COLLATE utf8mb4_0900_bin, 2, 2);",
   867  				Expected: []sql.Row{
   868  					{"a\x00"},
   869  				},
   870  			},
   871  		},
   872  	},
   873  	{
   874  		Name: "TO_BASE64() function",
   875  		Queries: []CharsetCollationWireTestQuery{
   876  			{
   877  				Query: "SELECT TO_BASE64(_utf16'\x00a\x00b\x00c' COLLATE utf16_unicode_ci);",
   878  				Expected: []sql.Row{
   879  					{"AGEAYgBj"},
   880  				},
   881  			},
   882  			{
   883  				Query: "SELECT TO_BASE64(_utf8mb4'abc' COLLATE utf8mb4_0900_bin);",
   884  				Expected: []sql.Row{
   885  					{"YWJj"},
   886  				},
   887  			},
   888  			{
   889  				Query: "SELECT TO_BASE64(_utf8mb4'\x00a\x00b\x00c' COLLATE utf8mb4_0900_bin);",
   890  				Expected: []sql.Row{
   891  					{"AGEAYgBj"},
   892  				},
   893  			},
   894  		},
   895  	},
   896  	{
   897  		Name: "FROM_BASE64() function",
   898  		Queries: []CharsetCollationWireTestQuery{
   899  			{
   900  				Query: "SELECT FROM_BASE64(_utf16'\x00Y\x00W\x00J\x00j' COLLATE utf16_unicode_ci);",
   901  				Expected: []sql.Row{
   902  					{"abc"},
   903  				},
   904  			},
   905  			{
   906  				Query: "SELECT FROM_BASE64(_utf8mb4'YWJj' COLLATE utf8mb4_0900_bin);",
   907  				Expected: []sql.Row{
   908  					{"abc"},
   909  				},
   910  			},
   911  		},
   912  	},
   913  	{
   914  		Name: "TRIM() function",
   915  		SetUpScript: []string{
   916  			"SET character_set_results = 'binary';",
   917  		},
   918  		Queries: []CharsetCollationWireTestQuery{
   919  			{
   920  				Query: "SELECT TRIM(_utf16'\x00 \x00a\x00b\x00c\x00 ' COLLATE utf16_unicode_ci);",
   921  				Expected: []sql.Row{
   922  					{"\x00a\x00b\x00c"},
   923  				},
   924  			},
   925  			{
   926  				Query: "SELECT TRIM(_utf8mb4' abc ' COLLATE utf8mb4_0900_bin);",
   927  				Expected: []sql.Row{
   928  					{"abc"},
   929  				},
   930  			},
   931  			{
   932  				Query: "SELECT TRIM(_utf8mb4'\x00 \x00a\x00b\x00c\x00 ' COLLATE utf8mb4_0900_bin);",
   933  				Expected: []sql.Row{
   934  					{"\x00 \x00a\x00b\x00c\x00"},
   935  				},
   936  			},
   937  		},
   938  	},
   939  	{
   940  		Name: "RTRIM() function",
   941  		SetUpScript: []string{
   942  			"SET character_set_results = 'binary';",
   943  		},
   944  		Queries: []CharsetCollationWireTestQuery{
   945  			{
   946  				Query: "SELECT RTRIM(_utf16'\x00 \x00a\x00b\x00c\x00 ' COLLATE utf16_unicode_ci);",
   947  				Expected: []sql.Row{
   948  					{"\x00 \x00a\x00b\x00c"},
   949  				},
   950  			},
   951  			{
   952  				Query: "SELECT RTRIM(_utf8mb4' abc ' COLLATE utf8mb4_0900_bin);",
   953  				Expected: []sql.Row{
   954  					{" abc"},
   955  				},
   956  			},
   957  			{
   958  				Query: "SELECT RTRIM(_utf8mb4'\x00 \x00a\x00b\x00c\x00 ' COLLATE utf8mb4_0900_bin);",
   959  				Expected: []sql.Row{
   960  					{"\x00 \x00a\x00b\x00c\x00"},
   961  				},
   962  			},
   963  		},
   964  	},
   965  	{
   966  		Name: "LTRIM() function",
   967  		SetUpScript: []string{
   968  			"SET character_set_results = 'binary';",
   969  		},
   970  		Queries: []CharsetCollationWireTestQuery{
   971  			{
   972  				Query: "SELECT LTRIM(_utf16'\x00 \x00a\x00b\x00c\x00 ' COLLATE utf16_unicode_ci);",
   973  				Expected: []sql.Row{
   974  					{"\x00a\x00b\x00c\x00 "},
   975  				},
   976  			},
   977  			{
   978  				Query: "SELECT LTRIM(_utf8mb4' abc ' COLLATE utf8mb4_0900_bin);",
   979  				Expected: []sql.Row{
   980  					{"abc "},
   981  				},
   982  			},
   983  			{
   984  				Query: "SELECT LTRIM(_utf8mb4'\x00 \x00a\x00b\x00c\x00 ' COLLATE utf8mb4_0900_bin);",
   985  				Expected: []sql.Row{
   986  					{"\x00 \x00a\x00b\x00c\x00 "},
   987  				},
   988  			},
   989  		},
   990  	},
   991  	{
   992  		Name: "BINARY() function",
   993  		Queries: []CharsetCollationWireTestQuery{
   994  			{
   995  				Query: "SELECT BINARY(_utf16'\x00a\x00b\x00c' COLLATE utf16_unicode_ci);",
   996  				Expected: []sql.Row{
   997  					{"\x00a\x00b\x00c"},
   998  				},
   999  			},
  1000  			{
  1001  				Query: "SELECT BINARY(_utf8mb4'abc' COLLATE utf8mb4_0900_bin);",
  1002  				Expected: []sql.Row{
  1003  					{"abc"},
  1004  				},
  1005  			},
  1006  			{
  1007  				Query: "SELECT BINARY(_utf8mb4'\x00a\x00b\x00c' COLLATE utf8mb4_0900_bin);",
  1008  				Expected: []sql.Row{
  1009  					{"\x00a\x00b\x00c"},
  1010  				},
  1011  			},
  1012  		},
  1013  	},
  1014  	{
  1015  		Name: "CAST(... AS BINARY) function",
  1016  		Queries: []CharsetCollationWireTestQuery{
  1017  			{
  1018  				Query: "SELECT CAST(_utf16'\x00a\x00b\x00c' COLLATE utf16_unicode_ci AS BINARY);",
  1019  				Expected: []sql.Row{
  1020  					{"\x00a\x00b\x00c"},
  1021  				},
  1022  			},
  1023  			{
  1024  				Query: "SELECT CAST(_utf8mb4'abc' COLLATE utf8mb4_0900_bin AS BINARY);",
  1025  				Expected: []sql.Row{
  1026  					{"abc"},
  1027  				},
  1028  			},
  1029  			{
  1030  				Query: "SELECT CAST(_utf8mb4'\x00a\x00b\x00c' COLLATE utf8mb4_0900_bin AS BINARY);",
  1031  				Expected: []sql.Row{
  1032  					{"\x00a\x00b\x00c"},
  1033  				},
  1034  			},
  1035  		},
  1036  	},
  1037  }
  1038  
  1039  // DatabaseCollationWireTests are used to validate that CREATE DATABASE and ALTER DATABASE correctly handle having their
  1040  // character set and collations modified.
  1041  var DatabaseCollationWireTests = []CharsetCollationWireTest{
  1042  	{
  1043  		Name: "CREATE DATABASE default collation",
  1044  		SetUpScript: []string{
  1045  			"CREATE DATABASE test_db;",
  1046  		},
  1047  		Queries: []CharsetCollationWireTestQuery{
  1048  			{
  1049  				Query:    "USE test_db;",
  1050  				Expected: []sql.Row{},
  1051  			},
  1052  			{
  1053  				Query: "SELECT @@character_set_database, @@collation_database;",
  1054  				Expected: []sql.Row{
  1055  					{"utf8mb4", "utf8mb4_0900_bin"},
  1056  				},
  1057  			},
  1058  			{
  1059  				Query:    "DROP DATABASE test_db;",
  1060  				Expected: []sql.Row{},
  1061  			},
  1062  		},
  1063  	},
  1064  	{
  1065  		Name: "CREATE DATABASE set character set only",
  1066  		SetUpScript: []string{
  1067  			"CREATE DATABASE test_db CHARACTER SET utf8mb3;",
  1068  		},
  1069  		Queries: []CharsetCollationWireTestQuery{
  1070  			{
  1071  				Query:    "USE test_db;",
  1072  				Expected: []sql.Row{},
  1073  			},
  1074  			{
  1075  				Query: "SELECT @@character_set_database, @@collation_database;",
  1076  				Expected: []sql.Row{
  1077  					{"utf8mb3", "utf8mb3_general_ci"},
  1078  				},
  1079  			},
  1080  			{
  1081  				Query:    "DROP DATABASE test_db;",
  1082  				Expected: []sql.Row{},
  1083  			},
  1084  		},
  1085  	},
  1086  	{
  1087  		Name: "CREATE DATABASE set collation only",
  1088  		SetUpScript: []string{
  1089  			"CREATE DATABASE test_db_a COLLATE latin1_general_ci;",
  1090  			"CREATE DATABASE test_db_b COLLATE latin1_general_cs;",
  1091  		},
  1092  		Queries: []CharsetCollationWireTestQuery{
  1093  			{
  1094  				Query:    "USE test_db_a;",
  1095  				Expected: []sql.Row{},
  1096  			},
  1097  			{
  1098  				Query: "SELECT @@character_set_database, @@collation_database;",
  1099  				Expected: []sql.Row{
  1100  					{"latin1", "latin1_general_ci"},
  1101  				},
  1102  			},
  1103  			{
  1104  				Query:    "USE test_db_b;",
  1105  				Expected: []sql.Row{},
  1106  			},
  1107  			{
  1108  				Query: "SELECT @@character_set_database, @@collation_database;",
  1109  				Expected: []sql.Row{
  1110  					{"latin1", "latin1_general_cs"},
  1111  				},
  1112  			},
  1113  			{
  1114  				Query:    "DROP DATABASE test_db_a;",
  1115  				Expected: []sql.Row{},
  1116  			},
  1117  			{
  1118  				Query:    "DROP DATABASE test_db_b;",
  1119  				Expected: []sql.Row{},
  1120  			},
  1121  		},
  1122  	},
  1123  	{
  1124  		Name: "CREATE DATABASE set character set and collation",
  1125  		SetUpScript: []string{
  1126  			"CREATE DATABASE test_db CHARACTER SET utf8mb3 COLLATE utf8mb3_bin;",
  1127  		},
  1128  		Queries: []CharsetCollationWireTestQuery{
  1129  			{
  1130  				Query:    "USE test_db;",
  1131  				Expected: []sql.Row{},
  1132  			},
  1133  			{
  1134  				Query: "SELECT @@character_set_database, @@collation_database;",
  1135  				Expected: []sql.Row{
  1136  					{"utf8mb3", "utf8mb3_bin"},
  1137  				},
  1138  			},
  1139  			{
  1140  				Query: "CREATE DATABASE invalid_db CHARACTER SET utf8mb4 COLLATE ascii_bin;",
  1141  				Error: true,
  1142  			},
  1143  			{
  1144  				Query:    "DROP DATABASE test_db;",
  1145  				Expected: []sql.Row{},
  1146  			},
  1147  		},
  1148  	},
  1149  	{
  1150  		Name: "ALTER DATABASE requires character set or collation",
  1151  		SetUpScript: []string{
  1152  			"CREATE DATABASE test_db;",
  1153  		},
  1154  		Queries: []CharsetCollationWireTestQuery{
  1155  			{
  1156  				Query: "ALTER DATABASE test_db;",
  1157  				Error: true,
  1158  			},
  1159  			{
  1160  				Query:    "DROP DATABASE test_db;",
  1161  				Expected: []sql.Row{},
  1162  			},
  1163  		},
  1164  	},
  1165  	{
  1166  		Name: "ALTER DATABASE set character set only",
  1167  		SetUpScript: []string{
  1168  			"CREATE DATABASE test_db;",
  1169  		},
  1170  		Queries: []CharsetCollationWireTestQuery{
  1171  			{
  1172  				Query:    "USE test_db;",
  1173  				Expected: []sql.Row{},
  1174  			},
  1175  			{
  1176  				Query: "SELECT @@character_set_database, @@collation_database;",
  1177  				Expected: []sql.Row{
  1178  					{"utf8mb4", "utf8mb4_0900_bin"},
  1179  				},
  1180  			},
  1181  			{
  1182  				Query:    "ALTER DATABASE test_db CHARACTER SET utf8mb3;",
  1183  				Expected: []sql.Row{},
  1184  			},
  1185  			{
  1186  				Query: "SELECT @@character_set_database, @@collation_database;",
  1187  				Expected: []sql.Row{
  1188  					{"utf8mb3", "utf8mb3_general_ci"},
  1189  				},
  1190  			},
  1191  			{
  1192  				Query:    "DROP DATABASE test_db;",
  1193  				Expected: []sql.Row{},
  1194  			},
  1195  		},
  1196  	},
  1197  	{
  1198  		Name: "ALTER DATABASE set collation only",
  1199  		SetUpScript: []string{
  1200  			"CREATE DATABASE test_db_a COLLATE latin1_general_ci;",
  1201  			"CREATE DATABASE test_db_b COLLATE latin1_general_cs;",
  1202  		},
  1203  		Queries: []CharsetCollationWireTestQuery{
  1204  			{
  1205  				Query:    "USE test_db_a;",
  1206  				Expected: []sql.Row{},
  1207  			},
  1208  			{
  1209  				Query: "SELECT @@character_set_database, @@collation_database;",
  1210  				Expected: []sql.Row{
  1211  					{"latin1", "latin1_general_ci"},
  1212  				},
  1213  			},
  1214  			{
  1215  				Query:    "USE test_db_b;",
  1216  				Expected: []sql.Row{},
  1217  			},
  1218  			{
  1219  				Query: "SELECT @@character_set_database, @@collation_database;",
  1220  				Expected: []sql.Row{
  1221  					{"latin1", "latin1_general_cs"},
  1222  				},
  1223  			},
  1224  			{
  1225  				Query:    "ALTER DATABASE test_db_a COLLATE utf8mb3_bin;",
  1226  				Expected: []sql.Row{},
  1227  			},
  1228  			{
  1229  				Query:    "ALTER DATABASE test_db_b COLLATE utf8mb3_general_ci;",
  1230  				Expected: []sql.Row{},
  1231  			},
  1232  			{ // Still on test_db_b
  1233  				Query: "SELECT @@character_set_database, @@collation_database;",
  1234  				Expected: []sql.Row{
  1235  					{"utf8mb3", "utf8mb3_general_ci"},
  1236  				},
  1237  			},
  1238  			{
  1239  				Query:    "USE test_db_a;",
  1240  				Expected: []sql.Row{},
  1241  			},
  1242  			{
  1243  				Query: "SELECT @@character_set_database, @@collation_database;",
  1244  				Expected: []sql.Row{
  1245  					{"utf8mb3", "utf8mb3_bin"},
  1246  				},
  1247  			},
  1248  			{
  1249  				Query:    "DROP DATABASE test_db_a;",
  1250  				Expected: []sql.Row{},
  1251  			},
  1252  			{
  1253  				Query:    "DROP DATABASE test_db_b;",
  1254  				Expected: []sql.Row{},
  1255  			},
  1256  		},
  1257  	},
  1258  	{
  1259  		Name: "ALTER DATABASE set character set and collation",
  1260  		SetUpScript: []string{
  1261  			"CREATE DATABASE test_db CHARACTER SET utf8mb3 COLLATE utf8mb3_bin;",
  1262  		},
  1263  		Queries: []CharsetCollationWireTestQuery{
  1264  			{
  1265  				Query:    "USE test_db;",
  1266  				Expected: []sql.Row{},
  1267  			},
  1268  			{
  1269  				Query: "SELECT @@character_set_database, @@collation_database;",
  1270  				Expected: []sql.Row{
  1271  					{"utf8mb3", "utf8mb3_bin"},
  1272  				},
  1273  			},
  1274  			{
  1275  				Query:    "ALTER DATABASE test_db CHARACTER SET ascii COLLATE ascii_bin;",
  1276  				Expected: []sql.Row{},
  1277  			},
  1278  			{
  1279  				Query: "SELECT @@character_set_database, @@collation_database;",
  1280  				Expected: []sql.Row{
  1281  					{"ascii", "ascii_bin"},
  1282  				},
  1283  			},
  1284  			{
  1285  				Query:    "DROP DATABASE test_db;",
  1286  				Expected: []sql.Row{},
  1287  			},
  1288  		},
  1289  	},
  1290  	{
  1291  		Name: "Tables inherit database collation",
  1292  		SetUpScript: []string{
  1293  			"CREATE DATABASE test_db COLLATE utf8mb3_bin;",
  1294  			"CREATE TABLE test_db.other (pk VARCHAR(20) PRIMARY KEY) COLLATE utf8mb3_unicode_ci;",
  1295  		},
  1296  		Queries: []CharsetCollationWireTestQuery{
  1297  			{
  1298  				Query:    "USE test_db;",
  1299  				Expected: []sql.Row{},
  1300  			},
  1301  			{
  1302  				Query: "CREATE TABLE test_a (pk VARCHAR(20) PRIMARY KEY);",
  1303  				Expected: []sql.Row{
  1304  					{types.NewOkResult(0)},
  1305  				},
  1306  			},
  1307  			{ // LIKE should inherit the table's collation, NOT the database's collation
  1308  				Query: "CREATE TABLE test_b LIKE other;",
  1309  				Expected: []sql.Row{
  1310  					{types.NewOkResult(0)},
  1311  				},
  1312  			},
  1313  			{ // AS SELECT should inherit the database's collation, but the column retains the original collation
  1314  				Query: "CREATE TABLE test_c AS SELECT * FROM other;",
  1315  				Expected: []sql.Row{
  1316  					{types.NewOkResult(0)},
  1317  				},
  1318  			},
  1319  			{
  1320  				Query: "SHOW CREATE TABLE test_a;",
  1321  				Expected: []sql.Row{
  1322  					{"test_a", "CREATE TABLE `test_a` (\n  `pk` varchar(20) NOT NULL,\n  PRIMARY KEY (`pk`)\n) ENGINE=InnoDB DEFAULT CHARSET=utf8mb3 COLLATE=utf8mb3_bin"},
  1323  				},
  1324  			},
  1325  			{
  1326  				Query: "SHOW CREATE TABLE test_b;",
  1327  				Expected: []sql.Row{
  1328  					{"test_b", "CREATE TABLE `test_b` (\n  `pk` varchar(20) NOT NULL,\n  PRIMARY KEY (`pk`)\n) ENGINE=InnoDB DEFAULT CHARSET=utf8mb3 COLLATE=utf8mb3_unicode_ci"},
  1329  				},
  1330  			},
  1331  			{
  1332  				Query: "SHOW CREATE TABLE test_c;",
  1333  				Expected: []sql.Row{
  1334  					{"test_c", "CREATE TABLE `test_c` (\n  `pk` varchar(20) COLLATE utf8mb3_unicode_ci NOT NULL\n) ENGINE=InnoDB DEFAULT CHARSET=utf8mb3 COLLATE=utf8mb3_bin"},
  1335  				},
  1336  			},
  1337  			{
  1338  				Query:    "ALTER DATABASE test_db COLLATE utf8mb3_general_ci;",
  1339  				Expected: []sql.Row{},
  1340  			},
  1341  			{
  1342  				Query: "CREATE TABLE test_d (pk VARCHAR(20) PRIMARY KEY);",
  1343  				Expected: []sql.Row{
  1344  					{types.NewOkResult(0)},
  1345  				},
  1346  			},
  1347  			{
  1348  				Query: "SHOW CREATE TABLE test_d;",
  1349  				Expected: []sql.Row{
  1350  					{"test_d", "CREATE TABLE `test_d` (\n  `pk` varchar(20) NOT NULL,\n  PRIMARY KEY (`pk`)\n) ENGINE=InnoDB DEFAULT CHARSET=utf8mb3 COLLATE=utf8mb3_general_ci"},
  1351  				},
  1352  			},
  1353  			{
  1354  				Query:    "DROP DATABASE test_db;",
  1355  				Expected: []sql.Row{},
  1356  			},
  1357  		},
  1358  	},
  1359  	{
  1360  		Name: "INFORMATION_SCHEMA shows character set and collation",
  1361  		SetUpScript: []string{
  1362  			"CREATE DATABASE test_db_a COLLATE latin1_general_ci;",
  1363  			"CREATE DATABASE test_db_b COLLATE latin1_general_cs;",
  1364  		},
  1365  		Queries: []CharsetCollationWireTestQuery{
  1366  			{
  1367  				Query:    "USE test_db_a;",
  1368  				Expected: []sql.Row{},
  1369  			},
  1370  			{
  1371  				Query: "SELECT DEFAULT_CHARACTER_SET_NAME, DEFAULT_COLLATION_NAME FROM INFORMATION_SCHEMA.SCHEMATA WHERE SCHEMA_NAME = 'test_db_a';",
  1372  				Expected: []sql.Row{
  1373  					{"latin1", "latin1_general_ci"},
  1374  				},
  1375  			},
  1376  			{
  1377  				Query: "SELECT DEFAULT_CHARACTER_SET_NAME, DEFAULT_COLLATION_NAME FROM INFORMATION_SCHEMA.SCHEMATA WHERE SCHEMA_NAME = 'test_db_b';",
  1378  				Expected: []sql.Row{
  1379  					{"latin1", "latin1_general_cs"},
  1380  				},
  1381  			},
  1382  			{
  1383  				Query:    "ALTER DATABASE test_db_a COLLATE utf8mb3_general_ci;",
  1384  				Expected: []sql.Row{},
  1385  			},
  1386  			{
  1387  				Query: "SELECT DEFAULT_CHARACTER_SET_NAME, DEFAULT_COLLATION_NAME FROM INFORMATION_SCHEMA.SCHEMATA WHERE SCHEMA_NAME = 'test_db_a';",
  1388  				Expected: []sql.Row{
  1389  					{"utf8mb3", "utf8mb3_general_ci"},
  1390  				},
  1391  			},
  1392  			{
  1393  				Query:    "DROP DATABASE test_db_a;",
  1394  				Expected: []sql.Row{},
  1395  			},
  1396  			{
  1397  				Query:    "DROP DATABASE test_db_b;",
  1398  				Expected: []sql.Row{},
  1399  			},
  1400  		},
  1401  	},
  1402  	{
  1403  		Name: "Issue #5482",
  1404  		Queries: []CharsetCollationWireTestQuery{
  1405  			{
  1406  				Query: `SELECT T.TABLE_NAME AS label, 'connection.table' as type, T.TABLE_SCHEMA AS 'schema',
  1407  T.TABLE_SCHEMA AS 'database', T.TABLE_CATALOG AS 'catalog',
  1408  0 AS isView FROM INFORMATION_SCHEMA.TABLES AS T WHERE T.TABLE_CATALOG = 'def' AND
  1409                                                        UPPER(T.TABLE_TYPE) = 'BASE TABLE' ORDER BY T.TABLE_NAME;`,
  1410  				Expected: []sql.Row(nil),
  1411  			},
  1412  		},
  1413  	},
  1414  }