vitess.io/vitess@v0.16.2/go/mysql/binlog_event_rbr_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 mysql
    18  
    19  import (
    20  	"bytes"
    21  	"fmt"
    22  	"testing"
    23  
    24  	"vitess.io/vitess/go/sqltypes"
    25  	querypb "vitess.io/vitess/go/vt/proto/query"
    26  )
    27  
    28  func TestCellLengthAndData(t *testing.T) {
    29  	testcases := []struct {
    30  		typ      byte
    31  		metadata uint16
    32  		styp     querypb.Type
    33  		data     []byte
    34  		out      sqltypes.Value
    35  	}{{
    36  		typ:  TypeTiny,
    37  		styp: querypb.Type_UINT8,
    38  		data: []byte{0x82},
    39  		out: sqltypes.MakeTrusted(querypb.Type_UINT8,
    40  			[]byte("130")),
    41  	}, {
    42  		typ:  TypeTiny,
    43  		styp: querypb.Type_INT8,
    44  		data: []byte{0xfe},
    45  		out: sqltypes.MakeTrusted(querypb.Type_INT8,
    46  			[]byte("-2")),
    47  	}, {
    48  		typ:  TypeYear,
    49  		data: []byte{0x82},
    50  		out: sqltypes.MakeTrusted(querypb.Type_YEAR,
    51  			[]byte("2030")),
    52  	}, {
    53  		typ:  TypeShort,
    54  		styp: querypb.Type_UINT16,
    55  		data: []byte{0x82, 0x81},
    56  		out: sqltypes.MakeTrusted(querypb.Type_UINT16,
    57  			[]byte(fmt.Sprintf("%v", 0x8182))),
    58  	}, {
    59  		typ:  TypeShort,
    60  		styp: querypb.Type_INT16,
    61  		data: []byte{0xfe, 0xff},
    62  		out: sqltypes.MakeTrusted(querypb.Type_INT16,
    63  			[]byte(fmt.Sprintf("%v", -1-int32(0x0001)))),
    64  	}, {
    65  		typ:  TypeInt24,
    66  		styp: querypb.Type_UINT24,
    67  		data: []byte{0x83, 0x82, 0x81},
    68  		out: sqltypes.MakeTrusted(querypb.Type_UINT24,
    69  			[]byte(fmt.Sprintf("%v", 0x818283))),
    70  	}, {
    71  		typ:  TypeInt24,
    72  		styp: querypb.Type_INT24,
    73  		data: []byte{0xfd, 0xfe, 0xff},
    74  		out: sqltypes.MakeTrusted(querypb.Type_INT24,
    75  			[]byte(fmt.Sprintf("%v", -1-int32(0x000102)))),
    76  	}, {
    77  		typ:  TypeLong,
    78  		styp: querypb.Type_UINT32,
    79  		data: []byte{0x84, 0x83, 0x82, 0x81},
    80  		out: sqltypes.MakeTrusted(querypb.Type_UINT32,
    81  			[]byte(fmt.Sprintf("%v", 0x81828384))),
    82  	}, {
    83  		typ:  TypeLong,
    84  		styp: querypb.Type_INT32,
    85  		data: []byte{0xfc, 0xfd, 0xfe, 0xff},
    86  		out: sqltypes.MakeTrusted(querypb.Type_INT32,
    87  			[]byte(fmt.Sprintf("%v", -1-int32(0x00010203)))),
    88  	}, {
    89  		// 3.1415927E+00 = 0x40490fdb
    90  		typ:  TypeFloat,
    91  		data: []byte{0xdb, 0x0f, 0x49, 0x40},
    92  		out: sqltypes.MakeTrusted(querypb.Type_FLOAT32,
    93  			[]byte("3.1415927E+00")),
    94  	}, {
    95  		// 3.1415926535E+00 = 0x400921fb54411744
    96  		typ:  TypeDouble,
    97  		data: []byte{0x44, 0x17, 0x41, 0x54, 0xfb, 0x21, 0x09, 0x40},
    98  		out: sqltypes.MakeTrusted(querypb.Type_FLOAT64,
    99  			[]byte("3.1415926535E+00")),
   100  	}, {
   101  		// 0x58d137c5 = 1490106309 = 2017-03-21 14:25:09
   102  		typ:  TypeTimestamp,
   103  		data: []byte{0xc5, 0x37, 0xd1, 0x58},
   104  		out: sqltypes.MakeTrusted(querypb.Type_TIMESTAMP,
   105  			[]byte("2017-03-21 14:25:09")),
   106  	}, {
   107  		typ:  TypeLongLong,
   108  		styp: querypb.Type_UINT64,
   109  		data: []byte{0x88, 0x87, 0x86, 0x85, 0x84, 0x83, 0x82, 0x81},
   110  		out: sqltypes.MakeTrusted(querypb.Type_UINT64,
   111  			[]byte(fmt.Sprintf("%v", uint64(0x8182838485868788)))),
   112  	}, {
   113  		typ:  TypeLongLong,
   114  		styp: querypb.Type_INT64,
   115  		data: []byte{0xf8, 0xf9, 0xfa, 0xfb, 0xfc, 0xfd, 0xfe, 0xff},
   116  		out: sqltypes.MakeTrusted(querypb.Type_INT64,
   117  			[]byte(fmt.Sprintf("%v", -1-int64(0x0001020304050607)))),
   118  	}, {
   119  		typ: TypeDate,
   120  		// 2010 << 9 + 10 << 5 + 3 = 1029443 = 0x0fb543
   121  		data: []byte{0x43, 0xb5, 0x0f},
   122  		out: sqltypes.MakeTrusted(querypb.Type_DATE,
   123  			[]byte("2010-10-03")),
   124  	}, {
   125  		typ: TypeNewDate,
   126  		// 2010 << 9 + 10 << 5 + 3 = 1029443 = 0x0fb543
   127  		data: []byte{0x43, 0xb5, 0x0f},
   128  		out: sqltypes.MakeTrusted(querypb.Type_DATE,
   129  			[]byte("2010-10-03")),
   130  	}, {
   131  		typ: TypeTime,
   132  		// 154532 = 0x025ba4
   133  		data: []byte{0xa4, 0x5b, 0x02},
   134  		out: sqltypes.MakeTrusted(querypb.Type_TIME,
   135  			[]byte("15:45:32")),
   136  	}, {
   137  		typ: TypeDateTime,
   138  		// 19840304154532 = 0x120b6e4807a4
   139  		data: []byte{0xa4, 0x07, 0x48, 0x6e, 0x0b, 0x12, 0x00, 0x00},
   140  		out: sqltypes.MakeTrusted(querypb.Type_DATETIME,
   141  			[]byte("1984-03-04 15:45:32")),
   142  	}, {
   143  		typ:      TypeVarchar,
   144  		metadata: 20, // one byte length encoding
   145  		data:     []byte{3, 'a', 'b', 'c'},
   146  		out: sqltypes.MakeTrusted(querypb.Type_VARCHAR,
   147  			[]byte("abc")),
   148  	}, {
   149  		typ:      TypeVarchar,
   150  		metadata: 384, // two bytes length encoding
   151  		data:     []byte{3, 0, 'a', 'b', 'c'},
   152  		out: sqltypes.MakeTrusted(querypb.Type_VARCHAR,
   153  			[]byte("abc")),
   154  	}, {
   155  		typ:      TypeBit,
   156  		metadata: 0x0107,
   157  		data:     []byte{0x3, 0x1},
   158  		out: sqltypes.MakeTrusted(querypb.Type_BIT,
   159  			[]byte{3, 1}),
   160  	}, {
   161  		// 0x58d137c5 = 1490106309 = 2017-03-21 14:25:09
   162  		typ:      TypeTimestamp2,
   163  		metadata: 0,
   164  		data:     []byte{0x58, 0xd1, 0x37, 0xc5},
   165  		out: sqltypes.MakeTrusted(querypb.Type_TIMESTAMP,
   166  			[]byte("2017-03-21 14:25:09")),
   167  	}, {
   168  		typ:      TypeTimestamp2,
   169  		metadata: 1,
   170  		data:     []byte{0x58, 0xd1, 0x37, 0xc5, 70},
   171  		out: sqltypes.MakeTrusted(querypb.Type_TIMESTAMP,
   172  			[]byte("2017-03-21 14:25:09.7")),
   173  	}, {
   174  		typ:      TypeTimestamp2,
   175  		metadata: 2,
   176  		data:     []byte{0x58, 0xd1, 0x37, 0xc5, 76},
   177  		out: sqltypes.MakeTrusted(querypb.Type_TIMESTAMP,
   178  			[]byte("2017-03-21 14:25:09.76")),
   179  	}, {
   180  		typ:      TypeTimestamp2,
   181  		metadata: 3,
   182  		// 7650 = 0x1de2
   183  		data: []byte{0x58, 0xd1, 0x37, 0xc5, 0x1d, 0xe2},
   184  		out: sqltypes.MakeTrusted(querypb.Type_TIMESTAMP,
   185  			[]byte("2017-03-21 14:25:09.765")),
   186  	}, {
   187  		typ:      TypeTimestamp2,
   188  		metadata: 4,
   189  		// 7654 = 0x1de6
   190  		data: []byte{0x58, 0xd1, 0x37, 0xc5, 0x1d, 0xe6},
   191  		out: sqltypes.MakeTrusted(querypb.Type_TIMESTAMP,
   192  			[]byte("2017-03-21 14:25:09.7654")),
   193  	}, {
   194  		typ:      TypeTimestamp2,
   195  		metadata: 5,
   196  		// 76540 = 0x0badf6
   197  		data: []byte{0x58, 0xd1, 0x37, 0xc5, 0x0b, 0xad, 0xf6},
   198  		out: sqltypes.MakeTrusted(querypb.Type_TIMESTAMP,
   199  			[]byte("2017-03-21 14:25:09.76543")),
   200  	}, {
   201  		typ:      TypeTimestamp2,
   202  		metadata: 6,
   203  		// 765432 = 0x0badf8
   204  		data: []byte{0x58, 0xd1, 0x37, 0xc5, 0x0b, 0xad, 0xf8},
   205  		out: sqltypes.MakeTrusted(querypb.Type_TIMESTAMP,
   206  			[]byte("2017-03-21 14:25:09.765432")),
   207  	}, {
   208  		typ:      TypeDateTime2,
   209  		metadata: 0,
   210  		// (2012 * 13 + 6) << 22 + 21 << 17 + 15 << 12 + 45 << 6 + 17)
   211  		// = 109734198097 = 0x198caafb51
   212  		// Then have to add 0x8000000000 = 0x998caafb51
   213  		data: []byte{0x99, 0x8c, 0xaa, 0xfb, 0x51},
   214  		out: sqltypes.MakeTrusted(querypb.Type_DATETIME,
   215  			[]byte("2012-06-21 15:45:17")),
   216  	}, {
   217  		typ:      TypeDateTime2,
   218  		metadata: 1,
   219  		data:     []byte{0x99, 0x8c, 0xaa, 0xfb, 0x51, 70},
   220  		out: sqltypes.MakeTrusted(querypb.Type_DATETIME,
   221  			[]byte("2012-06-21 15:45:17.7")),
   222  	}, {
   223  		typ:      TypeDateTime2,
   224  		metadata: 2,
   225  		data:     []byte{0x99, 0x8c, 0xaa, 0xfb, 0x51, 76},
   226  		out: sqltypes.MakeTrusted(querypb.Type_DATETIME,
   227  			[]byte("2012-06-21 15:45:17.76")),
   228  	}, {
   229  		typ:      TypeDateTime2,
   230  		metadata: 3,
   231  		// 7650 = 0x1de2
   232  		data: []byte{0x99, 0x8c, 0xaa, 0xfb, 0x51, 0x1d, 0xe2},
   233  		out: sqltypes.MakeTrusted(querypb.Type_DATETIME,
   234  			[]byte("2012-06-21 15:45:17.765")),
   235  	}, {
   236  		typ:      TypeDateTime2,
   237  		metadata: 4,
   238  		// 7654 = 0x1de6
   239  		data: []byte{0x99, 0x8c, 0xaa, 0xfb, 0x51, 0x1d, 0xe6},
   240  		out: sqltypes.MakeTrusted(querypb.Type_DATETIME,
   241  			[]byte("2012-06-21 15:45:17.7654")),
   242  	}, {
   243  		typ:      TypeDateTime2,
   244  		metadata: 5,
   245  		// 765430 = 0x0badf6
   246  		data: []byte{0x99, 0x8c, 0xaa, 0xfb, 0x51, 0x0b, 0xad, 0xf6},
   247  		out: sqltypes.MakeTrusted(querypb.Type_DATETIME,
   248  			[]byte("2012-06-21 15:45:17.76543")),
   249  	}, {
   250  		typ:      TypeDateTime2,
   251  		metadata: 6,
   252  		// 765432 = 0x0badf8
   253  		data: []byte{0x99, 0x8c, 0xaa, 0xfb, 0x51, 0x0b, 0xad, 0xf8},
   254  		out: sqltypes.MakeTrusted(querypb.Type_DATETIME,
   255  			[]byte("2012-06-21 15:45:17.765432")),
   256  	}, {
   257  		// This first set of tests is from a comment in
   258  		//  sql-common/my_time.c:
   259  		//
   260  		// Disk value  intpart frac   Time value   Memory value
   261  		// 800000.00    0      0      00:00:00.00  0000000000.000000
   262  		// 7FFFFF.FF   -1      255   -00:00:00.01  FFFFFFFFFF.FFD8F0
   263  		// 7FFFFF.9D   -1      99    -00:00:00.99  FFFFFFFFFF.F0E4D0
   264  		// 7FFFFF.00   -1      0     -00:00:01.00  FFFFFFFFFF.000000
   265  		// 7FFFFE.FF   -1      255   -00:00:01.01  FFFFFFFFFE.FFD8F0
   266  		// 7FFFFE.F6   -2      246   -00:00:01.10  FFFFFFFFFE.FE7960
   267  		typ:      TypeTime2,
   268  		metadata: 2,
   269  		data:     []byte{0x80, 0x00, 0x00, 0x00},
   270  		out: sqltypes.MakeTrusted(querypb.Type_TIME,
   271  			[]byte("00:00:00.00")),
   272  	}, {
   273  		typ:      TypeTime2,
   274  		metadata: 2,
   275  		data:     []byte{0x7f, 0xff, 0xff, 0xff},
   276  		out: sqltypes.MakeTrusted(querypb.Type_TIME,
   277  			[]byte("-00:00:00.01")),
   278  	}, {
   279  		typ:      TypeTime2,
   280  		metadata: 2,
   281  		data:     []byte{0x7f, 0xff, 0xff, 0x9d},
   282  		out: sqltypes.MakeTrusted(querypb.Type_TIME,
   283  			[]byte("-00:00:00.99")),
   284  	}, {
   285  		typ:      TypeTime2,
   286  		metadata: 2,
   287  		data:     []byte{0x7f, 0xff, 0xff, 0x00},
   288  		out: sqltypes.MakeTrusted(querypb.Type_TIME,
   289  			[]byte("-00:00:01.00")),
   290  	}, {
   291  		typ:      TypeTime2,
   292  		metadata: 2,
   293  		data:     []byte{0x7f, 0xff, 0xfe, 0xff},
   294  		out: sqltypes.MakeTrusted(querypb.Type_TIME,
   295  			[]byte("-00:00:01.01")),
   296  	}, {
   297  		typ:      TypeTime2,
   298  		metadata: 2,
   299  		data:     []byte{0x7f, 0xff, 0xfe, 0xf6},
   300  		out: sqltypes.MakeTrusted(querypb.Type_TIME,
   301  			[]byte("-00:00:01.10")),
   302  	}, {
   303  		// Similar tests for 4 decimals.
   304  		typ:      TypeTime2,
   305  		metadata: 4,
   306  		data:     []byte{0x80, 0x00, 0x00, 0x00, 0x00},
   307  		out: sqltypes.MakeTrusted(querypb.Type_TIME,
   308  			[]byte("00:00:00.0000")),
   309  	}, {
   310  		typ:      TypeTime2,
   311  		metadata: 4,
   312  		data:     []byte{0x7f, 0xff, 0xff, 0xff, 0xff},
   313  		out: sqltypes.MakeTrusted(querypb.Type_TIME,
   314  			[]byte("-00:00:00.0001")),
   315  	}, {
   316  		typ:      TypeTime2,
   317  		metadata: 4,
   318  		data:     []byte{0x7f, 0xff, 0xff, 0xff, 0x9d},
   319  		out: sqltypes.MakeTrusted(querypb.Type_TIME,
   320  			[]byte("-00:00:00.0099")),
   321  	}, {
   322  		typ:      TypeTime2,
   323  		metadata: 4,
   324  		data:     []byte{0x7f, 0xff, 0xff, 0x00, 0x00},
   325  		out: sqltypes.MakeTrusted(querypb.Type_TIME,
   326  			[]byte("-00:00:01.0000")),
   327  	}, {
   328  		typ:      TypeTime2,
   329  		metadata: 4,
   330  		data:     []byte{0x7f, 0xff, 0xfe, 0xff, 0xff},
   331  		out: sqltypes.MakeTrusted(querypb.Type_TIME,
   332  			[]byte("-00:00:01.0001")),
   333  	}, {
   334  		typ:      TypeTime2,
   335  		metadata: 4,
   336  		data:     []byte{0x7f, 0xff, 0xfe, 0xff, 0xf6},
   337  		out: sqltypes.MakeTrusted(querypb.Type_TIME,
   338  			[]byte("-00:00:01.0010")),
   339  	}, {
   340  		// Similar tests for 6 decimals.
   341  		typ:      TypeTime2,
   342  		metadata: 6,
   343  		data:     []byte{0x80, 0x00, 0x00, 0x00, 0x00, 0x00},
   344  		out: sqltypes.MakeTrusted(querypb.Type_TIME,
   345  			[]byte("00:00:00.000000")),
   346  	}, {
   347  		typ:      TypeTime2,
   348  		metadata: 6,
   349  		data:     []byte{0x7f, 0xff, 0xff, 0xff, 0xff, 0xff},
   350  		out: sqltypes.MakeTrusted(querypb.Type_TIME,
   351  			[]byte("-00:00:00.000001")),
   352  	}, {
   353  		typ:      TypeTime2,
   354  		metadata: 6,
   355  		data:     []byte{0x7f, 0xff, 0xff, 0xff, 0xff, 0x9d},
   356  		out: sqltypes.MakeTrusted(querypb.Type_TIME,
   357  			[]byte("-00:00:00.000099")),
   358  	}, {
   359  		typ:      TypeTime2,
   360  		metadata: 6,
   361  		data:     []byte{0x7f, 0xff, 0xff, 0x00, 0x00, 0x00},
   362  		out: sqltypes.MakeTrusted(querypb.Type_TIME,
   363  			[]byte("-00:00:01.000000")),
   364  	}, {
   365  		typ:      TypeTime2,
   366  		metadata: 6,
   367  		data:     []byte{0x7f, 0xff, 0xfe, 0xff, 0xff, 0xff},
   368  		out: sqltypes.MakeTrusted(querypb.Type_TIME,
   369  			[]byte("-00:00:01.000001")),
   370  	}, {
   371  		typ:      TypeTime2,
   372  		metadata: 6,
   373  		data:     []byte{0x7f, 0xff, 0xfe, 0xff, 0xff, 0xf6},
   374  		out: sqltypes.MakeTrusted(querypb.Type_TIME,
   375  			[]byte("-00:00:01.000010")),
   376  	}, {
   377  		// Few more tests.
   378  		typ:      TypeTime2,
   379  		metadata: 0,
   380  		data:     []byte{0x80, 0x00, 0x00},
   381  		out: sqltypes.MakeTrusted(querypb.Type_TIME,
   382  			[]byte("00:00:00")),
   383  	}, {
   384  		typ:      TypeTime2,
   385  		metadata: 1,
   386  		data:     []byte{0x80, 0x00, 0x01, 0x0a},
   387  		out: sqltypes.MakeTrusted(querypb.Type_TIME,
   388  			[]byte("00:00:01.1")),
   389  	}, {
   390  		typ:      TypeTime2,
   391  		metadata: 2,
   392  		data:     []byte{0x80, 0x00, 0x01, 0x0a},
   393  		out: sqltypes.MakeTrusted(querypb.Type_TIME,
   394  			[]byte("00:00:01.10")),
   395  	}, {
   396  		typ:      TypeTime2,
   397  		metadata: 0,
   398  		// 15 << 12 + 34 << 6 + 54 = 63670 = 0x00f8b6
   399  		// and need to add 0x800000
   400  		data: []byte{0x80, 0xf8, 0xb6},
   401  		out: sqltypes.MakeTrusted(querypb.Type_TIME,
   402  			[]byte("15:34:54")),
   403  	}, {
   404  		typ:      TypeJSON,
   405  		metadata: 2,
   406  		data: []byte{0x0f, 0x00,
   407  			0, 1, 0, 14, 0, 11, 0, 1, 0, 12, 12, 0, 97, 1, 98},
   408  		out: sqltypes.MakeTrusted(sqltypes.Expression,
   409  			[]byte(`{"a":"b"}`)),
   410  	}, {
   411  		typ:      TypeJSON,
   412  		metadata: 4,
   413  		data: []byte{0x0f, 0x00, 0x00, 0x00,
   414  			0, 1, 0, 14, 0, 11, 0, 1, 0, 12, 12, 0, 97, 1, 98},
   415  		out: sqltypes.MakeTrusted(sqltypes.Expression,
   416  			[]byte(`{"a":"b"}`)),
   417  	}, {
   418  		typ:      TypeEnum,
   419  		metadata: 1,
   420  		data:     []byte{0x03},
   421  		out: sqltypes.MakeTrusted(querypb.Type_ENUM,
   422  			[]byte("3")),
   423  	}, {
   424  		typ:      TypeEnum,
   425  		metadata: 2,
   426  		data:     []byte{0x01, 0x02},
   427  		out: sqltypes.MakeTrusted(querypb.Type_ENUM,
   428  			[]byte(fmt.Sprintf("%v", 0x0201))),
   429  	}, {
   430  		typ:      TypeSet,
   431  		metadata: 2,
   432  		data:     []byte{0x01, 0x02},
   433  		out: sqltypes.MakeTrusted(querypb.Type_SET,
   434  			[]byte{0x01, 0x02}),
   435  	}, {
   436  		typ:      TypeString,
   437  		metadata: TypeString<<8 | 5, // maximum length = 5
   438  		data:     []byte{0x04, 0x01, 0x02, 0x03, 0x04},
   439  		out: sqltypes.MakeTrusted(querypb.Type_VARCHAR,
   440  			[]byte{0x01, 0x02, 0x03, 0x04}),
   441  	}, {
   442  		// Length is encoded in 10 bits, 2 of them are in a weird place.
   443  		// In this test, we set the two high bits.
   444  		// 773 = 512 + 256 + 5
   445  		// This requires 2 bytes to store the length.
   446  		typ:      TypeString,
   447  		metadata: (TypeString<<8 ^ 0x3000) | 5, // maximum length = 773
   448  		data:     []byte{0x04, 0x00, 0x01, 0x02, 0x03, 0x04},
   449  		out: sqltypes.MakeTrusted(querypb.Type_VARCHAR,
   450  			[]byte{0x01, 0x02, 0x03, 0x04}),
   451  	}, {
   452  		// See strings/decimal.c function decimal2bin for why these
   453  		// values are here.
   454  		typ:      TypeNewDecimal,
   455  		metadata: 14<<8 | 4,
   456  		data:     []byte{0x81, 0x0D, 0xFB, 0x38, 0xD2, 0x04, 0xD2},
   457  		out: sqltypes.MakeTrusted(querypb.Type_DECIMAL,
   458  			[]byte("1234567890.1234")),
   459  	}, {
   460  		typ:      TypeNewDecimal,
   461  		metadata: 14<<8 | 4,
   462  		data:     []byte{0x7E, 0xF2, 0x04, 0xC7, 0x2D, 0xFB, 0x2D},
   463  		out: sqltypes.MakeTrusted(querypb.Type_DECIMAL,
   464  			[]byte("-1234567890.1234")),
   465  	}, {
   466  		typ:      TypeNewDecimal,
   467  		metadata: 14<<8 | 4,
   468  		data:     []byte{0x81, 0x0D, 0xFB, 0x38, 0xD2, 0x00, 0x01},
   469  		out: sqltypes.MakeTrusted(querypb.Type_DECIMAL,
   470  			[]byte("1234567890.0001")),
   471  	}, {
   472  		typ:      TypeNewDecimal,
   473  		metadata: 20<<8 | 2, // DECIMAL(20,2)
   474  		data:     []byte{0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0, 0x01, 0x0a},
   475  		out: sqltypes.MakeTrusted(querypb.Type_DECIMAL,
   476  			[]byte("1.10")),
   477  	}, {
   478  		typ:      TypeBlob,
   479  		metadata: 1,
   480  		data:     []byte{0x3, 'a', 'b', 'c'},
   481  		out: sqltypes.MakeTrusted(querypb.Type_VARBINARY,
   482  			[]byte("abc")),
   483  	}, {
   484  		typ:      TypeBlob,
   485  		metadata: 2,
   486  		data:     []byte{0x3, 0x00, 'a', 'b', 'c'},
   487  		out: sqltypes.MakeTrusted(querypb.Type_VARBINARY,
   488  			[]byte("abc")),
   489  	}, {
   490  		typ:      TypeBlob,
   491  		metadata: 3,
   492  		data:     []byte{0x3, 0x00, 0x00, 'a', 'b', 'c'},
   493  		out: sqltypes.MakeTrusted(querypb.Type_VARBINARY,
   494  			[]byte("abc")),
   495  	}, {
   496  		typ:      TypeBlob,
   497  		metadata: 4,
   498  		data:     []byte{0x3, 0x00, 0x00, 0x00, 'a', 'b', 'c'},
   499  		out: sqltypes.MakeTrusted(querypb.Type_VARBINARY,
   500  			[]byte("abc")),
   501  	}, {
   502  		typ:      TypeVarString,
   503  		metadata: 20, // one byte length encoding
   504  		data:     []byte{3, 'a', 'b', 'c'},
   505  		out: sqltypes.MakeTrusted(querypb.Type_VARCHAR,
   506  			[]byte("abc")),
   507  	}, {
   508  		typ:      TypeVarString,
   509  		metadata: 384, // two bytes length encoding
   510  		data:     []byte{3, 0, 'a', 'b', 'c'},
   511  		out: sqltypes.MakeTrusted(querypb.Type_VARCHAR,
   512  			[]byte("abc")),
   513  	}, {
   514  		typ:      TypeGeometry,
   515  		metadata: 1,
   516  		data:     []byte{0x3, 'a', 'b', 'c'},
   517  		out: sqltypes.MakeTrusted(querypb.Type_GEOMETRY,
   518  			[]byte("abc")),
   519  	}, {
   520  		typ:      TypeGeometry,
   521  		metadata: 2,
   522  		data:     []byte{0x3, 0x00, 'a', 'b', 'c'},
   523  		out: sqltypes.MakeTrusted(querypb.Type_GEOMETRY,
   524  			[]byte("abc")),
   525  	}, {
   526  		typ:      TypeGeometry,
   527  		metadata: 3,
   528  		data:     []byte{0x3, 0x00, 0x00, 'a', 'b', 'c'},
   529  		out: sqltypes.MakeTrusted(querypb.Type_GEOMETRY,
   530  			[]byte("abc")),
   531  	}, {
   532  		typ:      TypeGeometry,
   533  		metadata: 4,
   534  		data:     []byte{0x3, 0x00, 0x00, 0x00, 'a', 'b', 'c'},
   535  		out: sqltypes.MakeTrusted(querypb.Type_GEOMETRY,
   536  			[]byte("abc")),
   537  	}}
   538  
   539  	for _, tcase := range testcases {
   540  		// Copy the data into a larger buffer (one extra byte
   541  		// on both sides), so we make sure the 'pos' field works.
   542  		padded := make([]byte, len(tcase.data)+2)
   543  		copy(padded[1:], tcase.data)
   544  
   545  		// Test cellLength.
   546  		l, err := cellLength(padded, 1, tcase.typ, tcase.metadata)
   547  		if err != nil || l != len(tcase.data) {
   548  			t.Errorf("testcase cellLength(%v,%v) returned unexpected result: %v %v was expected %v <nil>",
   549  				tcase.typ, tcase.data, l, err, len(tcase.data))
   550  		}
   551  
   552  		// Test CellValue.
   553  		out, l, err := CellValue(padded, 1, tcase.typ, tcase.metadata, &querypb.Field{Type: tcase.styp})
   554  		if err != nil || l != len(tcase.data) || out.Type() != tcase.out.Type() || !bytes.Equal(out.Raw(), tcase.out.Raw()) {
   555  			t.Errorf("testcase cellData(%v,%v) returned unexpected result: %v %v %v, was expecting %v %v <nil>",
   556  				tcase.typ, tcase.data, out, l, err, tcase.out, len(tcase.data))
   557  		}
   558  	}
   559  }