From 3bb2107df438fd2c3b5291cbf3a51dd64e597140 Mon Sep 17 00:00:00 2001 From: tiancaiamao Date: Thu, 13 Sep 2018 16:57:50 +0800 Subject: [PATCH 1/3] parser,ast: parse `admin show log` statement Add the following new syntax: * admin show log top [user | internal | all] N * admin show log recent N --- ast/misc.go | 22 ++++++++++++++++ parser/misc.go | 4 +++ parser/parser.y | 60 +++++++++++++++++++++++++++++++++++++++++-- parser/parser_test.go | 5 ++++ 4 files changed, 89 insertions(+), 2 deletions(-) diff --git a/ast/misc.go b/ast/misc.go index 2e89eeaec6945..9cd6e10f35212 100644 --- a/ast/misc.go +++ b/ast/misc.go @@ -618,6 +618,7 @@ const ( AdminCheckIndexRange AdminShowDDLJobQueries AdminChecksumTable + AdminShowLog ) // HandleRange represents a range where handle value >= Begin and < End. @@ -626,6 +627,26 @@ type HandleRange struct { End int64 } +// ShowLogType defines the type for SlowLog. +type ShowLogType int + +const ( + // ShowLogTop is a ShowLogType constant. + ShowLogTop ShowLogType = iota + // ShowLogRecent is a ShowLogType constant. + ShowLogRecent +) + +// ShowLog is used for the following command: +// admin show log top [user | internal | all] N +// admin show log recent N +type ShowLog struct { + Tp ShowLogType + Count uint64 + // "user" | "internal" | "all", default is user + Kind string +} + // AdminStmt is the struct for Admin statement. type AdminStmt struct { stmtNode @@ -637,6 +658,7 @@ type AdminStmt struct { JobNumber int64 HandleRanges []HandleRange + ShowLog *ShowLog } // Accept implements Node Accept interface. diff --git a/parser/misc.go b/parser/misc.go index 8de1a66e4754b..96e4478a4b8d6 100644 --- a/parser/misc.go +++ b/parser/misc.go @@ -300,6 +300,7 @@ var tokenMap = map[string]int{ "INT8": int8Type, "INTEGER": integerType, "INTERVAL": interval, + "INTERNAL": internal, "INTO": into, "INVOKER": invoker, "IS": is, @@ -324,6 +325,7 @@ var tokenMap = map[string]int{ "LOCALTIME": localTime, "LOCALTIMESTAMP": localTs, "LOCK": lock, + "LOG": log, "LONG": long, "LONGBLOB": longblobType, "LONGTEXT": longtextType, @@ -392,6 +394,7 @@ var tokenMap = map[string]int{ "RECOVER": recover, "READ": read, "REAL": realType, + "RECENT": recent, "REDUNDANT": redundant, "REFERENCES": references, "REGEXP": regexpKwd, @@ -472,6 +475,7 @@ var tokenMap = map[string]int{ "TINYINT": tinyIntType, "TINYTEXT": tinytextType, "TO": to, + "TOP": top, "TRACE": trace, "TRAILING": trailing, "TRANSACTION": transaction, diff --git a/parser/parser.y b/parser/parser.y index 49ba524035805..05c5bd24b42a0 100644 --- a/parser/parser.y +++ b/parser/parser.y @@ -420,16 +420,20 @@ import ( getFormat "GET_FORMAT" groupConcat "GROUP_CONCAT" inplace "INPLACE" + internal "INTERNAL" + log "LOG" min "MIN" max "MAX" maxExecutionTime "MAX_EXECUTION_TIME" now "NOW" position "POSITION" + recent "RECENT" subDate "SUBDATE" sum "SUM" substring "SUBSTRING" timestampAdd "TIMESTAMPADD" timestampDiff "TIMESTAMPDIFF" + top "TOP" trim "TRIM" /* The following tokens belong to TiDBKeyword. */ @@ -575,6 +579,7 @@ import ( UseStmt "USE statement" %type + AdminShowLog "Admin Show Log statement" AlterTableOptionListOpt "alter table option list opt" AlterTableSpec "Alter table specification" AlterTableSpecList "Alter table specification list" @@ -2824,8 +2829,8 @@ TiDBKeyword: "ADMIN" | "BUCKETS" | "CANCEL" | "DDL" | "JOBS" | "JOB" | "STATS" | "STATS_META" | "STATS_HISTOGRAMS" | "STATS_BUCKETS" | "STATS_HEALTHY" | "TIDB" | "TIDB_HJ" | "TIDB_SMJ" | "TIDB_INLJ" NotKeywordToken: - "ADDDATE" | "BIT_AND" | "BIT_OR" | "BIT_XOR" | "CAST" | "COPY" | "COUNT" | "CURTIME" | "DATE_ADD" | "DATE_SUB" | "EXTRACT" | "GET_FORMAT" | "GROUP_CONCAT" -| "INPLACE" |"MIN" | "MAX" | "MAX_EXECUTION_TIME" | "NOW" | "POSITION" | "SUBDATE" | "SUBSTRING" | "SUM" | "TIMESTAMPADD" | "TIMESTAMPDIFF" | "TRIM" + "ADDDATE" | "BIT_AND" | "BIT_OR" | "BIT_XOR" | "CAST" | "COPY" | "COUNT" | "CURTIME" | "DATE_ADD" | "DATE_SUB" | "EXTRACT" | "GET_FORMAT" | "GROUP_CONCAT" | "INPLACE" | "INTERNAL" +| "LOG" |"MIN" | "MAX" | "MAX_EXECUTION_TIME" | "NOW" | "RECENT" | "POSITION" | "SUBDATE" | "SUBSTRING" | "SUM" | "TIMESTAMPADD" | "TIMESTAMPDIFF" | "TOP" | "TRIM" /************************************************************************************ * @@ -3606,6 +3611,10 @@ FunctionCallNonKeyword: Args: []ast.ExprNode{ast.NewValueExpr($3), $5}, } } +| "LOG" '(' ExpressionListOpt ')' + { + $$ = &ast.FuncCallExpr{FnName: model.NewCIStr($1), Args: $3.([]ast.ExprNode)} + } | builtinPosition '(' BitExpr "IN" Expression ')' { $$ = &ast.FuncCallExpr{FnName: model.NewCIStr($1), Args: []ast.ExprNode{$3, $5}} @@ -5221,6 +5230,53 @@ AdminStmt: JobIDs: $6.([]int64), } } +| "ADMIN" "SHOW" "LOG" AdminShowLog + { + $$ = &ast.AdminStmt{ + Tp: ast.AdminShowLog, + ShowLog: $4.(*ast.ShowLog), + } + } + +AdminShowLog: + "RECENT" NUM + { + $$ = &ast.ShowLog{ + Tp: ast.ShowLogRecent, + Count: getUint64FromNUM($2), + } + } +| "TOP" NUM + { + $$ = &ast.ShowLog{ + Tp: ast.ShowLogTop, + Count: getUint64FromNUM($2), + } + } +| "TOP" "USER" NUM + { + $$ = &ast.ShowLog{ + Tp: ast.ShowLogTop, + Kind: "user", + Count: getUint64FromNUM($3), + } + } +| "TOP" "INTERNAL" NUM + { + $$ = &ast.ShowLog{ + Tp: ast.ShowLogTop, + Kind: "internal", + Count: getUint64FromNUM($3), + } + } +| "TOP" "ALL" NUM + { + $$ = &ast.ShowLog{ + Tp: ast.ShowLogTop, + Kind: "all", + Count: getUint64FromNUM($3), + } + } HandleRangeList: HandleRange diff --git a/parser/parser_test.go b/parser/parser_test.go index c649c6615deb7..7e55646817f13 100644 --- a/parser/parser_test.go +++ b/parser/parser_test.go @@ -440,6 +440,11 @@ func (s *testParserSuite) TestDMLStmt(c *C) { {"admin cancel ddl jobs 1, 2", true}, {"admin recover index t1 idx_a", true}, {"admin cleanup index t1 idx_a", true}, + {"admin show log top 3", true}, + {"admin show log top user 5", true}, + {"admin show log top internal 7", true}, + {"admin show log top all 9", true}, + {"admin show log recent 11", true}, // for on duplicate key update {"INSERT INTO t (a,b,c) VALUES (1,2,3),(4,5,6) ON DUPLICATE KEY UPDATE c=VALUES(a)+VALUES(b);", true}, From da18d4b7960d6ec6bed5a026625f51f3a1956828 Mon Sep 17 00:00:00 2001 From: tiancaiamao Date: Fri, 21 Sep 2018 11:18:29 +0800 Subject: [PATCH 2/3] address comment --- ast/misc.go | 28 +++++++++++++-------------- parser/misc.go | 2 +- parser/parser.y | 44 ++++++++++++++++--------------------------- parser/parser_test.go | 9 ++++----- 4 files changed, 35 insertions(+), 48 deletions(-) diff --git a/ast/misc.go b/ast/misc.go index 9cd6e10f35212..9b3b725472036 100644 --- a/ast/misc.go +++ b/ast/misc.go @@ -618,7 +618,7 @@ const ( AdminCheckIndexRange AdminShowDDLJobQueries AdminChecksumTable - AdminShowLog + AdminShowSlow ) // HandleRange represents a range where handle value >= Begin and < End. @@ -627,23 +627,23 @@ type HandleRange struct { End int64 } -// ShowLogType defines the type for SlowLog. -type ShowLogType int +// ShowSlowType defines the type for SlowLog. +type ShowSlowType int const ( - // ShowLogTop is a ShowLogType constant. - ShowLogTop ShowLogType = iota - // ShowLogRecent is a ShowLogType constant. - ShowLogRecent + // ShowSlowTop is a ShowSlowType constant. + ShowSlowTop ShowSlowType = iota + // ShowSlowRecent is a ShowSlowType constant. + ShowSlowRecent ) -// ShowLog is used for the following command: -// admin show log top [user | internal | all] N -// admin show log recent N -type ShowLog struct { - Tp ShowLogType +// ShowSlow is used for the following command: +// admin show slow top [ internal | all] N +// admin show slow recent N +type ShowSlow struct { + Tp ShowSlowType Count uint64 - // "user" | "internal" | "all", default is user + // May be "internal" or "all", default is "" Kind string } @@ -658,7 +658,7 @@ type AdminStmt struct { JobNumber int64 HandleRanges []HandleRange - ShowLog *ShowLog + ShowSlow *ShowSlow } // Accept implements Node Accept interface. diff --git a/parser/misc.go b/parser/misc.go index 96e4478a4b8d6..51c623016dd05 100644 --- a/parser/misc.go +++ b/parser/misc.go @@ -325,7 +325,6 @@ var tokenMap = map[string]int{ "LOCALTIME": localTime, "LOCALTIMESTAMP": localTs, "LOCK": lock, - "LOG": log, "LONG": long, "LONGBLOB": longblobType, "LONGTEXT": longtextType, @@ -429,6 +428,7 @@ var tokenMap = map[string]int{ "SHOW": show, "SIGNED": signed, "SLAVE": slave, + "SLOW": slow, "SMALLINT": smallIntType, "SNAPSHOT": snapshot, "SOME": some, diff --git a/parser/parser.y b/parser/parser.y index 05c5bd24b42a0..dca8e9ec9b720 100644 --- a/parser/parser.y +++ b/parser/parser.y @@ -370,6 +370,7 @@ import ( shared "SHARED" signed "SIGNED" slave "SLAVE" + slow "SLOW" snapshot "SNAPSHOT" sqlCache "SQL_CACHE" sqlNoCache "SQL_NO_CACHE" @@ -421,7 +422,6 @@ import ( groupConcat "GROUP_CONCAT" inplace "INPLACE" internal "INTERNAL" - log "LOG" min "MIN" max "MAX" maxExecutionTime "MAX_EXECUTION_TIME" @@ -579,7 +579,7 @@ import ( UseStmt "USE statement" %type - AdminShowLog "Admin Show Log statement" + AdminShowSlow "Admin Show Slow statement" AlterTableOptionListOpt "alter table option list opt" AlterTableSpec "Alter table specification" AlterTableSpecList "Alter table specification list" @@ -2820,7 +2820,7 @@ UnReservedKeyword: | "REPEATABLE" | "COMMITTED" | "UNCOMMITTED" | "ONLY" | "SERIALIZABLE" | "LEVEL" | "VARIABLES" | "SQL_CACHE" | "INDEXES" | "PROCESSLIST" | "SQL_NO_CACHE" | "DISABLE" | "ENABLE" | "REVERSE" | "PRIVILEGES" | "NO" | "BINLOG" | "FUNCTION" | "VIEW" | "MODIFY" | "EVENTS" | "PARTITIONS" | "NONE" | "SUPER" | "EXCLUSIVE" | "STATS_PERSISTENT" | "ROW_COUNT" | "COALESCE" | "MONTH" | "PROCESS" | "PROFILES" -| "MICROSECOND" | "MINUTE" | "PLUGINS" | "QUERY" | "QUERIES" | "SECOND" | "SEPARATOR" | "SHARE" | "SHARED" | "MAX_CONNECTIONS_PER_HOUR" | "MAX_QUERIES_PER_HOUR" | "MAX_UPDATES_PER_HOUR" +| "MICROSECOND" | "MINUTE" | "PLUGINS" | "QUERY" | "QUERIES" | "SECOND" | "SEPARATOR" | "SHARE" | "SHARED" | "SLOW" | "MAX_CONNECTIONS_PER_HOUR" | "MAX_QUERIES_PER_HOUR" | "MAX_UPDATES_PER_HOUR" | "MAX_USER_CONNECTIONS" | "REPLICATION" | "CLIENT" | "SLAVE" | "RELOAD" | "TEMPORARY" | "ROUTINE" | "EVENT" | "ALGORITHM" | "DEFINER" | "INVOKER" | "MERGE" | "TEMPTABLE" | "UNDEFINED" | "SECURITY" | "CASCADED" | "RECOVER" @@ -2830,7 +2830,7 @@ TiDBKeyword: NotKeywordToken: "ADDDATE" | "BIT_AND" | "BIT_OR" | "BIT_XOR" | "CAST" | "COPY" | "COUNT" | "CURTIME" | "DATE_ADD" | "DATE_SUB" | "EXTRACT" | "GET_FORMAT" | "GROUP_CONCAT" | "INPLACE" | "INTERNAL" -| "LOG" |"MIN" | "MAX" | "MAX_EXECUTION_TIME" | "NOW" | "RECENT" | "POSITION" | "SUBDATE" | "SUBSTRING" | "SUM" | "TIMESTAMPADD" | "TIMESTAMPDIFF" | "TOP" | "TRIM" +|"MIN" | "MAX" | "MAX_EXECUTION_TIME" | "NOW" | "RECENT" | "POSITION" | "SUBDATE" | "SUBSTRING" | "SUM" | "TIMESTAMPADD" | "TIMESTAMPDIFF" | "TOP" | "TRIM" /************************************************************************************ * @@ -3611,10 +3611,6 @@ FunctionCallNonKeyword: Args: []ast.ExprNode{ast.NewValueExpr($3), $5}, } } -| "LOG" '(' ExpressionListOpt ')' - { - $$ = &ast.FuncCallExpr{FnName: model.NewCIStr($1), Args: $3.([]ast.ExprNode)} - } | builtinPosition '(' BitExpr "IN" Expression ')' { $$ = &ast.FuncCallExpr{FnName: model.NewCIStr($1), Args: []ast.ExprNode{$3, $5}} @@ -5230,49 +5226,41 @@ AdminStmt: JobIDs: $6.([]int64), } } -| "ADMIN" "SHOW" "LOG" AdminShowLog +| "ADMIN" "SHOW" "SLOW" AdminShowSlow { $$ = &ast.AdminStmt{ - Tp: ast.AdminShowLog, - ShowLog: $4.(*ast.ShowLog), + Tp: ast.AdminShowSlow, + ShowSlow: $4.(*ast.ShowSlow), } } -AdminShowLog: +AdminShowSlow: "RECENT" NUM { - $$ = &ast.ShowLog{ - Tp: ast.ShowLogRecent, + $$ = &ast.ShowSlow{ + Tp: ast.ShowSlowRecent, Count: getUint64FromNUM($2), } } | "TOP" NUM { - $$ = &ast.ShowLog{ - Tp: ast.ShowLogTop, + $$ = &ast.ShowSlow{ + Tp: ast.ShowSlowTop, Count: getUint64FromNUM($2), } } -| "TOP" "USER" NUM - { - $$ = &ast.ShowLog{ - Tp: ast.ShowLogTop, - Kind: "user", - Count: getUint64FromNUM($3), - } - } | "TOP" "INTERNAL" NUM { - $$ = &ast.ShowLog{ - Tp: ast.ShowLogTop, + $$ = &ast.ShowSlow{ + Tp: ast.ShowSlowTop, Kind: "internal", Count: getUint64FromNUM($3), } } | "TOP" "ALL" NUM { - $$ = &ast.ShowLog{ - Tp: ast.ShowLogTop, + $$ = &ast.ShowSlow{ + Tp: ast.ShowSlowTop, Kind: "all", Count: getUint64FromNUM($3), } diff --git a/parser/parser_test.go b/parser/parser_test.go index 7e55646817f13..ac33937635704 100644 --- a/parser/parser_test.go +++ b/parser/parser_test.go @@ -440,11 +440,10 @@ func (s *testParserSuite) TestDMLStmt(c *C) { {"admin cancel ddl jobs 1, 2", true}, {"admin recover index t1 idx_a", true}, {"admin cleanup index t1 idx_a", true}, - {"admin show log top 3", true}, - {"admin show log top user 5", true}, - {"admin show log top internal 7", true}, - {"admin show log top all 9", true}, - {"admin show log recent 11", true}, + {"admin show slow top 3", true}, + {"admin show slow top internal 7", true}, + {"admin show slow top all 9", true}, + {"admin show slow recent 11", true}, // for on duplicate key update {"INSERT INTO t (a,b,c) VALUES (1,2,3),(4,5,6) ON DUPLICATE KEY UPDATE c=VALUES(a)+VALUES(b);", true}, From 0b59e45f19d891408c2c587bef2ed1d30e772e2a Mon Sep 17 00:00:00 2001 From: tiancaiamao Date: Wed, 26 Sep 2018 12:48:45 +0800 Subject: [PATCH 3/3] address comment --- ast/misc.go | 17 ++++++++++++++--- parser/parser.y | 5 +++-- 2 files changed, 17 insertions(+), 5 deletions(-) diff --git a/ast/misc.go b/ast/misc.go index 9b3b725472036..98ae76ca53ac0 100644 --- a/ast/misc.go +++ b/ast/misc.go @@ -627,7 +627,7 @@ type HandleRange struct { End int64 } -// ShowSlowType defines the type for SlowLog. +// ShowSlowType defines the type for SlowSlow statement. type ShowSlowType int const ( @@ -637,14 +637,25 @@ const ( ShowSlowRecent ) +// ShowSlowKind defines the kind for SlowSlow statement when the type is ShowSlowTop. +type ShowSlowKind int + +const ( + // ShowSlowKindDefault is a ShowSlowKind constant. + ShowSlowKindDefault ShowSlowKind = iota + // ShowSlowKindInternal is a ShowSlowKind constant. + ShowSlowKindInternal + // ShowSlowKindAll is a ShowSlowKind constant. + ShowSlowKindAll +) + // ShowSlow is used for the following command: // admin show slow top [ internal | all] N // admin show slow recent N type ShowSlow struct { Tp ShowSlowType Count uint64 - // May be "internal" or "all", default is "" - Kind string + Kind ShowSlowKind } // AdminStmt is the struct for Admin statement. diff --git a/parser/parser.y b/parser/parser.y index dca8e9ec9b720..786c4be6ae505 100644 --- a/parser/parser.y +++ b/parser/parser.y @@ -5246,6 +5246,7 @@ AdminShowSlow: { $$ = &ast.ShowSlow{ Tp: ast.ShowSlowTop, + Kind: ast.ShowSlowKindDefault, Count: getUint64FromNUM($2), } } @@ -5253,7 +5254,7 @@ AdminShowSlow: { $$ = &ast.ShowSlow{ Tp: ast.ShowSlowTop, - Kind: "internal", + Kind: ast.ShowSlowKindInternal, Count: getUint64FromNUM($3), } } @@ -5261,7 +5262,7 @@ AdminShowSlow: { $$ = &ast.ShowSlow{ Tp: ast.ShowSlowTop, - Kind: "all", + Kind: ast.ShowSlowKindAll, Count: getUint64FromNUM($3), } }