Skip to content

Commit

Permalink
evalengine: fix byte flags
Browse files Browse the repository at this point in the history
Signed-off-by: Vicent Marti <[email protected]>
  • Loading branch information
vmg committed Oct 26, 2023
1 parent f8a274d commit 81eb580
Show file tree
Hide file tree
Showing 8 changed files with 44 additions and 23 deletions.
6 changes: 5 additions & 1 deletion go/mysql/collations/local.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,13 @@ func Default() ID {
}

func DefaultCollationForType(t sqltypes.Type) ID {
return CollationForType(t, Default())
}

func CollationForType(t sqltypes.Type, default_ ID) ID {
switch {
case sqltypes.IsText(t):
return Default()
return default_
case t == sqltypes.TypeJSON:
return CollationUtf8mb4ID
default:
Expand Down
15 changes: 15 additions & 0 deletions go/vt/vtgate/evalengine/api_type_aggregation.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,21 @@ func AggregateTypes(types []sqltypes.Type) sqltypes.Type {
return typeAgg.result()
}

func (ta *typeAggregation) addEval(e eval) {
var t sqltypes.Type
var f typeFlag
switch e := e.(type) {
case nil:
t = sqltypes.Null
case *evalBytes:
t = sqltypes.Type(e.tt)
f = e.flag
default:
t = e.SQLType()
}
ta.add(t, f)
}

func (ta *typeAggregation) add(tt sqltypes.Type, f typeFlag) {
switch tt {
case sqltypes.Float32, sqltypes.Float64:
Expand Down
2 changes: 1 addition & 1 deletion go/vt/vtgate/evalengine/arithmetic.go
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ func integerDivideConvert(arg eval) evalNumeric {
return dec
}

if b1, ok := arg.(*evalBytes); ok && b1.isHexLiteral {
if b1, ok := arg.(*evalBytes); ok && b1.isHexLiteral() {
hex, ok := b1.toNumericHex()
if !ok {
return newEvalDecimal(decimal.Zero, 0, 0)
Expand Down
4 changes: 2 additions & 2 deletions go/vt/vtgate/evalengine/eval.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ import (
"vitess.io/vitess/go/vt/vthash"
)

type typeFlag uint32
type typeFlag uint16

const (
// flagNull marks that this value is null; implies flagNullable
Expand Down Expand Up @@ -149,7 +149,7 @@ func evalIsTruthy(e eval) boolean {
case *evalDecimal:
return makeboolean(!e.dec.IsZero())
case *evalBytes:
if e.isHexLiteral {
if e.isHexLiteral() {
hex, ok := e.toNumericHex()
if !ok {
// overflow
Expand Down
23 changes: 15 additions & 8 deletions go/vt/vtgate/evalengine/eval_bytes.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,10 @@ import (
)

type evalBytes struct {
tt int16
isHexLiteral bool
isBitLiteral bool
col collations.TypedCollation
bytes []byte
tt int16
flag typeFlag
col collations.TypedCollation
bytes []byte
}

var _ eval = (*evalBytes)(nil)
Expand All @@ -44,14 +43,14 @@ func newEvalRaw(typ sqltypes.Type, raw []byte, col collations.TypedCollation) *e
}

func newEvalBytesHex(raw []byte) eval {
return &evalBytes{tt: int16(sqltypes.VarBinary), isHexLiteral: true, col: collationBinary, bytes: raw}
return &evalBytes{tt: int16(sqltypes.VarBinary), flag: flagHex, col: collationBinary, bytes: raw}
}

// newEvalBytesBit creates a new evalBytes for a bit literal.
// Turns out that a bit literal is not actually typed with
// sqltypes.Bit, but with sqltypes.VarBinary.
func newEvalBytesBit(raw []byte) eval {
return &evalBytes{tt: int16(sqltypes.VarBinary), isBitLiteral: true, col: collationBinary, bytes: raw}
return &evalBytes{tt: int16(sqltypes.VarBinary), flag: flagBit, col: collationBinary, bytes: raw}
}

func newEvalBinary(raw []byte) *evalBytes {
Expand Down Expand Up @@ -119,8 +118,16 @@ func (e *evalBytes) isBinary() bool {
return e.SQLType() == sqltypes.VarBinary || e.SQLType() == sqltypes.Binary || e.SQLType() == sqltypes.Blob
}

func (e *evalBytes) isHexLiteral() bool {
return e.flag&flagHex != 0
}

func (e *evalBytes) isBitLiteral() bool {
return e.flag&flagBit != 0
}

func (e *evalBytes) isHexOrBitLiteral() bool {
return e.isHexLiteral || e.isBitLiteral
return e.isHexLiteral() || e.isBitLiteral()
}

func (e *evalBytes) isVarChar() bool {
Expand Down
8 changes: 4 additions & 4 deletions go/vt/vtgate/evalengine/eval_numeric.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ func evalToNumeric(e eval, preciseDatetime bool) evalNumeric {
case evalNumeric:
return e
case *evalBytes:
if e.isHexLiteral {
if e.isHexLiteral() {
hex, ok := e.toNumericHex()
if !ok {
// overflow
Expand Down Expand Up @@ -148,7 +148,7 @@ func evalToFloat(e eval) (*evalFloat, bool) {
case evalNumeric:
return e.toFloat()
case *evalBytes:
if e.isHexLiteral {
if e.isHexLiteral() {
hex, ok := e.toNumericHex()
if !ok {
// overflow
Expand Down Expand Up @@ -190,7 +190,7 @@ func evalToDecimal(e eval, m, d int32) *evalDecimal {
case evalNumeric:
return e.toDecimal(m, d)
case *evalBytes:
if e.isHexLiteral {
if e.isHexLiteral() {
hex, ok := e.toNumericHex()
if !ok {
// overflow
Expand Down Expand Up @@ -248,7 +248,7 @@ func evalToInt64(e eval) *evalInt64 {
case evalNumeric:
return e.toInt64()
case *evalBytes:
if e.isHexLiteral {
if e.isHexLiteral() {
hex, ok := e.toNumericHex()
if !ok {
// overflow
Expand Down
7 changes: 1 addition & 6 deletions go/vt/vtgate/evalengine/expr_literal.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,12 +43,7 @@ func (l *Literal) typeof(*ExpressionEnv, []*querypb.Field) (sqltypes.Type, typeF
case nil:
return sqltypes.Null, flagNull | flagNullable
case *evalBytes:
if e.isBitLiteral {
f |= flagBit
}
if e.isHexLiteral {
f |= flagHex
}
f = e.flag
case *evalInt64:
if e.i == math.MinInt64 {
f |= flagIntegerUdf
Expand Down
2 changes: 1 addition & 1 deletion go/vt/vtgate/evalengine/fn_time.go
Original file line number Diff line number Diff line change
Expand Up @@ -1375,7 +1375,7 @@ func dateTimeUnixTimestamp(env *ExpressionEnv, date eval) evalNumeric {
case *evalDecimal:
prec = d.length
case *evalBytes:
if d.isHexLiteral {
if d.isHexLiteral() {
return newEvalInt64(0)
}
prec = 6
Expand Down

0 comments on commit 81eb580

Please sign in to comment.