diff --git a/go/cmd/query_analyzer/query_analyzer.go b/go/cmd/query_analyzer/query_analyzer.go index dde5be1cca4..caeb1928270 100644 --- a/go/cmd/query_analyzer/query_analyzer.go +++ b/go/cmd/query_analyzer/query_analyzer.go @@ -113,7 +113,7 @@ func analyze(line []byte) { } func formatWithBind(buf *sqlparser.TrackedBuffer, node sqlparser.SQLNode) { - v, ok := node.(*sqlparser.SQLVal) + v, ok := node.(*sqlparser.Literal) if !ok { node.Format(buf) return diff --git a/go/mysql/conn.go b/go/mysql/conn.go index adf44dac897..81d7c64cec3 100644 --- a/go/mysql/conn.go +++ b/go/mysql/conn.go @@ -908,8 +908,8 @@ func (c *Conn) handleNextCommand(handler Handler) error { paramsCount := uint16(0) _ = sqlparser.Walk(func(node sqlparser.SQLNode) (bool, error) { switch node := node.(type) { - case *sqlparser.SQLVal: - if strings.HasPrefix(string(node.Val), ":v") { + case sqlparser.Argument: + if strings.HasPrefix(string(node), ":v") { paramsCount++ } } diff --git a/go/vt/sqlparser/analyzer.go b/go/vt/sqlparser/analyzer.go index 03b7d196b1b..ed4c9df4305 100644 --- a/go/vt/sqlparser/analyzer.go +++ b/go/vt/sqlparser/analyzer.go @@ -319,9 +319,11 @@ func IsColName(node Expr) bool { // NULL is not considered to be a value. func IsValue(node Expr) bool { switch v := node.(type) { - case *SQLVal: + case Argument: + return true + case *Literal: switch v.Type { - case StrVal, HexVal, IntVal, ValArg: + case StrVal, HexVal, IntVal: return true } } @@ -358,10 +360,10 @@ func IsSimpleTuple(node Expr) bool { // NewPlanValue builds a sqltypes.PlanValue from an Expr. func NewPlanValue(node Expr) (sqltypes.PlanValue, error) { switch node := node.(type) { - case *SQLVal: + case Argument: + return sqltypes.PlanValue{Key: string(node[1:])}, nil + case *Literal: switch node.Type { - case ValArg: - return sqltypes.PlanValue{Key: string(node.Val[1:])}, nil case IntVal: n, err := sqltypes.NewIntegral(string(node.Val)) if err != nil { diff --git a/go/vt/sqlparser/analyzer_test.go b/go/vt/sqlparser/analyzer_test.go index 317724cdbf5..d26d7e1d3f3 100644 --- a/go/vt/sqlparser/analyzer_test.go +++ b/go/vt/sqlparser/analyzer_test.go @@ -241,7 +241,7 @@ func TestIsColName(t *testing.T) { in: &ColName{}, out: true, }, { - in: newHexVal(""), + in: newHexLiteral(""), }} for _, tc := range testcases { out := IsColName(tc.in) @@ -256,32 +256,35 @@ func TestIsValue(t *testing.T) { in Expr out bool }{{ - in: newStrVal("aa"), + in: newStrLiteral("aa"), out: true, }, { - in: newHexVal("3131"), + in: newHexLiteral("3131"), out: true, }, { - in: newIntVal("1"), + in: newIntLiteral("1"), out: true, }, { - in: newValArg(":a"), + in: newArgument(":a"), out: true, }, { in: &NullVal{}, out: false, }} for _, tc := range testcases { - out := IsValue(tc.in) - if out != tc.out { - t.Errorf("IsValue(%T): %v, want %v", tc.in, out, tc.out) - } - if tc.out { - // NewPlanValue should not fail for valid values. - if _, err := NewPlanValue(tc.in); err != nil { - t.Error(err) + t.Run(String(tc.in), func(t *testing.T) { + out := IsValue(tc.in) + if out != tc.out { + t.Errorf("IsValue(%T): %v, want %v", tc.in, out, tc.out) } - } + if tc.out { + // NewPlanValue should not fail for valid values. + if _, err := NewPlanValue(tc.in); err != nil { + t.Error(err) + } + } + + }) } } @@ -293,7 +296,7 @@ func TestIsNull(t *testing.T) { in: &NullVal{}, out: true, }, { - in: newStrVal(""), + in: newStrLiteral(""), }} for _, tc := range testcases { out := IsNull(tc.in) @@ -308,7 +311,7 @@ func TestIsSimpleTuple(t *testing.T) { in Expr out bool }{{ - in: ValTuple{newStrVal("aa")}, + in: ValTuple{newStrLiteral("aa")}, out: true, }, { in: ValTuple{&ColName{}}, @@ -338,43 +341,40 @@ func TestNewPlanValue(t *testing.T) { out sqltypes.PlanValue err string }{{ - in: &SQLVal{ - Type: ValArg, - Val: []byte(":valarg"), - }, + in: Argument(":valarg"), out: sqltypes.PlanValue{Key: "valarg"}, }, { - in: &SQLVal{ + in: &Literal{ Type: IntVal, Val: []byte("10"), }, out: sqltypes.PlanValue{Value: sqltypes.NewInt64(10)}, }, { - in: &SQLVal{ + in: &Literal{ Type: IntVal, Val: []byte("1111111111111111111111111111111111111111"), }, err: "value out of range", }, { - in: &SQLVal{ + in: &Literal{ Type: StrVal, Val: []byte("strval"), }, out: sqltypes.PlanValue{Value: sqltypes.NewVarBinary("strval")}, }, { - in: &SQLVal{ + in: &Literal{ Type: BitVal, Val: []byte("01100001"), }, err: "expression is too complex", }, { - in: &SQLVal{ + in: &Literal{ Type: HexVal, Val: []byte("3131"), }, out: sqltypes.PlanValue{Value: sqltypes.NewVarBinary("11")}, }, { - in: &SQLVal{ + in: &Literal{ Type: HexVal, Val: []byte("313"), }, @@ -384,11 +384,8 @@ func TestNewPlanValue(t *testing.T) { out: sqltypes.PlanValue{ListKey: "list"}, }, { in: ValTuple{ - &SQLVal{ - Type: ValArg, - Val: []byte(":valarg"), - }, - &SQLVal{ + Argument(":valarg"), + &Literal{ Type: StrVal, Val: []byte("strval"), }, @@ -409,7 +406,7 @@ func TestNewPlanValue(t *testing.T) { in: &NullVal{}, out: sqltypes.PlanValue{}, }, { - in: &SQLVal{ + in: &Literal{ Type: FloatVal, Val: []byte("2.1"), }, @@ -417,7 +414,7 @@ func TestNewPlanValue(t *testing.T) { }, { in: &UnaryExpr{ Operator: Latin1Str, - Expr: &SQLVal{ + Expr: &Literal{ Type: StrVal, Val: []byte("strval"), }, @@ -426,7 +423,7 @@ func TestNewPlanValue(t *testing.T) { }, { in: &UnaryExpr{ Operator: UBinaryStr, - Expr: &SQLVal{ + Expr: &Literal{ Type: StrVal, Val: []byte("strval"), }, @@ -435,7 +432,7 @@ func TestNewPlanValue(t *testing.T) { }, { in: &UnaryExpr{ Operator: Utf8mb4Str, - Expr: &SQLVal{ + Expr: &Literal{ Type: StrVal, Val: []byte("strval"), }, @@ -444,7 +441,7 @@ func TestNewPlanValue(t *testing.T) { }, { in: &UnaryExpr{ Operator: Utf8Str, - Expr: &SQLVal{ + Expr: &Literal{ Type: StrVal, Val: []byte("strval"), }, @@ -453,7 +450,7 @@ func TestNewPlanValue(t *testing.T) { }, { in: &UnaryExpr{ Operator: MinusStr, - Expr: &SQLVal{ + Expr: &Literal{ Type: FloatVal, Val: []byte("2.1"), }, @@ -482,18 +479,18 @@ var mustMatch = utils.MustMatchFn( []string{".Conn"}, // ignored fields ) -func newStrVal(in string) *SQLVal { - return NewStrVal([]byte(in)) +func newStrLiteral(in string) *Literal { + return NewStrLiteral([]byte(in)) } -func newIntVal(in string) *SQLVal { - return NewIntVal([]byte(in)) +func newIntLiteral(in string) *Literal { + return NewIntLiteral([]byte(in)) } -func newHexVal(in string) *SQLVal { - return NewHexVal([]byte(in)) +func newHexLiteral(in string) *Literal { + return NewHexLiteral([]byte(in)) } -func newValArg(in string) *SQLVal { - return NewValArg([]byte(in)) +func newArgument(in string) Expr { + return NewArgument([]byte(in)) } diff --git a/go/vt/sqlparser/ast.go b/go/vt/sqlparser/ast.go index 60a699d993a..1f55f7149e2 100644 --- a/go/vt/sqlparser/ast.go +++ b/go/vt/sqlparser/ast.go @@ -353,13 +353,13 @@ type ColumnType struct { Autoincrement BoolVal Default Expr OnUpdate Expr - Comment *SQLVal + Comment *Literal // Numeric field options - Length *SQLVal + Length *Literal Unsigned BoolVal Zerofill BoolVal - Scale *SQLVal + Scale *Literal // Text field options Charset string @@ -614,12 +614,15 @@ type ( Subquery *Subquery } - // SQLVal represents a single value. - SQLVal struct { + // Literal represents a fixed value. + Literal struct { Type ValType Val []byte } + // Argument represents bindvariable expression + Argument []byte + // NullVal represents a NULL value. NullVal struct{} @@ -711,7 +714,7 @@ type ( // In this case StrVal will be set instead of Name. SubstrExpr struct { Name *ColName - StrVal *SQLVal + StrVal *Literal From Expr To Expr } @@ -771,7 +774,8 @@ func (*ComparisonExpr) iExpr() {} func (*RangeCond) iExpr() {} func (*IsExpr) iExpr() {} func (*ExistsExpr) iExpr() {} -func (*SQLVal) iExpr() {} +func (*Literal) iExpr() {} +func (Argument) iExpr() {} func (*NullVal) iExpr() {} func (BoolVal) iExpr() {} func (*ColName) iExpr() {} @@ -805,8 +809,8 @@ func (ListArg) iColTuple() {} // ConvertType represents the type in call to CONVERT(expr, type) type ConvertType struct { Type string - Length *SQLVal - Scale *SQLVal + Length *Literal + Scale *Literal Operator string Charset string } @@ -1579,7 +1583,7 @@ func (node *ExistsExpr) Format(buf *TrackedBuffer) { } // Format formats the node. -func (node *SQLVal) Format(buf *TrackedBuffer) { +func (node *Literal) Format(buf *TrackedBuffer) { switch node.Type { case StrVal: sqltypes.MakeTrusted(sqltypes.VarBinary, node.Val).EncodeSQL(buf) @@ -1589,13 +1593,16 @@ func (node *SQLVal) Format(buf *TrackedBuffer) { buf.astPrintf(node, "X'%s'", node.Val) case BitVal: buf.astPrintf(node, "B'%s'", node.Val) - case ValArg: - buf.WriteArg(string(node.Val)) default: panic("unexpected") } } +// Format formats the node. +func (node Argument) Format(buf *TrackedBuffer) { + buf.WriteArg(string(node)) +} + // Format formats the node. func (node *NullVal) Format(buf *TrackedBuffer) { buf.astPrintf(node, "null") @@ -1864,8 +1871,8 @@ func (node *SetExpr) Format(buf *TrackedBuffer) { case node.Name.EqualString("charset") || node.Name.EqualString("names"): buf.astPrintf(node, "%s %v", node.Name.String(), node.Expr) case node.Name.EqualString(TransactionStr): - sqlVal := node.Expr.(*SQLVal) - buf.astPrintf(node, "%s %s", node.Name.String(), strings.ToLower(string(sqlVal.Val))) + literal := node.Expr.(*Literal) + buf.astPrintf(node, "%s %s", node.Name.String(), strings.ToLower(string(literal.Val))) default: buf.astPrintf(node, "%v = %v", node.Name, node.Expr) } diff --git a/go/vt/sqlparser/ast_funcs.go b/go/vt/sqlparser/ast_funcs.go index 2e606d3814e..33517ad332a 100644 --- a/go/vt/sqlparser/ast_funcs.go +++ b/go/vt/sqlparser/ast_funcs.go @@ -76,20 +76,20 @@ func Append(buf *strings.Builder, node SQLNode) { // IndexColumn describes a column in an index definition with optional length type IndexColumn struct { Column ColIdent - Length *SQLVal + Length *Literal } // LengthScaleOption is used for types that have an optional length // and scale type LengthScaleOption struct { - Length *SQLVal - Scale *SQLVal + Length *Literal + Scale *Literal } // IndexOption is used for trailing options for indexes: COMMENT, KEY_BLOCK_SIZE, USING type IndexOption struct { Name string - Value *SQLVal + Value *Literal Using string } @@ -129,7 +129,7 @@ type ShowTablesOpt struct { Filter *ShowFilter } -// ValType specifies the type for SQLVal. +// ValType specifies the type for Literal. type ValType int // These are the possible Valtype values. @@ -143,7 +143,6 @@ const ( FloatVal HexNum HexVal - ValArg BitVal ) @@ -388,7 +387,7 @@ func replaceExpr(from, to Expr) func(cursor *Cursor) bool { cursor.Replace(to) } switch cursor.Node().(type) { - case *ExistsExpr, *SQLVal, *Subquery, *ValuesFuncExpr, *Default: + case *ExistsExpr, *Literal, *Subquery, *ValuesFuncExpr, *Default: return false } @@ -399,12 +398,12 @@ func replaceExpr(from, to Expr) func(cursor *Cursor) bool { // IsImpossible returns true if the comparison in the expression can never evaluate to true. // Note that this is not currently exhaustive to ALL impossible comparisons. func (node *ComparisonExpr) IsImpossible() bool { - var left, right *SQLVal + var left, right *Literal var ok bool - if left, ok = node.Left.(*SQLVal); !ok { + if left, ok = node.Left.(*Literal); !ok { return false } - if right, ok = node.Right.(*SQLVal); !ok { + if right, ok = node.Right.(*Literal); !ok { return false } if node.Operator == NotEqualStr && left.Type == right.Type { @@ -422,43 +421,43 @@ func (node *ComparisonExpr) IsImpossible() bool { return false } -// NewStrVal builds a new StrVal. -func NewStrVal(in []byte) *SQLVal { - return &SQLVal{Type: StrVal, Val: in} +// NewStrLiteral builds a new StrVal. +func NewStrLiteral(in []byte) *Literal { + return &Literal{Type: StrVal, Val: in} } -// NewIntVal builds a new IntVal. -func NewIntVal(in []byte) *SQLVal { - return &SQLVal{Type: IntVal, Val: in} +// NewIntLiteral builds a new IntVal. +func NewIntLiteral(in []byte) *Literal { + return &Literal{Type: IntVal, Val: in} } -// NewFloatVal builds a new FloatVal. -func NewFloatVal(in []byte) *SQLVal { - return &SQLVal{Type: FloatVal, Val: in} +// NewFloatLiteral builds a new FloatVal. +func NewFloatLiteral(in []byte) *Literal { + return &Literal{Type: FloatVal, Val: in} } -// NewHexNum builds a new HexNum. -func NewHexNum(in []byte) *SQLVal { - return &SQLVal{Type: HexNum, Val: in} +// NewHexNumLiteral builds a new HexNum. +func NewHexNumLiteral(in []byte) *Literal { + return &Literal{Type: HexNum, Val: in} } -// NewHexVal builds a new HexVal. -func NewHexVal(in []byte) *SQLVal { - return &SQLVal{Type: HexVal, Val: in} +// NewHexLiteral builds a new HexVal. +func NewHexLiteral(in []byte) *Literal { + return &Literal{Type: HexVal, Val: in} } -// NewBitVal builds a new BitVal containing a bit literal. -func NewBitVal(in []byte) *SQLVal { - return &SQLVal{Type: BitVal, Val: in} +// NewBitLiteral builds a new BitVal containing a bit literal. +func NewBitLiteral(in []byte) *Literal { + return &Literal{Type: BitVal, Val: in} } -// NewValArg builds a new ValArg. -func NewValArg(in []byte) *SQLVal { - return &SQLVal{Type: ValArg, Val: in} +// NewArgument builds a new ValArg. +func NewArgument(in []byte) Argument { + return in } // HexDecode decodes the hexval into bytes. -func (node *SQLVal) HexDecode() ([]byte, error) { +func (node *Literal) HexDecode() ([]byte, error) { dst := make([]byte, hex.DecodedLen(len([]byte(node.Val)))) _, err := hex.Decode(dst, []byte(node.Val)) if err != nil { diff --git a/go/vt/sqlparser/ast_test.go b/go/vt/sqlparser/ast_test.go index 968492bcece..de6ab06c0ce 100644 --- a/go/vt/sqlparser/ast_test.go +++ b/go/vt/sqlparser/ast_test.go @@ -264,7 +264,7 @@ func TestSetAutocommitON(t *testing.T) { e := s.Exprs[0] switch v := e.Expr.(type) { - case *SQLVal: + case *Literal: if v.Type != StrVal { t.Errorf("SET statement value is not StrVal: %T", v) } @@ -273,7 +273,7 @@ func TestSetAutocommitON(t *testing.T) { t.Errorf("SET statement value want: on, got: %s", v.Val) } default: - t.Errorf("SET statement expression is not SQLVal: %T", e.Expr) + t.Errorf("SET statement expression is not Literal: %T", e.Expr) } stmt, err = Parse("SET @@session.autocommit=ON") @@ -289,7 +289,7 @@ func TestSetAutocommitON(t *testing.T) { e = s.Exprs[0] switch v := e.Expr.(type) { - case *SQLVal: + case *Literal: if v.Type != StrVal { t.Errorf("SET statement value is not StrVal: %T", v) } @@ -298,7 +298,7 @@ func TestSetAutocommitON(t *testing.T) { t.Errorf("SET statement value want: on, got: %s", v.Val) } default: - t.Errorf("SET statement expression is not SQLVal: %T", e.Expr) + t.Errorf("SET statement expression is not Literal: %T", e.Expr) } } @@ -316,7 +316,7 @@ func TestSetAutocommitOFF(t *testing.T) { e := s.Exprs[0] switch v := e.Expr.(type) { - case *SQLVal: + case *Literal: if v.Type != StrVal { t.Errorf("SET statement value is not StrVal: %T", v) } @@ -325,7 +325,7 @@ func TestSetAutocommitOFF(t *testing.T) { t.Errorf("SET statement value want: on, got: %s", v.Val) } default: - t.Errorf("SET statement expression is not SQLVal: %T", e.Expr) + t.Errorf("SET statement expression is not Literal: %T", e.Expr) } stmt, err = Parse("SET @@session.autocommit=OFF") @@ -341,7 +341,7 @@ func TestSetAutocommitOFF(t *testing.T) { e = s.Exprs[0] switch v := e.Expr.(type) { - case *SQLVal: + case *Literal: if v.Type != StrVal { t.Errorf("SET statement value is not StrVal: %T", v) } @@ -350,7 +350,7 @@ func TestSetAutocommitOFF(t *testing.T) { t.Errorf("SET statement value want: on, got: %s", v.Val) } default: - t.Errorf("SET statement expression is not SQLVal: %T", e.Expr) + t.Errorf("SET statement expression is not Literal: %T", e.Expr) } } @@ -390,8 +390,8 @@ func TestIsAggregate(t *testing.T) { func TestIsImpossible(t *testing.T) { f := ComparisonExpr{ Operator: NotEqualStr, - Left: newIntVal("1"), - Right: newIntVal("1"), + Left: newIntLiteral("1"), + Right: newIntLiteral("1"), } if !f.IsImpossible() { t.Error("IsImpossible: false, want true") @@ -399,8 +399,8 @@ func TestIsImpossible(t *testing.T) { f = ComparisonExpr{ Operator: EqualStr, - Left: newIntVal("1"), - Right: newIntVal("1"), + Left: newIntLiteral("1"), + Right: newIntLiteral("1"), } if f.IsImpossible() { t.Error("IsImpossible: true, want false") @@ -408,8 +408,8 @@ func TestIsImpossible(t *testing.T) { f = ComparisonExpr{ Operator: NotEqualStr, - Left: newIntVal("1"), - Right: newIntVal("2"), + Left: newIntLiteral("1"), + Right: newIntLiteral("2"), } if f.IsImpossible() { t.Error("IsImpossible: true, want false") @@ -538,7 +538,7 @@ func TestReplaceExpr(t *testing.T) { in: "select * from t where case a when b then c when d then c else (select a from b) end", out: "case a when b then c when d then c else :a end", }} - to := NewValArg([]byte(":a")) + to := NewArgument([]byte(":a")) for _, tcase := range tcases { tree, err := Parse(tcase.in) if err != nil { @@ -668,7 +668,7 @@ func TestHexDecode(t *testing.T) { out: "encoding/hex: odd length hex string", }} for _, tc := range testcase { - out, err := newHexVal(tc.in).HexDecode() + out, err := newHexLiteral(tc.in).HexDecode() if err != nil { if err.Error() != tc.out { t.Errorf("Decode(%q): %v, want %s", tc.in, err, tc.out) diff --git a/go/vt/sqlparser/expression_converter.go b/go/vt/sqlparser/expression_converter.go index 695ad24fe97..10974e7bb10 100644 --- a/go/vt/sqlparser/expression_converter.go +++ b/go/vt/sqlparser/expression_converter.go @@ -28,14 +28,14 @@ var ErrExprNotSupported = fmt.Errorf("Expr Not Supported") //Convert converts between AST expressions and executable expressions func Convert(e Expr) (evalengine.Expr, error) { switch node := e.(type) { - case *SQLVal: + case Argument: + return evalengine.NewBindVar(string(node[1:])), nil + case *Literal: switch node.Type { case IntVal: return evalengine.NewLiteralIntFromBytes(node.Val) case FloatVal: return evalengine.NewLiteralFloat(node.Val) - case ValArg: - return evalengine.NewBindVar(string(node.Val[1:])), nil case StrVal: return evalengine.NewLiteralString(node.Val), nil } diff --git a/go/vt/sqlparser/expression_rewriting.go b/go/vt/sqlparser/expression_rewriting.go index 0276e73c9f9..1ea4bae0db4 100644 --- a/go/vt/sqlparser/expression_rewriting.go +++ b/go/vt/sqlparser/expression_rewriting.go @@ -215,6 +215,6 @@ func (er *expressionRewriter) didAnythingChange() bool { return len(er.bindVars) > 0 } -func bindVarExpression(name string) *SQLVal { - return NewValArg([]byte(":" + name)) +func bindVarExpression(name string) Expr { + return NewArgument([]byte(":" + name)) } diff --git a/go/vt/sqlparser/normalizer.go b/go/vt/sqlparser/normalizer.go index 23660fbec9c..89b31f8deac 100644 --- a/go/vt/sqlparser/normalizer.go +++ b/go/vt/sqlparser/normalizer.go @@ -33,11 +33,10 @@ import ( // treated as distinct. func Normalize(stmt Statement, bindVars map[string]*querypb.BindVariable, prefix string) { nz := newNormalizer(stmt, bindVars, prefix) - _ = Walk(nz.WalkStatement, stmt) + Rewrite(stmt, nz.WalkStatement, nil) } type normalizer struct { - stmt Statement bindVars map[string]*querypb.BindVariable prefix string reserved map[string]struct{} @@ -47,7 +46,6 @@ type normalizer struct { func newNormalizer(stmt Statement, bindVars map[string]*querypb.BindVariable, prefix string) *normalizer { return &normalizer{ - stmt: stmt, bindVars: bindVars, prefix: prefix, reserved: GetBindvars(stmt), @@ -59,57 +57,57 @@ func newNormalizer(stmt Statement, bindVars map[string]*querypb.BindVariable, pr // WalkStatement is the top level walk function. // If it encounters a Select, it switches to a mode // where variables are deduped. -func (nz *normalizer) WalkStatement(node SQLNode) (bool, error) { - switch node := node.(type) { +func (nz *normalizer) WalkStatement(cursor *Cursor) bool { + switch node := cursor.Node().(type) { // no need to normalize the statement types case *Set, *Show, *Begin, *Commit, *Rollback, *Savepoint, *SetTransaction, *DDL, *SRollback, *Release, *OtherAdmin, *OtherRead: - return false, nil + return false case *Select: - _ = Walk(nz.WalkSelect, node) + Rewrite(node, nz.WalkSelect, nil) // Don't continue - return false, nil - case *SQLVal: - nz.convertSQLVal(node) + return false + case *Literal: + nz.convertLiteral(node, cursor) case *ComparisonExpr: nz.convertComparison(node) case *ColName, TableName: - // Common node types that never contain SQLVals or ListArgs but create a lot of object + // Common node types that never contain Literal or ListArgs but create a lot of object // allocations. - return false, nil + return false case *ConvertType: // we should not rewrite the type description - return false, nil + return false } - return true, nil + return true } // WalkSelect normalizes the AST in Select mode. -func (nz *normalizer) WalkSelect(node SQLNode) (bool, error) { - switch node := node.(type) { - case *SQLVal: - nz.convertSQLValDedup(node) +func (nz *normalizer) WalkSelect(cursor *Cursor) bool { + switch node := cursor.Node().(type) { + case *Literal: + nz.convertLiteralDedup(node, cursor) case *ComparisonExpr: nz.convertComparison(node) case *ColName, TableName: - // Common node types that never contain SQLVals or ListArgs but create a lot of object + // Common node types that never contain Literals or ListArgs but create a lot of object // allocations. - return false, nil + return false case OrderBy, GroupBy: // do not make a bind var for order by column_position - return false, nil + return false case *ConvertType: // we should not rewrite the type description - return false, nil + return false } - return true, nil + return true } -func (nz *normalizer) convertSQLValDedup(node *SQLVal) { +func (nz *normalizer) convertLiteralDedup(node *Literal, cursor *Cursor) { // If value is too long, don't dedup. // Such values are most likely not for vindexes. // We save a lot of CPU because we avoid building // the key for them. if len(node.Val) > 256 { - nz.convertSQLVal(node) + nz.convertLiteral(node, cursor) return } @@ -138,12 +136,11 @@ func (nz *normalizer) convertSQLValDedup(node *SQLVal) { } // Modify the AST node to a bindvar. - node.Type = ValArg - node.Val = append([]byte(":"), bvname...) + cursor.Replace(NewArgument([]byte(":" + bvname))) } -// convertSQLVal converts an SQLVal without the dedup. -func (nz *normalizer) convertSQLVal(node *SQLVal) { +// convertLiteral converts an Literal without the dedup. +func (nz *normalizer) convertLiteral(node *Literal, cursor *Cursor) { bval := nz.sqlToBindvar(node) if bval == nil { return @@ -152,8 +149,7 @@ func (nz *normalizer) convertSQLVal(node *SQLVal) { bvname := nz.newName() nz.bindVars[bvname] = bval - node.Type = ValArg - node.Val = append([]byte(":"), bvname...) + cursor.Replace(NewArgument([]byte(":" + bvname))) } // convertComparison attempts to convert IN clauses to @@ -191,7 +187,7 @@ func (nz *normalizer) convertComparison(node *ComparisonExpr) { } func (nz *normalizer) sqlToBindvar(node SQLNode) *querypb.BindVariable { - if node, ok := node.(*SQLVal); ok { + if node, ok := node.(*Literal); ok { var v sqltypes.Value var err error switch node.Type { @@ -231,13 +227,11 @@ func GetBindvars(stmt Statement) map[string]struct{} { _ = Walk(func(node SQLNode) (kontinue bool, err error) { switch node := node.(type) { case *ColName, TableName: - // Common node types that never contain SQLVals or ListArgs but create a lot of object + // Common node types that never contain expressions but create a lot of object // allocations. return false, nil - case *SQLVal: - if node.Type == ValArg { - bindvars[string(node.Val[1:])] = struct{}{} - } + case Argument: + bindvars[string(node[1:])] = struct{}{} case ListArg: bindvars[string(node[2:])] = struct{}{} } diff --git a/go/vt/sqlparser/random_expr.go b/go/vt/sqlparser/random_expr.go index 783d96c6fe5..45757f2bb23 100644 --- a/go/vt/sqlparser/random_expr.go +++ b/go/vt/sqlparser/random_expr.go @@ -126,13 +126,13 @@ func (g *generator) randomBool() bool { func (g *generator) intLiteral() Expr { t := fmt.Sprintf("%d", g.r.Intn(1000)-g.r.Intn((1000))) - return NewIntVal([]byte(t)) + return NewIntLiteral([]byte(t)) } var words = []string{"ox", "ant", "ape", "asp", "bat", "bee", "boa", "bug", "cat", "cod", "cow", "cub", "doe", "dog", "eel", "eft", "elf", "elk", "emu", "ewe", "fly", "fox", "gar", "gnu", "hen", "hog", "imp", "jay", "kid", "kit", "koi", "lab", "man", "owl", "pig", "pug", "pup", "ram", "rat", "ray", "yak", "bass", "bear", "bird", "boar", "buck", "bull", "calf", "chow", "clam", "colt", "crab", "crow", "dane", "deer", "dodo", "dory", "dove", "drum", "duck", "fawn", "fish", "flea", "foal", "fowl", "frog", "gnat", "goat", "grub", "gull", "hare", "hawk", "ibex", "joey", "kite", "kiwi", "lamb", "lark", "lion", "loon", "lynx", "mako", "mink", "mite", "mole", "moth", "mule", "mutt", "newt", "orca", "oryx", "pika", "pony", "puma", "seal", "shad", "slug", "sole", "stag", "stud", "swan", "tahr", "teal", "tick", "toad", "tuna", "wasp", "wolf", "worm", "wren", "yeti", "adder", "akita", "alien", "aphid", "bison", "boxer", "bream", "bunny", "burro", "camel", "chimp", "civet", "cobra", "coral", "corgi", "crane", "dingo", "drake", "eagle", "egret", "filly", "finch", "gator", "gecko", "ghost", "ghoul", "goose", "guppy", "heron", "hippo", "horse", "hound", "husky", "hyena", "koala", "krill", "leech", "lemur", "liger", "llama", "louse", "macaw", "midge", "molly", "moose", "moray", "mouse", "panda", "perch", "prawn", "quail", "racer", "raven", "rhino", "robin", "satyr", "shark", "sheep", "shrew", "skink", "skunk", "sloth", "snail", "snake", "snipe", "squid", "stork", "swift", "swine", "tapir", "tetra", "tiger", "troll", "trout", "viper", "wahoo", "whale", "zebra", "alpaca", "amoeba", "baboon", "badger", "beagle", "bedbug", "beetle", "bengal", "bobcat", "caiman", "cattle", "cicada", "collie", "condor", "cougar", "coyote", "dassie", "donkey", "dragon", "earwig", "falcon", "feline", "ferret", "gannet", "gibbon", "glider", "goblin", "gopher", "grouse", "guinea", "hermit", "hornet", "iguana", "impala", "insect", "jackal", "jaguar", "jennet", "kitten", "kodiak", "lizard", "locust", "maggot", "magpie", "mammal", "mantis", "marlin", "marmot", "marten", "martin", "mayfly", "minnow", "monkey", "mullet", "muskox", "ocelot", "oriole", "osprey", "oyster", "parrot", "pigeon", "piglet", "poodle", "possum", "python", "quagga", "rabbit", "raptor", "rodent", "roughy", "salmon", "sawfly", "serval", "shiner", "shrimp", "spider", "sponge", "tarpon", "thrush", "tomcat", "toucan", "turkey", "turtle", "urchin", "vervet", "walrus", "weasel", "weevil", "wombat", "anchovy", "anemone", "bluejay", "buffalo", "bulldog", "buzzard", "caribou", "catfish", "chamois", "cheetah", "chicken", "chigger", "cowbird", "crappie", "crawdad", "cricket", "dogfish", "dolphin", "firefly", "garfish", "gazelle", "gelding", "giraffe", "gobbler", "gorilla", "goshawk", "grackle", "griffon", "grizzly", "grouper", "haddock", "hagfish", "halibut", "hamster", "herring", "jackass", "javelin", "jawfish", "jaybird", "katydid", "ladybug", "lamprey", "lemming", "leopard", "lioness", "lobster", "macaque", "mallard", "mammoth", "manatee", "mastiff", "meerkat", "mollusk", "monarch", "mongrel", "monitor", "monster", "mudfish", "muskrat", "mustang", "narwhal", "oarfish", "octopus", "opossum", "ostrich", "panther", "peacock", "pegasus", "pelican", "penguin", "phoenix", "piranha", "polecat", "primate", "quetzal", "raccoon", "rattler", "redbird", "redfish", "reptile", "rooster", "sawfish", "sculpin", "seagull", "skylark", "snapper", "spaniel", "sparrow", "sunbeam", "sunbird", "sunfish", "tadpole", "termite", "terrier", "unicorn", "vulture", "wallaby", "walleye", "warthog", "whippet", "wildcat", "aardvark", "airedale", "albacore", "anteater", "antelope", "arachnid", "barnacle", "basilisk", "blowfish", "bluebird", "bluegill", "bonefish", "bullfrog", "cardinal", "chipmunk", "cockatoo", "crayfish", "dinosaur", "doberman", "duckling", "elephant", "escargot", "flamingo", "flounder", "foxhound", "glowworm", "goldfish", "grubworm", "hedgehog", "honeybee", "hookworm", "humpback", "kangaroo", "killdeer", "kingfish", "labrador", "lacewing", "ladybird", "lionfish", "longhorn", "mackerel", "malamute", "marmoset", "mastodon", "moccasin", "mongoose", "monkfish", "mosquito", "pangolin", "parakeet", "pheasant", "pipefish", "platypus", "polliwog", "porpoise", "reindeer", "ringtail", "sailfish", "scorpion", "seahorse", "seasnail", "sheepdog", "shepherd", "silkworm", "squirrel", "stallion", "starfish", "starling", "stingray", "stinkbug", "sturgeon", "terrapin", "titmouse", "tortoise", "treefrog", "werewolf", "woodcock"} func (g *generator) stringLiteral() Expr { - return NewStrVal([]byte(g.randomOfS(words))) + return NewStrLiteral([]byte(g.randomOfS(words))) } func (g *generator) stringExpr() Expr { diff --git a/go/vt/sqlparser/rewriter.go b/go/vt/sqlparser/rewriter.go index e81701cc00f..43c2aff3acd 100644 --- a/go/vt/sqlparser/rewriter.go +++ b/go/vt/sqlparser/rewriter.go @@ -103,7 +103,7 @@ func replaceColumnTypeAutoincrement(newNode, parent SQLNode) { } func replaceColumnTypeComment(newNode, parent SQLNode) { - parent.(*ColumnType).Comment = newNode.(*SQLVal) + parent.(*ColumnType).Comment = newNode.(*Literal) } func replaceColumnTypeDefault(newNode, parent SQLNode) { @@ -111,7 +111,7 @@ func replaceColumnTypeDefault(newNode, parent SQLNode) { } func replaceColumnTypeLength(newNode, parent SQLNode) { - parent.(*ColumnType).Length = newNode.(*SQLVal) + parent.(*ColumnType).Length = newNode.(*Literal) } func replaceColumnTypeNotNull(newNode, parent SQLNode) { @@ -123,7 +123,7 @@ func replaceColumnTypeOnUpdate(newNode, parent SQLNode) { } func replaceColumnTypeScale(newNode, parent SQLNode) { - parent.(*ColumnType).Scale = newNode.(*SQLVal) + parent.(*ColumnType).Scale = newNode.(*Literal) } func replaceColumnTypeUnsigned(newNode, parent SQLNode) { @@ -169,11 +169,11 @@ func replaceConvertExprType(newNode, parent SQLNode) { } func replaceConvertTypeLength(newNode, parent SQLNode) { - parent.(*ConvertType).Length = newNode.(*SQLVal) + parent.(*ConvertType).Length = newNode.(*Literal) } func replaceConvertTypeScale(newNode, parent SQLNode) { - parent.(*ConvertType).Scale = newNode.(*SQLVal) + parent.(*ConvertType).Scale = newNode.(*Literal) } func replaceConvertUsingExprExpr(newNode, parent SQLNode) { @@ -654,7 +654,7 @@ func replaceSubstrExprName(newNode, parent SQLNode) { } func replaceSubstrExprStrVal(newNode, parent SQLNode) { - parent.(*SubstrExpr).StrVal = newNode.(*SQLVal) + parent.(*SubstrExpr).StrVal = newNode.(*Literal) } func replaceSubstrExprTo(newNode, parent SQLNode) { @@ -910,6 +910,8 @@ func (a *application) apply(parent, node SQLNode, replacer replacerFunc) { a.apply(node, n.Left, replaceAndExprLeft) a.apply(node, n.Right, replaceAndExprRight) + case Argument: + case *AutoIncSpec: a.apply(node, n.Column, replaceAutoIncSpecColumn) a.apply(node, n.Sequence, replaceAutoIncSpecSequence) @@ -1103,6 +1105,8 @@ func (a *application) apply(parent, node SQLNode, replacer replacerFunc) { case ListArg: + case *Literal: + case *MatchExpr: a.apply(node, n.Columns, replaceMatchExprColumns) a.apply(node, n.Expr, replaceMatchExprExpr) @@ -1184,8 +1188,6 @@ func (a *application) apply(parent, node SQLNode, replacer replacerFunc) { case *Rollback: - case *SQLVal: - case *SRollback: a.apply(node, n.Name, replaceSRollbackName) diff --git a/go/vt/sqlparser/sql.go b/go/vt/sqlparser/sql.go index 31a07b7ccbd..665f08b34e1 100644 --- a/go/vt/sqlparser/sql.go +++ b/go/vt/sqlparser/sql.go @@ -65,7 +65,7 @@ type yySymType struct { expr Expr exprs Exprs boolVal BoolVal - sqlVal *SQLVal + literal *Literal colTuple ColTuple values Values valTuple ValTuple @@ -4261,7 +4261,7 @@ yydefault: yyDollar[2].columnType.OnUpdate = yyDollar[5].optVal yyDollar[2].columnType.Autoincrement = yyDollar[6].boolVal yyDollar[2].columnType.KeyOpt = yyDollar[7].colKeyOpt - yyDollar[2].columnType.Comment = yyDollar[8].sqlVal + yyDollar[2].columnType.Comment = yyDollar[8].literal yyVAL.columnDefinition = &ColumnDefinition{Name: yyDollar[1].colIdent, Type: yyDollar[2].columnType} } case 98: @@ -4277,7 +4277,7 @@ yydefault: //line sql.y:778 { yyVAL.columnType = yyDollar[1].columnType - yyVAL.columnType.Length = yyDollar[2].sqlVal + yyVAL.columnType.Length = yyDollar[2].literal } case 103: yyDollar = yyS[yypt-1 : yypt+1] @@ -4389,19 +4389,19 @@ yydefault: yyDollar = yyS[yypt-2 : yypt+1] //line sql.y:863 { - yyVAL.columnType = ColumnType{Type: string(yyDollar[1].bytes), Length: yyDollar[2].sqlVal} + yyVAL.columnType = ColumnType{Type: string(yyDollar[1].bytes), Length: yyDollar[2].literal} } case 120: yyDollar = yyS[yypt-2 : yypt+1] //line sql.y:867 { - yyVAL.columnType = ColumnType{Type: string(yyDollar[1].bytes), Length: yyDollar[2].sqlVal} + yyVAL.columnType = ColumnType{Type: string(yyDollar[1].bytes), Length: yyDollar[2].literal} } case 121: yyDollar = yyS[yypt-2 : yypt+1] //line sql.y:871 { - yyVAL.columnType = ColumnType{Type: string(yyDollar[1].bytes), Length: yyDollar[2].sqlVal} + yyVAL.columnType = ColumnType{Type: string(yyDollar[1].bytes), Length: yyDollar[2].literal} } case 122: yyDollar = yyS[yypt-1 : yypt+1] @@ -4413,25 +4413,25 @@ yydefault: yyDollar = yyS[yypt-4 : yypt+1] //line sql.y:881 { - yyVAL.columnType = ColumnType{Type: string(yyDollar[1].bytes), Length: yyDollar[2].sqlVal, Charset: yyDollar[3].str, Collate: yyDollar[4].str} + yyVAL.columnType = ColumnType{Type: string(yyDollar[1].bytes), Length: yyDollar[2].literal, Charset: yyDollar[3].str, Collate: yyDollar[4].str} } case 124: yyDollar = yyS[yypt-4 : yypt+1] //line sql.y:885 { - yyVAL.columnType = ColumnType{Type: string(yyDollar[1].bytes), Length: yyDollar[2].sqlVal, Charset: yyDollar[3].str, Collate: yyDollar[4].str} + yyVAL.columnType = ColumnType{Type: string(yyDollar[1].bytes), Length: yyDollar[2].literal, Charset: yyDollar[3].str, Collate: yyDollar[4].str} } case 125: yyDollar = yyS[yypt-2 : yypt+1] //line sql.y:889 { - yyVAL.columnType = ColumnType{Type: string(yyDollar[1].bytes), Length: yyDollar[2].sqlVal} + yyVAL.columnType = ColumnType{Type: string(yyDollar[1].bytes), Length: yyDollar[2].literal} } case 126: yyDollar = yyS[yypt-2 : yypt+1] //line sql.y:893 { - yyVAL.columnType = ColumnType{Type: string(yyDollar[1].bytes), Length: yyDollar[2].sqlVal} + yyVAL.columnType = ColumnType{Type: string(yyDollar[1].bytes), Length: yyDollar[2].literal} } case 127: yyDollar = yyS[yypt-3 : yypt+1] @@ -4564,13 +4564,13 @@ yydefault: yyDollar = yyS[yypt-0 : yypt+1] //line sql.y:988 { - yyVAL.sqlVal = nil + yyVAL.literal = nil } case 149: yyDollar = yyS[yypt-3 : yypt+1] //line sql.y:992 { - yyVAL.sqlVal = NewIntVal(yyDollar[2].bytes) + yyVAL.literal = NewIntLiteral(yyDollar[2].bytes) } case 150: yyDollar = yyS[yypt-0 : yypt+1] @@ -4583,8 +4583,8 @@ yydefault: //line sql.y:1001 { yyVAL.LengthScaleOption = LengthScaleOption{ - Length: NewIntVal(yyDollar[2].bytes), - Scale: NewIntVal(yyDollar[4].bytes), + Length: NewIntLiteral(yyDollar[2].bytes), + Scale: NewIntLiteral(yyDollar[4].bytes), } } case 152: @@ -4598,7 +4598,7 @@ yydefault: //line sql.y:1013 { yyVAL.LengthScaleOption = LengthScaleOption{ - Length: NewIntVal(yyDollar[2].bytes), + Length: NewIntLiteral(yyDollar[2].bytes), } } case 154: @@ -4606,8 +4606,8 @@ yydefault: //line sql.y:1019 { yyVAL.LengthScaleOption = LengthScaleOption{ - Length: NewIntVal(yyDollar[2].bytes), - Scale: NewIntVal(yyDollar[4].bytes), + Length: NewIntLiteral(yyDollar[2].bytes), + Scale: NewIntLiteral(yyDollar[4].bytes), } } case 155: @@ -4758,13 +4758,13 @@ yydefault: yyDollar = yyS[yypt-0 : yypt+1] //line sql.y:1133 { - yyVAL.sqlVal = nil + yyVAL.literal = nil } case 180: yyDollar = yyS[yypt-2 : yypt+1] //line sql.y:1137 { - yyVAL.sqlVal = NewStrVal(yyDollar[2].bytes) + yyVAL.literal = NewStrLiteral(yyDollar[2].bytes) } case 181: yyDollar = yyS[yypt-5 : yypt+1] @@ -4801,13 +4801,13 @@ yydefault: //line sql.y:1167 { // should not be string - yyVAL.indexOption = &IndexOption{Name: string(yyDollar[1].bytes), Value: NewIntVal(yyDollar[3].bytes)} + yyVAL.indexOption = &IndexOption{Name: string(yyDollar[1].bytes), Value: NewIntLiteral(yyDollar[3].bytes)} } case 187: yyDollar = yyS[yypt-2 : yypt+1] //line sql.y:1172 { - yyVAL.indexOption = &IndexOption{Name: string(yyDollar[1].bytes), Value: NewStrVal(yyDollar[2].bytes)} + yyVAL.indexOption = &IndexOption{Name: string(yyDollar[1].bytes), Value: NewStrLiteral(yyDollar[2].bytes)} } case 188: yyDollar = yyS[yypt-0 : yypt+1] @@ -4921,7 +4921,7 @@ yydefault: yyDollar = yyS[yypt-2 : yypt+1] //line sql.y:1264 { - yyVAL.indexColumn = &IndexColumn{Column: yyDollar[1].colIdent, Length: yyDollar[2].sqlVal} + yyVAL.indexColumn = &IndexColumn{Column: yyDollar[1].colIdent, Length: yyDollar[2].literal} } case 207: yyDollar = yyS[yypt-3 : yypt+1] @@ -6750,7 +6750,7 @@ yydefault: yyDollar = yyS[yypt-2 : yypt+1] //line sql.y:2680 { - if num, ok := yyDollar[2].expr.(*SQLVal); ok && num.Type == IntVal { + if num, ok := yyDollar[2].expr.(*Literal); ok && num.Type == IntVal { yyVAL.expr = num } else { yyVAL.expr = &UnaryExpr{Operator: UPlusStr, Expr: yyDollar[2].expr} @@ -6760,13 +6760,13 @@ yydefault: yyDollar = yyS[yypt-2 : yypt+1] //line sql.y:2688 { - if num, ok := yyDollar[2].expr.(*SQLVal); ok && num.Type == IntVal { + if num, ok := yyDollar[2].expr.(*Literal); ok && num.Type == IntVal { // Handle double negative if num.Val[0] == '-' { num.Val = num.Val[1:] yyVAL.expr = num } else { - yyVAL.expr = NewIntVal(append([]byte("-"), num.Val...)) + yyVAL.expr = NewIntLiteral(append([]byte("-"), num.Val...)) } } else { yyVAL.expr = &UnaryExpr{Operator: UMinusStr, Expr: yyDollar[2].expr} @@ -6864,13 +6864,13 @@ yydefault: yyDollar = yyS[yypt-8 : yypt+1] //line sql.y:2778 { - yyVAL.expr = &SubstrExpr{StrVal: NewStrVal(yyDollar[3].bytes), From: yyDollar[5].expr, To: yyDollar[7].expr} + yyVAL.expr = &SubstrExpr{StrVal: NewStrLiteral(yyDollar[3].bytes), From: yyDollar[5].expr, To: yyDollar[7].expr} } case 539: yyDollar = yyS[yypt-8 : yypt+1] //line sql.y:2782 { - yyVAL.expr = &SubstrExpr{StrVal: NewStrVal(yyDollar[3].bytes), From: yyDollar[5].expr, To: yyDollar[7].expr} + yyVAL.expr = &SubstrExpr{StrVal: NewStrLiteral(yyDollar[3].bytes), From: yyDollar[5].expr, To: yyDollar[7].expr} } case 540: yyDollar = yyS[yypt-9 : yypt+1] @@ -7086,19 +7086,19 @@ yydefault: yyDollar = yyS[yypt-2 : yypt+1] //line sql.y:2960 { - yyVAL.convertType = &ConvertType{Type: string(yyDollar[1].bytes), Length: yyDollar[2].sqlVal} + yyVAL.convertType = &ConvertType{Type: string(yyDollar[1].bytes), Length: yyDollar[2].literal} } case 578: yyDollar = yyS[yypt-3 : yypt+1] //line sql.y:2964 { - yyVAL.convertType = &ConvertType{Type: string(yyDollar[1].bytes), Length: yyDollar[2].sqlVal, Charset: yyDollar[3].str, Operator: CharacterSetStr} + yyVAL.convertType = &ConvertType{Type: string(yyDollar[1].bytes), Length: yyDollar[2].literal, Charset: yyDollar[3].str, Operator: CharacterSetStr} } case 579: yyDollar = yyS[yypt-3 : yypt+1] //line sql.y:2968 { - yyVAL.convertType = &ConvertType{Type: string(yyDollar[1].bytes), Length: yyDollar[2].sqlVal, Charset: string(yyDollar[3].colIdent.String())} + yyVAL.convertType = &ConvertType{Type: string(yyDollar[1].bytes), Length: yyDollar[2].literal, Charset: string(yyDollar[3].colIdent.String())} } case 580: yyDollar = yyS[yypt-1 : yypt+1] @@ -7110,7 +7110,7 @@ yydefault: yyDollar = yyS[yypt-2 : yypt+1] //line sql.y:2976 { - yyVAL.convertType = &ConvertType{Type: string(yyDollar[1].bytes), Length: yyDollar[2].sqlVal} + yyVAL.convertType = &ConvertType{Type: string(yyDollar[1].bytes), Length: yyDollar[2].literal} } case 582: yyDollar = yyS[yypt-2 : yypt+1] @@ -7130,7 +7130,7 @@ yydefault: yyDollar = yyS[yypt-2 : yypt+1] //line sql.y:2990 { - yyVAL.convertType = &ConvertType{Type: string(yyDollar[1].bytes), Length: yyDollar[2].sqlVal} + yyVAL.convertType = &ConvertType{Type: string(yyDollar[1].bytes), Length: yyDollar[2].literal} } case 585: yyDollar = yyS[yypt-1 : yypt+1] @@ -7148,7 +7148,7 @@ yydefault: yyDollar = yyS[yypt-2 : yypt+1] //line sql.y:3002 { - yyVAL.convertType = &ConvertType{Type: string(yyDollar[1].bytes), Length: yyDollar[2].sqlVal} + yyVAL.convertType = &ConvertType{Type: string(yyDollar[1].bytes), Length: yyDollar[2].literal} } case 588: yyDollar = yyS[yypt-1 : yypt+1] @@ -7238,43 +7238,43 @@ yydefault: yyDollar = yyS[yypt-1 : yypt+1] //line sql.y:3073 { - yyVAL.expr = NewStrVal(yyDollar[1].bytes) + yyVAL.expr = NewStrLiteral(yyDollar[1].bytes) } case 603: yyDollar = yyS[yypt-1 : yypt+1] //line sql.y:3077 { - yyVAL.expr = NewHexVal(yyDollar[1].bytes) + yyVAL.expr = NewHexLiteral(yyDollar[1].bytes) } case 604: yyDollar = yyS[yypt-1 : yypt+1] //line sql.y:3081 { - yyVAL.expr = NewBitVal(yyDollar[1].bytes) + yyVAL.expr = NewBitLiteral(yyDollar[1].bytes) } case 605: yyDollar = yyS[yypt-1 : yypt+1] //line sql.y:3085 { - yyVAL.expr = NewIntVal(yyDollar[1].bytes) + yyVAL.expr = NewIntLiteral(yyDollar[1].bytes) } case 606: yyDollar = yyS[yypt-1 : yypt+1] //line sql.y:3089 { - yyVAL.expr = NewFloatVal(yyDollar[1].bytes) + yyVAL.expr = NewFloatLiteral(yyDollar[1].bytes) } case 607: yyDollar = yyS[yypt-1 : yypt+1] //line sql.y:3093 { - yyVAL.expr = NewHexNum(yyDollar[1].bytes) + yyVAL.expr = NewHexNumLiteral(yyDollar[1].bytes) } case 608: yyDollar = yyS[yypt-1 : yypt+1] //line sql.y:3097 { - yyVAL.expr = NewValArg(yyDollar[1].bytes) + yyVAL.expr = NewArgument(yyDollar[1].bytes) } case 609: yyDollar = yyS[yypt-1 : yypt+1] @@ -7291,19 +7291,19 @@ yydefault: yylex.Error("expecting value after next") return 1 } - yyVAL.expr = NewIntVal([]byte("1")) + yyVAL.expr = NewIntLiteral([]byte("1")) } case 611: yyDollar = yyS[yypt-2 : yypt+1] //line sql.y:3116 { - yyVAL.expr = NewIntVal(yyDollar[1].bytes) + yyVAL.expr = NewIntLiteral(yyDollar[1].bytes) } case 612: yyDollar = yyS[yypt-2 : yypt+1] //line sql.y:3120 { - yyVAL.expr = NewValArg(yyDollar[1].bytes) + yyVAL.expr = NewArgument(yyDollar[1].bytes) } case 613: yyDollar = yyS[yypt-0 : yypt+1] @@ -7553,13 +7553,13 @@ yydefault: yyDollar = yyS[yypt-3 : yypt+1] //line sql.y:3326 { - yyVAL.setExpr = &SetExpr{Name: yyDollar[1].colIdent, Expr: NewStrVal([]byte("on"))} + yyVAL.setExpr = &SetExpr{Name: yyDollar[1].colIdent, Expr: NewStrLiteral([]byte("on"))} } case 654: yyDollar = yyS[yypt-3 : yypt+1] //line sql.y:3330 { - yyVAL.setExpr = &SetExpr{Name: yyDollar[1].colIdent, Expr: NewStrVal([]byte("off"))} + yyVAL.setExpr = &SetExpr{Name: yyDollar[1].colIdent, Expr: NewStrLiteral([]byte("off"))} } case 655: yyDollar = yyS[yypt-3 : yypt+1] @@ -7590,13 +7590,13 @@ yydefault: yyDollar = yyS[yypt-1 : yypt+1] //line sql.y:3357 { - yyVAL.expr = NewStrVal([]byte(yyDollar[1].colIdent.String())) + yyVAL.expr = NewStrLiteral([]byte(yyDollar[1].colIdent.String())) } case 662: yyDollar = yyS[yypt-1 : yypt+1] //line sql.y:3361 { - yyVAL.expr = NewStrVal(yyDollar[1].bytes) + yyVAL.expr = NewStrLiteral(yyDollar[1].bytes) } case 663: yyDollar = yyS[yypt-1 : yypt+1] diff --git a/go/vt/sqlparser/sql.y b/go/vt/sqlparser/sql.y index 6dd94057720..49b74e5fc1e 100644 --- a/go/vt/sqlparser/sql.y +++ b/go/vt/sqlparser/sql.y @@ -75,7 +75,7 @@ func skipToEnd(yylex interface{}) { expr Expr exprs Exprs boolVal BoolVal - sqlVal *SQLVal + literal *Literal colTuple ColTuple values Values valTuple ValTuple @@ -296,7 +296,7 @@ func skipToEnd(yylex interface{}) { %type convert_type %type column_type %type int_type decimal_type numeric_type time_type char_type spatial_type -%type length_opt column_comment_opt +%type length_opt column_comment_opt %type column_default_opt on_update_opt %type charset_opt collate_opt %type unsigned_opt zero_fill_opt @@ -990,7 +990,7 @@ length_opt: } | '(' INTEGRAL ')' { - $$ = NewIntVal($2) + $$ = NewIntLiteral($2) } float_length_opt: @@ -1000,8 +1000,8 @@ float_length_opt: | '(' INTEGRAL ',' INTEGRAL ')' { $$ = LengthScaleOption{ - Length: NewIntVal($2), - Scale: NewIntVal($4), + Length: NewIntLiteral($2), + Scale: NewIntLiteral($4), } } @@ -1012,14 +1012,14 @@ decimal_length_opt: | '(' INTEGRAL ')' { $$ = LengthScaleOption{ - Length: NewIntVal($2), + Length: NewIntLiteral($2), } } | '(' INTEGRAL ',' INTEGRAL ')' { $$ = LengthScaleOption{ - Length: NewIntVal($2), - Scale: NewIntVal($4), + Length: NewIntLiteral($2), + Scale: NewIntLiteral($4), } } @@ -1135,7 +1135,7 @@ column_comment_opt: } | COMMENT_KEYWORD STRING { - $$ = NewStrVal($2) + $$ = NewStrLiteral($2) } index_definition: @@ -1166,11 +1166,11 @@ index_option: | KEY_BLOCK_SIZE equal_opt INTEGRAL { // should not be string - $$ = &IndexOption{Name: string($1), Value: NewIntVal($3)} + $$ = &IndexOption{Name: string($1), Value: NewIntLiteral($3)} } | COMMENT_KEYWORD STRING { - $$ = &IndexOption{Name: string($1), Value: NewStrVal($2)} + $$ = &IndexOption{Name: string($1), Value: NewStrLiteral($2)} } equal_opt: @@ -2678,7 +2678,7 @@ value_expression: } | '+' value_expression %prec UNARY { - if num, ok := $2.(*SQLVal); ok && num.Type == IntVal { + if num, ok := $2.(*Literal); ok && num.Type == IntVal { $$ = num } else { $$ = &UnaryExpr{Operator: UPlusStr, Expr: $2} @@ -2686,13 +2686,13 @@ value_expression: } | '-' value_expression %prec UNARY { - if num, ok := $2.(*SQLVal); ok && num.Type == IntVal { + if num, ok := $2.(*Literal); ok && num.Type == IntVal { // Handle double negative if num.Val[0] == '-' { num.Val = num.Val[1:] $$ = num } else { - $$ = NewIntVal(append([]byte("-"), num.Val...)) + $$ = NewIntLiteral(append([]byte("-"), num.Val...)) } } else { $$ = &UnaryExpr{Operator: UMinusStr, Expr: $2} @@ -2776,11 +2776,11 @@ function_call_keyword: } | SUBSTR openb STRING FROM value_expression FOR value_expression closeb { - $$ = &SubstrExpr{StrVal: NewStrVal($3), From: $5, To: $7} + $$ = &SubstrExpr{StrVal: NewStrLiteral($3), From: $5, To: $7} } | SUBSTRING openb STRING FROM value_expression FOR value_expression closeb { - $$ = &SubstrExpr{StrVal: NewStrVal($3), From: $5, To: $7} + $$ = &SubstrExpr{StrVal: NewStrLiteral($3), From: $5, To: $7} } | MATCH openb select_expression_list closeb AGAINST openb value_expression match_option closeb { @@ -3071,31 +3071,31 @@ column_name: value: STRING { - $$ = NewStrVal($1) + $$ = NewStrLiteral($1) } | HEX { - $$ = NewHexVal($1) + $$ = NewHexLiteral($1) } | BIT_LITERAL { - $$ = NewBitVal($1) + $$ = NewBitLiteral($1) } | INTEGRAL { - $$ = NewIntVal($1) + $$ = NewIntLiteral($1) } | FLOAT { - $$ = NewFloatVal($1) + $$ = NewFloatLiteral($1) } | HEXNUM { - $$ = NewHexNum($1) + $$ = NewHexNumLiteral($1) } | VALUE_ARG { - $$ = NewValArg($1) + $$ = NewArgument($1) } | NULL { @@ -3110,15 +3110,15 @@ num_val: yylex.Error("expecting value after next") return 1 } - $$ = NewIntVal([]byte("1")) + $$ = NewIntLiteral([]byte("1")) } | INTEGRAL VALUES { - $$ = NewIntVal($1) + $$ = NewIntLiteral($1) } | VALUE_ARG VALUES { - $$ = NewValArg($1) + $$ = NewArgument($1) } group_by_opt: @@ -3324,11 +3324,11 @@ set_list: set_expression: reserved_sql_id '=' ON { - $$ = &SetExpr{Name: $1, Expr: NewStrVal([]byte("on"))} + $$ = &SetExpr{Name: $1, Expr: NewStrLiteral([]byte("on"))} } | reserved_sql_id '=' OFF { - $$ = &SetExpr{Name: $1, Expr: NewStrVal([]byte("off"))} + $$ = &SetExpr{Name: $1, Expr: NewStrLiteral([]byte("off"))} } | reserved_sql_id '=' expression { @@ -3355,11 +3355,11 @@ charset_or_character_set: charset_value: sql_id { - $$ = NewStrVal([]byte($1.String())) + $$ = NewStrLiteral([]byte($1.String())) } | STRING { - $$ = NewStrVal($1) + $$ = NewStrLiteral($1) } | DEFAULT { diff --git a/go/vt/vtexplain/vtexplain_vttablet.go b/go/vt/vtexplain/vtexplain_vttablet.go index 5c38fbf9a93..5fcf2039d7b 100644 --- a/go/vt/vtexplain/vtexplain_vttablet.go +++ b/go/vt/vtexplain/vtexplain_vttablet.go @@ -513,7 +513,7 @@ func (t *explainTablet) HandleQuery(c *mysql.Conn, query string, callback func(* case sqlparser.ValTuple: for _, val := range values { switch v := val.(type) { - case *sqlparser.SQLVal: + case *sqlparser.Literal: inVal = append(inVal, v.Val) } } @@ -591,7 +591,7 @@ func inferColTypeFromExpr(node sqlparser.Expr, colTypeMap map[string]querypb.Typ // As a shortcut, functions are integral types colNames = append(colNames, sqlparser.String(node)) colTypes = append(colTypes, querypb.Type_INT32) - case *sqlparser.SQLVal: + case *sqlparser.Literal: colNames = append(colNames, sqlparser.String(node)) switch node.Type { case sqlparser.IntVal: diff --git a/go/vt/vtgate/engine/memory_sort_test.go b/go/vt/vtgate/engine/memory_sort_test.go index a2a9d682521..a9f2406d68e 100644 --- a/go/vt/vtgate/engine/memory_sort_test.go +++ b/go/vt/vtgate/engine/memory_sort_test.go @@ -68,7 +68,7 @@ func TestMemorySortExecute(t *testing.T) { } fp.rewind() - upperlimit, err := sqlparser.NewPlanValue(sqlparser.NewValArg([]byte(":__upper_limit"))) + upperlimit, err := sqlparser.NewPlanValue(sqlparser.NewArgument([]byte(":__upper_limit"))) if err != nil { t.Fatal(err) } @@ -136,7 +136,7 @@ func TestMemorySortStreamExecute(t *testing.T) { } fp.rewind() - upperlimit, err := sqlparser.NewPlanValue(sqlparser.NewValArg([]byte(":__upper_limit"))) + upperlimit, err := sqlparser.NewPlanValue(sqlparser.NewArgument([]byte(":__upper_limit"))) if err != nil { t.Fatal(err) } @@ -317,7 +317,7 @@ func TestMemorySortMultiColumn(t *testing.T) { } fp.rewind() - upperlimit, err := sqlparser.NewPlanValue(sqlparser.NewValArg([]byte(":__upper_limit"))) + upperlimit, err := sqlparser.NewPlanValue(sqlparser.NewArgument([]byte(":__upper_limit"))) if err != nil { t.Fatal(err) } diff --git a/go/vt/vtgate/executor.go b/go/vt/vtgate/executor.go index 22fb205e2dd..9356945ef03 100644 --- a/go/vt/vtgate/executor.go +++ b/go/vt/vtgate/executor.go @@ -409,7 +409,7 @@ func (e *Executor) handleSet(ctx context.Context, sql string, logStats *LogStats func getValueFor(expr *sqlparser.SetExpr) (interface{}, error) { switch expr := expr.Expr.(type) { - case *sqlparser.SQLVal: + case *sqlparser.Literal: switch expr.Type { case sqlparser.StrVal: return strings.ToLower(string(expr.Val)), nil @@ -1282,11 +1282,11 @@ func generateCharsetRows(showFilter *sqlparser.ShowFilter, colNames []string) ([ leftOk := left.Name.EqualString(charset) if leftOk { - sqlVal, ok := cmpExp.Right.(*sqlparser.SQLVal) + literal, ok := cmpExp.Right.(*sqlparser.Literal) if !ok { return nil, vterrors.Errorf(vtrpcpb.Code_INVALID_ARGUMENT, "we expect the right side to be a string") } - rightString := string(sqlVal.Val) + rightString := string(literal.Val) switch cmpExp.Operator { case sqlparser.EqualStr: diff --git a/go/vt/vtgate/planbuilder/builder.go b/go/vt/vtgate/planbuilder/builder.go index 336d8cf2309..5f5dabc26da 100644 --- a/go/vt/vtgate/planbuilder/builder.go +++ b/go/vt/vtgate/planbuilder/builder.go @@ -80,7 +80,7 @@ type builder interface { // SetUpperLimit is an optimization hint that tells that primitive // that it does not need to return more than the specified number of rows. // A primitive that cannot perform this can ignore the request. - SetUpperLimit(count *sqlparser.SQLVal) + SetUpperLimit(count sqlparser.Expr) // PushMisc pushes miscelleaneous constructs to all the primitives. PushMisc(sel *sqlparser.Select) @@ -165,7 +165,7 @@ func (bc *builderCommon) ResultColumns() []*resultColumn { return bc.input.ResultColumns() } -func (bc *builderCommon) SetUpperLimit(count *sqlparser.SQLVal) { +func (bc *builderCommon) SetUpperLimit(count sqlparser.Expr) { bc.input.SetUpperLimit(count) } diff --git a/go/vt/vtgate/planbuilder/concatenate.go b/go/vt/vtgate/planbuilder/concatenate.go index fe3e4d8eea9..91e298b48c4 100644 --- a/go/vt/vtgate/planbuilder/concatenate.go +++ b/go/vt/vtgate/planbuilder/concatenate.go @@ -48,7 +48,7 @@ func (c *concatenate) First() builder { panic("implement me") } -func (c *concatenate) SetUpperLimit(count *sqlparser.SQLVal) { +func (c *concatenate) SetUpperLimit(count sqlparser.Expr) { // not doing anything by design } diff --git a/go/vt/vtgate/planbuilder/expr.go b/go/vt/vtgate/planbuilder/expr.go index 595f4cefcb8..c289b073f29 100644 --- a/go/vt/vtgate/planbuilder/expr.go +++ b/go/vt/vtgate/planbuilder/expr.go @@ -156,7 +156,7 @@ func (pb *primitiveBuilder) findOrigin(expr sqlparser.Expr) (pullouts []*pullout construct, ok := constructsMap[sqi.ast] if !ok { // (subquery) -> :_sq - expr = sqlparser.ReplaceExpr(expr, sqi.ast, sqlparser.NewValArg([]byte(":"+sqName))) + expr = sqlparser.ReplaceExpr(expr, sqi.ast, sqlparser.NewArgument([]byte(":"+sqName))) pullouts = append(pullouts, newPulloutSubquery(engine.PulloutValue, sqName, hasValues, sqi.bldr)) continue } @@ -166,9 +166,9 @@ func (pb *primitiveBuilder) findOrigin(expr sqlparser.Expr) (pullouts []*pullout // a in (subquery) -> (:__sq_has_values = 1 and (a in ::__sq)) newExpr := &sqlparser.AndExpr{ Left: &sqlparser.ComparisonExpr{ - Left: sqlparser.NewValArg([]byte(":" + hasValues)), + Left: sqlparser.NewArgument([]byte(":" + hasValues)), Operator: sqlparser.EqualStr, - Right: sqlparser.NewIntVal([]byte("1")), + Right: sqlparser.NewIntLiteral([]byte("1")), }, Right: sqlparser.ReplaceExpr(construct, sqi.ast, sqlparser.ListArg([]byte("::"+sqName))), } @@ -178,9 +178,9 @@ func (pb *primitiveBuilder) findOrigin(expr sqlparser.Expr) (pullouts []*pullout // a not in (subquery) -> (:__sq_has_values = 0 or (a not in ::__sq)) newExpr := &sqlparser.OrExpr{ Left: &sqlparser.ComparisonExpr{ - Left: sqlparser.NewValArg([]byte(":" + hasValues)), + Left: sqlparser.NewArgument([]byte(":" + hasValues)), Operator: sqlparser.EqualStr, - Right: sqlparser.NewIntVal([]byte("0")), + Right: sqlparser.NewIntLiteral([]byte("0")), }, Right: sqlparser.ReplaceExpr(construct, sqi.ast, sqlparser.ListArg([]byte("::"+sqName))), } @@ -189,7 +189,7 @@ func (pb *primitiveBuilder) findOrigin(expr sqlparser.Expr) (pullouts []*pullout } case *sqlparser.ExistsExpr: // exists (subquery) -> :__sq_has_values - expr = sqlparser.ReplaceExpr(expr, construct, sqlparser.NewValArg([]byte(":"+hasValues))) + expr = sqlparser.ReplaceExpr(expr, construct, sqlparser.NewArgument([]byte(":"+hasValues))) pullouts = append(pullouts, newPulloutSubquery(engine.PulloutExists, sqName, hasValues, sqi.bldr)) } } @@ -281,16 +281,18 @@ func valEqual(a, b sqlparser.Expr) bool { if b, ok := b.(*sqlparser.ColName); ok { return a.Metadata == b.Metadata } - case *sqlparser.SQLVal: - b, ok := b.(*sqlparser.SQLVal) + case sqlparser.Argument: + b, ok := b.(sqlparser.Argument) + if !ok { + return false + } + return bytes.Equal(a, b) + case *sqlparser.Literal: + b, ok := b.(*sqlparser.Literal) if !ok { return false } switch a.Type { - case sqlparser.ValArg: - if b.Type == sqlparser.ValArg { - return bytes.Equal(a.Val, b.Val) - } case sqlparser.StrVal: switch b.Type { case sqlparser.StrVal: @@ -309,7 +311,7 @@ func valEqual(a, b sqlparser.Expr) bool { return false } -func hexEqual(a, b *sqlparser.SQLVal) bool { +func hexEqual(a, b *sqlparser.Literal) bool { v, err := a.HexDecode() if err != nil { return false diff --git a/go/vt/vtgate/planbuilder/expr_test.go b/go/vt/vtgate/planbuilder/expr_test.go index 3046862a609..ec5a2908134 100644 --- a/go/vt/vtgate/planbuilder/expr_test.go +++ b/go/vt/vtgate/planbuilder/expr_test.go @@ -51,48 +51,48 @@ func TestValEqual(t *testing.T) { in1: newValArg(":aa"), in2: newValArg(":bb"), }, { - in1: newStrVal("aa"), - in2: newStrVal("aa"), + in1: newStrLiteral("aa"), + in2: newStrLiteral("aa"), out: true, }, { - in1: newStrVal("11"), - in2: newHexVal("3131"), + in1: newStrLiteral("11"), + in2: newHexLiteral("3131"), out: true, }, { - in1: newHexVal("3131"), - in2: newStrVal("11"), + in1: newHexLiteral("3131"), + in2: newStrLiteral("11"), out: true, }, { - in1: newHexVal("3131"), - in2: newHexVal("3131"), + in1: newHexLiteral("3131"), + in2: newHexLiteral("3131"), out: true, }, { - in1: newHexVal("3131"), - in2: newHexVal("3132"), + in1: newHexLiteral("3131"), + in2: newHexLiteral("3132"), out: false, }, { - in1: newHexVal("313"), - in2: newHexVal("3132"), + in1: newHexLiteral("313"), + in2: newHexLiteral("3132"), out: false, }, { - in1: newHexVal("3132"), - in2: newHexVal("313"), + in1: newHexLiteral("3132"), + in2: newHexLiteral("313"), out: false, }, { - in1: newIntVal("313"), - in2: newHexVal("3132"), + in1: newIntLiteral("313"), + in2: newHexLiteral("3132"), out: false, }, { - in1: newHexVal("3132"), - in2: newIntVal("313"), + in1: newHexLiteral("3132"), + in2: newIntLiteral("313"), out: false, }, { - in1: newIntVal("313"), - in2: newIntVal("313"), + in1: newIntLiteral("313"), + in2: newIntLiteral("313"), out: true, }, { - in1: newIntVal("313"), - in2: newIntVal("314"), + in1: newIntLiteral("313"), + in2: newIntLiteral("314"), out: false, }} for _, tc := range testcases { @@ -103,18 +103,18 @@ func TestValEqual(t *testing.T) { } } -func newStrVal(in string) *sqlparser.SQLVal { - return sqlparser.NewStrVal([]byte(in)) +func newStrLiteral(in string) *sqlparser.Literal { + return sqlparser.NewStrLiteral([]byte(in)) } -func newIntVal(in string) *sqlparser.SQLVal { - return sqlparser.NewIntVal([]byte(in)) +func newIntLiteral(in string) *sqlparser.Literal { + return sqlparser.NewIntLiteral([]byte(in)) } -func newHexVal(in string) *sqlparser.SQLVal { - return sqlparser.NewHexVal([]byte(in)) +func newHexLiteral(in string) *sqlparser.Literal { + return sqlparser.NewHexLiteral([]byte(in)) } -func newValArg(in string) *sqlparser.SQLVal { - return sqlparser.NewValArg([]byte(in)) +func newValArg(in string) sqlparser.Expr { + return sqlparser.NewArgument([]byte(in)) } diff --git a/go/vt/vtgate/planbuilder/expression_converter.go b/go/vt/vtgate/planbuilder/expression_converter.go index d882201443e..7a19c9e5dd8 100644 --- a/go/vt/vtgate/planbuilder/expression_converter.go +++ b/go/vt/vtgate/planbuilder/expression_converter.go @@ -33,7 +33,7 @@ type expressionConverter struct { func booleanValues(astExpr sqlparser.Expr) evalengine.Expr { switch node := astExpr.(type) { - case *sqlparser.SQLVal: + case *sqlparser.Literal: //set autocommit = 'on' if node.Type == sqlparser.StrVal { switch strings.ToLower(string(node.Val)) { diff --git a/go/vt/vtgate/planbuilder/insert.go b/go/vt/vtgate/planbuilder/insert.go index 88e3f07ac3b..c485c4b0c60 100644 --- a/go/vt/vtgate/planbuilder/insert.go +++ b/go/vt/vtgate/planbuilder/insert.go @@ -184,7 +184,7 @@ func buildInsertShardedPlan(ins *sqlparser.Insert, table *vindexes.Table) (engin colNum := findOrAddColumn(ins, col) for rowNum, row := range rows { name := ":" + engine.InsertVarName(col, rowNum) - row[colNum] = sqlparser.NewValArg([]byte(name)) + row[colNum] = sqlparser.NewArgument([]byte(name)) } } } @@ -236,7 +236,7 @@ func modifyForAutoinc(ins *sqlparser.Insert, eins *engine.Insert) error { return fmt.Errorf("could not compute value for vindex or auto-inc column: %v", err) } autoIncValues.Values = append(autoIncValues.Values, pv) - row[colNum] = sqlparser.NewValArg([]byte(":" + engine.SeqVarName + strconv.Itoa(rowNum))) + row[colNum] = sqlparser.NewArgument([]byte(":" + engine.SeqVarName + strconv.Itoa(rowNum))) } eins.Generate = &engine.Generate{ diff --git a/go/vt/vtgate/planbuilder/join.go b/go/vt/vtgate/planbuilder/join.go index 0ee90033f4c..6ac69d08f4a 100644 --- a/go/vt/vtgate/planbuilder/join.go +++ b/go/vt/vtgate/planbuilder/join.go @@ -232,7 +232,7 @@ func (jb *join) PushOrderBy(orderBy sqlparser.OrderBy) (builder, error) { } for _, order := range orderBy { - if node, ok := order.Expr.(*sqlparser.SQLVal); ok { + if node, ok := order.Expr.(*sqlparser.Literal); ok { // This block handles constructs that use ordinals for 'ORDER BY'. For example: // SELECT a, b, c FROM t1, t2 ORDER BY 1, 2, 3. num, err := ResultFromNumber(jb.ResultColumns(), node) @@ -282,7 +282,7 @@ func (jb *join) PushOrderBy(orderBy sqlparser.OrderBy) (builder, error) { // The call is ignored because results get multiplied // as they join with others. So, it's hard to reliably // predict if a limit push down will work correctly. -func (jb *join) SetUpperLimit(_ *sqlparser.SQLVal) { +func (jb *join) SetUpperLimit(_ sqlparser.Expr) { } // PushMisc satisfies the builder interface. diff --git a/go/vt/vtgate/planbuilder/limit.go b/go/vt/vtgate/planbuilder/limit.go index 210da3399c1..74fdc754188 100644 --- a/go/vt/vtgate/planbuilder/limit.go +++ b/go/vt/vtgate/planbuilder/limit.go @@ -20,6 +20,8 @@ import ( "errors" "fmt" + "vitess.io/vitess/go/vt/vterrors" + "vitess.io/vitess/go/vt/sqlparser" "vitess.io/vitess/go/vt/vtgate/engine" ) @@ -85,18 +87,14 @@ func (l *limit) PushOrderBy(orderBy sqlparser.OrderBy) (builder, error) { // the underlying primitive that it doesn't need to return more rows than // specified. func (l *limit) SetLimit(limit *sqlparser.Limit) error { - count, ok := limit.Rowcount.(*sqlparser.SQLVal) - if !ok { - return fmt.Errorf("unexpected expression in LIMIT: %v", sqlparser.String(limit)) - } - pv, err := sqlparser.NewPlanValue(count) + pv, err := sqlparser.NewPlanValue(limit.Rowcount) if err != nil { - return err + return vterrors.Wrap(err, "unexpected expression in LIMIT") } l.elimit.Count = pv switch offset := limit.Offset.(type) { - case *sqlparser.SQLVal: + case *sqlparser.Literal: pv, err = sqlparser.NewPlanValue(offset) if err != nil { return err @@ -108,12 +106,12 @@ func (l *limit) SetLimit(limit *sqlparser.Limit) error { return fmt.Errorf("unexpected expression in LIMIT: %v", sqlparser.String(limit)) } - l.input.SetUpperLimit(sqlparser.NewValArg([]byte(":__upper_limit"))) + l.input.SetUpperLimit(sqlparser.NewArgument([]byte(":__upper_limit"))) return nil } // SetUpperLimit satisfies the builder interface. // This is a no-op because we actually call SetLimit for this primitive. // In the future, we may have to honor this call for subqueries. -func (l *limit) SetUpperLimit(count *sqlparser.SQLVal) { +func (l *limit) SetUpperLimit(count sqlparser.Expr) { } diff --git a/go/vt/vtgate/planbuilder/memory_sort.go b/go/vt/vtgate/planbuilder/memory_sort.go index 9255af36d79..055d32b8a2d 100644 --- a/go/vt/vtgate/planbuilder/memory_sort.go +++ b/go/vt/vtgate/planbuilder/memory_sort.go @@ -47,7 +47,7 @@ func newMemorySort(bldr builder, orderBy sqlparser.OrderBy) (*memorySort, error) for _, order := range orderBy { colNumber := -1 switch expr := order.Expr.(type) { - case *sqlparser.SQLVal: + case *sqlparser.Literal: var err error if colNumber, err = ResultFromNumber(ms.ResultColumns(), expr); err != nil { return nil, err @@ -147,6 +147,6 @@ func (ms *memorySort) Wireup(bldr builder, jt *jointab) error { // SetUpperLimit satisfies the builder interface. // This is a no-op because we actually call SetLimit for this primitive. // In the future, we may have to honor this call for subqueries. -func (ms *memorySort) SetUpperLimit(count *sqlparser.SQLVal) { +func (ms *memorySort) SetUpperLimit(count sqlparser.Expr) { ms.eMemorySort.UpperLimit, _ = sqlparser.NewPlanValue(count) } diff --git a/go/vt/vtgate/planbuilder/ordered_aggregate.go b/go/vt/vtgate/planbuilder/ordered_aggregate.go index 54548faffd4..177a918ba3c 100644 --- a/go/vt/vtgate/planbuilder/ordered_aggregate.go +++ b/go/vt/vtgate/planbuilder/ordered_aggregate.go @@ -184,7 +184,7 @@ func (pb *primitiveBuilder) groupByHasUniqueVindex(sel *sqlparser.Select, rb *ro } else { matchedExpr = node } - case *sqlparser.SQLVal: + case *sqlparser.Literal: if node.Type != sqlparser.IntVal { continue } @@ -389,7 +389,7 @@ func (oa *orderedAggregate) PushGroupBy(groupBy sqlparser.GroupBy) error { if colNumber == -1 { return errors.New("unsupported: in scatter query: group by column must reference column in SELECT list") } - case *sqlparser.SQLVal: + case *sqlparser.Literal: num, err := ResultFromNumber(oa.resultColumns, node) if err != nil { return err @@ -438,7 +438,7 @@ func (oa *orderedAggregate) PushOrderBy(orderBy sqlparser.OrderBy) (builder, err // Identify the order by column. var orderByCol *column switch expr := order.Expr.(type) { - case *sqlparser.SQLVal: + case *sqlparser.Literal: num, err := ResultFromNumber(oa.resultColumns, expr) if err != nil { return nil, err @@ -501,7 +501,7 @@ func (oa *orderedAggregate) PushOrderBy(orderBy sqlparser.OrderBy) (builder, err } // SetUpperLimit satisfies the builder interface. -func (oa *orderedAggregate) SetUpperLimit(count *sqlparser.SQLVal) { +func (oa *orderedAggregate) SetUpperLimit(count sqlparser.Expr) { oa.input.SetUpperLimit(count) } diff --git a/go/vt/vtgate/planbuilder/pullout_subquery.go b/go/vt/vtgate/planbuilder/pullout_subquery.go index 9977b0a6359..16bc15fd142 100644 --- a/go/vt/vtgate/planbuilder/pullout_subquery.go +++ b/go/vt/vtgate/planbuilder/pullout_subquery.go @@ -124,7 +124,7 @@ func (ps *pulloutSubquery) PushOrderBy(orderBy sqlparser.OrderBy) (builder, erro // SetUpperLimit satisfies the builder interface. // This is a no-op because we actually call SetLimit for this primitive. // In the future, we may have to honor this call for subqueries. -func (ps *pulloutSubquery) SetUpperLimit(count *sqlparser.SQLVal) { +func (ps *pulloutSubquery) SetUpperLimit(count sqlparser.Expr) { ps.underlying.SetUpperLimit(count) } diff --git a/go/vt/vtgate/planbuilder/route.go b/go/vt/vtgate/planbuilder/route.go index 2eead2e5e92..3881daed342 100644 --- a/go/vt/vtgate/planbuilder/route.go +++ b/go/vt/vtgate/planbuilder/route.go @@ -201,7 +201,7 @@ func (rb *route) PushOrderBy(orderBy sqlparser.OrderBy) (builder, error) { for _, order := range orderBy { colNumber := -1 switch expr := order.Expr.(type) { - case *sqlparser.SQLVal: + case *sqlparser.Literal: var err error if colNumber, err = ResultFromNumber(rb.resultColumns, expr); err != nil { return nil, err @@ -243,7 +243,7 @@ func (rb *route) SetLimit(limit *sqlparser.Limit) { // If it's a scatter query, the rows returned will be // more than the upper limit, but enough for the limit // primitive to chop off where needed. -func (rb *route) SetUpperLimit(count *sqlparser.SQLVal) { +func (rb *route) SetUpperLimit(count sqlparser.Expr) { rb.Select.SetLimit(&sqlparser.Limit{Rowcount: count}) } @@ -284,7 +284,7 @@ func (rb *route) Wireup(bldr builder, jt *jointab) error { if len(node.SelectExprs) == 0 { node.SelectExprs = sqlparser.SelectExprs([]sqlparser.SelectExpr{ &sqlparser.AliasedExpr{ - Expr: sqlparser.NewIntVal([]byte{'1'}), + Expr: sqlparser.NewIntLiteral([]byte{'1'}), }, }) } diff --git a/go/vt/vtgate/planbuilder/select.go b/go/vt/vtgate/planbuilder/select.go index d7764a27640..a5c5fab208a 100644 --- a/go/vt/vtgate/planbuilder/select.go +++ b/go/vt/vtgate/planbuilder/select.go @@ -257,7 +257,7 @@ func (r *rewriter) rewriteTableSchema(cursor *sqlparser.Cursor) bool { return false } r.tableNameExpressions = append(r.tableNameExpressions, evalExpr) - parent.Right = sqlparser.NewValArg([]byte(":__vtschemaname")) + parent.Right = sqlparser.NewArgument([]byte(":__vtschemaname")) } } } diff --git a/go/vt/vtgate/planbuilder/set.go b/go/vt/vtgate/planbuilder/set.go index fe66e0d4b0a..cf824afba67 100644 --- a/go/vt/vtgate/planbuilder/set.go +++ b/go/vt/vtgate/planbuilder/set.go @@ -214,7 +214,7 @@ func resolveDestination(vschema ContextVSchema) (*vindexes.Keyspace, key.Destina func extractValue(expr *sqlparser.SetExpr, boolean bool) string { switch node := expr.Expr.(type) { - case *sqlparser.SQLVal: + case *sqlparser.Literal: if node.Type == sqlparser.StrVal && boolean { switch strings.ToLower(string(node.Val)) { case "on": diff --git a/go/vt/vtgate/planbuilder/symtab.go b/go/vt/vtgate/planbuilder/symtab.go index ced54a6dc1c..0a5c4ea22ad 100644 --- a/go/vt/vtgate/planbuilder/symtab.go +++ b/go/vt/vtgate/planbuilder/symtab.go @@ -376,7 +376,7 @@ func (st *symtab) searchTables(col *sqlparser.ColName) (*column, error) { // ResultFromNumber returns the result column index based on the column // order expression. -func ResultFromNumber(rcs []*resultColumn, val *sqlparser.SQLVal) (int, error) { +func ResultFromNumber(rcs []*resultColumn, val *sqlparser.Literal) (int, error) { if val.Type != sqlparser.IntVal { return 0, errors.New("column number is not an int") } diff --git a/go/vt/vtgate/planbuilder/testdata/postprocess_cases.txt b/go/vt/vtgate/planbuilder/testdata/postprocess_cases.txt index a2352cfd2e7..8d0be931570 100644 --- a/go/vt/vtgate/planbuilder/testdata/postprocess_cases.txt +++ b/go/vt/vtgate/planbuilder/testdata/postprocess_cases.txt @@ -1060,4 +1060,4 @@ # invalid limit expression "select id from user limit 1+1" -"unexpected expression in LIMIT: limit 1 + 1" +"unexpected expression in LIMIT: expression is too complex '1 + 1'" diff --git a/go/vt/vtgate/planbuilder/vindex_func.go b/go/vt/vtgate/planbuilder/vindex_func.go index f158acbee24..aa826c6219d 100644 --- a/go/vt/vtgate/planbuilder/vindex_func.go +++ b/go/vt/vtgate/planbuilder/vindex_func.go @@ -177,7 +177,7 @@ func (vf *vindexFunc) PushOrderBy(orderBy sqlparser.OrderBy) (builder, error) { } // SetUpperLimit satisfies the builder interface. -func (vf *vindexFunc) SetUpperLimit(_ *sqlparser.SQLVal) { +func (vf *vindexFunc) SetUpperLimit(_ sqlparser.Expr) { } // PushMisc satisfies the builder interface. diff --git a/go/vt/vtgate/vindexes/consistent_lookup_test.go b/go/vt/vtgate/vindexes/consistent_lookup_test.go index d8123543d46..0a77317b47f 100644 --- a/go/vt/vtgate/vindexes/consistent_lookup_test.go +++ b/go/vt/vtgate/vindexes/consistent_lookup_test.go @@ -461,10 +461,10 @@ func TestConsistentLookupUpdateBecauseUncomparableTypes(t *testing.T) { t.Run(val.typ.String(), func(t *testing.T) { vc.AddResult(&sqltypes.Result{}, nil) vc.AddResult(&sqltypes.Result{}, nil) - sqlVal, err := sqltypes.NewValue(val.typ, []byte(val.val)) + literal, err := sqltypes.NewValue(val.typ, []byte(val.val)) require.NoError(t, err) - err = lookup.(Lookup).Update(vc, []sqltypes.Value{sqlVal, sqlVal}, []byte("test"), []sqltypes.Value{sqlVal, sqlVal}) + err = lookup.(Lookup).Update(vc, []sqltypes.Value{literal, literal}, []byte("test"), []sqltypes.Value{literal, literal}) require.NoError(t, err) require.NotEmpty(t, vc.log) vc.log = nil diff --git a/go/vt/vttablet/tabletmanager/vreplication/table_plan_builder.go b/go/vt/vttablet/tabletmanager/vreplication/table_plan_builder.go index 2d843e0dc6a..50b074e0c62 100644 --- a/go/vt/vttablet/tabletmanager/vreplication/table_plan_builder.go +++ b/go/vt/vttablet/tabletmanager/vreplication/table_plan_builder.go @@ -183,7 +183,7 @@ func buildTablePlan(tableName, filter string, tableKeys map[string][]string, las query = buf.String() case key.IsKeyRange(filter): buf := sqlparser.NewTrackedBuffer(nil) - buf.Myprintf("select * from %v where in_keyrange(%v)", sqlparser.NewTableIdent(tableName), sqlparser.NewStrVal([]byte(filter))) + buf.Myprintf("select * from %v where in_keyrange(%v)", sqlparser.NewTableIdent(tableName), sqlparser.NewStrLiteral([]byte(filter))) query = buf.String() case filter == ExcludeStr: return nil, nil diff --git a/go/vt/vttablet/tabletserver/planbuilder/plan.go b/go/vt/vttablet/tabletserver/planbuilder/plan.go index 3c966791881..424b93ab9a2 100644 --- a/go/vt/vttablet/tabletserver/planbuilder/plan.go +++ b/go/vt/vttablet/tabletserver/planbuilder/plan.go @@ -29,7 +29,7 @@ import ( ) var ( - execLimit = &sqlparser.Limit{Rowcount: sqlparser.NewValArg([]byte(":#maxLimit"))} + execLimit = &sqlparser.Limit{Rowcount: sqlparser.NewArgument([]byte(":#maxLimit"))} // PassthroughDMLs will return plans that pass-through the DMLs without changing them. PassthroughDMLs = false diff --git a/go/vt/vttablet/tabletserver/vstreamer/planbuilder.go b/go/vt/vttablet/tabletserver/vstreamer/planbuilder.go index d7da87e89c8..319d6c7e0d3 100644 --- a/go/vt/vttablet/tabletserver/vstreamer/planbuilder.go +++ b/go/vt/vttablet/tabletserver/vstreamer/planbuilder.go @@ -360,7 +360,7 @@ func (plan *Plan) analyzeWhere(vschema *localVSchema, where *sqlparser.Where) er if err != nil { return err } - val, ok := expr.Right.(*sqlparser.SQLVal) + val, ok := expr.Right.(*sqlparser.Literal) if !ok { return fmt.Errorf("unexpected: %v", sqlparser.String(expr)) } @@ -558,7 +558,7 @@ func selString(expr sqlparser.SelectExpr) (string, error) { if !ok { return "", fmt.Errorf("unsupported: %v", sqlparser.String(expr)) } - val, ok := aexpr.Expr.(*sqlparser.SQLVal) + val, ok := aexpr.Expr.(*sqlparser.Literal) if !ok { return "", fmt.Errorf("unsupported: %v", sqlparser.String(expr)) } diff --git a/go/vt/vttablet/tabletserver/vstreamer/uvstreamer.go b/go/vt/vttablet/tabletserver/vstreamer/uvstreamer.go index f5b8d1882eb..21e4cec9ce2 100644 --- a/go/vt/vttablet/tabletserver/vstreamer/uvstreamer.go +++ b/go/vt/vttablet/tabletserver/vstreamer/uvstreamer.go @@ -211,7 +211,7 @@ func getQuery(tableName string, filter string) string { query = buf.String() case key.IsKeyRange(filter): buf := sqlparser.NewTrackedBuffer(nil) - buf.Myprintf("select * from %v where in_keyrange(%v)", sqlparser.NewTableIdent(tableName), sqlparser.NewStrVal([]byte(filter))) + buf.Myprintf("select * from %v where in_keyrange(%v)", sqlparser.NewTableIdent(tableName), sqlparser.NewStrLiteral([]byte(filter))) query = buf.String() } return query diff --git a/go/vt/wrangler/materializer.go b/go/vt/wrangler/materializer.go index 369d992ee45..6f0e43bdc0c 100644 --- a/go/vt/wrangler/materializer.go +++ b/go/vt/wrangler/materializer.go @@ -783,8 +783,8 @@ func (mz *materializer) generateInserts(ctx context.Context) (string, error) { subExprs = append(subExprs, &sqlparser.AliasedExpr{Expr: mappedCol}) } vindexName := fmt.Sprintf("%s.%s", mz.ms.TargetKeyspace, cv.Name) - subExprs = append(subExprs, &sqlparser.AliasedExpr{Expr: sqlparser.NewStrVal([]byte(vindexName))}) - subExprs = append(subExprs, &sqlparser.AliasedExpr{Expr: sqlparser.NewStrVal([]byte("{{.keyrange}}"))}) + subExprs = append(subExprs, &sqlparser.AliasedExpr{Expr: sqlparser.NewStrLiteral([]byte(vindexName))}) + subExprs = append(subExprs, &sqlparser.AliasedExpr{Expr: sqlparser.NewStrLiteral([]byte("{{.keyrange}}"))}) sel.Where = &sqlparser.Where{ Type: sqlparser.WhereStr, Expr: &sqlparser.FuncExpr{ diff --git a/go/vt/wrangler/stream_migrater.go b/go/vt/wrangler/stream_migrater.go index 328a2b9eedc..5cbb4c745b7 100644 --- a/go/vt/wrangler/stream_migrater.go +++ b/go/vt/wrangler/stream_migrater.go @@ -507,7 +507,7 @@ func (sm *streamMigrater) templatizeKeyRange(ctx context.Context, rule *binlogda if !ok { return fmt.Errorf("unexpected in_keyrange parameters: %v", sqlparser.String(funcExpr)) } - val, ok := aliased.Expr.(*sqlparser.SQLVal) + val, ok := aliased.Expr.(*sqlparser.Literal) if !ok { return fmt.Errorf("unexpected in_keyrange parameters: %v", sqlparser.String(funcExpr)) } @@ -524,8 +524,8 @@ func (sm *streamMigrater) templatizeKeyRange(ctx context.Context, rule *binlogda Name: sqlparser.NewColIdent("in_keyrange"), Exprs: sqlparser.SelectExprs{ &sqlparser.AliasedExpr{Expr: &sqlparser.ColName{Name: vtable.ColumnVindexes[0].Columns[0]}}, - &sqlparser.AliasedExpr{Expr: sqlparser.NewStrVal([]byte(vtable.ColumnVindexes[0].Type))}, - &sqlparser.AliasedExpr{Expr: sqlparser.NewStrVal([]byte("{{.}}"))}, + &sqlparser.AliasedExpr{Expr: sqlparser.NewStrLiteral([]byte(vtable.ColumnVindexes[0].Type))}, + &sqlparser.AliasedExpr{Expr: sqlparser.NewStrLiteral([]byte("{{.}}"))}, }, } sel.AddWhere(inkr) diff --git a/go/vt/wrangler/vexec_plan.go b/go/vt/wrangler/vexec_plan.go index 6499d71ebdf..07634322223 100644 --- a/go/vt/wrangler/vexec_plan.go +++ b/go/vt/wrangler/vexec_plan.go @@ -104,7 +104,7 @@ func (vx *vexec) addDefaultWheres(where *sqlparser.Where) *sqlparser.Where { expr := &sqlparser.ComparisonExpr{ Left: &sqlparser.ColName{Name: sqlparser.NewColIdent("db_name")}, Operator: sqlparser.EqualStr, - Right: sqlparser.NewStrVal([]byte(vx.masters[0].DbName())), + Right: sqlparser.NewStrLiteral([]byte(vx.masters[0].DbName())), } if newWhere == nil { newWhere = &sqlparser.Where{ @@ -122,7 +122,7 @@ func (vx *vexec) addDefaultWheres(where *sqlparser.Where) *sqlparser.Where { expr := &sqlparser.ComparisonExpr{ Left: &sqlparser.ColName{Name: sqlparser.NewColIdent("workflow")}, Operator: sqlparser.EqualStr, - Right: sqlparser.NewStrVal([]byte(vx.workflow)), + Right: sqlparser.NewStrLiteral([]byte(vx.workflow)), } newWhere.Expr = &sqlparser.AndExpr{ Left: newWhere.Expr,