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
49 changes: 48 additions & 1 deletion go/test/endtoend/vtgate/vindex_bindvars/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,6 @@ var (
PRIMARY KEY (id)
) ENGINE=Innodb;


CREATE TABLE lookup1 (
field BIGINT NOT NULL,
keyspace_id binary(8),
Expand All @@ -56,6 +55,12 @@ CREATE TABLE lookup2 (
keyspace_id binary(8),
UNIQUE KEY (field2)
) ENGINE=Innodb;

CREATE TABLE thex (
id VARBINARY(64) NOT NULL,
field BIGINT NOT NULL,
PRIMARY KEY (id)
) ENGINE=InnoDB;
`

VSchema = `
Expand All @@ -65,6 +70,18 @@ CREATE TABLE lookup2 (
"hash": {
"type": "hash"
},
"binary_vdx": {
"type": "binary"
},
"binary_md5_vdx": {
"type": "binary_md5"
},
"xxhash_vdx": {
"type": "xxhash"
},
"numeric_vdx": {
"type": "numeric"
},
"lookup1": {
"type": "consistent_lookup",
"params": {
Expand Down Expand Up @@ -118,6 +135,14 @@ CREATE TABLE lookup2 (
"name": "hash"
}
]
},
"thex": {
"column_vindexes": [
{
"column": "id",
"name": "binary_vdx"
}
]
}
}
}`
Expand Down Expand Up @@ -162,6 +187,28 @@ func TestMain(m *testing.M) {
os.Exit(exitCode)
}

func TestVindexHexTypes(t *testing.T) {
defer cluster.PanicHandler(t)
ctx := context.Background()
conn, err := mysql.Connect(ctx, &vtParams)
require.Nil(t, err)
defer conn.Close()

exec(t, conn, "INSERT INTO thex (id, field) VALUES "+
"(0x01,1), "+
"(x'a5',2), "+
"(0x48656c6c6f20476f7068657221,3), "+
"(x'c26caa1a5eb94096d29a1bec',4)")
result := exec(t, conn, "select id, field from thex order by id")

expected :=
"[[VARBINARY(\"\\x01\") INT64(1)] " +
"[VARBINARY(\"Hello Gopher!\") INT64(3)] " +
"[VARBINARY(\"\\xa5\") INT64(2)] " +
"[VARBINARY(\"\\xc2l\\xaa\\x1a^\\xb9@\\x96Қ\\x1b\\xec\") INT64(4)]]"
assert.Equal(t, expected, fmt.Sprintf("%v", result.Rows))
}

func TestVindexBindVarOverlap(t *testing.T) {
defer cluster.PanicHandler(t)
ctx := context.Background()
Expand Down
35 changes: 14 additions & 21 deletions go/vt/sqlparser/ast_funcs.go
Original file line number Diff line number Diff line change
Expand Up @@ -1450,23 +1450,6 @@ const (
DoubleAt
)

// handleUnaryMinus handles the case when a unary minus operator is seen in the parser. It takes 1 argument which is the expr to which the unary minus has been added to.
func handleUnaryMinus(expr Expr) Expr {
if num, ok := expr.(*Literal); ok && (num.Type == IntVal || num.Type == FloatVal || num.Type == DecimalVal) {
// Handle double negative
if num.Val[0] == '-' {
num.Val = num.Val[1:]
return num
}
num.Val = "-" + num.Val
return num
}
if unaryExpr, ok := expr.(*UnaryExpr); ok && unaryExpr.Operator == UMinusOp {
return unaryExpr.Expr
}
return &UnaryExpr{Operator: UMinusOp, Expr: expr}
}

// encodeSQLString encodes the string as a SQL string.
func encodeSQLString(val string) string {
return sqltypes.EncodeStringSQL(val)
Expand Down Expand Up @@ -1586,6 +1569,19 @@ func (es *ExtractedSubquery) updateAlternative() {
}
}

func isExprLiteral(expr Expr) bool {
switch expr := expr.(type) {
case *Literal:
return true
case BoolVal:
return true
case *UnaryExpr:
return isExprLiteral(expr.Expr)
default:
return false
}
}

func defaultRequiresParens(ct *ColumnType) bool {
// in 5.7 null value should be without parenthesis, in 8.0 it is allowed either way.
// so it is safe to not keep parenthesis around null.
Expand All @@ -1601,10 +1597,7 @@ func defaultRequiresParens(ct *ColumnType) bool {
return true
}

_, isLiteral := ct.Options.Default.(*Literal)
_, isBool := ct.Options.Default.(BoolVal)

if isLiteral || isBool || isExprAliasForCurrentTimeStamp(ct.Options.Default) {
if isExprLiteral(ct.Options.Default) || isExprAliasForCurrentTimeStamp(ct.Options.Default) {
return false
}

Expand Down
14 changes: 8 additions & 6 deletions go/vt/sqlparser/parse_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,7 @@ var (
input: "select -1 from t where b = -2",
}, {
input: "select - -1 from t",
output: "select 1 from t",
output: "select - -1 from t",
}, {
input: "select a from t",
}, {
Expand Down Expand Up @@ -785,7 +785,7 @@ var (
output: "select /* binary unary */ a - -b from t",
}, {
input: "select /* - - */ - -b from t",
output: "select /* - - */ b from t",
output: "select /* - - */ - -b from t",
}, {
input: "select /* binary binary */ binary binary b from t",
}, {
Expand Down Expand Up @@ -1093,10 +1093,10 @@ var (
input: "set @period.variable = 42",
}, {
input: "set S= +++-++-+(4+1)",
output: "set S = 4 + 1",
output: "set S = - -(4 + 1)",
}, {
input: "set S= +- - - - -(4+1)",
output: "set S = -(4 + 1)",
output: "set S = - - - - -(4 + 1)",
}, {
input: "alter table a add foo int references simple (a) on delete restrict first",
output: "alter table a add column foo int references simple (a) on delete restrict first",
Expand Down Expand Up @@ -1354,9 +1354,11 @@ var (
}, {
input: "create table a (\n\ta int not null default 0\n)",
}, {
input: "create table a (\n\ta float not null default -1\n)",
input: "create table a (\n\ta float not null default -1\n)",
output: "create table a (\n\ta float not null default -1\n)",
}, {
input: "create table a (\n\ta float not null default -2.1\n)",
input: "create table a (\n\ta float not null default -2.1\n)",
output: "create table a (\n\ta float not null default -2.1\n)",
}, {
input: "create table a (a int not null default 0, primary key(a))",
output: "create table a (\n\ta int not null default 0,\n\tprimary key (a)\n)",
Expand Down
4 changes: 2 additions & 2 deletions go/vt/sqlparser/sql.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions go/vt/sqlparser/sql.y
Original file line number Diff line number Diff line change
Expand Up @@ -1346,7 +1346,7 @@ NULL
}
| '-' NUM_literal
{
$$ = handleUnaryMinus($2)
$$ = &UnaryExpr{Operator: UMinusOp, Expr: $2}
}

literal:
Expand Down Expand Up @@ -4291,7 +4291,7 @@ function_call_keyword
}
| '-' simple_expr %prec UNARY
{
$$ = handleUnaryMinus($2)
$$ = &UnaryExpr{Operator: UMinusOp, Expr: $2}
}
| '~' simple_expr %prec UNARY
{
Expand Down
12 changes: 6 additions & 6 deletions go/vt/sqlparser/testdata/select_cases.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3248,7 +3248,7 @@ INPUT
select date_add("1997-12-31 23:59:59",INTERVAL -100000 MINUTE);
END
OUTPUT
select date_add('1997-12-31 23:59:59', interval -100000 MINUTE) from dual
select date_add('1997-12-31 23:59:59', interval (-100000) MINUTE) from dual
END
INPUT
select insert('hello', 18446744073709551616, 1, 'hi');
Expand Down Expand Up @@ -8108,7 +8108,7 @@ INPUT
select -(-9223372036854775808), -(-(-9223372036854775808));
END
OUTPUT
select 9223372036854775808, -9223372036854775808 from dual
select - -9223372036854775808, - - -9223372036854775808 from dual
END
INPUT
select from t1_mrg;
Expand Down Expand Up @@ -12176,7 +12176,7 @@ INPUT
select date_add("2001-01-01 23:59:59",INTERVAL -2000 YEAR);
END
OUTPUT
select date_add('2001-01-01 23:59:59', interval -2000 YEAR) from dual
select date_add('2001-01-01 23:59:59', interval (-2000) YEAR) from dual
END
INPUT
select avg(2) from t1;
Expand Down Expand Up @@ -12812,7 +12812,7 @@ INPUT
select date_add("1997-12-31 23:59:59",INTERVAL -100000 DAY);
END
OUTPUT
select date_add('1997-12-31 23:59:59', interval -100000 DAY) from dual
select date_add('1997-12-31 23:59:59', interval (-100000) DAY) from dual
END
INPUT
select dummy1,count(distinct id) from t1 group by dummy1;
Expand Down Expand Up @@ -13832,7 +13832,7 @@ INPUT
select date_add("1997-12-31 23:59:59",INTERVAL -100000 YEAR);
END
OUTPUT
select date_add('1997-12-31 23:59:59', interval -100000 YEAR) from dual
select date_add('1997-12-31 23:59:59', interval (-100000) YEAR) from dual
END
INPUT
select substring_index('the king of the the hill',' ',-2);
Expand Down Expand Up @@ -20180,7 +20180,7 @@ INPUT
select -(-(9223372036854775808));
END
OUTPUT
select 9223372036854775808 from dual
select - -9223372036854775808 from dual
END
INPUT
select format(col2,7) from t1;
Expand Down
6 changes: 5 additions & 1 deletion go/vt/vtgate/evalengine/arithmetic.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ package evalengine
import (
"bytes"
"fmt"
"math"
"os"
"strings"

"vitess.io/vitess/go/hack"
Expand Down Expand Up @@ -558,7 +560,9 @@ func floatPlusAny(v1 float64, v2 *EvalResult, out *EvalResult) error {
if err != nil {
return err
}
out.setFloat(v1 + v2f)
add := v1 + v2f
fmt.Fprintf(os.Stderr, "%f (%v) + %f (%v) = %f (%v)\n", v1, math.Signbit(v1), v2f, math.Signbit(v2f), add, math.Signbit(add))
out.setFloat(add)
return nil
}

Expand Down
2 changes: 1 addition & 1 deletion go/vt/vtgate/evalengine/cached_size.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

18 changes: 9 additions & 9 deletions go/vt/vtgate/evalengine/casting_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,35 +28,35 @@ import (

func TestEvalResultToBooleanStrict(t *testing.T) {
trueValues := []*EvalResult{{
type_: sqltypes.Int64,
type_: int16(sqltypes.Int64),
numeric_: 1,
}, {
type_: sqltypes.Uint64,
type_: int16(sqltypes.Uint64),
numeric_: 1,
}, {
type_: sqltypes.Int8,
type_: int16(sqltypes.Int8),
numeric_: 1,
}}

falseValues := []*EvalResult{{
type_: sqltypes.Int64,
type_: int16(sqltypes.Int64),
numeric_: 0,
}, {
type_: sqltypes.Uint64,
type_: int16(sqltypes.Uint64),
numeric_: 0,
}, {
type_: sqltypes.Int8,
type_: int16(sqltypes.Int8),
numeric_: 0,
}}

invalid := []*EvalResult{{
type_: sqltypes.VarChar,
type_: int16(sqltypes.VarChar),
bytes_: []byte("foobar"),
}, {
type_: sqltypes.Float32,
type_: int16(sqltypes.Float32),
numeric_: math.Float64bits(1.0),
}, {
type_: sqltypes.Int64,
type_: int16(sqltypes.Int64),
numeric_: 12,
}}

Expand Down
18 changes: 15 additions & 3 deletions go/vt/vtgate/evalengine/convert.go
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,8 @@ func convertExpr(e sqlparser.Expr, lookup ConverterLookup) (Expr, error) {
case sqlparser.StrVal:
collation := getCollation(e, lookup)
return NewLiteralString(node.Bytes(), collation), nil
case sqlparser.HexNum:
return NewLiteralBinaryFromHexNum(node.Bytes())
case sqlparser.HexVal:
return NewLiteralBinaryFromHex(node.Bytes())
}
Expand Down Expand Up @@ -317,10 +319,10 @@ func convertExpr(e sqlparser.Expr, lookup ConverterLookup) (Expr, error) {
case *Literal:
switch collation {
case collations.CollationBinaryID:
lit.Val.type_ = querypb.Type_VARBINARY
lit.Val.type_ = int16(querypb.Type_VARBINARY)
lit.Val.collation_ = collationBinary
default:
lit.Val.type_ = querypb.Type_VARCHAR
lit.Val.type_ = int16(querypb.Type_VARCHAR)
lit.Val.replaceCollationID(collation)
}
case *BindVariable:
Expand Down Expand Up @@ -359,12 +361,22 @@ func convertExpr(e sqlparser.Expr, lookup ConverterLookup) (Expr, error) {
args = append(args, convertedExpr)
aliases = append(aliases, aliased.As)
}
return &CallExpression{
return &CallExpr{
Arguments: args,
Aliases: aliases,
Method: method,
Call: call,
}, nil
case *sqlparser.UnaryExpr:
expr, err := convertExpr(node.Expr, lookup)
if err != nil {
return nil, err
}

switch node.Operator {
case sqlparser.UMinusOp:
return &NegateExpr{UnaryExpr: UnaryExpr{expr}}, nil
}
}
return nil, convertNotSupported(e)
}
Expand Down
Loading