Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[16.0] evalengine: TypeOf for Columns should only use value type when we have a value #13154

Merged
merged 1 commit into from
Jun 5, 2023
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
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