Skip to content
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
24 changes: 24 additions & 0 deletions data/test/tabletserver/exec_cases.txt
Original file line number Diff line number Diff line change
Expand Up @@ -1036,6 +1036,30 @@
"PKValues":[":a"]
}

# insert no values autoinc
"insert into auto values ()"
{
"PlanID": "INSERT_PK",
"TableName": "auto",
"FullQuery": "insert into auto values ()",
"OuterQuery": "insert into auto(id) values (null)",
"PKValues":[
[null]
]
}

# insert no values defaults
"insert into with_defaults values ()"
{
"PlanID": "INSERT_PK",
"TableName": "with_defaults",
"FullQuery": "insert into with_defaults values ()",
"OuterQuery": "insert into with_defaults(aid, bid, cid) values (3, -2, null)",
"PKValues":[
3
]
}

# nextval on non-sequence table
"select next value from a"
"a is not a sequence"
Expand Down
62 changes: 62 additions & 0 deletions data/test/tabletserver/schema_test.json
Original file line number Diff line number Diff line change
Expand Up @@ -247,6 +247,68 @@
],
"Type": 1
},
{
"Name": "auto",
"Columns": [
{
"Name": "id",
"IsAuto": true
}
],
"Indexes": [
{
"Name": "PRIMARY",
"Unique": true,
"Columns": [
"id"
],
"Cardinality": [
1
],
"DataColumns": [
]
}
],
"PKColumns": [
0
],
"Type": 0
},
{
"Name": "with_defaults",
"Columns": [
{
"Name": "aid",
"Default": 3
},
{
"Name": "bid",
"Default": -2
},
{
"Name": "cid",
"Default": null
}
],
"Indexes": [
{
"Name": "PRIMARY",
"Unique": true,
"Columns": [
"id"
],
"Cardinality": [
1
],
"DataColumns": [
]
}
],
"PKColumns": [
0
],
"Type": 0
},
{
"Name": "msg",
"Columns": [
Expand Down
4 changes: 4 additions & 0 deletions go/sqltypes/type.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,10 @@ func isNumber(t querypb.Type) bool {
// neither binary or text.
// querypb.Type_TUPLE is not included in this list
// because it's not a valid Value type.
// TODO(sougou): provide a categorization function
// that returns enums, which will allow for cleaner
// switch statements for those who want to cover types
// by their category.
const (
Null = querypb.Type_NULL_TYPE
Int8 = querypb.Type_INT8
Expand Down
20 changes: 20 additions & 0 deletions go/vt/sqlparser/ast.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ import (

"github.com/youtube/vitess/go/sqltypes"
querypb "github.com/youtube/vitess/go/vt/proto/query"
vtrpcpb "github.com/youtube/vitess/go/vt/proto/vtrpc"
"github.com/youtube/vitess/go/vt/vterrors"
)

// Instructions for creating new types: If a type
Expand Down Expand Up @@ -1770,6 +1772,24 @@ func (node *ExistsExpr) WalkSubtree(visit Visit) error {
)
}

// ExprFromValue converts the given Value into an Expr or returns an error.
func ExprFromValue(value sqltypes.Value) (Expr, error) {
// The type checks here follow the rules defined in sqltypes/types.go.
switch {
case value.Type() == sqltypes.Null:
return &NullVal{}, nil
case value.IsIntegral():
return NewIntVal(value.ToBytes()), nil
case value.IsFloat() || value.Type() == sqltypes.Decimal:
return NewFloatVal(value.ToBytes()), nil
case value.IsQuoted():
return NewStrVal(value.ToBytes()), nil
default:
// We cannot support sqltypes.Expression, or any other invalid type.
return nil, vterrors.Errorf(vtrpcpb.Code_INTERNAL, "cannot convert value %v to AST", value)
}
}

// ValType specifies the type for SQLVal.
type ValType int

Expand Down
43 changes: 43 additions & 0 deletions go/vt/sqlparser/ast_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ import (
"reflect"
"testing"
"unsafe"

"github.com/youtube/vitess/go/sqltypes"
)

func TestAppend(t *testing.T) {
Expand Down Expand Up @@ -199,6 +201,47 @@ func TestIsAggregate(t *testing.T) {
}
}

func TestExprFromValue(t *testing.T) {
tcases := []struct {
in sqltypes.Value
out SQLNode
err string
}{{
in: sqltypes.NULL,
out: &NullVal{},
}, {
in: sqltypes.NewInt64(1),
out: NewIntVal([]byte("1")),
}, {
in: sqltypes.NewFloat64(1.1),
out: NewFloatVal([]byte("1.1")),
}, {
in: sqltypes.MakeTrusted(sqltypes.Decimal, []byte("1.1")),
out: NewFloatVal([]byte("1.1")),
}, {
in: sqltypes.NewVarChar("aa"),
out: NewStrVal([]byte("aa")),
}, {
in: sqltypes.MakeTrusted(sqltypes.Expression, []byte("rand()")),
err: "cannot convert value EXPRESSION(rand()) to AST",
}}
for _, tcase := range tcases {
got, err := ExprFromValue(tcase.in)
if tcase.err != "" {
if err == nil || err.Error() != tcase.err {
t.Errorf("ExprFromValue(%v) err: %v, want %s", tcase.in, err, tcase.err)
}
continue
}
if err != nil {
t.Error(err)
}
if got, want := got, tcase.out; !reflect.DeepEqual(got, want) {
t.Errorf("ExprFromValue(%v): %v, want %s", tcase.in, got, want)
}
}
}

func TestColNameEqual(t *testing.T) {
var c1, c2 *ColName
if c1.Equal(c2) {
Expand Down
6 changes: 6 additions & 0 deletions go/vt/sqlparser/parse_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -637,6 +637,12 @@ func TestValid(t *testing.T) {
}, {
input: "set character set utf8",
output: "set ",
}, {
input: "set character set 'utf8'",
output: "set ",
}, {
input: "set character set \"utf8\"",
output: "set ",
}, {
input: "set charset default",
output: "set ",
Expand Down
Loading