Skip to content

Commit

Permalink
sqlparser: normalize all bit literals to 0b
Browse files Browse the repository at this point in the history
Signed-off-by: Vicent Marti <[email protected]>
  • Loading branch information
vmg committed Oct 26, 2023
1 parent e77fe2c commit 7c48726
Show file tree
Hide file tree
Showing 7 changed files with 13 additions and 27 deletions.
4 changes: 1 addition & 3 deletions go/vt/sqlparser/ast_format.go
Original file line number Diff line number Diff line change
Expand Up @@ -1314,12 +1314,10 @@ func (node *Literal) Format(buf *TrackedBuffer) {
switch node.Type {
case StrVal:
sqltypes.MakeTrusted(sqltypes.VarBinary, node.Bytes()).EncodeSQL(buf)
case IntVal, FloatVal, DecimalVal, HexNum:
case IntVal, FloatVal, DecimalVal, HexNum, BitNum:
buf.astPrintf(node, "%#s", node.Val)
case HexVal:
buf.astPrintf(node, "X'%#s'", node.Val)
case BitNum:
buf.astPrintf(node, "0b%#s", node.Val)
case DateVal:
buf.astPrintf(node, "date'%#s'", node.Val)
case TimeVal:
Expand Down
5 changes: 1 addition & 4 deletions go/vt/sqlparser/ast_format_fast.go

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

2 changes: 1 addition & 1 deletion go/vt/sqlparser/normalizer.go
Original file line number Diff line number Diff line change
Expand Up @@ -366,7 +366,7 @@ func SQLToBindvar(node SQLNode) *querypb.BindVariable {
case BitNum:
out := make([]byte, 0, len(node.Bytes())+2)
out = append(out, '0', 'b')
out = append(out, node.Bytes()...)
out = append(out, node.Bytes()[2:]...)
v, err = sqltypes.NewValue(sqltypes.BitNum, out)
case DateVal:
v, err = sqltypes.NewValue(sqltypes.Date, node.Bytes())
Expand Down
8 changes: 4 additions & 4 deletions go/vt/sqlparser/sql.go

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

8 changes: 4 additions & 4 deletions go/vt/sqlparser/sql.y
Original file line number Diff line number Diff line change
Expand Up @@ -1689,27 +1689,27 @@ text_literal
}
| BITNUM
{
$$ = NewBitLiteral($1[2:])
$$ = NewBitLiteral($1)
}
| BIT_LITERAL
{
$$ = NewBitLiteral($1)
$$ = NewBitLiteral("0b" + $1)
}
| VALUE_ARG
{
$$ = parseBindVariable(yylex, $1[1:])
}
| underscore_charsets BIT_LITERAL %prec UNARY
{
$$ = &IntroducerExpr{CharacterSet: $1, Expr: NewBitLiteral($2)}
$$ = &IntroducerExpr{CharacterSet: $1, Expr: NewBitLiteral("0b" + $2)}
}
| underscore_charsets HEXNUM %prec UNARY
{
$$ = &IntroducerExpr{CharacterSet: $1, Expr: NewHexNumLiteral($2)}
}
| underscore_charsets BITNUM %prec UNARY
{
$$ = &IntroducerExpr{CharacterSet: $1, Expr: NewBitLiteral($2[2:])}
$$ = &IntroducerExpr{CharacterSet: $1, Expr: NewBitLiteral($2)}
}
| underscore_charsets HEX %prec UNARY
{
Expand Down
8 changes: 2 additions & 6 deletions go/vt/vtgate/evalengine/api_literal.go
Original file line number Diff line number Diff line change
Expand Up @@ -162,12 +162,8 @@ func parseBitNum(val []byte) ([]byte, error) {
if val[0] != '0' || val[1] != 'b' {
return nil, vterrors.Errorf(vtrpc.Code_INVALID_ARGUMENT, "malformed Bit literal: %q (missing 0b prefix)", val)
}
return parseBitLiteral(val[2:])
}

func parseBitLiteral(val []byte) ([]byte, error) {
var i big.Int
_, ok := i.SetString(hack.String(val), 2)
_, ok := i.SetString(hack.String(val)[2:], 2)
if !ok {
return nil, vterrors.Errorf(vtrpc.Code_INVALID_ARGUMENT, "malformed Bit literal: %q (not base 2)", val)
}
Expand Down Expand Up @@ -195,7 +191,7 @@ func NewLiteralBinaryFromHexNum(val []byte) (*Literal, error) {
}

func NewLiteralBinaryFromBit(val []byte) (*Literal, error) {
raw, err := parseBitLiteral(val)
raw, err := parseBitNum(val)
if err != nil {
return nil, err
}
Expand Down
5 changes: 0 additions & 5 deletions go/vt/vtgate/evalengine/compiler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -471,11 +471,6 @@ func TestCompilerSingle(t *testing.T) {
expression: `CONV(-1, -1.5e0, 3.141592653589793)`,
result: `VARCHAR("11112220022122120101211020120210210211220")`,
},
{
expression: `column0 between 10 and 20`,
values: []sqltypes.Value{sqltypes.NewInt16(15)},
result: `INT64(1)`,
},
{
expression: `column0 between 10 and 20`,
values: []sqltypes.Value{sqltypes.NULL},
Expand Down

0 comments on commit 7c48726

Please sign in to comment.