-
Notifications
You must be signed in to change notification settings - Fork 5.9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
types: convert string to MySQL BIT correctly #21310
Changes from 3 commits
ce10ab7
b31bb02
77375fd
05bd9ea
1683cef
b61e5c8
675933c
48fabcf
6d79fb0
caa552f
7e4fa93
f9f7a59
80c38c3
2c9ced7
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -1423,13 +1423,38 @@ func (d *Datum) convertToMysqlFloatYear(sc *stmtctx.StatementContext, target *Fi | |
return ret, err | ||
} | ||
|
||
func (d *Datum) convertStringToMysqlBit(sc *stmtctx.StatementContext) (uint64, error) { | ||
bitStr, err := ParseBitStr(BinaryLiteral(d.b).ToString()) | ||
if err != nil { | ||
// This is to be compatible with the previous processing method. | ||
return BinaryLiteral(d.b).ToInt(sc) | ||
} | ||
return bitStr.ToInt(sc) | ||
} | ||
|
||
func (d *Datum) convertToMysqlBit(sc *stmtctx.StatementContext, target *FieldType) (Datum, error) { | ||
var ret Datum | ||
var uintValue uint64 | ||
var err error | ||
switch d.k { | ||
case KindString, KindBytes: | ||
case KindBytes: | ||
uintValue, err = BinaryLiteral(d.b).ToInt(sc) | ||
case KindString: | ||
// To solve issue #18681, we need consider true, false, 0, 1. | ||
// More situations, we also consider b'0', b'1' and so on. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I guess this fix is not specific to 18681, and the code itself is self-explaining, if you want to add comment, it is better to find if there is a reference to MySQL's doc. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There is no mention of related behavior in the MySQL documentation, and I think there is nothing wrong with this comment There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What I am saying is that this comment is not comprehensive enough if not redundant, you don't fix code because of an issue report, you fix code because you want proper behavior, that's why I am asking for a doc reference. I am not against referencing to an issue here, but I would like a comment like this:
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Updated. |
||
s := BinaryLiteral(d.b).ToString() | ||
if target.Flen == 1 { | ||
switch strings.ToLower(s) { | ||
case "true", "1": | ||
uintValue = 1 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. In mysql 8.0: mysql > select true, b'1', cast(true as binary);
+------+------------+--------------------------------------------+
| true | b'1' | cast(true as binary) |
+------+------------+--------------------------------------------+
| 1 | 0x01 | 0x31 |
+------+------------+--------------------------------------------+
1 row in set (0.00 sec) Maybe we should handle it in another place. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This sentence of SQL has nothing to do with convert. It can be seen from the following that these values can be converted correctly, but they are returned as binary, so the output to MySQL Client may not be displayed. MySQL [test]> select true, HEX(b'1'), HEX(cast(true as binary));
+------+-----------+---------------------------+
| TRUE | HEX(b'1') | HEX(cast(true as binary)) |
+------+-----------+---------------------------+
| 1 | 01 | 31 |
+------+-----------+---------------------------+
1 row in set (0.001 sec)
MySQL [test]> select true, b'1', cast(true as binary);
+------+------+----------------------+
| TRUE | b'1' | cast(true as binary) |
+------+------+----------------------+
| 1 | | 1 |
+------+------+----------------------+
1 row in set (0.001 sec) I think these are two different problems, and it's best to solve it in another PR |
||
case "false", "0": | ||
uintValue = 0 | ||
default: | ||
uintValue, err = d.convertStringToMysqlBit(sc) | ||
} | ||
} else { | ||
uintValue, err = d.convertStringToMysqlBit(sc) | ||
} | ||
case KindInt64: | ||
// if input kind is int64 (signed), when trans to bit, we need to treat it as unsigned | ||
d.k = KindUint64 | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
please be more precise with this comment, because once the PR get merged, no one will know what the "previous processing method" is.