vitess.io/vitess@v0.16.2/go/vt/vttablet/endtoend/compatibility_test.go (about)

     1  /*
     2  Copyright 2019 The Vitess Authors.
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8      http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  
    17  package endtoend
    18  
    19  import (
    20  	"reflect"
    21  	"strings"
    22  	"testing"
    23  
    24  	"vitess.io/vitess/go/test/utils"
    25  
    26  	"github.com/stretchr/testify/assert"
    27  	"github.com/stretchr/testify/require"
    28  
    29  	"vitess.io/vitess/go/sqltypes"
    30  	querypb "vitess.io/vitess/go/vt/proto/query"
    31  	"vitess.io/vitess/go/vt/vttablet/endtoend/framework"
    32  )
    33  
    34  var point12 = "\x00\x00\x00\x00\x01\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\xf0?\x00\x00\x00\x00\x00\x00\x00@"
    35  
    36  func TestCharaterSet(t *testing.T) {
    37  	qr, err := framework.NewClient().Execute("select * from vitess_test where intval=1", nil)
    38  	if err != nil {
    39  		t.Fatal(err)
    40  	}
    41  	want := &sqltypes.Result{
    42  		Fields: []*querypb.Field{
    43  			{
    44  				Name:         "intval",
    45  				Type:         sqltypes.Int32,
    46  				Table:        "vitess_test",
    47  				OrgTable:     "vitess_test",
    48  				Database:     "vttest",
    49  				OrgName:      "intval",
    50  				ColumnLength: 11,
    51  				Charset:      63,
    52  				Flags:        49155,
    53  			}, {
    54  				Name:         "floatval",
    55  				Type:         sqltypes.Float32,
    56  				Table:        "vitess_test",
    57  				OrgTable:     "vitess_test",
    58  				Database:     "vttest",
    59  				OrgName:      "floatval",
    60  				ColumnLength: 12,
    61  				Charset:      63,
    62  				Decimals:     31,
    63  				Flags:        32768,
    64  			}, {
    65  				Name:         "charval",
    66  				Type:         sqltypes.VarChar,
    67  				Table:        "vitess_test",
    68  				OrgTable:     "vitess_test",
    69  				Database:     "vttest",
    70  				OrgName:      "charval",
    71  				ColumnLength: 40,
    72  				Charset:      45,
    73  			}, {
    74  				Name:         "binval",
    75  				Type:         sqltypes.VarBinary,
    76  				Table:        "vitess_test",
    77  				OrgTable:     "vitess_test",
    78  				Database:     "vttest",
    79  				OrgName:      "binval",
    80  				ColumnLength: 256,
    81  				Charset:      63,
    82  				Flags:        128,
    83  			},
    84  		},
    85  		Rows: [][]sqltypes.Value{
    86  			{
    87  				sqltypes.TestValue(sqltypes.Int32, "1"),
    88  				sqltypes.TestValue(sqltypes.Float32, "1.12345"),
    89  				sqltypes.TestValue(sqltypes.VarChar, "\xc2\xa2"),
    90  				sqltypes.TestValue(sqltypes.VarBinary, "\x00\xff"),
    91  			},
    92  		},
    93  	}
    94  	utils.MustMatch(t, want, qr)
    95  }
    96  
    97  func TestInts(t *testing.T) {
    98  	client := framework.NewClient()
    99  	defer client.Execute("delete from vitess_ints", nil)
   100  
   101  	_, err := client.Execute(
   102  		"insert into vitess_ints values(:tiny, :tinyu, :small, "+
   103  			":smallu, :medium, :mediumu, :normal, :normalu, :big, :bigu, :year)",
   104  		map[string]*querypb.BindVariable{
   105  			"tiny":    sqltypes.Int64BindVariable(-128),
   106  			"tinyu":   sqltypes.Uint64BindVariable(255),
   107  			"small":   sqltypes.Int64BindVariable(-32768),
   108  			"smallu":  sqltypes.Uint64BindVariable(65535),
   109  			"medium":  sqltypes.Int64BindVariable(-8388608),
   110  			"mediumu": sqltypes.Uint64BindVariable(16777215),
   111  			"normal":  sqltypes.Int64BindVariable(-2147483648),
   112  			"normalu": sqltypes.Uint64BindVariable(4294967295),
   113  			"big":     sqltypes.Int64BindVariable(-9223372036854775808),
   114  			"bigu":    sqltypes.Uint64BindVariable(18446744073709551615),
   115  			"year":    sqltypes.Int64BindVariable(2012),
   116  		},
   117  	)
   118  	if err != nil {
   119  		t.Fatal(err)
   120  	}
   121  	qr, err := client.Execute("select * from vitess_ints where tiny = -128", nil)
   122  	if err != nil {
   123  		t.Fatal(err)
   124  	}
   125  	want := &sqltypes.Result{
   126  		Fields: []*querypb.Field{
   127  			{
   128  				Name:         "tiny",
   129  				Type:         sqltypes.Int8,
   130  				Table:        "vitess_ints",
   131  				OrgTable:     "vitess_ints",
   132  				Database:     "vttest",
   133  				OrgName:      "tiny",
   134  				ColumnLength: 4,
   135  				Charset:      63,
   136  				Flags:        49155,
   137  			}, {
   138  				Name:         "tinyu",
   139  				Type:         sqltypes.Uint8,
   140  				Table:        "vitess_ints",
   141  				OrgTable:     "vitess_ints",
   142  				Database:     "vttest",
   143  				OrgName:      "tinyu",
   144  				ColumnLength: 3,
   145  				Charset:      63,
   146  				Flags:        32800,
   147  			}, {
   148  				Name:         "small",
   149  				Type:         sqltypes.Int16,
   150  				Table:        "vitess_ints",
   151  				OrgTable:     "vitess_ints",
   152  				Database:     "vttest",
   153  				OrgName:      "small",
   154  				ColumnLength: 6,
   155  				Charset:      63,
   156  				Flags:        32768,
   157  			}, {
   158  				Name:         "smallu",
   159  				Type:         sqltypes.Uint16,
   160  				Table:        "vitess_ints",
   161  				OrgTable:     "vitess_ints",
   162  				Database:     "vttest",
   163  				OrgName:      "smallu",
   164  				ColumnLength: 5,
   165  				Charset:      63,
   166  				Flags:        32800,
   167  			}, {
   168  				Name:         "medium",
   169  				Type:         sqltypes.Int24,
   170  				Table:        "vitess_ints",
   171  				OrgTable:     "vitess_ints",
   172  				Database:     "vttest",
   173  				OrgName:      "medium",
   174  				ColumnLength: 9,
   175  				Charset:      63,
   176  				Flags:        32768,
   177  			}, {
   178  				Name:         "mediumu",
   179  				Type:         sqltypes.Uint24,
   180  				Table:        "vitess_ints",
   181  				OrgTable:     "vitess_ints",
   182  				Database:     "vttest",
   183  				OrgName:      "mediumu",
   184  				ColumnLength: 8,
   185  				Charset:      63,
   186  				Flags:        32800,
   187  			}, {
   188  				Name:         "normal",
   189  				Type:         sqltypes.Int32,
   190  				Table:        "vitess_ints",
   191  				OrgTable:     "vitess_ints",
   192  				Database:     "vttest",
   193  				OrgName:      "normal",
   194  				ColumnLength: 11,
   195  				Charset:      63,
   196  				Flags:        32768,
   197  			}, {
   198  				Name:         "normalu",
   199  				Type:         sqltypes.Uint32,
   200  				Table:        "vitess_ints",
   201  				OrgTable:     "vitess_ints",
   202  				Database:     "vttest",
   203  				OrgName:      "normalu",
   204  				ColumnLength: 10,
   205  				Charset:      63,
   206  				Flags:        32800,
   207  			}, {
   208  				Name:         "big",
   209  				Type:         sqltypes.Int64,
   210  				Table:        "vitess_ints",
   211  				OrgTable:     "vitess_ints",
   212  				Database:     "vttest",
   213  				OrgName:      "big",
   214  				ColumnLength: 20,
   215  				Charset:      63,
   216  				Flags:        32768,
   217  			}, {
   218  				Name:         "bigu",
   219  				Type:         sqltypes.Uint64,
   220  				Table:        "vitess_ints",
   221  				OrgTable:     "vitess_ints",
   222  				Database:     "vttest",
   223  				OrgName:      "bigu",
   224  				ColumnLength: 20,
   225  				Charset:      63,
   226  				Flags:        32800,
   227  			}, {
   228  				Name:         "y",
   229  				Type:         sqltypes.Year,
   230  				Table:        "vitess_ints",
   231  				OrgTable:     "vitess_ints",
   232  				Database:     "vttest",
   233  				OrgName:      "y",
   234  				ColumnLength: 4,
   235  				Charset:      63,
   236  				Flags:        32864,
   237  			},
   238  		},
   239  		Rows: [][]sqltypes.Value{
   240  			{
   241  				sqltypes.TestValue(sqltypes.Int8, "-128"),
   242  				sqltypes.TestValue(sqltypes.Uint8, "255"),
   243  				sqltypes.TestValue(sqltypes.Int16, "-32768"),
   244  				sqltypes.TestValue(sqltypes.Uint16, "65535"),
   245  				sqltypes.TestValue(sqltypes.Int24, "-8388608"),
   246  				sqltypes.TestValue(sqltypes.Uint24, "16777215"),
   247  				sqltypes.TestValue(sqltypes.Int32, "-2147483648"),
   248  				sqltypes.TestValue(sqltypes.Uint32, "4294967295"),
   249  				sqltypes.TestValue(sqltypes.Int64, "-9223372036854775808"),
   250  				sqltypes.TestValue(sqltypes.Uint64, "18446744073709551615"),
   251  				sqltypes.TestValue(sqltypes.Year, "2012"),
   252  			},
   253  		},
   254  	}
   255  	utils.MustMatch(t, want, qr)
   256  
   257  	// This test was added because the following query causes mysql to
   258  	// return flags with both binary and unsigned set. The test ensures
   259  	// that a Uint64 is produced in spite of the stray binary flag.
   260  	qr, err = client.Execute("select max(bigu) from vitess_ints", nil)
   261  	if err != nil {
   262  		t.Fatal(err)
   263  	}
   264  	want = &sqltypes.Result{
   265  		Fields: []*querypb.Field{
   266  			{
   267  				Name:         "max(bigu)",
   268  				Type:         sqltypes.Uint64,
   269  				ColumnLength: 20,
   270  				Charset:      63,
   271  				Flags:        32928,
   272  			},
   273  		},
   274  		Rows: [][]sqltypes.Value{
   275  			{
   276  				sqltypes.TestValue(sqltypes.Uint64, "18446744073709551615"),
   277  			},
   278  		},
   279  	}
   280  	utils.MustMatch(t, want, qr)
   281  
   282  }
   283  
   284  func TestFractionals(t *testing.T) {
   285  	client := framework.NewClient()
   286  	defer client.Execute("delete from vitess_fracts", nil)
   287  
   288  	_, err := client.Execute(
   289  		"insert into vitess_fracts values(:id, :deci, :num, :f, :d)",
   290  		map[string]*querypb.BindVariable{
   291  			"id":   sqltypes.Int64BindVariable(1),
   292  			"deci": sqltypes.StringBindVariable("1.99"),
   293  			"num":  sqltypes.StringBindVariable("2.99"),
   294  			"f":    sqltypes.Float64BindVariable(3.99),
   295  			"d":    sqltypes.Float64BindVariable(4.99),
   296  		},
   297  	)
   298  	if err != nil {
   299  		t.Fatal(err)
   300  	}
   301  	qr, err := client.Execute("select * from vitess_fracts where id = 1", nil)
   302  	if err != nil {
   303  		t.Fatal(err)
   304  	}
   305  	want := &sqltypes.Result{
   306  		Fields: []*querypb.Field{
   307  			{
   308  				Name:         "id",
   309  				Type:         sqltypes.Int32,
   310  				Table:        "vitess_fracts",
   311  				OrgTable:     "vitess_fracts",
   312  				Database:     "vttest",
   313  				OrgName:      "id",
   314  				ColumnLength: 11,
   315  				Charset:      63,
   316  				Flags:        49155,
   317  			}, {
   318  				Name:         "deci",
   319  				Type:         sqltypes.Decimal,
   320  				Table:        "vitess_fracts",
   321  				OrgTable:     "vitess_fracts",
   322  				Database:     "vttest",
   323  				OrgName:      "deci",
   324  				ColumnLength: 7,
   325  				Charset:      63,
   326  				Decimals:     2,
   327  				Flags:        32768,
   328  			}, {
   329  				Name:         "num",
   330  				Type:         sqltypes.Decimal,
   331  				Table:        "vitess_fracts",
   332  				OrgTable:     "vitess_fracts",
   333  				Database:     "vttest",
   334  				OrgName:      "num",
   335  				ColumnLength: 7,
   336  				Charset:      63,
   337  				Decimals:     2,
   338  				Flags:        32768,
   339  			}, {
   340  				Name:         "f",
   341  				Type:         sqltypes.Float32,
   342  				Table:        "vitess_fracts",
   343  				OrgTable:     "vitess_fracts",
   344  				Database:     "vttest",
   345  				OrgName:      "f",
   346  				ColumnLength: 12,
   347  				Charset:      63,
   348  				Decimals:     31,
   349  				Flags:        32768,
   350  			}, {
   351  				Name:         "d",
   352  				Type:         sqltypes.Float64,
   353  				Table:        "vitess_fracts",
   354  				OrgTable:     "vitess_fracts",
   355  				Database:     "vttest",
   356  				OrgName:      "d",
   357  				ColumnLength: 22,
   358  				Charset:      63,
   359  				Decimals:     31,
   360  				Flags:        32768,
   361  			},
   362  		},
   363  		Rows: [][]sqltypes.Value{
   364  			{
   365  				sqltypes.TestValue(sqltypes.Int32, "1"),
   366  				sqltypes.TestValue(sqltypes.Decimal, "1.99"),
   367  				sqltypes.TestValue(sqltypes.Decimal, "2.99"),
   368  				sqltypes.TestValue(sqltypes.Float32, "3.99"),
   369  				sqltypes.TestValue(sqltypes.Float64, "4.99"),
   370  			},
   371  		},
   372  	}
   373  	utils.MustMatch(t, want, qr)
   374  }
   375  
   376  func TestStrings(t *testing.T) {
   377  	client := framework.NewClient()
   378  	defer client.Execute("delete from vitess_strings", nil)
   379  
   380  	_, err := client.Execute(
   381  		"insert into vitess_strings values "+
   382  			"(:vb, :c, :vc, :b, :tb, :bl, :ttx, :tx, :en, :s)",
   383  		map[string]*querypb.BindVariable{
   384  			"vb":  sqltypes.StringBindVariable("a"),
   385  			"c":   sqltypes.StringBindVariable("b"),
   386  			"vc":  sqltypes.StringBindVariable("c"),
   387  			"b":   sqltypes.StringBindVariable("d"),
   388  			"tb":  sqltypes.StringBindVariable("e"),
   389  			"bl":  sqltypes.StringBindVariable("f"),
   390  			"ttx": sqltypes.StringBindVariable("g"),
   391  			"tx":  sqltypes.StringBindVariable("h"),
   392  			"en":  sqltypes.StringBindVariable("a"),
   393  			"s":   sqltypes.StringBindVariable("a,b"),
   394  		},
   395  	)
   396  	if err != nil {
   397  		t.Fatal(err)
   398  	}
   399  	qr, err := client.Execute("select * from vitess_strings where vb = 'a'", nil)
   400  	if err != nil {
   401  		t.Fatal(err)
   402  	}
   403  	want := &sqltypes.Result{
   404  		Fields: []*querypb.Field{
   405  			{
   406  				Name:         "vb",
   407  				Type:         sqltypes.VarBinary,
   408  				Table:        "vitess_strings",
   409  				OrgTable:     "vitess_strings",
   410  				Database:     "vttest",
   411  				OrgName:      "vb",
   412  				ColumnLength: 16,
   413  				Charset:      63,
   414  				Flags:        16515,
   415  			}, {
   416  				Name:         "c",
   417  				Type:         sqltypes.Char,
   418  				Table:        "vitess_strings",
   419  				OrgTable:     "vitess_strings",
   420  				Database:     "vttest",
   421  				OrgName:      "c",
   422  				ColumnLength: 64,
   423  				Charset:      45,
   424  			}, {
   425  				Name:         "vc",
   426  				Type:         sqltypes.VarChar,
   427  				Table:        "vitess_strings",
   428  				OrgTable:     "vitess_strings",
   429  				Database:     "vttest",
   430  				OrgName:      "vc",
   431  				ColumnLength: 64,
   432  				Charset:      45,
   433  			}, {
   434  				Name:         "b",
   435  				Type:         sqltypes.Binary,
   436  				Table:        "vitess_strings",
   437  				OrgTable:     "vitess_strings",
   438  				Database:     "vttest",
   439  				OrgName:      "b",
   440  				ColumnLength: 4,
   441  				Charset:      63,
   442  				Flags:        128,
   443  			}, {
   444  				Name:         "tb",
   445  				Type:         sqltypes.Blob,
   446  				Table:        "vitess_strings",
   447  				OrgTable:     "vitess_strings",
   448  				Database:     "vttest",
   449  				OrgName:      "tb",
   450  				ColumnLength: 255,
   451  				Charset:      63,
   452  				Flags:        144,
   453  			}, {
   454  				Name:         "bl",
   455  				Type:         sqltypes.Blob,
   456  				Table:        "vitess_strings",
   457  				OrgTable:     "vitess_strings",
   458  				Database:     "vttest",
   459  				OrgName:      "bl",
   460  				ColumnLength: 65535,
   461  				Charset:      63,
   462  				Flags:        144,
   463  			}, {
   464  				Name:         "ttx",
   465  				Type:         sqltypes.Text,
   466  				Table:        "vitess_strings",
   467  				OrgTable:     "vitess_strings",
   468  				Database:     "vttest",
   469  				OrgName:      "ttx",
   470  				ColumnLength: 1020,
   471  				Charset:      45,
   472  				Flags:        16,
   473  			}, {
   474  				Name:         "tx",
   475  				Type:         sqltypes.Text,
   476  				Table:        "vitess_strings",
   477  				OrgTable:     "vitess_strings",
   478  				Database:     "vttest",
   479  				OrgName:      "tx",
   480  				ColumnLength: 262140,
   481  				Charset:      45,
   482  				Flags:        16,
   483  			}, {
   484  				Name:         "en",
   485  				Type:         sqltypes.Enum,
   486  				Table:        "vitess_strings",
   487  				OrgTable:     "vitess_strings",
   488  				Database:     "vttest",
   489  				OrgName:      "en",
   490  				ColumnLength: 4,
   491  				Charset:      45,
   492  				Flags:        256,
   493  			}, {
   494  				Name:         "s",
   495  				Type:         sqltypes.Set,
   496  				Table:        "vitess_strings",
   497  				OrgTable:     "vitess_strings",
   498  				Database:     "vttest",
   499  				OrgName:      "s",
   500  				ColumnLength: 12,
   501  				Charset:      45,
   502  				Flags:        2048,
   503  			},
   504  		},
   505  		Rows: [][]sqltypes.Value{
   506  			{
   507  				sqltypes.TestValue(sqltypes.VarBinary, "a"),
   508  				sqltypes.TestValue(sqltypes.Char, "b"),
   509  				sqltypes.TestValue(sqltypes.VarChar, "c"),
   510  				sqltypes.TestValue(sqltypes.Binary, "d\x00\x00\x00"),
   511  				sqltypes.TestValue(sqltypes.Blob, "e"),
   512  				sqltypes.TestValue(sqltypes.Blob, "f"),
   513  				sqltypes.TestValue(sqltypes.Text, "g"),
   514  				sqltypes.TestValue(sqltypes.Text, "h"),
   515  				sqltypes.TestValue(sqltypes.Enum, "a"),
   516  				sqltypes.TestValue(sqltypes.Set, "a,b"),
   517  			},
   518  		},
   519  	}
   520  	utils.MustMatch(t, want, qr)
   521  }
   522  
   523  func TestMiscTypes(t *testing.T) {
   524  	client := framework.NewClient()
   525  	defer client.Execute("delete from vitess_misc", nil)
   526  
   527  	_, err := client.Execute(
   528  		"insert into vitess_misc values(:id, :b, :d, :dt, :t, point(1, 2))",
   529  		map[string]*querypb.BindVariable{
   530  			"id": sqltypes.Int64BindVariable(1),
   531  			"b":  sqltypes.StringBindVariable("\x01"),
   532  			"d":  sqltypes.StringBindVariable("2012-01-01"),
   533  			"dt": sqltypes.StringBindVariable("2012-01-01 15:45:45"),
   534  			"t":  sqltypes.StringBindVariable("15:45:45"),
   535  		},
   536  	)
   537  	if err != nil {
   538  		t.Fatal(err)
   539  	}
   540  	qr, err := client.Execute("select * from vitess_misc where id = 1", nil)
   541  	if err != nil {
   542  		t.Fatal(err)
   543  	}
   544  	want := &sqltypes.Result{
   545  		Fields: []*querypb.Field{
   546  			{
   547  				Name:         "id",
   548  				Type:         sqltypes.Int32,
   549  				Table:        "vitess_misc",
   550  				OrgTable:     "vitess_misc",
   551  				Database:     "vttest",
   552  				OrgName:      "id",
   553  				ColumnLength: 11,
   554  				Charset:      63,
   555  				Flags:        49155,
   556  			}, {
   557  				Name:         "b",
   558  				Type:         sqltypes.Bit,
   559  				Table:        "vitess_misc",
   560  				OrgTable:     "vitess_misc",
   561  				Database:     "vttest",
   562  				OrgName:      "b",
   563  				ColumnLength: 8,
   564  				Charset:      63,
   565  				Flags:        32,
   566  			}, {
   567  				Name:         "d",
   568  				Type:         sqltypes.Date,
   569  				Table:        "vitess_misc",
   570  				OrgTable:     "vitess_misc",
   571  				Database:     "vttest",
   572  				OrgName:      "d",
   573  				ColumnLength: 10,
   574  				Charset:      63,
   575  				Flags:        128,
   576  			}, {
   577  				Name:         "dt",
   578  				Type:         sqltypes.Datetime,
   579  				Table:        "vitess_misc",
   580  				OrgTable:     "vitess_misc",
   581  				Database:     "vttest",
   582  				OrgName:      "dt",
   583  				ColumnLength: 19,
   584  				Charset:      63,
   585  				Flags:        128,
   586  			}, {
   587  				Name:         "t",
   588  				Type:         sqltypes.Time,
   589  				Table:        "vitess_misc",
   590  				OrgTable:     "vitess_misc",
   591  				Database:     "vttest",
   592  				OrgName:      "t",
   593  				ColumnLength: 10,
   594  				Charset:      63,
   595  				Flags:        128,
   596  			}, {
   597  				Name:         "g",
   598  				Type:         sqltypes.Geometry,
   599  				Table:        "vitess_misc",
   600  				OrgTable:     "vitess_misc",
   601  				Database:     "vttest",
   602  				OrgName:      "g",
   603  				ColumnLength: 4294967295,
   604  				Charset:      63,
   605  				Flags:        144,
   606  			},
   607  		},
   608  		Rows: [][]sqltypes.Value{
   609  			{
   610  				sqltypes.TestValue(sqltypes.Int32, "1"),
   611  				sqltypes.TestValue(sqltypes.Bit, "\x01"),
   612  				sqltypes.TestValue(sqltypes.Date, "2012-01-01"),
   613  				sqltypes.TestValue(sqltypes.Datetime, "2012-01-01 15:45:45"),
   614  				sqltypes.TestValue(sqltypes.Time, "15:45:45"),
   615  				sqltypes.TestValue(sqltypes.Geometry, point12),
   616  			},
   617  		},
   618  	}
   619  	utils.MustMatch(t, want, qr)
   620  }
   621  
   622  func TestNull(t *testing.T) {
   623  	client := framework.NewClient()
   624  	qr, err := client.Execute("select null from dual", nil)
   625  	if err != nil {
   626  		t.Fatal(err)
   627  	}
   628  	want := &sqltypes.Result{
   629  		Fields: []*querypb.Field{
   630  			{
   631  				Name:    "NULL",
   632  				Type:    sqltypes.Null,
   633  				Charset: 63,
   634  				Flags:   32896,
   635  			},
   636  		},
   637  		Rows: [][]sqltypes.Value{
   638  			{
   639  				{},
   640  			},
   641  		},
   642  	}
   643  	utils.MustMatch(t, want, qr)
   644  }
   645  
   646  func TestJSONType(t *testing.T) {
   647  	// JSON is supported only after mysql57.
   648  	client := framework.NewClient()
   649  	if _, err := client.Execute("create table vitess_json(id int default 1, val json, primary key(id))", nil); err != nil {
   650  		// If it's a syntax error, MySQL is an older version. Skip this test.
   651  		if strings.Contains(err.Error(), "syntax") {
   652  			return
   653  		}
   654  		t.Fatal(err)
   655  	}
   656  	defer client.Execute("drop table vitess_json", nil)
   657  
   658  	if _, err := client.Execute(`insert into vitess_json values(1, '{"foo": "bar"}')`, nil); err != nil {
   659  		t.Fatal(err)
   660  	}
   661  
   662  	qr, err := client.Execute("select id, val from vitess_json", nil)
   663  	if err != nil {
   664  		t.Fatal(err)
   665  	}
   666  	want := &sqltypes.Result{
   667  		Fields: []*querypb.Field{
   668  			{
   669  				Name:         "id",
   670  				Type:         sqltypes.Int32,
   671  				Table:        "vitess_json",
   672  				OrgTable:     "vitess_json",
   673  				Database:     "vttest",
   674  				OrgName:      "id",
   675  				ColumnLength: 11,
   676  				Charset:      63,
   677  				Flags:        49155,
   678  			}, {
   679  				Name:         "val",
   680  				Type:         sqltypes.TypeJSON,
   681  				Table:        "vitess_json",
   682  				OrgTable:     "vitess_json",
   683  				Database:     "vttest",
   684  				OrgName:      "val",
   685  				ColumnLength: 4294967295,
   686  				Charset:      63,
   687  				Flags:        144,
   688  			},
   689  		},
   690  		Rows: [][]sqltypes.Value{
   691  			{
   692  				sqltypes.TestValue(sqltypes.Int32, "1"),
   693  				sqltypes.TestValue(sqltypes.TypeJSON, "{\"foo\": \"bar\"}"),
   694  			},
   695  		},
   696  		StatusFlags: sqltypes.ServerStatusNoIndexUsed | sqltypes.ServerStatusAutocommit,
   697  	}
   698  	if !reflect.DeepEqual(qr, want) {
   699  		// MariaDB 10.3 has different behavior.
   700  		want2 := want.Copy()
   701  		want2.Fields[1].Type = sqltypes.Blob
   702  		want2.Fields[1].Charset = 33
   703  		want2.Rows[0][1] = sqltypes.TestValue(sqltypes.Blob, "{\"foo\": \"bar\"}")
   704  		utils.MustMatch(t, want2, qr)
   705  	}
   706  
   707  }
   708  
   709  func TestDBName(t *testing.T) {
   710  	client := framework.NewClient()
   711  	qr, err := client.Execute("select * from information_schema.tables where null", nil)
   712  	require.NoError(t, err)
   713  	for _, field := range qr.Fields {
   714  		t.Run("i_s:"+field.Name, func(t *testing.T) {
   715  			if field.Database != "" {
   716  				assert.Equal(t, "information_schema", field.Database, "field : %s", field.Name)
   717  			}
   718  		})
   719  	}
   720  
   721  	qr, err = client.Execute("select * from mysql.user where null", nil)
   722  	require.NoError(t, err)
   723  	for _, field := range qr.Fields {
   724  		t.Run("mysql:"+field.Name, func(t *testing.T) {
   725  			if field.Database != "" {
   726  				assert.Equal(t, "mysql", field.Database, "field : %s", field.Name)
   727  			}
   728  		})
   729  	}
   730  
   731  	qr, err = client.Execute("select * from sys.processlist where null", nil)
   732  	require.NoError(t, err)
   733  	for _, field := range qr.Fields {
   734  		t.Run("sys:"+field.Name, func(t *testing.T) {
   735  			assert.NotEqual(t, "vttest", field.Database, "field : %s", field.Name)
   736  		})
   737  	}
   738  
   739  	qr, err = client.Execute("select * from performance_schema.mutex_instances where null", nil)
   740  	require.NoError(t, err)
   741  	for _, field := range qr.Fields {
   742  		t.Run("performance_schema:"+field.Name, func(t *testing.T) {
   743  			if field.Database != "" {
   744  				assert.Equal(t, "performance_schema", field.Database, "field : %s", field.Name)
   745  			}
   746  		})
   747  	}
   748  }