@@ -442,9 +442,10 @@ func (h *Handler) doQuery(
442442 var r * sqltypes.Result
443443 var processedAtLeastOneBatch bool
444444
445- buf := sql .SingletonBuf
445+ buf := sql .ByteBufPool . Get ().( * sql. ByteBuffer )
446446 defer func () {
447- sql .SingletonBuf .Reset ()
447+ buf .Reset ()
448+ sql .ByteBufPool .Put (buf )
448449 }()
449450
450451 // zero/single return schema use spooling shortcut
@@ -930,28 +931,35 @@ func updateMaxUsedConnectionsStatusVariable() {
930931 }()
931932}
932933
934+ func toSqlHelper (ctx * sql.Context , typ sql.Type , buf * sql.ByteBuffer , val interface {}) (sqltypes.Value , error ) {
935+ if buf == nil {
936+ return typ .SQL (ctx , buf .Get (), val )
937+ }
938+ spare := buf .Spare ()
939+ ret , err := typ .SQL (ctx , buf .Get (), val )
940+ if ret .Len () > spare {
941+ buf .Double ()
942+ } else {
943+ buf .Advance (ret .Len ())
944+ }
945+ return ret , err
946+ }
947+
933948func RowToSQL (ctx * sql.Context , sch sql.Schema , row sql.Row , projs []sql.Expression , buf * sql.ByteBuffer ) ([]sqltypes.Value , error ) {
934949 // need to make sure the schema is not null as some plan schema is defined as null (e.g. IfElseBlock)
935950 if len (sch ) == 0 {
936951 return []sqltypes.Value {}, nil
937952 }
938953
939954 outVals := make ([]sqltypes.Value , len (sch ))
955+ var err error
940956 if len (projs ) == 0 {
941957 for i , col := range sch {
942958 if row [i ] == nil {
943959 outVals [i ] = sqltypes .NULL
944960 continue
945961 }
946- var err error
947- spare := buf .Spare ()
948- outVals [i ], err = col .Type .SQL (ctx , buf .Get (), row [i ])
949- if outVals [i ].Len () > spare {
950- buf .Double ()
951- } else {
952- buf .Advance (outVals [i ].Len ())
953- }
954-
962+ outVals [i ], err = toSqlHelper (ctx , col .Type , buf , row [i ])
955963 if err != nil {
956964 return nil , err
957965 }
@@ -968,13 +976,7 @@ func RowToSQL(ctx *sql.Context, sch sql.Schema, row sql.Row, projs []sql.Express
968976 outVals [i ] = sqltypes .NULL
969977 continue
970978 }
971- spare := buf .Spare ()
972- outVals [i ], err = col .Type .SQL (ctx , buf .Get (), field )
973- if outVals [i ].Len () > spare {
974- buf .Double ()
975- } else {
976- buf .Advance (outVals [i ].Len ())
977- }
979+ outVals [i ], err = toSqlHelper (ctx , col .Type , buf , field )
978980 if err != nil {
979981 return nil , err
980982 }
0 commit comments