github.com/insionng/yougam@v0.0.0-20170714101924-2bc18d833463/libraries/pingcap/tidb/evaluator/builtin.go (about)

     1  // Copyright 2013 The ql Authors. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSES/QL-LICENSE file.
     4  
     5  // Copyright 2015 PingCAP, Inc.
     6  //
     7  // Licensed under the Apache License, Version 2.0 (the "License");
     8  // you may not use this file except in compliance with the License.
     9  // You may obtain a copy of the License at
    10  //
    11  //     http://www.apache.org/licenses/LICENSE-2.0
    12  //
    13  // Unless required by applicable law or agreed to in writing, software
    14  // distributed under the License is distributed on an "AS IS" BASIS,
    15  // See the License for the specific language governing permissions and
    16  // limitations under the License.
    17  
    18  package evaluator
    19  
    20  import (
    21  	"strings"
    22  
    23  	"github.com/insionng/yougam/libraries/pingcap/tidb/context"
    24  	"github.com/insionng/yougam/libraries/pingcap/tidb/util/types"
    25  )
    26  
    27  // BuiltinFunc is the function signature for builtin functions
    28  type BuiltinFunc func([]types.Datum, context.Context) (types.Datum, error)
    29  
    30  // Func is for a builtin function.
    31  type Func struct {
    32  	// F is the specific calling function.
    33  	F BuiltinFunc
    34  	// MinArgs is the minimal arguments needed,
    35  	MinArgs int
    36  	// MaxArgs is the maximal arguments needed, -1 for infinity.
    37  	MaxArgs int
    38  }
    39  
    40  // Funcs holds all registered builtin functions.
    41  var Funcs = map[string]Func{
    42  	// common functions
    43  	"coalesce": {builtinCoalesce, 1, -1},
    44  	"isnull":   {builtinIsNull, 1, 1},
    45  
    46  	// math functions
    47  	"abs":   {builtinAbs, 1, 1},
    48  	"pow":   {builtinPow, 2, 2},
    49  	"power": {builtinPow, 2, 2},
    50  	"rand":  {builtinRand, 0, 1},
    51  	"round": {builtinRound, 1, 2},
    52  
    53  	// time functions
    54  	"curdate":           {builtinCurrentDate, 0, 0},
    55  	"current_date":      {builtinCurrentDate, 0, 0},
    56  	"current_time":      {builtinCurrentTime, 0, 1},
    57  	"current_timestamp": {builtinNow, 0, 1},
    58  	"curtime":           {builtinCurrentTime, 0, 1},
    59  	"date":              {builtinDate, 1, 1},
    60  	"day":               {builtinDay, 1, 1},
    61  	"dayname":           {builtinDayName, 1, 1},
    62  	"dayofmonth":        {builtinDayOfMonth, 1, 1},
    63  	"dayofweek":         {builtinDayOfWeek, 1, 1},
    64  	"dayofyear":         {builtinDayOfYear, 1, 1},
    65  	"hour":              {builtinHour, 1, 1},
    66  	"microsecond":       {builtinMicroSecond, 1, 1},
    67  	"minute":            {builtinMinute, 1, 1},
    68  	"month":             {builtinMonth, 1, 1},
    69  	"now":               {builtinNow, 0, 1},
    70  	"second":            {builtinSecond, 1, 1},
    71  	"sysdate":           {builtinSysDate, 0, 1},
    72  	"utc_date":          {builtinUTCDate, 0, 0},
    73  	"week":              {builtinWeek, 1, 2},
    74  	"weekday":           {builtinWeekDay, 1, 1},
    75  	"weekofyear":        {builtinWeekOfYear, 1, 1},
    76  	"year":              {builtinYear, 1, 1},
    77  	"yearweek":          {builtinYearWeek, 1, 2},
    78  	"extract":           {builtinExtract, 2, 2},
    79  	"date_arith":        {builtinDateArith, 3, 3},
    80  
    81  	// string functions
    82  	"ascii":           {builtinASCII, 1, 1},
    83  	"concat":          {builtinConcat, 1, -1},
    84  	"concat_ws":       {builtinConcatWS, 2, -1},
    85  	"left":            {builtinLeft, 2, 2},
    86  	"length":          {builtinLength, 1, 1},
    87  	"lower":           {builtinLower, 1, 1},
    88  	"lcase":           {builtinLower, 1, 1},
    89  	"ltrim":           {trimFn(strings.TrimLeft, spaceChars), 1, 1},
    90  	"repeat":          {builtinRepeat, 2, 2},
    91  	"upper":           {builtinUpper, 1, 1},
    92  	"ucase":           {builtinUpper, 1, 1},
    93  	"replace":         {builtinReplace, 3, 3},
    94  	"rtrim":           {trimFn(strings.TrimRight, spaceChars), 1, 1},
    95  	"strcmp":          {builtinStrcmp, 2, 2},
    96  	"convert":         {builtinConvert, 2, 2},
    97  	"substring":       {builtinSubstring, 2, 3},
    98  	"substring_index": {builtinSubstringIndex, 3, 3},
    99  	"locate":          {builtinLocate, 2, 3},
   100  	"trim":            {builtinTrim, 1, 3},
   101  	"reverse":         {builtinReverse, 1, 1},
   102  
   103  	// information functions
   104  	"current_user":  {builtinCurrentUser, 0, 0},
   105  	"database":      {builtinDatabase, 0, 0},
   106  	"found_rows":    {builtinFoundRows, 0, 0},
   107  	"user":          {builtinUser, 0, 0},
   108  	"connection_id": {builtinConnectionID, 0, 0},
   109  	"version":       {builtinVersion, 0, 0},
   110  
   111  	// control functions
   112  	"if":     {builtinIf, 3, 3},
   113  	"ifnull": {builtinIfNull, 2, 2},
   114  	"nullif": {builtinNullIf, 2, 2},
   115  }
   116  
   117  // See: http://dev.mysql.com/doc/refman/5.7/en/comparison-operators.html#function_coalesce
   118  func builtinCoalesce(args []types.Datum, ctx context.Context) (d types.Datum, err error) {
   119  	for _, d = range args {
   120  		if d.Kind() != types.KindNull {
   121  			return d, nil
   122  		}
   123  	}
   124  	return d, nil
   125  }
   126  
   127  // See: https://dev.mysql.com/doc/refman/5.7/en/comparison-operators.html#function_isnull
   128  func builtinIsNull(args []types.Datum, _ context.Context) (d types.Datum, err error) {
   129  	if args[0].Kind() == types.KindNull {
   130  		d.SetInt64(1)
   131  	} else {
   132  		d.SetInt64(0)
   133  	}
   134  	return d, nil
   135  }