Skip to content

Commit

Permalink
evalengine: TypeOf for Columns should only use value type when we hav…
Browse files Browse the repository at this point in the history
…e a value (#13148) (#13154)

Signed-off-by: Andres Taylor <[email protected]>
  • Loading branch information
systay authored Jun 5, 2023
1 parent 98f5afc commit 17fae95
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 3 deletions.
63 changes: 63 additions & 0 deletions go/vt/vtgate/evalengine/expr_column_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
/*
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 evalengine

import (
"testing"

"vitess.io/vitess/go/sqltypes"
querypb "vitess.io/vitess/go/vt/proto/query"
)

func TestTypeOf(t *testing.T) {
env := &ExpressionEnv{
BindVars: make(map[string]*querypb.BindVariable),
}

field1 := &querypb.Field{
Name: "field1",
Type: querypb.Type_INT64,
Flags: uint32(querypb.MySqlFlag_NOT_NULL_FLAG),
}
field2 := &querypb.Field{
Name: "field2",
Type: querypb.Type_VARCHAR,
Flags: 0,
}
fields := []*querypb.Field{field1, field2}

c := &Column{}
env.Row = sqltypes.Row{sqltypes.NewInt64(10)}
env.Fields = fields

t.Run("Check when row value is not null", func(t *testing.T) {
typ, f := c.typeof(env)
if typ != sqltypes.Int64 || f != flag(0) {
t.Errorf("typeof() failed, expected sqltypes.Int64 and typeFlag 0, got %v and %v", typ, f)
}
})

t.Run("Check when row value is null", func(t *testing.T) {
env.Row = sqltypes.Row{
sqltypes.NULL,
}
typ, flag := c.typeof(env)
if typ != querypb.Type_INT64 || flag != flagNullable {
t.Errorf("typeof() failed, expected querypb.Type_INT64 and flagNullable, got %v and %v", typ, flag)
}
})
}
5 changes: 2 additions & 3 deletions go/vt/vtgate/evalengine/expressions.go
Original file line number Diff line number Diff line change
Expand Up @@ -578,10 +578,9 @@ func (c *Column) typeof(env *ExpressionEnv) (sqltypes.Type, flag) {
// we'll try to do the best possible with the information we have
if c.Offset < len(env.Row) {
value := env.Row[c.Offset]
if value.IsNull() {
return sqltypes.Null, flagNull | flagNullable
if !value.IsNull() {
return value.Type(), flag(0)
}
return value.Type(), flag(0)
}

if c.Offset < len(env.Fields) {
Expand Down

0 comments on commit 17fae95

Please sign in to comment.