|
| 1 | +// Copyright 2015 PingCAP, 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 | +// Copyright 2013 The Go-MySQL-Driver Authors. All rights reserved. |
| 16 | +// |
| 17 | +// This Source Code Form is subject to the terms of the Mozilla Public |
| 18 | +// License, v. 2.0. If a copy of the MPL was not distributed with this file, |
| 19 | +// You can obtain one at http://mozilla.org/MPL/2.0/. |
| 20 | + |
| 21 | +// The MIT License (MIT) |
| 22 | +// |
| 23 | +// Copyright (c) 2014 wandoulabs |
| 24 | +// Copyright (c) 2014 siddontang |
| 25 | +// |
| 26 | +// Permission is hereby granted, free of charge, to any person obtaining a copy of |
| 27 | +// this software and associated documentation files (the "Software"), to deal in |
| 28 | +// the Software without restriction, including without limitation the rights to |
| 29 | +// use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of |
| 30 | +// the Software, and to permit persons to whom the Software is furnished to do so, |
| 31 | +// subject to the following conditions: |
| 32 | +// |
| 33 | +// The above copyright notice and this permission notice shall be included in all |
| 34 | +// copies or substantial portions of the Software. |
| 35 | + |
| 36 | +package column |
| 37 | + |
| 38 | +import ( |
| 39 | + "bytes" |
| 40 | + |
| 41 | + "github.com/pingcap/tidb/pkg/parser/charset" |
| 42 | + "github.com/pingcap/tidb/pkg/parser/mysql" |
| 43 | + "github.com/pingcap/tidb/pkg/util/logutil" |
| 44 | + "go.uber.org/zap" |
| 45 | +) |
| 46 | + |
| 47 | +// ResultEncoder encodes a column value to a byte slice. |
| 48 | +type ResultEncoder struct { |
| 49 | + encoding charset.Encoding |
| 50 | + |
| 51 | + // dataEncoding can be updated to match the column data charset. |
| 52 | + dataEncoding charset.Encoding |
| 53 | + |
| 54 | + buffer *bytes.Buffer |
| 55 | + |
| 56 | + // chsName and encoding are unchanged after the initialization from |
| 57 | + // session variable @@character_set_results. |
| 58 | + chsName string |
| 59 | + |
| 60 | + isBinary bool |
| 61 | + isNull bool |
| 62 | + dataIsBinary bool |
| 63 | +} |
| 64 | + |
| 65 | +// NewResultEncoder creates a new ResultEncoder. |
| 66 | +func NewResultEncoder(chs string) *ResultEncoder { |
| 67 | + return &ResultEncoder{ |
| 68 | + chsName: chs, |
| 69 | + encoding: charset.FindEncodingTakeUTF8AsNoop(chs), |
| 70 | + buffer: &bytes.Buffer{}, |
| 71 | + isBinary: chs == charset.CharsetBin, |
| 72 | + isNull: len(chs) == 0, |
| 73 | + } |
| 74 | +} |
| 75 | + |
| 76 | +// Clean prevent the ResultEncoder from holding too much memory. |
| 77 | +func (d *ResultEncoder) Clean() { |
| 78 | + d.buffer = nil |
| 79 | +} |
| 80 | + |
| 81 | +// UpdateDataEncoding updates the data encoding. |
| 82 | +func (d *ResultEncoder) UpdateDataEncoding(chsID uint16) { |
| 83 | + chs, _, err := charset.GetCharsetInfoByID(int(chsID)) |
| 84 | + if err != nil { |
| 85 | + logutil.BgLogger().Warn("unknown charset ID", zap.Error(err)) |
| 86 | + } |
| 87 | + d.dataEncoding = charset.FindEncodingTakeUTF8AsNoop(chs) |
| 88 | + d.dataIsBinary = chsID == mysql.BinaryDefaultCollationID |
| 89 | +} |
| 90 | + |
| 91 | +// ColumnTypeInfoCharsetID returns the charset ID for the column type info. |
| 92 | +func (d *ResultEncoder) ColumnTypeInfoCharsetID(info *Info) uint16 { |
| 93 | + // Only replace the charset when @@character_set_results is valid and |
| 94 | + // the target column is a non-binary string. |
| 95 | + charset := info.dumpCharset() |
| 96 | + if d.isNull || len(d.chsName) == 0 || !isStringColumnType(info.Type) { |
| 97 | + return charset |
| 98 | + } |
| 99 | + if charset == mysql.BinaryDefaultCollationID { |
| 100 | + return mysql.BinaryDefaultCollationID |
| 101 | + } |
| 102 | + return uint16(mysql.CharsetNameToID(d.chsName)) |
| 103 | +} |
| 104 | + |
| 105 | +// EncodeMeta encodes bytes for meta info like column names. |
| 106 | +// Note that the result should be consumed immediately. |
| 107 | +func (d *ResultEncoder) EncodeMeta(src []byte) []byte { |
| 108 | + return d.EncodeWith(src, d.encoding) |
| 109 | +} |
| 110 | + |
| 111 | +// EncodeData encodes bytes for row data. |
| 112 | +// Note that the result should be consumed immediately. |
| 113 | +func (d *ResultEncoder) EncodeData(src []byte) []byte { |
| 114 | + // For the following cases, TiDB encodes the results with column charset |
| 115 | + // instead of @@character_set_results: |
| 116 | + // - @@character_set_result = null. |
| 117 | + // - @@character_set_result = binary. |
| 118 | + // - The column is binary type like blob, binary char/varchar. |
| 119 | + if d.isNull || d.isBinary || d.dataIsBinary { |
| 120 | + // Use the column charset to encode. |
| 121 | + return d.EncodeWith(src, d.dataEncoding) |
| 122 | + } |
| 123 | + return d.EncodeWith(src, d.encoding) |
| 124 | +} |
| 125 | + |
| 126 | +// EncodeWith encodes bytes with the given encoding. |
| 127 | +func (d *ResultEncoder) EncodeWith(src []byte, enc charset.Encoding) []byte { |
| 128 | + data, err := enc.Transform(d.buffer, src, charset.OpEncodeReplace) |
| 129 | + if err != nil { |
| 130 | + logutil.BgLogger().Debug("encode error", zap.Error(err)) |
| 131 | + } |
| 132 | + return data |
| 133 | +} |
| 134 | + |
| 135 | +func isStringColumnType(tp byte) bool { |
| 136 | + switch tp { |
| 137 | + case mysql.TypeString, mysql.TypeVarString, mysql.TypeVarchar, mysql.TypeBit, |
| 138 | + mysql.TypeTinyBlob, mysql.TypeMediumBlob, mysql.TypeLongBlob, mysql.TypeBlob, |
| 139 | + mysql.TypeEnum, mysql.TypeSet, mysql.TypeJSON: |
| 140 | + return true |
| 141 | + case mysql.TypeTiDBVectorFloat32: |
| 142 | + // When passing Vector column to the SQL Client, pretend to be a non-binary String. |
| 143 | + return true |
| 144 | + } |
| 145 | + return false |
| 146 | +} |
0 commit comments