ttl.go 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. // Copyright 2019 Yunion
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package clickhouse
  15. import (
  16. "strconv"
  17. "strings"
  18. "yunion.io/x/pkg/errors"
  19. )
  20. type sTTL struct {
  21. // number of time interval
  22. Count int
  23. // TTL in month, day or hour
  24. Unit string
  25. }
  26. type sColumnTTL struct {
  27. sTTL
  28. ColName string
  29. }
  30. func parseTTL(ttl string) (sTTL, error) {
  31. ret := sTTL{}
  32. if len(ttl) == 0 {
  33. return ret, errors.Wrap(errors.ErrInvalidStatus, "not valid ttl")
  34. }
  35. unit := ""
  36. switch ttl[len(ttl)-1] {
  37. case 'h':
  38. unit = "HOUR"
  39. case 'd':
  40. unit = "DAY"
  41. case 'm':
  42. unit = "MONTH"
  43. }
  44. if len(unit) == 0 {
  45. unit = "MONTH"
  46. } else {
  47. ttl = ttl[:len(ttl)-1]
  48. }
  49. intv, err := strconv.ParseInt(ttl, 10, 64)
  50. if err != nil {
  51. return ret, errors.Wrap(errors.ErrInvalidStatus, "not valid ttl")
  52. }
  53. ret.Count = int(intv)
  54. ret.Unit = unit
  55. return ret, nil
  56. }
  57. // created_at + INTERVAL 3 MONTH
  58. func parseTTLExpression(expr string) (sColumnTTL, error) {
  59. parts := strings.Split(expr, " ")
  60. ret := sColumnTTL{}
  61. if len(parts) == 5 && parts[1] == "+" && strings.HasPrefix(parts[2], "INT") {
  62. ret.ColName = parts[0]
  63. if ret.ColName[0] == '`' || ret.ColName[0] == '\'' {
  64. ret.ColName = ret.ColName[1 : len(ret.ColName)-1]
  65. }
  66. switch parts[4] {
  67. case "MONTH", "DAY", "HOUR":
  68. ret.Unit = parts[4]
  69. default:
  70. return ret, errors.Wrap(errors.ErrInvalidStatus, "invalid time unit, MONTH, DAY or HOUR only")
  71. }
  72. var err error
  73. ret.Count, err = strconv.Atoi(parts[3])
  74. if err != nil {
  75. return ret, errors.Wrap(err, "invalid interval count")
  76. }
  77. return ret, nil
  78. } else if len(parts) == 3 && parts[1] == "+" && strings.HasPrefix(parts[2], "toInterval") {
  79. ret.ColName = parts[0]
  80. if ret.ColName[0] == '`' || ret.ColName[0] == '\'' {
  81. ret.ColName = ret.ColName[1 : len(ret.ColName)-1]
  82. }
  83. intvlCnts := strings.Split(parts[2][len("toInterval"):len(parts[2])-1], "(")
  84. cnt, err := strconv.Atoi(intvlCnts[1])
  85. if err != nil {
  86. return ret, errors.Wrapf(err, "strconv.Atoi %s", intvlCnts[1])
  87. }
  88. ret.Count = cnt
  89. switch intvlCnts[0] {
  90. case "Year":
  91. ret.Unit = "MONTH"
  92. ret.Count *= 12
  93. case "Quarter":
  94. ret.Unit = "MONTH"
  95. ret.Count *= 3
  96. case "Month":
  97. ret.Unit = "MONTH"
  98. case "Week":
  99. ret.Unit = "DAY"
  100. ret.Count *= 7
  101. case "Day":
  102. ret.Unit = "DAY"
  103. case "Hour":
  104. ret.Unit = "HOUR"
  105. default:
  106. return ret, errors.Wrapf(errors.ErrInvalidStatus, "invalid interval %s", intvlCnts[0])
  107. }
  108. return ret, nil
  109. } else {
  110. return ret, errors.Wrapf(errors.ErrInvalidStatus, "invalid format %s", expr)
  111. }
  112. }