[parser] parser: add WEIGHT_STRING() function (#743)

This commit is contained in:
bb7133
2020-02-17 14:05:34 +08:00
committed by Ti Chi Robot
parent 9a0826d097
commit 9fe4e83705
5 changed files with 7993 additions and 7901 deletions

View File

@ -226,6 +226,7 @@ const (
CharLength = "char_length"
CharacterLength = "character_length"
FindInSet = "find_in_set"
WeightString = "weight_string"
// information functions
Benchmark = "benchmark"
@ -420,6 +421,19 @@ func (n *FuncCallExpr) Restore(ctx *format.RestoreCtx) error {
return errors.Annotatef(err, "An error occurred while restore FuncCallExpr.Args[0]")
}
}
case WeightString:
if err := n.Args[0].Restore(ctx); err != nil {
return errors.Annotatef(err, "An error occurred while restore FuncCallExpr.(WEIGHT_STRING).Args[0]")
}
if len(n.Args) == 3 {
ctx.WriteKeyWord(" AS ")
ctx.WriteKeyWord(n.Args[1].(ValueExpr).GetValue().(string))
ctx.WritePlain("(")
if err := n.Args[2].Restore(ctx); err != nil {
return errors.Annotatef(err, "An error occurred while restore FuncCallExpr.(WEIGHT_STRING).Args[2]")
}
ctx.WritePlain(")")
}
default:
for i, argv := range n.Args {
if i != 0 {

View File

@ -84,6 +84,16 @@ func (ts *testFunctionsSuite) TestFuncCallExprRestore(c *C) {
{"next value for sequence", "NEXTVAL(`sequence`)"},
{"NeXt vAluE for seQuEncE2", "NEXTVAL(`seQuEncE2`)"},
{"NeXt vAluE for test.seQuEncE2", "NEXTVAL(`test`.`seQuEncE2`)"},
{"weight_string(a)", "WEIGHT_STRING(`a`)"},
{"Weight_stRing(test.a)", "WEIGHT_STRING(`test`.`a`)"},
{"weight_string('a')", "WEIGHT_STRING('a')"},
// TODO(bb7133): collate for literal values cannot be restored.
//{"weight_string(_utf8 'a' collate utf8_general_ci)", "WEIGHT_STRING(_UTF8'a' COLLATE utf8_general_ci)"},
{"weight_string(_utf8 'a')", "WEIGHT_STRING(_UTF8'a')"},
{"weight_string(a as char(5))", "WEIGHT_STRING(`a` AS CHAR(5))"},
{"weight_string(a as character(5))", "WEIGHT_STRING(`a` AS CHAR(5))"},
{"weight_string(a as binary(5))", "WEIGHT_STRING(`a` AS BINARY(5))"},
{"hex(weight_string('abc' as binary(5)))", "HEX(WEIGHT_STRING('abc' AS BINARY(5)))"},
}
extractNodeFunc := func(node Node) Node {
return node.(*SelectStmt).Fields.Fields[0].Expr

View File

@ -687,6 +687,7 @@ var tokenMap = map[string]int{
"WARNINGS": warnings,
"ERRORS": identSQLErrors,
"WEEK": week,
"WEIGHT_STRING": weightString,
"WHEN": when,
"WHERE": where,
"WIDTH": width,

File diff suppressed because it is too large Load Diff

View File

@ -548,6 +548,7 @@ import (
bindings "BINDINGS"
warnings "WARNINGS"
without "WITHOUT"
weightString "WEIGHT_STRING"
identSQLErrors "ERRORS"
week "WEEK"
yearType "YEAR"
@ -4718,6 +4719,7 @@ UnReservedKeyword:
| "YEAR"
| "MODE"
| "WEEK"
| "WEIGHT_STRING"
| "ANY"
| "SOME"
| "USER"
@ -5858,6 +5860,27 @@ FunctionCallNonKeyword:
Args: []ast.ExprNode{$6, $4, direction},
}
}
| weightString '(' Expression ')'
{
$$ = &ast.FuncCallExpr{
FnName: model.NewCIStr($1),
Args: []ast.ExprNode{$3},
}
}
| weightString '(' Expression "AS" Char FieldLen ')'
{
$$ = &ast.FuncCallExpr{
FnName: model.NewCIStr($1),
Args: []ast.ExprNode{$3, ast.NewValueExpr("CHAR"), ast.NewValueExpr($6)},
}
}
| weightString '(' Expression "AS" "BINARY" FieldLen ')'
{
$$ = &ast.FuncCallExpr{
FnName: model.NewCIStr($1),
Args: []ast.ExprNode{$3, ast.NewValueExpr("BINARY"), ast.NewValueExpr($6)},
}
}
| FunctionNameSequence
GetFormatSelector: