Skip to content

Commit 6e0ff81

Browse files
authored
parser: support DISTINCTROW(#4007)
1 parent 0a0a1a1 commit 6e0ff81

File tree

3 files changed

+62
-38
lines changed

3 files changed

+62
-38
lines changed

parser/misc.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -231,6 +231,7 @@ var tokenMap = map[string]int{
231231
"DESCRIBE": describe,
232232
"DISABLE": disable,
233233
"DISTINCT": distinct,
234+
"DISTINCTROW": distinctRow,
234235
"TIDB_SMJ": tidbSMJ,
235236
"TIDB_INLJ": tidbINLJ,
236237
"TIDB_VERSION": tidbVersion,

parser/parser.y

Lines changed: 34 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,7 @@ import (
9898
desc "DESC"
9999
describe "DESCRIBE"
100100
distinct "DISTINCT"
101+
distinctRow "DISTINCTROW"
101102
tidbSMJ "TIDB_SMJ"
102103
tidbINLJ "TIDB_INLJ"
103104
tidbVersion "TIDB_VERSION"
@@ -609,7 +610,10 @@ import (
609610
DeallocateStmt "Deallocate prepared statement"
610611
DefaultValueExpr "DefaultValueExpr(Now or Signed Literal)"
611612
DeleteFromStmt "DELETE FROM statement"
612-
DistinctOpt "Distinct option"
613+
DistinctOpt "Explicit distinct option"
614+
DefaultFalseDistinctOpt "Distinct option which defaults to false"
615+
DefaultTrueDistinctOpt "Distinct option which defaults to true"
616+
BuggyDefaultFalseDistinctOpt "Distinct option which accepts DISTINCT ALL and defaults to false"
613617
DoStmt "Do statement"
614618
DropDatabaseStmt "DROP DATABASE statement"
615619
DropIndexStmt "DROP INDEX statement"
@@ -729,7 +733,6 @@ import (
729733
SelectStmt "SELECT statement"
730734
SelectStmtCalcFoundRows "SELECT statement optional SQL_CALC_FOUND_ROWS"
731735
SelectStmtSQLCache "SELECT statement optional SQL_CAHCE/SQL_NO_CACHE"
732-
SelectStmtDistinct "SELECT statement optional DISTINCT clause"
733736
SelectStmtFieldList "SELECT statement field list"
734737
SelectStmtLimit "SELECT statement optional LIMIT clause"
735738
SelectStmtOpts "Select statement options"
@@ -852,6 +855,7 @@ import (
852855
TablesTerminalSym "{TABLE|TABLES}"
853856
IsolationLevel "Isolation level"
854857
ShowIndexKwd "Show index/indexs/key keyword"
858+
DistinctKwd "DISTINCT/DISTINCTROW keyword"
855859
FromOrIn "From or In"
856860
OptTable "Optional table keyword"
857861
OptInteger "Optional Integer keyword"
@@ -2422,7 +2426,7 @@ ReservedKeyword:
24222426
| "COLUMN" | "CONSTRAINT" | "CONVERT" | "CREATE" | "CROSS" | "CURRENT_DATE" | "CURRENT_TIME"
24232427
| "CURRENT_TIMESTAMP" | "CURRENT_USER" | "DATABASE" | "DATABASES" | "DAY_HOUR" | "DAY_MICROSECOND"
24242428
| "DAY_MINUTE" | "DAY_SECOND" | "DECIMAL" | "DEFAULT" | "DELETE" | "DESC" | "DESCRIBE"
2425-
| "DISTINCT" | "DIV" | "DOUBLE" | "DROP" | "DUAL" | "ELSE" | "ENCLOSED" | "ESCAPED"
2429+
| "DISTINCT" | "DISTINCTROW" | "DIV" | "DOUBLE" | "DROP" | "DUAL" | "ELSE" | "ENCLOSED" | "ESCAPED"
24262430
| "EXISTS" | "EXPLAIN" | "FALSE" | "FLOAT" | "FOR" | "FORCE" | "FOREIGN" | "FROM"
24272431
| "FULLTEXT" | "GENERATED" | "GRANT" | "GROUP" | "HAVING" | "HOUR_MICROSECOND" | "HOUR_MINUTE"
24282432
| "HOUR_SECOND" | "IF" | "IGNORE" | "IN" | "INDEX" | "INFILE" | "INNER" | "INSERT" | "INT" | "INTO" | "INTEGER"
@@ -2851,19 +2855,35 @@ FunctionCallConflict:
28512855
$$ = &ast.BinaryOperationExpr{Op: opcode.Mod, L: $3.(ast.ExprNode), R: $5.(ast.ExprNode)}
28522856
}
28532857

2858+
DistinctKwd:
2859+
"DISTINCT"
2860+
| "DISTINCTROW"
2861+
28542862
DistinctOpt:
2863+
"ALL"
28552864
{
28562865
$$ = false
28572866
}
2858-
| "ALL"
2867+
| DistinctKwd
2868+
{
2869+
$$ = true
2870+
}
2871+
2872+
DefaultFalseDistinctOpt:
28592873
{
28602874
$$ = false
28612875
}
2862-
| "DISTINCT"
2876+
| DistinctOpt
2877+
2878+
DefaultTrueDistinctOpt:
28632879
{
28642880
$$ = true
28652881
}
2866-
| "DISTINCT" "ALL"
2882+
| DistinctOpt
2883+
2884+
BuggyDefaultFalseDistinctOpt:
2885+
DefaultFalseDistinctOpt
2886+
| DistinctKwd "ALL"
28672887
{
28682888
$$ = true
28692889
}
@@ -3857,15 +3877,15 @@ TrimDirection:
38573877
}
38583878

38593879
FunctionCallAgg:
3860-
"AVG" '(' DistinctOpt Expression ')'
3880+
"AVG" '(' BuggyDefaultFalseDistinctOpt Expression ')'
38613881
{
38623882
$$ = &ast.AggregateFuncExpr{F: $1, Args: []ast.ExprNode{$4.(ast.ExprNode)}, Distinct: $3.(bool)}
38633883
}
38643884
| "BIT_XOR" '(' Expression ')'
38653885
{
38663886
$$ = &ast.AggregateFuncExpr{F: $1, Args: []ast.ExprNode{$3.(ast.ExprNode)}}
38673887
}
3868-
| "COUNT" '(' "DISTINCT" ExpressionList ')'
3888+
| "COUNT" '(' DistinctKwd ExpressionList ')'
38693889
{
38703890
$$ = &ast.AggregateFuncExpr{F: $1, Args: $4.([]ast.ExprNode), Distinct: true}
38713891
}
@@ -3882,19 +3902,19 @@ FunctionCallAgg:
38823902
args := []ast.ExprNode{ast.NewValueExpr(1)}
38833903
$$ = &ast.AggregateFuncExpr{F: $1, Args: args}
38843904
}
3885-
| "GROUP_CONCAT" '(' DistinctOpt ExpressionList ')'
3905+
| "GROUP_CONCAT" '(' BuggyDefaultFalseDistinctOpt ExpressionList ')'
38863906
{
38873907
$$ = &ast.AggregateFuncExpr{F: $1, Args: $4.([]ast.ExprNode), Distinct: $3.(bool)}
38883908
}
3889-
| "MAX" '(' DistinctOpt Expression ')'
3909+
| "MAX" '(' BuggyDefaultFalseDistinctOpt Expression ')'
38903910
{
38913911
$$ = &ast.AggregateFuncExpr{F: $1, Args: []ast.ExprNode{$4.(ast.ExprNode)}, Distinct: $3.(bool)}
38923912
}
3893-
| "MIN" '(' DistinctOpt Expression ')'
3913+
| "MIN" '(' BuggyDefaultFalseDistinctOpt Expression ')'
38943914
{
38953915
$$ = &ast.AggregateFuncExpr{F: $1, Args: []ast.ExprNode{$4.(ast.ExprNode)}, Distinct: $3.(bool)}
38963916
}
3897-
| "SUM" '(' DistinctOpt Expression ')'
3917+
| "SUM" '(' BuggyDefaultFalseDistinctOpt Expression ')'
38983918
{
38993919
$$ = &ast.AggregateFuncExpr{F: $1, Args: []ast.ExprNode{$4.(ast.ExprNode)}, Distinct: $3.(bool)}
39003920
}
@@ -4726,22 +4746,9 @@ SelectStmtLimit:
47264746
$$ = &ast.Limit{Offset: $4.(ast.ExprNode), Count: $2.(ast.ExprNode)}
47274747
}
47284748

4729-
SelectStmtDistinct:
4730-
/* EMPTY */
4731-
{
4732-
$$ = false
4733-
}
4734-
| "ALL"
4735-
{
4736-
$$ = false
4737-
}
4738-
| "DISTINCT"
4739-
{
4740-
$$ = true
4741-
}
47424749

47434750
SelectStmtOpts:
4744-
TableOptimizerHints SelectStmtDistinct Priority SelectStmtSQLCache SelectStmtCalcFoundRows
4751+
TableOptimizerHints DefaultFalseDistinctOpt Priority SelectStmtSQLCache SelectStmtCalcFoundRows
47454752
{
47464753
opt := &ast.SelectStmtOpts{}
47474754
if $1 != nil {
@@ -4937,17 +4944,7 @@ UnionSelect:
49374944
}
49384945

49394946
UnionOpt:
4940-
{
4941-
$$ = true
4942-
}
4943-
| "ALL"
4944-
{
4945-
$$ = false
4946-
}
4947-
| "DISTINCT"
4948-
{
4949-
$$ = true
4950-
}
4947+
DefaultTrueDistinctOpt
49514948

49524949

49534950
/********************Set Statement*******************************/

parser/parser_test.go

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ func (s *testParserSuite) TestSimple(c *C) {
4646
"column", "constraint", "convert", "create", "cross", "current_date", "current_time",
4747
"current_timestamp", "current_user", "database", "databases", "day_hour", "day_microsecond",
4848
"day_minute", "day_second", "decimal", "default", "delete", "desc", "describe",
49-
"distinct", "div", "double", "drop", "dual", "else", "enclosed", "escaped",
49+
"distinct", "distinctRow", "div", "double", "drop", "dual", "else", "enclosed", "escaped",
5050
"exists", "explain", "false", "float", "for", "force", "foreign", "from",
5151
"fulltext", "grant", "group", "having", "hour_microsecond", "hour_minute",
5252
"hour_second", "if", "ignore", "in", "index", "infile", "inner", "insert", "int", "into", "integer",
@@ -218,6 +218,10 @@ func (s *testParserSuite) TestDMLStmt(c *C) {
218218
// 30
219219
{"SELECT DISTINCTS * FROM t", false},
220220
{"SELECT DISTINCT * FROM t", true},
221+
{"SELECT DISTINCTROW * FROM t", true},
222+
{"SELECT ALL * FROM t", true},
223+
{"SELECT DISTINCT ALL * FROM t", false},
224+
{"SELECT DISTINCTROW ALL * FROM t", false},
221225
{"INSERT INTO foo (a) VALUES (42)", true},
222226
{"INSERT INTO foo (a,) VALUES (42,)", false},
223227
// 35
@@ -1014,26 +1018,45 @@ func (s *testParserSuite) TestBuiltin(c *C) {
10141018
// for aggregate functions
10151019
{`select avg(), avg(c1,c2) from t;`, false},
10161020
{`select avg(distinct c1) from t;`, true},
1021+
{`select avg(distinctrow c1) from t;`, true},
1022+
{`select avg(distinct all c1) from t;`, true},
1023+
{`select avg(distinctrow all c1) from t;`, true},
10171024
{`select avg(c2) from t;`, true},
10181025
{`select bit_xor(c1) from t;`, true},
10191026
{`select bit_xor(), bit_xor(distinct c1) from t;`, false},
1027+
{`select bit_xor(), bit_xor(distinctrow c1) from t;`, false},
1028+
{`select bit_xor(), bit_xor(all c1) from t;`, false},
10201029
{`select max(c1,c2) from t;`, false},
10211030
{`select max(distinct c1) from t;`, true},
1031+
{`select max(distinctrow c1) from t;`, true},
1032+
{`select max(distinct all c1) from t;`, true},
1033+
{`select max(distinctrow all c1) from t;`, true},
10221034
{`select max(c2) from t;`, true},
10231035
{`select min(c1,c2) from t;`, false},
10241036
{`select min(distinct c1) from t;`, true},
1037+
{`select min(distinctrow c1) from t;`, true},
1038+
{`select min(distinct all c1) from t;`, true},
1039+
{`select min(distinctrow all c1) from t;`, true},
10251040
{`select min(c2) from t;`, true},
10261041
{`select sum(c1,c2) from t;`, false},
10271042
{`select sum(distinct c1) from t;`, true},
1043+
{`select sum(distinctrow c1) from t;`, true},
1044+
{`select sum(distinct all c1) from t;`, true},
1045+
{`select sum(distinctrow all c1) from t;`, true},
10281046
{`select sum(c2) from t;`, true},
10291047
{`select count(c1) from t;`, true},
10301048
{`select count(distinct *) from t;`, false},
1049+
{`select count(distinctrow *) from t;`, false},
10311050
{`select count(*) from t;`, true},
10321051
{`select count(distinct c1, c2) from t;`, true},
1052+
{`select count(distinctrow c1, c2) from t;`, true},
10331053
{`select count(c1, c2) from t;`, false},
10341054
{`select count(all c1) from t;`, true},
1055+
{`select count(distinct all c1) from t;`, false},
1056+
{`select count(distinctrow all c1) from t;`, false},
10351057
{`select group_concat(c2,c1) from t group by c1;`, true},
10361058
{`select group_concat(distinct c2,c1) from t group by c1;`, true},
1059+
{`select group_concat(distinctrow c2,c1) from t group by c1;`, true},
10371060

10381061
// for encryption and compression functions
10391062
{`select AES_ENCRYPT('text',UNHEX('F3229A0B371ED2D9441B830D21A390C3'))`, true},
@@ -1562,7 +1585,10 @@ func (s *testParserSuite) TestUnion(c *C) {
15621585
{"select c1 from t1 union (select c2 from t2) limit 1, 1", true},
15631586
{"select c1 from t1 union (select c2 from t2) order by c1 limit 1", true},
15641587
{"(select c1 from t1) union distinct select c2 from t2", true},
1588+
{"(select c1 from t1) union distinctrow select c2 from t2", true},
15651589
{"(select c1 from t1) union all select c2 from t2", true},
1590+
{"(select c1 from t1) union distinct all select c2 from t2", false},
1591+
{"(select c1 from t1) union distinctrow all select c2 from t2", false},
15661592
{"(select c1 from t1) union (select c2 from t2) order by c1 union select c3 from t3", false},
15671593
{"(select c1 from t1) union (select c2 from t2) limit 1 union select c3 from t3", false},
15681594
{"(select c1 from t1) union select c2 from t2 union (select c3 from t3) order by c1 limit 1", true},

0 commit comments

Comments
 (0)