You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

130 lines
4.5 KiB

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