Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion go/mysql/datetime/parse.go
Original file line number Diff line number Diff line change
Expand Up @@ -321,7 +321,7 @@ func ParseDateTimeInt64(i int64) (dt DateTime, ok bool) {
if i == 0 {
return dt, true
}
if t == 0 || d == 0 {
if d == 0 {
return dt, false
}
dt.Time, ok = ParseTimeInt64(t)
Expand Down
52 changes: 52 additions & 0 deletions go/mysql/datetime/parse_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ limitations under the License.
package datetime

import (
"fmt"
"testing"

"github.com/stretchr/testify/assert"
Expand Down Expand Up @@ -235,6 +236,7 @@ func TestParseDateTime(t *testing.T) {
{input: "20221012111213.123456", output: datetime{2022, 10, 12, 11, 12, 13, 123456000}, l: 6},
{input: "221012111213.123456", output: datetime{2022, 10, 12, 11, 12, 13, 123456000}, l: 6},
{input: "2022101211121321321312", output: datetime{2022, 10, 12, 11, 12, 13, 0}, err: true},
{input: "3284004416225113510", output: datetime{}, err: true},
{input: "2012-12-31 11:30:45", output: datetime{2012, 12, 31, 11, 30, 45, 0}},
{input: "2012^12^31 11+30+45", output: datetime{2012, 12, 31, 11, 30, 45, 0}},
{input: "2012/12/31 11*30*45", output: datetime{2012, 12, 31, 11, 30, 45, 0}},
Expand Down Expand Up @@ -290,3 +292,53 @@ func TestParseDateTime(t *testing.T) {
})
}
}

func TestParseDateTimeInt64(t *testing.T) {
type datetime struct {
year int
month int
day int
hour int
minute int
second int
nanosecond int
}
tests := []struct {
input int64
output datetime
l int
err bool
}{
{input: 1, output: datetime{}, err: true},
{input: 20221012000000, output: datetime{2022, 10, 12, 0, 0, 0, 0}},
{input: 20221012112233, output: datetime{2022, 10, 12, 11, 22, 33, 0}},
}

for _, test := range tests {
t.Run(fmt.Sprintf("%d", test.input), func(t *testing.T) {
got, ok := ParseDateTimeInt64(test.input)
if test.err {
if !got.IsZero() {
assert.Equal(t, test.output.year, got.Date.Year())
assert.Equal(t, test.output.month, got.Date.Month())
assert.Equal(t, test.output.day, got.Date.Day())
assert.Equal(t, test.output.hour, got.Time.Hour())
assert.Equal(t, test.output.minute, got.Time.Minute())
assert.Equal(t, test.output.second, got.Time.Second())
assert.Equal(t, test.output.nanosecond, got.Time.Nanosecond())
}
assert.Falsef(t, ok, "did not fail to parse %s", test.input)
return
}

require.True(t, ok)
assert.Equal(t, test.output.year, got.Date.Year())
assert.Equal(t, test.output.month, got.Date.Month())
assert.Equal(t, test.output.day, got.Date.Day())
assert.Equal(t, test.output.hour, got.Time.Hour())
assert.Equal(t, test.output.minute, got.Time.Minute())
assert.Equal(t, test.output.second, got.Time.Second())
assert.Equal(t, test.output.nanosecond, got.Time.Nanosecond())
})
}
}
2 changes: 1 addition & 1 deletion go/mysql/datetime/timeparts.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ func (tp *timeparts) toDateTime(prec int) (DateTime, int, bool) {
if tp.yday > 0 {
return DateTime{}, 0, false
} else {
if tp.month < 0 {
if tp.month < 1 {
tp.month = int(time.January)
}
if tp.day < 0 {
Expand Down
26 changes: 25 additions & 1 deletion go/mysql/json/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,16 @@ package json

import (
"vitess.io/vitess/go/sqltypes"
vtrpcpb "vitess.io/vitess/go/vt/proto/vtrpc"
"vitess.io/vitess/go/vt/vterrors"
"vitess.io/vitess/go/vt/vthash"
)

const hashPrefixJSON = 0xCCBB

func (v *Value) Hash(h *vthash.Hasher) {
h.Write16(hashPrefixJSON)
_, _ = h.Write(v.ToRawBytes())
_, _ = h.Write(v.WeightString(nil))
}

func (v *Value) ToRawBytes() []byte {
Expand Down Expand Up @@ -81,6 +83,28 @@ func NewOpaqueValue(raw string) *Value {
return &Value{s: raw, t: TypeOpaque}
}

func NewFromSQL(v sqltypes.Value) (*Value, error) {
switch {
case v.Type() == sqltypes.TypeJSON:
var p Parser
return p.ParseBytes(v.Raw())
case v.IsSigned():
return NewNumber(v.RawStr(), NumberTypeSigned), nil
case v.IsUnsigned():
return NewNumber(v.RawStr(), NumberTypeUnsigned), nil
case v.IsDecimal():
return NewNumber(v.RawStr(), NumberTypeDecimal), nil
case v.IsFloat():
return NewNumber(v.RawStr(), NumberTypeFloat), nil
case v.IsText():
return NewString(v.RawStr()), nil
case v.IsBinary():
return NewBlob(v.RawStr()), nil
default:
return nil, vterrors.Errorf(vtrpcpb.Code_INTERNAL, "cannot coerce %v as a JSON type", v)
}
}

func (v *Value) Depth() int {
max := func(a, b int) int {
if a > b {
Expand Down
16 changes: 10 additions & 6 deletions go/mysql/json/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -704,6 +704,15 @@ func (v *Value) MarshalTime() string {
return ""
}

func (v *Value) marshalFloat(dst []byte) []byte {
f, _ := v.Float64()
buf := format.FormatFloat(f)
if bytes.IndexByte(buf, '.') == -1 && bytes.IndexByte(buf, 'e') == -1 {
buf = append(buf, '.', '0')
}
return append(dst, buf...)
}

// MarshalTo appends marshaled v to dst and returns the result.
func (v *Value) MarshalTo(dst []byte) []byte {
switch v.t {
Expand Down Expand Up @@ -744,12 +753,7 @@ func (v *Value) MarshalTo(dst []byte) []byte {
return dst
case TypeNumber:
if v.NumberType() == NumberTypeFloat {
f, _ := v.Float64()
buf := format.FormatFloat(f)
if bytes.IndexByte(buf, '.') == -1 && bytes.IndexByte(buf, 'e') == -1 {
buf = append(buf, '.', '0')
}
return append(dst, buf...)
return v.marshalFloat(dst)
}
return append(dst, v.s...)
case TypeBoolean:
Expand Down
169 changes: 169 additions & 0 deletions go/mysql/json/weights.go
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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit:

Suggested change
const MaxPadLength = 30
const maxPadLength = 30


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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why do we always use BigEndian here not also Little Endian?

Copy link
Member Author

Choose a reason for hiding this comment

The 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
}
44 changes: 44 additions & 0 deletions go/mysql/json/weights_test.go
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

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))
}
}
}
Loading