Skip to content

Commit 7e9e300

Browse files
author
James Cor
committed
limiting scope of changes
1 parent 67ade37 commit 7e9e300

File tree

8 files changed

+462
-283
lines changed

8 files changed

+462
-283
lines changed

sql/expression/comparison.go

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -190,6 +190,7 @@ func (c *comparison) CompareValue(ctx *sql.Context, row sql.ValueRow) (int, erro
190190
}
191191
return types.Float64.(sql.ValueType).CompareValue(ctx, lv, rv)
192192
}
193+
193194
return lTyp.CompareValue(ctx, lv, rv)
194195
}
195196

@@ -203,10 +204,8 @@ func (c *comparison) IsValueExpression() bool {
203204
if !ok {
204205
return false
205206
}
206-
if _, ok := c.LeftChild.Type().(sql.ValueType); !ok {
207-
return false
208-
}
209-
if _, ok := c.RightChild.Type().(sql.ValueType); !ok {
207+
// TODO: only allow comparisons between Integers, Floats, Decimals, Bits and Year for now
208+
if !types.IsNumber(c.LeftChild.Type()) || !types.IsNumber(c.RightChild.Type()) {
210209
return false
211210
}
212211
return l.IsValueExpression() && r.IsValueExpression()

sql/types/datetime.go

Lines changed: 1 addition & 132 deletions
Original file line numberDiff line numberDiff line change
@@ -191,25 +191,7 @@ func (t datetimeType) Compare(ctx context.Context, a interface{}, b interface{})
191191

192192
// CompareValue implements the ValueType interface
193193
func (t datetimeType) CompareValue(ctx *sql.Context, a, b sql.Value) (int, error) {
194-
if hasNulls, res := CompareNullValues(a, b); hasNulls {
195-
return res, nil
196-
}
197-
at, err := ConvertValueToDatetime(ctx, a)
198-
if err != nil {
199-
return 0, err
200-
}
201-
bt, err := ConvertValueToDatetime(ctx, b)
202-
if err != nil {
203-
return 0, err
204-
}
205-
switch {
206-
case at.Before(bt):
207-
return -1, nil
208-
case at.After(bt):
209-
return 1, nil
210-
default:
211-
return 0, nil
212-
}
194+
panic("TODO: implement CompareValue for DatetimeType")
213195
}
214196

215197
// Convert implements Type interface.
@@ -591,116 +573,3 @@ func ValidateTimestamp(t time.Time) interface{} {
591573
}
592574
return t
593575
}
594-
595-
func ConvertValueToDatetime(ctx *sql.Context, v sql.Value) (time.Time, error) {
596-
switch v.Typ {
597-
case sqltypes.Int8:
598-
x := values.ReadInt8(v.Val)
599-
if x == 0 {
600-
return zeroTime, nil
601-
}
602-
return zeroTime, ErrConvertingToTime.New(x)
603-
case sqltypes.Int16:
604-
x := values.ReadInt16(v.Val)
605-
if x == 0 {
606-
return zeroTime, nil
607-
}
608-
return zeroTime, ErrConvertingToTime.New(x)
609-
case sqltypes.Int24:
610-
x := values.ReadInt24(v.Val)
611-
if x == 0 {
612-
return zeroTime, nil
613-
}
614-
return zeroTime, ErrConvertingToTime.New(x)
615-
case sqltypes.Int32:
616-
x := values.ReadInt32(v.Val)
617-
if x == 0 {
618-
return zeroTime, nil
619-
}
620-
return zeroTime, ErrConvertingToTime.New(x)
621-
case sqltypes.Int64:
622-
x := values.ReadInt64(v.Val)
623-
if x == 0 {
624-
return zeroTime, nil
625-
}
626-
return zeroTime, ErrConvertingToTime.New(x)
627-
case sqltypes.Uint8:
628-
x := values.ReadUint8(v.Val)
629-
if x == 0 {
630-
return zeroTime, nil
631-
}
632-
return zeroTime, ErrConvertingToTime.New(x)
633-
case sqltypes.Uint16:
634-
x := values.ReadUint16(v.Val)
635-
if x == 0 {
636-
return zeroTime, nil
637-
}
638-
return zeroTime, ErrConvertingToTime.New(x)
639-
case sqltypes.Uint24:
640-
x := values.ReadUint24(v.Val)
641-
if x == 0 {
642-
return zeroTime, nil
643-
}
644-
return zeroTime, ErrConvertingToTime.New(x)
645-
case sqltypes.Uint32:
646-
x := values.ReadUint32(v.Val)
647-
if x == 0 {
648-
return zeroTime, nil
649-
}
650-
return zeroTime, ErrConvertingToTime.New(x)
651-
case sqltypes.Uint64:
652-
x := values.ReadUint64(v.Val)
653-
if x == 0 {
654-
return zeroTime, nil
655-
}
656-
return zeroTime, ErrConvertingToTime.New(x)
657-
case sqltypes.Float32:
658-
x := values.ReadFloat32(v.Val)
659-
if x == 0 {
660-
return zeroTime, nil
661-
}
662-
return zeroTime, ErrConvertingToTime.New(x)
663-
case sqltypes.Float64:
664-
x := values.ReadFloat64(v.Val)
665-
if x == 0 {
666-
return zeroTime, nil
667-
}
668-
return zeroTime, ErrConvertingToTime.New(x)
669-
case sqltypes.Decimal:
670-
x := values.ReadDecimal(v.Val)
671-
if x.IsZero() {
672-
return zeroTime, nil
673-
}
674-
return zeroTime, ErrConvertingToTime.New(x)
675-
case sqltypes.Year:
676-
return zeroTime, nil
677-
case sqltypes.Date:
678-
x := values.ReadDate(v.Val)
679-
return x, nil
680-
case sqltypes.Time:
681-
x := values.ReadInt64(v.Val)
682-
if x == 0 {
683-
return zeroTime, nil
684-
}
685-
return zeroTime, ErrConvertingToTime.New(x)
686-
case sqltypes.Datetime, sqltypes.Timestamp:
687-
x := values.ReadDatetime(v.Val)
688-
return x, nil
689-
case sqltypes.Text, sqltypes.Blob:
690-
var err error
691-
if v.Val == nil {
692-
v.Val, err = v.WrappedVal.Unwrap(ctx)
693-
if err != nil {
694-
return zeroTime, err
695-
}
696-
}
697-
val := values.ReadString(v.Val)
698-
res, parsed, err := parseDatetime(val)
699-
if !parsed {
700-
return zeroTime, ErrConvertingToTime.New(v)
701-
}
702-
return res, err
703-
default:
704-
return zeroTime, sql.ErrInvalidBaseType.New(v.Typ.String(), "datetime")
705-
}
706-
}

sql/types/enum.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,11 @@ func (t EnumType) Compare(ctx context.Context, a interface{}, b interface{}) (in
158158
return 0, nil
159159
}
160160

161+
// CompareValue implements the ValueType interface
162+
func (t EnumType) CompareValue(ctx *sql.Context, a, b sql.Value) (int, error) {
163+
panic("TODO: implement CompareValue for EnumType")
164+
}
165+
161166
// Convert implements Type interface.
162167
func (t EnumType) Convert(ctx context.Context, v interface{}) (interface{}, sql.ConvertInRange, error) {
163168
if v == nil {

sql/types/number.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1126,6 +1126,12 @@ func ConvertValueToInt64(ctx *sql.Context, v sql.Value) (int64, error) {
11261126
return math.MinInt64, nil
11271127
}
11281128
return v.Round(0).IntPart(), nil
1129+
case sqltypes.Bit:
1130+
v := values.ReadUint64(v.Val)
1131+
if v > math.MaxInt64 {
1132+
return math.MaxInt64, nil
1133+
}
1134+
return int64(v), nil
11291135
case sqltypes.Year:
11301136
v := values.ReadUint16(v.Val)
11311137
return int64(v), nil
@@ -1216,6 +1222,8 @@ func ConvertValueToUint64(ctx *sql.Context, v sql.Value) (uint64, error) {
12161222
return uint64(math.Round(ret)), nil
12171223
}
12181224
return uint64(v.Round(0).IntPart()), nil
1225+
case sqltypes.Bit:
1226+
return values.ReadUint64(v.Val), nil
12191227
case sqltypes.Year:
12201228
v := values.ReadUint16(v.Val)
12211229
return uint64(v), nil

0 commit comments

Comments
 (0)