string_functions.go 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354
  1. package engine
  2. import (
  3. "fmt"
  4. "math"
  5. "strings"
  6. "github.com/seaweedfs/seaweedfs/weed/pb/schema_pb"
  7. )
  8. // ===============================
  9. // STRING FUNCTIONS
  10. // ===============================
  11. // Length returns the length of a string
  12. func (e *SQLEngine) Length(value *schema_pb.Value) (*schema_pb.Value, error) {
  13. if value == nil {
  14. return nil, fmt.Errorf("LENGTH function requires non-null value")
  15. }
  16. str, err := e.valueToString(value)
  17. if err != nil {
  18. return nil, fmt.Errorf("LENGTH function conversion error: %v", err)
  19. }
  20. length := int64(len(str))
  21. return &schema_pb.Value{
  22. Kind: &schema_pb.Value_Int64Value{Int64Value: length},
  23. }, nil
  24. }
  25. // Upper converts a string to uppercase
  26. func (e *SQLEngine) Upper(value *schema_pb.Value) (*schema_pb.Value, error) {
  27. if value == nil {
  28. return nil, fmt.Errorf("UPPER function requires non-null value")
  29. }
  30. str, err := e.valueToString(value)
  31. if err != nil {
  32. return nil, fmt.Errorf("UPPER function conversion error: %v", err)
  33. }
  34. return &schema_pb.Value{
  35. Kind: &schema_pb.Value_StringValue{StringValue: strings.ToUpper(str)},
  36. }, nil
  37. }
  38. // Lower converts a string to lowercase
  39. func (e *SQLEngine) Lower(value *schema_pb.Value) (*schema_pb.Value, error) {
  40. if value == nil {
  41. return nil, fmt.Errorf("LOWER function requires non-null value")
  42. }
  43. str, err := e.valueToString(value)
  44. if err != nil {
  45. return nil, fmt.Errorf("LOWER function conversion error: %v", err)
  46. }
  47. return &schema_pb.Value{
  48. Kind: &schema_pb.Value_StringValue{StringValue: strings.ToLower(str)},
  49. }, nil
  50. }
  51. // Trim removes leading and trailing whitespace from a string
  52. func (e *SQLEngine) Trim(value *schema_pb.Value) (*schema_pb.Value, error) {
  53. if value == nil {
  54. return nil, fmt.Errorf("TRIM function requires non-null value")
  55. }
  56. str, err := e.valueToString(value)
  57. if err != nil {
  58. return nil, fmt.Errorf("TRIM function conversion error: %v", err)
  59. }
  60. return &schema_pb.Value{
  61. Kind: &schema_pb.Value_StringValue{StringValue: strings.TrimSpace(str)},
  62. }, nil
  63. }
  64. // LTrim removes leading whitespace from a string
  65. func (e *SQLEngine) LTrim(value *schema_pb.Value) (*schema_pb.Value, error) {
  66. if value == nil {
  67. return nil, fmt.Errorf("LTRIM function requires non-null value")
  68. }
  69. str, err := e.valueToString(value)
  70. if err != nil {
  71. return nil, fmt.Errorf("LTRIM function conversion error: %v", err)
  72. }
  73. return &schema_pb.Value{
  74. Kind: &schema_pb.Value_StringValue{StringValue: strings.TrimLeft(str, " \t\n\r")},
  75. }, nil
  76. }
  77. // RTrim removes trailing whitespace from a string
  78. func (e *SQLEngine) RTrim(value *schema_pb.Value) (*schema_pb.Value, error) {
  79. if value == nil {
  80. return nil, fmt.Errorf("RTRIM function requires non-null value")
  81. }
  82. str, err := e.valueToString(value)
  83. if err != nil {
  84. return nil, fmt.Errorf("RTRIM function conversion error: %v", err)
  85. }
  86. return &schema_pb.Value{
  87. Kind: &schema_pb.Value_StringValue{StringValue: strings.TrimRight(str, " \t\n\r")},
  88. }, nil
  89. }
  90. // Substring extracts a substring from a string
  91. func (e *SQLEngine) Substring(value *schema_pb.Value, start *schema_pb.Value, length ...*schema_pb.Value) (*schema_pb.Value, error) {
  92. if value == nil || start == nil {
  93. return nil, fmt.Errorf("SUBSTRING function requires non-null value and start position")
  94. }
  95. str, err := e.valueToString(value)
  96. if err != nil {
  97. return nil, fmt.Errorf("SUBSTRING function value conversion error: %v", err)
  98. }
  99. startPos, err := e.valueToInt64(start)
  100. if err != nil {
  101. return nil, fmt.Errorf("SUBSTRING function start position conversion error: %v", err)
  102. }
  103. // Convert to 0-based indexing (SQL uses 1-based)
  104. if startPos < 1 {
  105. startPos = 1
  106. }
  107. startIdx := int(startPos - 1)
  108. if startIdx >= len(str) {
  109. return &schema_pb.Value{
  110. Kind: &schema_pb.Value_StringValue{StringValue: ""},
  111. }, nil
  112. }
  113. var result string
  114. if len(length) > 0 && length[0] != nil {
  115. lengthVal, err := e.valueToInt64(length[0])
  116. if err != nil {
  117. return nil, fmt.Errorf("SUBSTRING function length conversion error: %v", err)
  118. }
  119. if lengthVal <= 0 {
  120. result = ""
  121. } else {
  122. if lengthVal > int64(math.MaxInt) || lengthVal < int64(math.MinInt) {
  123. // If length is out-of-bounds for int, take substring from startIdx to end
  124. result = str[startIdx:]
  125. } else {
  126. // Safe conversion after bounds check
  127. endIdx := startIdx + int(lengthVal)
  128. if endIdx > len(str) {
  129. endIdx = len(str)
  130. }
  131. result = str[startIdx:endIdx]
  132. }
  133. }
  134. } else {
  135. result = str[startIdx:]
  136. }
  137. return &schema_pb.Value{
  138. Kind: &schema_pb.Value_StringValue{StringValue: result},
  139. }, nil
  140. }
  141. // Concat concatenates multiple strings
  142. func (e *SQLEngine) Concat(values ...*schema_pb.Value) (*schema_pb.Value, error) {
  143. if len(values) == 0 {
  144. return &schema_pb.Value{
  145. Kind: &schema_pb.Value_StringValue{StringValue: ""},
  146. }, nil
  147. }
  148. var result strings.Builder
  149. for i, value := range values {
  150. if value == nil {
  151. continue // Skip null values
  152. }
  153. str, err := e.valueToString(value)
  154. if err != nil {
  155. return nil, fmt.Errorf("CONCAT function value %d conversion error: %v", i, err)
  156. }
  157. result.WriteString(str)
  158. }
  159. return &schema_pb.Value{
  160. Kind: &schema_pb.Value_StringValue{StringValue: result.String()},
  161. }, nil
  162. }
  163. // Replace replaces all occurrences of a substring with another substring
  164. func (e *SQLEngine) Replace(value, oldStr, newStr *schema_pb.Value) (*schema_pb.Value, error) {
  165. if value == nil || oldStr == nil || newStr == nil {
  166. return nil, fmt.Errorf("REPLACE function requires non-null values")
  167. }
  168. str, err := e.valueToString(value)
  169. if err != nil {
  170. return nil, fmt.Errorf("REPLACE function value conversion error: %v", err)
  171. }
  172. old, err := e.valueToString(oldStr)
  173. if err != nil {
  174. return nil, fmt.Errorf("REPLACE function old string conversion error: %v", err)
  175. }
  176. new, err := e.valueToString(newStr)
  177. if err != nil {
  178. return nil, fmt.Errorf("REPLACE function new string conversion error: %v", err)
  179. }
  180. result := strings.ReplaceAll(str, old, new)
  181. return &schema_pb.Value{
  182. Kind: &schema_pb.Value_StringValue{StringValue: result},
  183. }, nil
  184. }
  185. // Position returns the position of a substring in a string (1-based, 0 if not found)
  186. func (e *SQLEngine) Position(substring, value *schema_pb.Value) (*schema_pb.Value, error) {
  187. if substring == nil || value == nil {
  188. return nil, fmt.Errorf("POSITION function requires non-null values")
  189. }
  190. str, err := e.valueToString(value)
  191. if err != nil {
  192. return nil, fmt.Errorf("POSITION function string conversion error: %v", err)
  193. }
  194. substr, err := e.valueToString(substring)
  195. if err != nil {
  196. return nil, fmt.Errorf("POSITION function substring conversion error: %v", err)
  197. }
  198. pos := strings.Index(str, substr)
  199. if pos == -1 {
  200. pos = 0 // SQL returns 0 for not found
  201. } else {
  202. pos = pos + 1 // Convert to 1-based indexing
  203. }
  204. return &schema_pb.Value{
  205. Kind: &schema_pb.Value_Int64Value{Int64Value: int64(pos)},
  206. }, nil
  207. }
  208. // Left returns the leftmost characters of a string
  209. func (e *SQLEngine) Left(value *schema_pb.Value, length *schema_pb.Value) (*schema_pb.Value, error) {
  210. if value == nil || length == nil {
  211. return nil, fmt.Errorf("LEFT function requires non-null values")
  212. }
  213. str, err := e.valueToString(value)
  214. if err != nil {
  215. return nil, fmt.Errorf("LEFT function string conversion error: %v", err)
  216. }
  217. lengthVal, err := e.valueToInt64(length)
  218. if err != nil {
  219. return nil, fmt.Errorf("LEFT function length conversion error: %v", err)
  220. }
  221. if lengthVal <= 0 {
  222. return &schema_pb.Value{
  223. Kind: &schema_pb.Value_StringValue{StringValue: ""},
  224. }, nil
  225. }
  226. if lengthVal > int64(len(str)) {
  227. return &schema_pb.Value{
  228. Kind: &schema_pb.Value_StringValue{StringValue: str},
  229. }, nil
  230. }
  231. if lengthVal > int64(math.MaxInt) || lengthVal < int64(math.MinInt) {
  232. return &schema_pb.Value{
  233. Kind: &schema_pb.Value_StringValue{StringValue: str},
  234. }, nil
  235. }
  236. // Safe conversion after bounds check
  237. return &schema_pb.Value{
  238. Kind: &schema_pb.Value_StringValue{StringValue: str[:int(lengthVal)]},
  239. }, nil
  240. }
  241. // Right returns the rightmost characters of a string
  242. func (e *SQLEngine) Right(value *schema_pb.Value, length *schema_pb.Value) (*schema_pb.Value, error) {
  243. if value == nil || length == nil {
  244. return nil, fmt.Errorf("RIGHT function requires non-null values")
  245. }
  246. str, err := e.valueToString(value)
  247. if err != nil {
  248. return nil, fmt.Errorf("RIGHT function string conversion error: %v", err)
  249. }
  250. lengthVal, err := e.valueToInt64(length)
  251. if err != nil {
  252. return nil, fmt.Errorf("RIGHT function length conversion error: %v", err)
  253. }
  254. if lengthVal <= 0 {
  255. return &schema_pb.Value{
  256. Kind: &schema_pb.Value_StringValue{StringValue: ""},
  257. }, nil
  258. }
  259. if lengthVal > int64(len(str)) {
  260. return &schema_pb.Value{
  261. Kind: &schema_pb.Value_StringValue{StringValue: str},
  262. }, nil
  263. }
  264. if lengthVal > int64(math.MaxInt) || lengthVal < int64(math.MinInt) {
  265. return &schema_pb.Value{
  266. Kind: &schema_pb.Value_StringValue{StringValue: str},
  267. }, nil
  268. }
  269. // Safe conversion after bounds check
  270. startPos := len(str) - int(lengthVal)
  271. return &schema_pb.Value{
  272. Kind: &schema_pb.Value_StringValue{StringValue: str[startPos:]},
  273. }, nil
  274. }
  275. // Reverse reverses a string
  276. func (e *SQLEngine) Reverse(value *schema_pb.Value) (*schema_pb.Value, error) {
  277. if value == nil {
  278. return nil, fmt.Errorf("REVERSE function requires non-null value")
  279. }
  280. str, err := e.valueToString(value)
  281. if err != nil {
  282. return nil, fmt.Errorf("REVERSE function conversion error: %v", err)
  283. }
  284. // Reverse the string rune by rune to handle Unicode correctly
  285. runes := []rune(str)
  286. for i, j := 0, len(runes)-1; i < j; i, j = i+1, j-1 {
  287. runes[i], runes[j] = runes[j], runes[i]
  288. }
  289. return &schema_pb.Value{
  290. Kind: &schema_pb.Value_StringValue{StringValue: string(runes)},
  291. }, nil
  292. }