Skip to content

Commit

Permalink
evalengine: Cleanup unneeded flag
Browse files Browse the repository at this point in the history
We know this value always based on whether the type is -1 or a valid
value, so we don't need the additional boolean value here.

Signed-off-by: Dirkjan Bussink <[email protected]>
  • Loading branch information
dbussink committed Jul 17, 2023
1 parent 41ab596 commit 8da8cdf
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 16 deletions.
3 changes: 0 additions & 3 deletions go/vt/vtgate/evalengine/api_literal.go
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,6 @@ func NewBindVar(key string, typ sqltypes.Type, col collations.ID) *BindVariable
Coercibility: collations.CoerceCoercible,
Repertoire: collations.RepertoireUnicode,
},
typed: typ >= 0,
}
}

Expand All @@ -215,7 +214,6 @@ func NewBindVarTuple(key string, col collations.ID) *BindVariable {
Coercibility: collations.CoerceCoercible,
Repertoire: collations.RepertoireUnicode,
},
typed: true,
}
}

Expand All @@ -229,7 +227,6 @@ func NewColumn(offset int, typ sqltypes.Type, col collations.ID) *Column {
Coercibility: collations.CoerceImplicit,
Repertoire: collations.RepertoireUnicode,
},
typed: typ >= 0,
}
}

Expand Down
11 changes: 7 additions & 4 deletions go/vt/vtgate/evalengine/expr_bvar.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ type (
Key string
Type sqltypes.Type
Collation collations.TypedCollation
typed bool
}
)

Expand Down Expand Up @@ -75,7 +74,7 @@ func (bv *BindVariable) eval(env *ExpressionEnv) (eval, error) {
return nil, vterrors.Errorf(vtrpcpb.Code_INVALID_ARGUMENT, "query argument '%s' must be a tuple (is %s)", bv.Key, bvar.Type)
}
typ := bvar.Type
if bv.typed {
if bv.typed() {
typ = bv.Type
}
return valueToEval(sqltypes.MakeTrusted(typ, bvar.Value), collations.TypedCollation{
Expand All @@ -89,7 +88,7 @@ func (bv *BindVariable) eval(env *ExpressionEnv) (eval, error) {
// typeof implements the Expr interface
func (bv *BindVariable) typeof(env *ExpressionEnv, _ []*querypb.Field) (sqltypes.Type, typeFlag) {
var tt sqltypes.Type
if bv.typed {
if bv.typed() {
tt = bv.Type
} else {
if bvar, err := env.lookupBindVar(bv.Key); err == nil {
Expand All @@ -107,7 +106,7 @@ func (bv *BindVariable) typeof(env *ExpressionEnv, _ []*querypb.Field) (sqltypes
}

func (bvar *BindVariable) compile(c *compiler) (ctype, error) {
if !bvar.typed {
if !bvar.typed() {
return ctype{}, c.unsupported(bvar)
}

Expand Down Expand Up @@ -143,3 +142,7 @@ func (bvar *BindVariable) compile(c *compiler) (ctype, error) {
Col: bvar.Collation,
}, nil
}

func (bvar *BindVariable) typed() bool {
return bvar.Type >= 0
}
9 changes: 6 additions & 3 deletions go/vt/vtgate/evalengine/expr_column.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ type (
Offset int
Type sqltypes.Type
Collation collations.TypedCollation
typed bool
}
)

Expand All @@ -53,14 +52,14 @@ func (c *Column) typeof(env *ExpressionEnv, fields []*querypb.Field) (sqltypes.T
return fields[c.Offset].Type, flagNullable // we probably got here because the value was NULL,
// so let's assume we are on a nullable field
}
if c.typed {
if c.typed() {
return c.Type, flagNullable
}
return -1, flagAmbiguousType
}

func (column *Column) compile(c *compiler) (ctype, error) {
if !column.typed {
if !column.typed() {
return ctype{}, c.unsupported(column)
}

Expand Down Expand Up @@ -102,3 +101,7 @@ func (column *Column) compile(c *compiler) (ctype, error) {
Col: col,
}, nil
}

func (column *Column) typed() bool {
return column.Type >= 0
}
7 changes: 4 additions & 3 deletions go/vt/vtgate/evalengine/expr_column_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,9 @@ func TestTypeOf(t *testing.T) {
}
fields := []*querypb.Field{field1, field2}

c := &Column{}
c := &Column{
Type: -1,
}
env.Row = sqltypes.Row{sqltypes.NewInt64(10)}

t.Run("Check when row value is not null", func(t *testing.T) {
Expand All @@ -66,11 +68,10 @@ func TestTypeOf(t *testing.T) {
c.Offset = 10
typ, flag := c.typeof(env, fields)
if typ != -1 || flag != flagAmbiguousType {
t.Errorf("typeof() failed, expected sqltypes.Null and flagAmbiguousType, got %v and %v", typ, flag)
t.Errorf("typeof() failed, expected -1 and flagAmbiguousType, got %v and %v", typ, flag)
}
})
t.Run("Check when typed is true", func(t *testing.T) {
c.typed = true
c.Type = querypb.Type_FLOAT32
typ, flag := c.typeof(env, fields)
if typ != querypb.Type_FLOAT32 || flag != flagNullable {
Expand Down
6 changes: 3 additions & 3 deletions go/vt/vtgate/evalengine/translate.go
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,7 @@ func defaultCoercionCollation(id collations.ID) collations.TypedCollation {
func (ast *astCompiler) translateBindVar(arg *sqlparser.Argument) (Expr, error) {
bvar := NewBindVar(arg.Name, arg.Type, ast.cfg.Collation)

if !bvar.typed {
if !bvar.typed() {
ast.untyped++
}
return bvar, nil
Expand All @@ -211,7 +211,7 @@ func (ast *astCompiler) translateColOffset(col *sqlparser.Offset) (Expr, error)
}

column := NewColumn(col.V, typ, coll)
if !column.typed {
if !column.typed() {
ast.untyped++
}
return column, nil
Expand All @@ -236,7 +236,7 @@ func (ast *astCompiler) translateColName(colname *sqlparser.ColName) (Expr, erro

column := NewColumn(idx, typ, coll)

if !column.typed {
if !column.typed() {
ast.untyped++
}
return column, nil
Expand Down

0 comments on commit 8da8cdf

Please sign in to comment.