-
Notifications
You must be signed in to change notification settings - Fork 2.3k
Fix type comparisons for Nullsafe* functions #13605
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
Changes from all commits
b0c32a7
a736f83
9f6c613
9aab43f
07a777f
7a366ee
0e8661b
f2b110a
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 | ||||
|---|---|---|---|---|---|---|
| @@ -0,0 +1,169 @@ | ||||||
| /* | ||||||
| Copyright 2023 The Vitess Authors. | ||||||
|
|
||||||
| Licensed under the Apache License, Version 2.0 (the "License"); | ||||||
| you may not use this file except in compliance with the License. | ||||||
| You may obtain a copy of the License at | ||||||
|
|
||||||
| http://www.apache.org/licenses/LICENSE-2.0 | ||||||
|
|
||||||
| Unless required by applicable law or agreed to in writing, software | ||||||
| distributed under the License is distributed on an "AS IS" BASIS, | ||||||
| WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||||||
| See the License for the specific language governing permissions and | ||||||
| limitations under the License. | ||||||
| */ | ||||||
|
|
||||||
| package json | ||||||
|
|
||||||
| import ( | ||||||
| "encoding/binary" | ||||||
| "strings" | ||||||
|
|
||||||
| "vitess.io/vitess/go/hack" | ||||||
| "vitess.io/vitess/go/mysql/fastparse" | ||||||
| ) | ||||||
|
|
||||||
| const ( | ||||||
| JSON_KEY_NULL = '\x00' | ||||||
| JSON_KEY_NUMBER_NEG = '\x01' | ||||||
| JSON_KEY_NUMBER_ZERO = '\x02' | ||||||
| JSON_KEY_NUMBER_POS = '\x03' | ||||||
| JSON_KEY_STRING = '\x04' | ||||||
| JSON_KEY_OBJECT = '\x05' | ||||||
| JSON_KEY_ARRAY = '\x06' | ||||||
| JSON_KEY_FALSE = '\x07' | ||||||
| JSON_KEY_TRUE = '\x08' | ||||||
| JSON_KEY_DATE = '\x09' | ||||||
| JSON_KEY_TIME = '\x0A' | ||||||
| JSON_KEY_DATETIME = '\x0B' | ||||||
| JSON_KEY_OPAQUE = '\x0C' | ||||||
| ) | ||||||
|
|
||||||
| // numericWeightString generates a fixed-width weight string for any JSON | ||||||
| // number. It requires the `num` representation to be normalized, otherwise | ||||||
| // the resulting string will not sort. | ||||||
| func (v *Value) numericWeightString(dst []byte, num string) []byte { | ||||||
| const MaxPadLength = 30 | ||||||
|
Member
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. nit:
Suggested change
|
||||||
|
|
||||||
| var ( | ||||||
| exponent string | ||||||
| exp int64 | ||||||
| significant string | ||||||
| negative bool | ||||||
| original = len(dst) | ||||||
| ) | ||||||
|
|
||||||
| if num[0] == '-' { | ||||||
| negative = true | ||||||
| num = num[1:] | ||||||
| } | ||||||
|
|
||||||
| if i := strings.IndexByte(num, 'e'); i >= 0 { | ||||||
| exponent = num[i+1:] | ||||||
| num = num[:i] | ||||||
| } | ||||||
|
|
||||||
| significant = num | ||||||
| for len(significant) > 0 { | ||||||
| if significant[0] >= '1' && significant[0] <= '9' { | ||||||
| break | ||||||
| } | ||||||
| significant = significant[1:] | ||||||
| } | ||||||
| if len(significant) == 0 { | ||||||
| return append(dst, JSON_KEY_NUMBER_ZERO) | ||||||
| } | ||||||
|
|
||||||
| if len(exponent) > 0 { | ||||||
| exp, _ = fastparse.ParseInt64(exponent, 10) | ||||||
| } else { | ||||||
| dec := strings.IndexByte(num, '.') | ||||||
| ofs := len(num) - len(significant) | ||||||
| if dec < 0 { | ||||||
| exp = int64(len(significant) - 1) | ||||||
| } else if ofs < dec { | ||||||
| exp = int64(dec - ofs - 1) | ||||||
| } else { | ||||||
| exp = int64(dec - ofs) | ||||||
| } | ||||||
| } | ||||||
|
|
||||||
| if negative { | ||||||
| dst = append(dst, JSON_KEY_NUMBER_NEG) | ||||||
| dst = binary.BigEndian.AppendUint16(dst, uint16(-exp)^(1<<15)) | ||||||
|
|
||||||
| for _, ch := range []byte(significant) { | ||||||
| if ch >= '0' && ch <= '9' { | ||||||
| dst = append(dst, '9'-ch+'0') | ||||||
| } | ||||||
| } | ||||||
| for len(dst)-original < MaxPadLength { | ||||||
| dst = append(dst, '9') | ||||||
| } | ||||||
| } else { | ||||||
| dst = append(dst, JSON_KEY_NUMBER_POS) | ||||||
| dst = binary.BigEndian.AppendUint16(dst, uint16(exp)^(1<<15)) | ||||||
|
|
||||||
| for _, ch := range []byte(significant) { | ||||||
| if ch >= '0' && ch <= '9' { | ||||||
| dst = append(dst, ch) | ||||||
| } | ||||||
| } | ||||||
| for len(dst)-original < MaxPadLength { | ||||||
| dst = append(dst, '0') | ||||||
| } | ||||||
| } | ||||||
|
|
||||||
| return dst | ||||||
| } | ||||||
|
|
||||||
| func (v *Value) WeightString(dst []byte) []byte { | ||||||
| switch v.Type() { | ||||||
| case TypeNull: | ||||||
| dst = append(dst, JSON_KEY_NULL) | ||||||
| case TypeNumber: | ||||||
| if v.NumberType() == NumberTypeFloat { | ||||||
| f := v.marshalFloat(nil) | ||||||
| dst = v.numericWeightString(dst, hack.String(f)) | ||||||
| } else { | ||||||
| dst = v.numericWeightString(dst, v.s) | ||||||
| } | ||||||
| case TypeString: | ||||||
| dst = append(dst, JSON_KEY_STRING) | ||||||
| dst = append(dst, v.s...) | ||||||
| case TypeObject: | ||||||
| // MySQL compat: we follow the same behavior as MySQL does for weight strings in JSON, | ||||||
| // where Objects and Arrays are only sorted by their length and not by the values | ||||||
| // of their contents. | ||||||
| // Note that in MySQL, generating the weight string of a JSON Object or Array will actually | ||||||
| // print a warning in the logs! We're not printing anything. | ||||||
| dst = append(dst, JSON_KEY_OBJECT) | ||||||
| dst = binary.BigEndian.AppendUint32(dst, uint32(v.o.Len())) | ||||||
| case TypeArray: | ||||||
| dst = append(dst, JSON_KEY_ARRAY) | ||||||
| dst = binary.BigEndian.AppendUint32(dst, uint32(len(v.a))) | ||||||
|
Comment on lines
+135
to
+145
Member
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. Why do we always use BigEndian here not also Little Endian?
Member
Author
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. It has to be consistent across everything here. And doing it in big endian ensures that it's correctly byte ordered. |
||||||
| case TypeBoolean: | ||||||
| switch v { | ||||||
| case ValueTrue: | ||||||
| dst = append(dst, JSON_KEY_TRUE) | ||||||
| case ValueFalse: | ||||||
| dst = append(dst, JSON_KEY_FALSE) | ||||||
| default: | ||||||
| panic("invalid JSON Boolean") | ||||||
| } | ||||||
| case TypeDate: | ||||||
| dst = append(dst, JSON_KEY_DATE) | ||||||
| dst = append(dst, v.MarshalDate()...) | ||||||
| case TypeDateTime: | ||||||
| dst = append(dst, JSON_KEY_DATETIME) | ||||||
| dst = append(dst, v.MarshalDateTime()...) | ||||||
| case TypeTime: | ||||||
| dst = append(dst, JSON_KEY_TIME) | ||||||
| dst = append(dst, v.MarshalTime()...) | ||||||
| case TypeOpaque, TypeBit, TypeBlob: | ||||||
| dst = append(dst, JSON_KEY_OPAQUE) | ||||||
| dst = append(dst, v.s...) | ||||||
| } | ||||||
| return dst | ||||||
| } | ||||||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,44 @@ | ||
| /* | ||
| Copyright 2023 The Vitess Authors. | ||
|
|
||
| Licensed under the Apache License, Version 2.0 (the "License"); | ||
| you may not use this file except in compliance with the License. | ||
| You may obtain a copy of the License at | ||
|
|
||
| http://www.apache.org/licenses/LICENSE-2.0 | ||
|
|
||
| Unless required by applicable law or agreed to in writing, software | ||
| distributed under the License is distributed on an "AS IS" BASIS, | ||
| WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| See the License for the specific language governing permissions and | ||
| limitations under the License. | ||
| */ | ||
|
|
||
| package json | ||
dbussink marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| import ( | ||
| "bytes" | ||
| "testing" | ||
|
|
||
| "vitess.io/vitess/go/mysql/format" | ||
| ) | ||
|
|
||
| func TestWeightStrings(t *testing.T) { | ||
| var cases = []struct { | ||
| l, r *Value | ||
| }{ | ||
| {NewNumber("-2.3742940301417033", NumberTypeFloat), NewNumber("-0.024384053736998118", NumberTypeFloat)}, | ||
| {NewNumber("2.3742940301417033", NumberTypeFloat), NewNumber("20.3742940301417033", NumberTypeFloat)}, | ||
| {NewNumber(string(format.FormatFloat(1000000000000000.0)), NumberTypeFloat), NewNumber("100000000000000000", NumberTypeDecimal)}, | ||
| } | ||
|
|
||
| for _, tc := range cases { | ||
| l := tc.l.WeightString(nil) | ||
| r := tc.r.WeightString(nil) | ||
|
|
||
| if bytes.Compare(l, r) >= 0 { | ||
| t.Errorf("expected %s < %s\nl = %v\n = %v\nr = %v\n = %v", | ||
| tc.l.String(), tc.r.String(), l, string(l), r, string(r)) | ||
| } | ||
| } | ||
| } | ||
Uh oh!
There was an error while loading. Please reload this page.