Skip to content

Commit

Permalink
executor: add SHOW BUILTINS command (#12941)
Browse files Browse the repository at this point in the history
  • Loading branch information
ekalinin authored and sre-bot committed Dec 6, 2019
1 parent 67b4072 commit 0a22034
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 0 deletions.
9 changes: 9 additions & 0 deletions executor/show.go
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,8 @@ func (e *ShowExec) fetchAll(ctx context.Context) error {
return nil
case ast.ShowRegions:
return e.fetchShowTableRegions()
case ast.ShowBuiltins:
return e.fetchShowBuiltins()
}
return nil
}
Expand Down Expand Up @@ -1327,3 +1329,10 @@ func (e *ShowExec) fillRegionsToChunk(regions []regionMeta) {
e.result.AppendInt64(10, regions[i].approximateKeys)
}
}

func (e *ShowExec) fetchShowBuiltins() error {
for _, f := range expression.GetBuiltinList() {
e.appendRow([]interface{}{f})
}
return nil
}
10 changes: 10 additions & 0 deletions executor/show_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -639,3 +639,13 @@ func (s *testSuite5) TestShowEscape(c *C) {
tk.MustExec("rename table \"t`abl\"\"e\" to t")
tk.MustExec("set sql_mode=@old_sql_mode")
}

func (s *testSuite5) TestShowBuiltin(c *C) {
tk := testkit.NewTestKit(c, s.store)
res := tk.MustQuery("show builtins;")
c.Assert(res, NotNil)
rows := res.Rows()
c.Assert(262, Equals, len(rows))
c.Assert("abs", Equals, rows[0][0].(string))
c.Assert("yearweek", Equals, rows[261][0].(string))
}
29 changes: 29 additions & 0 deletions expression/builtin.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@
package expression

import (
"sort"
"strings"
"sync"

"github.com/pingcap/errors"
Expand Down Expand Up @@ -775,3 +777,30 @@ func IsFunctionSupported(name string) bool {
_, ok := funcs[name]
return ok
}

// GetBuiltinList returns a list of builtin functions
func GetBuiltinList() []string {
res := make([]string, 0, len(funcs))
notImplementedFunctions := []string{ast.RowFunc}
for funcName := range funcs {
skipFunc := false
// Skip not implemented functions
for _, notImplFunc := range notImplementedFunctions {
if funcName == notImplFunc {
skipFunc = true
}
}
// Skip literal functions
// (their names are not readable: 'tidb`.(dateliteral, for example)
// See: https://github.com/pingcap/parser/pull/591
if strings.HasPrefix(funcName, "'tidb`.(") {
skipFunc = true
}
if skipFunc {
continue
}
res = append(res, funcName)
}
sort.Strings(res)
return res
}
3 changes: 3 additions & 0 deletions planner/core/planbuilder.go
Original file line number Diff line number Diff line change
Expand Up @@ -2822,6 +2822,9 @@ func buildShowSchema(s *ast.ShowStmt, isView bool) (schema *expression.Schema, o
case ast.ShowAnalyzeStatus:
names = []string{"Table_schema", "Table_name", "Partition_name", "Job_info", "Processed_rows", "Start_time", "State"}
ftypes = []byte{mysql.TypeVarchar, mysql.TypeVarchar, mysql.TypeVarchar, mysql.TypeVarchar, mysql.TypeLonglong, mysql.TypeDatetime, mysql.TypeVarchar}
case ast.ShowBuiltins:
names = []string{"Supported_builtin_functions"}
ftypes = []byte{mysql.TypeVarchar}
}

schema = expression.NewSchema(make([]*expression.Column, 0, len(names))...)
Expand Down

0 comments on commit 0a22034

Please sign in to comment.